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.
Solved! Go to Solution.
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.
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
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
(Not an Autodesk Employee)
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
(Not an Autodesk Employee)
@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
(Not an Autodesk Employee)
@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
(Not an Autodesk Employee)
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.
Blog: hjalte.nl - github.com
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.
Blog: hjalte.nl - github.com
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
(Not an Autodesk Employee)
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
(Not an Autodesk Employee)
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
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
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.
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.
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
(Not an Autodesk Employee)
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
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.