合成リージョンを作成する(.NET)

合成リージョンは、リージョンまたは 3D ソリッドの交点を取り去ったり、組み合わせたり、検出したりして作成できます。次に、合成リージョンを押し出したり回転させたりして、複雑なソリッドを作成できます。合成リージョンを作成するには、BooleanOperation メソッドを使用します。

リージョンを取り去る

あるリージョンを別のリージョンから取り去るときは、最初のリージョンから BooleanOperation メソッドを呼び出します。これが、リージョンを取り去る元のリージョンになります。たとえば、平面図でどれだけのカーペットが必要かを計算するには、床面積の外側境界線から BooleanOperation メソッドを呼び出し、柱やカウンタなどのカーペットを敷かない領域を Boolean パラメータ リスト内のオブジェクトとして使用します。

リージョンを結合する

リージョンを結合するには、BooleanOperation メソッドを呼び出し、BooleanOperationType.BoolSubtract の代わりに、定数 BooleanOperationType.BoolUnite を演算に使用します。リージョンは任意の順序で組み合わせて、結合させることができます。

2 つのリージョンの交差を求める

2 つのリージョンの交差を求めるには、定数 BooleanOperationType.BoolIntersect を使用します。リージョンは任意の順序で組み合わせて、交差させることができます。

合成リージョンを作成する

次の例では、2 つの円で 2 つのリージョンを作成し、大きい方のリージョンから小さい方のリージョンを取り去ってホイールを作成します。

C# の例

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("CreateCompositeRegions")]
public static void CreateCompositeRegions()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                     OpenMode.ForRead) as BlockTable;

        // Open the Block table record Model space for write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForWrite) as BlockTableRecord;

        // Create two in memory circles
        using (Circle acCirc1 = new Circle())
        {
            acCirc1.Center = new Point3d(4, 4, 0);
            acCirc1.Radius = 2;

            using (Circle acCirc2 = new Circle())
            {
                acCirc2.Center = new Point3d(4, 4, 0);
                acCirc2.Radius = 1;

                // Adds the circle to an object array
                DBObjectCollection acDBObjColl = new DBObjectCollection();
                acDBObjColl.Add(acCirc1);
                acDBObjColl.Add(acCirc2);

                // Calculate the regions based on each closed loop
                DBObjectCollection myRegionColl = new DBObjectCollection();
                myRegionColl = Region.CreateFromCurves(acDBObjColl);
                Region acRegion1 = myRegionColl[0] as Region;
                Region acRegion2 = myRegionColl[1] as Region;

                // Subtract region 1 from region 2
                if (acRegion1.Area > acRegion2.Area)
                {
                    // Subtract the smaller region from the larger one
                    acRegion1.BooleanOperation(BooleanOperationType.BoolSubtract, acRegion2);
                    acRegion2.Dispose();

                    // Add the final region to the database
                    acBlkTblRec.AppendEntity(acRegion1);
                    acTrans.AddNewlyCreatedDBObject(acRegion1, true);
                }
                else
                {
                    // Subtract the smaller region from the larger one
                    acRegion2.BooleanOperation(BooleanOperationType.BoolSubtract, acRegion1);
                    acRegion1.Dispose();

                    // Add the final region to the database
                    acBlkTblRec.AppendEntity(acRegion2);
                    acTrans.AddNewlyCreatedDBObject(acRegion2, true);
                }

                // Dispose of the in memory objects not appended to the database
            }
        }

        // Save the new object to the database
        acTrans.Commit();
    }
}