iLogic: Clear Part Appearance Overrides

iLogic clear face colour overrides

Have you ever wanted to clear all of the face appearance overrides in a part model? Below is some iLogic code that resets all of the faces an Inventor part model.

Here is an animation showing the code at work:

iLogic clear face colour overrides

 

Below is the iLogic code:

'Original Code by @ClintBrown3D posted at https://clintbrown.co.uk/ilogic-reset-faces
On Error GoTo Clint
i = MessageBox.Show("Set all Faces in the model back to default?", "@ClintBrown3D", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If i = vbYes Then
	
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument
Dim oCompDef As ComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

Dim oFaces As Faces
Dim oFace As Face
Dim oSurfBodies As SurfaceBodies
Dim oSurfBody As SurfaceBody
oSurfBodies = oCompDef.SurfaceBodies
For Each oSurfBody In oSurfBodies
oFaces = oSurfBody.Faces
For Each oFace In oFaces
oFace.SetRenderStyle(kPartRenderStyle)
Next
Next
End If
Return

Clint :
MessageBox.Show("ERROR: You must be in a part file to run this code", "@ClintBrown3D - Error Message", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

 

This iLogic code is a conversion of an old .ivb (VBA) macro that can be found here. I have tweaked it to work with iLogic, and added in an error check, so that if it is run in an Assembly or a Drawing, a neat iLogic error message will pop up (one on the right) rather than the “Exception from HRESULT” (error on the left).

It works quite well, but be careful, if you have a lot of faces that have different colour overrides and you wish to go back, the Inventor undo feature will need to undo each face change (this will only go back to the maximum number of undo’s you have allowed in your settings). This may result in you not being able to go all the way back to your start point, although the intention was to clear all of the overridden faces!

UPDATE 25/02/2019 – With Undo wrapper (as per Alex’s comments below)

The iLogic code below includes an “undo wrapper”, see Brian Ekins blog post here, explaining the concept of turning your code into a single transaction that can then be undone with a single undo.

'Original Code by @ClintBrown3D posted at https://clintbrown.co.uk/ilogic-reset-faces
oDoc = ThisDoc.Document
Dim UNDO As Transaction 
UNDO = ThisApplication.TransactionManager.StartTransaction(oDoc, "Clear Appearance overrides")
'Put your code in here (This is the Transaction): ------------------------------------------------------------------------------------------------------------

'Get user input
i = MessageBox.Show("Set all Faces in the model back to default?", "@ClintBrown3D", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If i = vbYes Then
	
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

Dim oCompDef As ComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

Dim oFaces As Faces
Dim oFace As Face

Dim oSurfBodies As SurfaceBodies
Dim oSurfBody As SurfaceBody
oSurfBodies = oCompDef.SurfaceBodies
For Each oSurfBody In oSurfBodies
oFaces = oSurfBody.Faces
For Each oFace In oFaces
oFace.SetRenderStyle(kPartRenderStyle)
Next
Next
End If
'-------------------------------------------------------------------------------------------------------------------------------------
'End Transaction
UNDO.End

4 thoughts on “iLogic: Clear Part Appearance Overrides

  1. Here is code if you want to run it from an assembly and clear all the parts in it:


    Dim Title As String = "Excitech iLogic"

    ' Attempt to get the active assembly document
    Dim oDoc As AssemblyDocument = Nothing
    Try
    oDoc = ThisApplication.ActiveEditDocument
    Catch
    MessageBox.Show("This rule must be run from an assembly", Title)
    Exit Sub
    End Try

    Dim oADoc As AssemblyDocument = Nothing
    Dim oPDoc As PartDocument = Nothing
    Dim TotCount As Integer = oDoc.AllReferencedDocuments.Count

    Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
    Dim oRefDoc As Document = Nothing
    Dim oPartDef As PartComponentDefinition = Nothing
    Dim oAsmDef As AssemblyComponentDefinition = Nothing
    Dim FailCount As Integer = 0

    Dim Count As Integer = 1

    ' Top level clear override command
    oDef.ClearAppearanceOverrides()

    ' Loop through all the documents referenced by this assembly document...
    For Each oRefDoc In oDoc.AllReferencedDocuments
    Try
    ThisApplication.StatusBarText = Count & " of " & TotCount & " components processed."

    ' Is it a part document?
    If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
    ' Get the component definition.
    oPartDef = oRefDoc.ComponentDefinition

    ' First set the top level part appearance to be the same as the material
    oRefDoc.AppearanceSourceType = AppearanceSourceTypeEnum.kMaterialAppearance

    ' Try a top level 'clear appearance overrides' command first
    oPartDef.ClearAppearanceOverrides()

    ' Clear the override on all the override objects found....
    oPartDef.ClearAppearanceOverrides(ObjColl)

    ThisApplication.ActiveView.Update()

    ' Is it an assembly document?
    ElseIf oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
    ' Get the assembly definition
    oAsmDef = oRefDoc.ComponentDefinition
    ' Run top level 'clear appearances' command on this assembly
    ThisApplication.StatusBarText = Count & " of " & TotCount & " components processed. Clearing assembly overrides..."
    oAsmDef.ClearAppearanceOverrides()
    End If
    Count += 1
    Catch
    FailCount += 1
    End Try
    Next

    ThisApplication.ActiveView.Update()

    If FailCount > 0 Then
    MsgBox("All Appearance overrides removed." & vbLf & vbLf & _
    "Operation failed on " & FailCount & " component(s) - these may be read-only.", , Title)
    Else
    MsgBox("All Appearance overrides removed.", , Title)
    End If

Comments are closed.

Create a website or blog at WordPress.com

Up ↑