DWG Export Filename

Here is a question handled by Joe Ye that I thought might be of general interest.

It makes use of the Revit SDK sample ImportExport converted to VB.NET. We mentioned the issue of converting Revit SDK samples from C# to VB in one post, and Adam Nagy later presented a more complete port.

Question: I am making an application to export Revit views to DWG files. I used the ImportExport sample converted to VB.NET as a basis. I am having trouble defining filenames other than the built-in solution. The syntax for defining the DWG filename for a given view seems to be:

[FileName] & " - " & [ViewType] & " - " & [ViewName] 

The filename can be changed, but the other substrings seem to be hardcoded, or at least I can't find where they are defined. I would like the FileName and ViewType substrings to be optional and maybe replaced by some other project defined prefix. Is this possible through the API?

If not, a workaround could be to rename the DWG files afterwards, but I hope to avoid that.

Answer: In this case, you are using one of the overloads of the Export method taking four arguments, the fourth of which specifies the type of export required. The first three are folder, name, and views:

As the documentation says, the exported DWG file name is in fact generated using a hardcoded algorithm inside Revit if the view set contains more than one single element.

If only one view is specified, then the filename string constructed from three substrings provided by specific variables is used. It is defined in the module ExportData.vb, in the Initialize function:

Private Sub Initialize()
  ' The directory into which the file will be exported 
  Dim dllFilePath As String _
    = Assembly.GetExecutingAssembly().Location
  m_exportFolder = Path.GetDirectoryName(dllFilePath)
  ' The file name to be used by export 
  Dim docTitle As String = m_activeDoc.Title
  Dim v As View = m_activeDoc.ActiveView
  Dim viewName As String = v.Name
  Dim viewType As String = v.ViewType.ToString()
  m_exportFileName = docTitle _
    + "-" + viewType _
    + "-" + viewName _
    + "." + getExtension().ToString()
  ' Whether current active view is 3D view 
  If v.ViewType = Enums.ViewType.ThreeD Then
    m_is3DView = True
    m_is3DView = False
  End If
End Sub

Therefore, you can easily make FileName and ViewType optional or replace them by some other prefix. Simply change the string values in the definition of m_exportFileName as you please, for instance like this:

m_exportFileName = docTitle + "-" + “aaaa” + “.” + getExtension().ToString

Note that changes to the specified filename have no effect if more than one view is given in the third argument to the Export method. If the third argument includes more than one view, Revit will automatically name each exported DWG file using a hard-coded rule including the second argument to the Export method as a prefix. If the third argument includes just one single view, then the file name passed in is used. So the workaround to use your own naming rule is to export only one view at a time. In case of multiple views, you can export them one by one in a loop.

This information obviously also applies to the other overloads of the Export method taking a ViewSet argument, including export to:

Thank you very much Joe for this answer!