Region creates another instance of polygon

Region creates another instance of polygon

Anonymous
Not applicable
583 Views
5 Replies
Message 1 of 6

Region creates another instance of polygon

Anonymous
Not applicable

Hi, I'm using this code below to create a region of an existing polygon. When I run this code it creates a region ok but it creates the region as a new entity and duplicates the polygon. Is there a way to covert the selected polygon into a region with out duplicating?

 

<CommandMethod("CreateRegion")>
Public Sub CreateRegion()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim db As Database = HostApplicationServices.WorkingDatabase
'Dim copied As ObjectIdCollection = New ObjectIdCollection()

'******Prompt user to select objects
Dim psr As PromptSelectionResult = doc.Editor.GetSelection()
'Dim acSSet As SelectionSet = psr.Value

If psr.Status = PromptStatus.OK Then
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim curves = New DBObjectCollection()
For Each obj As SelectedObject In psr.Value
curves.Add(tr.GetObject(obj.ObjectId, OpenMode.ForRead))
Dim regions = Region.CreateFromCurves(curves)
Dim curSpace As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)

For Each region In regions
curSpace.AppendEntity(region)
tr.AddNewlyCreatedDBObject(region, True)
Next
Next
tr.Commit()
ed.WriteMessage("Region created")
End Using
End If
End Sub

 

Thanks

Shaban.

0 Likes
Accepted solutions (1)
584 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

You have to explicitly erase the curves used to create the region.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

Anonymous
Not applicable

Do i do this before or after the tr.commit()??

0 Likes
Message 4 of 6

_gile
Consultant
Consultant

Before. Transaction.Commit() saves the changes to Database done with the transaction.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 6

Anonymous
Not applicable

Hi, do you have some example code of explicitly erase the "curves" used to create the region.

 

Thanks In advance

0 Likes
Message 6 of 6

_gile
Consultant
Consultant
Accepted solution
        [CommandMethod("CreateRegion")]
        public static void CreateRegion()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var psr = ed.GetSelection();
            if (psr.Status != PromptStatus.OK)
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var curves = new DBObjectCollection();
                foreach (SelectedObject so in psr.Value)
                {
                    var id = so.ObjectId;
                    if (id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Curve))))
                    {
                        curves.Add(tr.GetObject(id, OpenMode.ForWrite));
                    }
                }
                var regions = Region.CreateFromCurves(curves);
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (Region region in regions)
                {
                    curSpace.AppendEntity(region);
                    tr.AddNewlyCreatedDBObject(region, true);
                }
                foreach(Curve curve in curves)
                {
                    curve.Erase();
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes