Remove All Appearances with iLogic

Remove All Appearances with iLogic

spencerthomas32J29
Contributor Contributor
2,060 Views
7 Replies
Message 1 of 8

Remove All Appearances with iLogic

spencerthomas32J29
Contributor
Contributor

Hello,

I frequently have to import .stp models that import as a large assembly with many sub-assemblies and each sub-assembly has many parts. I need to recolor these models to have acceptable appearances, but based on the software that the supplier of the .stp file uses, the parts and faces already have incorrect appearances applied.

 

I want to use iLogic to loop through all sub-assemblies and strip all appearances from all parts so that the parts have no appearances applied and are just set to the default appearance.

 

Can someone help me with this iLogic. I have tried using ChatGPT to generate the code, but it has yet to work. As a jumping off point, here is what ChatGPT has helped me generate so far:

Sub Main()
    ' Get the active document
    Dim oDoc As Document
    oDoc = ThisApplication.ActiveDocument

    ' Check if the active document is an assembly
    If oDoc.DocumentType = kAssemblyDocumentObject Then
        Dim oAsmDoc As AssemblyDocument
        oAsmDoc = oDoc

        ' Loop through all occurrences in the assembly
        For Each oOccurrence As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
            RemoveAppearanceFromOccurrence(oOccurrence)
        Next

        MessageBox.Show("All appearances in the assembly have been reset to material appearance.")
    ElseIf oDoc.DocumentType = kPartDocumentObject Then
        ' If the active document is a part, set its appearance to material appearance
        Dim oPartDoc As PartDocument
        oPartDoc = oDoc
        oPartDoc.AppearanceSourceType = AppearanceSourceTypeEnum.kMaterialAppearance
        MessageBox.Show("The appearance of the part has been reset to material appearance.")
    Else
        MessageBox.Show("The active document is neither an assembly nor a part document.")
    End If
End Sub

' Function to remove appearances from a component occurrence
Sub RemoveAppearanceFromOccurrence(oOccurrence As ComponentOccurrence)
    Try
        ' Set the appearance source type to material appearance
        oOccurrence.AppearanceSourceType = AppearanceSourceTypeEnum.kMaterialAppearance

        ' If this occurrence is an assembly, apply to all sub-occurrences
        If oOccurrence.DefinitionDocumentType = kAssemblyDocumentObject Then
            Dim oSubAsmDoc As AssemblyDocument
            oSubAsmDoc = oOccurrence.Definition.Document

            For Each oSubOccurrence As ComponentOccurrence In oSubAsmDoc.ComponentDefinition.Occurrences
                RemoveAppearanceFromOccurrence(oSubOccurrence)
            Next
        End If
    Catch ex As Exception
        ' Handle any errors that might occur
        MessageBox.Show("Error occurred while processing occurrence: " & oOccurrence.Name & vbCrLf & ex.Message)
    End Try
End Sub

 

Any help you can provide with this is greatly appreciated. I have searched the forums and Google and tried all code snippets that I have found, but none work as I would like.

I just want to remove all appearance overrides for every component in the assembly.

 

Thank you!

0 Likes
2,061 Views
7 Replies
Replies (7)
Message 2 of 8

Curtis_W
Consultant
Consultant
Message 3 of 8

spencerthomas32J29
Contributor
Contributor
Thank you! I'll check them out now.
0 Likes
Message 4 of 8

spencerthomas32J29
Contributor
Contributor

Thank you Curtis; thank you so much! I have been trying to overcome this for quite a while and you have just provided the solution. You're a wizard.

 

The code in message 1 of 5 worked perfectly for me! I tried the revised code (message 5 of 5)) first, but that did not appear to have any result.

Message 5 of 8

Curtis_W
Consultant
Consultant

@spencerthomas32J29 would you be able to share a .stp file that did not work well with the example in message 5 of 5? 

 

I'd like to see where that example is going wrong.

 

I can send you an link to upload if it's too large or not something you want to post publicly.

EESignature

0 Likes
Message 6 of 8

spencerthomas32J29
Contributor
Contributor

Hi Curtis, I'm currently running your first script on an assembly with like 3500+ parts... so when that task completes, I will work on putting together a smaller assembly that I can share that replicates the lack of results with the second script.

 

Actually, I will try to see if I can work in a secondary instance of Inventor in the meantime... although that first instance is using most of my resources at the moment, so my effort may not be fruitful.

I'll keep you posted and share the assembly as soon as I am able to.

 

Thanks again!

Message 7 of 8

spencerthomas32J29
Contributor
Contributor

Hello Curtis, I was incorrect; both scripts work!

I am not certain why it did not appear to function the first time I ran the shorter script - perhaps the model just didn't update... I'm not sure. However, in attempting to replicate the issue it worked perfectly as it should.

 

Once again, thank you for getting me the answer so quickly this morning. Sorry for the confusion.

Message 8 of 8

Curtis_W
Consultant
Consultant

@spencerthomas32J29 wrote:

Hello Curtis, I was incorrect; both scripts work!

I am not certain why it did not appear to function the first time I ran the shorter script - perhaps the model just didn't update... I'm not sure. However, in attempting to replicate the issue it worked perfectly as it should.

 

Once again, thank you for getting me the answer so quickly this morning. Sorry for the confusion.


No worries... I actually revised the code slightly, this morning after I posted to this thread, so it might not have worked then but did later due to that.


In any case, that great to hear that it worked for you on that large assembly!

EESignature

0 Likes