ilogic for save as assembly including child parts into new location

ilogic for save as assembly including child parts into new location

yathul_k
Explorer Explorer
117 Views
1 Reply
Message 1 of 2

ilogic for save as assembly including child parts into new location

yathul_k
Explorer
Explorer

iLogic for save as the assembly includes the child parts into a new Location. After doing this, the new child parts should be referenced or linked with the new assembly.

 

The Following will used for Save as the assembly and child parts into a new location, but the parts wouldn't linked with the new assembly.

 

' iLogic rule to copy an assembly and its referenced files to a new location

' Ensure the current document is an assembly
Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
If oAsmDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("This rule only works on assembly documents.", "Error")
Return
End If

' Prompt user to select a new target folder
Dim fbd As New System.Windows.Forms.FolderBrowserDialog
fbd.Description = "Select the folder where the assembly and its parts will be copied"
If fbd.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then Return

Dim targetFolder As String = fbd.SelectedPath

' Prompt for new assembly name
Dim newAsmName As String = InputBox("Enter the new assembly file name (without extension):", "Save As", "NewAssembly")
If String.IsNullOrWhiteSpace(newAsmName) Then Return

Dim fso As Object = CreateObject("Scripting.FileSystemObject")

' === 1. Copy all referenced documents ===
Dim refDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
For Each refDoc As Document In refDocs
If Not String.IsNullOrEmpty(refDoc.FullFileName) Then
Dim srcPath As String = refDoc.FullFileName
Dim destPath As String = System.IO.Path.Combine(targetFolder, System.IO.Path.GetFileName(srcPath))
If Not System.IO.File.Exists(destPath) Then
System.IO.File.Copy(srcPath, destPath)
End If
End If
Next

' === 2. Copy the assembly file itself with the new name ===
Dim newAsmPath As String = System.IO.Path.Combine(targetFolder, newAsmName & ".iam")
Dim currentAsmPath As String = oAsmDoc.FullFileName
System.IO.File.Copy(currentAsmPath, newAsmPath, True)

' === 3. Open the new assembly in Inventor ===
Dim newDoc As AssemblyDocument = ThisApplication.Documents.Open(newAsmPath, True)
MessageBox.Show("Assembly and components saved to: " & targetFolder, "Success")

0 Likes
118 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor

Hi @yathul_k.  This looks like a 'design copy' scenario, which has been a relatively common topic in this forum over the years.  Not really something that I use, but I understand the concept and a lot of what it involves.  After creating copies of all the model documents involved with the assembly, you must open each 'new' assembly, iterate through its top level components (ComponentOccurrences and ComponentOccurrence), and use the 'component replace' method (ComponentOccurrence.Replace or ComponentOccurrence.Replace2) to replace each one, which is where you change it from referencing the 'old' file to the 'new' one.  Using the assembly component replace method preserves all the assembly constraints (usually, as long as the 'old' and 'new' share ancestry).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)