同标题,我在使用c#对CAD进行二次开发的时候发现,使用BlockTableRecord.append往块里面添加实体,报错显示实体已在图形数据库中,
- public static ObjectId AddBlockTableRecord(this Database db,string blockName,List<Entity> ents)
- {
- ObjectId btID;
- DocumentLock m_DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument();
- using (Transaction trans=db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);
- if (!bt.Has(blockName))
- {
- //创建一个blocktablerecord类的对象,表示需要创建的块
- BlockTableRecord btr = new BlockTableRecord();
- btr.Name = blockName; //设置块名
- //将列表中的实体添加到块里面去
- foreach (var ent in ents)
- {
- btr.AppendEntity(ent);
- }
- bt.UpgradeOpen(); //提升块表为写入
- db.TransactionManager.AddNewlyCreatedDBObject(btr, true);
- bt.DowngradeOpen();
- btID = bt[blockName];
- }
- else
- {
- BlockTableRecord btr;
- btr = (BlockTableRecord)trans.GetObject(bt[blockName], OpenMode.ForWrite);
- btr.Erase(); //删除快
- btr = new BlockTableRecord();
- btr.Name = blockName; //设置块名
- //将列表中的实体添加到块里面去
- foreach (var ent in ents)
- {
- btr.AppendEntity(ent);
- }
- bt.UpgradeOpen(); //提升块表为写入
- db.TransactionManager.AddNewlyCreatedDBObject(btr, true);
- bt.DowngradeOpen();
- btID = bt[blockName];
- }
- trans.Commit();
- }
- m_DocumentLock.Dispose();
- return btID;
- }
求高手指点,我实现的是,用户选择实体,我这边创建块。
|