.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

get connected lines

9 REPLIES 9
Reply
Message 1 of 10
sanjaymann
2012 Views, 9 Replies

get connected lines

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

9 REPLIES 9
Message 2 of 10
cdinten
in reply to: sanjaymann

Entity.IntersectWith Method, Line is also a class derives from this abstract class
Message 3 of 10
Hallex
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
Message 4 of 10
BKSpurgeon
in reply to: sanjaymann

There is a built in autcad command called "FS" - fast selection or fast selection (can't remember which).

it selects lines which are connected to your main line.

If you switch FSMODE to "ON" you can also have all subsidiary lines selected if they are in any way connected to your main base line.

it's in the autocad 2015 version.
Message 5 of 10
BKSpurgeon
in reply to: Hallex

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

for some reason, I cannot declare the List collection (see above) in Visual studio - intellisense is not coming up with that option.

any ideas?
Message 6 of 10
hgasty1001
in reply to: BKSpurgeon

Hi,

 

Try importing: System.Collections.Generic Namespace into the class you are coding.

 

Gaston Nunez

Message 7 of 10
BKSpurgeon
in reply to: hgasty1001

excellent thank you!

for everyone's reference, also found that in addition to that, these two need to be added too:

using System.Collections.Generic;
using System.Runtime.InteropServices;

rgds

BK
Message 8 of 10
BKSpurgeon
in reply to: Hallex

// if PEDITACCEPT is set to 1 enshort the command avoiding "_Y" argument:
                    ads_queueexpr(uEncode.GetBytes("(COMMAND \"_.PEDIT\" \"_M\"" + handles  +  "\"\"" +  "\"_Y\" \"_J\" \"\" \"\")"));

 

 

what is the purpose of the above here?

 

When I sent to the ojbectarx reference guide to see what ads_queueexpr means i was meant with confusion: 

 

  • ads_queueexpr Function
  • This function queues up the LISP expression expr to be executed the next time AutoCAD is quiescent and able to evaluate LISP expressions. This function should only be called from within the kLoadDwgMsg case in your acrxEntryPoint() function. Queued LISP expressions will be executed after the (s::startup) has been executed. Multiple calls to ads_queueexpr() from the kLoadDwgMsg callback are perfectly acceptable. They get queued up in the order of the calls. ads_queueexpr Function
  • Do not use ads_queueexpr() in any context other then the kLoadDwgMsg case of acrxEntryPoint(). To do so will have unpredictable results, and may even be damaging, because the queue may not be looked at until active AutoLISP evaluations, MENU items, and/or SCRIPT executions are all finished.

 

I do not understand much of the above.

 

  1. any explanation was to what ads_queueexpr(uEncode.GetBytes("(COMMAND \"_.PEDIT\" \"_M\"" + handles + "\"\"" + "\"_Y\" \"_J\" \"\" \"\")")); does would be helpful.
  2. since pedit is set to 0 the "\"_Y\" is omitted in my code leaving behind this: ads_queueexpr(uEncode.GetBytes("(COMMAND \"_.PEDIT\" \"_M\"" + handles + "\"\"" + " \"_J\" \"\" \"\")"));

 

Am i doing this right? Any assistance much appreciated.

 

 

 

Message 9 of 10
BKSpurgeon
in reply to: sanjaymann

For anyone reading this perhaps you can make use of the following routine Mr Gilles has kindly provided:

 

https://www.theswamp.org/index.php?action=post;quote=446965;topic=31865.15;last_msg=565479

 

it seems to work very quickly - incredibly quickly compared to the algorithm posted above. You will also need the GeometryExtensions Library also kindly provided by Mr Gilles.

 

good luck.

 

 

Message 10 of 10
kdub_nz
in reply to: BKSpurgeon

Here is a direct link to that post :

https://www.theswamp.org/index.php?topic=31865.msg446965#msg446965


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost