Issue
Using the code posted earlier in the blog "Create hyperlinks for model objects using .NET API", I was able to attach links to a model item. Now, I want to find out if a given model item already has links attached to it or not. Could you tell me how to do it?
Solution
Once links are added, they are save as properties. You can use the same methods that you would do with other types of properties. (If you are new of Navisworks API, you may want to take a look at a post "Navisworks .NET API properties", for example. Also, make sure you install AppInfo plugin found in the SDK samples folder, which is a good tool to "snoop" into the Navisworks model structure. )
Depending on the context, there are a few approaches you can take. The bottom line is that links are stored under the category "LcOaExURLAttribute", then property named "LcOaIRLAttributeURL*". Below is the sample code. The first approach iterates through the list of properties and try to find properties whose internal names matches "LcOaIRLAttributeURL*". The second uses ModelItem.PropertyCategories.FindPropertyByName() with explicite names.
NwApp is an alias I set as follows:
using NwApp = Autodesk.Navisworks.Api.Application;
// Find Links with names "LcOaURLAttributeURL*"
private void FindLinks()
{
const string InternalNameUrl = "LcOaURLAttributeURL";
// Get the current document
Document doc = NwApp.ActiveDocument;
// Look through each selected objects
foreach (ModelItem item in doc.CurrentSelection.SelectedItems)
{
// Find by a property category for Hyperlinks
PropertyCategory propCat =
item.PropertyCategories.FindCategoryByName(
"LcOaExURLAttribute");
if (propCat == null)
{
Debug.WriteLine("<no hyperlinks>");
}
else
{
// We have hyperlinks. Print out.
Debug.WriteLine("We have hyperlinks.");
Debug.WriteLine("***" + propCat.ToString());
foreach (DataProperty dataProp in propCat.Properties)
{
// If you are interested in seeing all the data
// properties, uncomment this line.
//Debug.WriteLine(" " + dataProp.ToString());
// We are interested in only the names with
// LcOaURLAttributeURL*
if (dataProp.Name.Contains(InternalNameUrl))
{
// We have a URL in this data prop.
Debug.WriteLine(" *URL = " +
dataProp.Value.ToDisplayString());
}
}
}
}
}
Alternatively, if you know the internal category and property names, you can also use ModelItem.PropertyCategories.FindPropertyByName(). Below is a sample code:
// Find Links with FindPropertyByName() method
private void FindLinks2()
{
// Get the current document
Document doc = NwApp.ActiveDocument;
// Look through each selected objects
foreach (ModelItem item in doc.CurrentSelection.SelectedItems)
{
// Assuming that you used the code in the blog:
// http://adndevblog.typepad.com/aec/2012/05/
// create-hyperlinks-for-model-objects-using-net-api.html
// First, see if this item has a hyperlink
PropertyCategory propCat =
item.PropertyCategories.FindCategoryByName(
"LcOaExURLAttribute");
if (propCat == null)
{
Debug.WriteLine("<no hyperlinks>");
}
else
{
Debug.WriteLine("We have hyperlinks.");
}
// Check url's at data prop level
DataProperty url =
item.PropertyCategories.FindPropertyByName(
"LcOaExURLAttribute", // Internal category name
"LcOaURLAttributeURL"); // Internal property name
if (url != null)
{
Debug.WriteLine("url = " + url.Value.ToDisplayString());
}
// Same idea with other two url's.
DataProperty url1 =
item.PropertyCategories.FindPropertyByName(
"LcOaExURLAttribute",
"LcOaURLAttributeURL1");
if (url1 != null)
{
Debug.WriteLine("url1 = " + url1.Value.ToDisplayString());
}
DataProperty url2 =
item.PropertyCategories.FindPropertyByName(
"LcOaExURLAttribute",
"LcOaURLAttributeURL2");
if (url2 != null)
{
Debug.WriteLine("url2 = " + url2.Value.ToDisplayString());
}
}
}
