炸开属性块-填充不对

使用如下方法, 炸开后文字是对的, 但是填充不对, 需要再单独变换一次填充, 这样的话填充和边框也没有关联关系了

[CommandMethod("FIXEDEXPLODEATTRIB1")]

public void FixedExplodeAttributes_1()
{
	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;

            // 获取块参照的变换矩阵(处理比例和旋转)
            Matrix3d blockTransform = br.BlockTransform;

            // 创建新的匿名块定义
            BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
            BlockTableRecord newBtr = new BlockTableRecord();

            // 设置匿名块

            newBtr.Name = "123456789";

            // 添加新块到块表
            ObjectId newBtrId = bt.Add(newBtr);
            tr.AddNewlyCreatedDBObject(newBtr, true);

            // 克隆原始块中的实体并应用变换
            foreach (ObjectId id in btr)
            {
                Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
                if (ent != null && !(ent is AttributeDefinition))
                {
                    Entity cloneEnt = ent.Clone() as Entity;

                    // 应用块参照的变换到实体
                    if (cloneEnt != null)
                    {
                        cloneEnt.TransformBy(blockTransform);
                        newBtr.AppendEntity(cloneEnt);
                        tr.AddNewlyCreatedDBObject(cloneEnt, true);
                    }
                }
            }

            // 处理属性:转换为文字并应用变换
            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;

                    if (ar.HorizontalMode != TextHorizontalMode.TextLeft || ar.VerticalMode != TextVerticalMode.TextBase)
                    {
                        try
                        {
                            text.AlignmentPoint = ar.AlignmentPoint;
                        }
                        catch
                        {
                            // 如果设置AlignmentPoint失败,我们可以尝试使用位置点作为对齐点
                            text.AlignmentPoint = text.Position;
                        }
                    }
                    newBtr.AppendEntity(text);
                    tr.AddNewlyCreatedDBObject(text, true);
                }
            }

            // 创建新的块参照(使用单位矩阵,因为实体已经变换过了)
            BlockReference newBr = new BlockReference(Point3d.Origin, newBtrId);
            newBr.SetDatabaseDefaults();
            newBr.Layer = br.Layer;
            newBr.ColorIndex = br.ColorIndex;

            // 添加到当前空间
            BlockTableRecord currentSpace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
            currentSpace.AppendEntity(newBr);
            tr.AddNewlyCreatedDBObject(newBr, true);

            // 炸开新的块参照
            DBObjectCollection explodedObjects = new DBObjectCollection();
            newBr.Explode(explodedObjects);

            if (explodedObjects.Count > 0)
            {
                foreach (DBObject obj in explodedObjects)
                {
                    Entity ent = obj as Entity;
                    if (ent != null)
                    {
                        currentSpace.AppendEntity(ent);
                        tr.AddNewlyCreatedDBObject(ent, true);
                    }
                }

                // 删除临时块参照
                newBr.Erase();
            }

            // 删除原始块参照
            br.UpgradeOpen();
            br.Erase();
        }
        else
        {
            ed.WriteMessage("\n选择的对象不是块参照。");
        }

        tr.Commit();
    }
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
    ed.WriteMessage($"\n错误: {ex.Message}");
}
}
posted @ 2025-09-04 15:48  elepeipei  阅读(5)  评论(0)    收藏  举报