Color change of list of faces in Assembly file .iam

Color change of list of faces in Assembly file .iam

anil_dangi
Explorer Explorer
103 Views
0 Replies
Message 1 of 1

Color change of list of faces in Assembly file .iam

anil_dangi
Explorer
Explorer

Hello everyone,

I am trying to assign a permanent color to selected faces (not the entire body) in an Assembly file using the FinishFeature API.

So far, I tried SetRenderStyle, but it either applied to the entire part or threw an error (E_NOTIMPL). However, I found that FinishDefinition API can help apply color only to selected faces.

There is already a tool under Inventor UI :

 Assemble Tab → Appearance Panel → "Finish" Tool
When clicked, it applies a color/material kind of thing to the selected faces in the assembly.

1. How can I correctly implement this in C# using FinishFeature API or above feature?

2.  Is there a better way to handle face-level appearance inside an Assembly document?

Below is my working code for this:

 

 

public void AssignColorToSelectedFaces(List<Face> selectedFaces)
{
    try
    {
        // Get the active document (should be an Assembly)
        AssemblyDocument asmDoc = StandardAddInServer.InventorApp.ActiveDocument as AssemblyDocument;
        if (asmDoc == null)
        {
            MessageBox.Show("Please open an assembly document in Inventor.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        // Iterate through selected faces
        foreach (Face face in selectedFaces)
        {
            // Get the ComponentOccurrence that owns the face
            ComponentOccurrence occurrence = face.Parent as ComponentOccurrence;
            if (occurrence == null) continue;

            // Get the PartDocument inside the occurrence
            PartDocument partDoc = occurrence.Definition.Document as PartDocument;
            if (partDoc == null) continue;

            PartComponentDefinition partDef = partDoc.ComponentDefinition;

            // Ensure there is a FinishFeature object
            if (partDef.Features.FinishFeatures.Count == 0)
            {
                partDef.Features.FinishFeatures.Add();
            }

            // Get the FinishFeature object
            FinishFeature finishFeature = partDef.Features.FinishFeatures[1];

            // Assign a color (Get an existing AppearanceAsset or create one)
            string appearanceName = "CustomFaceColor";
            AppearanceAsset appearanceAsset;
            try
            {
                appearanceAsset = partDoc.AppearanceAssets[appearanceName];
            }
            catch
            {
                // Create new Appearance Asset if not found
                appearanceAsset = partDoc.AppearanceAssets.Add(appearanceName);
                appearanceAsset.DiffuseColor = Inventor.Color.Create(255, 0, 0); // Red
            }

            // Apply appearance to the face
            finishFeature.AddFace(face, appearanceAsset);
        }

        // Update the document
        asmDoc.Update();
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Error assigning color to faces: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

 

 

0 Likes
104 Views
0 Replies
Replies (0)