Join using Command

Join using Command

SENL1362
Advisor Advisor
2,062 Views
4 Replies
Message 1 of 5

Join using Command

SENL1362
Advisor
Advisor

I am kind of stuck getting the JOIN command accepting my selected entities. The error messages are:

Value does not fall within the expected range

From within AutoCAD the selected entities could be JOINed of course.

 

So what's wrong with the code:

1. Is it the way objects are added to the ed.Command(...);

2. Or has it something to do with the JOIN command itself

 

Thanks for any help.

 

 

 

These are the trials so far.

 

       //Goal: Create Polyline from calculated objects using JOIN command
        [CommandMethod("TJC", CommandFlags.UsePickSet | CommandFlags.Modal)]
        public static  async void TestJoinCommand()
        {
            Document doc = null;
            Database db = null;
            Editor ed = null;

            try
            {
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                if (doc == null)
                    throw new System.Exception("No MdiActiveDocument");
                db = doc.Database;
                ed = doc.Editor;

                //ed.Command("_.JOIN", ed.SelectImplied()); //Error: Value does not fall within the expected range
                //ed.Command("_.JOIN", objIds[0], objIds.Skip(1)); //Error: Value does not fall within the expected range

                //PromptSelectionResult selectionRes1 = ed.SelectImplied();
                //ObjectId[] objIds = selectionRes1.Value.GetObjectIds();
                //ed.Command("_.JOIN", objIds[0], objIds[0], objIds[1], objIds[2], "");
                //ed.SetImpliedSelection(new ObjectId[0]);
                //ed.Command("_.JOIN", objIds); //Error: Value does not fall within the expected range
                
                


                //ed.SetImpliedSelection(selectionRes1.Value);
                //PromptSelectionResult selectionRes2 = ed.SelectImplied();
                //ed.Command(new object[] { "_.JOIN", selectionRes2 }); //Invalid range
                //ed.Command("_.JOIN", selectionRes1.Value);  //Bad Selection Set

                //await ed.CommandAsync("_.JOIN");
                //using (Transaction tr = db.TransactionManager.StartTransaction())
                //{
                //    foreach (ObjectId objId in objIds)
                //    {
                //        Entity ent = (Entity)tr.GetObject(objId, OpenMode.ForRead);
                //        await ed.CommandAsync(ent.ObjectId);
                //    }
                //    await ed.CommandAsync("");
                //    tr.Commit();
                //}

                //Test1: Send PickSet. Failed
                if (ed.SelectImplied().Value != null)
                    ed.Command("_.JOIN");

                //Test2: Send PickSet, and close selection. Failed
                if (ed.SelectImplied().Value != null)
                    ed.Command(new object[]{"_.JOIN", ""});
                    ed.Command("_.JOIN","");

                //Test3: Add objects one at the time. Failed
                SelectionSetDelayMarshalled ssMarshal = (SelectionSetDelayMarshalled)ed.SelectImplied().Value;
                var selObjIds = ssMarshal.GetObjectIds();
                ed.SetImpliedSelection(new ObjectId[0]);
                SelectionSet selSet = (SelectionSet)ssMarshal;
                using (DocumentLock docLck = doc.LockDocument())
                {
                     await ed.CommandAsync("_.JOIN");
                     await ed.CommandAsync(selObjIds[0]);
                     for (int i = 1; i < selObjIds.Length; i++)
                         await ed.CommandAsync(selObjIds[i]);
                     await ed.CommandAsync("");
                }
            }
            catch (System.Exception ex)
            {
                if (ed != null)
                    ed.WriteMessage("\n Error in TestJoinCommand: {0}", ex.Message);
            }
        }

 

 

Accepted solutions (1)
2,063 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

 

It looks like the "JOIN" command called by Editor.Command() doesn't work exactly the same as the one called from within AutoCAD.

 

Maybe you'd be more lucky with the "PEDIT" command.

 

        [CommandMethod("TJC", CommandFlags.Modal | CommandFlags.UsePickSet)]
        public static void TestJoinCommand()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptSelectionResult psr = ed.GetSelection();
            if (psr.Status != PromptStatus.OK)
                return;
            object peditAccept = Application.GetSystemVariable("PEDITACCEPT");
            Application.SetSystemVariable("PEDITACCEPT", 1);
            try
            {
                ed.Command("_.PEDIT", "_Multiple", psr.Value, "", "_Join", 0.0, "");
            }
            catch (Exception ex)
            {
                ed.WriteMessage("\nError: n{0}", ex.Message);
            }
            finally
            {
                Application.SetSystemVariable("PEDITACCEPT", peditAccept);
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

SENL1362
Advisor
Advisor
Hi Gilles,
Thanks for confirming my issues. I hoped the JOIN command could replace the somewhat complicated c# implementation to build polylines from entities.
Also thanks for sending the PEDIT alternative.
0 Likes
Message 4 of 5

_gile
Consultant
Consultant

You're welcome.

If you want to implement your own method, you can use or get some inspiration from the GeometryExtension library, mainly the PolylineSegment and PolylineSegmentcollection classes (the second one have an overloaded Join() method).

A using example here.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

SENL1362
Advisor
Advisor
Thanks for pointing out to that code, I am already using parts of your library.
However in another discussion (i think Kean took part in that) the use of the ed.Command functions where recommended, and thus thought lets try to implement a JOIN function using ed.Command.

Again thanks for you're considerations.
0 Likes