各位老师 :
请问 一下 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);