
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Trying to replace all these sub assemblies and parts within them.
Solved! Go to Solution.