马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 csharp 于 2014-7-26 18:43 编辑
- [CommandMethod("GetUnionRegs")]
- public static void GetCurveUnionRegions()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- TypedValue[] val = new TypedValue[1]
- {
- new TypedValue(0, "REGION")
- };
- PromptSelectionOptions pso = new PromptSelectionOptions();
- pso.MessageForAdding = "\nSelect Regions";
- PromptSelectionResult psr = ed.GetSelection(pso, new SelectionFilter(val));
- if (psr.Status != PromptStatus.OK) return;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- SelectionSet ss = (SelectionSet)psr.Value;
- ObjectId[] ids = ss.GetObjectIds();
- try
- {
- RegionsUnion(ids);
- }
- catch (Exception)
- {
- throw;
- }
- tr.Commit();
- }
- }
- private static bool RectIntersects(Extents3d first, Extents3d second)
- {
- bool one = second.MinPoint.X < first.MaxPoint.X &&
- first.MinPoint.X < second.MaxPoint.X &&
- second.MinPoint.Y < first.MaxPoint.Y &&
- first.MinPoint.Y < second.MaxPoint.Y;
- bool two = first.MinPoint.X < second.MaxPoint.X &&
- second.MinPoint.X < first.MaxPoint.X &&
- first.MinPoint.Y < second.MaxPoint.Y &&
- second.MinPoint.Y < first.MaxPoint.Y;
- return one | two;
- }
- private static void RegionsUnion(ObjectId[] ids)
- {
- Matrix3d mat = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
- for (int i = 0; i < ids.Length; i++)
- {
- if (!ids[i].IsNull)
- {
- Region fRegion = (Region)ids[i].GetObject(OpenMode.ForRead);
- using (fRegion)
- {
- for (int j = i + 1; j < ids.Length; j++)
- {
- Extents3d ext1 = fRegion.GeometricExtents;
- Region sRegion = (Region)ids[j].GetObject(OpenMode.ForRead);
- if (!sRegion.IsNull)
- {
- Extents3d ext2 = sRegion.GeometricExtents;
- Region tmpfRegion = (Entity) fRegion.Clone() as Region;
- Region tmpsRegion = (Entity) sRegion.Clone() as Region;
- if (RectIntersects(ext1, ext2))
- {
- tmpfRegion.BooleanOperation(BooleanOperationType.BoolIntersect, tmpsRegion);
- if (!tmpfRegion.IsNull)
- {
- fRegion.UpgradeOpen();
- sRegion.UpgradeOpen();
- fRegion.BooleanOperation(BooleanOperationType.BoolUnite, sRegion);
- }
- }
- }
- }
- }
- }
- }
- }
|