/// <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 }

  

        /// <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];
}

  

05-22 01:26