当前位置: 首页 > article >正文

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

效果如下:

(使用方法:命令行输入 “netload” 加载此dll插件,然后输入“xx”运行,选择文件夹即可。)

部分代码如下:


#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
    {
    }
}


http://www.kler.cn/a/441860.html

相关文章:

  • ElasticSearch DSL查询之高亮显示
  • 关于机器学习的一份总结
  • 【Leetcode 热题 100】45. 跳跃游戏 II
  • Spring Boot 3.3.4 升级导致 Logback 之前回滚策略配置不兼容问题解决
  • Apache SeaTunnel 2.3.9 正式发布:多项新特性与优化全面提升数据集成能力
  • FPGA 21 ,深入理解 Verilog 中的基数,以及二进制数与十进制数之间的关系( Verilog中的基数 )
  • dify智能体系列:selenium有啥好玩的?
  • 如何为IntelliJ IDEA配置JVM参数
  • springboot中Controller内文件上传到本地以及阿里云
  • 【Prompt Engineering】2.迭代优化
  • 【附源码】Electron Windows桌面壁纸开发中的 CommonJS 和 ES Module 引入问题以及 Webpack 如何处理这种兼容
  • 判题机的开发(代码沙箱、三种模式、工厂模式、策略模式优化、代理模式)
  • 单片机:实现蜂鸣器数码管的显示(附带源码)
  • Numpy基本介绍
  • Leetcode打卡:形成目标字符串需要的最少字符串数II
  • 如何在 Linux 服务器上部署 Pydio Cells 教程
  • STM32F407ZGT6-UCOSIII笔记7:优先级反转现象
  • 【图形渲染】【Unity Shader】【Nvidia CG】有用的参考资料链接
  • Composer指定php版本执行(windows)
  • git branch -r(--remotes )显示你本地仓库知道的所有 远程分支 的列表
  • Hadoop是什么?Hadoop介绍
  • workman服务端开发模式-应用开发-总架构逻辑说明
  • 虚拟现实辅助工程技术在航空领域的应用
  • git pull 和 git pull --rebase 区别
  • 初见react
  • 搭建springmvc项目