I have this code run in C#.net Framework 4.8 in visual studio 2022 does anyone know how to change from paint 2 color alternate on solid3d object to paint 2 color alternate on every face in solid3d object.
More specific: when chosing 2 color from autocad paint window(1-255), color1 will apply to even faces, color2 will apply to odd faces, now i want to change both two color will apply alternate on every face on solid3d object.
One more thing im first try using Solid3d.SetSubentityMaterial method(Solid3d.SetSubentityColor method applies a single color) but failed, anyone know better way
here my code:
private void ApplyColorsTo3DObject()
{
// Get the active document and its database
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// Check if both colors have been chosen, if not, prompt the user
if (_color1 == null || _color2 == null)
{
ed.WriteMessage("nPlease choose 2 colors.");
return;
}
// Lock the document to prevent other operations while applying the colors
using (var lck = doc.LockDocument())
{
// Prompt the user to select a 3D object
PromptEntityOptions peo = new PromptEntityOptions("nSelect 3D object:");
peo.SetRejectMessage("nNot a Solid3d object.");
peo.AddAllowedClass(typeof(Solid3d), true); // Only allow Solid3d objects
var per = ed.GetEntity(peo);
// If the user did not select an object, exit the function
if (per.Status != PromptStatus.OK)
{
ed.WriteMessage("nObject not found.");
return;
}
// Start a transaction to modify the 3D object
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Get the Solid3d object from the ObjectId
Solid3d solid3d = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Solid3d;
if (solid3d == null)
{
// If the selected object is not a valid Solid3d, show an error
ed.WriteMessage("nSelected object is not a valid Solid3d.");
return;
}
// Create a FullSubentityPath to access the BREP (boundary representation)
var fullSubentityPath = new FullSubentityPath(new[] { per.ObjectId }, new SubentityId(SubentityType.Null, IntPtr.Zero));
using (var brep = new Brep(fullSubentityPath))
{
int faceIndex = 0;
var faces = brep.Faces.ToArray(); // Get all the faces of the 3D object
// Loop through each face and apply alternating colors
foreach (var face in faces)
{
try
{
var subEntityId = face.SubentityPath.SubentId;
var colorToApply = (faceIndex % 2 == 0) ? _color1 : _color2; // Apply color1 to even faces, color2 to odd faces
solid3d.SetSubentityColor(subEntityId, colorToApply); // Set the color of the face
faceIndex++; // Move to the next face
}
catch (System.Exception ex)
{
// Handle any exceptions and display an error message
ed.WriteMessage($"nERROR: {ex.Message}");
}
}
// Commit the transaction to apply the changes
tr.Commit();
}
}
}
}