Combine multiple regions and use the 'Union' command to merge them all at once

Combine multiple regions and use the 'Union' command to merge them all at once

artillis_prado
Enthusiast Enthusiast
459 Views
1 Reply
Message 1 of 2

Combine multiple regions and use the 'Union' command to merge them all at once

artillis_prado
Enthusiast
Enthusiast
In civil engineering, I have a quantity that can vary from 4,000 to 13,000 Regions to unite. I've already tested it with the command that adds one by one, but it takes a lot of time. Using this option, I can select several elements of the project and unite everything in one second. I would like to know if anyone has used this Union via .NET C# to add a set of elements.

artillis_prado_0-1718912983294.png

 

0 Likes
460 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant

Hi,

From the tests I did calling the UNION comand (directly or from code) is not faster than calling the Region.BooleanOperation.

 

[CommandMethod("TEST1")]
public void Test1()
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;

    var filter = new SelectionFilter(new[] { new TypedValue(0, "REGION") });
    var selection = ed.GetSelection(filter);
    if (selection.Status == PromptStatus.OK)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        using (var tr = db.TransactionManager.StartTransaction())
        {
            selection.Value.GetObjectIds()
                .Select (id => (Region)tr.GetObject(id, OpenMode.ForWrite))
                .Aggregate((r1, r2) =>
                {
                    r1.BooleanOperation(BooleanOperationType.BoolUnite, r2);
                    return r1;
                });
            tr.Commit();
        }
        stopwatch.Stop();
        ed.WriteMessage($"\nElapsed millisecond: {stopwatch.ElapsedMilliseconds} to union {selection.Value.Count} regions.");
    }
}

[CommandMethod("TEST2")]
public void Test2()
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;

    var filter = new SelectionFilter(new[] { new TypedValue(0, "REGION") });
    var selection = ed.GetSelection(filter);
    if (selection.Status == PromptStatus.OK)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        ed.Command("_.UNION", selection.Value, "");
        stopwatch.Stop();
        ed.WriteMessage($"\nElapsed millisecond: {stopwatch.ElapsedMilliseconds} to union {selection.Value.Count} regions.");
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub