You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plant3d原生设备属性读写报错及解决方案咨询

解决Plant3D原生设备属性读写时的ObjectDoesNotHaveLink错误

错误原因

  1. 项目模块不匹配:你代码中使用projeto.ProjectParts["PnId"]获取的是P&ID模块的DataLinksManager,但Plant3D原生设备属于Plant3D项目模块,两者数据库上下文不兼容,导致无法找到设备的链接记录。
  2. 未过滤非设备实体:遍历ModelSpace所有实体时,包含了线条、文字等非Plant3D设备对象,这类对象本身没有和PnP数据库的关联,调用FindAcPpRowId必然触发ObjectDoesNotHaveLink错误。

修正后的实现代码

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.ProcessPower.ProjectManager;
using Autodesk.ProcessPower.DataLinks;
using System.Windows.Forms;
using Autodesk.ProcessPower.PnP3dObjects;
using System.Collections.Generic;

namespace ParametEquip
{
    public class Plugin
    {
        [CommandMethod("PARAMEQ", CommandFlags.UsePickSet)]
        public static void ParametEquip()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                PlantProject projeto = PlantApplication.CurrentProject;
                // 切换到Plant3D项目模块
                Project pj = projeto.ProjectParts["Plant3D"];
                DataLinksManager dlm = pj.DataLinksManager;

                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

                foreach (ObjectId id in btr)
                {
                    // 仅处理Plant3D原生设备对象
                    if (id.ObjectClass.Name.Equals("Autodesk.ProcessPower.PnP3dObjects.PnP3dEquipment"))
                    {
                        Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
                        try
                        {
                            int rowId = dlm.FindAcPpRowId(ent.ObjectId);
                            if (rowId != -1)
                            {
                                // 获取所有属性
                                List<KeyValuePair<string, string>> props = dlm.GetAllProperties(rowId, true);
                                foreach (var prop in props)
                                {
                                    ed.WriteMessage($"属性: {prop.Key} | 值: {prop.Value}\n");
                                    // 示例:修改Long Description (Size)属性为指定值
                                    if (prop.Key == "Long Description (Size)")
                                    {
                                        dlm.SetProperty(rowId, "Long Description (Size)", "Heat Exchanger");
                                    }
                                }
                            }
                        }
                        catch (DLException ex)
                        {
                            ed.WriteMessage($"对象处理失败: {ex.Message}\n");
                        }
                    }
                }

                trans.Commit();
                ed.WriteMessage("设备属性处理完成\n");
            }
        }
    }
}

核心改动说明

  • 切换项目模块:使用"Plant3D"替代"PnId",确保DataLinksManager指向Plant3D的数据库,匹配设备的存储上下文。
  • 过滤设备类型:通过对象类名筛选出PnP3dEquipment类型的实体,只处理Plant3D原生设备,避免无关实体干扰。
  • 异常容错处理:添加try-catch捕获DLException,防止单个异常导致整个命令中断。
  • 属性读写示例:包含了属性读取和修改的实现,使用dlm.SetProperty即可直接写入属性值。

内容的提问来源于stack exchange,提问作者João Arthur Falcão

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 19:51:04