In the other post we introduce how to add new saved viewpoints and folder. Sometimes we need to add new saved viewpoint to the existing folder, or move existing saved viewpoints to the folder. How to do? It could use
DocumentSavedViewpoints.Move
This method moves item at oldIndex in OldParent to newIndex in newParent. The parent could be the root of saved viewpoints collection or the folder. The following are two demos.
//add new saved viewpoints to the existing folder
private void addNewSavedVPtoOldFolder()
{
// assume the document has a saved viewpoint folder
// named "myFolder"
// get document
Document oDoc =
Autodesk.Navisworks.Api.Application.ActiveDocument;
// get the saved viewpoints
DocumentSavedViewpoints oSavePts =
oDoc.SavedViewpoints;
GroupItem oFolder =null;
foreach(SavedItem oEachItem in oSavePts.Value )
{
if (oEachItem.DisplayName == "MyNewFolder")
{
oFolder = oEachItem as GroupItem ;
break;
}
}
if(oFolder != null)
{
try
{
// Create a saved viewpoint with current viewpoint
SavedViewpoint oNewViewPt1 =
new SavedViewpoint(
oDoc.CurrentViewpoint.ToViewpoint());
oNewViewPt1.DisplayName = "MySavedView1";
// add the new saved viewpoint to the collection
oSavePts.AddCopy(oNewViewPt1);
// move the last saved viewpoint to the folder
oSavePts.Move(
oSavePts.RootItem, oSavePts.Value.Count-1,
oFolder, oFolder.Children.Count);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
//move two saved viewpoints to new folder
private void moveSavedVPtoNewFolder()
{
// assume the document has at least saved viewpoints
// get document
Document oDoc =
Autodesk.Navisworks.Api.Application.ActiveDocument;
// get the saved viewpoints
DocumentSavedViewpoints oSavePts =
oDoc.SavedViewpoints;
if (oSavePts.RootItem.Children.Count > 2)
{
// new a folder
FolderItem oFolder = new FolderItem();
oFolder.DisplayName = "MyNewFolder";
// add the folder to saved viewpoint collection
oDoc.SavedViewpoints.AddCopy(oFolder);
try
{
// move the first saved viewpoint to the folder
oSavePts.Move(
oSavePts.RootItem, 0,
(GroupItem)oDoc.SavedViewpoints.Value.
Last<SavedItem>(), 0);
// move the second saved viewpoint to the folder
oSavePts.Move(oSavePts.RootItem, 1,
(GroupItem)oDoc.SavedViewpoints.Value.
Last<SavedItem>(), 1);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("make sure the sets has at least two items");
}
}