Change appearance of Parts in Assembly based on custom iProp field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
We are on a project where we have many structures. Each of these share a handful of parts and subassemblies. I wanted to create a tool that allowed us to 'see' these shared parts. I made a script that will make a new view rep and change the appearance of parts based on the value assigned in the custom iProperty. So, I sort the parts in the BOM based on their filepath, and provide the appropriate value to the iProp field. Then I run my script.
Here's the problem: When parts are nested in the root of the major assembly, the tool works as expected. But most of the shared parts are nested in sub-assemblies within my major assembly, and their appearance does not change. My first thought is to have it create a view rep at each assembly level that has a shared part and have them all activate together. But that sounds way more complicated. I just want to assign an appearance to the parts at the highest assembly level. Is there an easier way? Here is my code (it calls a simple class from an external script to help me create objects): ((Also kinda amazing that this forum's 'Insert Code' button doesn't recognize visual basic?!?))
AddVbFile "Q:\iLogicRules_Whale\UniversalObjectClass.iLogicVB"
Imports UnivObj
Sub Main()
Dim iPropName As String = "PartCode"
Dim iPropValue As String = "Default"
Dim DVRName As String = "SharedView+"
Dim bluValue As String =""
Dim greValue As String =""
Dim yelValue As String =""
'----------Prompt User for entries
DVRName = InputBox("What would you like your view called?", "VisRepTool", "SharedView")
iPropName = InputBox("What Custom iProperty are you using?", "VisRepTool", "PartCode")
bluValue = InputBox("What code for clear-blue?", "VisRepTool", "xs")
greValue = InputBox("What code for clear-green?", "VisRepTool", "xa")
yelValue = InputBox("What code for clear-yellow?", "VisRepTool", "xp")
'----------Get the active assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
'----------Get the assembly component definition.
Dim oAsmDef As AssemblyComponentDefinition
oAsmDef = oAsmDoc.ComponentDefinition
oAsmDef.RepresentationsManager.DesignViewRepresentations.Item("Master").activate
'----------Get all of the leaf occurrences of the assembly.
Dim oLeafOccs As ComponentOccurrencesEnumerator
oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences
Dim oOcc As ComponentOccurrence
'----------Initiate base object using our class
Dim testpiece As UnivObj
'----------Create collection to stuff objects into
Dim oTP As New List(Of UnivObj)
'----------Create object instances and put in collection
For Each oOcc In oLeafOccs
testpiece = New UnivObj
testpiece.MyName = oOcc.Name
oTP.Add(testpiece)
Next
'----------Create a new level of detail and activate
Dim oviewrep As DesignViewRepresentation
Try
oAsmDef.SetDesignViewRepresentation(DVRName, True)
Catch
oviewrep = oAsmDef.RepresentationsManager.DesignViewRepresentations.Add(DVRName,True)
oviewrep.Activate
End Try
'----------Loop through collection and find any iPropName
'----------and apply new appearance based on iPropName
Dim missedParts As Integer = 0
For index = 0 To oTP.Count-1
Try
Dim iCode As String
iCode = iProperties.Value(oTP(index).MyName, "Inventor User Defined Properties", iPropName)
If iCode = bluValue Then
Component.Color(oTP(index).MyName) = "Clear - Blue"
Else If iCode = yelValue Then
Component.Color(oTP(index).MyName) = "Clear - Yellow"
Else If iCode = greValue Then
Component.Color(oTP(index).MyName) = "Clear - Green 1"
Else If iCode = "up" Or "Default" Or "" Then
'Do Nothing
End If
Catch
missedParts = missedParts +1
End Try
Next
MessageBox.Show(missedParts & " did not have " & iPropName)
'----------Now activate Master level of detail
oAsmDef.RepresentationsManager.DesignViewRepresentations("Master").Activate
End Sub