Hi @augustin_roegiersTXZKR. I created an iLogic rule that you may be able to use for this task, but I have not tested it out yet. One detail I am not sure about yet, is if the 'new' assembly needs to be saved to disk 'before' we can export it to a STEP file. I did leave some code in there for saving the new assembly document, just in case that step is necessary. And I also left some code in there for closing the new assembly, and deleting its file after exporting it as a STEP file, just in case that is something you may need/want to do also, but I left those lines of code commented out for now, to be safe. Do not be intimidated by the code being broken up into multiple separate routines. This is partially necessary for a truly 'recursive' process (a process that can call itself to run again, to step down another level deeper). And it also helps keep the code processes modular (copyable and usable in other situations).
I left several comments in the code, but if you have questions about some of this stuff, feel free to ask them. And of course be cautious with any new code like this, and try it out on test files before trying it on production files to start with, just to be safe, because I have not tested the final code myself yet.
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbCritical, "WRONG DOCUMENT TYPE")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
If oOccs.Count = 0 Then Return 'if there were no components, then exit rule (nothing to do)
'specify the assembly template file to use when creating the new assembly
Dim sAsmTemplate As String = "C:\Temp\MyAssemblyTemplate.iam"
'create the new assembly document here
Dim oNewAsmDoc As AssemblyDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, sAsmTemplate, False)
'set the value of the 'shared' variable for the components collection of this new assembly
oNewOccs = oNewAsmDoc.ComponentDefinition.Occurrences
'run our custom recursive Sub routine here, which will process all levels of components in the original assembly
RecurseComponents(oOccs, AddressOf ProcessComponent)
'formulate full path and file name of the new STEP file we want to create
Dim sPath As String = System.IO.Path.GetDirectoryName(oADoc.FullFileName) 'no directory separator character at end
Dim sDirSep As Char = System.IO.Path.DirectorySeparatorChar
Dim sName As String = System.IO.Path.GetFileNameWithoutExtension(oADoc.FullFileName)
Dim sSTEP_FullFileName As String = sPath & sDirSep & sName & " - Sheet Metal Parts.stp"
'the new assembly may need to be 'saved' before it can be exported, not sure
Dim sNewAssemblyFile As String = sPath & sDirSep & sName & " - Sheet Metal Parts.iam"
oNewAsmDoc.SaveAs(sNewAssemblyFile, False)
'call custom Sub routine to export the 'New' assembly as STEP file
ExportAsSTEP(oNewAsmDoc, sSTEP_FullFileName)
'we could now close the new assembly, and delete the new assembly file
'oNewAsmDoc.Close(True) 'True = skip save
'System.IO.File.Delete(sNewAssemblyFile) 'to delete the new
MsgBox("This rule's work has finished.", vbInformation, "Job Is Done!")
End Sub
'variable declared between other routines, so that it will be 'shared' by other routines
Dim oNewOccs As ComponentOccurrences
Sub RecurseComponents(oComps As ComponentOccurrences, ComponentProcess As Action(Of ComponentOccurrence))
If oComps Is Nothing OrElse oComps.Count = 0 Then Return
For Each oComp As ComponentOccurrence In oComps
ComponentProcess(oComp)
If oComp.Suppressed = False AndAlso _
oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
RecurseComponents(oComp.Definition.Occurrences, ComponentProcess)
End If
Next
End Sub
Sub ProcessComponent(oComp As ComponentOccurrence)
If oComp Is Nothing OrElse oComp.Suppressed Then Return
'check if this component represents a sheet metal part, if not, return to calling routine
If Not TypeOf oComp.Definition Is SheetMetalComponentDefinition Then Return
Dim oCompDoc As Document = oComp.Definition.Document
'check the 'Cost Center' iProperty value
If oCompDoc.PropertySets.Item(3).Item(4).Value = "Sheet Metal" Then
Dim sFDN As String = oComp.ReferencedDocumentDescriptor.FullDocumentName
Dim oPos As Inventor.Matrix = ThisApplication.TransientGeometry.CreateMatrix
Dim oNewOcc As ComponentOccurrence = Nothing
Try : oNewOccs.Add(sFDN, oPos)
Catch : Logger.Error("Error adding new component for:" & vbCrLf & oFDN & vbCrLf & "to new assembly.")
End Try
End If
End Sub
Sub ExportAsSTEP(oDoc As Inventor.Document, sNewFullFileName As String)
Dim sSTEP_ClientID As String = "{90AF7F40-0C01-11D5-8E83-0010B541CD80}"
Dim oSTEP As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById(sSTEP_ClientID)
'create needed variables for translator
Dim oTO As TransientObjects = ThisApplication.TransientObjects
Dim oContext As TranslationContext = oTO.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = oTO.CreateNameValueMap
Dim oDataMedium As DataMedium = oTO.CreateDataMedium
'check if this file already exists, if so, let user know, and ask about overwriting it
If System.IO.File.Exists(sNewFullFileName) Then
Dim oAns As MsgBoxResult = MsgBox("A STEP file with this name already exists." & vbCrLf &
sNewFullFileName & vbCrLf & _
"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "STEP FILE EXISTS")
If oAns = MsgBoxResult.No Then Exit Sub
End If
'proceed to use the supplied new full file name, if passed that test
oDataMedium.FileName = sNewFullFileName
'set options for export now
If oSTEP.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
' Set application protocol.
' 2 = AP 203 - Configuration Controlled Design
' 3 = AP 214 - Automotive Design
oOptions.Value("ApplicationProtocolType") = 3
oOptions.Value("IncludeSketches") = True
oOptions.Value("export_fit_tolerance") = .000393701 'minimum
oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
'oOptions.Value("Authorization") = ""
'oOptions.Value("Description") = oDoc.PropertySets.Item(3).Item(14).Value 'Description iProperty
'oOptions.Value("Organization") = oDoc.PropertySets.Item(2).Item(3).Value 'Company iProperty
Try
oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
Catch
Logger.Error("Error exporting to following STEP file:" & vbCrLf & sNewFullFileName)
End Try
End If
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)