Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Export all Files of assembly to step files with part number as name

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
roelfoubert
6279 Views, 12 Replies

Export all Files of assembly to step files with part number as name

roelfoubert
Enthusiast
Enthusiast

I had a script to export all files of an assembly to seperate STEP-files.

In this script I use the filename to name the STEP-files.

I want to change it to the partnumber but it won't work. It always uses the partnumber of the assembly.

Not the individual parts.

'check that the active document is an assembly file
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MessageBox.Show("This Rule " & iLogicVb.RuleName & " only works on Assembly Files.", "WRONG DOCUMENT TYPE",MessageBoxButtons.OK,MessageBoxIcon.Error)
	Return
End If

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
    Exit Sub
End If

'get user input
RUsure = MessageBox.Show ( _
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If

'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
oPath = ThisDoc.Path
'get STEP target folder path
' original => oFolder = oPath & "\" & oAsmName & " STEP Files"
oFolder = oPath & "\STEP Files"
'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".stp") , True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
'work the referenced models
For Each oRefDoc In oRefDocs
    Dim oCurFile As Document
    oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    oCurFileName = oCurFile.FullFileName
   
    'defines backslash As the subdirectory separator
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
   
    'find the postion of the last backslash in the path
    FNamePos = InStrRev(oCurFileName, "\", -1)  
    'get the file name with the file extension
    Name = Right(oCurFileName, Len(oCurFileName) - FNamePos)
    'get the file name (without extension)
    ShortName = Left(Name, Len(Name) - 4)

    Try
        oCurFile.SaveAs(oFolder & "\" & ShortName & (".stp") , True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
)

 

 

0 Likes

Export all Files of assembly to step files with part number as name

I had a script to export all files of an assembly to seperate STEP-files.

In this script I use the filename to name the STEP-files.

I want to change it to the partnumber but it won't work. It always uses the partnumber of the assembly.

Not the individual parts.

'check that the active document is an assembly file
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MessageBox.Show("This Rule " & iLogicVb.RuleName & " only works on Assembly Files.", "WRONG DOCUMENT TYPE",MessageBoxButtons.OK,MessageBoxIcon.Error)
	Return
End If

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
    Exit Sub
End If

'get user input
RUsure = MessageBox.Show ( _
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If

'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
oPath = ThisDoc.Path
'get STEP target folder path
' original => oFolder = oPath & "\" & oAsmName & " STEP Files"
oFolder = oPath & "\STEP Files"
'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".stp") , True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
'work the referenced models
For Each oRefDoc In oRefDocs
    Dim oCurFile As Document
    oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    oCurFileName = oCurFile.FullFileName
   
    'defines backslash As the subdirectory separator
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
   
    'find the postion of the last backslash in the path
    FNamePos = InStrRev(oCurFileName, "\", -1)  
    'get the file name with the file extension
    Name = Right(oCurFileName, Len(oCurFileName) - FNamePos)
    'get the file name (without extension)
    ShortName = Left(Name, Len(Name) - 4)

    Try
        oCurFile.SaveAs(oFolder & "\" & ShortName & (".stp") , True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
)

 

 

Labels (2)
12 REPLIES 12
Message 2 of 13
JelteDeJong
in reply to: roelfoubert

JelteDeJong
Mentor
Mentor
Accepted solution

does this work for you?

'check that the active document is an assembly file
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
    MessageBox.Show("This Rule " & iLogicVb.RuleName & " only works on Assembly Files.", "WRONG DOCUMENT TYPE", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Return
End If

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmName As String = ThisDoc.FileName(False) 'without extension


'get user input
Dim RUsure = MessageBox.Show(
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If

'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
Dim oPath = ThisDoc.Path
'get STEP target folder path
' original => oFolder = oPath & "\" & oAsmName & " STEP Files"
Dim oFolder = oPath & "\STEP Files"
'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName & (".stp"), True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
'work the referenced models
For Each oRefDoc As Document In oRefDocs
    Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    Dim oCurFileName = oCurFile.FullFileName
    Dim ShortName = IO.Path.GetFileNameWithoutExtension(oCurFileName)

    Dim oPropSets As PropertySets = oCurFile.PropertySets
    Dim oPropSet As PropertySet = oPropSets.Item("Design Tracking Properties")
    Dim oPartNumiProp As [Property] = oPropSet.Item("Part Number")


    Try
        oCurFile.SaveAs(oFolder & "\" & oPartNumiProp.Value & (".stp"), True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close()
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

does this work for you?

'check that the active document is an assembly file
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
    MessageBox.Show("This Rule " & iLogicVb.RuleName & " only works on Assembly Files.", "WRONG DOCUMENT TYPE", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Return
End If

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmName As String = ThisDoc.FileName(False) 'without extension


'get user input
Dim RUsure = MessageBox.Show(
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If

'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
Dim oPath = ThisDoc.Path
'get STEP target folder path
' original => oFolder = oPath & "\" & oAsmName & " STEP Files"
Dim oFolder = oPath & "\STEP Files"
'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName & (".stp"), True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
'work the referenced models
For Each oRefDoc As Document In oRefDocs
    Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    Dim oCurFileName = oCurFile.FullFileName
    Dim ShortName = IO.Path.GetFileNameWithoutExtension(oCurFileName)

    Dim oPropSets As PropertySets = oCurFile.PropertySets
    Dim oPropSet As PropertySet = oPropSets.Item("Design Tracking Properties")
    Dim oPartNumiProp As [Property] = oPropSet.Item("Part Number")


    Try
        oCurFile.SaveAs(oFolder & "\" & oPartNumiProp.Value & (".stp"), True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close()
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 13
WCrihfield
in reply to: roelfoubert

WCrihfield
Mentor
Mentor

@roelfoubert 

Just another variation of the same process:

This is a rule I made a while back that does the same thing.  I just commented out a couple of lines & added two alternate lines to adapt it from using the file names to using the Part Numbers, as you are doing in this situation.  My code doesn't include the big question in the middle of the rule, because when I'm running this rule I'm doing so deliberately, knowing it may take some time, so it seemed pointless.  However this rule seems a bit more condensed and efficient, so I figured you (or others) might like it.

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oADoc.FullFileName)
'Dim oAsmPN As String = oADoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
Dim oPath As String = System.IO.Path.GetDirectoryName(oADoc.FullFileName) & "\STEP Files\"
If Not System.IO.Directory.Exists(oPath) Then
	System.IO.Directory.CreateDirectory(oPath)
End If
oADoc.SaveAs(oPath & oAsmName & ".stp", True)
Dim oRefName, oRefPN As String
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	oRefDoc = ThisApplication.Documents.Open(oRefDoc.FullDocumentName, False)
	'oRefName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	oRefPN = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	Try
		'oRefDoc.SaveAs(oPath & oRefName & ".stp", True)
		oRefDoc.SaveAs(oPath & oRefPN & ".stp", True)
	Catch
		MsgBox("Failed to save '" & oRefDoc.FullFileName & " out as an STEP file.", vbOKOnly, " ")
	End Try
	oRefDoc.Close(True) 'True = Skip Save
Next
MsgBox("All new STEP files were saved to:" & vbCrLf & oPath, vbOKOnly,"FINISHED")

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

@roelfoubert 

Just another variation of the same process:

This is a rule I made a while back that does the same thing.  I just commented out a couple of lines & added two alternate lines to adapt it from using the file names to using the Part Numbers, as you are doing in this situation.  My code doesn't include the big question in the middle of the rule, because when I'm running this rule I'm doing so deliberately, knowing it may take some time, so it seemed pointless.  However this rule seems a bit more condensed and efficient, so I figured you (or others) might like it.

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oADoc.FullFileName)
'Dim oAsmPN As String = oADoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
Dim oPath As String = System.IO.Path.GetDirectoryName(oADoc.FullFileName) & "\STEP Files\"
If Not System.IO.Directory.Exists(oPath) Then
	System.IO.Directory.CreateDirectory(oPath)
End If
oADoc.SaveAs(oPath & oAsmName & ".stp", True)
Dim oRefName, oRefPN As String
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	oRefDoc = ThisApplication.Documents.Open(oRefDoc.FullDocumentName, False)
	'oRefName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	oRefPN = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	Try
		'oRefDoc.SaveAs(oPath & oRefName & ".stp", True)
		oRefDoc.SaveAs(oPath & oRefPN & ".stp", True)
	Catch
		MsgBox("Failed to save '" & oRefDoc.FullFileName & " out as an STEP file.", vbOKOnly, " ")
	End Try
	oRefDoc.Close(True) 'True = Skip Save
Next
MsgBox("All new STEP files were saved to:" & vbCrLf & oPath, vbOKOnly,"FINISHED")

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 13
paulobelesa
in reply to: WCrihfield

paulobelesa
Contributor
Contributor

hello friends

 

I am very newbie on Inventor programming  
How can I use / run those sample scripts?   

 

Thanks for any help

0 Likes

hello friends

 

I am very newbie on Inventor programming  
How can I use / run those sample scripts?   

 

Thanks for any help

Message 5 of 13
WCrihfield
in reply to: paulobelesa

WCrihfield
Mentor
Mentor

With Inventor open, look at your 'Model Browser', and if you don't see a tab beside the 'Model' tab called "iLogic", then you may have to click the little plus sight (+) next to the Model tab, then choose "iLogic" to show it.  Once that 'iLogic' tab is showing, switch to it (activate it, so its contents, if any, are showing).  Now right-click somewhere within the open area below that tab and choose "Add Rule".  A little pop-up dialog will show, asking you to specify a name for the rule.  You can type something new in there, or just leave the default name, then click OK.  Now the iLogic rule editor dialog should be showing.  Copy the contents of a code window here on the forum then paste it into the empty text area of this iLogic rule editor.  Then click the Save button near the bottom right corner.  Or, if you click the 'Save & Run' button, it will obviously save the rule, then run the rule.  But don't run the rule until your absolutely sure about it, and you don't have any really important files open that may be changed in an unknown way.  Once this rule editor dialog is closed.  You can manually run this rule by navigating to that iLogic tab, then right-clicking on the rule's name, then choose 'Run'.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

With Inventor open, look at your 'Model Browser', and if you don't see a tab beside the 'Model' tab called "iLogic", then you may have to click the little plus sight (+) next to the Model tab, then choose "iLogic" to show it.  Once that 'iLogic' tab is showing, switch to it (activate it, so its contents, if any, are showing).  Now right-click somewhere within the open area below that tab and choose "Add Rule".  A little pop-up dialog will show, asking you to specify a name for the rule.  You can type something new in there, or just leave the default name, then click OK.  Now the iLogic rule editor dialog should be showing.  Copy the contents of a code window here on the forum then paste it into the empty text area of this iLogic rule editor.  Then click the Save button near the bottom right corner.  Or, if you click the 'Save & Run' button, it will obviously save the rule, then run the rule.  But don't run the rule until your absolutely sure about it, and you don't have any really important files open that may be changed in an unknown way.  Once this rule editor dialog is closed.  You can manually run this rule by navigating to that iLogic tab, then right-clicking on the rule's name, then choose 'Run'.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 13
rmilazzo
in reply to: WCrihfield

rmilazzo
Explorer
Explorer

THank you for the nice script! As i run it into an assembly derived from multisolid body part, it exports ALL the depndants parts and assembly...like it exports ALL the .ipt and .iam inside the project folder relatead to the dirved assembly, and this means also che "master multisolid body" file (which is quite big). How can i modify the script to just export the parts that compose the derived assembly?

0 Likes

THank you for the nice script! As i run it into an assembly derived from multisolid body part, it exports ALL the depndants parts and assembly...like it exports ALL the .ipt and .iam inside the project folder relatead to the dirved assembly, and this means also che "master multisolid body" file (which is quite big). How can i modify the script to just export the parts that compose the derived assembly?

Message 7 of 13
WCrihfield
in reply to: rmilazzo

WCrihfield
Mentor
Mentor

Hi @rmilazzo.  I generally don't use the workflow of deriving assemblies and parts from multi-body part files, so I'm not as familiar with all the links and/or references that are created between all the documents involved in that process as some others may be, but I think I may still be able to help here.  The main thing that would have to change is how the code is looping through documents.  Both of the codes above are looping through the 'AllReferencedDocuments' collection of the main ('active') assembly.  That will need to change, because it is not a good fit for your situation.  I'm guessing a more traditional route of just looping through the assembly's components may work better in this case.  And if needed the code could step down into sub-assemblies to lower levels of sub-assemblies and parts.

 

 I have some questions for you:

Do you want the 'main / active' assembly to be exported as a STEP file?  Are there any sub-assemblies within your main assembly?  If so, do you want those sub-assemblies exported to STEP files too?  If you don't want those sub-assemblies exported, then do you want any of the parts within those sub-assemblies exported?  How do you want the files to be named?  Where do you want the files to be saved?  Do you prefer this simple process of simply saving the files with the ".stp" file extension, without any other settings, or would you prefer to incorporate Inventor's built in Translator Add-in into the rule, so you can set more options before saving the files out?  Just let us know your specific needs.

 

Here is an updated code that is designed to loop through all components in all levels of the main assembly and save each unique component document as a STEP file.  If the main assembly contains any sub-assemblies, this will save that sub-assembly as a STEP file, then step down into that sub-assembly and save each unique document within to STEP files, and so on.  But beware, because it does not filter for Content Center stuff, iAssembly/iPart stuff, or virtual components.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisAssembly.Document
	'save the 'active' assembly as a STEP file, using our Sub below
	SaveAsSTEP(oADoc)
	'create a List to keep track of which ones we have already exported
	'so we don't export the same document more than once
	Dim oExportedDocs As List(Of String)
	'this will process all parts & sub-assemlies in all levels of the main assembly
	StepDown(oADoc.ComponentDefinition.Occurrences, oExportedDocs)
	MsgBox("Finished saving files to STEP format.", vbInformation, "")
End Sub

Sub StepDown(oComps As ComponentOccurrences, oList As List(Of String))
	'this will process all parts & sub-assemlies in (top level only) of main assembly
	For Each oComp As ComponentOccurrence In oComps
		Dim oCompDoc As Document = oComp.Definition.Document
		If Not oList.Contains(oCompDoc.FullFileName) Then
			'save this component document as a STEP file, using our Sub below
			SaveAsSTEP(oCompDoc)
			'add the full file name of that document to the list
			oList.Add(oCompDoc.FullFileName)
		End If
		If TypeOf oComp.Definition Is AssemblyComponentDefinition Then
			StepDown(oComp.Definition.Occurrences, oList)
		End If
	Next
End Sub

Sub SaveAsSTEP(oDoc As Document)
	oDirChar = System.IO.Path.DirectorySeparatorChar
	oPath = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & oDirChar & "STEP Files" & oDirChar
	If Not System.IO.Directory.Exists(oPath) Then System.IO.Directory.CreateDirectory(oPath)
	Dim oPN As String = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	oNewName = oPath & oPN & ".stp"
	'Check to see if the STEP file already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(oNewName) Then
		oAns = MsgBox("A STEP file with this name already exists." & vbCrLf & _
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "FILE ALREADY EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	Try
		oDoc.SaveAs(oNewName, True)
	Catch
		MsgBox("Failed to save '" & oDoc.FullFileName & " out as a STEP file.", , "")
	End Try
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Hi @rmilazzo.  I generally don't use the workflow of deriving assemblies and parts from multi-body part files, so I'm not as familiar with all the links and/or references that are created between all the documents involved in that process as some others may be, but I think I may still be able to help here.  The main thing that would have to change is how the code is looping through documents.  Both of the codes above are looping through the 'AllReferencedDocuments' collection of the main ('active') assembly.  That will need to change, because it is not a good fit for your situation.  I'm guessing a more traditional route of just looping through the assembly's components may work better in this case.  And if needed the code could step down into sub-assemblies to lower levels of sub-assemblies and parts.

 

 I have some questions for you:

Do you want the 'main / active' assembly to be exported as a STEP file?  Are there any sub-assemblies within your main assembly?  If so, do you want those sub-assemblies exported to STEP files too?  If you don't want those sub-assemblies exported, then do you want any of the parts within those sub-assemblies exported?  How do you want the files to be named?  Where do you want the files to be saved?  Do you prefer this simple process of simply saving the files with the ".stp" file extension, without any other settings, or would you prefer to incorporate Inventor's built in Translator Add-in into the rule, so you can set more options before saving the files out?  Just let us know your specific needs.

 

Here is an updated code that is designed to loop through all components in all levels of the main assembly and save each unique component document as a STEP file.  If the main assembly contains any sub-assemblies, this will save that sub-assembly as a STEP file, then step down into that sub-assembly and save each unique document within to STEP files, and so on.  But beware, because it does not filter for Content Center stuff, iAssembly/iPart stuff, or virtual components.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisAssembly.Document
	'save the 'active' assembly as a STEP file, using our Sub below
	SaveAsSTEP(oADoc)
	'create a List to keep track of which ones we have already exported
	'so we don't export the same document more than once
	Dim oExportedDocs As List(Of String)
	'this will process all parts & sub-assemlies in all levels of the main assembly
	StepDown(oADoc.ComponentDefinition.Occurrences, oExportedDocs)
	MsgBox("Finished saving files to STEP format.", vbInformation, "")
End Sub

Sub StepDown(oComps As ComponentOccurrences, oList As List(Of String))
	'this will process all parts & sub-assemlies in (top level only) of main assembly
	For Each oComp As ComponentOccurrence In oComps
		Dim oCompDoc As Document = oComp.Definition.Document
		If Not oList.Contains(oCompDoc.FullFileName) Then
			'save this component document as a STEP file, using our Sub below
			SaveAsSTEP(oCompDoc)
			'add the full file name of that document to the list
			oList.Add(oCompDoc.FullFileName)
		End If
		If TypeOf oComp.Definition Is AssemblyComponentDefinition Then
			StepDown(oComp.Definition.Occurrences, oList)
		End If
	Next
End Sub

Sub SaveAsSTEP(oDoc As Document)
	oDirChar = System.IO.Path.DirectorySeparatorChar
	oPath = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & oDirChar & "STEP Files" & oDirChar
	If Not System.IO.Directory.Exists(oPath) Then System.IO.Directory.CreateDirectory(oPath)
	Dim oPN As String = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	oNewName = oPath & oPN & ".stp"
	'Check to see if the STEP file already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(oNewName) Then
		oAns = MsgBox("A STEP file with this name already exists." & vbCrLf & _
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "FILE ALREADY EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	Try
		oDoc.SaveAs(oNewName, True)
	Catch
		MsgBox("Failed to save '" & oDoc.FullFileName & " out as a STEP file.", , "")
	End Try
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 13
rmilazzo
in reply to: WCrihfield

rmilazzo
Explorer
Explorer

hI @WCrihfield , and thank you so much for the quick and detailed reply. The answers:

 

  1. No, I dont need the main/active assembly to be extported as step.
  2. Yes, there could be some subassemblies, but i dont need to be exported in STP.
  3. No, i dont need to export subassemblies.
  4. Generally I dont even need any of those sub assemblies parts to be exported
  5. The name of the file has to be the part name in the BOM (like in the script you posted a bit up here)
  6. The files should be saved in a \EXPORT_STEP directory within the assembly dsave directory
  7. It can be just s imple STEP export without any translator add in

I tried the code you updated but i get this message

Error in rule: EXPORT_STEP_2, in document: 143_GYM_MOBILE_BAR.iam

Object reference not set to an instance of an object.

And now it creates only the STP export of the derived assembly (the whole assemply), but not any parts.


Thank you in advance for the help!

0 Likes

hI @WCrihfield , and thank you so much for the quick and detailed reply. The answers:

 

  1. No, I dont need the main/active assembly to be extported as step.
  2. Yes, there could be some subassemblies, but i dont need to be exported in STP.
  3. No, i dont need to export subassemblies.
  4. Generally I dont even need any of those sub assemblies parts to be exported
  5. The name of the file has to be the part name in the BOM (like in the script you posted a bit up here)
  6. The files should be saved in a \EXPORT_STEP directory within the assembly dsave directory
  7. It can be just s imple STEP export without any translator add in

I tried the code you updated but i get this message

Error in rule: EXPORT_STEP_2, in document: 143_GYM_MOBILE_BAR.iam

Object reference not set to an instance of an object.

And now it creates only the STP export of the derived assembly (the whole assemply), but not any parts.


Thank you in advance for the help!

Message 9 of 13
WCrihfield
in reply to: rmilazzo

WCrihfield
Mentor
Mentor

OK.  I believe I understand what you need now.  I have adjusted my code to suit those needs.  Now it first gets the main assembly.  Then gets the folder that main assembly is in, and makes sure there is a sub folder there called "EXPORT_STEP" to put all of the exported STEP files that we make with this rule into.  It then creates a List to keep track of which documents we have exported.  This is because the assembly may have multiple components within it that represent the same document, and helps us to not try to export that same document multiple times.  Then it starts looping through just the top level components in the assembly.  If the component doesn't represent a Part, it skips to the next component.  It then gets that Part document that the component represents, gets its file name (without path or extension), and assembles what will be the name of the new STEP file.  It then checks to see if that file already exists, and if so, asks you if you want to overwrite that existing file with this new one.  If you say no, it will skip to the next component.  If no existing file was found, or if you say yes to the question, it will continue to save the file out as STEP.  Then when it does save the file, it also adds that document's name to our list to keep track.  Then there is a message letting you know it has finished.

See if this works better for you:

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	oDirChar = System.IO.Path.DirectorySeparatorChar
	'define path to save STEP file to
	oPath = System.IO.Path.GetDirectoryName(oADoc.FullFileName) & oDirChar & "EXPORT_STEP" & oDirChar
	'make sure that path exists
	If Not System.IO.Directory.Exists(oPath) Then
		System.IO.Directory.CreateDirectory(oPath)
	End If
	'create a List to keep track of which ones we have already exported
	Dim oExportedDocs As List(Of String)
	'loop through the components in this assembly
	For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
		'if it's not a part, skip to next component
		If oOcc.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		'get the document this component is representing
		Dim oOccPDoc As PartDocument = oOcc.Definition.Document
		'if this document is already in our list, we've already saved it out, so skip to next component
		If oExportedDocs.Contains(oOccPDoc.FullFileName) Then Continue For
		'get the file name (without path or extension) 
		oName = System.IO.Path.GetFileNameWithoutExtension(oOccPDoc.FullFileName)
		'combine path, name, & new extension
		oNewName = oPath & oName & ".stp"
		'Check to see if the STEP file already exists, if it does, ask if you want to overwrite it or not.
		If System.IO.File.Exists(oNewName) Then
			oAns = MsgBox("A STEP file with this name already exists." & vbCrLf & _
			"Do you want to overwrite it with this new one?", vbYesNo + vbQuestion + vbDefaultButton2, "FILE ALREADY EXISTS")
			If oAns = vbNo Then Continue For
		End If
		Try
			'save it out as STEP file
			oOccPDoc.SaveAs(oNewName, True)
			'add this document's full file name to our list
			oExportedDocs.Add(oOccPDoc.FullFileName)
		Catch oEx As Exception
			MsgBox("Failed to save '" & oOccPDoc.FullFileName & " out as a STEP file." & vbCrLf & _
			oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "iLogic")
		End Try
	Next
	MsgBox("Finished saving files to STEP format.", vbInformation, "iLogic")
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

OK.  I believe I understand what you need now.  I have adjusted my code to suit those needs.  Now it first gets the main assembly.  Then gets the folder that main assembly is in, and makes sure there is a sub folder there called "EXPORT_STEP" to put all of the exported STEP files that we make with this rule into.  It then creates a List to keep track of which documents we have exported.  This is because the assembly may have multiple components within it that represent the same document, and helps us to not try to export that same document multiple times.  Then it starts looping through just the top level components in the assembly.  If the component doesn't represent a Part, it skips to the next component.  It then gets that Part document that the component represents, gets its file name (without path or extension), and assembles what will be the name of the new STEP file.  It then checks to see if that file already exists, and if so, asks you if you want to overwrite that existing file with this new one.  If you say no, it will skip to the next component.  If no existing file was found, or if you say yes to the question, it will continue to save the file out as STEP.  Then when it does save the file, it also adds that document's name to our list to keep track.  Then there is a message letting you know it has finished.

See if this works better for you:

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	oDirChar = System.IO.Path.DirectorySeparatorChar
	'define path to save STEP file to
	oPath = System.IO.Path.GetDirectoryName(oADoc.FullFileName) & oDirChar & "EXPORT_STEP" & oDirChar
	'make sure that path exists
	If Not System.IO.Directory.Exists(oPath) Then
		System.IO.Directory.CreateDirectory(oPath)
	End If
	'create a List to keep track of which ones we have already exported
	Dim oExportedDocs As List(Of String)
	'loop through the components in this assembly
	For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
		'if it's not a part, skip to next component
		If oOcc.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		'get the document this component is representing
		Dim oOccPDoc As PartDocument = oOcc.Definition.Document
		'if this document is already in our list, we've already saved it out, so skip to next component
		If oExportedDocs.Contains(oOccPDoc.FullFileName) Then Continue For
		'get the file name (without path or extension) 
		oName = System.IO.Path.GetFileNameWithoutExtension(oOccPDoc.FullFileName)
		'combine path, name, & new extension
		oNewName = oPath & oName & ".stp"
		'Check to see if the STEP file already exists, if it does, ask if you want to overwrite it or not.
		If System.IO.File.Exists(oNewName) Then
			oAns = MsgBox("A STEP file with this name already exists." & vbCrLf & _
			"Do you want to overwrite it with this new one?", vbYesNo + vbQuestion + vbDefaultButton2, "FILE ALREADY EXISTS")
			If oAns = vbNo Then Continue For
		End If
		Try
			'save it out as STEP file
			oOccPDoc.SaveAs(oNewName, True)
			'add this document's full file name to our list
			oExportedDocs.Add(oOccPDoc.FullFileName)
		Catch oEx As Exception
			MsgBox("Failed to save '" & oOccPDoc.FullFileName & " out as a STEP file." & vbCrLf & _
			oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "iLogic")
		End Try
	Next
	MsgBox("Finished saving files to STEP format.", vbInformation, "iLogic")
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 13
rmilazzo
in reply to: WCrihfield

rmilazzo
Explorer
Explorer

Thank you again @WCrihfield!

I think you perfectly got what I need.... I tried to run the rule but i get this error message:

Error in rule: EXPORT_RULE_2, in document: 143_GYM_MOBILE_BAR.iam

Object reference not set to an instance of an object.

on the first tab of the error window,
 then on the second one

System.NullReferenceException: Object reference not set to an instance of an object.
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

maybe am I  doing something wrong copying and pasting the code?

Thank you again for the nice and quick reply!!



0 Likes

Thank you again @WCrihfield!

I think you perfectly got what I need.... I tried to run the rule but i get this error message:

Error in rule: EXPORT_RULE_2, in document: 143_GYM_MOBILE_BAR.iam

Object reference not set to an instance of an object.

on the first tab of the error window,
 then on the second one

System.NullReferenceException: Object reference not set to an instance of an object.
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

maybe am I  doing something wrong copying and pasting the code?

Thank you again for the nice and quick reply!!



Message 11 of 13
WCrihfield
in reply to: rmilazzo

WCrihfield
Mentor
Mentor

The only thing I can think of that might possibly be causing that error is maybe the following line:

Dim oOccPDoc As PartDocument = oOcc.Definition.Document

This may not be working, for some reason, so it is not setting the value of the oOccPDoc variable.  Then the further lines of code are trying to use that variable without it having a value.  You could try a simple variation of that line to see if it fixes the problem, like this:

Dim oOccPDoc As PartDocument = oOcc.ReferencedDocumentDescriptor.ReferencedDocument

But I'm not sure that will work 100% of the time either.  Or we could simply check if that variable Is Nothing after that line, to make sure, and if it is Nothing, use Continue For or show a message about the situation then continue, or similar.  Since I'm not that familiar with the derived assembly from multi-body part situation, there may be other better lines of code to use in that place.  The ComponentOccurrence object might be an DerivedAssemblyOccurrence or DerivedPartComponent or something else I'm not familiar with, I'm not sure.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

The only thing I can think of that might possibly be causing that error is maybe the following line:

Dim oOccPDoc As PartDocument = oOcc.Definition.Document

This may not be working, for some reason, so it is not setting the value of the oOccPDoc variable.  Then the further lines of code are trying to use that variable without it having a value.  You could try a simple variation of that line to see if it fixes the problem, like this:

Dim oOccPDoc As PartDocument = oOcc.ReferencedDocumentDescriptor.ReferencedDocument

But I'm not sure that will work 100% of the time either.  Or we could simply check if that variable Is Nothing after that line, to make sure, and if it is Nothing, use Continue For or show a message about the situation then continue, or similar.  Since I'm not that familiar with the derived assembly from multi-body part situation, there may be other better lines of code to use in that place.  The ComponentOccurrence object might be an DerivedAssemblyOccurrence or DerivedPartComponent or something else I'm not familiar with, I'm not sure.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 13

daniel.lestrange
Explorer
Explorer

Hello

I have copied that code hopping that it will work but there is an error, do I need to do anything else before trying to run the code (the model I tried to export is an assembly)?

 

 

daniellestrange_0-1663685471163.png

 

0 Likes

Hello

I have copied that code hopping that it will work but there is an error, do I need to do anything else before trying to run the code (the model I tried to export is an assembly)?

 

 

daniellestrange_0-1663685471163.png

 

Message 13 of 13

WCrihfield
Mentor
Mentor

Hi @daniel.lestrange.  If you are having trouble with that line of code that you have highlighted in blue within your image, you may have to remove the 'iLogicVb.RuleName' part of that message.  Some folks have trouble using that for some reason.  It is just there to show the name of the current iLogic rule within the feedback message, and is not really necessary.  That first block of code could be changed to be like this:

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MessageBox.Show("This rule only works on Assembly files.", "Wrong Document Type", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Return
End If
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

...or like this:

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("This rule only works on Assembly files.", vbCritical, "Wrong Document Type")
	Return
End If
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Hi @daniel.lestrange.  If you are having trouble with that line of code that you have highlighted in blue within your image, you may have to remove the 'iLogicVb.RuleName' part of that message.  Some folks have trouble using that for some reason.  It is just there to show the name of the current iLogic rule within the feedback message, and is not really necessary.  That first block of code could be changed to be like this:

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MessageBox.Show("This rule only works on Assembly files.", "Wrong Document Type", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Return
End If
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

...or like this:

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("This rule only works on Assembly files.", vbCritical, "Wrong Document Type")
	Return
End If
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report