Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Need iLogic code for rename component & subcomponent name in model browser

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Anonymous
831 Views, 7 Replies

Need iLogic code for rename component & subcomponent name in model browser

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 & subcomponent name in model browser to file name, due that iLogic rule unable to replace the component & subcomponent.

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

 

7 REPLIES 7
Message 2 of 8
WCrihfield
in reply to: Anonymous

Here is an iLogic rule that will rename every component at every level of an assembly to a pre-defined base name, followed by one or more Integers separated by periods (dots).  Each layer deeper gets another 'dot' and an Integer added to the end of the component name (showing hierarchy and order).  The one little annoyance I've noticed when testing this is that within all levels other than the top level, the component names seem to be starting with the Integer 2, instead of 1, for some reason, even though my code is specifying starting from 0 and adding 1 each time it attempts to rename a component.

Here's the code:

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 oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oItem As String = "Item "
	Dim oInt As Integer = 0
	Dim oCO As ComponentOccurrence
	'rename all comps in top level first
	For Each oCO In oADef.Occurrences
		oInt = oInt + 1
		Try
			oCO.Name = oItem & oInt
		Catch
			MsgBox("Failed to rename:  " & oCO.Name,,"")
		End Try
	Next
	'now try to rename comps at deeper levels
	For Each oCO In oADef.Occurrences
		If oCO.SubOccurrences.Count > 0 Then 'it is a sub-assembly
			'run 'recursive' sub here
			'and supply the SubOccurrences to it
			Iterate(oCO.SubOccurrences)
		End If
	Next
End Sub

Sub Iterate(oOccs As ComponentOccurrencesEnumerator)
	'create new variable to enable 'Intellisense' recognition
	Dim oComps As ComponentOccurrencesEnumerator = oOccs
	Dim oComp As ComponentOccurrence
	Dim oNum As Integer = 0
	'try to rename all comps at this level first
	For Each oComp In oComps
		oNum = oNum + 1
		Try
			oComp.Name = oComp.ParentOccurrence.Name & "." & oNum
		Catch
			MsgBox("Failed to rename:  " & oComp.Name,,"")
		End Try
	Next
	'now loop through again checking for SubOccurrences, then Iterate
	For Each oComp In oComps
		If oComp.SubOccurrences.Count > 0 Then 'it is a sub-assembly
			Iterate(oComp.SubOccurrences)
		End If
	Next
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 have time, please... Vote For My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8
WCrihfield
in reply to: WCrihfield

@Anonymous

I also posted a very similar code to this on another post shortly after posting this one.  That other code also incorporates using a 'Transaction' to bundle all the component rename actions into one item in the 'Undo' menu.  It is renaming all components to their Part Numbers, instead of a generic base name.

 

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 4 of 8
JelteDeJong
in reply to: Anonymous

This rule will also rename all occurrences in an assembly (at least in IV2021 and maybe older versions).

For Each item In Components
	item.Occurrence.Name = ""
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 5 of 8
WCrihfield
in reply to: JelteDeJong

Hi @JelteDeJong .  Nice idea, and very efficient.  However, that code will only convert the top level component's names back to their 'default' component names (if you leave the new name as an empty String).  And if you specify a 'base name' instead of an empty String, in my tests, it will only rename the first component in the top level, then throw an error.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8
Darkforce_the_ilogic_guy
in reply to: Anonymous

try this code

 

 

Sub Main()
	
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document

Dim wrongDisplayname As String
 
For Each oRefDoc In oRefDocs
	Try
		'debug(oRefDoc.FullDocumentName)
		If oRefDoc.DisplayNameOverridden = True 
			oRefDoc.DisplayName =""
			
		End If
		
	Catch
	End Try

Next

End Sub


Public Sub debug(txt As String)
	Trace.WriteLine("Debug Log  : Colour " & txt)
End Sub
Message 7 of 8
Anonymous
in reply to: WCrihfield

Hi,

 

Thanks. This is exactly what I need.

 

More more query.

 

Is it possible to lock component sequence in browser with the help of iLogic code.

No one can move or drag it manually.

Message 8 of 8
WCrihfield
in reply to: Anonymous

I'm not currently aware of anything you can do using an iLogic rule, that would permanently disable your ability to rearrange components within the model browser of an assembly.  And there is no setting within the 'Application Options' or the 'Document Settings' for controlling that behavior either.  There are certain scenario's in which Inventor won't allow you to move certain items in the model browser, as you normally would though.  They usually have to do with dependencies.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report