CAD二次开发c#.Net编程 - 工具类集合

创建新图层

public static void CreateLayer(string LayerName)
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;//获取当前数据库
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读的方式打开图层表
                LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
                string sLayerName = LayerName;//设定一个图层名称
                if (acLyrTbl.Has(sLayerName) == false)
                {
                    using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
                    {
                        acLyrTblRec.Name = sLayerName;// 指定图层名称
                        acLyrTbl.UpgradeOpen();//修改图层打开方式为写
                        acLyrTbl.Add(acLyrTblRec);//将新图层追加到图层表
                        acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
                    }
                }
                acTrans.Commit();
            }
        }

镜像CAD实体对象

 public static Entity MirrorEntity(this ObjectId entId, Point3d point1, Point3d point2, bool isEraseSource)
        {
            // 声明一个图形对象用于返回
            Entity entR;
            // 计算镜像的变换矩阵
            Matrix3d mt = Matrix3d.Mirroring(new Line3d(point1, point2));
            // 打开事务处理
            using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
            {
                // 判断是否删除原对象
                if (isEraseSource)
                {
                    // 打开原对象
                    Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
                    // 执行变换
                    ent.TransformBy(mt);
                    entR = ent;
                }
                else
                {

                    // 打开原对象
                    Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForRead);
                    entR = ent.GetTransformedCopy(mt);
                }
                return entR;
            }
        }
        /// 
        /// 镜像图形
        /// 
        /// 图形对象的ObjectId
        /// 第一个镜像点
        /// 第二个镜像点
        /// 是否删除原图形
        /// 返回新的图形对象  没有加入图形数据库的情况
        public static Entity MirrorEntity(this Entity ent, Point3d point1, Point3d point2, bool isEraseSource)
        {

            // 声明一个图形对象用于返回
            Entity entR;

            if (ent.IsNewObject == true)
            {
                // 计算镜像的变换矩阵
                Matrix3d mt = Matrix3d.Mirroring(new Line3d(point1, point2));
                entR = ent.GetTransformedCopy(mt);
            }
            else
            {
                entR = ent.ObjectId.MirrorEntity(point1, point2, isEraseSource);
            }
            return entR;
        }

CAD ObjectId转换为实体对象

        public static DBObject GetDBObject(ObjectId id)
        {
            Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
            _ = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction trans = doc.TransactionManager.StartTransaction())
            {
                DBObject pline = trans.GetObject(id, OpenMode.ForRead);//数据库对象可读
                trans.Commit();
                return pline;
            }
        }

弧度值转角度值

public static double RadianToDegree(this double angle)
        {
            return angle * (180.0 / Math.PI);
        }

角度值转弧度值

 public static double DegreeToRadian(this double angle)
        {
            return angle * (Math.PI / 180.0);
        }

将实体对象添加到模型空间

public static ObjectId AddToModelSpace(this Database db, Entity ent)
        {
            ObjectId entId;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                entId = btr.AppendEntity(ent);
                tr.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }
            return entId;
        }

添加实体到块表记录

 public static ObjectId AddBlockTableRecord(this Database db, string blockName, List<Entity> ents)
        {
            DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
            BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
            if (!bt.Has(blockName))
            {
                //创建一个blocktablerecord类的对象,表示要创建的块
                BlockTableRecord btr = new BlockTableRecord();
                btr.Name = blockName;//设置块名
                //将列表中的实体加入到新建的blocktablerecord对象中
                ents.ForEach(ent => btr.AppendEntity(ent));
                bt.UpgradeOpen();
                bt.Add(btr);
                db.TransactionManager.AddNewlyCreatedDBObject(btr, true);
                bt.DowngradeOpen();
            }
            m_DocumentLock.Dispose();
            return bt[blockName];
        }
        public static ObjectId AddBlockTableRecord(this Database db, string blockName, params Entity[] ents)
        {
            return AddBlockTableRecord(db, blockName, ents.ToList());
        }

将块定义插入到模型空间

public static ObjectId InsertBlockReference(this ObjectId spaceId, string layer, string blockName, Point3d position, double rotateAngle)
        {
            ObjectId blockRefId;
            Database db = spaceId.Database;//获取数据库对象
            BlockTable bt = (BlockTable)db.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
            if (!bt.Has(blockName)) return ObjectId.Null;
            DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
            BlockTableRecord space = (BlockTableRecord)spaceId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);

            //创建块参照并设置插入点
            BlockReference br = new BlockReference(position, bt[blockName]);
            //br.ScaleFactors = sacle;
            br.Layer = layer;
            br.Rotation = rotateAngle;
            blockRefId = space.AppendEntity(br);
            //通知事务处理
            db.TransactionManager.AddNewlyCreatedDBObject(br, true);
            space.DowngradeOpen();
            m_DocumentLock.Dispose();
            return blockRefId;
        }

删除CAD模型空间内当前实体对象

 public static bool DelEntity(ObjectId id)
        {
            try
            {

                if (!id.IsNull)
                {

                    using (Database db = HostApplicationServices.WorkingDatabase)
                    {

                        using (Transaction trans = db.TransactionManager.StartTransaction())
                        {

                            Entity entity = (Entity)trans.GetObject(id, OpenMode.ForWrite, true);

                            entity.Erase(true);

                            trans.Commit();
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            catch
            {

                return false;
            }
            return true;
        }

判断用户当前是否已选择实体

private static void IsSelected()
        {
            DocumentCollection acDocMgr = Application.DocumentManager;
            //激活的文档
            Document acDoc = acDocMgr.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (acDoc.LockDocument())
            {
                // 启动事务
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    // 请求在图形区域选择对象
                    PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();

                    // 如果提示状态OK,表示已选择对象
                    if (acSSPrompt.Status == PromptStatus.OK)
                    {
                        SelectionSet acSSet = acSSPrompt.Value;
                        //遍历选择集内的对象
                        foreach (SelectedObject acSSObj in acSSet)
                        {
                            // 确认返回的是合法的SelectedObject对象
                            if (acSSObj != null)
                            {
                                //获取实体
                                //Entity acEnt = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite) as Entity;
                                /*if (acEnt != null)
                                {
                                    //具体的逻辑代码
                                }
                                else
                                {
                                    MessageBox.Show("该实体为空!", "提示");
                                }*/
                            }
                        }
                        // 保存新对象到数据库
                        acTrans.Commit();
                    }
                }
            }
        }

转载请注明转载地址!CAD二次开发c#.Net编程 - 工具类集合_第1张图片

你可能感兴趣的:(CAD,c#,c#)