Message 1 of 4
Regions subtract problem.
Not applicable
09-10-2012
01:32 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have closed polylines - few rectangles, some of them are inside another (picture).
Now I would like to:
1. Calculate areas of each closed polyline.
2. Calculate areas of each closed polyline subtract area of inside polyline.
Everything seems to work if I have only 1 polyline or 2 (first inside second). If I have more, I'm always getting zero.
Here is my code (ClipLines is a list of closed polylines):
private static void CalculateClips(List<ClipsLines> cls)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
DBObjectCollection dbCl = new DBObjectCollection();
DBObjectCollection myRegionColl = new DBObjectCollection();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
foreach (ClipsLines cl in cls)
{
DBObject obj = tr.GetObject(cl.id, OpenMode.ForRead);
dbCl.Add(obj);
}
myRegionColl = Region.CreateFromCurves(dbCl);
for (int i = 0; i < myRegionColl.Count; i++)
{
Region r1 = myRegionColl[i] as Region;
double mainRegArea = r1.Area;
for (int j = 0; j < myRegionColl.Count; j++)
{
if (i != j)
{
Region r2 = myRegionColl[j] as Region;
r1.BooleanOperation(BooleanOperationType.BoolSubtract, r2);
if (r1.Area > 0)
mainRegArea -= r1.Area;
}
}
doc.Editor.WriteMessage("\nReg1: " + mainRegArea);
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
tr.Abort();
utilities.ErrorMessage("\nClips calculating exception!\n" + ex.Message);
}
}
}