Author Topic: How to select all the lines without drag the cursor?  (Read 2835 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
How to select all the lines without drag the cursor?
« on: August 12, 2011, 12:50:50 AM »
Hello,everyone.I want to create a command to select all the lines automatically,without demanding users to drag the cursor to select.The code below is all I can get by my own.Can anyone help me improve it ?
Code: [Select]
[CommandMethod("AllLines")]
        public void seletcAllLines()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    //define the criteria for selecting lines only
                    TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "LINE") };
                    SelectionFilter flt = new SelectionFilter(tv);

                    /*This method needs to drag the cursor,which I think is not smart.
                     I will want to improve it.*/
                    PromptSelectionOptions optSel = new PromptSelectionOptions();
                    optSel.MessageForAdding = "You will need to drag this cursor to select.";
                    PromptSelectionResult resSel = ed.GetSelection(optSel,flt);

                    SelectionSet selSet = resSel.Value;
                    ObjectId[] ids = selSet.GetObjectIds();

                    foreach (ObjectId sSetEntId in ids)
                    {
                       
                        Entity ent = (Entity)trans.GetObject(sSetEntId, OpenMode.ForWrite);
                        if (ent.GetType().Name=="Line")  //Is there any better ideal to write the condition,the"typeof(ent) is Line" just gets an error!
                        {
                            Line myLine = (Line)trans.GetObject(sSetEntId, OpenMode.ForWrite);
                            ed.WriteMessage("\n"+myLine.StartPoint.ToString()+ myLine.EndPoint.ToString());
                        }
                     

             }
                    trans.Commit();
                }
                catch(System.Exception ex)
                {
                    ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                }
            }


Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to select all the lines without drag the cursor?
« Reply #1 on: August 12, 2011, 01:41:18 AM »
Welcome to the Swamp!
 
What you can do is use Editor.SelectAll(filter)
 
That selects all the entites and filtering them out using your filter which requires no user action.
 
Since it will be filtered to lines no real reason to check if it is a line, should only open for write if you are going to modify it.
Code: [Select]

  [CommandMethod("AllLines")]
        public void seletcAllLines()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "LINE") };
                    SelectionFilter flt = new SelectionFilter(tv);
                    PromptSelectionResult resSel = ed.SelectAll(flt);                   
                    SelectionSet selSet = resSel.Value;
                    ObjectId[] ids = selSet.GetObjectIds();
                    foreach (ObjectId sSetEntId in ids)
                    {
                        Line myLine = (Line)trans.GetObject(sSetEntId, OpenMode.ForWrite);
                        ed.WriteMessage("\n" + myLine.StartPoint.ToString() + myLine.EndPoint.ToString());
                    }
                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                }
            }
        }

Another way would be to itterate through model space or all spaces grabbing the lines for example this does same thing but for all lines in model space
 
Code: [Select]

         [CommandMethod("AllLines2")]
        public void seletcAllLines2()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelBtr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                    foreach (ObjectId objId in modelBtr)
                    {
                        if (objId.ObjectClass == RXClass.GetClass(typeof(Line)))
                        {
                            Line myLine = (Line)trans.GetObject(objId, OpenMode.ForRead);
                            ed.WriteMessage("\n" + myLine.StartPoint.ToString() + myLine.EndPoint.ToString());
                        }
                    }
                   
                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                }
            }
        }

waterharbin

  • Guest
Re: How to select all the lines without drag the cursor?
« Reply #2 on: August 13, 2011, 04:59:18 AM »
Hello,Jeff.Thank you very much.
But I get an error in your second method.It's "Autodesk.AutoCAD.DatabaseServices.ObjectId" does not contain "ObjectClass,....,".And the complier shows an tip "(are you missing a using directive or an assembly reference?)".So,is there any other reference to add besides the acdbmgd.dll and acmd.dll?Or I need to add other namespace by using keyword?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to select all the lines without drag the cursor?
« Reply #3 on: August 13, 2011, 06:16:03 AM »
Hi,

The ObjectId.ObjectClass property is quite new (A2010 or A2011 SDK), prior this property was add to the AutoCAD .NET API, the DBObject type test can be done opening the object and trying to cast it with the 'as' keyword. If the cast fail, the onject is null.

Try to replace the foreach statement wit this one:
Code: [Select]
foreach (ObjectId objId in modelBtr)
{
Line myLine = trans.GetObject(objId, OpenMode.ForRead) as Line;
if (myLine != null)
{
ed.WriteMessage("\n" + myLine.StartPoint.ToString() + myLine.EndPoint.ToString());
}
}
Speaking English as a French Frog

waterharbin

  • Guest
Re: How to select all the lines without drag the cursor?
« Reply #4 on: August 13, 2011, 10:49:39 AM »
Thank you,gile.Your code works fine on my AutoCAD 2008.And thank Jeff too.And thank all people who are willing to help others.Nice to meet you  here.