CAD二次开发(C#)第三节

前言

这是最后一部分代码,往后若有时间,对其中的代码详细讲解。初心不变,以便日后查看。特别感谢作者孙成波译作《AutoCAD .NET 开发指南 2012 版》。三部分代码里面有些许失误,但很容易看出来,在此均做修改。若要直接运行,需要对其中的一些地址跟引用加以修改。

代码

#region CAD二次开发--第三节
        /// <summary>
        /// 折弯尺寸线 
        /// </summary>
        public static void DemisionLine()
        {
            //以读模式打开已注册应用程序表
            RegAppTable acRegAppTbl;
            acRegAppTbl = null;
            //<transaction>.GetObject(<current_database>.RegAppTableId,
            //    OpenMode.ForRead) as RegAppTable;
            //检查看看应用"ACAD_DSTYLE_DIMJAG_POSITION"是否已经注册,
            //如果没注册的话就将它添加进去
            if (acRegAppTbl.Has("ACAD_DSTYLE_DIMJAG_POSITION") == false)
            {
                RegAppTableRecord acRegAppTblRec = new RegAppTableRecord();
                acRegAppTblRec.Name = "ACAD_DSTYLE_DIMJAG_POSITION";
                acRegAppTbl.UpgradeOpen();
                acRegAppTbl.Add(acRegAppTblRec);
                //<transaction>.AddNewlyCreatedDBObject(acRegAppTblRec,true)
            }
            //创建一个结果缓冲区来定义Xdata
            ResultBuffer acResBuf = new ResultBuffer();
            acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName,
                "ACAD_DSTYLE_DIMJAG_POSITION"));
            acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 387));
            acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 3));
            acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 389));
            acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataXCoordinate,
                new Point3d(-1.26985, 3.91514, 0)));
            //将Xdata添加到尺寸标注对象
            //<dimension_object>.XData=acResBuf;
        }


        /// <summary>
        /// 创建旋转线性标注 rotated linear dimension
        /// </summary>
        [CommandMethod("CreateRotatedDimension")]
        public static void CreateRotatedDimension()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录ModelSpace(模型空间)
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建旋转尺寸标注
                RotatedDimension acRotDim = new RotatedDimension();
                acRotDim.XLine1Point = new Point3d(0, 0, 0);
                acRotDim.XLine2Point = new Point3d(6, 3, 0);
                acRotDim.Rotation = 0.707;
                acRotDim.DimLinePoint = new Point3d(0, 5, 0);
                acRotDim.DimensionStyle = acCurDb.Dimstyle;
                //将新对象添加到块表记录ModelSpace及事务
                acBlkTblRec.AppendEntity(acRotDim);
                acTrans.AddNewlyCreatedDBObject(acRotDim, true);
                //提交修改,关闭事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建径向标注  Create a radial dimension
        /// </summary>
        [CommandMethod("CreateRadialDimension")]
        public static void CreateRadialDimension()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.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
                //以写模式打开 块表记录ModelSpace
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //Create the radial dimension 创建半径标注
                RadialDimension acRadDim = new RadialDimension();
                acRadDim.Center = new Point3d(0, 0, 0);
                acRadDim.ChordPoint = new Point3d(5, 5, 0);
                acRadDim.LeaderLength = 5;
                acRadDim.DimensionStyle = acCurDb.Dimstyle;
                //添加新对象到模型空间和事务
                acBlkTblRec.AppendEntity(acRadDim);
                acTrans.AddNewlyCreatedDBObject(acRadDim, true);
                //提交修改,关闭事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建角度标注  Create an angular dimension
        /// </summary>
        [CommandMethod("CreateAngularDimension")]
        public static void CreateAngularDimension()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录ModelSpace
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //建立角度标注
                LineAngularDimension2 acLinAngDim = new LineAngularDimension2();
                acLinAngDim.XLine1Start = new Point3d(0, 5, 0);
                acLinAngDim.XLine1End = new Point3d(1, 7, 0);
                acLinAngDim.XLine2Start = new Point3d(0, 5, 0);
                acLinAngDim.XLine2End = new Point3d(1, 3, 0);
                acLinAngDim.ArcPoint = new Point3d(3, 5, 0);
                acLinAngDim.DimensionStyle = acCurDb.Dimstyle;
                //添加新对象到模型空间和事务中
                acBlkTblRec.AppendEntity(acLinAngDim);
                acTrans.AddNewlyCreatedDBObject(acLinAngDim, true);
                //提交修改,关闭事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建折弯半径标注  Create a jogged radius dimension
        /// </summary>
        [CommandMethod("CreateJoggedDimension")]
        public static void CreateJoggedDimension()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建一个大半径标注
                RadialDimensionLarge acRadDimLrg = new RadialDimensionLarge();
                acRadDimLrg.Center = new Point3d(-3, -4, 0);
                acRadDimLrg.ChordPoint = new Point3d(2, 7, 0);
                acRadDimLrg.OverrideCenter = new Point3d(0, 2, 0);
                acRadDimLrg.JogPoint = new Point3d(1, 4.5, 0);
                acRadDimLrg.JogAngle = 0.707;
                acRadDimLrg.DimensionStyle = acCurDb.Dimstyle;
                //将新对象添加到模型空间和事务
                acBlkTblRec.AppendEntity(acRadDimLrg);
                acTrans.AddNewlyCreatedDBObject(acRadDimLrg, true);
                //提交修改,关闭事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建弧长标注  Create an arc length dimension
        /// </summary>
        [CommandMethod("CreateArcLengthDimension")]
        public static void CreateArcLengthDimension()
        {
            //获取当前文档和数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开Block表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开Block表记录Model空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建一个弧长标注
                ArcDimension acArcDim = new ArcDimension(new Point3d(4.5, 1.5, 0),
                    new Point3d(8, 4.25, 0),
                    new Point3d(0, 2, 0),
                    new Point3d(5, 7, 0),
                    "<>",
                    acCurDb.Dimstyle);
                //将新对象添加到模型空间和事务
                acBlkTblRec.AppendEntity(acArcDim);
                acTrans.AddNewlyCreatedDBObject(acArcDim, true);
                //提交修改,关闭事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建一个坐标标注 
        /// </summary>
        [CommandMethod("CreateOrdinateDimension")]
        public static void CreateOrdinateDimension()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录ModelSpace
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建坐标标注
                OrdinateDimension acOrdDim = new OrdinateDimension();
                acOrdDim.UsingXAxis = true;
                acOrdDim.DefiningPoint = new Point3d(5, 5, 0);
                acOrdDim.LeaderEndPoint = new Point3d(10, 5, 0);
                acOrdDim.DimensionStyle = acCurDb.Dimstyle;
                //将新对象添加到模型空间和事务
                acBlkTblRec.AppendEntity(acOrdDim);
                acTrans.AddNewlyCreatedDBObject(acOrdDim, true);
                //提交修改,关闭事务
                acTrans.Commit();
            }
        }

        /// <summary>
        /// 修改标注文字 
        /// </summary>
        [CommandMethod("OverrideDimensionText")]
        public static void OverrideDimensionText()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建一个对齐标注
                AlignedDimension acAlidim = new AlignedDimension();
                acAlidim.XLine1Point = new Point3d(5, 3, 0);
                acAlidim.XLine2Point = new Point3d(10, 3, 0);
                acAlidim.DimLinePoint = new Point3d(7.5, 5, 0);
                acAlidim.DimensionStyle = acCurDb.Dimstyle;
                //改写标注文字
                acAlidim.DimensionText = "The value is <>";
                //将新对象添加到模型空间和事务
                acBlkTblRec.AppendEntity(acAlidim);
                acTrans.AddNewlyCreatedDBObject(acAlidim, true);
                //提交修改,关闭事务
                acTrans.Commit();
            }
        }

        /// <summary>
        /// 拷贝 并修改
        /// </summary>
        [CommandMethod("CopyDimStyles")]
        public static void CopyDimStyles()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开Block表ModelSpace
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForRead) as BlockTableRecord;

                object acObj = null;

                foreach (ObjectId acObjId in acBlkTblRec)
                {
                    //获取模型空间中的第一个对象
                    acObj = acTrans.GetObject(acObjId,
                        OpenMode.ForRead);
                    break;
                }
                //以读模式打开DimStyle表
                DimStyleTable acDimStyleTbl;
                acDimStyleTbl = acTrans.GetObject(acCurDb.DimStyleTableId,
                    OpenMode.ForRead) as DimStyleTable;

                string[] strDimStyleNames = new string[3];
                strDimStyleNames[0] = "Style 1 copied from a dim.";
                strDimStyleNames[1] = "Style 2 copied from Style 1";
                strDimStyleNames[2] = "Style 3 copied from the running drawing values.";

                int nCnt = 0;

                //定义一个第一个标注样式的引用,一会儿要用到
                DimStyleTableRecord acDimStyleTblRec1 = null;
                //遍历标注样式名称数组
                foreach (string strDimStyleName in strDimStyleNames)
                {
                    DimStyleTableRecord acDimStyleTblRec;
                    DimStyleTableRecord acDimStyleTblRecCopy = null;
                    //检查标注样式是否存在
                    if (acDimStyleTbl.Has(strDimStyleName) == false)
                    {
                        if (acDimStyleTbl.IsWriteEnabled == false)
                            acDimStyleTbl.UpgradeOpen();

                        acDimStyleTblRec = new DimStyleTableRecord();
                        acDimStyleTblRec.Name = strDimStyleName;

                        acDimStyleTbl.Add(acDimStyleTblRec);
                        acTrans.AddNewlyCreatedDBObject(acDimStyleTblRec, true);
                    }
                    else
                    {
                        acDimStyleTblRec = acTrans.GetObject(acDimStyleTbl[strDimStyleName],
                            OpenMode.ForWrite) as DimStyleTableRecord;
                    }
                    //确定新的标注样式是如何填充的
                    switch ((int)nCnt)
                    {
                        //将标注对象的值赋给新标注样式
                        case 0:
                            try
                            {
                                //强制将对象类型转换为标注类型
                                Dimension acDim = acObj as Dimension;
                                //从标注复制标注样式数据并设置标注样式的名称
                                //(复制的设置是未命名的)
                                acDimStyleTblRecCopy = acDim.GetDimstyleData();
                                acDimStyleTblRec1 = acDimStyleTblRec;
                            }
                            catch
                            {
                                //对象不是个标注
                            }
                            break;
                        //将标注样式的值赋给新样式
                        case 1:
                            acDimStyleTblRecCopy = acDimStyleTblRec1;
                            break;
                        //将当前图形的值赋给标注样式
                        case 2:
                            acDimStyleTblRecCopy = acCurDb.GetDimstyleData();
                            break;
                    }
                    //复制标注设置,设标注样式名称
                    acDimStyleTblRec.CopyFrom(acDimStyleTblRecCopy);
                    acDimStyleTblRec.Name = strDimStyleName;
                    //注销复制的标注样式,回收内存
                    acDimStyleTblRecCopy.Dispose();

                    nCnt = nCnt + 1;
                }
                //提交修改,注销事务,回收内存
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 为对齐标注输入一个用户自定义后缀
        /// </summary>
        [CommandMethod("AddDimensionTextSuffix")]
        public static void AddDimensionTextSuffix()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录ModelSpace
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForRead) as BlockTableRecord;
                //创建对齐标注
                AlignedDimension acAliDim = new AlignedDimension();
                acAliDim.XLine1Point = new Point3d(0, 5, 0);
                acAliDim.XLine2Point = new Point3d(5, 5, 0);
                acAliDim.DimLinePoint = new Point3d(5, 7, 0);
                acAliDim.DimensionStyle = acCurDb.Dimstyle;
                //将新对象添加到模型空间并进行事务记录
                acBlkTblRec.AppendEntity(acAliDim);
                acTrans.AddNewlyCreatedDBObject(acAliDim, true);
                //给标注文字添加后缀
                PromptStringOptions pStrOpts = new PromptStringOptions("");
                pStrOpts.Message = "\nEnter a new text suffix for the dimension: ";
                pStrOpts.AllowSpaces = true;
                PromptResult pStrRes = acDoc.Editor.GetString(pStrOpts);

                if (pStrRes.Status == PromptStatus.OK)
                {
                    acAliDim.Suffix = pStrRes.StringResult;
                }
                //提交修改,注销事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建一条引线 
        /// </summary>
        [CommandMethod("CreateLeader")]
        public static void CreateLeader()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建引线
                Leader acLdr = new Leader();
                acLdr.AppendVertex(new Point3d(0, 0, 0));
                acLdr.AppendVertex(new Point3d(4, 4, 0));
                acLdr.AppendVertex(new Point3d(4, 5, 0));
                acLdr.HasArrowHead = true;
                //添加新对象到模型空间,记录事务
                acBlkTblRec.AppendEntity(acLdr);
                acTrans.AddNewlyCreatedDBObject(acLdr, true);
                //提交修改,回收内存
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 关联引线与注释  Associate a leader to the annotation
        /// </summary>
        [CommandMethod("AddLeaderAnnotation")]
        public static void AddLeaderAnnotation()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建多行文字(MText)注释
                MText acMText = new MText();
                acMText.Contents = "Hello,World.";
                acMText.Location = new Point3d(5, 5, 0);
                acMText.Width = 2;
                //添加新对象到模型空间,记录事务
                acBlkTblRec.AppendEntity(acMText);
                acTrans.AddNewlyCreatedDBObject(acMText, true);
                //创建带注释的引线
                Leader acLdr = new Leader();
                acLdr.AppendVertex(new Point3d(0, 0, 0));
                acLdr.AppendVertex(new Point3d(4, 4, 0));
                acLdr.AppendVertex(new Point3d(4, 5, 0));
                acLdr.HasArrowHead = true;
                //添加新对象到模型空间,记录事务
                acBlkTblRec.AppendEntity(acLdr);
                acTrans.AddNewlyCreatedDBObject(acLdr, true);
                //给引线对象附加注释
                acLdr.Annotation = acMText.ObjectId;
                acLdr.EvaluateLeader();
                //提交修改,回收内存
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建形位公差 
        /// </summary>
        [CommandMethod("CreateGeometricTolerance")]
        public static void CreateGeometricTolerance()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建形位公差(特征控制框)
                FeatureControlFrame acFcf = new FeatureControlFrame();
                acFcf.Text = "{\\Fgdt;j}%%v{\\Fgdt;n}0.001%%v%%v%%v%%v";
                acFcf.Location = new Point3d(5, 5, 0);
                //添加新对象到模型空间,作事务记录
                acBlkTblRec.AppendEntity(acFcf);
                acTrans.AddNewlyCreatedDBObject(acFcf, true);
                //提交修改,关闭事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 定义、查询 2D/3D 
        /// </summary>
        [CommandMethod("Polyline_2D_3D")]
        public static void Polyline_2D_3D()
        {
            //获取当前数据库
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //注意创建3D多段线和创建2D多段线时,
                //调用AddVertexAt()函数和AppendEntity()函数的顺序不一样

                //用2条线段(3个点)创建一条多段线
                Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                acPoly.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
                acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
                acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
                acPoly.ColorIndex = 1;
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acPoly);
                acTrans.AddNewlyCreatedDBObject(acPoly, true);
                //用2条线段(3个点)创建一条3D多段线
                Polyline3d acPoly3d = new Polyline3d();
                acPoly3d.ColorIndex = 5;
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acPoly3d);
                acTrans.AddNewlyCreatedDBObject(acPoly3d, true);
                //给3D多段线添加顶点前,3D多段线必须已在数据库中了
                Point3dCollection acPts3dPoly = new Point3dCollection();
                acPts3dPoly.Add(new Point3d(1, 1, 0));
                acPts3dPoly.Add(new Point3d(2, 1, 0));
                acPts3dPoly.Add(new Point3d(2, 2, 0));

                foreach (Point3d acPt3d in acPts3dPoly)
                {
                    PolylineVertex3d acPolVer3d = new PolylineVertex3d(acPt3d);
                    acPoly3d.AppendVertex(acPolVer3d);
                    acTrans.AddNewlyCreatedDBObject(acPolVer3d, true);
                }
                //获取多段线的顶点坐标
                Point2dCollection acPts2d = new Point2dCollection();
                for (int nCnt = 0; nCnt < acPoly.NumberOfVertices; nCnt++)
                {
                    acPts2d.Add(acPoly.GetPoint2dAt(nCnt));
                }
                //获取3D多段线的顶点坐标
                Point3dCollection acPts3d = new Point3dCollection();
                foreach (ObjectId acObjIdvert in acPoly3d)
                {
                    PolylineVertex3d acPolVer3d;
                    acPolVer3d = acTrans.GetObject(acObjIdvert,
                        OpenMode.ForRead) as PolylineVertex3d;
                    acPts3d.Add(acPolVer3d.Position);
                }
                //显示坐标
                Autodesk.AutoCAD.ApplicationServices.Application.
                    ShowAlertDialog("2D polyline (red): \n" +
                    acPts2d[0].ToString() + "\n" +
                    acPts2d[1].ToString() + "\n" +
                    acPts2d[2].ToString());
                Autodesk.AutoCAD.ApplicationServices.Application.
                    ShowAlertDialog("3D polyline (blue): \n" +
                    acPts3d[0].ToString() + "\n" +
                    acPts3d[1].ToString() + "\n" +
                    acPts3d[2].ToString());
                //提交事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 新创建一个 UCS  并设置为当前坐标系,然后将一个点的坐标转变为 UCS 
        /// </summary>
        [CommandMethod("NewUCS")]
        public static void NewUCS()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开UCSTable
                UcsTable acUCSTbl;
                acUCSTbl = acTrans.GetObject(acCurDb.UcsTableId,
                    OpenMode.ForRead) as UcsTable;

                UcsTableRecord acUCSTblRec;
                //检查UCS表中是否有“New_UCS”这条记录
                if (acUCSTbl.Has("New_UCS") == false)
                {
                    acUCSTblRec = new UcsTableRecord();
                    acUCSTblRec.Name = "New_UCS";
                    //以写模式打开UCSTable
                    acUCSTbl.UpgradeOpen();
                    //往UCSTable添加新纪录
                    acUCSTbl.Add(acUCSTblRec);
                    acTrans.AddNewlyCreatedDBObject(acUCSTblRec, true);
                }
                else
                {
                    acUCSTblRec = acTrans.GetObject(acUCSTbl["New_UCS"],
                        OpenMode.ForWrite) as UcsTableRecord;
                }
                acUCSTblRec.Origin = new Point3d(4, 5, 3);
                acUCSTblRec.XAxis = new Vector3d(1, 0, 0);
                acUCSTblRec.YAxis = new Vector3d(0, 1, 0);
                //打开当前视口
                ViewportTableRecord acVportTblRec;
                acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                    OpenMode.ForWrite) as ViewportTableRecord;
                //在当前视口的原点显示UCS图标
                acVportTblRec.IconAtOrigin = true;
                acVportTblRec.IconEnabled = true;
                //设置UCS为当前坐标系
                acVportTblRec.SetUcs(acUCSTblRec.ObjectId);
                acDoc.Editor.UpdateTiledViewportsFromDatabase();
                //显示当前UCS坐标系的名称
                UcsTableRecord acUCSTblRecActive;
                acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName,
                    OpenMode.ForRead) as UcsTableRecord;

                Autodesk.AutoCAD.ApplicationServices.Application.
                    ShowAlertDialog("The current UCS is: " +
                    acUCSTblRecActive.Name);

                PromptPointResult pPtRes;
                PromptPointOptions pPtOpts = new PromptPointOptions("");
                //提示选取一个点
                pPtOpts.Message = "\nEnter a point: ";
                pPtRes = acDoc.Editor.GetPoint(pPtOpts);

                Point3d pPt3dWCS;
                Point3d pPt3dUCS;

                //将获得的点的坐标转变为当前UCS坐标
                if (pPtRes.Status == PromptStatus.OK)
                {
                    pPt3dWCS = pPtRes.Value;
                    pPt3dUCS = pPtRes.Value;
                    //将该点的当前UCS坐标转变为WCS坐标
                    Matrix3d newMatrix = new Matrix3d();
                    newMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin,
                        Vector3d.XAxis,
                        Vector3d.YAxis,
                        Vector3d.ZAxis,
                        acVportTblRec.Ucs.Origin,
                        acVportTblRec.Ucs.Xaxis,
                        acVportTblRec.Ucs.Yaxis,
                        acVportTblRec.Ucs.Zaxis);
                    pPt3dUCS = pPt3dWCS.TransformBy(newMatrix);

                    Autodesk.AutoCAD.ApplicationServices.Application.
                        ShowAlertDialog("The WCS coordinates are: \n" +
                        pPt3dWCS.ToString() + "\n" +
                        "The UCS coordinates are: \n" +
                        pPt3dUCS.ToString());
                }
                //提交事务
                acTrans.Commit();
            }
        }

        /// <summary>
        /// 将OCS坐标转变为WCS坐标
        /// </summary>
        [CommandMethod("TranslateCoordinates")]
        public static void TranslateCoordinates()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建2D多段线
                Polyline2d acPoly2d = new Polyline2d();
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acPoly2d);
                acTrans.AddNewlyCreatedDBObject(acPoly2d, true);
                //先将多段线添加到块表记录,然后才能给它添加顶点
                Point3dCollection acPts2dPoly = new Point3dCollection();
                acPts2dPoly.Add(new Point3d(1, 1, 0));
                acPts2dPoly.Add(new Point3d(1, 2, 0));
                acPts2dPoly.Add(new Point3d(2, 2, 0));
                acPts2dPoly.Add(new Point3d(3, 2, 0));
                acPts2dPoly.Add(new Point3d(4, 4, 0));

                foreach (Point3d acPt3d in acPts2dPoly)
                {
                    Vertex2d acVer2d = new Vertex2d(acPt3d, 0, 0, 0, 0);
                    acPoly2d.AppendVertex(acVer2d);
                    acTrans.AddNewlyCreatedDBObject(acVer2d, true);
                }
                //设置2D多段线的法线
                acPoly2d.Normal = new Vector3d(0, 1, 2);
                //获取2D多段线的第1个坐标
                Point3dCollection acPts3d = new Point3dCollection();
                Vertex2d acFirstVer = null;
                foreach (ObjectId acObjIdVert in acPoly2d)
                {
                    acFirstVer = acTrans.GetObject(acObjIdVert,
                        OpenMode.ForRead) as Vertex2d;
                    acPts3d.Add(acFirstVer.Position);
                    break;
                }
                //获取2D多段线的第1个顶点
                //Z坐标值用Elevation属性值
                Point3d pFirstVer = new Point3d(acFirstVer.Position.X,
                    acFirstVer.Position.Y,
                    acPoly2d.Elevation);
                //OCS到WCS转换
                Matrix3d mWPlane = Matrix3d.WorldToPlane(acPoly2d.Normal);
                Point3d pWCSPt = pFirstVer.TransformBy(mWPlane);
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("The first vertex has the following " +
                    "coordinates: " +
                    "\nOCS: " + pFirstVer.ToString() +
                    "\nWCS: " + pWCSPt.ToString());
                //提交事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建多边形网格
        /// </summary>
        [CommandMethod("Create3DMesh")]
        public static void Create3DMesh()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建多边形网格
                PolygonMesh acPolyMesh = new PolygonMesh();
                acPolyMesh.MSize = 4;
                acPolyMesh.NSize = 4;
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acPolyMesh);
                acTrans.AddNewlyCreatedDBObject(acPolyMesh, true);
                //添加顶点前,必须先将多边形网格添加到块表记录
                Point3dCollection acPts3dPMesh = new Point3dCollection();
                acPts3dPMesh.Add(new Point3d(0, 0, 0));
                acPts3dPMesh.Add(new Point3d(2, 0, 1));
                acPts3dPMesh.Add(new Point3d(4, 0, 0));
                acPts3dPMesh.Add(new Point3d(6, 0, 1));
                acPts3dPMesh.Add(new Point3d(0, 2, 0));
                acPts3dPMesh.Add(new Point3d(2, 2, 1));
                acPts3dPMesh.Add(new Point3d(4, 2, 0));
                acPts3dPMesh.Add(new Point3d(6, 2, 1));
                acPts3dPMesh.Add(new Point3d(0, 4, 0));
                acPts3dPMesh.Add(new Point3d(2, 4, 1));
                acPts3dPMesh.Add(new Point3d(4, 4, 0));
                acPts3dPMesh.Add(new Point3d(6, 4, 0));
                acPts3dPMesh.Add(new Point3d(0, 6, 0));
                acPts3dPMesh.Add(new Point3d(2, 6, 1));
                acPts3dPMesh.Add(new Point3d(4, 6, 0));
                acPts3dPMesh.Add(new Point3d(6, 6, 0));

                foreach (Point3d acPt3d in acPts3dPMesh)
                {
                    PolygonMeshVertex acPMeshVer = new PolygonMeshVertex(acPt3d);
                    acPolyMesh.AppendVertex(acPMeshVer);
                    acTrans.AddNewlyCreatedDBObject(acPMeshVer, true);
                }
                //打开当前视口
                ViewportTableRecord acVportTblRec;
                acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                    OpenMode.ForWrite) as ViewportTableRecord;
                //旋转当前视口的观察方向
                acVportTblRec.ViewDirection = new Vector3d(-1, -1, 1);
                acDoc.Editor.UpdateTiledViewportsFromDatabase();
                //提交事务
                acTrans.Commit();
            }
        }


        public static void CreatePolyfaceMesh()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建多面网格
                PolyFaceMesh acPFaceMesh = new PolyFaceMesh();
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acPFaceMesh);
                acTrans.AddNewlyCreatedDBObject(acPFaceMesh, true);
                //添加顶点前,必须先将多边形网格添加到块表记录
                Point3dCollection acPts3dPFMesh = new Point3dCollection();
                acPts3dPFMesh.Add(new Point3d(4, 7, 0));
                acPts3dPFMesh.Add(new Point3d(5, 7, 0));
                acPts3dPFMesh.Add(new Point3d(6, 7, 0));

                acPts3dPFMesh.Add(new Point3d(4, 6, 0));
                acPts3dPFMesh.Add(new Point3d(5, 6, 0));
                acPts3dPFMesh.Add(new Point3d(6, 6, 1));

                foreach (Point3d acPt3d in acPts3dPFMesh)
                {
                    PolyFaceMeshVertex acPMeshVer = new PolyFaceMeshVertex(acPt3d);
                    acPFaceMesh.AppendVertex(acPMeshVer);
                    acTrans.AddNewlyCreatedDBObject(acPMeshVer, true);
                }
                FaceRecord acFaceRec1 = new FaceRecord(1, 2, 3, 4);
                acPFaceMesh.AppendFaceRecord(acFaceRec1);
                acTrans.AddNewlyCreatedDBObject(acFaceRec1, true);

                FaceRecord acFaceRec2 = new FaceRecord(2, 3, 6, 5);
                acFaceRec2.MakeEdgeInvisibleAt(1);//使第2个顶点开始的表示不可见
                acPFaceMesh.AppendFaceRecord(acFaceRec2);
                acTrans.AddNewlyCreatedDBObject(acFaceRec2, true);
                //打开当前视口
                ViewportTableRecord acVportTblRec;
                acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                    OpenMode.ForWrite) as ViewportTableRecord;
                acDoc.Editor.UpdateTiledViewportsFromDatabase();
                //提交事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建一个楔形实体
        /// </summary>
        [CommandMethod("CreateWedge")]
        public static void CreateWedge()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建3D楔形实体wedge
                Solid3d acSol3D = new Solid3d();
                acSol3D.CreateWedge(10, 15, 20);
                //3D实体的中心点放在(5,5,0)
                acSol3D.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) - Point3d.Origin));
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3D);
                acTrans.AddNewlyCreatedDBObject(acSol3D, true);
                //打开当前视口
                ViewportTableRecord acVportTblRec;
                acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                    OpenMode.ForWrite) as ViewportTableRecord;
                //旋转当前视口的观察方向
                acVportTblRec.ViewDirection = new Vector3d(-1, -1, 1);
                acDoc.Editor.UpdateTiledViewportsFromDatabase();
                //提交事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建一个3D旋转对象
        /// </summary>
        [CommandMethod("Rotate_3DBox")]
        public static void Rotate_3DBox()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建一个3D实体箱子
                Solid3d acSol3D = new Solid3d();
                acSol3D.CreateBox(5, 7, 10);
                //3D实体的中心点放在(5,5,0)
                acSol3D.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
                    Point3d.Origin));
                Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
                CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
                //将3D箱体绕点 (-3,4,0)和点(-3,-4,0)定义的轴旋转30度
                Vector3d vRot = new Point3d(-3, 4, 0).GetVectorTo(new Point3d(-3, -4, 0));
                acSol3D.TransformBy(Matrix3d.Rotation(0.5226, vRot, new Point3d(-3, 4, 0)));
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3D);
                acTrans.AddNewlyCreatedDBObject(acSol3D, true);
                //提交事务
                acTrans.Commit();
            }
        }


        static Point2d Polarpoints(Point2d pPt, double dAng, double dDist)
        {
            return new Point2d(pPt.X + dDist * Math.Cos(dAng),
                pPt.Y + dDist * Math.Sin(dAng));
        }
        /// <summary>
        /// 创建 3D  矩形阵列
        /// </summary>
        [CommandMethod("CreateRectangular3DArray")]
        public static void CreateRectangular3DArray()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建圆,圆心(2,2,0),半径0.5
                Circle acCirc = new Circle();
                acCirc.Center = new Point3d(2, 2, 0);
                acCirc.Radius = 0.5;
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acCirc);
                acTrans.AddNewlyCreatedDBObject(acCirc, true);
                //创建4行4列3层的矩形阵列
                int nRows = 4;
                int nColumns = 4;
                int nLevels = 3;
                //设置行、列、层3个方向的偏移量及阵列基角
                double dRowOffset = 1;
                double dColumnOffset = 1;
                double dLevelsOffset = 1;
                double dArrayAng = 0;
                //获取当前UCS坐标系X轴的角度
                Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
                CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
                Vector2d acVec2dAng = new Vector2d(curUCS.Xaxis.X, curUCS.Xaxis.Y);
                //如果UCS被旋转了,相应地调整阵列角度
                dArrayAng = dArrayAng + acVec2dAng.Angle;
                //使用对象界限的左上角作为阵列基点
                Extents3d acExts = acCirc.Bounds.GetValueOrDefault();
                Point2d acPt2dArrayBase = new Point2d(acExts.MinPoint.X,
                    acExts.MaxPoint.Y);
                //跟踪为每列创建的对象
                DBObjectCollection acDBObjCollCols = new DBObjectCollection();
                acDBObjCollCols.Add(acCirc);
                //创建首列对象
                int nColumnsCount = 1;
                while (nColumns > nColumnsCount)
                {
                    Entity acEntClone = acCirc.Clone() as Entity;
                    acDBObjCollCols.Add(acEntClone);
                    //给复制的对象计算新位置
                    Point2d acPt2dTo = PolarPoints(acPt2dArrayBase, dArrayAng, dColumnOffset * nColumnsCount);

                    Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
                    Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
                    acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));

                    acBlkTblRec.AppendEntity(acEntClone);
                    acTrans.AddNewlyCreatedDBObject(acEntClone, true);

                    nColumnsCount = nColumnsCount + 1;
                }
                //90度的弧度值
                double dAng = 1.5708;
                //跟踪每行和列中创建的对象
                DBObjectCollection acDBObjCollLvls = new DBObjectCollection();

                foreach (DBObject acObj in acDBObjCollCols)
                {
                    acDBObjCollLvls.Add(acObj);
                }
                //创建每行对象
                foreach (Entity acEnt in acDBObjCollCols)
                {
                    int nRowsCount = 1;
                    while (nRows > nRowsCount)
                    {
                        Entity acEntClone = acEnt.Clone() as Entity;
                        acDBObjCollLvls.Add(acEntClone);
                        //给复制的对象计算新位置
                        Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
                            dArrayAng + dAng,
                            dRowOffset * nRowsCount);

                        Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
                        Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
                        acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));

                        acBlkTblRec.AppendEntity(acEntClone);
                        acTrans.AddNewlyCreatedDBObject(acEntClone, true);

                        nRowsCount = nRowsCount + 1;
                    }
                }
                //创建3D阵列的层
                foreach (Entity acEnt in acDBObjCollLvls)
                {
                    int nLvlsCount = 1;
                    while (nLevels > nLvlsCount)
                    {
                        Entity acEntClone = acEnt.Clone() as Entity;
                        Vector3d acVec3d = new Vector3d(0, 0, dLevelsOffset * nLvlsCount);
                        acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));

                        acBlkTblRec.AppendEntity(acEntClone);
                        acTrans.AddNewlyCreatedDBObject(acEntClone, true);
                        nLvlsCount = nLvlsCount + 1;
                    }
                }
                //提交事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 3D镜像
        /// </summary>
        [CommandMethod("MirrorABoc3D")]
        public static void MirrorABox3D()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建3D箱体
                Solid3d acSol3D = new Solid3d();
                acSol3D.CreateBox(5, 7, 10);
                //3D实体的中心点放在(5,5,0)
                acSol3D.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
                    Point3d.Origin));
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3D);
                acTrans.AddNewlyCreatedDBObject(acSol3D, true);
                //创建原3D箱体的拷贝并修改颜色
                Solid3d acSol3DCopy = acSol3D.Clone() as Solid3d;
                acSol3DCopy.ColorIndex = 1;
                //定义镜像平面
                Plane acPlane = new Plane(new Point3d(1.25, 0, 0),
                    new Point3d(1.25, 2, 0),
                    new Point3d(1.25, 2, 2));
                //沿平面镜像3D实体
                acSol3DCopy.TransformBy(Matrix3d.Mirroring(acPlane));
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3DCopy);
                acTrans.AddNewlyCreatedDBObject(acSol3DCopy, true);
                //提交事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 查找两实体间的干涉(interference )
        /// </summary>
        [CommandMethod("FindInterferenceBetweenSolids")]
        public static void FindInterferenceBetweenSolids()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建3D箱体
                Solid3d acSol3DBox = new Solid3d();
                acSol3DBox.CreateBox(5, 7, 10);
                acSol3DBox.ColorIndex = 7;
                //3D实体中心点放在(5,5,0)
                acSol3DBox.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
                    Point3d.Origin));
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3DBox);
                acTrans.AddNewlyCreatedDBObject(acSol3DBox, true);
                //创建3D圆柱体
                //默认构造函数的中心点为(0,0,0),这样就不用在移动了
                Solid3d acSol3DCyl = new Solid3d();
                acSol3DCyl.CreateFrustum(20, 5, 5, 5);
                acSol3DCyl.ColorIndex = 4;
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3DCyl);
                acTrans.AddNewlyCreatedDBObject(acSol3DCyl, true);
                //用箱体和圆柱体的干涉创建一个3D实体
                Solid3d acSol3DCopy = acSol3DCyl.Clone() as Solid3d;
                //检查箱体和圆柱体是否有重叠部分
                if (acSol3DCopy.CheckInterference(acSol3DBox) == true)
                {
                    acSol3DCopy.BooleanOperation(BooleanOperationType.BoolIntersect,
                        acSol3DBox.Clone() as Solid3d);
                    acSol3DCopy.ColorIndex = 1;
                }
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3DCopy);
                acTrans.AddNewlyCreatedDBObject(acSol3DCopy, true);
                //提交事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 将一个实体切割成两个实体
        /// </summary>
        [CommandMethod("SliceABox")]
        public static void SliceABox()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建3D箱体
                Solid3d acSol3D = new Solid3d();
                acSol3D.CreateBox(5, 7, 10);
                acSol3D.ColorIndex = 7;
                //3D实体的中心点放在(5,5,0)
                acSol3D.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
                    Point3d.Origin));
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3D);
                acTrans.AddNewlyCreatedDBObject(acSol3D, true);
                //定义镜像平面
                Plane acPlane = new Plane(new Point3d(1.5, 7.5, 0),
                    new Point3d(1.5, 7.5, 10),
                    new Point3d(8.5, 2.5, 10));
                Solid3d acSol3DSlice = acSol3D.Slice(acPlane, true);
                acSol3DSlice.ColorIndex = 1;
                //将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3DSlice);
                acTrans.AddNewlyCreatedDBObject(acSol3DSlice, true);
                //提交事务
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 查询和设置视图
        /// </summary>
        [CommandMethod("ChangePlotSetting")]
        public static void ChangePlotSetting()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //引用布局管理器LayoutManager
                LayoutManager acLayoutMgr;
                acLayoutMgr = LayoutManager.Current;
                //读取当前布局,在命令行窗口显示布局名
                Layout acLayout;
                acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
                    OpenMode.ForRead) as Layout;
                //输出当前布局名和设备名
                acDoc.Editor.WriteMessage("\nCurrent layout: " +
                    acLayout.LayoutName);
                acDoc.Editor.WriteMessage("\nCurrent device name: " +
                    acLayout.PlotConfigurationName);
                //从布局中获取PlotInfo
                PlotInfo acPlInfo = new PlotInfo();
                acPlInfo.Layout = acLayout.ObjectId;
                //复制布局中的PlotSettings
                PlotSettings acPlSet = new PlotSettings(acLayout.ModelType);
                acPlSet.CopyFrom(acLayout);
                //更新PlotSettings对象的PlotConfigurationName属性
                PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
                acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3",
                    "ANSI_A_(8.50_x_11.00_Inches)");
                //更新布局
                acLayout.UpgradeOpen();
                acLayout.CopyFrom(acPlSet);
                //输出已更新的布局设备名
                acDoc.Editor.WriteMessage("\nNew device name: " +
                    acLayout.PlotConfigurationName);
                //将新对象保存到数据库
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 在模型空间与图纸空间间转换
        /// </summary>
        [CommandMethod("ToggleSpace")]
        public static void ToggleSpace()
        {
            //获取当前文档
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            //获取系统变量CVPORT和TILEMODE的当前值
            object oCvports = Autodesk.AutoCAD.ApplicationServices.Application.
                GetSystemVariable("CVPORT");
            object oTilemode = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("TILEMODE");
            //检查Mode布局是否为活动的(TILEMODE=1为活动)
            if (System.Convert.ToInt16(oTilemode) == 0)
            {
                //检查Model空间是否为活动的(CVPORT=2)
                //CVPORT is 2 if Model space is active
                if (System.Convert.ToInt16(oCvports) == 2)
                {
                    acDoc.Editor.SwitchToPaperSpace();
                }
                else
                {
                    acDoc.Editor.SwitchToModelSpace();
                }
            }
            else
            {
                //切换到Paper空间布局
                Autodesk.AutoCAD.ApplicationServices.Application.
                    SetSystemVariable("TILEMODE", 0);
            }
        }


        /// <summary>
        /// 创建并激活浮动窗口
        /// </summary>
        //导入ObjectARX全局函数acedSetCurrentVPort
        //对2014版,该函数在accore.dll内
        [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
            EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@PBVAcDbViewport@@@Z")]
        extern static private int acedSetCurrentVPort(IntPtr AcDbVport);
        [CommandMethod("CreateFloatingViewport")]
        public static void CreateFloatingViewport()
        {
            //获取当前文档和数据库,并启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录Paper空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //切换到Paper空间布局
                Autodesk.AutoCAD.ApplicationServices.Application.
                    SetSystemVariable("TILEMODE", 0);
                acDoc.Editor.SwitchToPaperSpace();
                //创建一个视口
                Autodesk.AutoCAD.DatabaseServices.Viewport acVport = new Autodesk.AutoCAD.DatabaseServices.Viewport();
                acVport.CenterPoint = new Point3d(3.25, 3, 0);
                acVport.Width = 6;
                acVport.Height = 5;
                //添加新对象到块表记录和事务
                acBlkTblRec.AppendEntity(acVport);
                acTrans.AddNewlyCreatedDBObject(acVport, true);
                //修改观察方向
                acVport.ViewDirection = new Vector3d(1, 1, 1);
                //激活视口
                acVport.On = true;
                //激活视口的模型空间
                acDoc.Editor.SwitchToModelSpace();
                //使用导入的ObjectARX函数设置新视口为当前视口
                acedSetCurrentVPort(acVport.UnmanagedObject);
                //将对象保存到数据库
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 创建四个浮动视口
        /// </summary>
        [CommandMethod("FourFloatingViewports")]
        public static void FourFloatingViewports()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开块表记录Paper空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //切换到Paper空间布局
                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("TILEMODE", 0);
                acDoc.Editor.SwitchToPaperSpace();

                Point3dCollection acPt3dCol = new Point3dCollection();
                acPt3dCol.Add(new Point3d(2.5, 5.5, 0));
                acPt3dCol.Add(new Point3d(2.5, 2.5, 0));
                acPt3dCol.Add(new Point3d(5.5, 5.5, 0));
                acPt3dCol.Add(new Point3d(5.5, 2.5, 0));

                Vector3dCollection acVec3dCol = new Vector3dCollection();
                acVec3dCol.Add(new Vector3d(0, 0, 1));
                acVec3dCol.Add(new Vector3d(0, 1, 0));
                acVec3dCol.Add(new Vector3d(1, 0, 0));
                acVec3dCol.Add(new Vector3d(1, 1, 1));

                double dWidth = 2.5;
                double dHeight = 2.5;

                Autodesk.AutoCAD.DatabaseServices.Viewport acVportLast = null;
                int nCnt = 0;
                foreach(Point3d acPt3d in acPt3dCol)
                {
                    Autodesk.AutoCAD.DatabaseServices.Viewport acVport = new Autodesk.AutoCAD.DatabaseServices.Viewport();
                    acVport.CenterPoint = acPt3d;
                    acVport.Width = dWidth;
                    acVport.Height = dHeight;
                    //添加新对象到块表记录和事务
                    acBlkTblRec.AppendEntity(acVport);
                    acTrans.AddNewlyCreatedDBObject(acVport, true);
                    //修改观察方向
                    acVport.ViewDirection = acVec3dCol[nCnt];
                    //激活视口
                    acVport.On = true;
                    //记录创建的最后视口
                    acVportLast = acVport;
                    //计数器加1
                    nCnt = nCnt + 1;
                }
                if (acVportLast != null)
                {
                    //激活视口的模型空间
                    acDoc.Editor.SwitchToModelSpace();
                    //使用导入的ObjectARX函数设置新视口为当前视口
                    acedSetCurrentVPort(acVportLast.UnmanagedObject);
                }
                //将新对象保存到数据库
                acTrans.Commit();
            }
        }


        /// <summary>
        /// 打印模型布局的范围
        /// </summary>
        [CommandMethod("PlotCurrentLayout")]
        public static void PlotCurrentLayout()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //引用布局管理器LayoutManager
                LayoutManager acLayoutMgr;
                acLayoutMgr = LayoutManager.Current;
                //获取当前布局,在命令行窗口显示布局名字
                Layout acLayout;
                acLayout =
                    acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
                    OpenMode.ForRead) as Layout;
                //从布局中获取PlotInfo
                PlotInfo acPlInfo = new PlotInfo();
                acPlInfo.Layout = acLayout.ObjectId;
                //复制布局中的PlotSettings
                PlotSettings acPlSet = new PlotSettings(acLayout.ModelType);
                acPlSet.CopyFrom(acLayout);
                //更新PlotSettings对象
                PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
                //设置打印区域
                acPlSetVdr.SetPlotType(acPlSet,
                    Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
                //设置打印比例
                acPlSetVdr.SetUseStandardScale(acPlSet, true);
                acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
                //居中打印
                acPlSetVdr.SetPlotCentered(acPlSet, true);
                //设置使用的打印设备
                acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3",
                    "ANSI_A_(8.50_x_11.00_Inches)");
                //用上述设置信息覆盖PlotInfo对象
                //不会将修改保存回布局
                acPlInfo.OverrideSettings = acPlSet;
                //验证打印信息
                PlotInfoValidator acPlInfoVdr = new PlotInfoValidator();
                acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                acPlInfoVdr.Validate(acPlInfo);
                //检查是否有正在处理的打印任务
                if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
                {
                    using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
                    {
                        //使用PlotProgressDialog对话框跟踪打印进度
                        PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false,
                            1,
                            true);
                        using (acPlProgDlg)
                        {
                            //定义打印开始时显示的状态信息
                            acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle,
                                "Plot Progress");
                            acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage,
                                "Cancel Job");
                            acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage,
                                "Cancel Sheet");
                            acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption,
                                "Sheet Progress");
                            //设置打印进度范围
                            acPlProgDlg.LowerPlotProgressRange = 0;
                            acPlProgDlg.UpperPlotProgressRange = 100;
                            acPlProgDlg.PlotProgressPos = 0;
                            //显示打印进度对话框
                            acPlProgDlg.OnBeginPlot();
                            acPlProgDlg.IsVisible = true;
                            //开始打印
                            acPlEng.BeginPlot(acPlProgDlg, null);
                            //定义打印输出
                            acPlEng.BeginDocument(acPlInfo,
                                acDoc.Name,
                                null,
                                1,
                                true,
                                "e:\\myplot");
                            //显示当前打印任务的有关信息
                            acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status,
                                "Plotting: " + acDoc.Name + "-" + acLayout.LayoutName);
                            //设置图纸进度范围
                            acPlProgDlg.OnBeginSheet();
                            acPlProgDlg.LowerSheetProgressRange = 0;
                            acPlProgDlg.UpperSheetProgressRange = 100;
                            acPlProgDlg.SheetProgressPos = 0;
                            //打印第一张图/布局
                            PlotPageInfo acPlPageInfo = new PlotPageInfo();
                            acPlEng.BeginPage(acPlPageInfo,
                                acPlInfo,
                                true,
                                null);
                            acPlEng.BeginGenerateGraphics(null);
                            acPlEng.EndGenerateGraphics(null);
                            //结束第一张图/布局的打印
                            acPlEng.EndPage(null);
                            acPlProgDlg.SheetProgressPos = 100;
                            acPlProgDlg.OnEndSheet();
                            //结束文档局的打印
                            acPlEng.EndDocument(null);
                            //打印结束
                            acPlProgDlg.PlotProgressPos = 100;
                            acPlProgDlg.OnEndPlot();
                            acPlEng.EndPlot(null);
                        }
                    }
                }
            }
        }




        /*使用事件*/



        /// <summary>
        /// 激活一个 AutoCAD  对象事件
        /// </summary>
        [CommandMethod("AddAppEvent")]
        public void AddAppEvent()
        {
            Autodesk.AutoCAD.ApplicationServices.Application.
                SystemVariableChanged += new Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventHandler(appSysVarChanged);
        }
        [CommandMethod("RemoveAppEvent")]
        public void RemoveAppEvent()
        {
            Autodesk.AutoCAD.ApplicationServices.Application.
                SystemVariableChanged -=
                new Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventHandler(appSysVarChanged);
        }
        public void appSysVarChanged(object senderObj,
            Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs sysVarChEvtArgs)
        {
            object oVal = Autodesk.AutoCAD.ApplicationServices.Application.
                GetSystemVariable(sysVarChEvtArgs.Name);
            //弹出消息框,显示系统变量的名称及新值
            Autodesk.AutoCAD.ApplicationServices.Application.
                ShowAlertDialog(sysVarChEvtArgs.Name + " was changed." +
                "\nNew value: " + oVal.ToString());
        }


        /// <summary>
        /// 激活一个 Document  对象事件
        /// </summary>
        [CommandMethod("AddDocEvent")]
        public void AddDocEvent()
        {
            //获取当前文档
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            acDoc.BeginDocumentClose +=
                new DocumentBeginCloseEventHandler(docBeginDocClose);
        }
        [CommandMethod("RemoveDocEvent")]
        public void RemoveDocEvent()
        {
            //获取当前文档
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            acDoc.BeginDocumentClose -=
                new DocumentBeginCloseEventHandler(docBeginDocClose);
        }
        public void docBeginDocClose(object senderObj,
            DocumentBeginCloseEventArgs docBegClsEvtArgs)
        {
            //显示消息框提示是否继续关闭文档
            if(System.Windows.Forms.MessageBox.Show("The document is about to be closed."+
                "\nDo you want to continue?",
                "Close Document",
                System.Windows.Forms.MessageBoxButtons.YesNo)==
                System.Windows.Forms.DialogResult.No)
            {
                docBegClsEvtArgs.Veto();
            }
        }


        /// <summary>
        /// 激活一个 DocumentCollection  对象事件
        /// </summary>
        [CommandMethod("AddDocColEvent")]
        public void AddDocColEvent()
        {
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.
                DocumentActivated += new DocumentCollectionEventHandler(docColDocAct);
        }
        [CommandMethod("RemoveDocColEvent")]
        public void RemoveDocColEvent()
        {
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentActivated -=
                new DocumentCollectionEventHandler(docColDocAct);
        }
        public void docColDocAct(object senderObj,
            DocumentCollectionEventArgs docColDocActEvtArgs)
        {
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(docColDocActEvtArgs.Document.Name +
                " was activated.");
        }



        //Polyline对象全局变量
        Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = null;
        /// <summary>
        /// 激活Object事件
        /// </summary>
        [CommandMethod("AddPlObjEvent")]
        public void AddPlObjEvent()
        {
            //获取当前文档和数据库,启动事务
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
                DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读模式打开BlockTable
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                    OpenMode.ForRead) as BlockTable;
                //以写模式打开BlockTable记录Model空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                //创建闭合多段线
                acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                acPoly.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
                acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
                acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
                acPoly.AddVertexAt(3, new Point2d(3, 3), 0, 0, 0);
                acPoly.AddVertexAt(4, new Point2d(3, 2), 0, 0, 0);
                acPoly.Closed = true;
                //添加新对象到块表记录及事务
                acBlkTblRec.AppendEntity(acPoly);
                acTrans.AddNewlyCreatedDBObject(acPoly, true);

                acPoly.Modified += new EventHandler(acPolyMod);
                //保存新对象到数据库
                acTrans.Commit();
            }
        }
        [CommandMethod("RemovePlObjEvent")]
        public void RemovePlObjEvent()
        {
            if (acPoly != null)
            {
                //获取当前文档和数据库,启动事务
                Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.
                    MdiActiveDocument;
                Database acCurDb = acDoc.Database;
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    //以读模式打开多段线
                    acPoly = acTrans.GetObject(acPoly.ObjectId,
                        OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Polyline;

                    if (acPoly.IsWriteEnabled == false)
                    {
                        acPoly.UpgradeOpen();
                    }
                    acPoly.Modified -= new EventHandler(acPolyMod);
                    acPoly = null;
                }
            }
        }
        public void acPolyMod(object senderObj, EventArgs evtArgs)
        {
            Autodesk.AutoCAD.ApplicationServices.Application.
                ShowAlertDialog("The area of " +
                acPoly.ToString() + " is: " +
                acPoly.Area);
        }


        //定义一个全局变量,用于AddCOMEvent命令和RemoveCOMEvent命令
        AcadApplication acAppCom;
        [CommandMethod("AddCOMEvent")]
        public void AddCOMEvent()
        {
            //用全局变量保存到应用程序的引用
            //并注册COM事件BeginFileDrop
            acAppCom = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as AcadApplication;
            acAppCom.BeginFileDrop += new
                _DAcadApplicationEvents_BeginFileDropEventHandler(appComBeginFileDrop);
        }
        [CommandMethod("RemoveCOMEvent")]
        public void RemoveCOMEvent()
        {
            //撤销注册的COM事件处理程序
            acAppCom.BeginFileDrop -=
                new
                _DAcadApplicationEvents_BeginFileDropEventHandler(appComBeginFileDrop);
            acAppCom = null;
        }
        public void appComBeginFileDrop(string strFileName, ref bool bCancel)
        {
            //显示消息框,提示是否继续插入DWG文件
            if (System.Windows.Forms.MessageBox.Show("AutoCAD is about to load " +
                strFileName +
                "\nDo you want to continue loading this file?",
                "DWG File Dropped",
                System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
            {
                bCancel = true;
            }
        }
        #endregion
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值