Author Topic: Create region from connected arcs and lines  (Read 1035 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Create region from connected arcs and lines
« on: January 10, 2013, 08:23:44 AM »
Code: [Select]
        //Create region from connected arcs and lines
        [CommandMethod("craw")]
        public static void CreateRegionFromConnected()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            DBObjectCollection objs = new DBObjectCollection();

            DBObjectCollection regcoll = new DBObjectCollection();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                TypedValue[] tvs = new TypedValue[] { new TypedValue(0, "line,arc") };

                Point3dCollection points = new Point3dCollection();

                SelectionFilter sf = new SelectionFilter(tvs);

                PromptSelectionResult sres = ed.GetSelection(sf);

                if (sres.Status != PromptStatus.OK)
                {
                    ed.WriteMessage("\nInvalid selection!");

                    return;
                }

                foreach (SelectedObject selobj in sres.Value)
                {
                    DBObject obj = tr.GetObject(selobj.ObjectId, OpenMode.ForWrite, false) as DBObject;

                    objs.Add(obj);

                }

                regcoll = Region.CreateFromCurves(objs);

                Region reg = regcoll[0] as Region;

                btr.AppendEntity(reg);

                tr.AddNewlyCreatedDBObject(reg, true);
                // you might be want to remove the parent objects here,
                // because of DELOBJ variable did not effect :
                //foreach (DBObject obj in objs)
                //    obj.Erase();  // uncomment this code block if needs

                tr.Commit();
            }
        }