Summary
Semantic’s data properties can be accessed ‘programmatically’ allowing users to perform custom data automation with a .NET application. This article discusses the basic approach.
Semantic serializes data to the Rhino objects and document. Inspecting the “UserStrings” of a Rhino object with Semantic properties reveals that the properties are keyed to unique IDs (GUIDs) that refer to a defined property stored at the document level.
The PGData DLL
Semantic ships with a DLL called ProvingGround.PGData.dll that includes methods for serializing and deserializing Semantic properties within the document.
You can set reference to this DLL in another .NET Rhino plugin to build an application for working with Semantic properties. From there you can using the namespace within your project.
using ProvingGround.PGData;
Getting the Document Property Definitions
Because Semantic’s object properties uses a GUID lookup, we have to get a collection of the available properties at the document level. Here is an example of iterating through the document-level strings to find the serialized “PGDataCollection” which define the available properties.
The “clsPGDataCollection” class is an enumerable collection of Semantic property definitions. Here you can get data about available property IDs, names, types, groups, and categories.
private void GetDataCollection()
{
string hexDataCollectionString = _doc.Strings.GetValue("PGDataCollection");
if (!string.IsNullOrEmpty(hexDataCollectionString))
{
try
{
_pgDataCollection = clsPGDataCollection.Deserialize(hexDataCollectionString);
}
catch (Exception ex)
{
_pgDataCollection = new clsPGDataCollection();
}
}
}
Lookup Object Properties
Using the Document-level property definitions, it is possible to lookup Object-level properties.
List<clsPGDataProperty> dataLookup = GetDataCollection();
File3dmObject obj = [some Rhino object];
// get the object property key (aligns with the Document property ID)
foreach (string key in obj.Attributes.GetUserStrings().AllKeys)
{
// guid-value pair for the object property
string guid = key;
string propertyValue = obj.Attributes.GetUserString(key);
// lookup the Document property definition
using the guid
foreach (clsPGDataProperty p in dataLookup)
{
if (guid == p.Id)
{
// get the property name
string propertyName = p.Name;
// if the property is a 'category' property (re: Drop down values) then you have to lookup the category definition also
if (p.Type == "Category")
{
if (p.Categories.ContainsKey(propertyValue))
{
// gets the readable value of the category property
propertyValue = p.Categories[propertyValue].Item1;
}
}
}
}
}