AutoCAD C#二次开发

2021-03-31 11:28

阅读:454

标签:and   def   table   object   option   try   geo   str   二次开发   

https://www.cnblogs.com/gisoracle/archive/2012/02/19/2357925.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

 

namespace MyFirstProject
{
    public class Class1
    {
        [CommandMethod("HelloNet")]
        public void HelloNet()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("使用NET开发AutoCAD 程序bygisoracle");
        }
        [CommandMethod("PickPoint")]
        public void PickPoint()
        {
            //获取Editor 对象
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptPointOptions promptPtOp = new PromptPointOptions("选择一个点:");
            //指定的基点,如果指定了该点,则在选择的时候绘制一条橡皮线。
            promptPtOp.BasePoint = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0);
            PromptPointResult resPt;
            resPt = ed.GetPoint(promptPtOp);
            if (resPt.Status == PromptStatus.OK)
            {
                ed.WriteMessage(" 选择的点为: " + resPt.Value.ToString());
            }

        }
        [CommandMethod("createCircle")]
        public void createCircle()
        {
           
            //首先声明我们要使用的对象
            Circle circle; //这个是我们要加入到模型空间的圆
            BlockTableRecord btr;//要加入圆,我们必须打开模型空间
            BlockTable bt; //要打开模型空间,我们必须通过块表(BlockTable)来访问它

            //我们使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来
            Transaction trans;

            //使用TransactionManager的StartTransaction()成员来开始事务处理
            trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();

            //现在创建圆……请仔细看这些参数——注意创建Point3d对象的‘New’和Vector3d的静态成员ZAxis
            circle = new Circle(new Point3d(10, 10, 0), Vector3d.ZAxis, 2);
            bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);

            //使用当前的空间Id来获取块表记录——注意我们是打开它用来写入
            btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);

            //现在使用btr对象来加入圆
            btr.AppendEntity(circle);
            trans.AddNewlyCreatedDBObject(circle, true); //并确定事务处理知道要加入圆!

            //一旦完成以上操作,我们就提交事务处理,这样以上所做的改变就被保存了……
            trans.Commit();

            //…然后销毁事务处理,因为我们已经完成了相关的操作(事务处理不是数据库驻留对象,可以销毁)
            trans.Dispose();

        }
        [CommandMethod("SelectAPoint")]
        public void SelectAPoint()
        {
            //实例化一个 PromptPointOptions类用来设置提示字符串和其他的一些控制提示
            PromptPointOptions prPointOptions = new PromptPointOptions("Select a point");
            PromptPointResult prPointRes;
            // 实例化一个Editor类,使用GetPoint方法返回
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            prPointRes = ed.GetPoint(prPointOptions);
            if (prPointRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("Error");
            }
            else
            {
                ed.WriteMessage("选择的点为:" + prPointRes.Value.ToString());
            }
        }
        [CommandMethod("getDistance")]
        public void GetDistance()
        {
            PromptDistanceOptions prDistOptions = new
            PromptDistanceOptions("计算两点距离,请选择第一个点:");
            PromptDoubleResult prDistRes;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            prDistRes = ed.GetDistance(prDistOptions);
            if (prDistRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("选择错误!");
            }
            else
            {
                ed.WriteMessage("两点的距离为:" + prDistRes.Value.ToString());
            }
        }
        [CommandMethod("AddPointAndSetPointStyle")]

        public static void AddPointAndSetPointStyle()
        {
            // 获得当前文档和数据库   Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            // 启动一个事务  Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表   Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // 在模型空间中创建一个坐标为(4,3,0)的点   Create a point at (4, 3, 0) in Model space
                for (int i = 0; i 100; i++)
                {
                    DBPoint acPoint = new DBPoint(new Point3d(4*i, 3, 0));

                    acPoint.SetDatabaseDefaults();

                    // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                    acBlkTblRec.AppendEntity(acPoint);
                    acTrans.AddNewlyCreatedDBObject(acPoint, true);
                }
              

                // 在图形中设置所有点对象的样式   Set the style for all point objects in the drawing
                acCurDb.Pdmode = 34;
                acCurDb.Pdsize = 1;

                // 保存新对象到数据库中   Save the new object to the database
                acTrans.Commit();
            }
        }
        [CommandMethod("Add2DSolid")]
        public static void Add2DSolid()
        {
            // 获得当前文档和数据库   Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            // 启动一个事务  Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表   Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // Create a quadrilateral (bow-tie) solid in Model space
               
                Solid ac2DSolidBow = new Solid(new Point3d(0, 0, 0),
                                               new Point3d(5, 0, 0),
                                               new Point3d(5, 8, 0),
                                               new Point3d(0, 8, 0));

                ac2DSolidBow.SetDatabaseDefaults();

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(ac2DSolidBow);
                acTrans.AddNewlyCreatedDBObject(ac2DSolidBow, true);

                // Create a quadrilateral (square) solid in Model space
                Solid ac2DSolidSqr = new Solid(new Point3d(10, 0, 0),
                                               new Point3d(15, 0, 0),
                                               new Point3d(10, 8, 0),
                                               new Point3d(15, 8, 0));

                ac2DSolidSqr.SetDatabaseDefaults();

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(ac2DSolidSqr);
                acTrans.AddNewlyCreatedDBObject(ac2DSolidSqr, true);

                // 保存新对象到数据库中   Save the new object to the database
                acTrans.Commit();
            }
        }

        [CommandMethod("AddLine")]
        public static void AddLine()
        {
            // 获得当前文档和数据库   Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            // 启动一个事务  Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表   Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // 创建一条起点为(5,5,0),终点为(12,3,0)的直线  Create a line that starts at 5,5 and ends at 12,3
                Line acLine = new Line(new Point3d(5, 5, 0),
                                       new Point3d(12, 3, 0));

                acLine.SetDatabaseDefaults();

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acLine);
                acTrans.AddNewlyCreatedDBObject(acLine, true);

                // 保存新对象到数据库中   Save the new object to the database
                acTrans.Commit();
            }


        }
    }
}

 

AutoCAD C#二次开发

标签:and   def   table   object   option   try   geo   str   二次开发   

原文地址:https://www.cnblogs.com/belx/p/9256364.html


评论


亲,登录后才可以留言!