Help regarding renaming assembly file and a part file in it and saving it at same location using iLogic

Help regarding renaming assembly file and a part file in it and saving it at same location using iLogic

sohaib.as01
Advocate Advocate
1,960 Views
7 Replies
Message 1 of 8

Help regarding renaming assembly file and a part file in it and saving it at same location using iLogic

sohaib.as01
Advocate
Advocate

Hello everyone,

I have put together a code which renames the assembly file, one specific part file (which is Litecode Acrylic.ipt) within that assembly and all the related/referenced drawing files I want to rename and overwrites and saves them in the same directory.

The code is working fine for everything, except that after the replacement and everything, when I reopen the newly renamed assembly file,it is still referencing and asking for that one part file that I renamed using iLogic. All the other references are maintained fine, so I guess I am not replacing the reference correctly? I am using this line of code to replace reference of my part file;

 

Call assOcc.ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(ThisDoc.Path & "\" & newFileName)

 

Here is the full code in case someone needs it for their projects.

 

oApp = ThisApplication
oAD = oApp.ActiveDocument
oACD = oAD.ComponentDefinition

          MasterPos = oACD.RepresentationsManager.PositionalRepresentations.Item("Master")
		  MasterPos.Activate
ThisDoc.Save()

'Getting filenames/part numbers from excel file PN_AssyM = GoExcel.CellValue("SHEET_LXJ LIGHTSHEET linked data 082618.xlsx", "Sheet1", "J24") PN_Litecode = GoExcel.CellValue("SHEET_LXJ LIGHTSHEET linked data 082618.xlsx", "Sheet1", "J23") PN_Lightstick = GoExcel.CellValue("SHEET_LXJ LIGHTSHEET linked data 082618.xlsx", "Sheet1", "J25") ' Rename and replace main assembly file assemblyFileName = ThisDoc.PathAndFileName & ".iam" newAssemblyFileName = ThisDoc.Path & "\" & PN_AssyM & ".iam" drawingFileName = assemblyFileName.Replace(".iam", ".idw") newDrawingFileName = ThisDoc.Path & "\" & PN_AssyM & ".idw" IO.File.Move(assemblyFileName, newAssemblyFileName) 'Updating assembly drawing drawingFile = ThisApplication.Documents.Open(drawingFileName) drawingFile.ReferencedDocumentDescriptors.Item(1).ReferencedFileDescriptor.ReplaceReference(newAssemblyFileName) drawingFile.Update() drawingFile.Close IO.File.Move(drawingFileName, newDrawingFileName) 'Updating Litestick drawing oldLsDrawingFileName = ThisDoc.Path & "\" & "Lightstick Drawing.idw" newLsDrawingFileName = ThisDoc.Path & "\" & PN_Lightstick & ".idw" lsDrawingFile = ThisApplication.Documents.Open(oldLsDrawingFileName) lsDrawingFile.ReferencedDocumentDescriptors.Item(1).ReferencedFileDescriptor.ReplaceReference(newAssemblyFileName) lsDrawingFile.Update() lsDrawingFile.Close IO.File.Move(oldLsDrawingFileName, newLsDrawingFileName) ''Renaming and updating Litecode acrylic part file Dim fileMgr As FileManager fileMgr = ThisApplication.FileManager For Each assOcc As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences If assOcc.Definition.Document.DisplayName = "Litecode acrylic.ipt" Then oldFileName = "Litecode acrylic.ipt" newFileName = PN_Litecode & ".ipt" partDrawingFileName = ThisDoc.Path & "\" & "Litecode acrylic.idw" newPartDrawingFileName = ThisDoc.Path & "\" & PN_Litecode & ".idw" Call fileMgr.CopyFile(oldFileName, newFileName) Call assOcc.ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(ThisDoc.Path & "\" & newFileName) Call fileMgr.DeleteFile(oldFileName) ''Updating drawing file for litecode acrylic partDrawing = ThisApplication.Documents.Open(partDrawingFileName) partDrawing.ReferencedDocumentDescriptors.Item(1).ReferencedFileDescriptor.ReplaceReference(ThisDoc.Path & "\" & newFileName) partDrawing.Update() partDrawing.Close IO.File.Move(partDrawingFileName, newPartDrawingFileName) End If Next

 

0 Likes
1,961 Views
7 Replies
Replies (7)
Message 2 of 8

sohaib.as01
Advocate
Advocate

No one?? @JhoelForshav ?

0 Likes
Message 3 of 8

JhoelForshav
Mentor
Mentor

Hi @sohaib.as01 

It's not easy to help if you don't attach any dataset for us to test the code with. I can only speak for myself but if answering a question here requires me to create an entire assembly with parts and drawings just in order to test some code, I often feel like I don't have the time or energy to do so.

 

You can replace a reference to an occurrence the way you do here if the file is a copy of the original.

I think your problem is more the way you rename the document in which you're working (IO.File.Move). If I create an assembly and rename it like that, the entire assembly file gets messed up and I cannot save it afterwards (The assembly document has properties for filename, document name etc that doesn't correspond to the actual file name anymore).

I'd say your best option would be to create a copy of the assembly with a different name and change its references. I cant think of any robust way to rename a file while you have it open like that.

 

This thread may be helpful:

https://forums.autodesk.com/t5/inventor-customization/restoration-of-broken-links-using-ilogic/m-p/9...

 

Message 4 of 8

JhoelForshav
Mentor
Mentor

After some further thought... What if you rename your file this way?

Dim oldFileName As String = ThisDoc.Document.FullFileName
Dim newFileName As String = "C:\Users\Jhoel\Desktop\Forum\NewName.iam"
ThisDoc.Document.SaveAs(newFileName, False) 'Save with new name
IO.File.Delete(oldFileName) 'Delete old file

That seems to work... Then just remember to save the assembly after you've changed the references.

0 Likes
Message 5 of 8

sohaib.as01
Advocate
Advocate

hey @JhoelForshav, you are qute right, its quite uneasy and difficult to understand the code as to how it runs when you dont have the complete data set for the code to run onto. Thats why I providing my assembly and all the referenced files here so that you can better understand whats causing the problem.

When you open the assembly a form will pop up, just press the "Rename and Overwrite files" button and it will rename and overwrite the files in same directory while the main assembly is open.

Now after renaming, the workflow I devised tells me to close the opened assembly without saving it and then reopen the newly renamed main assembly file, and here comes the problem that it now asks for the missing part "Litecode Acrylic.ipt" even though I have tried replacing its reference in the rule named "Renaming parts".

Hope you can better understand my problem now 🙂

0 Likes
Message 6 of 8

JhoelForshav
Mentor
Mentor

@sohaib.as01 

As I said. The approach is wrong from the beginning. You can't rename the files like that - it will corrupt the files.

I ran your code from the form and when it completed I couldn't even close the form. The assembly file that remains open doesn't exist anymore  - its file properties doesn't match what exists in the system.

When you get this type of error trying to close an iLogic form you know something is very wrong with the file.

error.PNG

0 Likes
Message 7 of 8

JhoelForshav
Mentor
Mentor

I rewrote the rule using my suggested approach (Saving the files with the new names and then delete the originals). It works for me when I've tested it 🙂

iLogicVb.Automation.RulesOnEventsEnabled = False
Try	
Dim FilesToDelete As New List(Of String)
Dim oAsm As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmDef As AssemblyComponentDefinition = oAsm.ComponentDefinition
Dim MasterPos As PositionalRepresentation = oAsmDef.RepresentationsManager.PositionalRepresentations.Item("Master")
MasterPos.Activate
oAsm.Save
PN_AssyM = GoExcel.CellValue("SHEET_LXJ LIGHTSHEET linked data 082618.xlsx", "Sheet1", "J24")
PN_Litecode = GoExcel.CellValue("SHEET_LXJ LIGHTSHEET linked data 082618.xlsx", "Sheet1", "J23")
PN_Lightstick = GoExcel.CellValue("SHEET_LXJ LIGHTSHEET linked data 082618.xlsx", "Sheet1", "J25")
' Rename and move file
assemblyFileName = ThisDoc.PathAndFileName & ".iam"
FilesToDelete.Add(assemblyFileName)
newAssemblyFileName = ThisDoc.Path & "\" & PN_AssyM & ".iam"
'Save assembly with new name
oAsm.SaveAs(newAssemblyFileName, False)
'Replace part reference
For Each oRefDoc As Document In oAsm.ReferencedDocuments
	If oRefDoc.DisplayName = "Litecode acrylic.ipt"
		Dim oRefDocFileName As String = oRefDoc.FullFileName
		FilesToDelete.Add(oRefDocFileName)
		Dim oRefDocNewFileName As String = IO.Path.GetDirectoryName(oRefDocFileName) & "\" & PN_Litecode & ".ipt"
		oRefDoc.SaveAs(oRefDocNewFileName, False)
		oAsm.File.ReferencedFileDescriptors.Item(oRefDocFileName).ReplaceReference(oRefDocNewFileName)
		'And the drawing
		Dim DrawingFileName As String = IO.Path.GetDirectoryName(oRefDocFileName) & "\" & IO.Path.GetFileNameWithoutExtension(oRefDocFileName) & ".idw"
		FilesToDelete.Add(DrawingFileName)
		Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(DrawingFileName, False)
		oDrawDoc.File.ReferencedFileDescriptors.Item(oRefDocFileName).ReplaceReference(oRefDocNewFileName)
		oDrawDoc.Update()
		oDrawDoc.SaveAs(IO.Path.GetDirectoryName(oRefDocNewFileName) & "\" & IO.Path.GetFileNameWithoutExtension(oRefDocNewFileName) & ".idw", False)
		oDrawDoc.Close
		oAsm.Save
		Exit For
	End If
Next
'Update assembly drawing
Dim AsmDrawingFileName As String = IO.Path.GetDirectoryName(assemblyFileName) & "\" & IO.Path.GetFileNameWithoutExtension(assemblyFileName) & ".idw"
FilesToDelete.Add(AsmDrawingFileName)
Dim AsmDrawingFileNameNew As String = IO.Path.GetDirectoryName(newAssemblyFileName) & "\" & IO.Path.GetFileNameWithoutExtension(newAssemblyFileName) & ".idw"
Dim oAssyDrawing As DrawingDocument = ThisApplication.Documents.Open(AsmDrawingFileName, False)
oAssyDrawing.File.ReferencedFileDescriptors.Item(assemblyFileName).ReplaceReference(newAssemblyFileName)
oAssyDrawing.Update()
oAssyDrawing.SaveAs(AsmDrawingFileNameNew, False)
oAssyDrawing.Close

'Update LightStick drawing

Dim lsDrawingFileName As String = IO.Path.GetDirectoryName(assemblyFileName) & "\" & "Lightstick Drawing.idw"
FilesToDelete.Add(lsDrawingFileName)
Dim newLsDrawingFileName As String = IO.Path.GetDirectoryName(assemblyFileName) & "\" & PN_Lightstick & ".idw"
Dim lsDrawing As DrawingDocument = ThisApplication.Documents.Open(lsDrawingFileName, False)
lsDrawing.File.ReferencedFileDescriptors.Item(assemblyFileName).ReplaceReference(newAssemblyFileName)
lsDrawing.Update()
lsDrawing.SaveAs(newLsDrawingFileName, False)
lsDrawing.Close

ThisApplication.Documents.CloseAll(True)
For Each oFileName As String In FilesToDelete
	IO.File.Delete(oFileName)
Next

oAsm.Update
MessageBox.Show("Success!", "Rename files", MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch
iLogicVb.Automation.RulesOnEventsEnabled = True
End Try
iLogicVb.Automation.RulesOnEventsEnabled = True
0 Likes
Message 8 of 8

sohaib.as01
Advocate
Advocate

Hey @JhoelForshav  Thanks for the reply and your kind guidance.

In the back of my head I kind of knew that the issue might be coz of the way I was saving the file, but I just didnt know for sure how to avoid this workflow. I will test out your code tomorrow and see if it works fine for me, as to what I want to achieve. Though it does look perfect when I am dry running it.

Bundle of thanks mate.

0 Likes