Save and replace every sub-assembly and part in a main assembly with iLogic

Save and replace every sub-assembly and part in a main assembly with iLogic

Anonymous
Not applicable
2,622 Views
7 Replies
Message 1 of 8

Save and replace every sub-assembly and part in a main assembly with iLogic

Anonymous
Not applicable

Hello all,

 

I am new to iLogic and was hoping to get some help with my iLogic code. My goal is to have our staff open the master assembly and then be able to save a copy with all new subassemblies and parts and be able to make changes to the new assembly without affecting the master.

 

I've looked into "iLogic Design Copy" and the Design assistant but this process needs to be automated as it will  happen hundreds of (or more) times.

 

It is very similar to https://forums.autodesk.com/t5/inventor-customization/save-and-replace-parts-in-an-assembly-using-il.... I have adapted the solution given by @CurtisWaguespack and it is working up until it has to replace all parts and subassemblies. That original solution does not run through all SubOccurences so I have tried to "Traverse" through the assembly.

 

Not sure if this is the right method and have tried multiple times to make it work with no such luck. 

 

Any help would be greatly appreciated.

 

Thanks

 

 

Sub Main()
	
	SaveAndReplaceThisDoc()
	
End Sub

Sub SaveAndReplaceThisDoc()
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Assembly Documents.",vbOK, "WRONG DOCUMENT TYPE")
Return
End If

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim oFileDlg As Inventor.FileDialog = Nothing
ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
oFileDlg.FileName = IO.Path.GetFileName(oADoc.FullFileName).TrimEnd("."c,"i"c,"a"c,"m"c)
oFileDlg.DialogTitle = "Specify New Name & Location For Copied Assembly"
oFileDlg.CancelError = True

On Error Resume Next
oFileDlg.ShowSave
If Err.Number <> 0 Then
	MsgBox("No File Saved.", vbOKOnly, "DIALOG CANCELED")
ElseIf oFileDlg.FileName <> "" Then
	oNewFileName = oFileDlg.FileName
	oADoc.SaveAs(oNewFileName, False)
End If


oADoc = Nothing

InventorVb.DocumentUpdate()
i = 1
j = 100

oADoc = ThisApplication.ActiveDocument

Dim oLast3Chars As String
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	ThisApplication.Documents.Open(oRefDoc.FullFileName,False)
	oLast3Chars = i 
	If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		 oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".ipt", True)
	ElseIf oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		 oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".iam", True)
	End If
	oRefDoc.Close
	i = i + 1
	'MessageBox.Show(oRefDoc.DisplayName & vbCrLf & oADoc.DisplayName, "Debug 1")
Next

Dim oOccDoc As Document
Dim oOccNewFileName As String
oADoc = ThisApplication.Documents.Open(oADoc.FullFileName,False)
	For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
	oOccDoc = oOcc.Definition.Document
	oLast3Chars = j			
	If oOccDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		oOccNewFileName = Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".ipt"
		
	ElseIf oOccDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oOccNewFileName = Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".iam"
		
	End If
	If oOcc.SubOccurrences.Count >0 Then
		Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)
	End If
	oOcc.Replace(oOccNewFileName, True)   
	j = j + 1
	'MessageBox.Show(oOccDoc.DisplayName & vbCrLf & oOccNewFileName & vbCrLf & String.Format(oOcc.Name) , "Debug 2") ' 
Next

End Sub



 Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer)

    Dim oOccur As ComponentOccurrence
    For Each oOccur In Occurrences

		'MessageBox.Show(oOccur.Name, "oOccur.Name")
     

        Call TraverseAssembly(oOccur.SubOccurrences, Level + 1)


    Next
 
End Sub  

 

chartleyWAPKZ_0-1617114418451.png

chartleyWAPKZ_1-1617114428771.png

Trying to replace all these sub assemblies and parts within them.

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

Ralf_Krieg
Advisor
Advisor

Hello

 

Try this one.

Keep in mind, this is a very, very simple script. It doesn't keep track of suppressed files, derived parts, level of details and so on.

Sub Main()
    SaveAndReplaceThisDoc()
End Sub

Sub SaveAndReplaceThisDoc()
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Assembly Documents.",vbOK, "WRONG DOCUMENT TYPE")
		Return
	End If

	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument

	Dim oFileDlg As Inventor.FileDialog = Nothing
	ThisApplication.CreateFileDialog (oFileDlg)
	oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
	oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oFileDlg.FileName = IO.Path.GetFileName(oADoc.FullFileName).TrimEnd("."c,"i"c,"a"c,"m"c)
	oFileDlg.DialogTitle = "Specify New Name & Location For Copied Assembly"
	oFileDlg.CancelError = True

	On Error Resume Next
	oFileDlg.ShowSave
	If Err.Number <> 0 Then
	    MsgBox("No File Saved.", vbOKOnly, "DIALOG CANCELED")
		Exit Sub
	ElseIf oFileDlg.FileName <> "" Then
	    oNewFileName = oFileDlg.FileName
	    oADoc.SaveAs(oNewFileName, False)
	End If

	InventorVb.DocumentUpdate()
	i = 1
	'j = 100

	oADoc = ThisApplication.ActiveDocument
	
	Dim sNewPath As String = IO.Path.GetDirectoryName(oADoc.FullFileName )
	Dim sNewFilename As String
	Dim oLast3Chars As String
	
	For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	    ThisApplication.Documents.Open(oRefDoc.FullFileName, False)
		sNewFilename=IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	    oLast3Chars = i
	    If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	         oRefDoc.SaveAs(sNewPath & "\" & sNewFilename & oLast3Chars & ".ipt",True)	'(Left(oRefDoc.FullFileName, Len(oRefDoc.FullFileName) -4) & oLast3Chars & ".ipt", True)
	    ElseIf oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	         oRefDoc.SaveAs(sNewPath & "\" & sNewFilename & oLast3Chars & ".iam",True)	'(Left(oRefDoc.FullFileName, Len(oRefDoc.FullFileName) -4) & oLast3Chars & ".iam", True)
	    End If
	    oRefDoc.Close
	    'i = i + 1
	    'MessageBox.Show(oRefDoc.DisplayName & vbCrLf & oADoc.DisplayName, "Debug 1")
	Next
	
	Dim oOccDoc As Document
	Dim oOccNewFileName As String
	
	For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
	    oOccDoc = oOcc.Definition.Document
		sNewFilename=IO.Path.GetFileNameWithoutExtension(oOccDoc.FullFileName)
	    oLast3Chars = i
	    If oOccDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	        oOccNewFileName = sNewPath & "\" & sNewFilename & oLast3Chars & ".ipt"	'Left(oOccDoc.FullFileName, Len(oOccDoc.FullFileName) - 4) & oLast3Chars & ".ipt"
	    ElseIf oOccDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	        oOccNewFileName = sNewPath & "\" & sNewFilename & oLast3Chars & ".iam"	'Left(oOccDoc.FullFileName, Len(oOccDoc.FullFileName) - 4) & oLast3Chars & ".iam"
	    End If
	    
		oOcc.Replace(oOccNewFileName, True)
		
		If oOcc.SubOccurrences.Count > 0 Then
	        Call TraverseAssembly(oOcc.SubOccurrences, sNewPath, i)
	    End If
	    
	    'j = j + 1
	    'MessageBox.Show(oOccDoc.DisplayName & vbCrLf & oOccNewFileName & vbCrLf & String.Format(oOcc.Name) , "Debug 2") '
	Next

	InventorVb.DocumentUpdate()
	
End Sub



 Sub TraverseAssembly(Occurrences As ComponentOccurrences, sNewPath As String, i As Integer)


	Dim oOccDoc As Document
	Dim oOccNewFileName As String
	
    For Each oOcc As ComponentOccurrence In Occurrences
        oOccDoc = oOcc.Definition.Document
		sNewFilename=IO.Path.GetFileNameWithoutExtension(oOccDoc.FullFileName)
	    oLast3Chars = i
	    If oOccDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	        oOccNewFileName = sNewPath & "\" & sNewFilename & oLast3Chars & ".ipt"	'Left(oOccDoc.FullFileName, Len(oOccDoc.FullFileName) - 4) & oLast3Chars & ".ipt"
		ElseIf oOccDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	        oOccNewFileName = sNewPath & "\" & sNewFilename & oLast3Chars & ".iam"	'Left(oOccDoc.FullFileName, Len(oOccDoc.FullFileName) - 4) & oLast3Chars & ".iam"
	    End If

		oOcc.Replace(oOccNewFileName, True)
		
		If oOcc.SubOccurrences.Count > 0 Then
	        Call TraverseAssembly(oOcc.SubOccurrences, sNewPath, i)
	    End If
	    
	    'j = j + 1
	    'MessageBox.Show(oOccDoc.DisplayName & vbCrLf & oOccNewFileName & vbCrLf & String.Format(oOcc.Name) , "Debug 2") '
    Next
 
End Sub

R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 3 of 8

Anonymous
Not applicable

Thank you @Ralf_Krieg,

 

It is now replacing the parts in each subassembly but not the replacing the sub assemblies in the main assembly.

I have attached a very simplified assembly for reference.

0 Likes
Message 4 of 8

Ralf_Krieg
Advisor
Advisor

Hello

 

Sorry, I can't reproduce this behaviour. On left side the model browser of the original and on the right side the copied assembly.

 

orig-new.jpg


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 5 of 8

Anonymous
Not applicable

Okay thanks for trying @Ralf_Krieg. I'm not really sure what is going on. This is what I am getting.

TheCK48_1-1617304338004.png

But I will keep trying and let you know if I figure it out. Thanks Again.

0 Likes
Message 6 of 8

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

I have copied your test assembly around 10 times with different results.

4 times all copied correct like my first test

4 times your mix with not replaced subassemblies

2 times a mix with replaced subassemblies but not all parts were replaced

 

I could not find a reproducable reason for this. I think this could founded in the occurrences collection. Replacing an occurrence while traversing the collection, could invalidate the collection. Maybe I'm right, maybe that's nonce.

So I started thinking about an alternative way. I remembered some years ago a tool named CopyDesign in the UserTools from the SDK. This tool copies a drawing with all referenced documents. Not a perfect fit to your needs, but a good base.

I modified the program to copy an assembly with all referenced documents and customized the GUI. It runs as stand alone exe.

If you want to start with an assembly currently opened in Inventor, create an iLogic rule and paste this code

Dim sFullDocname As String = ThisDoc.Document.FullDocumentName

Dim myprocess As System.Diagnostics.Process = New System.Diagnostics.Process()
With myprocess.StartInfo
    .FileName = ("C:\Temp\CopyDesign.exe") '<----------------------------MODIFY !!!!!!!!!!!
    .CreateNoWindow = False
    .Arguments = Chr(34 )+ sFullDocname + Chr(34)
    .RedirectStandardInput = False
    .RedirectStandardOutput = False
    .RedirectStandardError = False
    .UseShellExecute = True
End With

myprocess.Start()

. The program starts with a pre-filled field for the assembly to copy. Select a folder where to save the copy. You can add a prefix and/or suffix to the copied assembly, parts and subassemblies. If you select to make more than one copy, a subfolder for each copy is created in the target folder you selected above.

 

Can you try, if this works?


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 7 of 8

Anonymous
Not applicable

Yes that is perfect! Thanks so much for your help! This will do the job nicely.

0 Likes
Message 8 of 8

Stakin
Collaborator
Collaborator
0 Likes