[CommandMethod("TestSelects")]
public void TestSelect()
{
doc = Application.DocumentManager.MdiActiveDocument;
ed = doc.Editor;
db = doc.Database;
var filter = new SelectionFilter(new TypedValue[]
{
new TypedValue((int)DxfCode.Start, "INSERT"),
});
var result = ed.GetSelection(filter);
if (result.Status == PromptStatus.OK)
{
SelectedBlockIds = result.Value.GetObjectIds().ToList();
ed.WriteMessage($"\n已选中 {SelectedBlockIds.Count} 个图框,开始分析...");
AnalyzeAndMarkFrames();
}
else
{
SelectedBlockIds.Clear();
ed.WriteMessage("\n未选中任何图框。");
}
}
private void AnalyzeAndMarkFrames()
{
ValidFrameIds.Clear();
frameCounter = 1;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (ObjectId blockId in SelectedBlockIds)
{
Entity entity = tr.GetObject(blockId, OpenMode.ForWrite) as Entity;
if (entity == null) continue;
BlockReference blockRef = entity as BlockReference;
if (blockRef == null) continue;
blockRef.ColorIndex = 1;
AddFrameNumber(blockRef, frameCounter, tr, modelSpace);
ValidFrameIds.Add(blockId);
frameCounter++;
}
tr.Commit();
ed.WriteMessage($"\n共标记了 {ValidFrameIds.Count} 个有效图框");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage($"\n分析过程中发生错误: {ex.Message}");
tr.Abort();
}
}
}
private void AddFrameNumber(BlockReference blockRef, int number, Transaction tr, BlockTableRecord modelSpace)
{
try
{
Extents3d? frameExtents = GetFrameBoundary(blockRef, tr);
if (!frameExtents.HasValue)
{
frameExtents = blockRef.GeometricExtents;
}
Extents3d ext = frameExtents.Value;
double entityHeight = ext.MaxPoint.Y - ext.MinPoint.Y;
double textHeight = entityHeight;
Point3d center = ext.MinPoint + (ext.MaxPoint - ext.MinPoint) / 2.0;
DBText text = new DBText
{
TextString = number.ToString(),
Position = center,
Height = textHeight,
HorizontalMode = TextHorizontalMode.TextCenter,
VerticalMode = TextVerticalMode.TextVerticalMid,
AlignmentPoint = center,
ColorIndex = 1
};
Polyline rectangle = new Polyline();
rectangle.AddVertexAt(0, new Point2d(ext.MinPoint.X, ext.MinPoint.Y), 0, 0, 0);
rectangle.AddVertexAt(1, new Point2d(ext.MaxPoint.X, ext.MinPoint.Y), 0, 0, 0);
rectangle.AddVertexAt(2, new Point2d(ext.MaxPoint.X, ext.MaxPoint.Y), 0, 0, 0);
rectangle.AddVertexAt(3, new Point2d(ext.MinPoint.X, ext.MaxPoint.Y), 0, 0, 0);
rectangle.Closed = true;
rectangle.ColorIndex = 2;
rectangle.LineWeight = LineWeight.LineWeight050;
ZoomToPoint(ed, center);
modelSpace.AppendEntity(rectangle);
tr.AddNewlyCreatedDBObject(rectangle, true);
CreatedMarkingIds.Add(rectangle.ObjectId);
modelSpace.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
CreatedMarkingIds.Add(text.ObjectId);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage($"\n添加编号时出错: {ex.Message}");
}
}
private Extents3d? GetFrameBoundary(BlockReference blockRef, Transaction tr)
{
try
{
BlockTableRecord blockDef = (BlockTableRecord)tr.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead);
Extents3d? frameExtents = null;
Matrix3d transform = blockRef.BlockTransform;
foreach (ObjectId objId in blockDef)
{
Entity ent = tr.GetObject(objId, OpenMode.ForRead) as Entity;
if (ent != null)
{
if (ent is Polyline || ent is Line || ent is Circle || ent is Arc ||
ent is Ellipse || ent is Spline || ent is Region)
{
try
{
Extents3d entExtents = ent.GeometricExtents;
Point3d minPt = entExtents.MinPoint.TransformBy(transform);
Point3d maxPt = entExtents.MaxPoint.TransformBy(transform);
Extents3d transformedExtents = new Extents3d(minPt, maxPt);
if (frameExtents == null)
frameExtents = transformedExtents;
else
{
Extents3d current = frameExtents.Value;
current.AddExtents(transformedExtents);
frameExtents = current;
}
}
catch { }
}
}
}
return frameExtents;
}
catch
{
return null;
}
}