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;
}
}
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Solved by kerry_w_brown. Go to Solution.
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
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
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
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
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
this happen when i chose a solid3d object
and this is the testing solid(it quite complex)
and finally im changing it from using Solid3d.SetSubentityColor method to using Solid3d.SetSubentityMaterial method
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
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).
im doing it, but i thinks my FullSubentityPath wrong and i am trying to fix it, thanks again for your advice
@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.
Can't find what you're looking for? Ask the community or share your knowledge.