Author Topic: A reliable way to select the entities with ed.Getentity method  (Read 968 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
A reliable way to select the entities with ed.Getentity method
« on: January 11, 2013, 06:31:39 AM »
 
Code: [Select]
              public static ErrorStatus fixoGetEntity(Transaction tr, Editor ed, RXClass rx, string msg, out ObjectId id)
        {
            ErrorStatus es;
            Entity ent;
            id = ObjectId.Null;
            PromptEntityOptions peo = new PromptEntityOptions(msg);
            peo.SetRejectMessage("\nYou're missing, try again >>");
            peo.AddAllowedClass(typeof(Entity), false);
            PromptEntityResult res;
            res = ed.GetEntity(peo);
            if (res.Status != PromptStatus.OK)
                es = ErrorStatus.PointNotOnEntity;
            id = res.ObjectId;
            if (id == ObjectId.Null)
                es = ErrorStatus.NullObjectId;
            ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity;
            if (ent.GetRXClass() != rx)
            {
                ed.WriteMessage("\n{0}Must be a \"{0}\" type of only!",rx.DxfName);
                es = ErrorStatus.NotThatKindOfClass;
            }
            if (ent == null)
                es = ErrorStatus.NotAnEntity;
            else es = ErrorStatus.OK;
            return es;
        }
         

        // Use function above this way
        [CommandMethod("tef")]
        public static void testSelectFunction()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            ObjectId id = ObjectId.Null;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
               if (fixoGetEntity(tr, ed, RXClass.GetClass(typeof(MText)), "\nSelect MTEXT to edit", out id) == ErrorStatus.OK)
                {
                    ed.WriteMessage("\nObjectId: {0}\n", id);
                    ed.WriteMessage("\nEntity Name: {0}\n", id.ObjectClass.DxfName);
                    // get entity by direct cast
                    Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                    ent.ColorIndex = 121;// do whatever you need with entity here
                    tr.Commit();
                }
            }
        }
« Last Edit: February 06, 2013, 08:52:30 AM by fixo »