用objectARX实现了一个复杂实体的Jig

点选一点,动态标注改点坐标。

用objectARX实现了一个复杂实体的Jig..

//坐标标注JIg类
    class ZBJig : DrawJig
    {
        public readonly static double TextHeight = 8;
        public readonly static double Len = 80;
 
        private Point3d mLocation;
        private string strZb = "";
        private DBText mText;
        private Polyline mPline;
        public void Bzb()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            PromptPointOptions opt = new PromptPointOptions("\n选择标注点:");
            PromptPointResult res = ed.GetPoint(opt);
            if (res.Status != PromptStatus.OK)
            {
                return;
            }
            Point3d FirstPoint = res.Value;
            strZb = string.Format("X={0:0.0000},Y={1:0.0000}", FirstPoint.X, FirstPoint.Y);
            mText = new DBText();
           // mText.TextStyleId = TextStyles.AddTextStyle("hztxt");
            mText.Position = FirstPoint;
            mText.TextString = strZb;
            mText.Height = TextHeight;
            mText.WidthFactor = 0.7;
            mText.Rotation = 0.00;
 
            mPline = new Polyline();
            mPline.AddVertexAt(0, new Point2d(FirstPoint.X, FirstPoint.Y), 0, 0, 0);
            mPline.AddVertexAt(1, new Point2d(FirstPoint.X, FirstPoint.Y), 0, 0, 0);
            mPline.AddVertexAt(2, new Point2d(FirstPoint.X + Len, FirstPoint.Y), 0, 0, 0);
 
            PromptResult res2 = ed.Drag(this);
            if (res2.Status == PromptStatus.OK)
            {
                AppendEntity();
            }
        }
 
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigOpts = new JigPromptPointOptions();
            jigOpts.UserInputControls =
                UserInputControls.Accept3dCoordinates |
                UserInputControls.NoZeroResponseAccepted |
                UserInputControls.NoNegativeResponseAccepted;
            jigOpts.Message = "\n标注位置:";
            PromptPointResult res = prompts.AcquirePoint(jigOpts);
            Point3d positionTemp = res.Value;
            if (positionTemp != mLocation)
            {
                mLocation = positionTemp;
            }
            else
                return SamplerStatus.NoChange;
            if (res.Status == PromptStatus.Cancel)
                return SamplerStatus.Cancel;
            else
                return SamplerStatus.OK;
        }
 
        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
            try
            {
                Update();
                draw.Geometry.Draw(mPline);
                draw.Geometry.Draw(mText);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }
 
        private void Update()
        {
            mPline.SetPointAt(1, new Point2d(mLocation.X, mLocation.Y));
            mPline.SetPointAt(2, new Point2d(mLocation.X + Len, mLocation.Y));
            mText.Position = mLocation;
        }
 
        //在当前图形文件中添加实体
        private void AppendEntity()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);          
                Update();
                btr.AppendEntity(mText);
                tr.AddNewlyCreatedDBObject(mText, true);
                btr.AppendEntity(mPline);
                tr.AddNewlyCreatedDBObject(mPline, true);
                tr.Commit();
            }
        }
    }

增加一个AutoCAD命令

      [CommandMethod("Bzb")]
        static public void Cmd5() // This method can have any name
        {
            ZBJig bzb1 = new ZBJig();
            bzb1.Bzb();
        }

实现效果如下:
用objectARX实现了一个复杂实体的Jig_第1张图片

你可能感兴趣的:(ObjectARX,CAD)