本帖最后由 landsat99 于 2022-4-27 22:54 编辑
创建动态块得 .net或Objarx
下面这段.net 试试看
- using System;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- [assembly: CommandClass(typeof(Sample.AddBlock))]
- namespace Sample
- {
- class AddBlock
- {
- /// <summary>
- /// 把块定义变为块参照插入
- /// </summary>
- [CommandMethod("AddBlock")]
- public void AddBk()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId blkid;
- //创建块定义
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
- BlockTableRecord blk = new BlockTableRecord();
- blk.Name = "BLK";
- Line L = new Line(Point3d.Origin, new Point3d(1000, 1000, 0));
- Circle C = new Circle(Point3d.Origin, Vector3d.ZAxis, 500);
- blk.AppendEntity(L);
- blk.AppendEntity(C);
- blkid = bt.Add(blk);
- trans.AddNewlyCreatedDBObject(blk, true);
- trans.Commit();
- }
-
- //把块参照添加到块表记录
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- BlockReference br = new BlockReference(new Point3d(0,0, 0), blkid); // 通过块定义创建块参照
- btr.AppendEntity(br);
- tr.AddNewlyCreatedDBObject(br, true); // 通过事务添加块参照到数据库
- tr.Commit();
- }
- }
- }
- }
|