AutoCAD.NET API2018二次开发第十六章

矩形阵列,延伸直线,分解多段线,编辑多段线,编辑图案填充,使用图层,使用线性,使用图层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//运行时
using Autodesk.AutoCAD.Runtime;
//数据库
using Autodesk.AutoCAD.DatabaseServices;
//应用程序服务
using Autodesk.AutoCAD.ApplicationServices;
//几何
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
//编辑输入
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;

namespace AutoDemo20
{
    public class Class1
    {
        //声明实体对象
        static Point2d PointC(Point2d zxd, double ang, double dic)
        {
            //中心点坐标加上距离*角度
            double x = zxd.X + dic * Math.Cos(ang);

            double y = zxd.Y + dic * Math.Sin(ang);

            Point2d p = new Point2d(x, y);

            return p;

        }
        //矩形阵列
        [CommandMethod("HXZL")]

        public static void HXZL()
        {
            //当前文档
            Document doc = Application.DocumentManager.MdiActiveDocument;
            //当前数据库
            Database db = doc.Database;
            //开始事务
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                //声明块表
                BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                //声明块表记录
                BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //数据点
                DBPoint p = new DBPoint(new Point3d(100, 100, 0));
                //添加点
                btr.AppendEntity(p);
                //添加事务
                tran.AddNewlyCreatedDBObject(p, true);
                //声明二维点
                Point2d pZXD = new Point2d(100, 100);
                //声明圆
                Circle c = new Circle();
                //圆点坐标
                c.Center = new Point3d(100, 150, 0);
                //边境
                c.Radius = 15;
                //添加
                btr.AppendEntity(c);
                //添加书屋
                tran.AddNewlyCreatedDBObject(c, true);

                int n = 1;
                //弧度
                double angle = 1.0472;
                //循环6次
                while (n < 6)
                {
                    //克隆
                    Circle xC = c.Clone() as Circle;

                    Point2d pJD = new Point2d(xC.Center.X, xC.Center.Y);

                    double dic = pZXD.GetDistanceTo(pJD);

                    double angleFX = pZXD.GetVectorTo(pJD).Angle;

                    Point2d pXCc = PointC(pZXD, angleFX + n * angle, dic);

                    Vector2d v2d = pJD.GetVectorTo(pXCc);

                    Vector3d v3d = new Vector3d(v2d.X, v2d.Y, 0);

                    xC.TransformBy(Matrix3d.Displacement(v3d));

                    btr.AppendEntity(xC);

                    tran.AddNewlyCreatedDBObject(xC, true);

                    n = n + 1;
                }
                tran.Commit();


            }
        }
        //延伸直线
        [CommandMethod("YCZX")]
        public static void YCZX()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Line L = new Line(new Point3d(0, 0, 0), new Point3d(5, 5, 0));

                btr.AppendEntity(L);

                tran.AddNewlyCreatedDBObject(L, true);

                doc.Editor.Regen();

                Application.ShowAlertDialog("延长之前的线段");

                L.EndPoint = L.EndPoint + L.Delta;

                tran.Commit();
            }

        }
        //分解多段线
        [CommandMethod("FJDDX")]
        public static void FJDDX()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Polyline PL = new Polyline();

                PL.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);

                PL.AddVertexAt(1, new Point2d(10, 10), 0, 0, 0);

                PL.AddVertexAt(2, new Point2d(10, 20), 0, 0, 0);

                PL.AddVertexAt(3, new Point2d(15, 30), 0, 0, 0);

                btr.AppendEntity(PL);

                tran.AddNewlyCreatedDBObject(PL, true);

                DBObjectCollection dbOC = new DBObjectCollection();

                PL.Explode(dbOC);

                foreach (Entity ent in dbOC)
                {
                    ent.ColorIndex = 1;

                    btr.AppendEntity(ent);

                    tran.AddNewlyCreatedDBObject(ent, true);

                }

                tran.Commit();
            }
        }

        //编辑多段线
        [CommandMethod("BJDDX")]

        public static void BJDDX()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Polyline pL = new Polyline();

                pL.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);

                pL.AddVertexAt(1, new Point2d(10, 5), 0, 0, 0);

                pL.AddVertexAt(2, new Point2d(20, 25), 0, 0, 0);

                btr.AppendEntity(pL);

                tran.AddNewlyCreatedDBObject(pL, true);

                Polyline pL2 = pL.Clone() as Polyline;

                pL2.ColorIndex = 1;

                pL2.AddVertexAt(3, new Point2d(30, 40), 0, 0, 0);

                pL2.SetStartWidthAt(1, 0.1);

                pL2.SetEndWidthAt(1, 0.5);

                pL2.Closed = true;

                pL2.SetBulgeAt(2, -0.5);

                btr.AppendEntity(pL2);

                tran.AddNewlyCreatedDBObject(pL2, true);

                tran.Commit();
            }

        }
        //编辑图案填充
        [CommandMethod("BJTC")]
        public static void BJTC()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Polyline pl = new Polyline();

                pl.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);

                pl.AddVertexAt(1, new Point2d(0, 100), 0, 0, 0);

                pl.AddVertexAt(2, new Point2d(100, 100), 0, 0, 0);

                pl.AddVertexAt(3, new Point2d(100, 0), 0, 0, 0);

                btr.AppendEntity(pl);

                tran.AddNewlyCreatedDBObject(pl, true);

                Line L = new Line(new Point3d(0, 0, 0), new Point3d(100, 0, 0));

                btr.AppendEntity(L);

                tran.AddNewlyCreatedDBObject(L, true);

                ObjectIdCollection oic = new ObjectIdCollection();

                oic.Add(pl.Id);

                oic.Add(L.Id);

                Hatch H = new Hatch();

                btr.AppendEntity(H);

                tran.AddNewlyCreatedDBObject(H, true);

                H.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");

                H.Associative = true;

                H.AppendLoop(HatchLoopTypes.Outermost, oic);

                Hatch H2 = H.Clone() as Hatch;

                btr.AppendEntity(H2);

                tran.AddNewlyCreatedDBObject(H2, true);

                H2.ColorIndex = 1;

                H2.PatternScale = H.PatternScale + 2;

                H2.SetHatchPattern(H.PatternType, H.PatternName);

                H2.EvaluateHatch(true);

                tran.Commit();
            }
        }

        //使用图层
        [CommandMethod("SYTC")]
        public static void SYTC()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = tran.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                if (lt.Has("绘图") == false)
                {
                    LayerTableRecord ltr = new LayerTableRecord();

                    ltr.Name = "绘图";

                    ltr.Color = Color.FromRgb(255, 0, 0);

                    lt.Add(ltr);

                    tran.AddNewlyCreatedDBObject(ltr, true);

                }

                BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Circle c = new Circle();

                c.Center = new Point3d(100, 100, 0);

                c.Radius = 20;

                c.Layer = "绘图";

                btr.AppendEntity(c);

                tran.AddNewlyCreatedDBObject(c, true);

                if (lt.Has("关闭图层") == false)
                {
                    LayerTableRecord ltr = new LayerTableRecord();

                    ltr.Name = "关闭图层";

                    ltr.IsOff = true;

                    lt.Add(ltr);

                    tran.AddNewlyCreatedDBObject(ltr, true);

                }

                if (lt.Has("冻结图层") == false)
                {
                    LayerTableRecord ltr = new LayerTableRecord();

                    ltr.Name = "冻结图层";

                    ltr.IsFrozen = true;

                    lt.Add(ltr);

                    tran.AddNewlyCreatedDBObject(ltr, true);

                }

                tran.Commit();
            }
        }

        //使用线性
        [CommandMethod("SYXX")]
        public static void SYXX()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LinetypeTable ltt = tran.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;

                string strLN = "dot";

                if (ltt.Has(strLN) == false)
                {
                    db.LoadLineTypeFile(strLN, "acad.lin");

                }

                BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Line L = new Line(new Point3d(0, 0, 0), new Point3d(100, 100, 0));

                L.Linetype = strLN;

                btr.AppendEntity(L);

                tran.AddNewlyCreatedDBObject(L, true);

                tran.Commit();
            }

        }


        // 使用图层
        [CommandMethod("BCYX")]
        public static void BCYX()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            LayerStateManager lsn;

            lsn = doc.Database.LayerStateManager;

            string name;

            name = "yxxx";

            if (lsn.HasLayerState(name) == false)
            {
                lsn.SaveLayerState(name, LayerStateMasks.Color | LayerStateMasks.LineType, ObjectId.Null);

            }

        }

        [CommandMethod("HFYX")]

        public static void HFYX()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            LayerStateManager lsn = doc.Database.LayerStateManager;

            string name;

            name = "yxxx";

            if (lsn.HasLayerState(name) == true)
            {
                lsn.RestoreLayerState(name, ObjectId.Null, 1, LayerStateMasks.Color | LayerStateMasks.LineType);

            }
            else Application.ShowAlertDialog("不含有该图层状态");


        }

        [CommandMethod("DCZT")]

        public static void DCZT()
        {

            Document doc = Application.DocumentManager.MdiActiveDocument;

            LayerStateManager lsn = doc.Database.LayerStateManager;

            string name;

            name = "yxxx";

            if (lsn.HasLayerState(name) == true)
            {
                lsn.ExportLayerState(name, "c:\\" + name + ".las");

            }
            else Application.ShowAlertDialog("该图层状态不存在!!");


        }

        [CommandMethod("DRZT")]
        public static void DRZT()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            LayerStateManager lsn = doc.Database.LayerStateManager;

            string path = "c:\\yxxx.las";

            if (System.IO.File.Exists(path))
            {
                try
                {
                    lsn.ImportLayerState(path);
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    Application.ShowAlertDialog(ex.Message);
                }

            }


        }

    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值