Code Red > .NET

.NET SELECTION Routines

(1/4) > >>

Kerry:
LIBRARY THREAD for  AutoCAD SELECTIONS
 Members are encouraged to post any functions, methods, snips regarding
AutoCAD SELECTIONSS in .NET : C# ,  VB , F# , Python , etc

Feel free to include comments, descriptive notes, limitations,  and images to document your post.

Please post questions in a regular thread.

MickD:
just to get the ball rolling, here's an example of iterating over the modelspace block table record to retrieve all Solid3d and Line objects, change as per your required type.
I'll post an example using selection sets and filters as well shortly.

[added kwb] This is a Boo example
Boo is an object oriented statically typed programming language for the Common Language Infrastructure with a python inspired syntax.


--- Code: --- public def SelectAll3dSolidsAndLines(prompt as string) as ObjectIdCollection:
db = HostApplicationServices.WorkingDatabase
ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
ids = ObjectIdCollection()
using tr = db.TransactionManager.StartTransaction():
//roll over the block table and get useable entity types:
bt = (tr.GetObject(db.BlockTableId, OpenMode.ForRead, false) as BlockTable)
if bt is null:
ed.WriteMessage('{0}Catastrophic error: Failed to open the BlockTable!', Environment.NewLine)
return

modelspace = (tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord)
if modelspace is null:
ed.WriteMessage('{0}Catastrophic error: Failed to open the modelspace btr!', Environment.NewLine)
return
for msid as ObjectId in modelspace:
l = (tr.GetObject(msid, OpenMode.ForRead) as Line)
if l is not null:
ids.Add(msid)

s = (tr.GetObject(msid, OpenMode.ForRead) as Solid3d)
if s is not null:
ids.Add(msid)
tr.Commit()
return ids
--- End code ---

DaveWish:
That's beautiful code. Thanks

MickD:
I can't take all the credit, I pinched most of it from one of GlennR's posts  (thanks Glenn) and changed it to Boo ;)

Boo is a nice language, it's easy to read and use just like Python, probably even a little better in some respects.

Here's a selection set example as well although I was having some issues with Boo and enums. I have a work around though and have made a post at the Boo mailing lists to see what the problem is so hopefully it will be a short term fix. Basically I just use the actual enum assigned value for the comparison and this works fine.

Here'tis

--- Code: ---// Autocad enums, we need to fudge these as Boo doesn't handle enums to well just yet :(
// these values are taken from the C++ ARX documentation on PromptStatus enums
// eNone 5000 No result
// eNormal 5100 Request succeeded
// eError -5001 Nonspecific error
// eCancel -5002 User canceled the request with a CTRL-C
// eRejected -5003 AutoCAD rejected the request as invalid
// eFailed -5004 Link failure, possibly with the LISP interpreter
// eKeyword -5005 Keyword returned from a "get" routine
// eDirect -5999 Passed to endGetPoint() if the getpoint was nested within
//                      another geometric value prompt (such as for angle), and the response
//                      entered was such a value rather than a point

public static def Select3dSolids(prompt as string) as (ObjectId):
ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
// set up some prompt options:
pso = PromptSelectionOptions()
pso.MessageForAdding = prompt
pso.AllowDuplicates = false
// create an sset filter to only grab 3d solids, an array of typed values
ssFilter = SelectionFilter((TypedValue(0, "3DSOLID"),)) // notice the ','!
// get the sset:
res as PromptSelectionResult = ed.GetSelection(pso, ssFilter)
if res.Status != 5100 or res.Status == -5002: // OK and Cancel enums.
return null
return res.Value.GetObjectIds()

--- End code ---

fixo:
    ==  Continuous copy  ==


--- Code: ---//inspired by Donnia Tabor-Hanson's (CadMama) continuous copy lisp 'cc.lsp'
[CommandMethod("CC")]
        public static void ContinuousCopy()

        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = HostApplicationServices.WorkingDatabase;

            Editor ed = doc.Editor;

            Transaction tr = db.TransactionManager.StartTransaction();

            BlockTableRecord btr =

                   (BlockTableRecord)tr.GetObject(

                   db.CurrentSpaceId,

                   OpenMode.ForWrite,

                   false);

            using (tr)
            {
                PromptEntityOptions peo = new PromptEntityOptions("\nSelect Object >>");

                peo.SetRejectMessage("\nNothing selected >>");

                PromptEntityResult res;

                res = ed.GetEntity(peo);

                if (res.Status != PromptStatus.OK)

                    return;

                Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);

                if (ent == null)

                    return;

                Point3d pt = res.PickedPoint;

                PromptAngleOptions pao =

                            new PromptAngleOptions("\nSelect angle: ");

                PromptDoubleResult ares;

                ares = ed.GetAngle(pao);

                if (ares.Status != PromptStatus.OK)

                    return;

                double ang = ares.Value;

                PromptDistanceOptions pdo =

    new PromptDistanceOptions("\nEnter a distance >>");

                pdo.AllowNone = true;

                pdo.UseDefaultValue = true;

                pdo.DefaultValue = 12.0;

                pdo.Only2d = true;

                PromptDoubleResult dres;

                dres = ed.GetDistance(pdo);

                if (dres.Status != PromptStatus.OK)

                    return;

                double dist = dres.Value;

                double step = dist;

                PromptKeywordOptions pko = new PromptKeywordOptions("\nCopy or Break out? [Copy/Break]:", "Copy Break");

                pko.AllowNone = true;

                pko.Keywords.Default = "Copy";

                PromptResult kres = ed.GetKeywords(pko);

                ed.WriteMessage("\n" + kres.StringResult);

                while (kres.StringResult == "Copy")
                {
                    Entity cent =(Entity) ent.Clone() as Entity;

                    Point3d topt = PolarPoint(pt, ang, dist);

                    Matrix3d mtx = Matrix3d.Displacement(pt.GetVectorTo(topt));

                    cent.TransformBy(mtx);

                    btr.AppendEntity(cent);

                    tr.AddNewlyCreatedDBObject(cent, true);

                    db.TransactionManager.QueueForGraphicsFlush();

                    dist = dist+step;

                    kres = ed.GetKeywords(pko);

                    if (kres.StringResult != "Copy") break;
                }

                tr.Commit();
            }
        }

        // by Tony Tanzillo
        public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
        {
            return new Point3d(

            basepoint.X + (distance * Math.Cos(angle)),

            basepoint.Y + (distance * Math.Sin(angle)),

            basepoint.Z);
        }
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version