Skip to content

Instantly share code, notes, and snippets.

@giobel
Last active September 30, 2025 06:35
Show Gist options
  • Select an option

  • Save giobel/7f2f8afb029775b8e37c8db7e96deb23 to your computer and use it in GitHub Desktop.

Select an option

Save giobel/7f2f8afb029775b8e37c8db7e96deb23 to your computer and use it in GitHub Desktop.
Navis
Snippet for navis addins
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using swf = System.Windows.Forms;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
using Autodesk.Navisworks.Api.Interop.ComApi;
using ComApiBridge = Autodesk.Navisworks.Api.ComApi.ComApiBridge;
using System.Runtime.CompilerServices;
namespace BasicPlugIn
{
[PluginAttribute("Test.ImportData", //Plugin name
"ADSK", //4 character Developer ID or GUID
ToolTip = "Test.ATestPlugin tool tip",//The tooltip for the item in the ribbon
DisplayName = "Import Properties from CSV")] //Display name for the Plugin in the Ribbon
public class ImportPropertiesPlugin : AddInPlugin
{
public override int Execute(params string[] parameters)
{
try
{
// current document (.NET)
Document doc = Application.ActiveDocument;
// current document (COM)
InwOpState10 cdoc = ComApiBridge.State;
// current selected items
ModelItemCollection items = doc.CurrentSelection.SelectedItems;
//swf.MessageBox.Show(Application.Gui.MainWindow, items.Count.ToString());
if (items.Count > 0)
{
foreach (ModelItem item in items)
{
// convert ModelItem to COM Path
InwOaPath citem = (InwOaPath)ComApiBridge.ToInwOaPath(item);
// Get item's PropertyCategoryCollection
InwGUIPropertyNode2 cpropcates = (InwGUIPropertyNode2)cdoc.GetGUIPropertyNode(citem, true);
//Get PropertyCategoryCollection data
InwGUIAttributesColl propCol = cpropcates.GUIAttributes();
//add timeliner + tab
InwOaPropertyVec newcate =(InwOaPropertyVec)cdoc.ObjectFactory(nwEObjectType.eObjectType_nwOaPropertyVec, null, null);
//cpropcates.SetUserDefined(1, "TimeLiner +", "TimeLiner +", newcate);
foreach (InwGUIAttribute2 i in propCol)
{
//if (i.UserDefined && i.ClassUserName == "Cat Test"){
if (i.ClassUserName == "TimeLiner")
{
//swf.MessageBox.Show(Application.Gui.MainWindow, i.ClassUserName);
//AddNewPropertyToExtgCategory(i);
cpropcates.SetUserDefined(1, "TimeLiner +", "TimeLiner +", AddNewPropertyToExtgCategory(i));
}
}
}
}
}
catch (Exception ex)
{
swf.MessageBox.Show(Application.Gui.MainWindow, ex.Message);
}
swf.MessageBox.Show(Application.Gui.MainWindow, "Done");
return 0;
}
private InwOaProperty AddProperty(InwOpState10 cdoc, string name, DateTime dt)
{
InwOaProperty prop = (InwOaProperty)cdoc.ObjectFactory(nwEObjectType.eObjectType_nwOaProperty, null, null);
prop.name = name;
prop.UserName = name;
var v = RuntimeHelpers.GetObjectValue(ToUtc(dt));
prop.value = v;
return prop;
}
private InwOaProperty AddProperty(InwOpState10 cdoc, string name, object dt)
{
InwOaProperty prop = (InwOaProperty)cdoc.ObjectFactory(nwEObjectType.eObjectType_nwOaProperty, null, null);
prop.name = name;
prop.UserName = name;
var v = dt;
prop.value = v;
return prop;
}
public InwOaPropertyVec AddNewPropertyToExtgCategory(InwGUIAttribute2 propertyCategory)
{
// COM state (document)
InwOpState10 cdoc = ComApiBridge.State;
// a new propertycategory object
InwOaPropertyVec category = (InwOaPropertyVec)cdoc.ObjectFactory(nwEObjectType.eObjectType_nwOaPropertyVec, null, null);
DateTime dt = DateTime.Now; // or your target datetime
category.Properties().Add(AddProperty(cdoc, "Dump Date", dt));
foreach (InwOaProperty property in propertyCategory.Properties())
{
if (property.name.Contains("Start (Actual)"))
{
swf.MessageBox.Show(Application.Gui.MainWindow, property.UserName);
category.Properties().Add(AddProperty(cdoc, property.name, property.value));
}
}
InwOaProperty newProp = (InwOaProperty)cdoc.ObjectFactory(nwEObjectType.eObjectType_nwOaProperty, null, null);
newProp.name = "Actual End Date";
newProp.UserName = "Actual End Date";
var v = RuntimeHelpers.GetObjectValue(ToUtc(dt));
newProp.value = v;
category.Properties().Add(newProp);
InwOaProperty asd = (InwOaProperty)cdoc.ObjectFactory(nwEObjectType.eObjectType_nwOaProperty, null, null);
asd.name = "Actual Start Date";
asd.UserName = "Actual Start Date";
var asdValue = RuntimeHelpers.GetObjectValue(ToUtc(dt));
asd.value = asdValue;
category.Properties().Add(asd);
return category;
}
private DateTime ToUtc(DateTime dt)
{
DateTime time = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond, DateTimeKind.Local);
return TimeZone.CurrentTimeZone.ToUniversalTime(time);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment