|
- [CommandMethod("CreateBlock")]
- public void CreateBlock()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Autodesk.<a href="http://bbs.mjtd.com/forum-41-1.html" target="采用blank" class="relatedlink">AutoCAD</a>.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
- {
- ObjectId blockId = ObjectId.Null; //用于返回所创建的块的对象Id
- BlockTableRecord record = new BlockTableRecord(); //创建一个BlockTableRecord类的对象,表示所要创建的块
- record.Name = "MyBlock"; //设置块名
- record.Origin = new Point3d(0, 0, 0); //设置块的基点
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);//以写的方式打开块表
- if (!bt.Has(record.Name))
- {
- //选择对象
- PromptSelectionResult res = ed.GetSelection();
- if (res.Status == PromptStatus.OK)
- {
- foreach (ObjectId id in res.Value.GetObjectIds())
- {
- Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
- Entity NewEnt = (Entity)ent.Clone();
- record.AppendEntity(NewEnt);
- }
- }
- bt.Add(record); //在块表中加入块
- trans.AddNewlyCreatedDBObject(record, true);//通知事务处理
- }
- else
- {
- ed.WriteMessage("此图块名已存在,请检查!");
- }
- trans.Commit();
- }
- }
- }
复制代码 |
|