- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2010-12-6 20:53:40
|
显示全部楼层
重写了一遍,CreateXXX其实感觉没必要的,
简单的东西没必要搞一大堆函数。
其次,建议的方式是一个事务到底,否则会出问题的- [CommandMethod("tt4")]
- public void test24()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- string blkname = "Test";
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Circle c1 = new Circle(Point3d.Origin, Vector3d.ZAxis, 10);
- Circle c2 = new Circle(Point3d.Origin, Vector3d.ZAxis, 15);
- BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- ObjectId blkdefid = GetRecorId(tr, bt, blkname);
- if (blkdefid == ObjectId.Null)
- {
- BlockTableRecord blkdef = new BlockTableRecord { Name = blkname };
- bt.UpgradeOpen();
- blkdefid = bt.Add(blkdef);
- tr.AddNewlyCreatedDBObject(blkdef, true);
- bt.DowngradeOpen();
- AddEntity(tr, blkdef, c1);
- AddEntity(tr, blkdef, c2);
- }
- BlockReference bref = new BlockReference(Point3d.Origin, blkdefid);
- BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
- AddEntity(tr, btr, bref);
- tr.Commit();
- }
- }
- /// <summary>
- /// 在符号表中获取对应键值的记录Id
- /// </summary>
- /// <param name="table">符号表</param>
- /// <param name="key">记录键值</param>
- /// <returns>对应键值的记录Id</returns>
- public static ObjectId GetRecorId<T>(Transaction tr, T table, string key) where T : SymbolTable
- {
- if (table.Has(key))
- {
- if (Application.Version.Major < 18)
- {
- ObjectId idres = table[key];
- if (!idres.IsErased)
- return idres;
- foreach (ObjectId id in table)
- {
- if (!id.IsErased)
- {
- SymbolTableRecord str = id.GetObject<SymbolTableRecord>(tr);
- if (str.Name == key)
- return str.ObjectId;
- }
- }
- }
- else
- {
- return table[key];
- }
- }
- return ObjectId.Null;
- }
- public ObjectId AddEntity(Transaction tr, BlockTableRecord btr, Entity ent)
- {
- ObjectId id = btr.AppendEntity(ent);
- tr.AddNewlyCreatedDBObject(ent, true);
- return id;
- }
|
|