Inventor 2024 VBA Revit Exporting

Inventor 2024 VBA Revit Exporting

mattschmieding
Explorer Explorer
315 Views
3 Replies
Message 1 of 4

Inventor 2024 VBA Revit Exporting

mattschmieding
Explorer
Explorer

In Inventor 2024, I am trying to use a VBA macro to open a Solidworks assembly file on my computer, save it as a Revit file (which I can do manually using File>Save As>Revit) to my computer (along with a few other file types), and then the program ends. However, I can't seem to find a method to save the Revit file (.rvt) to my disk, I can only create objects within the code. See the middle section of the code below, lines 53-70).

 

This help doc is all I have: https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-RevitExport.

 

Any help is appreciated 🙂

 

Sub File_Exports_Macro()
    ' Declare the application and document objects
    Dim invApp As Application
    Dim invDoc As Document
    Dim swFilePath As String
    Dim savePath As String
    Dim partNum As String
    Dim fileExtension As String
    Dim fullFilePath As String

    ' Initialize the Inventor application
    Set invApp = ThisApplication

    ' Prompt user for the SolidWorks file path to open
    swFilePath = InputBox("Enter the full path of the SolidWorks file to open:", "Open SolidWorks File")
    If swFilePath = "" Then
        MsgBox "No file path provided. Operation cancelled.", vbExclamation
        Exit Sub
    End If

    ' Open the SolidWorks document
    On Error Resume Next
    Set invDoc = invApp.Documents.Open(swFilePath)
    If invDoc Is Nothing Then
        MsgBox "Failed to open the SolidWorks file. Please check the path and try again.", vbCritical
        Exit Sub
    End If
    On Error GoTo 0

    ' Prompt user for the part number
    partNum = InputBox("Enter the part number:", "Part Number")
    If partNum = "" Then
        MsgBox "No part number provided. Operation cancelled.", vbExclamation
        invDoc.Close (True)
        Exit Sub
    End If

    ' Prompt user for the save directory
    savePath = InputBox("Enter the directory path where you want to save the converted files:", "Save Directory")
    If savePath = "" Then
        MsgBox "No save path provided. Operation cancelled.", vbExclamation
        invDoc.Close (True)
        Exit Sub
    End If

    ' Ensure the savePath ends with a backslash
    If Right(savePath, 1) <> "\" Then
        savePath = savePath & "\"
    End If



    ' Save as .RVT
    On Error Resume Next
    Dim oAssemblyDoc As AssemblyDocument
    Set oAssemblyDoc = invDoc
    If oAssemblyDoc.ComponentDefinition.ModelStates.ActiveModelState.ModelStateType = ModelStateTypeEnum.kSubstituteModelStateType Then
        oAssemblyDoc.ComponentDefinition.ModelStates.Item(1).Activate
        Set oAssemblyDoc = ThisApplication.ActiveDocument
    End If

    Dim oRevitExportDef As RevitExportDefinition
    Set oRevitExportDef = oAssemblyDoc.ComponentDefinition.RevitExports.CreateDefinition
    oRevitExportDef.Location = savePath
    oRevitExportDef.FileName = partNum & " RVT.rvt"
    oRevitExportDef.Structure = kEachTopLevelComponentStructure
    oRevitExportDef.EnableUpdating = True

    ' Create RevitExport
    ' invDoc.SaveAs doesn't work, what goes here??



    ' Save as .DWG
    fileExtension = ".dwg"
    fullFilePath = savePath & partNum & " DWG" & fileExtension
    invDoc.SaveAs fullFilePath, True

    ' Save as .IGES
    fileExtension = ".iges"
    fullFilePath = savePath & partNum & " IGES" & fileExtension
    invDoc.SaveAs fullFilePath, True

    ' Save as .STL
    fileExtension = ".stl"
    fullFilePath = savePath & partNum & " STL" & fileExtension
    invDoc.SaveAs fullFilePath, True

    ' Close the document
    invDoc.Close (True)

    ' Inform the user that the operation is complete
    MsgBox "File converted and saved as .RVT, .DWG, .IGES, and .STL successfully!"

End Sub

 

0 Likes
316 Views
3 Replies
Replies (3)
Message 2 of 4

mat_hijs
Collaborator
Collaborator

You don't need to do a save as to export to RVT. Here is the VBA sample:

Sub CreateRevitExportSample()
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    ' Actiate the Master model state if the active model state is substitute.
    If oDoc.ComponentDefinition.ModelStates.ActiveModelState.ModelStateType = ModelStateTypeEnum.kSubstituteModelStateType Then
        oDoc.ComponentDefinition.ModelStates.Item(1).Activate
        Set oDoc = ThisApplication.ActiveDocument
    End If
    
    Dim oRevitExportDef As RevitExportDefinition
    Set oRevitExportDef = oDoc.ComponentDefinition.RevitExports.CreateDefinition
    
    oRevitExportDef.Location = "C:\Temp"
    oRevitExportDef.FileName = "MyRevitExport.rvt"
    oRevitExportDef.Structure = kEachTopLevelComponentStructure
    oRevitExportDef.EnableUpdating = True
    
    ' Create RevitExport.
    Dim oRevitExport As RevitExport
    Set oRevitExport = oDoc.ComponentDefinition.RevitExports.Add(oRevitExportDef)
End Sub

The actual export happens at the last line of the sub. You might want to add some checks because I believe this will fail if the file already exists. Here's some code I wrote a while ago that checks if it already exists and updates it instead if it does. This is iLogic code though, you'd need to convert it to VBA.

' Set a reference to the Assembly Document
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

' Set a reference to the Assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

' Set the File Path and File Name
Dim sFilePath As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
Dim sFileName As String = iProperties.Value("Project", "Part Number") & ".rvt"
Dim sFullFileName As String = sFilePath & "\" & sFileName

' Try to get the first Revit Export, if it's not found create one
Dim oRevitExport As RevitExport
Try
	oRevitExport = oAsmCompDef.RevitExports.Item(1)
Catch
	' Create RevitExportDefinition
	Dim oRevitExportDef As RevitExportDefinition = oAsmCompDef.RevitExports.CreateDefinition
	
	oRevitExportDef.Location = sFilePath
	oRevitExportDef.FileName = sFileName
	oRevitExportDef.ActiveDesignViewRepresentation = "Default"
	oRevitExportDef.IsAssociativeDesignView = True
	oRevitExportDef.Structure = kEachTopLevelComponentStructure
	oRevitExportDef.UseColorOverrideFromSourceComponent = False
	oRevitExportDef.EnableUpdating = True
	
	' Check if the file exists, if it does, delete it
	If System.IO.File.Exists(sFullFileName) = True Then System.IO.File.Delete(sFullFileName)
	
	' Create RevitExport
	oRevitExport = oAsmCompDef.RevitExports.Add(oRevitExportDef)
	
	Exit Sub
	
End Try

' Update the Revit Export if it was already there
oRevitExport.Update

0 Likes
Message 3 of 4

mattschmieding
Explorer
Explorer

I have no idea how I missed that, that seems much more obvious now. I appreciate it, thank you so much!

0 Likes
Message 4 of 4

mattschmieding
Explorer
Explorer

Another thing popped up: I keep getting the error "Method 'Add' of object 'RevitExports' failed" for line 83 even though there is no existing Revit model. Is there something I need to add? Thanks.

Sub File_Exports_Macro()
    ' Declare the application and document objects
    Dim invApp As Application
    Dim invDoc As Document
    Dim swFilePath As String
    Dim savePath As String
    Dim partNum As String
    Dim fileExtension As String
    Dim fullFilePath As String

    ' Initialize the Inventor application
    Set invApp = ThisApplication

    ' Prompt user for the SolidWorks file path to open
    swFilePath = InputBox("Enter the full path of the SolidWorks file to open:", "Open SolidWorks File")
    If swFilePath = "" Then
        MsgBox "No file path provided. Operation cancelled.", vbExclamation
        Exit Sub
    End If

    ' Open the SolidWorks document
    On Error Resume Next
    Set invDoc = invApp.Documents.Open(swFilePath)
    If invDoc Is Nothing Then
        MsgBox "Failed to open the SolidWorks file. Please check the path and try again.", vbCritical
        Exit Sub
    End If
    On Error GoTo 0

    ' Prompt user for the part number
    partNum = InputBox("Enter the part number:", "Part Number")
    If partNum = "" Then
        MsgBox "No part number provided. Operation cancelled.", vbExclamation
        invDoc.Close (True)
        Exit Sub
    End If

    ' Prompt user for the save directory
    savePath = InputBox("Enter the directory path where you want to save the converted files:", "Save Directory")
    If savePath = "" Then
        MsgBox "No save path provided. Operation cancelled.", vbExclamation
        invDoc.Close (True)
        Exit Sub
    End If

    ' Ensure the savePath ends with a backslash
    If Right(savePath, 1) <> "\" Then
        savePath = savePath & "\"
    End If



    ' Save as .RVT
    Dim oDoc As AssemblyDocument
    Set oDoc = invDoc
    
    ' Actiate the Master model state if the active model state is substitute
    If oDoc.ComponentDefinition.ModelStates.ActiveModelState.ModelStateType = ModelStateTypeEnum.kSubstituteModelStateType Then
        oDoc.ComponentDefinition.ModelStates.Item(1).Activate
        Set oDoc = ThisApplication.ActiveDocument
    End If
    
    Dim oRevitExportDef As RevitExportDefinition
    Set oRevitExportDef = oDoc.ComponentDefinition.RevitExports.CreateDefinition
    
    oRevitExportDef.Location = savePath
    oRevitExportDef.FileName = partNum & " RVT" & ".rvt"
    
    oRevitExportDef.Structure = kEachTopLevelComponentStructure
    oRevitExportDef.EnableUpdating = True
    oRevitExportDef.EnvelopesReplaceStyle() = kNoneReplaceStyle
    ' 5 in = 12.7 cm
    oRevitExportDef.RemovePartsSize() = 12.7
    oRevitExportDef.RemoveHolesStyle() = kSimplificationRemoveNone
    oRevitExportDef.RemoveFilletsStyle() = kSimplificationRemoveNone
    oRevitExportDef.RemoveChamfersStyle() = kSimplificationRemoveNone
    oRevitExportDef.RemovePocketsStyle() = kSimplificationRemoveNone
    oRevitExportDef.RemoveEmbossesStyle() = kSimplificationRemoveNone
    oRevitExportDef.RemoveTunnelsStyle() = kSimplificationRemoveAll
    
    ' Create RevitExport
    Dim oRevitExport As RevitExport
    Set oRevitExport = oDoc.ComponentDefinition.RevitExports.Add(oRevitExportDef)



    ' Save as .DWG
    fileExtension = ".dwg"
    fullFilePath = savePath & partNum & " DWG" & fileExtension
    invDoc.SaveAs fullFilePath, True

    ' Save as .IGES
    fileExtension = ".iges"
    fullFilePath = savePath & partNum & " IGES" & fileExtension
    invDoc.SaveAs fullFilePath, True

    ' Save as .STL
    fileExtension = ".stl"
    fullFilePath = savePath & partNum & " STL" & fileExtension
    invDoc.SaveAs fullFilePath, True

    ' Close the document
    invDoc.Close (True)

    ' Inform the user that the operation is complete
    MsgBox "File converted and saved as .RVT, .DWG, .IGES, and .STL successfully!"

End Sub

 

0 Likes