当前位置:   article > 正文

CAD二次开发(c#)利用选择集获取标注尺寸_c# 获取cad标注的值

c# 获取cad标注的值

1.目的

本文的目的是利用C#中选择集GetSelection函数得到CAD中标注尺寸
  • 1

2.实现代码

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoDrawingSF
{
    public class Drawing
    {
   
        [CommandMethod("AutoDrwing")]
        public void AutoDrwing()
        {

            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //保存图形信息到Excel文件
            string fileName = ed.OpenSaveDialog(db);
            if (fileName == "") return;

            TypedValue[] values = new TypedValue[]
            {   
                //设定为标注图元             
                new TypedValue((int)DxfCode.Start,"DIMENSION"),
                //new TypedValue((int)DxfCode.Start,"CIRCLE"),
            };
            //过滤标注图元
            SelectionFilter filter = new SelectionFilter(values);
            PromptSelectionResult psr = ed.GetSelection(filter);
            List<string> strX = new List<string>();
            List<string> strY = new List<string>();
            //List<ObjectId> ids = new List<ObjectId>();
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet sSet = psr.Value;
                ObjectId[] ids = sSet.GetObjectIds();
                //如果ids中没有数据则返回;
                if (ids.Length == 0) return;

                //开启事务处理
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    for (int i = 0; i < ids.Length; i++)
                    {
                        Entity ent = (Entity)ids[i].GetObject(OpenMode.ForRead);

                        //计算两个点X轴方向差值
                        double DistanceX = ((RotatedDimension)ent).XLine1Point.X - ((RotatedDimension)ent).XLine2Point.X;
                        //计算两个点Y轴方向差值
                        double DistanceY = ((RotatedDimension)ent).XLine1Point.Y - ((RotatedDimension)ent).XLine2Point.Y;
                        //判读尺寸是X轴方向还是Y轴方向,两点坐标X轴方向差值为0,此标注尺寸为Y轴方向,反之为X轴方向;
                        if (DistanceX == 0.0)
                        {
                            //获取标注尺寸长度,Y轴方向尺寸
                            double Distan = ((RotatedDimension)ent).XLine1Point.GetDistanceBetweenTwoPoint(((RotatedDimension)ent).XLine2Point);
                            strY.Add(Distan.ToString());
                        }
                        else if (DistanceY == 0)
                        {
                            //获取标注尺寸长度
                            double Distan = ((RotatedDimension)ent).XLine1Point.GetDistanceBetweenTwoPoint(((RotatedDimension)ent).XLine2Point);
                            strX.Add(Distan.ToString());
                        }
                    }
                    //关闭事务处理
                    trans.Commit();
                }
            }
           }
          }
         }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

3.CAD常用图元类型

c#选择集中不仅能过滤标注图元,还能用来过滤直线(LIne),圆(circle),圆弧(Arc)等等,CAD中常见图元详见下图:
  • 1

在这里插入图片描述图元类型前面AcDb在实际程序开发中,不可添加到程序中;

4.其他相关资料链接

1)选择集Dxf规则
https://blog.csdn.net/weixin_42339460/article/details/80662972?ops_request_misc=&request_id=&biz_id=102&utm_term=CAD%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91(.net)%E9%80%89%E6%8B%A9%E9%9B%86DXF&utm_medium=distribute.pc_search_result.none-task-blog-2blogsobaiduweb~default-3-80662972.pc_v2_rank_blog_default&spm=1018.2226.3001.4450
2)选择集讲解
https://blog.csdn.net/yzk1062913581/article/details/105537412?ops_request_misc=&request_id=&biz_id=102&utm_term=CAD%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91(.net)%E9%80%89%E6%8B%A9%E9%9B%86&utm_medium=distribute.pc_search_result.none-task-blog-2blogsobaiduweb~default-0-105537412.pc_v2_rank_blog_default&spm=1018.2226.3001.4450

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/111790
推荐阅读
相关标签
  

闽ICP备14008679号