- 积分
- 14418
- 明经币
- 个
- 注册时间
- 2007-7-8
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2010-12-6 19:43:17
|
显示全部楼层
本帖最后由 chpmould 于 2012-3-28 21:26 编辑
我测试的时候一直在新建块的时候就出错,请帮我调试看看...- [CommandMethod("test")]
- public void CreateCircle()
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- using (doc.LockDocument())
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- ObjectId c1 = AddCircle(new Point3d(0, 0, 0), 10);
- ObjectId c2 = AddCircle(new Point3d(0, 0, 0), 15);
- ObjectIdCollection cir01 = new ObjectIdCollection();
- cir01.Add(c1);
- cir01.Add(c2);
- DBObjectCollection objs = GetEntity(cir01);
- CreateBlockDef("Test", objs);
- tr.Commit();
- }
- }
- public static ObjectId AddCircle(Point3d cenPt, double radius)
- {
- Circle ent = new Circle(cenPt, Vector3d.ZAxis, radius);
- ObjectId entId = AppendEntity(ent);
- return entId;
- }
- public static DBObjectCollection GetEntity(ObjectIdCollection objId)
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- DBObjectCollection obj = new DBObjectCollection();
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- foreach (ObjectId Id in objId)
- {
- obj.Add(Id.GetObject(OpenMode.ForRead));
- }
- tr.Commit();
- }
- return obj;
- }
- public static ObjectId CreateBlockDef(string blockname, DBObjectCollection objs)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
- if (bt.Has(blockname))
- {
- return bt[blockname];
- }
- else
- {
- BlockTableRecord btr = new BlockTableRecord();
- btr.Name = blockname;
- ObjectId id = bt.Add(btr);
- tr.AddNewlyCreatedDBObject(btr, true);
- foreach (Entity ent in objs)
- {
- btr.AppendEntity(ent);
- tr.AddNewlyCreatedDBObject(ent, true);
- }
- tr.Commit();
- return id;
- }
- }
- }
- public static ObjectId AppendEntity(Entity ent)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId entId;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- entId = btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent, true);
- trans.Commit();
- }
- return entId;
- }
|
|