Using iLogic to create a unique instance of a component and reset references

Using iLogic to create a unique instance of a component and reset references

duke
Enthusiast Enthusiast
729 Views
4 Replies
Message 1 of 5

Using iLogic to create a unique instance of a component and reset references

duke
Enthusiast
Enthusiast

I am attempting to use iLogic code to add a component and then save it as a unique instance so that I can create two or more unique instances from the same original model. The code I have written correctly adds the component, creates the new files, and appears to create the unique parts as desired. The problem is that when I close and reopen the assembly the file references do not stay the same and both parts revert to having the same dimensions and referencing only the first set of part files. How can I correct this?

 

Before  closing the file and after reopening:

saveAs_Before_Closing.pngsaveAs_After_Closing.png

 

Below is the code for my saveAs function which creates the unique files for each part and assembly:

'Performs a function similar to 'Place iLogic Component'. This rule
'acts on the component referenced by 'activeComp'. It creates new save 
'files for all of the referenced documents for the added component.

Dim comp As String
comp = activeComp
compFile = Left(comp, Len(comp) - 2) & " " & Right(comp, 1)
'The file path to the file the component and parts will be saved to
saveLoc = ThisDoc.Path & "\" & compFile & "\"

Dim doc As Document
'Reference the proper component assembly
doc = ThisAssembly.Components.Item(comp).Occurrence.Definition.Document

Try
	'Identify and save the component main document
	Dim displayName As String
	Dim ref As String
	displayName = doc.DisplayName
	ref = Left(displayName, Len(displayName) - 4)
	
	ext = "-01"
	oFile = saveLoc & ref
	While System.IO.File.Exists(oFile & ext & ".iam")
		ext = ext & "-01"
	End While
	doc.SaveAs(oFile & ext & ".iam", False)
Catch
	MsgBox("Error occurred in saving the main document. The file path may be invalid or files of the same name may already exist.", , "Error Message")
End Try

Try
	Dim doc1 As Document
	Dim name As String

	'Iterate through and save all referenced documents Of the chosen assembly
	For Each doc1 In doc.AllReferencedDocuments
		name = doc1.DisplayName
		ref1 = Left(name, Len(name) -4)
		
		If doc1.DocumentType = kAssemblyDocumentObject
			doc1.SaveAs(saveLoc & ref1 & ext & ".iam", False)
		Else
			doc1.SaveAs(saveLoc & ref1 & ext & ".ipt", False)
		End If
	Next
Catch
	MsgBox("Error occurred in saving the referenced documents. The file path may be invalid or files of the same name may already exist.", , "Error Message")
End Try

 

0 Likes
730 Views
4 Replies
Replies (4)
Message 2 of 5

johnsonshiue
Community Manager
Community Manager

Hi! Without seeing the actual iam and ipt files, it is very hard to tell where the problem is. Try restoring the default browser name. Go to Assembly Productivity tool -> Rename Browser Nodes -> Default. After that, the occurrence browser names may be reset back to default names or file names. You may need to modify the rule a bit.

Does it work afterwards?

Many thanks!



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
Message 3 of 5

duke
Enthusiast
Enthusiast

This was an excellent tool for debugging that I wasn't aware of, thank you! Unfortunately it did not solve my problem, but did yield some very interesting results. When I first add both components using the code in my original post all of the assemblies and components are unique as intended. When I save, close, and reopen the file it first prompts me to update for component changes, and now all of the default component names are the same for the 2 occurrences. Both occurrences now only reference the first set of files!

 

I have found with some experimentation that if I open one of the occurrences and change the parameters and use the "Rebuild All" button that it will often retain the changes and correct files references when I then save and close the file. I am unsure why that has the effect it does and thus am trying to identify the problem so I can create a more reliable solution.

0 Likes
Message 4 of 5

Anonymous
Not applicable

I think your issue is that you are saving the assembly and components as new files, but not replacing the parts that make up the new assembly. So the new assembly is still using the components from the old one. This post walks through it very well. Here 

0 Likes
Message 5 of 5

duke
Enthusiast
Enthusiast

From my experimentation this does not seem to be the issue. When I run the rule I can confirm that the file does change to the new reference. Running a programmed replace command at this point only throws an error, presumably because the documents are identical. It is only when the file is saved and closed that the references become corrupted.

0 Likes