[MSCE C#]ItemType添加属性疑问(中文属性名如何赋值)?

各位老师 :

         请问 一下 Bentley.DgnPlatformNET.CustomProperty testProperty = itemType.AddProperty("放置基点"); 和

        foreach (string name in dictionary.Keys)
       {
          Bentley.DgnPlatformNET.CustomProperty testProperty = itemType.AddProperty(name);

          testProperty.Type = CustomProperty.TypeKind.String;
     }

     有什么区别吗? 为什么第一种在item.SetAsString(string,string)添加属性值的时候报错?

Parents Reply Children
  • 对于中文(或者带有空格的)属性名,需要调用ECNameValidation.EncodeToValidName将该属性名转换成内部名才能用于SetValue或你的SetAsString。可惜的是,C#中的ECNameValidation.EncodeToValidName居然有Bug,在C++中测试是通过的。

    如下代码可以在C++中工作:

    ElementRefP oldRef = eeh.GetElementRef();
    CustomItemHost itemHost(eeh, true);  // true = schedule changed
    DgnECInstancePtr pInstance = itemHost.ApplyCustomItem(*pItemType);
    WString encodedProp1, encodedProp2;
    ECNameValidation::EncodeToValidName(encodedProp1, WString(L"整数属性"));
    ECNameValidation::EncodeToValidName(encodedProp2, WString(L"字符属性"));
    pInstance->SetValue(encodedProp1.GetWCharCP(), ECValue(123));
    pInstance->SetValue(encodedProp2.GetWCharCP(), ECValue("ABC"));
    pInstance->ScheduleWriteChanges(eeh);
    eeh.ReplaceInModel(oldRef);



  • 针对C#,郭工在两年前就写了一个修正程序FurtherConvert,能让我们在C#下也能工作。完整C#测试代码如下:

    public static void AttachItem(string itemLibName, string itemTypeName, ElementId elemId)
            {
                DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                Element elem = dgnModel.FindElementById(elemId);
                if (null == elem)
                {
                    MessageBox.Show("Can't find element!");
                    return;
                }
    
                DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
                ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName(itemLibName, dgnFile);
                if (null == itemTypeLibrary)
                {
                    MessageBox.Show("Can't find ItemType Library");
                    return;
                }
                ItemType itemType = itemTypeLibrary.GetItemTypeByName(itemTypeName);
    
                CustomItemHost host = new CustomItemHost(elem, true);
                IDgnECInstance item = host.ApplyCustomItem(itemType, true);
                string intProp = "整数属性";
                ECNameValidation.EncodeToValidName(ref intProp);
                item.SetValue(FurtherConvert(intProp), 123);
                string strProp = "字符属性";
                ECNameValidation.EncodeToValidName(ref strProp);
                item.SetValue(FurtherConvert(strProp), "CCCC");
    
                item.ScheduleChanges(elem);
                elem.ReplaceInModel(elem);
            }
            // ECNameValidation.EncodeToValidName has bug in C# in MSCE version
            private static string FurtherConvert(string inStr)
            {
                string rtnStr = inStr;
                string pa = @"^[a-zA-Z0-9]*$";
                Regex reg = new Regex(pa);
                Match mc = null;
                for (int i = 0; i < rtnStr.Length - 4; i++)
                {
                    if (rtnStr.Substring(0, 2) == "__")
                    {
                        string curMiddleStr = "";
                        int j = i + 2;
                        for (; j < rtnStr.Length; j++)
                        {
                            if (rtnStr[j] == '_')
                            {
                                curMiddleStr = rtnStr.Substring(i + 2, j - (i + 2));
                                break;
                            }
                        }
                        if (j > rtnStr.Length - 2)
                        {
                            break;
                        }
                        mc = reg.Match(curMiddleStr);
                        if (mc.Success)
                        {
                            if (rtnStr.Substring(j, 2) == "__")
                            {
                                string curTempStr = rtnStr.Substring(i, j + 2 - i);
                                if (curTempStr.Length == 9 && curTempStr[2] == 'x')
                                {
                                    curTempStr = "__x" + curTempStr.Substring(3).ToUpper();
                                    rtnStr = rtnStr.Substring(0, i - 0) + curTempStr + rtnStr.Substring(j + 2, rtnStr.Length - j - 2);
                                }
                            }
                        }
                    }
                }
                return rtnStr;
            }

    注意,在调用了ECNameValidation.EncodeToValidName后又套了一层FurtherConvert,即进一步转换的意思。



    Answer Verified By: 超 上官 

  • 符老师,如果是字母/数字+中文,比如"HRB400重量"或者“IFD编码”,FurtherConvert会失效。请问这种情况有没有什么解决办法?

  • 最新C# API已经解决了该问题。ECNameValidation.EncodeToValidName(xxx, true); 即可。这个true能纠正原有的Bug,不再需要我们这个FurtherConvert函数了。