Cad二次开发绘图2

该代码示例展示了如何使用C#从JSON文件中读取数据,然后在AutoCAD环境中创建直线、圆、弧和多段线。程序首先解析JSON数据,提取几何信息(如坐标和颜色索引),然后根据这些信息实例化相应的图形对象并添加到当前图层。
摘要由CSDN通过智能技术生成

绘制图形(线、圆、弧、多段线信息)


        [CommandMethod("DrawEnt")]
        public void DrawEnt()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            using (doc.LockDocument())
            {
                Database db = doc.Database;
                Editor ed = doc.Editor;
                //直线
                string pathL = "Line.json";
                string dataL = File.ReadAllText(pathL);
                List<MyLine> myLines = JsonConvert.DeserializeObject<List<MyLine>>(dataL);
                Line line = new Line();

                var startXs = myLines.Select(x => x.StartX).ToList();
                var startYs = myLines.Select(x => x.StartY).ToList();
                var endXs = myLines.Select(x => x.EndX).ToList();
                var endYs = myLines.Select(x => x.EndY).ToList();
                var lineIndex = myLines.Select(x => x.ColorIndex).ToList();
                var lineLayer = myLines.Select(x => x.Layer).ToList();
                //lineLayer.ForEach(x => line.Layer = x);

                if (myLines.Count != 0)
                {
                    for (int i = 0; i < startXs.Count; i++)
                    {
                        line = new Line(new Point3d(startXs[i], startYs[i], 0), new Point3d(endXs[i], endYs[i], 0));
                        this.AddLayer(lineLayer[i], lineIndex[i]);


                        line.Layer = lineLayer[i];
                        line.ColorIndex = lineIndex[i];
                        line.Color = Color.FromColorIndex(ColorMethod.ByLayer, (short)lineIndex[i]);
                        this.AddEntityToModeSpace(line, db);
                    }
                }





                //圆
                string pathCirCle = "Cir.json";
                string dataCirCle = File.ReadAllText(pathCirCle);
                List<MyCircle> myCirCles = JsonConvert.DeserializeObject<List<MyCircle>>(dataCirCle);


                Circle cir = new Circle();

                var cirRadius = myCirCles.Select(x => x.Radius).ToList();
                var cirCenterX = myCirCles.Select(X => X.CirCenterX).ToList();
                var cirCenterY = myCirCles.Select(x => x.CirCenterY).ToList();
                var cirIndex = myCirCles.Select(x => x.CirIndex).ToList();
                var cirLayer = myCirCles.Select(x => x.CirLayer).ToList();
                if (myCirCles.Count != 0)
                {
                    for (int i = 0; i < cirRadius.Count; i++)
                    {
                        cir = new Circle(new Point3d(cirCenterX[i], cirCenterY[i], 0), Vector3d.ZAxis, cirRadius[i]);
                        this.AddLayer(cirLayer[i], cirIndex[i]);
                        cir.Layer = cirLayer[i];
                        cir.ColorIndex = cirIndex[i];


                        this.AddEntityToModeSpace(cir, db);
                    }
                }





                //圆弧

                string pathArc = "Arc.json";
                string dataArc = File.ReadAllText(pathArc);
                List<MyArc> myArcs = JsonConvert.DeserializeObject<List<MyArc>>(dataArc);

                Arc arc = new Arc();

                var arcRadius = myArcs.Select(x => x.ArcRadius).ToList();
                var arcCEnterX = myArcs.Select(x => x.ArcCenterX).ToList();
                var arcCEnterY = myArcs.Select(x => x.ArcCenterY).ToList();
                var startAngle = myArcs.Select(x => x.StartAngle).ToList();
                var endAngle = myArcs.Select(x => x.EndAngle).ToList();
                var arcIndex = myArcs.Select(x => x.ArcIndex).ToList();
                var arcLAyer = myArcs.Select(x => x.ArcLayer).ToList();
                if (myArcs.Count != 0)
                {
                    for (int i = 0; i < arcRadius.Count; i++)
                    {
                        arc = new Arc(new Point3d(arcCEnterX[i], arcCEnterY[i], 0), arcRadius[i], startAngle[i], endAngle[i]);

                        this.AddLayer(arcLAyer[i], arcIndex[i]);

                        arc.Layer = arcLAyer[i];
                        arc.ColorIndex = arcIndex[i];
                        this.AddEntityToModeSpace(arc, db);
                    }
                }




                //多段线
                string pathPl = "pline.json";
                string dataPl = File.ReadAllText(pathPl);
                List<MyPolyline> myPolyLines = JsonConvert.DeserializeObject<List<MyPolyline>>(dataPl);




                var plVertexX = myPolyLines.Select(x => x.VertecX).ToList();
                var plVartexY = myPolyLines.Select(x => x.VertecY).ToList();
                var plEndX = myPolyLines.Select(x => x.EndPointX).ToList();
                var plEndY = myPolyLines.Select(x => x.EndPointY).ToList();
                var plIndex = myPolyLines.Select(x => x.ColorIndex).ToList();
                var plLayer = myPolyLines.Select(x => x.Layer).ToList();


                if (myPolyLines.Count != 0)
                {
                    for (int i = 0; i < myPolyLines.Count; i++)
                    {
                        Polyline pline = new Polyline();
                        for (int j = 0; j < plVertexX[i].Count; j++)
                        {
                            pline.AddVertexAt(j, new Point2d(plVertexX[i][j], plVartexY[i][j]), 0, 0, 0);
                        }
                        int index = (int)(pline.EndParam + 1);
                        pline.AddVertexAt(index, new Point2d(plEndX[i], plEndY[i]), 0, 0, 0);
                        this.AddLayer(plLayer[i], plIndex[i]);
                        pline.Layer = plLayer[i];
                        pline.ColorIndex = plIndex[i];
                        pline.Closed = false;
                        //ed.WriteMessage("\n 我的数量是:" +myPolyLines.Count);
                        this.AddEntityToModeSpace(db, pline);
                    }
                }



            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

周杰伦fans

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

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

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

打赏作者

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

抵扣说明:

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

余额充值