fangmin723 发表于 2024-11-29 16:11:47

【已解决】获取属性快中的属性参照包围盒,块中属性定义无法获取包围盒

本帖最后由 fangmin723 于 2024-11-30 18:15 编辑

AcDbAttributeDefinition.GeometricExtents;出错了,AutoCAD.Net怎么获取AcDbAttributeDefinition的包围框

AutoCAD二次开发:获取属性快中的属性名,属性值,属性值范围_cad在块中手动加的属性,在c++代码中怎么获取-CSDN博客
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;

public class AttributeExtractor
{
    public static void GetAttributeValuesAndExtents()
    {
      // 获取当前文档和数据库
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor ed = doc.Editor;

      // 开始事务
      using (Transaction tr = db.TransactionManager.StartTransaction())
      {
            // 打开模型空间块表记录
            BlockTable blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
            BlockTableRecord btr= (BlockTableRecord)tr.GetObject(blockTable, OpenMode.ForRead);

            // 遍历模型空间中的所有对象
            foreach (ObjectId id in btr)
            {
                // 检查是否为块参照
                Entity ent = tr.GetObject(id , OpenMode.ForRead) as Entity;
                if (ent != null && ent is BlockReference)
                {
                  BlockReference blockRef = ent as BlockReference;
                  // 检查块是否有属性
                  if (blockRef.AttributeCollection.Count > 0)
                  {
                        // 遍历块中的每个属性
                        foreach (ObjectId attrId in blockRef.AttributeCollection)
                        {
                            DBObject obj = tr.GetObject(attrId, OpenMode.ForRead);
                            if (obj is AttributeReference)
                            {
                              AttributeReference attribute = obj as AttributeReference;
                              // 输出属性值
                              ed.WriteMessage($"\nAttribute: {attribute.Tag} - Value: {attribute.TextString}");

                              // 计算并输出属性文本的几何范围
                              Extents3d ext = attribute.GeometricExtents;
                              ed.WriteMessage($"\nGeometric Extents: Min({ext.MinPoint}), Max({ext.MaxPoint})");
                            }
                        }
                  }
                }
            }

            // 提交事务
            tr.Commit();
      }
    }
}

fangmin723 发表于 2024-11-29 16:23:05

明经AutoCAD.NetApi QQ群sailor(75063165)大佬:

这个我试过,后台新建一个DWG文档,将属性文字复制过去,才能测算出正确的包围盒,否则修改后包围盒一直不正确。

liuhe 发表于 2024-11-29 16:26:40

忽略掉,遇到块就分解就避开他,我也是经常到找不到它的包围盒

fangmin723 发表于 2024-11-29 16:59:23

liuhe 发表于 2024-11-29 16:26
忽略掉,遇到块就分解就避开他,我也是经常到找不到它的包围盒

按照字面意思理解的话,就是说当块属性在块内部的话,就获取不了,那在外部呢,复制一份在内存里的呢

你有种再说一遍 发表于 2024-11-29 17:11:13

fangmin723 发表于 2024-11-29 16:59
按照字面意思理解的话,就是说当块属性在块内部的话,就获取不了,那在外部呢,复制一份在内存里的呢

在内部也可以通过创建单行文字描述它

fangmin723 发表于 2024-11-29 17:14:19

本帖最后由 fangmin723 于 2024-11-29 17:16 编辑

你有种再说一遍 发表于 2024-11-29 17:11
在内部也可以通过创建单行文字描述它
嗯嗯,明天我都测试下外部和复制,看看可行性,可行的话,优先考虑单行文字:lol

e2002 发表于 2024-11-30 09:06:20

还要考虑到多行 Attrib 的情况吧?

fangmin723 发表于 2024-11-30 18:12:56

e2002 发表于 2024-11-30 09:06
还要考虑到多行 Attrib 的情况吧?

不知道,等用到了多行时,再研究吧
页: [1]
查看完整版本: 【已解决】获取属性快中的属性参照包围盒,块中属性定义无法获取包围盒