create many regions at once

create many regions at once

Anonymous
Not applicable
2,726 Views
7 Replies
Message 1 of 8

create many regions at once

Anonymous
Not applicable

Greetings,

The DWG have thousands of connecting flat lines. By using the REGION command we can instantly create bounded regions from the lines.

How can I do the same with dot net?

The knowledge network shows a single path example like this...

 

DBObjectCollection myRegionColl = new DBObjectCollection();
myRegionColl = Region.CreateFromCurves(acDBObjColl);
Region acRegion = myRegionColl[0] as Region;
acBlkTblRec.AppendEntity(acRegion);

where the collection was a single circle

 

I guess ... Region acRegion = myRegionColl[0] as Region;  is expecting a single bounded planar object ... and will not work with thousands of lines

Thx,

Kevin.

 

 

0 Likes
Accepted solutions (1)
2,727 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant
Accepted solution

Hi,

 

This works for me:

 

        [CommandMethod("Test")]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            var selection = ed.GetSelection();
            if (selection.Status != PromptStatus.OK)
                return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var curves = new DBObjectCollection();
                foreach (SelectedObject obj in selection.Value)
                {
                    curves.Add(tr.GetObject(obj.ObjectId, OpenMode.ForRead));
                }
                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);
                }
                tr.Commit();
                ed.WriteMessage($"\nCreated {regions.Count} region(s).");
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 8

Anonymous
Not applicable

Thx Gilles ... neat clean example... very nice   : )

0 Likes
Message 4 of 8

Anonymous
Not applicable

Gilles, I hope you have 30 odd minutes to check this. I have attached a DWG. top left are 3D lines, left of that a copy of the lines flattened, left of that the same lines converted to regions with the region command.

next row, copies of the same flattened lines; on running code on final copy of lines, error: invalid.

...

Looks like the region command is forgiving and skips invalid line objects; but we need to check for validity before attempting:

var regions = Region.CreateFromCurves(curves)

... how do we check for validity?

 

0 Likes
Message 5 of 8

_gile
Consultant
Consultant

I took a look, but I did not find the cause of the error. By making several successive selections one succeeds in creating all the regions.
I'm sorry but I do not have more time to devote to that now.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 8

Anonymous
Not applicable

Gilles, thx for continuing to help with this,there's no rush in solving this, we will solve this eventually...

I went back and applied the region command... check the command line text...

Select objects: Specify opposite corner: 6554 found
Select objects:
2188 loops extracted.
3 loops rejected.
Vertex with degree greater than two : 3 loops.
2185 Regions created.

...

So, there's an indicator here... Vertex with degree greater than two: 3 loops and those 3 loops rejected.

I just need to write code to do the same rejection from the object collection (curves) ...

Vertex with degree greater than two ... hmmmmm how do we check for that in code?

 

0 Likes
Message 7 of 8

Anonymous
Not applicable

I just used overkill prior to region, enabled "combine co-linear objects that partially overlap" and "combine co-linear objects when aligned end to end". on using region after this overkill, the number of rejected increases from 3 to 14, same message "Vertex with degree greater than two".

 

0 Likes
Message 8 of 8

Anonymous
Not applicable

I am going to cheat and return to the awful SEND COMMAND eeeuuuggghhh  ...

Autodesk api developers please improve the Region.CreateFromCurves method to work exactly the same as the region command (rejecting strange inconsistencies) ...

 

List<ObjectId> ids = new List<ObjectId>();
ids.AddRange(selection.Value.GetObjectIds());
ed.SetImpliedSelection(ids.ToArray());
doc.SendStringToExecute(".region p  ", false, false, false);
0 Likes