Cad二次开发绘图1

获取图形(线、圆、弧、多段线信息json)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
[assembly:CommandClass(typeof(CadAndJson.Class1))]
namespace CadAndJson
{
    public class Class1
    {
        [CommandMethod("SelectAllEnt")]
        public void SelectAllEnt()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            using (doc.LockDocument())
            {
                Database db = doc.Database;
                Editor ed = doc.Editor;
                //直线
                TypedValue libeValue = new TypedValue((int)DxfCode.Start, "Line");
                TypedValue[] LilineValues = new TypedValue[] { libeValue };
                SelectionFilter lineFilter = new SelectionFilter(LilineValues);
                PromptSelectionResult psrL = ed.SelectAll(lineFilter);


                var myLines = new List<MyLine>();
                if (psrL.Status == PromptStatus.OK)
                {
                    SelectionSet set = psrL.Value;
                    ObjectId[] ids = set.GetObjectIds();
                    Entity[] ents = this.GetEntity(ids, db);
                    for (int i = 0; i < ents.Length; i++)
                    {
                        Line line = (Line)ents[i];
                        var myLine = new MyLine()
                        {
                            StartX = line.StartPoint.X,
                            StartY = line.StartPoint.Y,
                            EndX = line.EndPoint.X,
                            EndY = line.EndPoint.Y,
                            ColorIndex = line.ColorIndex,
                            Layer = line.Layer,
                        };
                        myLines.Add(myLine);
                    }
                }
                string jsonL2 = JsonConvert.SerializeObject(myLines, Formatting.Indented);
                string pathL2 = "Line.json";
                File.WriteAllText(pathL2, jsonL2);

                //圆
                TypedValue cirValue = new TypedValue((int)DxfCode.Start, "Circle");
                TypedValue[] cirValues = new TypedValue[] { cirValue };
                SelectionFilter cirFilter = new SelectionFilter(cirValues);

                PromptSelectionResult psrC = ed.SelectAll(cirFilter);
                var myCircles = new List<MyCircle>();



                if (psrC.Status == PromptStatus.OK)
                {
                    SelectionSet set = psrC.Value;
                    ObjectId[] objs = set.GetObjectIds();
                    Entity[] ents = this.GetEntity(objs, db);
                    for (int i = 0; i < ents.Length; i++)
                    {
                        Circle cir = (Circle)ents[i];
                        var myCircle = new MyCircle()
                        {
                            Radius = cir.Radius,
                            CirCenterX = cir.Center.X,
                            CirCenterY = cir.Center.Y,
                            CirIndex = cir.ColorIndex,
                            CirLayer = cir.Layer,
                        };
                        myCircles.Add(myCircle);
                    }
                }

                string pathC = "Cir.json";
                string jsonC = JsonConvert.SerializeObject(myCircles, Formatting.Indented);
                File.WriteAllText(pathC, jsonC);

                //圆弧
                TypedValue valueA = new TypedValue((int)DxfCode.Start, "Arc");
                TypedValue[] valuesA = new TypedValue[] { valueA };
                SelectionFilter filterA = new SelectionFilter(valuesA);
                var myArcs = new List<MyArc>();

                PromptSelectionResult psrA = ed.SelectAll(filterA);
                if (psrA.Status == PromptStatus.OK)
                {
                    SelectionSet set = psrA.Value;
                    ObjectId[] objs = set.GetObjectIds();
                    Entity[] ents = this.GetEntity(objs, db);
                    for (int i = 0; i < ents.Length; i++)
                    {
                        Arc arc = (Arc)ents[i];
                        var myArc = new MyArc()
                        {
                            ArcRadius = arc.Radius,
                            ArcCenterX = arc.Center.X,
                            ArcCenterY = arc.Center.Y,
                            StartAngle = arc.StartAngle,
                            EndAngle = arc.EndAngle,
                            ArcIndex = arc.ColorIndex,
                            ArcLayer = arc.Layer,
                        };
                        myArcs.Add(myArc);
                    }
                }
                string pathA = "Arc.json";
                string jsonA = JsonConvert.SerializeObject(myArcs, Formatting.Indented);
                File.WriteAllText(pathA, jsonA);

                //多段线
                TypedValue pLValue = new TypedValue((int)DxfCode.Start, "LWPOLYLINE");
                TypedValue[] pLValues = new TypedValue[] { pLValue };
                SelectionFilter pLFilter = new SelectionFilter(pLValues);
                PromptSelectionResult pLpsr = ed.SelectAll(pLFilter);
                var myPolyLines = new List<MyPolyline>();


                List<string> pLayer = new List<string>();

                if (pLpsr.Status == PromptStatus.OK)
                {
                    SelectionSet set = pLpsr.Value;
                    ObjectId[] objIds = set.GetObjectIds();

                    Entity[] ents = this.GetEntity(objIds, db);

                    for (int i = 0; i < ents.Length; i++)
                    {
                        List<double> vertaxX = new List<double>();
                        List<double> vertaxY = new List<double>();
                        Polyline pl = (Polyline)ents[i];
                        for (int j = 0; j < pl.EndParam; j++)
                        {
                            vertaxX.Add(pl.GetPoint3dAt(j).X);
                            vertaxY.Add(pl.GetPoint3dAt(j).Y);
                        }
                        var myPolyline = new MyPolyline()
                        {
                            ColorIndex = pl.ColorIndex,
                            Layer = pl.Layer,
                            VertecX = vertaxX,
                            VertecY = vertaxY,
                            EndPointX = pl.EndPoint.X,
                            EndPointY = pl.EndPoint.Y,
                        };


                        myPolyLines.Add(myPolyline);

                    }
                }
                //序列化
                string pathPl = "pline.json";
                string jsonPl = JsonConvert.SerializeObject(myPolyLines, Formatting.Indented);
                File.WriteAllText(pathPl, jsonPl);

            }
        }
     }
  }
    class MyLine
    {
        public double StartX { get; set; }
        public double StartY { get; set; }
        public double EndX { get; set; }
        public double EndY { get; set; }
        public int ColorIndex { get; set; }
        public string Layer { get; set; }
    }
    class MyPolyline
    {
        //public List<MyPolylineVerext> Vertexs { get; set; }
        public List<double> VertecX { get; set; }
        public List<double> VertecY { get; set; }
        //public List<MyPOlyLineEndPoint> EndPoint { get; set; }

        public double EndPointX { get; set; }
        public double EndPointY { get; set; }

        public int ColorIndex { get; set; }
        public string Layer { get; set; }
    }


    public class MyCircle
    {
        public double Radius { get; set; }
        public double CirCenterX { get; set; }
        public double CirCenterY { get; set; }
        public string CirLayer { get; set; }
        public int CirIndex { get; set; }
    }
    public class MyArc
    {
        public double ArcCenterX { get; set; }
        public double ArcCenterY { get; set; }
        public double ArcRadius { get; set; }
        public double StartAngle { get; set; }
        public double EndAngle { get; set; }
        public int ArcIndex { get; set; }
        public string ArcLayer { get; set; }
    }
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周杰伦fans

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值