.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why my code won't work

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
dinhthit25
355 Views, 11 Replies

Why my code won't work

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;
    }
}

 

 

Tags (1)
Labels (3)
11 REPLIES 11
Message 2 of 12
kerry_w_brown
in reply to: dinhthit25

 

Is the code hitting this line ??

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

 

 


// Called Kerry in my other life.

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

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 3 of 12
dinhthit25
in reply to: kerry_w_brown

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

Message 4 of 12
kerry_w_brown
in reply to: dinhthit25

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 in my other life.

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

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 5 of 12
kerry_w_brown
in reply to: dinhthit25

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 in my other life.

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

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 6 of 12
dinhthit25
in reply to: kerry_w_brown

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

 

Message 7 of 12
kerry_w_brown
in reply to: dinhthit25

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 in my other life.

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

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 8 of 12
_gile
in reply to: dinhthit25

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

Message 9 of 12
dinhthit25
in reply to: _gile

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

Message 10 of 12
_gile
in reply to: dinhthit25


@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
in reply to: _gile

thanks you i will watch it agains

Message 12 of 12
dinhthit25
in reply to: _gile

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report