iLogic: Overwrite a file with another

iLogic: Overwrite a file with another

Anonymous
Not applicable
1,676 Views
16 Replies
Message 1 of 17

iLogic: Overwrite a file with another

Anonymous
Not applicable

In my assembly I have two parts; Part1.ipt and Part2.ipt, which I have renamed to "MyPart1" and "MyPart2" in the Inventor browser. I want to overwrite MyPart2 with MyPart1, and replace it in the assembly.

 

My code is:

Dim oOcurrence As ComponentOccurrence = Components("MyPart2").Occurrence
Dim PathOfFileToBeReplaced = oOcurrence.Definition.Document.FullFileName
oOcurrence.Delete()
System.IO.File.Delete(PathOfFileToBeReplaced)
Components("MyPart1").Occurrence.Definition.Document.SaveAs(PathOfFileToBeReplaced, True)
Components.Add("MyPart2", PathOfFileToBeReplaced)

The first 4 lines run fine, I can see MyPart2 disappearing from the assembly, and Part2.ipt disappears from the folder in Windows Explorer.

But then Document.SaveAs throws an error, just saying "The parameter is incorrect.".

If I save, close, reopen the assembly and run :

Components("MyPart1").Occurrence.Definition.Document.SaveAs("C:\Testing\Part2.ipt", True)
Components.Add("MyPart2", "C:\Testing\Part2.ipt")

it works fine! (PathOfFileToBeReplaced = "C:\Testing\Part2.ipt").

 

Inventor seems to believe that "C:\Testing\Part2.ipt" exists even after the code deletes it, until I reopen the assembly. How do I solve it?

0 Likes
Accepted solutions (1)
1,677 Views
16 Replies
Replies (16)
Message 2 of 17

bradeneuropeArthur
Mentor
Mentor
Document.update

Maybe!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

Message 3 of 17

Anonymous
Not applicable

That would be

ThisDoc.Document.Update()

between line 4 & 5, right? No effect. =I

0 Likes
Message 4 of 17

fullevent
Advisor
Advisor

Hello @Anonymous,

 

try this one please.

 

ThisDoc.Document.Rebuild2
ThisDoc.Document.Update2

Otherwise it would be good if you could create a Pack&Go of the assembly and provide it to us for testing.

 

regards,
Aleks


Aleksandar Krstic
Produkt- und Projektmanager

Message 5 of 17

Anonymous
Not applicable

@fullevent That doesn't seem to have an effect either.

 

Please find my files attached.

Rule1 is the straightforward code, how I'd like it to work.

 

Rule0 is made for testing. It works like:

1. Open Assembly1.iam, Run Rule0. It recognizes that the component MyPart2 exists, saves the MyPart2 FullFileName to Parameter("File2Path"), deletes MyPart2/Part2.ipt, tries to save Part1.ipt as Part2.ipt, and throws an error.

2. Save and close Assembly1.iam.

3. Open Assembly1.iam, Run Rule0. This time, It doesn't find MyPart2, so it tries to save Part1 to path found in Parameter("File2Path"), and it succeeds!

0 Likes
Message 6 of 17

fullevent
Advisor
Advisor

@Anonymous  schrieb:

@fullevent That doesn't seem to have an effect either.

 


Hm.. ok.

There are no rules and no macros in the assembly. 🤔

Can you post the rules with full code?

 


Aleksandar Krstic
Produkt- und Projektmanager

0 Likes
Message 7 of 17

Anonymous
Not applicable

Weird that the rules don't show up. Please find them below:

 

Rule0:

ThisDoc.Document.Update()

Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oComps As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
Dim PartExists As Boolean = False
For Each oComp As ComponentOccurrence In oComps
	If oComp.Name = ("MyPart2") Then
		PartExists = True
		Exit For
	End If
Next

Dim FileToReplace As String

If PartExists Then
	Logger.Info("File Exists!")
	
	Dim oOcurrence As ComponentOccurrence = Components("MyPart2").Occurrence
	Parameter("File2Path") = oOcurrence.Definition.Document.FullFileName

	Logger.Info("To delete: " & Parameter("File2Path"))

	oOcurrence.Delete()

	System.IO.File.Delete(Parameter("File2Path"))
	
	ThisDoc.Document.Rebuild2
	ThisDoc.Document.Update2
Else
	Logger.Info("File Does NOT Exist!")
End If

	Components("MyPart1").Occurrence.Definition.Document.SaveAs(Parameter("File2Path"), True)
	Components.Add("MyPart2", Parameter("File2Path"))

 

Rule1: 

Dim oOcurrence As ComponentOccurrence = Components("MyPart2").Occurrence
Dim PathOfFileToBeReplaced = oOcurrence.Definition.Document.FullFileName
oOcurrence.Delete()
System.IO.File.Delete(PathOfFileToBeReplaced)

ThisDoc.Document.Update
ThisDoc.Document.Rebuild2
ThisDoc.Document.Update2

Components("MyPart1").Occurrence.Definition.Document.SaveAs(PathOfFileToBeReplaced, True)
Components.Add("MyPart2", PathOfFileToBeReplaced)  

 

0 Likes
Message 8 of 17

fullevent
Advisor
Advisor

@Anonymous  schrieb:

Weird that the rules don't show up. Please find them below:

 


I think something's gone wrong. Maybe because I'm using 2018 and you're using a higher one.
which Inventor version are you using?

 

Anyway, thanks for the rules.. I'll let you know when I find time to try it out.


Aleksandar Krstic
Produkt- und Projektmanager

0 Likes
Message 9 of 17

Anonymous
Not applicable

@fullevent Yeah, I'm on 2019, so that might explain it!

Thanks for taking your time, looking forward to hearing back from you! 😃

0 Likes
Message 10 of 17

bradeneuropeArthur
Mentor
Mentor

Hi,

 

Maybe you are seeing different.

I could be that the file is already been replaced correctly but:

Maybe the display name is still the old one!

 

Try to investigate this with the .displaynameoverwritten !!!

 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 11 of 17

Anonymous
Not applicable

@bradeneuropeArthur That does not appear to be the case, since running Rule1 causes an error. "System.IO.File.Delete" successfully deletes Part2.ipt, but "Document.SaveAs" doesn't create a new one.

0 Likes
Message 12 of 17

bradeneuropeArthur
Mentor
Mentor

try this:

 

Dim oOcurrence As ComponentOccurrence = Components("MyPart2").Occurrence
Dim PathOfFileToBeReplaced = oOcurrence.Definition.Document.FullFileName
oOcurrence.Delete()

ThisDoc.Document.Update

ThisDoc.Document.save

System.IO.File.Delete(PathOfFileToBeReplaced)



Components("MyPart1").Occurrence.Definition.Document.SaveAs(PathOfFileToBeReplaced, True)
Components.Add("MyPart2", PathOfFileToBeReplaced)  

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 13 of 17

bradeneuropeArthur
Mentor
Mentor

Or this one:

ThisDoc.Document.Update()

Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oComps As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
Dim PartExists As Boolean = False
For Each oComp As ComponentOccurrence In oComps
	If oComp.Name = ("MyPart2") Then
		PartExists = True
		Exit For
	End If
Next

Dim FileToReplace As String

If PartExists Then
	'Logger.Info("File Exists!")
	
	Dim oOcurrence As ComponentOccurrence = components("MyPart2").Occurrence
	Parameter("File2Path") = oOcurrence.Definition.Document.FullFileName

	Logger.Info("To delete: " & Parameter("File2Path"))

	oOcurrence.Delete()

	System.IO.File.Delete(Parameter("File2Path"))
	
	ThisDoc.Document.Rebuild2
	ThisDoc.Document.Update2
Else
	Logger.Info("File Does NOT Exist!")
End If

	Components("MyPart1").Occurrence.Definition.Document.SaveAs(Parameter("File2Path"), True)
	Component.Replace("MyPart2", "File2Path", True)

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 14 of 17

Anonymous
Not applicable

@bradeneuropeArthur Thanks to you as well for taking your time, but both of those give the same error.

 

As for the second code, the last line (Component.Replace) is never run, since the error occurs on the previous line (Document.SaveAs).

 

But also, The component MyPart2 is deleted earlier on:

Dim oOcurrence As ComponentOccurrence = components("MyPart2").Occurrence
oOcurrence.Delete()

I need to delete the component, in order to delete the file Part2.ipt:

System.IO.File.Delete(Parameter("File2Path"))

 And the file needs to be deleted in order to save copy of Part1.ipt as Part2.ipt:

Components("MyPart1").Occurrence.Definition.Document.SaveAs(Parameter("File2Path"), True)

 Which means that:

Component.Replace("MyPart2", "File2Path", True)

  would throw an error, since "MyPart2" does not exist.

Message 15 of 17

JhoelForshav
Mentor
Mentor
Accepted solution

I've seen the problem where saveas doesn't work before. For me it seemed to be because I had a document with that name still open in inventor memory. Even if the file was deleted. Try this to close unreferenced files from inventors memory before the saveas line.

 

ThisApplication.Documents.CloseAll(True)

(True indicates that only unreferenced documents should be closed)

Message 16 of 17

Anonymous
Not applicable

@JhoelForshav This did it! Phew..

I assumed it was something along those lines, but didn't know of CloseAll(True).

Thank you!

Message 17 of 17

andrzej_m
Participant
Participant

Instead of closing all documents You can set specific files to be released from memory.

If You create/use documents by code at the end of using any document call document.ReleaseReference() method to remove them from the memory.

To be sure that Your code don't hold any document after running code You can close all documents manually and in bottom-right corner should display 0 documents in memory

0 Likes