Why my code won't work

Why my code won't work

dinhthit25
Contributor Contributor
834 Views
11 Replies
Message 1 of 12

Why my code won't work

dinhthit25
Contributor
Contributor

I want to paint 2 color alternate on every face of a solid3d object, im using Solid3d.SetSubentityMaterial method the code have no error but it won't work can anyone have me. Im using C#.net framework 4.8 at visual studio 2022. Here the code

 

public class Solid3dMaterialExample
{
    [CommandMethod("SetTwoColorsToSolid3dFaces")]
    public void SetTwoColorsToSolid3dFaces()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;

        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            // Select Solid3d object
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a Solid3d:");
            peo.SetRejectMessage("\nObject must be a Solid3d.");
            peo.AddAllowedClass(typeof(Solid3d), true);
            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nNo valid Solid3d selected.");
                return;
            }

            Solid3d solid3d = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Solid3d;
            if (solid3d == null)
            {
                ed.WriteMessage("\nFailed to access Solid3d.");
                return;
            }

            // Create two materials with different colors
            ObjectId materialId1 = CreateMaterial(tr, db, "Material1", Color.FromColorIndex(ColorMethod.ByAci, 1)); // Red
            ObjectId materialId2 = CreateMaterial(tr, db, "Material2", Color.FromColorIndex(ColorMethod.ByAci, 3)); // Blue

            // Check and apply alternating materials to the faces
            try
            {
                using (Brep brep = new Brep(solid3d))
                {
                    int faceIndex = 0;
                    foreach (Autodesk.AutoCAD.BoundaryRepresentation.Face face in brep.Faces)
                    {
                        try
                        {
                            SubentityId faceId = face.SubentityPath.SubentId;

                            if (faceId.Type == SubentityType.Face) // Check the type of Subentity
                            {
                                // Apply alternating materials
                                ObjectId materialToApply = (faceIndex % 2 == 0) ? materialId1 : materialId2;
                                solid3d.SetSubentityMaterial(faceId, materialToApply);
                                ed.WriteMessage($"\nMaterial applied to face {faceIndex}.");
                                faceIndex++;
                            }
                        }
                        catch (System.Exception ex)
                        {
                            ed.WriteMessage($"\nError applying material to face: {ex.Message}");
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage($"\nError processing Brep: {ex.Message}");
            }

            tr.Commit();
        }
    }

    private ObjectId CreateMaterial(Transaction tr, Database db, string materialName, Color color)
    {
        // Create a new material
        Material material = new Material
        {
            Name = materialName
        };  
        // Add the material to the MaterialDictionary
        DBDictionary materialDict = (DBDictionary)tr.GetObject(db.MaterialDictionaryId, OpenMode.ForWrite);
        ObjectId materialId = materialDict.SetAt(materialName, material);
        tr.AddNewlyCreatedDBObject(material, true);

        return materialId;
    }
}

 

 

0 Likes
Accepted solutions (2)
835 Views
11 Replies
Replies (11)
Message 2 of 12

kerry_w_brown
Advisor
Advisor

 

Is the code hitting this line ??

ed.WriteMessage($"\nMaterial applied to face {faceIndex}.");

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 3 of 12

dinhthit25
Contributor
Contributor

i dubugging it and this line appear:
Autodesk.AutoCAD.BoundaryRepresentation.Exception: 'Exception of type 'Autodesk.AutoCAD.BoundaryRepresentation.Exception' was thrown.'
i hope i can print 2 color alternate on every face of a solid3d object but won't

0 Likes
Message 4 of 12

kerry_w_brown
Advisor
Advisor

On what line was the error reported ?

What was the actual error ?

How complex was the solid you are testing with ?

Any other information you can think of that may help resolve the issue ?


 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 5 of 12

kerry_w_brown
Advisor
Advisor

This looks suspect. Are you sure this block of code is correct ?

 

        // Create a new material
        Material material = new Material
        {
            Name = materialName
        };  

 

 

How much help did you get from an AI source ?

 

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 6 of 12

dinhthit25
Contributor
Contributor

sorry for my deficiency,
I use ai on private ObjectId CreateMaterial class

i do not check that code you sent thoroughly 
when i debug, this happen

dinhthit25_0-1733714564456.png

this happen when i chose a solid3d object
and this is the testing solid(it quite complex)

dinhthit25_1-1733714721307.png

and finally im changing it from using Solid3d.SetSubentityColor method to using  Solid3d.SetSubentityMaterial method

 

0 Likes
Message 7 of 12

kerry_w_brown
Advisor
Advisor
Accepted solution

I'd close that  drawing . . . it looks like about 30 solid components.

Do your initial development on a simple solid ( or perhaps one of the bracing plate components )

You are currently catching System Exceptions.  Perhaps change that to 

Autodesk.AutoCAD.BoundaryRepresentation.Exception

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 8 of 12

_gile
Consultant
Consultant

Hi,

 

As I replied you in another thread, you need to use the Brep constructor which takes a FullSubentityPath as argument to be able to access to the Solid3d Subentities (Faces here).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 12

dinhthit25
Contributor
Contributor

im doing it, but i thinks my FullSubentityPath wrong and i am trying to fix it, thanks again for your advice

0 Likes
Message 10 of 12

_gile
Consultant
Consultant
Accepted solution

@dinhthit25  a écrit :

im doing it, but i thinks my FullSubentityPath wrong and i am trying to fix it, thanks again for your advice


You're not doing it in the code you posted in the thread:

using (Brep brep = new Brep(solid3d))

I showed you how to do it here.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 11 of 12

dinhthit25
Contributor
Contributor

thanks you i will watch it agains

0 Likes
Message 12 of 12

dinhthit25
Contributor
Contributor

Thanks you, now i can put both color in 1 solid3d object

0 Likes