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

此篇博客展示了如何在AutoCAD中创建三维线段、用户坐标系、多边形网格和多面网格,包括长方体、楔形实体、旋转长方体,以及进行长方体与椭圆体的交集运算和裁切操作。通过实例演示了几何对象的创建、变换和交互操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三维线段,用户坐标系,多边形网格,多面网格,长方体,楔形实体,旋转长方体,长方体与椭圆体的交集运算,裁切长方体

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;


namespace AutoDemo22
{
    public class Class1
    {
        //三维线段
        [CommandMethod("SWXD")]
        public static void SWXD()
        {
            //当前文档
            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(5, 0), 0, 0, 0);
                //第三个节点
                Pl.AddVertexAt(2, new Point2d(5, 5), 0, 0, 0);

                Pl.ColorIndex = 1;

                btr.AppendEntity(Pl);
                tran.AddNewlyCreatedDBObject(Pl, true);

                Polyline3d pl3d = new Polyline3d();

                pl3d.ColorIndex = 5;

                btr.AppendEntity(pl3d);

                tran.AddNewlyCreatedDBObject(pl3d, true);

                Point3dCollection p3dC = new Point3dCollection();

                p3dC.Add(new Point3d(5, 5, 0));

                p3dC.Add(new Point3d(0, 5, 5));

                p3dC.Add(new Point3d(0, 0, 5));

                p3dC.Add(new Point3d(0, 0, 0));

                foreach (Point3d p in p3dC)
                {
                    PolylineVertex3d pv = new PolylineVertex3d(p);

                    pl3d.AppendVertex(pv);

                    tran.AddNewlyCreatedDBObject(pv, true);
                }

                tran.Commit();

                
            }
        }

        //用户坐标系
        [CommandMethod("YHZBX")]
        public static void YHZBX()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                UcsTable ucs = tran.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable;

                UcsTableRecord ucsr;

                if (ucs.Has("用户坐标系") == false)
                {
                    ucsr = new UcsTableRecord();
                    tran.GetObject(db.UcsTableId, OpenMode.ForWrite);

                    ucsr.Name = "用户坐标系";

                    ucs.Add(ucsr);

                    tran.AddNewlyCreatedDBObject(ucsr, true);

                }
                else
                {
                    ucsr = tran.GetObject(ucs["用户坐标系"], OpenMode.ForWrite) as UcsTableRecord;
                }

                ucsr.Origin = new Point3d(5, 5, 0);

                ucsr.XAxis = new Vector3d(1, 0, 0);

                ucsr.YAxis = new Vector3d(0, 1, 0);

                ViewportTableRecord vprt = tran.GetObject(doc.Editor.ActiveViewportId, OpenMode.ForRead) as ViewportTableRecord;

                vprt.IconAtOrigin = true;

                vprt.IconEnabled = true;

                vprt.SetUcs(ucsr.Id);

                doc.Editor.UpdateTiledViewportsFromDatabase();

                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(0, 0, 0);

                c.Radius = 1;

                c.ColorIndex = 1;

                btr.AppendEntity(c);

                tran.AddNewlyCreatedDBObject(c, true);

                Circle c2 = c.Clone() as Circle;

                c2.ColorIndex = 5;

                Matrix3d m3d = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
                    vprt.Ucs.Origin, vprt.Ucs.Xaxis, vprt.Ucs.Yaxis, vprt.Ucs.Zaxis);

                c2.TransformBy(m3d);

                btr.AppendEntity(c2);

                tran.AddNewlyCreatedDBObject(c2, true);

                tran.Commit();
            }


        }


        //多边形网格
        [CommandMethod("DBXWG")]
        public static void DBXWG()
        {
            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;

                PolygonMesh pm = new PolygonMesh();

                pm.MSize = 3;

                pm.NSize = 4;

                btr.AppendEntity(pm);

                tran.AddNewlyCreatedDBObject(pm, true);

                Point3dCollection p3dC = new Point3dCollection();

                p3dC.Add(new Point3d(0, 0, 0));
                p3dC.Add(new Point3d(5, 0, 5));
                p3dC.Add(new Point3d(10, 0, 0));
                p3dC.Add(new Point3d(15, 0, 5));

                p3dC.Add(new Point3d(0, 5, 0));
                p3dC.Add(new Point3d(5, 5, 5));
                p3dC.Add(new Point3d(10, 5, 0));
                p3dC.Add(new Point3d(15, 5, 5));

                p3dC.Add(new Point3d(0, 10, 0));
                p3dC.Add(new Point3d(5, 10, 5));
                p3dC.Add(new Point3d(10, 10, 0));
                p3dC.Add(new Point3d(15, 10, 5));

                foreach (Point3d p in p3dC)
                {
                    PolygonMeshVertex pmv = new PolygonMeshVertex(p);

                    pm.AppendVertex(pmv);

                    tran.AddNewlyCreatedDBObject(pmv, true);

                }

                tran.Commit();
            }



        }

        //多面网格
        [CommandMethod("DMWG")]
        public static void DMWG()
        {
            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;

                PolyFaceMesh pfm = new PolyFaceMesh();

                btr.AppendEntity(pfm);

                tran.AddNewlyCreatedDBObject(pfm, true);

                Point3dCollection p3dC = new Point3dCollection();

                p3dC.Add(new Point3d(0, 0, 0));

                p3dC.Add(new Point3d(0, 5, 0));

                p3dC.Add(new Point3d(0, 10, 5));

                p3dC.Add(new Point3d(5, 0, 0));

                p3dC.Add(new Point3d(5, 5, 0));

                p3dC.Add(new Point3d(5, 10, 5));

                foreach (Point3d p in p3dC)
                {
                    PolyFaceMeshVertex pfmv = new PolyFaceMeshVertex(p);

                    pfm.AppendVertex(pfmv);

                    tran.AddNewlyCreatedDBObject(pfmv, true);

                }

                FaceRecord fr1 = new FaceRecord(1, 2, 5, 4);

                pfm.AppendFaceRecord(fr1);

                tran.AddNewlyCreatedDBObject(fr1, true);

                FaceRecord fr2 = new FaceRecord(2, 3, 6, 5);

                pfm.AppendFaceRecord(fr2);

                tran.AddNewlyCreatedDBObject(fr2, true);

                tran.Commit();


            }

        }

        //长方体
        [CommandMethod("CFT")]
        public static void CFT()
        {
            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;

                Solid3d s3d = new Solid3d();

                s3d.CreateBox(10, 20, 40);

                Matrix3d m3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);

                s3d.TransformBy(m3d);

                btr.AppendEntity(s3d);

                tran.AddNewlyCreatedDBObject(s3d, true);

                tran.Commit();

            }

        }

        //楔形实体
        [CommandMethod("XXST")]
        public static void XXST()
        {
            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;

                Solid3d s3d = new Solid3d();

                s3d.CreateWedge(10, 20, 40);

                Matrix3d ma3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);

                s3d.TransformBy(ma3d);

                btr.AppendEntity(s3d);

                tran.AddNewlyCreatedDBObject(s3d, true);

                tran.Commit();

            }


        }

        //旋转长方体
        [CommandMethod("XZST")]
        public static void XZST()
        {
            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;

                Solid3d s3d = new Solid3d();

                s3d.CreateBox(10, 20, 40);

                Matrix3d m3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);

                s3d.TransformBy(m3d);

                btr.AppendEntity(s3d);

                tran.AddNewlyCreatedDBObject(s3d, true);

                Solid3d s3d2 = s3d.Clone() as Solid3d;

                s3d2.ColorIndex = 1;

                Matrix3d m = doc.Editor.CurrentUserCoordinateSystem;

                CoordinateSystem3d cs = m.CoordinateSystem3d;

                Matrix3d m3dx = Matrix3d.Rotation(3.14, cs.Zaxis, new Point3d(0, 0, 0));

                s3d2.TransformBy(m3dx);

                btr.AppendEntity(s3d2);

                tran.AddNewlyCreatedDBObject(s3d2, true);

                tran.Commit();



            }




        }

        //长方体与椭圆体的交集运算
        [CommandMethod("JSJJ")]
        public static void JSJJ()
        {
            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;

                Solid3d s3d = new Solid3d();

                s3d.CreateBox(10, 20, 40);

                Matrix3d m3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);

                s3d.TransformBy(m3d);

                btr.AppendEntity(s3d);

                tran.AddNewlyCreatedDBObject(s3d, true);

                Solid3d s3d2 = new Solid3d();

                s3d2.CreateFrustum(20, 5, 10, 5);

                s3d2.ColorIndex = 5;

                btr.AppendEntity(s3d2);

                tran.AddNewlyCreatedDBObject(s3d2, true);

                Solid3d s3dc = s3d2.Clone() as Solid3d;

                if (s3dc.CheckInterference(s3d) == true)
                {
                    s3dc.BooleanOperation(BooleanOperationType.BoolIntersect, s3d.Clone() as Solid3d);

                    s3dc.ColorIndex = 1;


                }

                btr.AppendEntity(s3dc);

                tran.AddNewlyCreatedDBObject(s3dc, true);

                tran.Commit();




            }



        }

        //裁切长方体
        [CommandMethod("CQST")]

        public static void CQST()
        {
            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;

                Solid3d s3d = new Solid3d();

                s3d.CreateBox(10, 20, 40);

                Matrix3d m3d = Matrix3d.Displacement(new Point3d(5, 10, 20) - Point3d.Origin);

                s3d.TransformBy(m3d);

                btr.AppendEntity(s3d);

                tran.AddNewlyCreatedDBObject(s3d, true);

                Plane p = new Plane(new Point3d(5, 10, 20), Vector3d.XAxis, Vector3d.YAxis);

                Solid3d s3d2 = new Solid3d();

                s3d2 = s3d.Slice(p, true);

                s3d2.ColorIndex = 1;

                btr.AppendEntity(s3d2);

                tran.AddNewlyCreatedDBObject(s3d2, true);

                tran.Commit();

            }

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值