- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Alright I have a assembly named Box and it has 3 parts named mesh, side metal and support structure.
Side metal and mesh are used twice in the assembly and support structure three times. support structure doesn't need renaming.
So it's like:
Box.iam
- Mesh: 1
- Mesh: 2
- Side metal: 1
- Side metal: 2
- Support structure: 1
- Support structure: 2
- Support structure: 3
What I need is a rule to place as a button to a form. When I press it the assembly gets saved to a new location with a new name while both are user selectable and the parts get saved and replaced in this assembly with predefined names based on few parameters like length and some other text parameter.
Example:
newboxname.iam
- Mesh_lenght=1400:1
- Mesh_lenght=1400: 2
- Side metal_lenght=1400:1
- Side metal_lenght=1400: 2
- Support structure: 1
- Support structure: 2
- Support structure: 3
Also it would be great if this would work when bringing this into a larger assembly.
I think something like this would be sufficient, but no idea how this would work in another larger assembly when placing this subassembly. Currently it errors after new prefix name has been given in larger assembly after placement. The code below also asks one time for new and old prefix, so it only works for one part. Two times would be great:
Sub Main If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "") Exit Sub End If Dim oADoc As AssemblyDocument = ThisDoc.Document Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments If oRefDocs.Count = 0 Then Exit Sub Dim sNewFullFileName As String = UseSaveAsDialog 'runs the custom Function below If sNewFullFileName = "" Then Exit Sub oADoc.SaveAs(sNewFullFileName, False) oADoc = ThisDoc.Document Dim sNewPath As String = ThisDoc.Path Dim sOldPrefix As String = InputBox("Please enter old prefix", "Warning", "") Dim sNewPrefix As String = InputBox("Please enter new prefix", "Warning", "") 'this next variable is to store a list of old & new FullFileNames, so we can replace them all easier Dim oDict As New Dictionary(Of String, String) 'oldFullFileName, then newFullFileName For Each oRefDoc As Document In oRefDocs 'they are already open, by default Dim sFileName As String = System.IO.Path.GetFileName(oRefDoc.FullFileName) Dim sNewFileName As String = sFileName.Replace(sOldPrefix, sNewPrefix) Dim sNewFullName As String = System.IO.Path.Combine(sNewPath, sNewFileName) oRefDoc.SaveAs(sNewFullName, True) oDict.Add(oRefDoc.FullFileName, sNewFullName) Next 'oRefDoc Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences RecursivelyReplaceAllComponents(oOccs, oDict) 'run our custom Sub below If oADoc.RequiresUpdate Then oADoc.Update2(True) If oADoc.Dirty Then oADoc.Save2(True) ThisApplication.Documents.CloseAll(True) End Sub Function UseSaveAsDialog() As String 'just reaturns new file name, does not actually save Dim oFileDialog As Inventor.FileDialog = Nothing ThisApplication.CreateFileDialog(oFileDialog) oFileDialog.DialogTitle = "Specify New Name & Location For Copied Assembly" oFileDialog.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath oFileDialog.Filter = "Autodesk Inventor Assemblies (*.iam) | *.iam" oFileDialog.FileName = ThisDoc.FileName(False) oFileDialog.MultiSelectEnabled = False oFileDialog.OptionsEnabled = False oFileDialog.InsertMode = False oFileDialog.CancelError = True Try : oFileDialog.ShowSave : Return oFileDialog.FileName Catch : Return "" : End Try End Function Sub RecursivelyReplaceAllComponents(oComps As ComponentOccurrences, oFileNamePairs As Dictionary(Of String, String)) If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub If IsNothing(oFileNamePairs) OrElse oFileNamePairs.Count = 0 Then Exit Sub For Each oComp As ComponentOccurrence In oComps For Each oPair In oFileNamePairs If oComp.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName = oPair.Key Then Try : oComp.Replace(oPair.Value, True) : Catch : End Try End If Next 'oPair If oComp.SubOccurrences.Count > 0 Then RecursivelyReplaceAllComponents(oComp.SubOccurrences, oFileNamePairs) End If Next 'oComp End Sub
Edit: When I bring this assembly to a larger assembly using save and replace where I give it a name like box1400 and after that I open the subassembly to run this code I can't replace the current file named box1400 with this. With a new name it kinda works, but the old file still exists in the folder. It also runs into error if I'm trying to save into a folder where there is another copy of this subassembly parts and it saves every part like they would be another ones instance:
newboxname.iam
- Mesh_lenght=1400:1
- Mesh_lenght=1400: 2
- Side metal: 3
- Side metal: 4
- Support structure: 5
- Support structure: 6
- Support structure: 7
Solved! Go to Solution.