Changing Appearances with iLogic

Changing Appearances with iLogic

n_costelloe
Explorer Explorer
127 Views
2 Replies
Message 1 of 3

Changing Appearances with iLogic

n_costelloe
Explorer
Explorer

As a sort of check tool, I've written a code that looks at the different finishes on parts and assemblies (eg anodising, brushing) and gives them a colour. The code works for top-level parts and assemblies but I can't get it to work for lower levels. The code can see and read all the finishes (see the messagebox in the code), its the changing of the colours that it seems to be hanging on. 

 

Sub Main()
    iLogicVb.RunExternalRule("AddRGB")

    Dim oAnoClear = "Anodise E0/A20/C0 (clear)" 'ear)
    Dim oAnoBlack = "Anodise E0/A20/C35 (black)" 'ack)
    Dim oK320 = "Brushed K320" 'K320
    Dim oNickel = "Chemically nickel plated" 'ated
    Dim oHardAno = "Hard Anodized" 'ized
    Dim oPearl = "Pearl blasted" 'sted
    Dim oPolished = "Polished" 'shed
    Dim oSeeDwg = "(See Drawing)" 'ing)

    Dim oAsmDoc = ThisApplication.ActiveDocument
    If oAsmDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Return

    Dim oAsmDocu As AssemblyDocument = DirectCast(oAsmDoc, AssemblyDocument)
    Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

    ' Start recursive traversal
    For Each oOcc In oAsmCompDef.Occurrences
        ProcessOccurrence(oOcc, oAsmDocu)
    Next
End Sub

Sub ProcessOccurrence(oOcc As ComponentOccurrence, oAsmDocu As AssemblyDocument)
    Dim refDoc As Document = Nothing
    Try
        refDoc = oOcc.Definition.Document
    Catch
        Return ' Skip if document is not accessible
    End Try

    Dim surfTreatment As String = ""
    Try
        surfTreatment = refDoc.PropertySets.Item("Inventor User Defined Properties").Item("Surf. Treatment").Value
    Catch
        surfTreatment = ""
    End Try

    Dim oApp As String = ""
    Dim oFinish As Boolean = False

    If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Or _
       oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

        Dim oWhichTreatment = Right(surfTreatment, 4)
        oFinish = False

        Select Case oWhichTreatment
            Case "ear)"
                oApp = "Anodise E0/A20/C0 (clear)"
                oFinish = True
            Case "ack)"
                oApp = "Anodise E0/A20/C35 (black)"
                oFinish = True
            Case "K320"
                oApp = "Brushed K320"
                oFinish = True
            Case "ated"
                oApp = "Chemically nickel plated"
                oFinish = True
            Case "ized"
                oApp = "Hard Anodized"
                oFinish = True
            Case "sted"
                oApp = "Pearl blasted"
                oFinish = True
            Case "shed"
                oApp = "Polished"
                oFinish = True
            Case "ing)"
                oApp = "(See Drawing)"
                oFinish = True
        End Select
    End If

    If oFinish = True Then
        Dim oAppearance As Asset = Nothing

        Try
            oAppearance = oAsmDocu.Assets.Item(oApp)
            oOcc.Appearance = oAppearance
        Catch
      		MessageBox.Show(oAppearance.Name, "Check")
        End Try
    End If

    ' If it's a subassembly, recurse into its children
    If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
        Dim subAsmDef As AssemblyComponentDefinition = DirectCast(oOcc.Definition, AssemblyComponentDefinition)
        For Each subOcc In subAsmDef.Occurrences
            ProcessOccurrence(subOcc, oAsmDocu)
        Next
    End If
End Sub

Anyone any ideas? Is this even possible in Inventor? 

 

 

0 Likes
Accepted solutions (2)
128 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @n_costelloe.  Since you said this is working for top level components, but not for lower level components, that gives me a clue about how to fix it.  There are multiple ways to recurse down through an assembly, but not all ways are equal, when it comes to functionality.  In the last part of your code, where you are checking if a component represents an assembly, so that you can step down into its sub components.  Instead of getting the 'definition' of that assembly, then using the definition.Occurrences property, use the ComponentOccurrence.SubOccurrences property instead.  When you use that property, the process will retain its 'lineage or ancestry' to the main/active assembly, instead of attempting to make changes within all those other model files directly.  We can make changes within assemblies that will only effect that assembly, without effecting things within other referenced documents, and this is one way to do so.  However, if your design intention is to make those changes directly in all of those other referenced model files, then different types of changes would likely be needed, instead of this change.

Appearances, as well as colors and object visibility, are all recorded by the DVR's (DesignViewRepresentations).  These are available in both assemblies and in parts, but there are usually none by default, unless you are starting from a template in which some have already been created.  In a part, there is a folder in the model browser tree with its name starting with "View:", and ending with the name of the current/active view representation, within that folder.  In assemblies, there is a folder named 'Representations' which we have to expand, then we can see the similar folder for view representations.  The original/default one is usually named "Master" or "[Primary]", and is always 'Locked', meaning it will not record those types of changes.  It is up to us to create one or more 'custom' ones, activate them, and have them active while we make appearance/color/visibility related changes, so that those changes will be recorded by them.  When we have that DVR set-up the way we want it in the part or sub assembly level, we can then just set the component occurrence of that source model to that DVR, and it will look like that in a parent level assembly.  If you do not have any custom DVR's in any of the model's though, it can be difficult to manage those types of things from a higher level assembly.  If we have a component occurrence in our assembly that is set to a specific/custom DVR, and have that set as 'Associative', then we change its appearance by code, that will break its 'Associative' link to the source model, and will no longer be kept accurate to the source model.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

n_costelloe
Explorer
Explorer
Accepted solution

Mate, you are an absolute legend! Pretty sure it has worked... but of testing and i can tell you for sure. Thanks for the help!

0 Likes