Navisworks 2012 .NET API provides the ability to access TimeLiner information. Using the TimeLiner API, you can access TimeLiner document data, and create/query/modify tasks and task types as well as simulation appearances. You can also query model items attached to tasks, and define your own data source for tasks. In this article, we will look at these functionalities with Navisworks TimeLiner .NET API.
Getting TimeLiner Information
The top most objects of the TimeLiner API are IDocumentTimeliner and DocumentTimeliner. IDocumentTimeliner is an intermediary interface and is defined in the Autodesk.Navisworks.Api namespace. We use it to access Timeliner object from a given document. DocumentTimeliner class is defined in the Autodesk.Navisworks.Timeliner namespace. We use this to further access the information in the Timeliner object.
To access a Timeliner object, you first obtain IDocumentTimeliner object from the document, we then cast to the DocumentTimeliner class. Alternatively, we can access Timeliner object from a document using Timeliner.TimelinerDocumentExtensions.GetTimeliner(Document) method. We can then further access for more information from the properties and methods of the DocumentTimeliner object; for example, for further drilling down the data sources.
The code below shows a sample code to dump the data sources and tasks to the debug window:
// get Timeliner of the document
using Nw = Autodesk.Navisworks.Api;
using Tl = Autodesk.Navisworks.Api.Timeliner;
public void DumpTimeLinerInfo()
{
// get Timeliner of the document
Nw.Document doc = Nw.Application.ActiveDocument;
Nw.DocumentParts.IDocumentTimeliner tl = doc.Timeliner;
Tl.DocumentTimeliner tl_doc = (Tl.DocumentTimeliner)tl;
// alternatively, you can also use a static method that slightly
// simplifies the access to the DocumentTimeLiner slightly.
//Tl.DocumentTimeliner tl_doc2 = Tl.TimelinerDocumentExtensions.GetTimeliner(doc);
// dump some information of each data source
foreach (Tl.TimelinerDataSource oDS in tl_doc.DataSources)
{
Debug.Write("\n DisplayName: " + oDS.DisplayName +
"\n ProviderName: " + oDS.DataSourceProviderName);
foreach (Tl.TimelinerDataSourceField oTlF in oDS.AvailableFields)
{
Debug.Write("\n DisplayName: " + oTlF.DisplayName);
}
} // end dump data sources
// dump some information of each task
foreach (Tl.TimelinerTask oTask in tl_doc.Tasks)
{
Debug.Write("\n DisplayName: " + oTask.DisplayName +
"\n Planned Start: " + oTask.PlannedStartDate.ToString());
// you can also access selection of a given task
// (although we aren’ˉt printing out here.)
Tl.TimelinerSelection oTlSel = oTask.Selection;
if (oTlSel != null) {
if(oTlSel.HasExplicitSelection) {
Nw.ModelItemCollection oExplicitSel = oTlSel.ExplicitSelection;
}
if (oTlSel.HasSearch) {
Nw.Search oSearch = oTlSel.Search;
}
}
} //end dump tasks
}
Attaching a Selection to a Task
To attach the selection to a task, you first need to make a copy of the task, modify the copy and replace the original with the copy. The code below shows an example of attaching the current selection to all the tasks at the root level:
// attach a current selection to the time liner tasks at the root level
public void AttachSelection()
{
// get the Timeliner document part from an active document
Document main_doc = Application.ActiveDocument;
Tl.DocumentTimeliner doc_tl = Tl.TimelinerDocumentExtensions.GetTimeliner(main_doc);
// copy the current set of tasks. get the current selection.
GroupItem root_copy = doc_tl.TasksRoot.CreateCopy() as GroupItem;
Selection current = main_doc.CurrentSelection;
// walk through the tasks at the root level and attach the current selection
foreach (Tl.TimelinerTask task in root_copy.Children)
{ // attach the selection
task.Selection.CopyFrom(current);
}
// replace the timeliner tasks
doc_tl.TasksCopyFrom(root_copy.Children);
}