.NET

Reply
Valued Contributor
sanjaymann
Posts: 100
Registered: ‎05-22-2013
Message 1 of 3 (115 Views)

get connected lines

115 Views, 2 Replies
03-31-2014 04:42 AM

Hi,

 

How do  I get all lines connected to a selected line. The lines can cross other lines ,they can share same start or end point or they can share common vertices. In all I mean to say that I want all lines connected/touching/intersecting lines to the selected line.

 

Thanks & Regards

Sanjay Pandey

Valued Contributor
cdinten
Posts: 61
Registered: ‎11-19-2010
Message 2 of 3 (107 Views)

Re: get connected lines

03-31-2014 05:04 AM in reply to: sanjaymann
Entity.IntersectWith Method, Line is also a class derives from this abstract class
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 3 (85 Views)

Re: get connected lines

03-31-2014 08:56 AM in reply to: sanjaymann

Change this code to your needs:

      //_____________________________________________________________________//
        //ads_queueexpr
        [DllImport("accore.dll", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "ads_queueexpr")]
        extern static private int ads_queueexpr(byte[] command);


        [CommandMethod("tJL", CommandFlags.Modal | CommandFlags.UsePickSet)]
        public void TestConnectedLines()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            PromptEntityOptions pso = new PromptEntityOptions("\nPick a single line to join: ");

            pso.SetRejectMessage("\nObject must be of type Line!");

            pso.AddAllowedClass(typeof(Line), false);

            PromptEntityResult res = ed.GetEntity(pso);

            if (res.Status != PromptStatus.OK) return;
            using (DocumentLock doclock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {

                   Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable( "PICKFIRST",1); 
               Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable( "PEDITACCEPT",0); 
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;

                    List<ObjectId> ids = JoinLines(btr, res.ObjectId);

                    ObjectId[] lines = ids.ToArray();
                    
                    ed.SetImpliedSelection(lines);
                 
                    PromptSelectionResult chres = ed.SelectImplied();
              
                    if (chres.Status != PromptStatus.OK)
                    {
                        ed.WriteMessage("\nNothing added in the chain!");

                        return;
                    }
                        else
                        {
                        ed.WriteMessage(chres.Value.Count.ToString());
                        }
                    SelectionSet newset = SelectionSet.FromObjectIds(lines);

                    ed.SetImpliedSelection(newset);
           
                    string handles = "";
                    foreach (SelectedObject selobj in newset)
                    {
                        
                        Entity subent = tr.GetObject(selobj.ObjectId, OpenMode.ForWrite) as Entity;
                        string hdl=string.Format("(handent \"{0}\")",subent.Handle.ToString());
                        handles= handles + hdl + " ";
                  
                    }
                    System.Text.UnicodeEncoding uEncode = new System.Text.UnicodeEncoding();
                 // if PEDITACCEPT is set to 1 enshort the command avoiding "_Y" argument:
                    ads_queueexpr(uEncode.GetBytes("(COMMAND \"_.PEDIT\" \"_M\"" + handles  +  "\"\"" +  "\"_Y\" \"_J\" \"\" \"\")"));

                    tr.Commit();
                }
            }
        }

        private void SelectConnectedLines(BlockTableRecord btr, List<ObjectId> ids, ObjectId id)
        {
            Entity en = id.GetObject(OpenMode.ForRead, false) as Entity;

            Line ln = en as Line;

            if (ln != null)

                foreach (ObjectId idx in btr)
                {
                    Entity ex = idx.GetObject(OpenMode.ForRead, false) as Entity;

                    Line lx = ex as Line;

                    if (((ln.StartPoint == lx.StartPoint) || (ln.StartPoint == lx.EndPoint)) ||

                        ((ln.EndPoint == lx.StartPoint) || (ln.EndPoint == lx.EndPoint)))

                        if (!ids.Contains(idx))
                        {
                            ids.Add(idx);

                            SelectConnectedLines(btr, ids, idx);
                        }
                }
        }

        public List<ObjectId> JoinLines(BlockTableRecord btr, ObjectId id)
        {
            List<ObjectId> ids = new List<ObjectId>();

            SelectConnectedLines(btr, ids, id);

            return ids;
        }

        //_____________________________________________________________________//

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.