炸开属性块-可用
我有一个炸开属性块的需求, 直接炸开不可以用,会显示属性的tag, 不是值
如果创建一个新块,会出现填充位置不对的情况, 所以使用了炸开块, 然后将属性tag去掉的方法
public class Class1
{
[CommandMethod("FIXEDEXPLODEATTRIB")]
public void FixedExplodeAttributes()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
try
{
// 选择要炸开的块参照
PromptEntityOptions peo = new PromptEntityOptions("\n选择属性块: ");
peo.SetRejectMessage("\n请选择一个块参照.");
peo.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// 获取选中的块参照
BlockReference br = tr.GetObject(per.ObjectId, OpenMode.ForRead) as BlockReference;
if (br != null)
{
// 获取块定义
BlockTableRecord btr = tr.GetObject(br.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
// 获取块参照的比例因子
double scaleFactor = br.ScaleFactors.X; // 假设X、Y、Z比例相同
// 获取当前空间
BlockTableRecord currentSpace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
// 直接炸开原始块参照
DBObjectCollection explodedObjects = new DBObjectCollection();
br.Explode(explodedObjects);
// 处理炸开后的实体
List<AttInfo> inbisibleTagInfos = new List<AttInfo>();
foreach (DBObject obj in explodedObjects)
{
Entity ent = obj as Entity;
if (ent != null)
{
if (ent is AttributeDefinition attDef)
{
bool isInvisible = attDef.Invisible;
if (isInvisible)
{
inbisibleTagInfos.Add(new AttInfo
{
Tag = attDef.Tag,
Position = attDef.Position,
AlignmentPoint = attDef.AlignmentPoint
});
}
}
else
{
currentSpace.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
}
}
// 处理属性:转换为文字
List<AttInfo> attInfos = new List<AttInfo>();
foreach (ObjectId arId in br.AttributeCollection)
{
AttributeReference ar = tr.GetObject(arId, OpenMode.ForRead) as AttributeReference;
if (ar != null && ar.Visible)
{
DBText text = new DBText();
text.Position = ar.Position;
text.TextString = ar.TextString;
text.Height = ar.Height;
text.Rotation = ar.Rotation + br.Rotation;
text.Layer = ar.Layer;
text.ColorIndex = ar.ColorIndex;
text.TextStyleId = ar.TextStyleId;
text.HorizontalMode = ar.HorizontalMode;
text.VerticalMode = ar.VerticalMode;
text.WidthFactor = ar.WidthFactor;
text.LineWeight = ar.LineWeight;
if (ar.HorizontalMode != TextHorizontalMode.TextLeft || ar.VerticalMode != TextVerticalMode.TextBase)
{
try
{
text.AlignmentPoint = ar.AlignmentPoint;
}
catch
{
text.AlignmentPoint = text.Position;
}
}
//如果属性的标记是不可见, 就不添加,对比坐标是在对比double, 这里需要有个精度处理
var newTextInvisible = inbisibleTagInfos.Any(x =>x.Tag == ar.Tag &&
Math.Round(x.Position.X,6) == Math.Round(ar.Position.X, 6) &&
Math.Round(x.Position.Y,6) == Math.Round(ar.Position.Y, 6) &&
Math.Round(x.Position.Z, 6) == Math.Round(ar.Position.Z, 6));
if (!newTextInvisible)
{
currentSpace.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
}
}
}
// 删除原始块参照
br.UpgradeOpen();
br.Erase();
}
else
{
ed.WriteMessage("\n选择的对象不是块参照。");
}
tr.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage($"\n错误: {ex.Message}");
}
}
}
// 用于存储属性信息的类
public class AttInfo
{
public string Tag { get; set; }
public string Value { get; set; }
public Point3d Position { get; set; }
public Point3d AlignmentPoint { get; set; }
}