各位老师 :
请问 一下 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)添加属性值的时候报错?
你这只是把前者放到后者额循环里边了,没啥区别啊。报错的话,报的什么错,您只发这么点代码看不出来问题出在哪啊
程序直接崩掉...发现当itemType.AddProperty()传入的字符串为中文或者特殊字符时 会将其转为十六进制
我这里测试没有问题呀。我用的是最新版MSCE U12。测试代码如下:
public static void CreateItemType(string itemLibName, string itemTypeName) { DgnFile dgnFile = Session.Instance.GetActiveDgnFile(); ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName(itemLibName, dgnFile); if (null == itemTypeLibrary) { itemTypeLibrary = ItemTypeLibrary.Create(itemLibName, dgnFile); } itemTypeLibrary.AddItemType(itemTypeName); ItemType itemType = itemTypeLibrary.GetItemTypeByName(itemTypeName); CustomProperty intProperty = itemType.AddProperty("整数属性"); intProperty.Type = CustomProperty.TypeKind.Integer; CustomProperty strProperty = itemType.AddProperty("字符属性"); strProperty.Type = CustomProperty.TypeKind.String; itemTypeLibrary.Write(); }
创建出来的ItemType如下:
符工 是的 是的 这个确实没有问题的...但是在这里item.SetAsString(string,string) 为属性赋值的时候 会有问题
例如 item.SetAsString(“整数属性”,“123”) 您测试一下 看是否可以写入
对于中文(或者带有空格的)属性名,需要调用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: 超 上官