【MSCE C#】 如何实现(隐藏/显示)元素附加的属性值?

您好,我想追问是否能实现与原元素属性同样的属性(隐藏/显示)的方法?(如下图所示)

上图是想要实现的效果;

上图为现在实现的效果;

参照帖子:

https://communities.bentley.com/communities/other_communities/chinafirst/f/microstation-projectwise/188940/ec/556173#556173

https://communities.bentley.com/communities/other_communities/chinafirst/f/microstation-projectwise/207218/msce-c

测试代码:

1. 添加代码:

var specification = new InstanceLabelSpecification("false").CreatePopulatedCustomAttributeInstance();
ecClass.SetCustomAttribute(specification);

相关函数:

public static void CreateECDataTable(ECSchema ecSchema, IECClass iecClass)
{
	ECSchema ecSchema = new ECSchema(TestSchema, 1, 0, "Cjxjy");
    IECClass iecClass = ecSchema.AddClass(TestClass);
	iecClass.DisplayLabel = "测试属性表名";
	
	var specification = new InstanceLabelSpecification("false").CreatePopulatedCustomAttributeInstance();
	iecClass.SetCustomAttribute(specification);

	InitializeDataTable();

	foreach (DataRow item in dataTable.Rows)
	{
		string propertyName = item["column1"].ToString() + "_" + item["column2"].ToString() + "(" + item["column3"].ToString() + ")";
		string validName = propertyName;
		ECNameValidation.EncodeToValidName(ref validName);
		IECProperty iecProperty = iecClass.AddProperty(validName, ECObjects.StringType);
		iecProperty.DisplayLabel = propertyName;
	}

	SchemaImportStatus schemaImportStatus = DgnECManager.Manager.ImportSchema(ecSchema, Session.Instance.GetActiveDgnFile(), new ImportSchemaOptions());
	if (schemaImportStatus != SchemaImportStatus.Success) { return; }
}

public void AddInstanceToElement(Element element, Dictionary<string, string> dictionary)
{
	IECSchema iecSchema = GetECSchemaForDgnFile(TestSchema);
	IECClass iecClass = iecSchema.GetClass(TestClass);

	FindInstancesScope scope = FindInstancesScope.CreateScope(element, new FindInstancesScopeOption(DgnECHostType.Element, true));
	ECQuery query = new ECQuery(iecClass);
	using (DgnECInstanceCollection ecInstances = DgnECManager.Manager.FindInstances(scope, query))
	{
		if (ecInstances.Count() > 0) { foreach (IDgnECInstance dgnECInstance in ecInstances) { dgnECInstance.Delete(); } }
		DgnECInstanceEnabler instanceEnabler = DgnECManager.Manager.ObtainInstanceEnabler(Session.Instance.GetActiveDgnFile(), iecClass);
		StandaloneECDInstance instance = instanceEnabler.SharedWipInstance;

		foreach (var item in dictionary)
		{
			string validName = item.Key;
			ECNameValidation.EncodeToValidName(ref validName);
			if (item.Value != null)
			{
				string value = item.Value;
				instance.SetString(validName, value);
			}
		}
		
		instanceEnabler.CreateInstanceOnElement(element, instance, false);
	}
}

实现效果:

测试代码:

2. 添加代码:

var specification = new DisplayOptions(true).CreatePopulatedCustomAttributeInstance();
iecClass.SetCustomAttribute(specification);

相关函数:

public static void CreateECDataTable(ECSchema ecSchema, IECClass iecClass)
{
	ECSchema ecSchema = new ECSchema(TestSchema, 1, 0, "Cjxjy");
    IECClass iecClass = ecSchema.AddClass(TestClass);
	iecClass.DisplayLabel = "测试属性表名";
	
	var specification = new DisplayOptions(true).CreatePopulatedCustomAttributeInstance();
	iecClass.SetCustomAttribute(specification);

	InitializeDataTable();

	foreach (DataRow item in dataTable.Rows)
	{
		string propertyName = item["column1"].ToString() + "_" + item["column2"].ToString() + "(" + item["column3"].ToString() + ")";
		string validName = propertyName;
		ECNameValidation.EncodeToValidName(ref validName);
		IECProperty iecProperty = iecClass.AddProperty(validName, ECObjects.StringType);
		iecProperty.DisplayLabel = propertyName;
	}

	SchemaImportStatus schemaImportStatus = DgnECManager.Manager.ImportSchema(ecSchema, Session.Instance.GetActiveDgnFile(), new ImportSchemaOptions());
	if (schemaImportStatus != SchemaImportStatus.Success) { return; }
}

public void AddInstanceToElement(Element element, Dictionary<string, string> dictionary)
{
	IECSchema iecSchema = GetECSchemaForDgnFile(TestSchema);
	IECClass iecClass = iecSchema.GetClass(TestClass);

	FindInstancesScope scope = FindInstancesScope.CreateScope(element, new FindInstancesScopeOption(DgnECHostType.Element, true));
	ECQuery query = new ECQuery(iecClass);
	using (DgnECInstanceCollection ecInstances = DgnECManager.Manager.FindInstances(scope, query))
	{
		if (ecInstances.Count() > 0) { foreach (IDgnECInstance dgnECInstance in ecInstances) { dgnECInstance.Delete(); } }
		DgnECInstanceEnabler instanceEnabler = DgnECManager.Manager.ObtainInstanceEnabler(Session.Instance.GetActiveDgnFile(), iecClass);
		StandaloneECDInstance instance = instanceEnabler.SharedWipInstance;

		foreach (var item in dictionary)
		{
			string validName = item.Key;
			ECNameValidation.EncodeToValidName(ref validName);
			if (item.Value != null)
			{
				string value = item.Value;
				instance.SetString(validName, value);
			}
		}
		
		instanceEnabler.CreateInstanceOnElement(element, instance, false);
	}
}

实现效果:

测试代码:

3. 添加代码:

IECProperty iecProperty = iecClass.AddProperty(validName, ECObjects.StringType);
iecProperty.DisplayLabel = propertyName;
iecProperty.SetCustomAttribute(Bentley.ECObjects.UI.ECPropertyPane.CreateHidePropertyAttribute(true, true));

相关函数:

public static void CreateECDataTable(ECSchema ecSchema, IECClass iecClass)
{
	ECSchema ecSchema = new ECSchema(TestSchema, 1, 0, "Cjxjy");
    IECClass iecClass = ecSchema.AddClass(TestClass);
	iecClass.DisplayLabel = "测试属性表名";

	InitializeDataTable();

	foreach (DataRow item in dataTable.Rows)
	{
		string propertyName = item["column1"].ToString() + "_" + item["column2"].ToString() + "(" + item["column3"].ToString() + ")";
		string validName = propertyName;
		ECNameValidation.EncodeToValidName(ref validName);
		IECProperty iecProperty = iecClass.AddProperty(validName, ECObjects.StringType);
		iecProperty.DisplayLabel = propertyName;
	}

	SchemaImportStatus schemaImportStatus = DgnECManager.Manager.ImportSchema(ecSchema, Session.Instance.GetActiveDgnFile(), new ImportSchemaOptions());
	if (schemaImportStatus != SchemaImportStatus.Success) { return; }
}

public void AddInstanceToElement(Element element, Dictionary<string, string> dictionary)
{
	IECSchema iecSchema = GetECSchemaForDgnFile(TestSchema);
	IECClass iecClass = iecSchema.GetClass(TestClass);

	FindInstancesScope scope = FindInstancesScope.CreateScope(element, new FindInstancesScopeOption(DgnECHostType.Element, true));
	ECQuery query = new ECQuery(iecClass);
	using (DgnECInstanceCollection ecInstances = DgnECManager.Manager.FindInstances(scope, query))
	{
		if (ecInstances.Count() > 0) { foreach (IDgnECInstance dgnECInstance in ecInstances) { dgnECInstance.Delete(); } }
		DgnECInstanceEnabler instanceEnabler = DgnECManager.Manager.ObtainInstanceEnabler(Session.Instance.GetActiveDgnFile(), iecClass);
		StandaloneECDInstance instance = instanceEnabler.SharedWipInstance;

		foreach (var item in dictionary)
		{
			string validName = item.Key;
			ECNameValidation.EncodeToValidName(ref validName);
			if (item.Value != null)
			{
				string value = item.Value;
				instance.SetString(validName, value);
			}
		}
		
		IEnumerator<IECProperty> propertyEnum = instance.ClassDefinition.GetEnumerator();
		while (propertyEnum.MoveNext())
		{
			IECPropertyValue propertyValue = instance.GetPropertyValue(propertyEnum.Current.Name);
			if (propertyValue != null)
			{
				string strVal;
				propertyValue.TryGetStringValue(out strVal);
				propertyEnum.Current.SetCustomAttribute(ECPropertyPane.CreateHidePropertyAttribute(false, false));
			}
			else
			{
				propertyEnum.Current.SetCustomAttribute(ECPropertyPane.CreateHidePropertyAttribute(true, true));
			}
		}
		
		instanceEnabler.CreateInstanceOnElement(element, instance, false);
	}
}

实现效果:

1. 程序运行完的效果:

2. 再次打开DGN文件的效果

Parents Reply Children
  • 您好,我使用的版本是:OBD Update5 版本,培训期间未涉及ClassEditor软件,是否能实现您所说的功能?

  • 符老师说的是EditorCustomAttributes这个ECSchema里边的DontShowNullProperties这个ECClass,这是一个给其他ECClass添加CustomAttribute的ECClass,您在程序里边Load到DontShowNullProperties这个ECClass后,创建一个ECInstance,然后调用ECClass的对象实例的SetCustomAttribute可以设置这个ECClass下如果某个属性的值是空的话可以不显示。

  • 您好,我在 ECObjects API 中查找到 ECPropertyPane Class 中 CreateDontShowNullPropertiesAttribute 函数

    public static void CreateECDataTable(ECSchema ecSchema, IECClass iecClass)
    {
    	ECSchema ecSchema = new ECSchema(TestSchema, 1, 0, "Cjxjy");
        IECClass iecClass = ecSchema.AddClass(TestClass);
    	iecClass.DisplayLabel = "测试属性表名";
    	
    	var specification = ECPropertyPane.CreateDontShowNullPropertiesAttribute();
        iecClass.SetCustomAttribute(specification);
    
    	InitializeDataTable();
    
    	foreach (DataRow item in dataTable.Rows)
    	{
    		string propertyName = item["column1"].ToString() + "_" + item["column2"].ToString() + "(" + item["column3"].ToString() + ")";
    		string validName = propertyName;
    		ECNameValidation.EncodeToValidName(ref validName);
    		iecProperty.DisplayLabel = propertyName;
    	}
    
    	SchemaImportStatus schemaImportStatus = DgnECManager.Manager.ImportSchema(ecSchema, Session.Instance.GetActiveDgnFile(), new ImportSchemaOptions());
    	if (schemaImportStatus != SchemaImportStatus.Success) { return; }
    }

    <?xml version="1.0" encoding="utf-8"?>
    <ECSchema schemaName="SchemaTest" nameSpacePrefix="Cjxjy" version="1.0" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.2.0">
        <ECSchemaReference name="Bentley_Standard_CustomAttributes" version="01.13" prefix="bsca" />
        <ECClass typeName="ClassTest" displayLabel="测试属性表名" isDomainClass="True">
            <ECCustomAttributes>
                <DisplayOptions xmlns="Bentley_Standard_CustomAttributes.01.13">
                    <Hidden>True</Hidden>
                    <HideInstances>True</HideInstances>
                    <HideRelated>False</HideRelated>
                </DisplayOptions>
            </ECCustomAttributes>
            <ECProperty propertyName="__x5c5e____x6027____x540d__1___x6570____x636e____x540d__1__x0028____x6570____x636e____x5355____x4f4d__1__x0029__" typeName="string" displayLabel="属性名1_数据名1(数据单位1)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__1___x6570____x636e____x540d__2__x0028____x6570____x636e____x5355____x4f4d__2__x0029__" typeName="string" displayLabel="属性名1_数据名2(数据单位2)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__1___x6570____x636e____x540d__3__x0028____x6570____x636e____x5355____x4f4d__3__x0029__" typeName="string" displayLabel="属性名1_数据名3(数据单位3)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__1___x6570____x636e____x540d__4__x0028____x6570____x636e____x5355____x4f4d__4__x0029__" typeName="string" displayLabel="属性名1_数据名4(数据单位4)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__1___x6570____x636e____x540d__5__x0028____x6570____x636e____x5355____x4f4d__5__x0029__" typeName="string" displayLabel="属性名1_数据名5(数据单位5)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__2___x6570____x636e____x540d__6__x0028____x6570____x636e____x5355____x4f4d__6__x0029__" typeName="string" displayLabel="属性名2_数据名6(数据单位6)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__2___x6570____x636e____x540d__7__x0028____x6570____x636e____x5355____x4f4d__7__x0029__" typeName="string" displayLabel="属性名2_数据名7(数据单位7)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__2___x6570____x636e____x540d__8__x0028____x6570____x636e____x5355____x4f4d__8__x0029__" typeName="string" displayLabel="属性名2_数据名8(数据单位8)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__2___x6570____x636e____x540d__9__x0028____x6570____x636e____x5355____x4f4d__9__x0029__" typeName="string" displayLabel="属性名2_数据名9(数据单位9)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__2___x6570____x636e____x540d__10__x0028____x6570____x636e____x5355____x4f4d__10__x0029__" typeName="string" displayLabel="属性名2_数据名10(数据单位10)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__3___x6570____x636e____x540d__11__x0028____x6570____x636e____x5355____x4f4d__11__x0029__" typeName="string" displayLabel="属性名3_数据名11(数据单位11)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__3___x6570____x636e____x540d__12__x0028____x6570____x636e____x5355____x4f4d__12__x0029__" typeName="string" displayLabel="属性名3_数据名12(数据单位12)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__3___x6570____x636e____x540d__13__x0028____x6570____x636e____x5355____x4f4d__13__x0029__" typeName="string" displayLabel="属性名3_数据名13(数据单位13)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__3___x6570____x636e____x540d__14__x0028____x6570____x636e____x5355____x4f4d__14__x0029__" typeName="string" displayLabel="属性名3_数据名14(数据单位14)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__3___x6570____x636e____x540d__15__x0028____x6570____x636e____x5355____x4f4d__15__x0029__" typeName="string" displayLabel="属性名3_数据名15(数据单位15)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__4___x6570____x636e____x540d__16__x0028____x6570____x636e____x5355____x4f4d__16__x0029__" typeName="string" displayLabel="属性名4_数据名16(数据单位16)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__4___x6570____x636e____x540d__17__x0028____x6570____x636e____x5355____x4f4d__17__x0029__" typeName="string" displayLabel="属性名4_数据名17(数据单位17)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__4___x6570____x636e____x540d__18__x0028____x6570____x636e____x5355____x4f4d__18__x0029__" typeName="string" displayLabel="属性名4_数据名18(数据单位18)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__4___x6570____x636e____x540d__19__x0028____x6570____x636e____x5355____x4f4d__19__x0029__" typeName="string" displayLabel="属性名4_数据名19(数据单位19)" />
            <ECProperty propertyName="__x5c5e____x6027____x540d__4___x6570____x636e____x540d__20__x0028____x6570____x636e____x5355____x4f4d__20__x0029__" typeName="string" displayLabel="属性名4_数据名20(数据单位20)" />
        </ECClass>
    </ECSchema>

    问题还是存在于再次打开DGN文件的无值属性无法隐藏,

  • FindInstancesScope scopeTemp = FindInstancesScope.CreateScope(Session.Instance.GetActiveDgnFile(), new FindInstancesScopeOption());
                int verMaj = 1, verMin = 3;
                IECSchema schemaCustomAtt = DgnECManager.Manager.LocateSchemaInScope(scopeTemp, "EditorCustomAttributes", verMaj, verMin,
                          SchemaMatchType.LatestCompatible);
                if (schemaCustomAtt != null)
                {
                    IECClass ecClassCate= schemaCustomAtt.GetClass("DontShowNullProperties");
                    IECInstance ecInsCate = ecClassCate.CreateInstance();
                    ecClass.SetCustomAttribute(ecInsCate);
                }

    创建ECClass的时候添加这个属性

    Answer Verified By: 明昊 刘 

  • 您好,郭老师。添加隐藏无值属性后,选取多个元素,属性栏显示时,只显示共有有值属性。如下图:

    元素1,有两个附加属性:

    元素2,有六个附加属性:

    如何显示取并集的有值属性?