|
本例介绍如何创建AutoCAD中的图块,创建图块实际上就是给DWG数据库中的块表中添加一条块表记录。
- using System;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- namespace Sample
- {
- class AddBlock
- {
- [CommandMethod("CreateBlock")]
- public void Add()
- {
- Database db=HostApplicationServices.WorkingDatabase;
- BlockTableRecord btr = new BlockTableRecord();
- btr.Name = "bimcad";
- Line line = new Line(Point3d.Origin, new Point3d(10, 15, 0));
- Circle circle = new Circle(Point3d.Origin, Vector3d.ZAxis, 10);
- btr.AppendEntity(line);
- btr.AppendEntity(circle);
- AddBlockTableRecord(btr, db);
- }
- /// <summary>
- /// 将块表记录加入到块表中
- /// </summary>
- /// <returns></returns>
- public ObjectId AddBlockTableRecord(BlockTableRecord btr, Database db)
- {
- ObjectId id = new ObjectId();
- using (Transaction transaction = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = transaction.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
- id = bt.Add(btr);
- transaction.AddNewlyCreatedDBObject(btr, true);
- transaction.Commit();
- }
- return id;
- }
- }
- }
复制代码
加载程序后运行"CreateBlock"命令,则创建了一个名为bimcad的图块。
|
|