Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
nsheldonGGNEN
537 Views, 5 Replies

iLogic for Save and Replace within an assembly by part name

Hello All!

 

I'm wondering if someone can help me with some iLogic code. Basically I want to perform the "Save and Replace" fuction within an assembly, but via iLogic. I'd like the code to be able to find a part within an assembly by name, say "cube.ipt", and save a copy of cube.ipt using a filename I've already generated earlier in the code, say "my_custom_name.ipt", and then replace cube.ipt with my_custom_name.ipt in the assembly.

 

If this isn't possible, perhaps code that at least can save a copy of a given part? I can always replace a component but I need to generate the copy first.

 

Any advice is greatly appreciated. Thanks!

Do you want to replace all occurrences of this 'cube.ipt'?

 

If you have the full filename of the part you can, to save a copy of this part:

 

Dim doc as PartDocument = ThisApplication.Documents.Open(filename)
Call doc.SaveAs(newFilename, true)

 

Then find the occurrence to be replaced in the assembly, either loop through the assemblies all (leaf) occurrences or access the occurrence by name (ItemByName) from the assemblies component definition. You can then replace it using:

 

ComponentOccurrence.Replace(filename, true) ' true/false depending on replace all

 

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


dalton98
in reply to: nsheldonGGNEN

This should be what your looking for. First it goes through all reference docs and selects one with "cube.ipt" in its file path. Then it creates a copy of that naming it "my_custom_name". Then it makes a privite event to skip the dialog box. Then it runs the replace all cmd. Finally it refreshes the browser nodes.

Dim oAss As AssemblyDocument
oAss = ThisApplication.ActiveDocument
Dim oDoc As Document
For Each oDoc In oAss.AllReferencedDocuments
	If InStr(oDoc.FullDocumentName, "cube.ipt") > 0
		template = oDoc.FullDocumentName
		newlength = InStrRev(oDoc.FullDocumentName, "\")
		newfile = Left(template, newlength) & "my_custom_name.ipt"
		IO.File.Copy(template, newfile, True)

		oDocName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullDocumentName)
ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, newfile)
		compo = Component.InventorComponent(oDocName & ":1")
		oAss.SelectSet.Select(compo)
			
		
		Dim oReplaceCmd As ControlDefinition
		oReplaceCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyReplaceAllCmd")
		oReplaceCmd.Execute
		
		Dim oRefreshNodes As ControlDefinition
		oRefreshNodes = ThisApplication.CommandManager.ControlDefinitions.Item("MoldRefreshBrowserNodes")
		oRefreshNodes.Execute
		

	End If
Next

Hi!

 

Thanks for your reply. Could you expand on how to call a specific instance by name and replace only that instance of a component? I attempted it uncessfully.

 

Thanks!

nsheldonGGNEN
in reply to: dalton98

Hi!

 

Thanks for your reply. The code worked well for me! Only follow-up question would be is there a way to modify the code to only replace a specific instance of a component, rather than replacing all of them? This wasn't part of my original thought, but I could see this functionality being useful.

 

Thank you for helping with this!

dalton98
in reply to: nsheldonGGNEN

@nsheldonGGNEN 

Replace it with this "AssemblyReplaceCmd"

Someone made a helpful website that lists all the api commands:

https://spiderinnet2.typepad.com/blog/2012/07/inventor-net-find-all-the-control-definitions-1.html