joining entities doesnt seem to do anything

joining entities doesnt seem to do anything

adrian_sieradzki
Contributor Contributor
718 Views
3 Replies
Message 1 of 4

joining entities doesnt seem to do anything

adrian_sieradzki
Contributor
Contributor

Hello,

In .NET sdk for autocad I am trying to replicate _JOIN command. I have an entity[] which are all splines and would like to join it to as few splines/entities as possible.

entityArray.ForEach( x => x.JoinEntities(entityArray));
 

but not even single spline is joined to another spline. Surely I must be missing something because debugger doesnt throw any exceptions and executes normally. 

0 Likes
Accepted solutions (1)
719 Views
3 Replies
Replies (3)
Message 2 of 4

adrian_sieradzki
Contributor
Contributor
Accepted solution

Okay, figured this out but its quite weird


//THIS WORKS
                foreach (Entity entity in device.myEntities)
                {
                    entity.JoinEntities(device.myEntities.ToArray());
                    btr.AppendEntity(entity);
                    tr.AddNewlyCreatedDBObject(entity, true);

                }
//THIS DOESNT WORK
                device.myEntities.ForEach(x => x.JoinEntities(device.myEntities.ToArray()));
foreach (Entity entity in device.myEntities)
                {
                    btr.AppendEntity(entity);
                    tr.AddNewlyCreatedDBObject(entity, true);

                }
0 Likes
Message 3 of 4

_gile
Consultant
Consultant

Hi,

Entity.JoinEntities method does not work this way. It have to be called on one instance of entity and requires as argument an array of entities.

Here's an example:

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

            // Prompt the user to select the primary spline
            var entityOptions = new PromptEntityOptions("\nSelect primary spline: ");
            entityOptions.SetRejectMessage("\nMust be a spline.");
            entityOptions.AddAllowedClass(typeof(Spline), true);
            var entityResult = ed.GetEntity(entityOptions);
            if (entityResult.Status != PromptStatus.OK)
                return;

            // Prompt the user to select the spline to join to the first one.
            var filter = new SelectionFilter(new[]
            {
                new TypedValue(0, "SPLINE"),
                new TypedValue(-4, "<NOT"),
                new TypedValue(-4, "&"),
                new TypedValue(70, 1),
                new TypedValue(-4, "NOT>")
            });
            var selectionOptions = new PromptSelectionOptions();
            selectionOptions.MessageForAdding = "\nSelect the spline to join: ":
            var selection = ed.GetSelection(filter);
            if (selection.Status != PromptStatus.OK)
                return;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                // Open the first spline
                var spline = (Spline)tr.GetObject(entityResult.ObjectId, OpenMode.ForWrite);

                // Create an array with the other splines
                var otherSplines = selection.Value
                    .GetObjectIds()
                    .Select(id => (Spline)tr.GetObject(id, OpenMode.ForWrite))
                    .ToArray();

                try
                {
                    // Join the selected splines to primary one
                    var indices = spline.JoinEntities(otherSplines);

                    // Erase the joined plines
                    foreach (int index in indices)
                    {
                        otherSplines[index].Erase();
                    }
                }
                catch(Autodesk.AutoCAD.Runtime.Exception ex) when (ex.ErrorStatus == ErrorStatus.NotApplicable)
                {
                    ed.WriteMessage("\nThe first spline does not join to any selected ones");
                }
                catch(System.Exception ex)
                {
                    ed.WriteMessage($"\n{ex.Message}");
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

adrian_sieradzki
Contributor
Contributor

Yes it does, see my answer above.
For some reason in only works if I call it inside the same loop as my 

tr.AddNewlyCreatedDBObject

There must be some object manipulating behaviour I am not aware of.

My last 3 days of playtime with AutoCAD has led me to believe the code-base must be incredible spaghetti.

If you are looking to re-write it with some modern framework such as blazor or tauri I can help you as part-time consultant. 

0 Likes