ImportExport Update

I mentioned some features of the ImportExport SDK sample in previous posts, e.g. on

porting it from C# to VB
, setting the

DWG export filename
,

modifying the DWF export filename
, and

exporting a 3D view to 2D DWF
.

Now Kurt Schrempp ran into a little problem with this sample.
He says: I solved my problem.
Here is the issue:
When you try to export as DWG, it requires and gives you options to define the layer mapping.
If you don’t open up the layer mapping options dialogue, no default settings are set, and a null argument exception is thrown.

Kurt provided a slightly updated version which fixes that and some other minor problems of the standard implementation.
The main fix is to automatically open the dialogue so that the default options are instantiated and set.
If this step is not performed, and the associated property is left set to its default value of null, the sample fails in Revit 2012.

In Kurt’s words:
There are actually two errors due to similar things.
One is based on the layer options, the other on the selected views when selecting more than just the current view.

To reproduce the layer bug, launch ImportExport > Select Export > Select DWG format > Click OK > Select Directory > Click Save.

Now, because the options button is optional, and it is skipped by default, the error should come up.

To reproduce the view bug, launch ImportExport > Select Export > Select DWG format > Click OK > Select Directory > Select the “Selected views/sheets” Radio Button > Click Options > Click Ok > Click Save.

In this one, because the “…” button to select views is optional, no default views are set, similarly to the layer options issue, so a different error comes up.

Here is an image that illustrates a solution for this:

ImportExport fix

Here is the options button click handler implementation:


private void buttonOptions_Click(
  object sender,
  EventArgs e )
{
  // Export dwg
  if( m_exportData.ExportFormat
    == ExportFormat.DWG )
  {
    bool contain3DView = false;
 
    if( radioButtonCurrentView.Checked )
    {
      if( m_exportData.Is3DView )
      {
        contain3DView = true;
      }
    }
    else
    {
      if( m_exportData.SelectViewsData
        .Contain3DView )
      {
        contain3DView = true;
      }
    }
 
    ExportDWGData exportDWGData
      = m_exportData as ExportDWGData;
 
    using( ExportDWGOptionsForm exportOptionsForm
      = new ExportDWGOptionsForm(
        exportDWGData.ExportOptionsData,
        contain3DView ) )
    {
      exportOptionsForm.ShowDialog();
    }
  }
  // . . .
}

It sets up the DWGExportOptions parameter:


  DWGExportOptions dwgExportOptions = new DWGExportOptions();
  dwgExportOptions.ExportingAreas = m_exportOptionsData.ExportAreas;
  dwgExportOptions.ExportOfSolids = m_exportOptionsData.ExportSolid;
  dwgExportOptions.FileVersion = m_exportFileVersion;
  dwgExportOptions.LayerMapping = m_exportOptionsData.ExportLayerMapping;
  dwgExportOptions.LineScaling = m_exportOptionsData.ExportLineScaling;
  dwgExportOptions.MergedViews = m_exportOptionsData.ExportMergeFiles;
  dwgExportOptions.PropOverrides = m_exportOptionsData.ExportLayersAndProperties;
  dwgExportOptions.SharedCoords = m_exportOptionsData.ExportCoorSystem;
  dwgExportOptions.TargetUnit = m_exportOptionsData.ExportUnit;

These properties aren’t set until this window initializes.
So unless you click the “Options” button on the previous form, the values are null, thus throwing the exception.

All I did was make it so the options form displays as soon as the export form comes up, setting the defaults immediately.

Here is the code of my
modified version of the ImportExport SDK sample
which opens up the two windows automatically, so there are no null defaults.

It implements a couple of other small changes as well.
Here is a list of the differing files:

  • ExportData.cs – modify output filename
  • ExportDWGData.cs – rename output file
  • ExportWithViewsForm.cs – display layer mapping and options as discussed above
  • SelectViewsForm.cs – additional handling of 2D and 3D views

Many thanks to Kurt for these insights and sharing his update!

Avoid Casting Twice

Here is an additional little note on casting efficiency.

This code accesses the directory containing an external referenced file:


  Type T = es.Current.GetType();
  if( T == typeof( ImportInstance ) )
  {
    ImportInstance imp = es.Current
      as ImportInstance;
 
    if( imp.IsLinked )
    {
      ExternalFileReference el = ExternalFileUtils
        .GetExternalFileReference(
          actdoc, imp.GetTypeId() );
 
      if( el != null )
      {
        directory = ModelPathUtils
          .ConvertModelPathToUserVisiblePath(
            el.GetAbsolutePath() );
      }
    }
    else
      error = "The import instance you selected"
        + " is not linked into the project.";
  }

It can be simplified by using the ‘is’ statement, replacing the first two lines by the following more readable statement:


  if( es.Current is ImportInstance )

We can go one step further, though, because the next line is casting to an ImportInstance.
The sequence of statements saying ‘if x is Y then: Z z = x as Y’ is basically performing the cast twice over.
You can avoid that unneccessary cost by casting once only and testing the result agains null.
In this case, we can combine the first if statement with the cast and the second if statement, i.e. combine the line above with the one folowing it line and just say:


  ImportInstance imp = es.Current
    as ImportInstance;
 
  if( null != imp && imp.IsLinked )

This eliminates the first two lines altogether, leaving just this:


  ImportInstance imp = es.Current
    as ImportInstance;
 
  if( null != imp && imp.IsLinked )
  {
    ExternalFileReference el = ExternalFileUtils
      .GetExternalFileReference(
        actdoc, imp.GetTypeId() );
 
    if( el != null )
    {
      directory = ModelPathUtils
        .ConvertModelPathToUserVisiblePath(
          el.GetAbsolutePath() );
    }
  }
  else
  {
    error = "The import instance you selected"
      + " is not linked into the project.";
  }

For more details, you can refer to sections 8.2.2 ‘The is Operator’, 8.2.3 ‘The as Operator’, and especially 8.2.4 ‘The is Operator Versus the as Operator’ in this

C# tutorial
.


Comments

13 responses to “ImportExport Update”

  1. Hi,
    We have just implemented Revit in our office. And I made DWG Export Setup according to our company standard. But if I use – DWGExportOptions.GetPredefinedOptions(
    Document document,string setup)
    then – bool MergedViews { get; set; } returns always false, no matter how it is in the Setup. I need help.
    I’m just starting to learn C# and Revit API.
    best regards
    arch. N.Marinov

  2. Dear Niki,
    Congratulations on implementing Revit.
    There was an issue with this once upon a time, but it should be fixed in Revit 2012.
    What version are you using?
    Cheers, Jeremy.

  3. Hi
    We use Revit Architecture 2012.
    best regards

  4. I was wrong MergedViews always return as true. All linked rvt files are exported as external references, no matter how I put it in the DWG Export Setup.
    best regards

  5. Dear Niki,
    Well, then it should work :-)
    I see no other reports on such a problem, and it was looked at prior to the 2012 release…
    Cheers, Jeremy.

  6. //parameter : DWGExportOptions
    DWGExportOptions dwgExportOptions = DWGExportOptions.GetPredefinedOptions(m_activeDoc, “AIA2”);
    //Export
    exported = m_activeDoc.Export(m_exportFolder, m_exportFileName, views, dwgExportOptions);
    using this code Revit uses all settings from AIA2 setup, except whether to merge the views.
    best regards

  7. Dear Niki,
    Are you an ADN member?
    http://www.autodesk.com/joinadn
    If so, please submit a DevHelp Online request. Otherwise, hmm, don’t know what to do.
    Cheers, Jeremy.

  8. Dear Niki,
    Some interesting news on this:
    1. The merge views check box in the UI has no effect; views are never merged in the UI regardless of the setting.
    2. The MergedViews property in the API is initially always set to false, regardless of the UI setting.
    3. The good news: The MergedViews property in the API does do the job you would expect, e.g. control whether or not views are merged.
    It seems to be the only way to control this, since the UI check box does not.
    Thank you for bringing this to our attention!
    Cheers, Jeremy.

  9. hi
    in revit 2011 i was able to cast CADExportOptions type object to DWGExportOptions. but as i changed the dll to 2012 version its start giving me error:
    Cannot convert type ‘Autodesk.Revit.DB.CADExportOptions’ to ‘Autodesk.Revit.DB.DWGExportOptions’
    pls help.

  10. Dear Mnav,
    In the Revit 2012 API, DWGExportOptions is derived from ACADExportOptions, which in turn is based directly on the base class System.Object:
    System.Object

    Autodesk.Revit.DB.ACADExportOptions

    Autodesk.Revit.DB.DWGExportOptions
    Autodesk.Revit.DB.DXFExportOptions
    CADExportOptions is the parent class of the other export option classes for DGN, DWF and SAT, but not DWG, so they are now unrelated:
    System.Object
    Autodesk.Revit.DB.CADExportOptions
    Autodesk.Revit.DB.DGNExportOptions
    Autodesk.Revit.DB.DWFExportOptions
    Autodesk.Revit.DB.SATExportOptions
    In the Revit 2011 API, CADExportOptions was the parent class of ACADExportOptions as well, and thus the grandparent of DWGExportOptions:
    System.Object
    Autodesk.Revit.DB.CADExportOptions
    Autodesk.Revit.DB.ACADExportOptions
    Autodesk.Revit.DB.DGNExportOptions
    Autodesk.Revit.DB.DWFExportOptions
    Autodesk.Revit.DB.SATExportOptions
    So you will have to address whatever you are trying to achieve differently in 2012.
    Cheers, Jeremy.

  11. thanks…..

  12. Mira Saad Avatar
    Mira Saad

    Dear Sir,
    I am trying to customize the Revit 2012 Export to CAD to suit our company standards. I need the application to take the layer mapping as it is fixed in the Revit file and not being read from a txt file or from a standard. The users are editing the export settings in the revit files and I need the export application to read these settings.
    Please advise.

  13. Dear Mira,
    I am not aware of much customisation access to the built-in Revit CAD export functionality.
    I would suggest using it as is, and implementing your own add-in to export other additional data, such as the desired layer information, in parallel.
    At the other end of the process, e.g. from a stand-alone command line application or in the target CAD system, read in both the exported CAD file and your additional layer data information and merge the two in whatever way you like.
    For instance, if you are working with DWG, a stand-alone application could be implemented based on RealDWG, or you could use a .NET add-in inside of (or outside of, and driving) AutoCAD.
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading