Navisworks .NET API 2013 new feature – saved viewpoint

By Xiaodong Liang

Saved Viewpoint presets views of the model. It enables to jump to viewpoints without having to navigate each time to reach an item. The saved viewpoints could be orignized within a folder. A folder could contain sub-folder. A saved viewpoint could also be a animation clip. Each element is a saved viewpoint.

2013 .NET API provides the following objects:

Document.SavedViewpoints: the saved viewpoints collection of a document.

SavedViewpoint: The object derives from GroupItem. It could be a specific saved viewpoint, or a folder (FolderItem), or the animation clips ( SavedViewpointAnimation)

SavedViewpoints.CurrentSavedViewpoint: The object can know  which saved viewpoint is displayed currently. Any view changes manually will cause it be null- anonymous viewpoint.

The code iterates the saved view collection. The code below dumps all saved viewpoints and switch current saved viewpoint to one whose name is “mysavedviewpoint1”.

 

void savedVPs()    {    Document oDoc =             Autodesk.Navisworks.Api.Application.ActiveDocument;         // the saved viewpoint we will set it to current    SavedViewpoint oSVP_to_SetCurrent = null;          // iterate the collection of saved viewpoints    foreach (SavedItem oSVP in             oDoc.SavedViewpoints.Value)    {        // if it is a folder/animation              if (oSVP.IsGroup)                  recurse(oSVP);            else            {            // Access the properties of the saved             //viewpoint                            SavedViewpoint oThisSVP =                oSVP as SavedViewpoint;                     if(oThisSVP != null)                {                    Debug.Print("Display Name" +                         oThisSVP.DisplayName);                    Viewpoint oVP =                         oThisSVP.Viewpoint;                         // dump the properties of Viewpoint                    // ......                         if(oThisSVP.DisplayName ==                         "mysavedviewpoint1")                        oSVP_to_SetCurrent = oThisSVP;                }            }    }    // current selected saved viewpoint        SavedViewpoint oCurViewPt =            oDoc.SavedViewpoints.CurrentSavedViewpoint                 as SavedViewpoint;             Viewpoint oCurrentVP =                         oCurViewPt.Viewpoint;             // dump the properties of current Viewpoint        // ......             // switch the current viewpoint to "myviewpoint1"        oDoc.SavedViewpoints.CurrentSavedViewpoint =                                     oSVP_to_SetCurrent;         }         void recurse(SavedItem oFolder)    {        foreach (SavedViewpoint oSVP in ((GroupItem)                                    oFolder).Children)        {      // if it is a folder/animation                                        if (oSVP.IsGroup)                recurse(oSVP);            else            {            // Access the properties of the saved             //viewpoint                                             }        }    }

The code below creates two saved viewpooints. One is copied from current viewpoint. The other moves the current viewpoint along X axis with value 10. And the code creates one folder and adds the first saved viewpoint to the folder. The other viewpoints is added directly to the saved viewpoints collection.

void createSavedVP_Folder()    {        Document oDoc =            Autodesk.Navisworks.Api.Application.ActiveDocument;             // Create a saved viewpoint with current viewpoint        SavedViewpoint oNewViewPt1 =            new SavedViewpoint(                oDoc.CurrentViewpoint.ToViewpoint());             oNewViewPt1.DisplayName = "MySavedView1";             // Create a viewpoint for the         // second saved viewpoint             Viewpoint oNewVP = new Viewpoint();        // Based on the current viewpoint,        // move camera along X+ with value 10        oNewVP =            oDoc.CurrentViewpoint.ToViewpoint().CreateCopy();        Point3D oNewPos =            new Point3D(oNewVP.Position.X + 10,                        oNewVP.Position.Y,                        oNewVP.Position.Z);        oNewVP.Position = oNewPos;        // Create the saved viewpoint         // with the new viewpoint        SavedViewpoint oNewViewPt2 =            new SavedViewpoint(oNewVP);        oNewViewPt2.DisplayName =            "MySavedView2";        // Add the saved viewpoints to the collection         oDoc.SavedViewpoints.AddCopy(oNewViewPt1);        oDoc.SavedViewpoints.AddCopy(oNewViewPt2);             FolderItem oNewViewPtFolder1 =            new FolderItem();        oNewViewPtFolder1.DisplayName = "Group1";             // put the saved viewpoint1 to group1        oNewViewPtFolder1.Children.Add(oNewViewPt1);             // add the group        oDoc.SavedViewpoints.AddCopy(oNewViewPtFolder1);        // add saved viewpoint        oDoc.SavedViewpoints.AddCopy(oNewViewPt2);    }

Comments

5 responses to “Navisworks .NET API 2013 new feature – saved viewpoint”

  1. Gregor Vilkner Avatar
    Gregor Vilkner

    Hi there,
    very good example. I have a few questions:
    1) How can i save a new SavedViewPoint directly into a folder? It seams i always have to save a copy (AddCopy()) into the SavedViewpoints collection before i can append the SavedViewPoint to a folder. the Children collection of a folder does not have a AddCopy method.
    2) In the GUI there is the option in the global settings “Viewpoint Defaults” section to save hide/show settings with a view. How is this possible with the api? I am not able to “attach” hide/view settings to a new SavedViewPoint that i create using the api.
    Thanks and kind regards,
    Gregor

  2. Xiaodong Liang Avatar
    Xiaodong Liang

    Hi Gregor ,
    I wrote a blog to answer your first question.
    http://adndevblog.typepad.com/aec/2012/07/move-saved-viewpoint-to-folder.html
    as to the second question, you will need COM API by InwOpView.ApplyHideAttribs ApplyMaterialAttribs.
    I hope I could post some blogs about how to access saved viewpoint by COM API soon.
    Regards,
    Xiaodong

  3. Farhan Avatar
    Farhan

    Can we get modelItems which is inside particular saved viewpoint ?

  4. Lotfi FERCHICHI Avatar
    Lotfi FERCHICHI

    how to import a viewpoint from xml into NW 2017

  5. Brian Smith Avatar
    Brian Smith

    Xiaodong Liang: Thank you for sharing this code! I used it to good effect to traverse through a tree structure with folders and Saved Viewpoints. It seems to get hung up on Animations though.
    I’ll keep looking for a way to determine id a SavedItem is an animation.
    Thanks again,
    -Brian Smith

Leave a Reply to Gregor VilknerCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading