1、块定义与块参照两个概念
块定义类似于模具,而块参照类似于模具浇筑出来的模型,在图形中只需用块定义来保存块的实际几何组成,而仅用插入点和比例因子来存储块定义,因为块参照的几何形状与快参照完全一样,仅大小不同,
好处有:
不需要存储款块参照中的实体,减小了图形文件;
只需要修改块定义,块参照会自动更新,减轻了修改和维护工作。
2 、实例代码
创建一个块并添加到数据库中
/// <summary> /// 插入一个块参照到CAD图形中 /// </summary> /// <param name="spaceId">模型空间Id</param> /// <param name="layer">要插入的图层名</param> /// <param name="blockName">块名</param> /// <param name="postion">插入点</param> /// <param name="scale">缩放比例</param> /// <param name="rotateAngle">旋转角度</param> /// <returns></returns> public static ObjectId InsertBlockTableRecord(this ObjectId spaceId, string layer, string blockName, Point3d postion, Scale3d scale, double rotateAngle) { ObjectId blockRefId;//存储要插入的块参照的Id Database db = spaceId.Database;//获取数据库对象 BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; if (!bt.Has(blockName)) return ObjectId.Null;//如果没有blockName的块,程序返回 //以写的方式打开空间 BlockTableRecord space = spaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord; //创建块参照的插入点 BlockReference br = new BlockReference(postion, bt[blockName]); br.ScaleFactors = scale; br.Layer = layer; br.Rotation = rotateAngle; blockRefId = space.AppendEntity(br); db.TransactionManager.AddNewlyCreatedDBObject(br, true); space.DowngradeOpen(); return blockRefId;//返回添加的块参照的ID }
在CAD中插入一个块参照
/// <summary> /// 创建一个块,添加到数据库中 /// </summary> /// <param name="db"></param> /// <param name="blockName"></param> /// <param name="ents"></param> /// <returns></returns> public static ObjectId AddBlockTableRecord(this Database db, string blockName, List<Entity> ents) { BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead); if (!bt.Has(blockName)) { 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();//切换块为读的状态 } return bt[blockName]; }
时间: 2024-10-10 17:36:06