Нужна помощь по изменению кода в ObjectArx Autocad.C#

Место общения программистов C#
Ответить
Anonymous
 Нужна помощь по изменению кода в ObjectArx Autocad.

Сообщение Anonymous »

У меня есть этот код на C#, работающий на .NET 4.8 в Visual Studio 2022. Кто-нибудь знает, как перейти от чередования двух цветов на объекте Solid3d к рисованию чередования двух цветов на каждой грани в объекте Solid3d?Более конкретно: при выборе 2 цветов в окне рисования Autocad (1-255) цвет1 будет применяться к четным граням, цвет2 будет применяться к нечетным граням. Я хочу изменить его так, чтобы оба цвета применялись поочередно на каждой грани объекта Solid3d.
Еще одна вещь, которую я сначала попробую использовать метод Solid3d.SetSubentityMaterial (Solid3d.SetSubentityColor применяет один цвет), но не удалось, кто-нибудь знает способ получше?
Вот мой код:
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();
}
}
}
}


Подробнее здесь: https://stackoverflow.com/questions/792 ... rx-autocad
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»