1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > cad二次开发-C#-VS-镜像-判断用户当前是否已选择实体-将实体对象添加到模型空间-添加

cad二次开发-C#-VS-镜像-判断用户当前是否已选择实体-将实体对象添加到模型空间-添加

时间:2022-11-01 16:54:54

相关推荐

cad二次开发-C#-VS-镜像-判断用户当前是否已选择实体-将实体对象添加到模型空间-添加

创建新图层

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);}}mit();}}

镜像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;}}

镜像图形

/// <summary>/// 镜像图形/// </summary>/// <param name="ent">图形对象的ObjectId</param>/// <param name="point1">第一个镜像点</param>/// <param name="point2">第二个镜像点</param>/// <param name="isEraseSource">是否删除原图形</param>/// <returns>返回新的图形对象 没有加入图形数据库的情况</returns>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);//数据库对象可读mit();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);mit();}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);mit();}}}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("该实体为空!", "提示");}*/}}// 保存新对象到数据库mit();}}}}

cad二次开发-C#-VS-镜像-判断用户当前是否已选择实体-将实体对象添加到模型空间-添加实体到块表记录-将块定义插入到模型空间

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。