Author Topic: Create field by picked point coordinate  (Read 646 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
Create field by picked point coordinate
« on: July 21, 2012, 09:11:34 AM »
 
Code: [Select]
       [CommandMethod("FieldPointLocation", "fpl", CommandFlags.Redraw | CommandFlags.Modal)]
        public static void SetField()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            // Set snap to whatever you need:
            //  Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 1);
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Point3d pt = ed.GetPoint("\nPick field location: ").Value;
                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("lastpoint", pt);
                Field fieldobj = new Field();
                fieldobj.EvaluationOption = FieldEvaluationOptions.Automatic;
                fieldobj.SetFieldCode("%<\\AcVar lastpoint \\f \"%lu2%pt3%ls59%pr3\">%");// decimal, separator semicolon,presicion 3
                //fieldobj.SetFieldCode("%<\\AcVar lastpoint \\f \"%lu4%pt3\">%");// architectural, separator comma, precision 1/8"
                MText mtextobj = new MText();
                mtextobj.SetDatabaseDefaults();
                mtextobj.SetField("Text", fieldobj);
                fieldobj.Evaluate(0, db);
                mtextobj.Location = pt;
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                btr.AppendEntity(mtextobj);
                tr.AddNewlyCreatedDBObject(mtextobj, true);
                tr.Commit();
            }
        }