Assembly and some of its parts renamed and saved as new copy.

Assembly and some of its parts renamed and saved as new copy.

e2000773
Enthusiast Enthusiast
796 Views
8 Replies
Message 1 of 9

Assembly and some of its parts renamed and saved as new copy.

e2000773
Enthusiast
Enthusiast

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

 

0 Likes
Accepted solutions (2)
797 Views
8 Replies
Replies (8)
Message 2 of 9

e2000773
Enthusiast
Enthusiast

Ok, I have another code that almost works. The thing what is wrong with it is that it saves parts with the new name, but not the assembly. It never saves the assembly and I get errors during saving. the individual parts are saved though, but it doesn't always save some of them. Can you guys help me out? It would be really important for it to work on a larger assembly when this is placed in it too as a subassembly. Like I place it, run the code -> Happy days. 

The code:

' Get current location of where you want the search area to be.

Dim ExportPath As String = "C:\"

 

 

' Define folder browse dialog

Dim Dialog = New FolderBrowserDialog()

 

' Set options for folder browser dialog

Dialog.SelectedPath = ExportPath

Dialog.ShowNewFolderButton = True

Dialog.Description = "Choose Folder for Export..."

 

' Show dialog box

If DialogResult.OK = Dialog.ShowDialog() Then

        ' User clicked 'ok' on dialog box - capture the export path

        ExportPath = Dialog.SelectedPath & "\"

       

Else

        ' User clicked 'cancel' on dialog box - exit

        Return

End If

 

 

Dim Path As String = ExportPath

MessageBox.Show(Path, "Directory path")

 

InvDoc = ThisDoc.Document

Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments

Dim refDoc As Document

 

For Each refDoc In refDocs

    'MessageBox.Show(refDoc.DisplayName)

    If refDoc.DisplayName.EndsWith("Verkko_kopa.ipt")= True Then

        NewFileName = "Mesh=" & Length & ".ipt"

        refDoc.SaveAs(Path & NewFileName, False)

    End If

        If refDoc.DisplayName.EndsWith("Side_metal.ipt")= True Then

        NewFileName = "Side_metal=" & Lenght & ".ipt"

        refDoc.SaveAs(Path & NewFileName, False)

    End If

        If refDoc.DisplayName.EndsWith("Support_structure.iam")= True Then

        NewFileName = "Support_structure.iam"

        refDoc.SaveAs(Path & NewFileName, False)

    End If

Next

 

 

ThisDoc.Document.SaveAs("Box=" & Lenght & ".iam", False)

 

iLogicVb.UpdateWhenDone = True

 

0 Likes
Message 3 of 9

A.Acheson
Mentor
Mentor

Hi @e2000773 

Because the assembly contains the fullfilepath to the part files it contains. You will need to save as of each sub assembly then save as of each part within the assembly and then occurrence replace the part reference file links. This is how ilogic copy design is working. 

 

See this post here on how this is achieved.

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/help-regarding-renaming-assembly-fil...

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 4 of 9

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi  @e2000773 . You need to add the full file storage path "Path & "Box=" & Lenght & ".iam"".

ThisDoc.Document.SaveAs(Path & "Box=" & Lenght & ".iam", False)

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 5 of 9

e2000773
Enthusiast
Enthusiast

Should it be Exportpath? Like in the code there isn't a Path mentioned.

0 Likes
Message 6 of 9

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Yes, you need a storage path to save the file, in your folder it is: Dim Path As String = ExportPath.

Path.png

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 7 of 9

e2000773
Enthusiast
Enthusiast

.

0 Likes
Message 8 of 9

e2000773
Enthusiast
Enthusiast

OK, ExportPath (and Path) worked. 

0 Likes
Message 9 of 9

e2000773
Enthusiast
Enthusiast

Does anyone have any idea why it only works once in a larger assembly when I have placed this as subassembly?

Place one -> run code -> changes and saves the file name and copy -> Place another -> either it has correct names or the same new names I have on the first one either way doesn't work the second time. If it has the original names it will not change the mesh-parts name  or save copy of the subassembly and runs into error. Also curious is if I close the large assembly and open the name of the first subassembly is now correct renamed one. 

0 Likes