public class Demo{[CommandMethod("xx")]public void Demo1(){using var tr1 = new DBTrans();var doc = Application.DocumentManager.MdiActiveDocument; var db = doc.Database;var ed = doc.Editor;var 圓心 = new Point3d(0, 0, 0); var 半徑 = 10.0;using (var tr = db.TransactionManager.StartTransaction()){var bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;var ms = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;var btr = new BlockTableRecord();btr.Name = "MyBlock";if (bt.Has("MyBlock")) {var bid = bt["MyBlock"];bid.EraseBlock();}var bkid = bt.Add(btr); tr.AddNewlyCreatedDBObject(btr, true);Circle cir = new Circle(Point3d.Origin, Vector3d.ZAxis, 1);cir.ColorIndex = 1;var id = btr.AppendEntity(cir); tr.AddNewlyCreatedDBObject(cir, true);Hatch hat = new Hatch();hat.ColorIndex = 1;hat.SetHatchPattern(HatchPatternType.CustomDefined, "SOLID");hat.AppendLoop(HatchLoopTypes.Default, [id]);hat.EvaluateHatch(true); btr.AppendEntity(hat);tr.AddNewlyCreatedDBObject(hat, true);var br = new BlockReference(圓心,bkid);br.ScaleFactors = new Scale3d(半徑);ms.AppendEntity(br);tr.AddNewlyCreatedDBObject(br, true);tr.Commit();}}public Polyline 多段線圓(Point3d pt ,double 半徑, int colorindex){Polyline pl = new Polyline();var p1 = new Point2d(pt.X - 半徑, pt.Y);var p2 = new Point2d(pt.X + 半徑, pt.Y);pl.AddVertexAt(0, p1, Math.Tan(Math.PI/4), 0, 0);pl.AddVertexAt(1, p2, Math.Tan(Math.PI / 4), 0, 0);pl.Closed = true;pl.ColorIndex = colorindex;return pl;}}
public static bool EraseBlock(this ObjectId objId){Database db = HostApplicationServices.WorkingDatabase;bool isErase = false;try{using (Transaction trans = db.TransactionManager.StartTransaction()){BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);if (bt.Has(objId)){BlockTableRecord bEra = (BlockTableRecord)objId.GetObject(OpenMode.ForWrite);bEra.Erase();}trans.Commit();isErase = true;}return isErase;}catch (Exception){return isErase;throw;}}
網上
public class Demo
{
? ? [CommandMethod("xx")]
? ? public void Demo1()
? ? {
? ? ? ? var doc = Application.DocumentManager.MdiActiveDocument;
? ? ? ? var db = doc.Database;
? ? ? ? var ed = doc.Editor;
? ? ? ? // 獲取用戶輸入
? ? ? ? var insertPointResult = ed.GetPoint("\n請指定插入點: ");
? ? ? ? if (insertPointResult.Status != PromptStatus.OK) return;
? ? ? ? Point3d 插入點 = insertPointResult.Value;
? ? ? ? var elevationResult = ed.GetDouble("\n請輸入高程值: ");
? ? ? ? if (elevationResult.Status != PromptStatus.OK) return;
? ? ? ? double 高程值 = elevationResult.Value;
? ? ? ? double 半徑 = 10.0; // 可改為用戶輸入
? ? ? ? using (var tr = db.TransactionManager.StartTransaction()) // 使用單一事務
? ? ? ? {
? ? ? ? ? ? var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
? ? ? ? ? ? var ms = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
? ? ? ? ? ? // 塊定義處理
? ? ? ? ? ? string 塊名 = "ElevationBlock";
? ? ? ? ? ? if (bt.Has(塊名))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // 徹底清除舊塊定義及其參照
? ? ? ? ? ? ? ? var oldBtr = (BlockTableRecord)tr.GetObject(bt[塊名], OpenMode.ForWrite);
? ? ? ? ? ? ? ? oldBtr.PurgeAll(); // 清除所有關聯實體
? ? ? ? ? ? ? ? oldBtr.Erase(true);
? ? ? ? ? ? }
? ? ? ? ? ? var btr = new BlockTableRecord { Name = 塊名 };
? ? ? ? ? ? bt.Add(btr);
? ? ? ? ? ? tr.AddNewlyCreatedDBObject(btr, true);
? ? ? ? ? ? // 創建圖形
? ? ? ? ? ? Circle cir = new Circle(Point3d.Origin, Vector3d.ZAxis, 1);
? ? ? ? ? ? cir.ColorIndex = 1;
? ? ? ? ? ? btr.AppendEntity(cir);
? ? ? ? ? ? tr.AddNewlyCreatedDBObject(cir, true);
? ? ? ? ? ? // 創建屬性定義(動態計算位置)
? ? ? ? ? ? AttributeDefinition attDef = new AttributeDefinition
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Position = new Point3d(1.5 * 半徑, 0, 0), // 根據半徑調整初始位置
? ? ? ? ? ? ? ? Prompt = "高程值",
? ? ? ? ? ? ? ? Tag = "ELEVATION",
? ? ? ? ? ? ? ? TextString = 高程值.ToString("0.00"),
? ? ? ? ? ? ? ? Height = 0.2 * 半徑, // 文字高度與半徑關聯
? ? ? ? ? ? ? ? WidthFactor = 0.8,
? ? ? ? ? ? ? ? Justify = AttachmentPoint.MiddleLeft,
? ? ? ? ? ? ? ? ColorIndex = 1
? ? ? ? ? ? };
? ? ? ? ? ? btr.AppendEntity(attDef);
? ? ? ? ? ? tr.AddNewlyCreatedDBObject(attDef, true);
? ? ? ? ? ? // 創建塊參照
? ? ? ? ? ? BlockReference br = new BlockReference(插入點, btr.ObjectId);
? ? ? ? ? ? br.ScaleFactors = new Scale3d(半徑); // 縮放因子控制整體尺寸
? ? ? ? ? ? // 添加屬性引用
? ? ? ? ? ? br.AttributeCollection.AppendAttribute(
? ? ? ? ? ? ? ? new AttributeReference {
? ? ? ? ? ? ? ? ? ? TextString = 高程值.ToString("0.00"),
? ? ? ? ? ? ? ? ? ? Position = attDef.Position.TransformBy(br.BlockTransform) // 應用變換
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ms.AppendEntity(br);
? ? ? ? ? ? tr.AddNewlyCreatedDBObject(br, true);
? ? ? ? ? ? tr.Commit();
? ? ? ? }
? ? }
? ? // 移除未使用的多段線方法(根據需求決定是否保留)
}
加入jig功能
public class Demo
{
? ? [CommandMethod("xx")]
? ? public void Demo1()
? ? {
? ? ? ? var doc = Application.DocumentManager.MdiActiveDocument;
? ? ? ? var db = doc.Database;
? ? ? ? var ed = doc.Editor;
? ? ? ? // 第一步:獲取高程值
? ? ? ? var elevationResult = ed.GetDouble("\n請輸入高程值: ");
? ? ? ? if (elevationResult.Status != PromptStatus.OK) return;
? ? ? ? double 高程值 = elevationResult.Value;
? ? ? ? // 第二步:創建塊定義
? ? ? ? string 塊名 = "ElevationBlock";
? ? ? ? ObjectId blockId;
? ? ? ? using (var tr = db.TransactionManager.StartTransaction())
? ? ? ? {
? ? ? ? ? ? var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
? ? ? ? ? ??
? ? ? ? ? ? // 如果塊不存在則創建
? ? ? ? ? ? if (!bt.Has(塊名))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? bt.UpgradeOpen();
? ? ? ? ? ? ? ? var btr = new BlockTableRecord { Name = 塊名 };
? ? ? ? ? ? ? ? // 創建圓形(基準尺寸為1單位)
? ? ? ? ? ? ? ? var cir = new Circle(Point3d.Origin, Vector3d.ZAxis, 1);
? ? ? ? ? ? ? ? cir.ColorIndex = 1;
? ? ? ? ? ? ? ? btr.AppendEntity(cir);
? ? ? ? ? ? ? ? tr.AddNewlyCreatedDBObject(cir, true);
? ? ? ? ? ? ? ? // 創建屬性定義
? ? ? ? ? ? ? ? var attDef = new AttributeDefinition
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Position = new Point3d(1.5, 0, 0), ?// 相對位置
? ? ? ? ? ? ? ? ? ? Tag = "ELEV",
? ? ? ? ? ? ? ? ? ? Prompt = "高程值",
? ? ? ? ? ? ? ? ? ? TextString = "0.00",
? ? ? ? ? ? ? ? ? ? Height = 0.2, ?// 基準文字高度
? ? ? ? ? ? ? ? ? ? ColorIndex = 1,
? ? ? ? ? ? ? ? ? ? Justify = AttachmentPoint.MiddleLeft
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? btr.AppendEntity(attDef);
? ? ? ? ? ? ? ? tr.AddNewlyCreatedDBObject(attDef, true);
? ? ? ? ? ? ? ? blockId = bt.Add(btr);
? ? ? ? ? ? ? ? tr.AddNewlyCreatedDBObject(btr, true);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? blockId = bt[塊名];
? ? ? ? ? ? }
? ? ? ? ? ? tr.Commit();
? ? ? ? }
? ? ? ? // 第三步:使用Jig交互式插入
? ? ? ? using (var tr = db.TransactionManager.StartTransaction())
? ? ? ? {
? ? ? ? ? ? var ms = (BlockTableRecord)tr.GetObject(
? ? ? ? ? ? ? ? SymbolUtilityServices.GetBlockModelSpaceId(db),?
? ? ? ? ? ? ? ? OpenMode.ForWrite);
? ? ? ? ? ? // 創建臨時塊參照
? ? ? ? ? ? var br = new BlockReference(Point3d.Origin, blockId)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ScaleFactors = new Scale3d(10) // 默認縮放因子
? ? ? ? ? ? };
? ? ? ? ? ? // 設置屬性值
? ? ? ? ? ? var attCol = br.AttributeCollection;
? ? ? ? ? ? foreach (ObjectId attDefId in br.BlockTableRecord.GetObject<BlockTableRecord>())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (attDefId.ObjectClass.DxfName == "ATTDEF")
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? var attDef = tr.GetObject(attDefId, OpenMode.ForRead) as AttributeDefinition;
? ? ? ? ? ? ? ? ? ? var attRef = new AttributeReference();
? ? ? ? ? ? ? ? ? ? attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
? ? ? ? ? ? ? ? ? ? attRef.TextString = 高程值.ToString("0.00");
? ? ? ? ? ? ? ? ? ? attCol.AppendAttribute(attRef);
? ? ? ? ? ? ? ? ? ? tr.AddNewlyCreatedDBObject(attRef, true);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? // 啟動拖動Jig
? ? ? ? ? ? var jig = new BlockJig(br, 高程值);
? ? ? ? ? ? var result = ed.Drag(jig);
? ? ? ? ? ? if (result.Status == PromptStatus.OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ms.AppendEntity(br);
? ? ? ? ? ? ? ? tr.AddNewlyCreatedDBObject(br, true);
? ? ? ? ? ? ? ? tr.Commit();
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? tr.Abort();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? // Jig實現類
? ? public class BlockJig : EntityJig
? ? {
? ? ? ? private double _elevation;
? ? ? ? private Point3d _insertPoint;
? ? ? ? public BlockJig(BlockReference br, double elevation) : base(br)
? ? ? ? {
? ? ? ? ? ? _elevation = elevation;
? ? ? ? ? ? _insertPoint = br.Position;
? ? ? ? }
? ? ? ? protected override SamplerStatus Sampler(JigPrompts prompts)
? ? ? ? {
? ? ? ? ? ? var opts = new JigPromptPointOptions("\n指定插入點: ");
? ? ? ? ? ? opts.UserInputControls = UserInputControls.NullResponseAccepted;
? ? ? ? ? ? var res = prompts.AcquirePoint(opts);
? ? ? ? ? ? if (res.Status == PromptStatus.OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (_insertPoint.DistanceTo(res.Value) < Tolerance.Global.EqualPoint)
? ? ? ? ? ? ? ? ? ? return SamplerStatus.NoChange;
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? _insertPoint = res.Value;
? ? ? ? ? ? ? ? return SamplerStatus.OK;
? ? ? ? ? ? }
? ? ? ? ? ? return SamplerStatus.Cancel;
? ? ? ? }
? ? ? ? protected override bool Update()
? ? ? ? {
? ? ? ? ? ? ((BlockReference)Entity).Position = _insertPoint;
? ? ? ? ? ??
? ? ? ? ? ? // 更新屬性位置
? ? ? ? ? ? var tr = Entity.Database.TransactionManager.TopTransaction;
? ? ? ? ? ? foreach (ObjectId attId in ((BlockReference)Entity).AttributeCollection)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? var attRef = tr.GetObject(attId, OpenMode.ForWrite) as AttributeReference;
? ? ? ? ? ? ? ? attRef.Position = attRef.Position.TransformBy(
? ? ? ? ? ? ? ? ? ? Matrix3d.Displacement(_insertPoint - Point3d.Origin));
? ? ? ? ? ? }
? ? ? ? ? ? return true;
? ? ? ? }
? ? }
}
封裝jig
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using System.Collections.Generic;
public static class EntityJigger
{
? ? /// <summary>
? ? /// 交互式拖動插入多個實體
? ? /// </summary>
? ? /// <param name="entities">要插入的實體列表(建議使用未添加到數據庫的副本)</param>
? ? /// <param name="basePoint">實體組合的基準點</param>
? ? /// <returns>最終的插入點(如果取消返回null)</returns>
? ? public static Point3d? DragEntities(List<Entity> entities, Point3d basePoint)
? ? {
? ? ? ? var doc = Application.DocumentManager.MdiActiveDocument;
? ? ? ? var db = doc.Database;
? ? ? ? var ed = doc.Editor;
? ? ? ? using (var tr = db.TransactionManager.StartTransaction())
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // 創建臨時實體副本
? ? ? ? ? ? ? ? var tempEntities = new List<Entity>();
? ? ? ? ? ? ? ? foreach (var ent in entities)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? var clone = ent.Clone() as Entity;
? ? ? ? ? ? ? ? ? ? clone.TransformBy(Matrix3d.Displacement(basePoint.GetVectorTo(Point3d.Origin)));
? ? ? ? ? ? ? ? ? ? tempEntities.Add(clone);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 創建Jig實例
? ? ? ? ? ? ? ? var jig = new MultiEntityJig(tempEntities, basePoint);
? ? ? ? ? ? ? ? var result = ed.Drag(jig);
? ? ? ? ? ? ? ? if (result.Status == PromptStatus.OK)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? // 正式添加實體到模型空間
? ? ? ? ? ? ? ? ? ? var ms = (BlockTableRecord)tr.GetObject(
? ? ? ? ? ? ? ? ? ? ? ? SymbolUtilityServices.GetBlockModelSpaceId(db),
? ? ? ? ? ? ? ? ? ? ? ? OpenMode.ForWrite);
? ? ? ? ? ? ? ? ? ? foreach (var ent in tempEntities)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ms.AppendEntity(ent);
? ? ? ? ? ? ? ? ? ? ? ? tr.AddNewlyCreatedDBObject(ent, true);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? tr.Commit();
? ? ? ? ? ? ? ? ? ? return jig.InsertPoint;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? tr.Abort();
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? ? ? catch
? ? ? ? ? ? {
? ? ? ? ? ? ? ? tr.Abort();
? ? ? ? ? ? ? ? throw;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /// <summary>
? ? /// 多實體拖動Jig實現
? ? /// </summary>
? ? private class MultiEntityJig : DrawJig
? ? {
? ? ? ? private readonly List<Entity> _entities;
? ? ? ? private Point3d _basePoint;
? ? ? ? private Point3d _insertPoint;
? ? ? ? public Point3d InsertPoint => _insertPoint;
? ? ? ? public MultiEntityJig(List<Entity> entities, Point3d basePoint)
? ? ? ? {
? ? ? ? ? ? _entities = entities;
? ? ? ? ? ? _basePoint = basePoint;
? ? ? ? ? ? _insertPoint = basePoint;
? ? ? ? }
? ? ? ? protected override SamplerStatus Sampler(JigPrompts prompts)
? ? ? ? {
? ? ? ? ? ? var opts = new JigPromptPointOptions("\n指定插入點: ")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? UserInputControls = UserInputControls.Accept3dCoordinates |
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UserInputControls.NoZeroResponse |
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UserInputControls.NoNegativeResponse
? ? ? ? ? ? };
? ? ? ? ? ? var result = prompts.AcquirePoint(opts);
? ? ? ? ? ? if (result.Value.DistanceTo(_insertPoint) < Tolerance.Global.EqualPoint)
? ? ? ? ? ? ? ? return SamplerStatus.NoChange;
? ? ? ? ? ? _insertPoint = result.Value;
? ? ? ? ? ? return SamplerStatus.OK;
? ? ? ? }
? ? ? ? protected override bool WorldDraw(WorldDraw draw)
? ? ? ? {
? ? ? ? ? ? // 計算位移矩陣
? ? ? ? ? ? var vector = _basePoint.GetVectorTo(_insertPoint);
? ? ? ? ? ? var matrix = Matrix3d.Displacement(vector);
? ? ? ? ? ? // 繪制所有臨時實體
? ? ? ? ? ? foreach (var ent in _entities)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? draw.Geometry.Draw(ent);
? ? ? ? ? ? ? ? ent.TransformBy(matrix);
? ? ? ? ? ? }
? ? ? ? ? ? return true;
? ? ? ? }
? ? }
}
?
[CommandMethod("testJig")]
public void TestJigCommand()
{
? ? var doc = Application.DocumentManager.MdiActiveDocument;
? ? var db = doc.Database;
? ? var ed = doc.Editor;
?
? ? // 創建要插入的實體集合
? ? var entities = new List<Entity>();
? ??
? ? // 創建圓形(基準位置在原點)
? ? var circle = new Circle(Point3d.Origin, Vector3d.ZAxis, 5);
? ? circle.ColorIndex = 1;
? ? entities.Add(circle);
?
? ? // 創建單行文字
? ? var text = new DBText
? ? {
? ? ? ? Position = new Point3d(8, 0, 0),
? ? ? ? TextString = "高程值",
? ? ? ? Height = 2,
? ? ? ? ColorIndex = 2,
? ? ? ? Justify = AttachmentPoint.MiddleLeft
? ? };
? ? entities.Add(text);
?
? ? // 設置基準點(通常取組合的中心點)
? ? var basePoint = new Point3d(0, 0, 0);
?
? ? // 調用Jig進行拖動插入
? ? var resultPoint = EntityJigger.DragEntities(entities, basePoint);
?
? ? if (resultPoint.HasValue)
? ? {
? ? ? ? ed.WriteMessage($"\n插入點坐標:{resultPoint.Value}");
? ? }
}
?