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;

namespace AutoDemo18
{
    public class Class1
    {
        //绘制圆
        [CommandMethod("ADDC")]
        public static void ADDC()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取数据库
            Database acDB = acDoc.Database;
            //事务管理器
            using (Transaction acTran = acDB.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDB.BlockTableId,OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                using (Circle c = new Circle())
                {
                    //定义园
                    c.Center = new Point3d(10, 10, 0);
                    c.Radius = 10;
                    c.ColorIndex = 3;
                    acBTR.AppendEntity(c);
                    acTran.AddNewlyCreatedDBObject(c, true);
                }
                acTran.Commit();
            }
        }

        //绘制圆弧
        [CommandMethod("ADDARC")]
        public static void ADDARC()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取数据库
            Database acDB = acDoc.Database;
            //事务管理器
            using (Transaction acTran = acDB.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                    //圆弧 四个参数 圆弧圆心 圆弧半径 圆弧起始角度 终止角度
                    Arc a = new Arc(new Point3d(20, 10, 0), 10, 0, 1.57);
                    a.ColorIndex = 3;

                    acBTR.AppendEntity(a);
                    acTran.AddNewlyCreatedDBObject(a, true);
                    acTran.Commit();
            }
        }

        //绘制曲线
        [CommandMethod("AddSpline")]
        public static void AddSpline()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取数据库
            Database acDB = acDoc.Database;
            //事务管理器
            using (Transaction acTran = acDB.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //三维集合
                Point3dCollection p3d = new Point3dCollection();
                p3d.Add(new Point3d(0, 0, 0));
                p3d.Add(new Point3d(10, 10, 0));
                p3d.Add(new Point3d(20, 30, 0));

                //三维矢量
                Vector3d vc3 = new Point3d(10, 10, 0).GetAsVector();

                //定义曲线五个参数 三维点集合,曲线起始切面点 末端点 创建顺序  公差 
                Spline sp = new Spline(p3d,vc3,vc3,2,0);
                sp.ColorIndex = 1;
                

                acBTR.AppendEntity(sp);
                acTran.AddNewlyCreatedDBObject(sp, true);
                acTran.Commit();
            }
        }

        //绘制点 以及样式
        [CommandMethod("AddD")]
        public static void AddD()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取数据库
            Database acDB = acDoc.Database;
            //事务管理器
            using (Transaction acTran = acDB.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //数据库点
                DBPoint DP = new DBPoint(new Point3d(10,10,0));
                
                //添加块表记录
                acBTR.AppendEntity(DP);
                acTran.AddNewlyCreatedDBObject(DP, true);
                //定义点的样式
                acDB.Pdmode = 66;
                //定义大小
                acDB.Pdsize = 5;
                //事务提交
                acTran.Commit();
            }
        }

        //绘制实体填充区域
        [CommandMethod("AddP")]
        public static void AddP()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取数据库
            Database acDB = acDoc.Database;
            //事务管理器
            using (Transaction acTran = acDB.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Point3d p3d1 = new Point3d(0, 10, 0);
                Point3d p3d2 = new Point3d(10, 10, 0);
                Point3d p3d3 = new Point3d(0, 0, 0);
                Point3d p3d4 = new Point3d(10, 0, 0);

                Solid s = new Solid(p3d1, p3d2, p3d3, p3d4);
                //添加块表记录
                acBTR.AppendEntity(s);
                acTran.AddNewlyCreatedDBObject(s, true);

                Point3d p3d5 = new Point3d(20, 10, 0);
                Point3d p3d6 = new Point3d(30, 10, 0);
                Point3d p3d7 = new Point3d(30, 0, 0);
                Point3d p3d8 = new Point3d(20, 0, 0);

                Solid sa= new Solid(p3d5, p3d6, p3d7, p3d8);
                //添加块表记录
                acBTR.AppendEntity(sa);
                acTran.AddNewlyCreatedDBObject(sa, true);

                //事务提交
                acTran.Commit();
            }
        }

        //创建面域
        [CommandMethod("AddM")]
        public static void AddM()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取数据库
            Database acDB = acDoc.Database;
            //事务管理器
            using (Transaction acTran = acDB.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;


                Circle c = new Circle();
                //定义园
                c.Center = new Point3d(10, 10, 0);
                c.Radius = 10;
                c.ColorIndex = 3;

                //数据库对象集合
                DBObjectCollection dboc = new DBObjectCollection();
                dboc.Add(c);
                DBObjectCollection dbr = new DBObjectCollection();
                //从曲线创建方法创建面域
                dbr = Region.CreateFromCurves(dboc); 
                //获得刚才定义的面域
                Region r = dbr[0] as Region;

                acBTR.AppendEntity(r);
                acTran.AddNewlyCreatedDBObject(r, true);

                //事务提交
                acTran.Commit();
            }
        }

        //创建组合面域
        [CommandMethod("AddMS")]
        public static void AddMS()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取数据库
            Database acDB = acDoc.Database;
            //事务管理器
            using (Transaction acTran = acDB.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //定义园
                Circle c = new Circle();
                c.Center = new Point3d(10, 10, 0);
                c.Radius = 10;
                c.ColorIndex = 1;


                Circle c2 = new Circle();
                c2.Center = new Point3d(15, 10, 0);
                c2.Radius = 10;
                c2.ColorIndex = 3;


                acBTR.AppendEntity(c);
                acTran.AddNewlyCreatedDBObject(c, true);
                acBTR.AppendEntity(c2);
                acTran.AddNewlyCreatedDBObject(c2, true);


                //数据库对象集合
                DBObjectCollection dboc = new DBObjectCollection();
                dboc.Add(c);
                dboc.Add(c2);
                //获取面域
                DBObjectCollection dbr = new DBObjectCollection();
                //从曲线创建方法创建面域
                dbr = Region.CreateFromCurves(dboc);
                //获得刚才定义的面域
                //注意此时获取的面域 顺序是相反的
                Region r1 = dbr[1] as Region;
                Region r2 = dbr[0] as Region;

                r1.BooleanOperation(BooleanOperationType.BoolSubtract, r2);

                acBTR.AppendEntity(r1);
                acTran.AddNewlyCreatedDBObject(r1, true);


                //事务提交
                acTran.Commit();
            }
        }

        //创建图案填充
        [CommandMethod("AddH")]
        public static void AddH()
        {
            //获取当前文档
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //获取数据库
            Database acDB = acDoc.Database;
            //事务管理器
            using (Transaction acTran = acDB.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[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, 0), 0, 0, 0);
                pl.AddVertexAt(2, new Point2d(10, 5), 0, 0, 0);
                pl.AddVertexAt(3, new Point2d(0, 5), 0, 0, 0);
                pl.AddVertexAt(4, new Point2d(0, 0), 0, 0, 0);
                //添加到块表记录
                acBTR.AppendEntity(pl);
                //添加到事务
                acTran.AddNewlyCreatedDBObject(pl, true);

                //对象集合
                ObjectIdCollection oic = new ObjectIdCollection();

                oic.Add(pl.ObjectId);
                //图案
                Hatch h = new Hatch();
                acBTR.AppendEntity(h);
                acTran.AddNewlyCreatedDBObject(h, true);

                //图案填充类型(预定义模型) 图案填充名称
                h.SetHatchPattern(HatchPatternType.PreDefined, "angle");
                //设置关联
                h.Associative = true;
                //填充边界 oic
                h.AppendLoop(HatchLoopTypes.Outermost, oic);
                //计算填充线
                h.EvaluateHatch(true);
                
                //事务提交
                acTran.Commit();
            }
        }


        //选择集 
        [CommandMethod("pickFirst", CommandFlags.UsePickSet)]
        public static void pickFirst()
        {
            //获取先选择后选择的系统变量 pickfirst
            object pickF = Application.GetSystemVariable("pickfirst");
            //如果不是1 则设置成为1 
            if (System.Convert.ToInt16(pickF) != 1)
            {
                Application.SetSystemVariable("pickfirst", 1);
            }
            //获取当前文档的编辑器
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //选择器
            PromptSelectionResult psr = ed.SelectImplied();

            SelectionSet ss;
            //如果为OK则说明有图形被选中
            if (psr.Status == PromptStatus.OK)
            {
                ss = psr.Value;

                Application.ShowAlertDialog("选中图形的数量是" + ss.Count.ToString());
            }
            else
            {
                Application.ShowAlertDialog("没有图形对象被选中!!");
            }
            //声明数组
            ObjectId[] id = new ObjectId[0];
            //赋值给当前选择集
            ed.SetImpliedSelection(id);

            psr = ed.GetSelection();

            if (psr.Status == PromptStatus.OK)
            {
                ss = psr.Value;

                Application.ShowAlertDialog("选中图形的数量是" + ss.Count.ToString());
            }
            else
            {
                Application.ShowAlertDialog("没有图形对象被选中!!");
            }


        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值