ilogic save as part number

ilogic save as part number

darrell.wcd
Enthusiast Enthusiast
1,700 Views
4 Replies
Message 1 of 5

ilogic save as part number

darrell.wcd
Enthusiast
Enthusiast

I've put together some iLogic from different samples, but I have run into a real problem.

I'm trying to take a top level assembly and open all the parts referenced, and save a step file named as the part's "Part Number" to a specific folder.

It will cycle through but cant seam to put the individual part number. I've tried a few different options to pull the part number from the referenced part but just cant seam to make it work.

 

With the code below, I get an error saying the file *.ipt was not found.

the error is on line 47 I believe.

Any suggestions, ideas, or references would be much appreciated.

 

Regards

Darrell

 

 

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

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension
oAsmNameex=ThisDoc.FileName(True)
oAsmPN= iProperties.Value(ThisApplication,"Project", "Part Number")
'MessageBox.Show (oAsmPN)

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

oFolder = ("C:\Inventor STP")


'- - - - - - - - - - - - -Assembly Export- - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmPN &(".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
	Dim oModelPN As String
	
		oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)       
    oCurFileName = oCurFile.FullFileName 
	oModelPN =  iProperties.Value(oCurFileName,"Project", "Part Number")
		
	MessageBox.Show ("Part Number is" & oModelPN)
	
	
	
	
	
	If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then
   
    'defines backslash As the subdirectory separator
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
   
    	

    	Try  
		    		
        	oCurFile.SaveAs(oFolder & "\" & ShortName & (".stp") , True)
			MessageBox.Show(oCurFile)
    	Catch
        	MessageBox.Show("Error processing " & oCurFileName, "ilogic")
		
    	End Try
	End If		
	oCurFile.Close
	
	Next

'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus) 
0 Likes
Accepted solutions (1)
1,701 Views
4 Replies
Replies (4)
Message 2 of 5

frederic.vandenplas
Collaborator
Collaborator

Hi, I've modified your code a bit, this should work.

 

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

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension
oAsmNameex=ThisDoc.FileName(True)
oAsmPN= iProperties.Value(ThisApplication,"Project", "Part Number")
'MessageBox.Show (oAsmPN)

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

oFolder = ("C:\Inventor STP")


'- - - - - - - - - - - - -Assembly Export- - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmPN &(".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
	Dim oModelPN As String
	
	oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)       
    oModelPN =  oCurFile.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
			
	If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then
   
    'defines backslash As the subdirectory separator
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
     	

    	Try  
		    		
        	oCurFile.SaveAs(oFolder & "\" & oModelPN & (".stp") , True)
			'MessageBox.Show(oCurFile)
    	Catch
        	'MessageBox.Show("Error processing " & oCurFileName, "ilogic")
		
    	End Try
	End If		
	oCurFile.Close
	
	Next

'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus) 
If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
Message 3 of 5

Owner2229
Advisor
Advisor
Accepted solution

Hey, I believe it's already the right time for spring cleanup, so here you go:

A) It's good to have any constant values definitions at the top of your code, so you can see it right away (when you're going to edit your code for whatever reason couple of moths in the future).

B) There's no need for opening any of the sub-files, you can export them right from the assembly

 

Dim oFolder As String = "C:\Inventor STP" 'The target folder for export
If Not System.IO.Directory.Exists(oFolder) Then System.IO.Directory.CreateDirectory(oFolder)

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

'Define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmPN As String = oAsmDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value

'Get user input
Dim RUsure As DialogResult = 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 <> vbYes Then Exit Sub

oAsmDoc.SaveAs(oFolder & "\" & oAsmPN & ".stp", True) 'Export the Assembly

'Look at the files referenced by the assembly and work the referenced models
For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments  
    If oRefDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
    Dim oModelPN As String =  oRefDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
    Try  	
       	oRefDoc.SaveAs(oFolder & "\" & oModelPN & ".stp", True)
    Catch ex As Exception
       	MessageBox.Show("Error processing " & oCurFileName & vbNewLine & ex.Message, "ilogic")
    End Try
Next

'Show the folder
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'Open the folder containing the new files
Shell("explorer.exe " & oFolder,vbNormalFocus) 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 4 of 5

darrell.wcd
Enthusiast
Enthusiast

Mike,

Both the code fix from frederic.vandenplas and your code worked. But yours works 100x faster as inventor is not opening all the parts before exporting them.

 

Is iLogic and or VBA code trial and error and years of experience?

Or where can one find some course on this?

0 Likes
Message 5 of 5

Owner2229
Advisor
Advisor

I work quite a bit with Inventor API lately, so I've got some experience with iLogic, VBA, AddIns and external Inventor control.

 

Here you can find some useful reading (in my last post in the topic):

autodesk.com/#M69946

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes