Autodesk Community Tipsではちょっとしたコツ、やり方、ショートカット、アドバイスやヒントを共有しています。
Issue
AutoCAD .NET API を利用してサーフェス同士を結合することは出来ますか?
Solution
AutoCAD .NET API には、サーフェス同士を結合する ObjectARX の AcDbSurface::booleanUnion() メンバ関数ラッパーである サーフェス同士を結合する Surface.BooleanUnion メソッドが用意されています。
次の C# 例は、Surface.BooleanUnion メソッドでサーフェス同士を結合するものです。
[CommandMethod("MyCommand", CommandFlags.Modal)]
public void MyCommand() // This method can have any name
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\n結合するサーフェスを選択:");
peo.SetRejectMessage("\nサーフェスを選択してください...");
peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Surface), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
ObjectId objId1 = per.ObjectId;
peo = new PromptEntityOptions("\n結合させたいサーフェスを選択:");
peo.SetRejectMessage("\nサーフェスを選択してください...");
peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Surface), false);
per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
ObjectId objId2 = per.ObjectId;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Autodesk.AutoCAD.DatabaseServices.Surface surf1 = tr.GetObject(objId1, OpenMode.ForWrite) as Autodesk.AutoCAD.DatabaseServices.Surface;
Autodesk.AutoCAD.DatabaseServices.Surface surf2 = tr.GetObject(objId2, OpenMode.ForWrite) as Autodesk.AutoCAD.DatabaseServices.Surface;
Autodesk.AutoCAD.DatabaseServices.Surface surf = surf1.BooleanUnion(surf2);
if (surf == null)
{
if (surf1 != null)
{
surf1.ColorIndex = 5;
surf1.LineWeight = LineWeight.LineWeight100;
}
if (surf2 != null)
{
surf2.Erase();
}
}
else
{
surf1.Erase();
surf2.Erase();
ObjectId objId = SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord btr = tr.GetObject(objId, OpenMode.ForWrite) as BlockTableRecord;
surf.ColorIndex = 5;
surf.LineWeight = LineWeight.LineWeight100;
btr.AppendEntity(surf);
tr.AddNewlyCreatedDBObject(surf, true);
}
tr.Commit();
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\n" + ex.Message);
}
}