You can create composite regions by subtracting, combining, or finding the intersection of regions or 3D solids. You can then extrude or revolve composite regions to create complex solids. To create a composite region, use the BooleanOperation method.
When you subtract one region from another, you call the BooleanOperation method from the first region. This is the region from which you want to subtract. For example, to calculate how much carpeting is needed for a floor plan, call the BooleanOperation method from the outer boundary of the floor space and use the non-carpeted areas, such as pillars and counters, as the object in the Boolean parameter list.
To unite regions, call the BooleanOperation method and use the constant BooleanOperationType.BoolUnite for the operation instead of BooleanOperationType.BoolSubtract. You can combine regions in any order to unite them.
Find the intersection of two regions
To find the intersection of two regions, use the constant BooleanOperationType.BoolIntersect. You can combine regions in any order to intersect them.
The following example creates two regions from two circles and then subtracts the smaller region from the large one to create a wheel.
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("CreateCompositeRegions")> _
Public Sub CreateCompositeRegions()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' Create two in memory circles
Dim acCirc1 As Circle = New Circle()
acCirc1.Center = New Point3d(4, 4, 0)
acCirc1.Radius = 2
Dim acCirc2 As Circle = New Circle()
acCirc2.Center = New Point3d(4, 4, 0)
acCirc2.Radius = 1
'' Adds the circle to an object array
Dim acDBObjColl As DBObjectCollection = New DBObjectCollection()
acDBObjColl.Add(acCirc1)
acDBObjColl.Add(acCirc2)
'' Calculate the regions based on each closed loop
Dim myRegionColl As DBObjectCollection = New DBObjectCollection()
myRegionColl = Region.CreateFromCurves(acDBObjColl)
Dim acRegion1 As Region = myRegionColl(0)
Dim acRegion2 As Region = myRegionColl(1)
'' Subtract region 1 from region 2
If acRegion1.Area > acRegion2.Area Then
'' 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)
End If
'' Dispose of the in memory objects not appended to the database
acCirc1.Dispose()
acCirc2.Dispose()
'' Save the new object to the database
acTrans.Commit()
End Using
End Sub
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
Circle acCirc1 = new Circle();
acCirc1.Center = new Point3d(4, 4, 0);
acCirc1.Radius = 2;
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
acCirc1.Dispose();
acCirc2.Dispose();
// Save the new object to the database
acTrans.Commit();
}
}