Get specific SE properties

Creator
Creator

Hello,

 

I have rather big assembly and I need to get just specific properties from each references.

 

Currently I am been using the following snippet:

 

 var properties = (SolidEdgeFramework.Properties)props.Item("Custom");
foreach (var field in fields)
{
string value = "";
try
{
var prop = properties.Item(field.name);
if (prop != null)
{
value = prop.get_Value() as string;
if (!string.IsNullOrEmpty(value))
assyStruct.Attributes.Add(field, value);
}
}
catch
{ }
}

 

If the property does not exit I get a assert which creates performance issue when dealing with big assemblies.

 

I can have more that 300 properties per each files so it is even worst to iterate to all existing properties.

 

Is there a nicer way to now if a specific property is availliable ?

 

Regards,

/Eric 

2 REPLIES

Re: Get specific SE properties

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Linq is your friend. All you have to do to enable it is:

 

 

using System.Linq;

 

That using statement enables extension methods like Enumerable.OfType<TResult> that enhances classes that implement the System.Collections.IEnumerable interface. If you look at the Interop definition of the SolidEdgeFramework.Properties interface, you'll see:

 

public interface Properties : System.Collections.IEnumerable
{
    ...
}

Now that you understand the background, this is how you can use it.

 

 

var application = SolidEdgeCommunity.SolidEdgeUtils.Connect();
var document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
var propertySets = (SolidEdgeFramework.PropertySets)document.Properties;
var customPropertySet = propertySets.Item("Custom");

// This returns an IEnumerable<SolidEdgeFramework.Property>
var properties = customPropertySet.OfType<SolidEdgeFramework.Property>();

// Enumerate all properties.
foreach (var property in properties)
{
    Console.WriteLine(property.Name);
}

// Get single property by name or null if the property does not exist.
var myProperty = properties.FirstOrDefault(x => x.Name.Equals("MyProperty"));

 

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Get specific SE properties

Creator
Creator

Thanks Jason,

 

// This returns an IEnumerable<SolidEdgeFramework.Property>
var properties = customPropertySet.OfType<SolidEdgeFramework.Property>();

This was what I was missing. Linq is more than my friends already Smiley Happy .

 

/Eric