четверг, 29 мая 2014 г.

Выполнение логических операций с областями (regions)

В приведённом ниже коде сначала создаётся две области (красная и жёлтая), затем к их копиям применяются различные логические операции. Результат размещается рядом и подсвечивается зелёным цветом. Результат работы кода показан на скрине.

Комментировать тут особо не чего, поэтому сразу код:

   1:  /* RegionBooleanOperationsSample.cs
   2:   * © Andrey Bushman, 2014 
   3:   */
   4:  using System;
   5:  using System.Collections.Generic;
   6:  using System.Linq;
   7:   
   8:  #if AUTOCAD
   9:  using cad = Autodesk.AutoCAD.ApplicationServices.Application;
  10:  using Ap = Autodesk.AutoCAD.ApplicationServices;
  11:  using Db = Autodesk.AutoCAD.DatabaseServices;
  12:  using Ed = Autodesk.AutoCAD.EditorInput;
  13:  using Rt = Autodesk.AutoCAD.Runtime;
  14:  using Gm = Autodesk.AutoCAD.Geometry;
  15:  using Wn = Autodesk.AutoCAD.Windows;
  16:  using Hs = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices;
  17:  using Us = Autodesk.AutoCAD.DatabaseServices.SymbolUtilityServices;
  18:  #endif
  19:   
  20:  [assembly: Rt.CommandClass(typeof(Bushman.CAD.Experiences
  21:    .RegionBooleanOperationsSample))]
  22:   
  23:  namespace Bushman.CAD.Experiences {
  24:    public class RegionBooleanOperationsSample {
  25:      const String cmdGroup = "Bushman";
  26:      [Rt.CommandMethod(cmdGroup, "CreateRegions", Rt.CommandFlags.Modal)]
  27:      public void CreateRegions() {
  28:        Ap.Document doc = cad.DocumentManager.MdiActiveDocument;
  29:        if(doc == null || doc.IsDisposed)
  30:          return;
  31:        using(doc.LockDocument()) {
  32:          Ed.Editor ed = doc.Editor;
  33:          Db.Database db = doc.Database;
  34:   
  35:          Gm.Point2d[] points_1 = new Gm.Point2d[]{
  36:            new Gm.Point2d(0,0),
  37:            new Gm.Point2d(0,70),
  38:            new Gm.Point2d(10,70),
  39:            new Gm.Point2d(10,10),
  40:            new Gm.Point2d(40,10),
  41:            new Gm.Point2d(40,70),
  42:            new Gm.Point2d(50,70),
  43:            new Gm.Point2d(50,10),
  44:            new Gm.Point2d(80,10),
  45:            new Gm.Point2d(80,70),
  46:            new Gm.Point2d(90,70),
  47:            new Gm.Point2d(90,0)
  48:          };
  49:          Db.Region region_1 = CreateRegion(points_1, 10);
  50:   
  51:          Gm.Point2d[] points_2 = new Gm.Point2d[]{          
  52:            new Gm.Point2d(0,70),
  53:            new Gm.Point2d(90,70),
  54:            new Gm.Point2d(90,60),
  55:            new Gm.Point2d(0,60)
  56:          };
  57:          Db.Region region_2 = CreateRegion(points_2, 50);
  58:   
  59:          using(Db.Transaction tr = db.TransactionManager.StartTransaction()) {
  60:            Db.BlockTable bt = tr.GetObject(db.BlockTableId, Db.OpenMode.ForRead)
  61:              as Db.BlockTable;
  62:            Db.BlockTableRecord ms = tr.GetObject(bt[Db.BlockTableRecord
  63:              .ModelSpace], Db.OpenMode.ForWrite) as Db.BlockTableRecord;
  64:            ms.AppendEntity(region_1);
  65:            tr.AddNewlyCreatedDBObject(region_1, true);
  66:            ms.AppendEntity(region_2);
  67:            tr.AddNewlyCreatedDBObject(region_2, true);
  68:   
  69:            Db.DBText text = new Db.DBText();
  70:            text.SetDatabaseDefaults();
  71:            text.Height = 5;
  72:            text.TextString = "Base regions";
  73:            text.Position = new Gm.Point3d(0, 80, 0);
  74:            ms.AppendEntity(text);
  75:            tr.AddNewlyCreatedDBObject(text, true);
  76:   
  77:            Int32 dx = 120;
  78:            Int32 x = dx;
  79:   
  80:            foreach(String name in Enum.GetNames(typeof(Db.BooleanOperationType)
  81:              )) {
  82:              Db.Region region_1Clone = region_1.Clone() as Db.Region;
  83:              Db.Region region_2Clone = region_2.Clone() as Db.Region;
  84:              Db.DBText textClone = text.Clone() as Db.DBText;
  85:              textClone.TextString = String.Format("The {0} operation", name);
  86:   
  87:              Gm.Point3d acPt3d = new Gm.Point3d(0, 0, 0);
  88:              Gm.Vector3d acVec3d = acPt3d.GetVectorTo(new Gm.Point3d(x, 0,
  89:                0));
  90:   
  91:              region_1Clone.TransformBy(Gm.Matrix3d.Displacement(acVec3d));
  92:              region_2Clone.TransformBy(Gm.Matrix3d.Displacement(acVec3d));
  93:              textClone.TransformBy(Gm.Matrix3d.Displacement(acVec3d));
  94:   
  95:              region_2Clone.BooleanOperation((Db.BooleanOperationType)Enum
  96:                .Parse(typeof(Db.BooleanOperationType), name), region_1Clone);
  97:   
  98:              if(!region_2Clone.IsNull && region_1Clone.IsNull)
  99:                region_2Clone.ColorIndex = 80;
 100:   
 101:              ms.AppendEntity(region_1Clone);
 102:              tr.AddNewlyCreatedDBObject(region_1Clone, true);
 103:              ms.AppendEntity(region_2Clone);
 104:              tr.AddNewlyCreatedDBObject(region_2Clone, true);
 105:              ms.AppendEntity(textClone);
 106:              tr.AddNewlyCreatedDBObject(textClone, true);
 107:   
 108:              x += dx;
 109:            }
 110:            tr.Commit();
 111:          }
 112:        }
 113:        doc.SendStringToExecute("_ZOOM _E ", true, false, false);
 114:      }
 115:   
 116:      internal Db.Region CreateRegion(Gm.Point2d[] points, Int32 colorIndex) {
 117:        if(points == null)
 118:          throw new ArgumentNullException("points");
 119:        if(points.Length == 0)
 120:          throw new ArgumentException("points.Length == 0");
 121:        using(Db.Polyline pline = new Db.Polyline(12)) {
 122:          pline.SetDatabaseDefaults();
 123:          for(int i = 0; i < points.Length; i++)
 124:            pline.AddVertexAt(i, points[i], 0, 0, 0);
 125:          pline.Closed = true;
 126:          Db.Region region = Db.Region.CreateFromCurves(
 127:            new Db.DBObjectCollection { pline }).Cast<Db.Region>().First();
 128:          region.SetDatabaseDefaults();
 129:          region.ColorIndex = colorIndex;
 130:          return region;
 131:        }
 132:      }
 133:    }
 134:  }

Результат выглядит следующим образом:



Комментариев нет: