CAD xy坐标标注(跟随鼠标位置实时移动)——C#插件实现

效果如下:

(使用方法:命令行输入 “netload” 加载此dll插件,然后输入“zbbz”运行,选择文件夹即可。支持字体大小变化,输入“zbbd”可设置坐标字体变大或缩小的倍数)

部分代码如下:


#if DEBUG
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.GraphicsInterface;
using IFoxCAD.Cad;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Shapes;


namespace IfoxDemo
{
    public class Class1
    {
        [CommandMethod("XX")]
        public void Jigworlddraw()
        {
      
            //Debugger.Break();
            double dist = 5;//横线的长度
            double textHeight = 1;
            double vertDist = textHeight * 1.2;//坐标标注距离横向的长度
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            PromptPointResult ppr = ed.GetPoint("\n请指定坐标点:\n");
            if (ppr.Status != PromptStatus.OK)
            {
                return;
            }
            Point3d pointStart = ppr.Value;//获取第一个点
            var m_Polyline = new Polyline();//定义线
            m_Polyline.AddVertexAt(0, new Point2d(pointStart.X, pointStart.Y), 0, 0, 0);
            double mpwX = pointStart.X;
            double mpwY= pointStart.Y;
            m_Polyline.AddVertexAt(1, new Point2d(mpwX, mpwY), 0, 0, 0);
            m_Polyline.AddVertexAt(2, new Point2d(mpwX, mpwY), 0, 0, 0);
            DBText textx = new DBText()//定义x坐标
            {
                Height = textHeight,
                TextString = Math.Round(pointStart.X, 3).ToString(),
                //Position = new Point3d(mpwX + dist / 2, mpwY + vertDist, 0)
                
            };
            DBText texty = new DBText()//定义y坐标
            {
                Height = textHeight,
                TextString = Math.Round(pointStart.Y, 3).ToString(),
                //Position = new Point3d(mpwX + dist / 2, mpwY - vertDist, 0)
            };
            Entity[] allentity = new Entity[3] { m_Polyline ,textx,texty};
            using var jig = new JigEx((mpw, queue) =>
            {
                mpwX = mpw.X;
                mpwY = mpw.Y;
                m_Polyline.SetPointAt(1,new Point2d (mpw.X,mpw.Y));
                
                if (mpw.X >= pointStart.X)
                {
                    m_Polyline.SetPointAt(2, new Point2d(mpw.X + dist, mpw.Y));
                    textx.Position = new Point3d(mpwX + dist / 2, mpwY + vertDist, 0);
                    texty.Position = new Point3d(mpwX + dist / 2, mpwY - vertDist, 0);
                    textx.HorizontalMode = TextHorizontalMode.TextMid;
                    textx.VerticalMode = TextVerticalMode.TextVerticalMid;
                    texty.HorizontalMode = TextHorizontalMode.TextMid;
                    texty.VerticalMode = TextVerticalMode.TextVerticalMid;
                    textx.AlignmentPoint = textx.Position;
                    texty.AlignmentPoint = texty.Position;
                    textx.AdjustAlignment(db);
                    texty.AdjustAlignment(db);
                }
                else
                {
                    m_Polyline.SetPointAt(2, new Point2d(mpw.X - dist, mpw.Y));
                    textx.Position = new Point3d(mpw.X - dist / 2, mpw.Y + vertDist, 0);
                    texty.Position = new Point3d(mpw.X - dist / 2, mpw.Y - vertDist, 0);
                    textx.HorizontalMode = TextHorizontalMode.TextMid;
                    textx.VerticalMode = TextVerticalMode.TextVerticalMid;
                    texty.HorizontalMode = TextHorizontalMode.TextMid;
                    texty.VerticalMode = TextVerticalMode.TextVerticalMid;
                    textx.AlignmentPoint = textx.Position;
                    texty.AlignmentPoint = texty.Position;
                    textx.AdjustAlignment(db);
                    texty.AdjustAlignment(db);
                }
            });
            jig.DatabaseEntityDraw(worlddraw => worlddraw.Geometry.Draw(allentity));
            jig.SetOptions("\n选注记点");
            var r1 = jig.Drag();
            if (r1.Status != PromptStatus.OK)
                return;
            using var tr = new DBTrans();
            tr.CurrentSpace.AddEntity(allentity);
        }
        [CommandMethod("XXX")]
        public void Jig()
        {
            
            double dist = 3;//横线的长度
            double textHeight = 1;
            double vertDist = textHeight*1.2;//坐标标注距离横向的长度
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            PromptPointResult ppr = ed.GetPoint("\n请指定坐标点:\n");
            if (ppr.Status != PromptStatus.OK)
            {
                return;
            }
            Point3d pointStart = ppr.Value;
            using var jig = new JigEx((mpw, queue) =>
             {
                 var m_Polyline = new Polyline();
                 m_Polyline.AddVertexAt(0, new Point2d(pointStart.X, pointStart.Y), 0, 0, 0);
                 // 初始时,第二和第三个点设置为与第一个点相同(将在Sampler中更新)
                 m_Polyline.AddVertexAt(1, new Point2d(mpw.X+0.1, mpw.Y+0.1), 0, 0, 0);
                 DBText textx = new DBText() 
                 { Height = textHeight, TextString = Math.Round(pointStart.X, 3).ToString(),
                     //HorizontalMode = TextHorizontalMode.TextMid,VerticalMode = TextVerticalMode.TextVerticalMid 
                 };
                 DBText texty = new DBText() 
                 { Height = textHeight, TextString = Math.Round(pointStart.Y, 3).ToString(),
                     //HorizontalMode = TextHorizontalMode.TextMid,VerticalMode = TextVerticalMode.TextVerticalMid 
                 };
                // textx.AdjustAlignment(db);
                 //texty.AdjustAlignment(db);
                 if (mpw.X>= pointStart.X)
                 {
                      m_Polyline.AddVertexAt(2, new Point2d(mpw.X+ dist, mpw.Y+0.1), 0, 0, 0);
                     textx.Position = new Point3d(mpw.X + dist / 2, mpw.Y + vertDist, 0);
                     texty.Position = new Point3d(mpw.X + dist / 2, mpw.Y - vertDist, 0);
                     textx.HorizontalMode = TextHorizontalMode.TextMid;
                     textx.VerticalMode = TextVerticalMode.TextVerticalMid;
                     texty.HorizontalMode = TextHorizontalMode.TextMid;
                     texty.VerticalMode = TextVerticalMode.TextVerticalMid;
                     textx.AlignmentPoint = textx.Position;
                     texty.AlignmentPoint = texty.Position;
                     textx.AdjustAlignment(db);
                     texty.AdjustAlignment(db);
                 }
                 else
                 {
                     m_Polyline.AddVertexAt(2, new Point2d(mpw.X - dist, mpw.Y+0.1), 0, 0, 0);
                     textx.Position = new Point3d(mpw.X - dist / 2, mpw.Y + vertDist, 0);
                     texty.Position = new Point3d(mpw.X - dist / 2, mpw.Y - vertDist, 0);
      
                     textx.HorizontalMode = TextHorizontalMode.TextMid;
                     textx.VerticalMode = TextVerticalMode.TextVerticalMid;
                     texty.HorizontalMode = TextHorizontalMode.TextMid;
                     texty.VerticalMode = TextVerticalMode.TextVerticalMid;
                     textx.AlignmentPoint = textx.Position;
                     texty.AlignmentPoint = texty.Position;
                     textx.AdjustAlignment(db);
                     texty.AdjustAlignment(db);
                 }
                 m_Polyline.Closed = false; // 确保多段线不是闭合的
                 queue.Enqueue(m_Polyline);
                 queue.Enqueue(textx);
                 queue.Enqueue(texty);
             });
             jig.SetOptions("\n选下一个点");
             var r1 = jig.Drag();
             if (r1.Status != PromptStatus.OK)
                 return;
             using var tr = new DBTrans();
             tr.CurrentSpace.AddEntity(jig.Entities);
        }
    }
}
#endif

global using System;
global using IfoxDemo;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;
global using Autodesk.AutoCAD.ApplicationServices;
global using Autodesk.AutoCAD.EditorInput;
global using Autodesk.AutoCAD.Runtime;
global using Autodesk.AutoCAD.Geometry;
global using Autodesk.AutoCAD.DatabaseServices;
global using IFoxCAD.Cad;
global using Application = Autodesk.AutoCAD.ApplicationServices.Application;
global using Polyline = Autodesk.AutoCAD.DatabaseServices.Polyline;
namespace IfoxDemo
{
    public class GlobalUsing
    {
    }
}

(CAD二次开发插件制作)见↓↓↓↓

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值