Author Topic: Create ordinate dimension  (Read 733 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 ordinate dimension
« on: January 03, 2013, 01:13:35 PM »
   http://adndevblog.typepad.com/autocad/2012/09/ordinate-dimension-text-is-incorrect-in-rotated-ucs.html   
   By Philippe Leefsma (translated on C#)]
Code: [Select]
       [CommandMethod("CreateOrdinateDimension","cdom", CommandFlags.Modal | CommandFlags.UsePickSet)]
        public static void testOrdinateDimension()
        {
            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())
            {
                PromptPointOptions ppo = new PromptPointOptions("\n  >>  Select point for ordinate dimension >>");

                PromptPointResult ppr = ed.GetPoint(ppo);

                if (ppr.Status != PromptStatus.OK) return;

                double gap = db.Dimtxt * 2;

               Point3d  pt = ppr.Value;

               Point3d tpt = new Point3d(pt.X, pt.Y + gap, pt.Z);

               Matrix3d mx = ed.CurrentUserCoordinateSystem.Inverse();

               Vector3d mXPrev = Vector3d.XAxis;
                // build custom dimension text
               string dstr = string.Format("X = {0:f3}\\PY = {1:f3}", pt.X, pt.Y);// set precision to 3 decimals {:f3}

               OrdinateDimension odim = new OrdinateDimension(false, pt, tpt, dstr, db.Dimstyle);

               double ohrot = odim.HorizontalRotation;
               // get the old Xaxis and transform it to current plane
               Matrix3d mMat = Matrix3d.PlaneToWorld(odim.Normal); //ECS

               mXPrev.TransformBy(mMat); //get the ECS xaxis in the world coordinates

               mXPrev.TransformBy(mx); //transform current xaxis to plane defined by mx

               // transform the dimension entity by the given transformation matrix

               odim.TransformBy(mx);

               // get the xAxis after transformation of the dimension entity

               mMat=   Matrix3d.WorldToPlane(odim.Normal);

               // transform the old axis to the current ECS.

               mXPrev.TransformBy(mMat);

                  // get the angle of the old xaxis with respect to the current xaxis
               
                 double newhrot = - Math.Atan2(mXPrev.X, mXPrev.Y);

                 odim.HorizontalRotation = ohrot + newhrot;

               postToModelSpace(odim);

               tr.AddNewlyCreatedDBObject(odim, true);
   
                    tr.Commit();
                }
            }


        static void postToModelSpace(Entity ent)
        {

            BlockTableRecord btr;

            Database db = HostApplicationServices.WorkingDatabase;

            btr = SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject(OpenMode.ForWrite) as BlockTableRecord;

            btr.AppendEntity(ent);

        }
« Last Edit: January 11, 2013, 10:41:17 AM by fixo »