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

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

Murat.Sözen
Advocate Advocate
6,141 Views
10 Replies
Message 1 of 11

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

Murat.Sözen
Advocate
Advocate

Hi all , 

 

I need to export all parts in an assembly to STEP files.

 

I found this topic and it works fine but i need an edit.

 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/export-all-files-of-assembly-to-step...

 

This iLogic code works fine but exports sub-assembly files as STEP too , i just need Part Only.

 

Can anyone help me ?

 

'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")

 

Mechanical Specialist

Inventor Professional
Inventor Nesting
Factory Design Utilities
VRED Pro
Vault Professional

Çözülen sorularınızı lütfen  ÇÖZÜMÜ KABUL ET  olarak işaretlemeyi unutmayın.

LinkedIn!


0 Likes
Accepted solutions (1)
6,142 Views
10 Replies
Replies (10)
Message 2 of 11

yan.gauthier
Advocate
Advocate

convert the component section of your code in a function taking an assembly as parameter

 

in your foreach, check if the document is an assembly, if yes, recursively call the function.

 

 

0 Likes
Message 3 of 11

Murat.Sözen
Advocate
Advocate
I tried but i could't , may you correct this?
Mechanical Specialist

Inventor Professional
Inventor Nesting
Factory Design Utilities
VRED Pro
Vault Professional

Çözülen sorularınızı lütfen  ÇÖZÜMÜ KABUL ET  olarak işaretlemeyi unutmayın.

LinkedIn!


0 Likes
Message 4 of 11

basautomationservices
Advocate
Advocate

You could also use the 'oAsmDoc.ComponentDefinition.Occurrences.AllLeafOccurrences' instead of AllReferencedDocuments

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Message 5 of 11

WCrihfield
Mentor
Mentor
Accepted solution

You can give this version a try.  I mostly just condensed it a bit, then added in the following line just inside the loop of referenced documents, to force it to only process Part documents.

If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For

Here is the updated code:

Sub Main
	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
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

	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

	Dim oPath As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
	Dim oFolder As String = oPath & "\STEP Files"
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If

	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	For Each oRefDoc As Document In oRefDocs
		'if is not a Part, skip to next referenced document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
		Dim oCurFileName = oCurFile.FullFileName
		Dim ShortName = IO.Path.GetFileNameWithoutExtension(oCurFileName)
		Dim oPN As String = oCurFile.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
		'might want to ckeck if oPN = "" or not
		Try
			oCurFile.SaveAs(oFolder & "\" & oPN & ".stp", True)
		Catch
			MessageBox.Show("Error processing " & oCurFileName, "ilogic")
		End Try
		oCurFile.Close()
	Next
	MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
End Sub

If the term 'iLogicVb.RuleName' within the first If...Then statement gives you any trouble, you can just delete it out of that message.  It is just meant to include the name of the iLogic rule in the message, but some folks have had trouble with it causing problems/errors when they try to use it.   I also changed the code used to set the value of the oPath variable, because in some odd situations it is possible that using the term 'ThisDoc.Path' may actually be referencing a different document than the one we have already established before that point in the code by using the ThisApplication.ActiveDocument term.  Also, I'm not sure that the Open method (especially when using True) is necessary here.  You can check if a document is already open using oRefDoc.Open (ReadOnly Boolean), then, if needed use the Open method, but I doubt it needs to be opened visibly to export the .stp file.  By default most (if not all) referenced documents are already at least 'initiated', if not fully opened in the background, when you open the assembly.  There are likely exceptions, like when using Express Mode, or LOD's/ModelStates and have lots of stuff suppressed.  Just FYI.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 11

Murat.Sözen
Advocate
Advocate

Thank you @WCrihfield  , it works great.

Mechanical Specialist

Inventor Professional
Inventor Nesting
Factory Design Utilities
VRED Pro
Vault Professional

Çözülen sorularınızı lütfen  ÇÖZÜMÜ KABUL ET  olarak işaretlemeyi unutmayın.

LinkedIn!


0 Likes
Message 7 of 11

donnie.morris
Enthusiast
Enthusiast
Can this code be rewritten to allow the user to define the save location instead of placing the files in the same directory as the assembly?
0 Likes
Message 8 of 11

basautomationservices
Advocate
Advocate

You can use a SaveFileDialog to get a filename, to just select a folder you can use FolderBrowserDialog.

 

The folder browser dialog is rather annoying, but it does the job. 

 

There are also some dialogs implemented in Inventor, some links:

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/folder-browser-change-to-windows-for...

https://inventortrenches.blogspot.com/2012/10/ilogic-adding-save-as-dialog-box.html

 

 

 

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Message 9 of 11

e_frissell
Advocate
Advocate
Sub Main
	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
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oPath As String = InputBox("Paste folder location for export")
	Dim exportFile As Integer
	Dim CorrectPartName As Integer
	Dim oPartNo As String

	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.YesNoCancel)
	If RUsure = vbNo Then Return
	If RUsure = vbCancel Then Exit Sub

	
	Dim oFolder As String = oPath & "\STEP Files"
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If

	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	For Each oRefDoc As Document In oRefDocs
		'if is not a Part, skip to next referenced document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
		exportFile = MsgBox("Export this to step file?  Click no to skip", vbYesNoCancel)
		If exportFile = 2 Then Exit Sub '2 is vbCancel
		If exportFile = 7 Then GoTo Skip : '7 is vbno
		If exportFile = 6 Then ' 6 is vbyes
			Dim oPN As String = oCurFile.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
			CorrectPartName = MsgBox("Exporting " & oCurFile.DisplayName & " as " & oPN & vbLf & vbLf & "Is part number correct?" , vbYesNo)
			If CorrectPartName = 7 Then oPartNo = InputBox("Modify part name to ")
			If CorrectPartName = 6 Then oPartNo = oPN
			Try
				oCurFile.SaveAs(oFolder & "\" & oPartNo & ".stp", True)
			Catch
				MessageBox.Show("Error processing " & oCurFileName, "ilogic")
			End Try
		End If
		Skip :
		oCurFile.Close()
	Next
	MessageBox.Show("All files have been exported")
End Sub

The code can be modified to allow a user to pick the save location for their step files - I re-wrote the script to suit my needs which was to pick 1 directory to export all the step files to as well as pick and choose what files get exported

 

 

Sub Main
	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
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oPath As String = InputBox("Paste folder location for export")
	Dim exportFile As Integer
	Dim CorrectPartName As Integer
	Dim oPartNo As String

	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.YesNoCancel)
	If RUsure = vbNo Then Return
	If RUsure = vbCancel Then Exit Sub

	
	Dim oFolder As String = oPath & "\STEP Files"
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If

	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	For Each oRefDoc As Document In oRefDocs
		'if is not a Part, skip to next referenced document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
		exportFile = MsgBox("Export this to step file?  Click no to skip", vbYesNoCancel)
		If exportFile = 2 Then Exit Sub '2 is vbCancel
		If exportFile = 7 Then GoTo Skip : '7 is vbno
		If exportFile = 6 Then ' 6 is vbyes
			Dim oPN As String = oCurFile.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
			CorrectPartName = MsgBox("Exporting " & oCurFile.DisplayName & " as " & oPN & " is part number correct?" , vbYesNo)
			If CorrectPartName = 7 Then oPartNo = InputBox("Modify part name to ")
			If CorrectPartName = 6 Then oPartNo = oPN
			Try
				oCurFile.SaveAs(oFolder & "\" & oPartNo & ".stp", True)
			Catch
				MessageBox.Show("Error processing " & oCurFileName, "ilogic")
			End Try
		End If
		Skip :
		oCurFile.Close()
	Next
	MessageBox.Show("All files have been exported")
End Sub

 

0 Likes
Message 10 of 11

JMARTINEZP5F4F
Participant
Participant

How can I change the location of the folder I want to save the step files in

0 Likes
Message 11 of 11

WCrihfield
Mentor
Mentor

Hi @JMARTINEZP5F4F.  If you are referencing the code example I posted in Message 5 above, then we would need to focus on Lines 14 & 15.  Line 14 is getting the full file path of the main assembly document, which does not include the final "\" character just before the file name, or the file name, or the file's extension.  Then Line 15 is combining that path with a directory separator character ("\") and the name of a sub folder ("STEP Files") to specify the location of the folder where we want the STEP files to be created within.  Then Lines 16 through 18 check to see if that folder exists, and if not creates it, so that we can save the files into it later.  That folder path is then used for all the files it creates within the 'loop', by adding the final directory separator character ("\"), then the Part Number iProperty value (as the file's name), then the ".stp" file extension, in Line 30.

 

I do not know the specifics of how you want to change the path, but you should be able to change Lines 14 & 15 to whatever is needed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes