开始往cad数据库中添加实体了。
一、添加实体到模型空间【图纸空间类似】
// 加入实体到模型空间
public static ObjectId Append(Entity ent, Database db)
{
ObjectId id;
using(Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord modeSpce = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
id = modeSpce.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
tr.Commit();
}
return id;
}
// 为了避免重复打开数据库,这里就单独列出数组形式重载
public static ObjectIdCollection Append(DBObjectCollection ents, Database db)
{
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord modeSpce = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
foreach (DBObject obj in ents)
{
Entity ent = obj as Entity;
if (null != ent)
{
ids.Add(modeSpce.AppendEntity(ent));
tr.AddNewlyCreatedDBObject(ent, true);
}
}
tr.Commit();
}
return ids;
}
二、 块定义,插入模型空间
// 块定义,插入模型空间
public ObjectId BlkInDb(BlockTableRecord block, Point3d pt, Database