Need iLogic code for rename component name in model browser

Need iLogic code for rename component name in model browser

Anonymous
Not applicable
3,421 Views
16 Replies
Message 1 of 17

Need iLogic code for rename component name in model browser

Anonymous
Not applicable

Hi,

 

I have created a parametric assembly file in which I given iLogic rule to replace a component.

But issue is that we have a addin that rename component name in model browser to file name, due that iLogic rule unable to replace the component.

So I need a iLogic code that can rename component name in model browser for a while so that iLogic rule can run.

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

dutt.thakar
Collaborator
Collaborator

@Anonymous 

 

To rename a specific component in the model browser you can use this method. As an example, if you have a part named "X" in the model browser and you want to name it to "Y" see below code.

 

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oOcc As ComponentOccurrence
For Each oOcc In oDef.Occurrences 
	If oOcc.Name = "X"
		oOcc.Name = "Y"
	End If
Next

 

Some notes here, If you want to rename your occurrences in a specific order you also need to arrange them inside assembly in a specific order and rename them sequentially because, if you randomly place them in assembly, then it may be possible that it will rename wrongly to some components.

 

Also, I would advise you to check below link that also has some code inside it, to rename occurrences and remove :1, :2 etc from them.

 

https://clintbrown.co.uk/2020/07/29/ilogic-normalise-browser-nodes-with-code/

 

I would also advise you to post here how you want to rename your occurrence browser names and exactly what you are looking for, so one can create mode descriptive and specific code for you.

 

Hope this will be helpful.

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
Message 3 of 17

Anonymous
Not applicable

Hi,

 

Is it possible to all rename as Item 1, Item 2...

Because due to addin when I click on save my component name will changed in model browser.

so component will not same always.

when use 1st variant it will be X-001, for 2nd X-002...

So I am looking a code that can rename component name in model browser as Item 1, Item-2 for a while.

so iLogic rule can run.

0 Likes
Message 4 of 17

WCrihfield
Mentor
Mentor

Tip:  If you set a component's name to an empty string (""), it will attempt to revert back to its original/default component name.  And if that name is already being used, it will add an incremental Integer to the end of its name as usual.

If you simply want to change the component name of all components within your assembly, you can use a code like above, but just add a counting integer that increases +1 within each loop.

Similar to this:

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
For i As Integer = 1 To oADef.Occurrences.Count - 1
	For Each oOcc As ComponentOccurrence In oADef.Occurrences
		oOcc.Name = "Item " & i
		i = i + 1
	Next
Next

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 17

Anonymous
Not applicable

Hi,

 

find below error while run the rule
Error on Line 2 : 'RuleName' is not a member of 'Autodesk.iLogic.Interfaces.ILowLevelSupport'.
Error on Line 5 : 'ThisAssembly' is not accessible in this context because it is 'Friend'.

0 Likes
Message 6 of 17

smart-cad
Enthusiast
Enthusiast

Please show the entire code for replacing components. I want to see why the code cannot be made without permeation.

0 Likes
Message 7 of 17

dutt.thakar
Collaborator
Collaborator

@Anonymous 

 

The code posted by @WCrihfield Works as expected, can you share a screenshot of the error? Also, can you confirm which IDE you are using ? iLogic or Visual Studio or VBA to run the code?

 

 

 

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Message 8 of 17

Anonymous
Not applicable

I have work on iLogic

0 Likes
Message 9 of 17

dutt.thakar
Collaborator
Collaborator

Can you attach the files in which you are trying to run the code? I can test and see if that works fine or not.

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Message 10 of 17

Anonymous
Not applicable

find below error while run the rule


Error on Line 2 : 'RuleName' is not a member of 'Autodesk.iLogic.Interfaces.ILowLevelSupport'.
Error on Line 5 : 'ThisAssembly' is not accessible in this context because it is 'Friend'.

0 Likes
Message 11 of 17

dutt.thakar
Collaborator
Collaborator

@Anonymous 

 

Not the errors, but can you upload your assembly file in which you are running the code?

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Message 12 of 17

WCrihfield
Mentor
Mentor
Accepted solution

Within the first If...Then block in the code I posted, there is a MsgBox with a statement in it.  Within that statement is [ (" & iLogicVb.RuleName & ") ].  Just get rid of the whole section of the message, and that will get rid of the first error you are seeing.  Some folks seem to have trouble with that bit of code, and some folks don't.  It is simply meant to include the name of the current iLogic rule within the message, when shown, and is totally not necessary (just a bonus).

 

As for the second error...try changing this:

Dim oADoc As AssemblyDocument = ThisAssembly.Document

to this:

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument

and see if that works better for you.  It is possible, but not common, for the two to not be pointing to the same document.

Just checking though, since you said something about an add-in in your first post...you are trying to run this within Inventor as a regular iLogic rule, right (not from within Visual Studio or some other platform)?  If you're attempting to run it from another platform, you may need to get the assembly document in a slightly different way, because terms like "ThisDoc", "ThisDrawing", & "ThisAssembly" are defined within the iLogic Add-in, and not available outside that setting unless specifically declared/defined.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 17

Anonymous
Not applicable

Thank

 it is working fine.

I have one more query-

Is possible to rename particular subassembly component name also 

0 Likes
Message 14 of 17

ladderman
Contributor
Contributor

Good morning,

 

This link is almost what I am looking for.  Is there a way to get it to count all of the different parts and add an increment of one to each of the different parts that are in the assembly???

 

The first image is what the code is doing.  The second and third image is what I would like it to do.  I would prefer the third, but the second would work also.

Message 15 of 17

WCrihfield
Mentor
Mentor

Hi @ladderman.  Are you just wanting this code to effect the top level components within your assembly, or do you want it to also go down into all levels of sub assemblies too?  I'm assuming for now that you just want to target top level components, because that is all that was covered here in this post so far.

You can give this iLogic/vb.net code a try if you want.  It first gets the documents being directly referenced by the top level components.  Then it gets the collection of components that are representing each of those documents, because that's how you want to group the naming convention.  Then pairs that group with the document that they represent within a Dictionary collection object, for convenience later.  Then it loops through those pairs in the dictionary, getting the document name as the base part of component's name, then uses the classic incrementing Integer method to add the ".", then the Integer to the ends of each component in the group that are representing that document.  And before it actually makes any changes, it bundles all the changes into a 'Transaction', which makes 'undoing' this potentially large number of changes into one item in the UNDO list for you.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oRefDocs As DocumentsEnumerator = oADoc.ReferencedDocuments
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	'the document will be input as the 'Key' of each pair
	Dim oCompsByDoc As New Dictionary(Of Inventor.Document, ComponentOccurrencesEnumerator)
	
	For Each oRefDoc As Document In oRefDocs
		Dim oCompsForThisDoc As ComponentOccurrencesEnumerator = Nothing
		oCompsForThisDoc = oOccs.AllReferencedOccurrences(oRefDoc)
		If IsNothing(oCompsForThisDoc) OrElse oCompsForThisDoc.Count = 0 Then Continue For
		oCompsByDoc.Add(oRefDoc, oCompsForThisDoc)
	Next
		
	'Start a Transaction to bundle all the name changes into a single item in the 'Undo' menu.
	Dim oTransaction As Transaction = ThisApplication.TransactionManager.StartTransaction(oADoc, "Rename Components")
	
	Dim oOcc As ComponentOccurrence
	For Each oPair In oCompsByDoc
		Dim oInt As Integer = 0
		'get the name of the document that these components represent from this oPair's Key
		Dim oDocName As String = System.IO.Path.GetFileNameWithoutExtension(oPair.Key.FullFileName)
		For Each oOcc In oPair.Value
			oInt = oInt + 1
			Dim oNewName As String = oDocName & "." & oInt
			Try
				oOcc.Name = oNewName
			Catch oEx As Exception
				MsgBox("Renaming component from '" & oOcc.Name & "' to '" & oNewName & "' failed." _
				& vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "Rename Failed")
				Logger.Error("Renaming component from '" & oOcc.Name & "' to '" & oNewName & "' failed.")
			End Try
		Next 'oOcc
	Next 'oPair
	
	'end the Transaction
	oTransaction.End
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 16 of 17

Waguespack_Curtis
Contributor
Contributor

Hi @ladderman 

 

Here's is another approach. One thing to note that is if you have an assembly that has a subassembly and part named the same, such as:

 

Support Post:1 ( subassembly)  

Support Post:2 ( subassembly)  

Support Post:1 ( part)  

Support Post:2 ( part)  

 

Then my example results are:

     Support Post.1

     Support Post.2

     Support Post.3

     Support Post.4

 

WCrihfield's results are:

     Support Post.1

     Support Post.2

     Support Post.1

     Support Post.2

 

This will likely matter to you, but I'm not sure which you would prefer.

 

Hope that helps,

Curtis

 

 

sSplitChar = ":"
sReplaceChar = "."

Dim oDoc As AssemblyDocument
oDoc = ThisAssembly.Document

Dim oList As New ArrayList
oList.Clear
Dim oOcc As Inventor.ComponentOccurrence
For Each oOcc In oDoc.ComponentDefinition.Occurrences
	If Not oOcc.Name.Contains(sSplitChar) Then Logger.Info(sSplitChar & " not found!")
		
	'split string using character
	sSplit = Split(oOcc.Name, sSplitChar)
	'example: Support Post:44
	sName = sSplit(0)	'returns: Support Post
	sNumber = sSplit(1)	'returns: 44
	
	'add name (without colon and number) to list if not present
	If Not oList.Contains(sName) Then
		Logger.Info("Added to list: " & sName)
		oList.Add(sName)
	End If
Next

'now our list has the names of all the unique occurences
For Each oItem In oList
	Logger.Info(oItem)
	Logger.Info("_________________")
	i = 1
	For Each oOcc In oDoc.ComponentDefinition.Occurrences
		'split string using character
		sSplit = Split(oOcc.Name, sSplitChar)
		'compare occurence name to item in list
		If oItem = sSplit(0) Then
			oOcc.Name = sSplit(0) & sReplaceChar & i
			Logger.Info("New name: " & oOcc.Name)
			i = i + 1
		End If
	Next
Next


 

Message 17 of 17

WCrihfield
Mentor
Mentor

Yeah, I noticed just shortly after posting yesterday, after some more testing that my code was affecting lower level components names, when I did not intend for that to happen.  On line 7 of my code I used 'ReferencedDocuments', instead of 'AllReferencedDocuments' to just get the top level referenced documents.  But then down on line 14 of my code, I did not immediately realize that by using the 'AllReferencedOccurrences' method to get all the components that represent that document, that it was digging down into all sub levels too.  That was one mistake.  Then neither of our codes really deals with the possibility of suppressed components, 'virtual' components, or components representing welds within a weldment type assembly...all of which would stump up my code as it was.

 

So, I decided to update my earlier code to correct those issues, and it seems to be functioning better.  However, there may still be a chance for failure to rename certain components in certain odd situations.  For instance, there were a couple of component in my test assembly that both represent the same part document, but the two components are set to different ModelStates (one is set to its Master ModelState, while the other was set to a member ModelState).  When set to the Master ModelState, there are two separate solid bodies in the part, while the other ModelState, here is just one, and it is a larger size.  Due to the difference in the two components, the symbol for one appears as a multi-body part, while the other appears like a normal part.  For some reason, it would not rename either of those two components, while working just fine on all other components.  Just an FYI.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oRefDocs As DocumentsEnumerator = oADoc.ReferencedDocuments
	Dim oRefDoc As Document
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	Dim oOcc As ComponentOccurrence
	'the document will be input as the 'Key' of each pair
	Dim oListOfCompsByRefDoc As New Dictionary(Of Inventor.Document, List(Of ComponentOccurrence))
	For Each oRefDoc In oRefDocs
		Dim oListOfOccs As New List(Of ComponentOccurrence)
		For Each oOcc In oOccs
			If oOcc.Suppressed Then Continue For
			If TypeOf oOcc.Definition Is VirtualComponentDefinition Or _
				TypeOf oOcc.Definition Is WeldsComponentDefinition Then
				Continue For
			End If
			Dim oRefDocA As Document = Nothing
			Try
				oRefDocA = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
			Catch oEx As Exception
				Logger.Error("Failed to get the referenced document from component named '" & oOcc.Name & "'.")
			End Try
			If (oRefDocA IsNot Nothing) AndAlso (oRefDocA Is oRefDoc) Then
				oListOfOccs.Add(oOcc)
			End If
		Next
		If oListOfOccs.Count > 0 Then
			oListOfCompsByRefDoc.Add(oRefDoc, oListOfOccs)
		End If
	Next
	Dim oTransaction As Transaction = ThisApplication.TransactionManager.StartTransaction(oADoc, "Rename Components")
	For Each oPair In oListOfCompsByRefDoc
		Dim oInt As Integer = 1
		'get the name of the document that these components represent from this oPair's Key
		Dim oDocName As String = System.IO.Path.GetFileNameWithoutExtension(oPair.Key.FullFileName)
		For Each oOcc In oPair.Value
			Dim oNewName As String = oDocName & "." & oInt
			Try
				oOcc.Name = oNewName
				oInt = oInt + 1
			Catch oEx As Exception
				Logger.Error("Renaming component from '" & oOcc.Name & "' to '" & oNewName & "' failed.")
			End Try
		Next 'oOcc
	Next 'oPair
	oTransaction.End
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)