Ilogic export specific files with a filename in assembly & subassembly's

Ilogic export specific files with a filename in assembly & subassembly's

rp
Contributor Contributor
860 Views
7 Replies
Message 1 of 8

Ilogic export specific files with a filename in assembly & subassembly's

rp
Contributor
Contributor

Hello,

 

We build a code to export files with a specific filename.

In the code below we want to export the files with a code "-PI-".

This works fine in a topassembly.

 

This code is for the occurences(with a specific filename) in the top assembly.

Is it possible to do this for the complete assembly incl. the subassembly's?

And even beter is it possible to make a choise between the top or the complete assembly (incl. the subassembly's).

 

'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 & JPG file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP & JPG's 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


'- - - - - - - - - - - - -Folder setup - - - - - - - - - - - -
oPath = ThisDoc.Path

'get STEP target folder path
oFolder = oPath & "\" & oAsmName & "Export"  
'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


'- - - - - - - - - - - - -Export selected parts- - - - - - - - - - - -

'define the active document as an assembly file
Dim AssyDoc As AssemblyDocument = ThisDoc.Document

Dim oOccurrencesToExport As New List(Of ComponentOccurrence) 						


'Select the part file contains a filename
For Each oOcc As ComponentOccurrence In AssyDoc.ComponentDefinition.Occurrences 	
	Dim oFilename As String = oOcc.Definition.Document.displayname 					
	If oFilename.Contains("-PI-") Then 												
		oOccurrencesToExport.Add(oOcc) 														
	End If
Next


''- - - - - - - - - - - - -Export all parts & assembly's in main & sub-assembly - - - - - - - - - - - -


'below the export loop to export all the component occurrences we saved in the list before.
For Each oCompOcc As ComponentOccurrence In oOccurrencesToExport
	Dim oOccDoc As Document = oCompOcc.Definition.Document
	Dim oOccDocFillPath As String = oOccDoc.FullFileName
	Dim oFilenameWithoutExtension As String = System.IO.Path.GetFileNameWithoutExtension(oOccDocFillPath)	

'Filename STP
	Dim oFullPathstp As String = String.Format("{0}\{1}.stp", oFolder, oFilenameWithoutExtension)				
	
'save documents
	oOccDoc.SaveAs(oFullPathstp, True)
	
Next

 

0 Likes
Accepted solutions (1)
861 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @rp.  Below is one example of how to ask a question with 3 possible outcomes.  You can use either a MsgBox or MessageBox.Show the same way.  In a case like yours, it would be much simpler to loop through the assembly's referenced documents, instead of its components, because it would simply be more efficient, and less complex.  I am showing an important distinction between two of the assembly's properties, which will easily allow you to choose between only top level referenced documents, or all levels of referenced documents.  In my mind it would be easier to put your code for exporting the documents to other formats into a separate Sub routine, as I am showing a simplified example of below.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	oContinue = MsgBox("Do you want to process this whole assembly now?" & vbCrLf & _
	"Yes = Process Top Level Only" & vbCrLf & _
	"No = Process All Levels" & vbCrLf & _
	"Cancel = Exit Rule Without Proceeding", vbYesNoCancel + vbQuestion, "Export Assembly Docs?")
	If oContinue = vbCancel Then Exit Sub
	If oContinue = vbYes Then 'process top level only
		ExportDocs(oADoc.ReferencedDocuments)
	ElseIf oContinue = vbNo Then 'process all levels
		ExportDocs(oADoc.AllReferencedDocuments)
	End If
	'If oADoc.RequiresUpdate Then oADoc.Update2(True)
End Sub

Sub ExportDocs(oRefDocs As DocumentsEnumerator)
	'put all code here that is not document specific
	For Each oRefDoc As Document In oRefDocs
		'put your document specific code here
	Next
End Sub

...but if you prefer to keep it all in one long routine, instead of using a separate Sub routine, you could declare the oRefDocs variable before asking the big question, then set its value based on the answer recieved, similar to the simplified example below.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oRefDocs As DocumentsEnumerator = Nothing
	oContinue = MsgBox("Do you want to process this whole assembly now?" & vbCrLf & _
	"Yes = Process Top Level Only" & vbCrLf & _
	"No = Process All Levels" & vbCrLf & _
	"Cancel = Exit Rule Without Proceeding", vbYesNoCancel + vbQuestion, "Export Assembly Docs?")
	If oContinue = vbCancel Then Exit Sub
	If oContinue = vbYes Then 'process top level only
		oRefDocs = oADoc.ReferencedDocuments
	ElseIf oContinue = vbNo Then 'process all levels
		oRefDocs = oADoc.AllReferencedDocuments
	End If

	'put all code here that is not document specific
	
	For Each oRefDoc As Document In oRefDocs
		'put your document specific code here
	Next
	'If oADoc.RequiresUpdate Then oADoc.Update2(True)
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

Cadkunde.nl
Collaborator
Collaborator

When I need to go through all files in an assembly I always go to this:

https://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html

 

To prevent processing the same document multiple times:

 

Class iRule
	Private ListDocName As New List(Of String)
Sub Main()
    Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument	
    TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)
End Sub

Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer)
    Dim oOcc As ComponentOccurrence
    For Each oOcc In Occurrences
		If oOcc.Suppressed = False Then
			If Not ListDocName.Contains(oOcc.Definition.Document.FullFileName) Then
				ListDocName.add(oOcc.Definition.Document.FullFileName)
				'DO YOUR ACTIONS HERE
				
		        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
		            TraverseAssembly(oOcc.SubOccurrences, Level + 1)
		        End If
			End If
		End If
    Next
End Sub
End Class
0 Likes
Message 4 of 8

rp
Contributor
Contributor

Thanks for the reply,

 

Searching and trying a lot and didn't find a solution for the filename.

Maybe i'm stuck in the old export loop with the componentoccurences.

Get that feeling i'm almost there, but missing that last piece.

 

Below the 'put your document specific code here 

I tried to find the filename with the "PI" to export to .stp

 

Thanks for your help

 

	For Each oRefDoc As Document In oRefDocs
		'put your document specific code here

'select the files with a specific filename
	Dim oFilename As String = oRefDoc.DisplayName		
	If oFilename.Contains("-PI-") Then 	
	oRefdocToExport???
	End If
Next
	'Filename STP
	Dim oRefDocFillPath As String = oRefDoc.FullFileName
	Dim oFilenameWithoutExtension As String = System.IO.Path.GetFileNameWithoutExtension(oRefDocFillPath)
	Dim oFullPathstp As String = String.Format("{0}\{1}.stp", oFolder, oFilenameWithoutExtension)	
	
	'save documents
	oRefDocs.SaveAs(oFullPathstp, True)
End Sub

 

 

0 Likes
Message 5 of 8

Cadkunde.nl
Collaborator
Collaborator
Class iRule
	Private ListDocName As New List(Of String)
	Sub Main()
		'define the active document as an assembly file
		Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
		If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
			MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
			Exit Sub
		End If		

		'- - - - - - - - - - - - -Folder setup - - - - - - - - - - - -
		oAsmName = ThisDoc.FileName(False) 'without extension
		oPath = ThisDoc.Path

		'get STEP target folder path
		oFolder = oPath & "\" & oAsmName & "Export\"
		'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
		
		'get user input
		RUsure = MessageBox.Show( _
		"This will create a STEP & JPG file for all components." _
		& vbLf & " " _
		& vbLf & "Are you sure you want to create STEP & JPG's for all of the assembly components?" _
		& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ", MessageBoxButtons.YesNo)
		If Not RUsure = vbYes Then Exit Sub
		
		'Collect Occurrences
		TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)

		'Export		
		Export(oFolder)
	End Sub

	Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer)
		Dim oOcc As ComponentOccurrence
		For Each oOcc In Occurrences
			If oOcc.Suppressed = False Then
				If Not ListDocName.Contains(oOcc.Definition.Document.FullFileName) Then
					ListDocName.add(oOcc.Definition.Document.FullFileName)

					If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
						TraverseAssembly(oOcc.SubOccurrences, Level + 1)
					End If
				End If
			End If
		Next
	End Sub
	
	Sub Export(oFolder As String)
		Dim oApp As Inventor.Application = ThisApplication
		Dim odoc As Inventor.Document = Nothing

		For i = 0 To ListDocName.count - 1
			If ListDocName(i).Contains("-PI-") Then
				odoc = oApp.Documents.Open(ListDocName(i), False)
				Dim oFilenameWithoutExtension As String = System.IO.Path.GetFileNameWithoutExtension(ListDocName(i))
				Dim oFullPathstp As String = oFolder & oFilenameWithoutExtension & ".stp"
				odoc.SaveAs(oFullPathstp, True)
			End If
		Next
	End Sub
End Class

0 Likes
Message 6 of 8

rp
Contributor
Contributor

Hello @WCrihfield,

 

I was wondering if you have an other good suggestion in your code (the first code you admitted is the best i thing:-) ).

In the place where you have the text    'put your document specific code here 

I tried to find the filename with the "PI" to export to .stp

 

Thank you very much,

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi @rp.  I have attached an alternative iLogic rule code as a text file.  You can either use the simple Document.SaveAs method that you have been using, which does not allow you to specify any options/settings, or you can take advantage of the built-in TranslatorAddIn for exporting to the STEP file format, which will allow you to specify all the same options as when you export manually, and use the Options button.  I have included both codes in the attached rule.  If you want to keep using the simple SaveAs method, you can simply delete the whole separate Sub routine and the line near the end of the main routine that calls that other routine to run, then uncomment the SaveAs line.  Let me know how this works our for you.

 

By the way...what year/version of Inventor are you using?  If you are using 2022 or newer, then are you using any custom ModelStates in any of those documents you are trying to export?  Do any of the ones you are exporting represent something like an iPart or iAssembly?

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

rp
Contributor
Contributor

@WCrihfield thank you very much, this is what we wanted!

0 Likes