TimelinerSelection.CopyFrom allows you copy explicit selected items, a Search, a Selection, SelectionSourceCollection. SelectionSourceCollection is a collection of SelectionSources, while SelectionSource represents a reference to a source that can generate a selection on demand, generally a kind of middleware when you want to attach SelectionSet to Timeliner task.
The following code demos the two scenarios. It assumes the document has two Timeliner tasks, current search is available, and at least one SelectionSet exists. The code checks the tasks in first level only. It will attach current Search to the first task, and attach first SelectionSet to the second task.
using Nw = Autodesk.Navisworks.Api;
using Tl = Autodesk.Navisworks.Api.Timeliner;
public override int Execute(params string[] parameters)
{
Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
try
{
Nw.Document doc = Nw.Application.ActiveDocument;
Nw.DocumentParts.IDocumentTimeliner tl = doc.Timeliner;
Tl.DocumentTimeliner tl_doc = (Tl.DocumentTimeliner)tl;
//assume we check the tasks in first level only
SavedItem parentItem = tl_doc.TasksRoot;
if (tl_doc.Tasks.Count > 1)
{
//test with the first and second tasks only
//first task, attach a Search Set
Tl.TimelinerTask oTask1 = tl_doc.Tasks[0] as Tl.TimelinerTask;
if (!doc.CurrentSearch.ToSearch().IsClear)
{
//if a current search exists
SelectionSet oMySet = new SelectionSet(oDoc.CurrentSearch.ToSearch());
//attach selection set to the task
Tl.TimelinerTask oCopyTask1 = oTask1.CreateCopy();
oCopyTask1.Selection.CopyFrom(doc.CurrentSearch.ToSearch());
tl_doc.TaskReplaceWithCopy((GroupItem)parentItem, 0, oCopyTask1);
}
//second task, attach a Selection Set
Tl.TimelinerTask oTask2 = tl_doc.Tasks[1] as Tl.TimelinerTask;
if (doc.SelectionSets.RootItem.Children.Count > 0)
{
SelectionSet oOneSet = doc.SelectionSets.RootItem.Children[0] as SelectionSet ;
SelectionSourceCollection oSetSourceColl = new SelectionSourceCollection();
SelectionSource oSetSource =doc.SelectionSets.CreateSelectionSource(oOneSet);
oSetSourceColl.Add(oSetSource);
//attach selection set to the task
Tl.TimelinerTask oCopyTask2 = oTask2.CreateCopy();
oCopyTask2.Selection.CopyFrom(oSetSourceColl);
tl_doc.TaskReplaceWithCopy((GroupItem)parentItem, 1, oCopyTask2);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return 0;
}</pre
>

Leave a Reply to Technostruct LLCCancel reply