Change appearance of Parts in Assembly based on custom iProp field

Change appearance of Parts in Assembly based on custom iProp field

6.Day.Old.Filet-O-Fish
Enthusiast Enthusiast
280 Views
3 Replies
Message 1 of 4

Change appearance of Parts in Assembly based on custom iProp field

6.Day.Old.Filet-O-Fish
Enthusiast
Enthusiast

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

 

0 Likes
281 Views
3 Replies
Replies (3)
Message 2 of 4

Curtis_Waguespack
Consultant
Consultant

Hi @6.Day.Old.Filet-O-Fish ,

 

I think you just need to use the RenderStyle and StyleSourceEnum to tell it to be an override. See this example that sets all the parts and subassemblies to be Red at the assembly level. 

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

Sub Main
	Dim asmDoc As AssemblyDocument
	asmDoc = ThisApplication.ActiveDocument

	Call SetColor(asmDoc.ComponentDefinition.Occurrences, "Red")
End Sub

Private Sub SetColor(Occurrences As ComponentOccurrences, Color As String)

	Dim oAsmDoc As AssemblyDocument
	oAsmDoc = ThisApplication.ActiveDocument
	Dim occ As ComponentOccurrence
	For Each occ In Occurrences

		Call occ.SetRenderStyle(StyleSourceTypeEnum.kOverrideRenderStyle, _
			oAsmDoc.RenderStyles.Item(Color))

		If occ.DefinitionDocumentType = kAssemblyDocumentObject Then
			Call SetColor(occ.SubOccurrences, Color)
		End If
	Next
End Sub

 

EESignature

Message 3 of 4

6.Day.Old.Filet-O-Fish
Enthusiast
Enthusiast

Won't have a chance to try this till later but it's so simple and looks exactly like what I'm trying to accomplish! TY fren!

Message 4 of 4

Curtis_Waguespack
Consultant
Consultant

Note too that since you are only concerned with parts (leaf occurrences) you could just use something like this

 

Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument

Dim occ As ComponentOccurrence
For Each occ In asmDoc.ComponentDefinition.Occurrences.AllLeafOccurrences

	Call occ.SetRenderStyle(StyleSourceTypeEnum.kOverrideRenderStyle, _
	asmDoc.RenderStyles.Item("Red"))
Next

 

EESignature