Community
Navisworks API
Welcome to Autodesk’s Navisworks API Forums. Share your knowledge, ask questions, and explore popular Navisworks API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Cannot change GUID of SavedViewpoint

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
k-horiYJ27A
559 Views, 5 Replies

Cannot change GUID of SavedViewpoint

I am currently developing a plugin to add models in a hidden state.

To do this, I need to hide and update the model in all views after it is added.

However, when I update the views using the COM API, the Guid of the Savedviewpoint is unified to a series of 0's.
That is very annoying because in my program I am switching views by referencing the GUID.
Changing the Guid of the SavedViewpoint is not reflected.
Does anyone know how to change the Guid of the SavedViewpoint?
Or do you know of any other way to switch views?

Thanks in advance!

 

// 追加モデルを非表示にする関数
private void HiddenModelForViewpoints(ModelItemCollection hidden, List<SavedViewpoint> viewpoints, List<Guid> guids)
{
    DocumentModels models = activeDocument.Models;
    InwOpState10 state = ComApiBridge.State;

    for (int i = 0; i < viewpoints.Count; i++)
    {
        // 現在のビューポイントを更新する
        Guid orgGuid = viewpoints[i].Guid;
        guids.Add(orgGuid);
        var crVp = (SavedViewpoint)activeDocument.SavedViewpoints.ResolveGuid(orgGuid);
        activeDocument.SavedViewpoints.CurrentSavedViewpoint = crVp;

        // モデルを非表示
        models.SetHidden(hidden, true);

        // 元のビューの外観の表示非表示保存の設定
        bool hideOption = viewpoints[i].ContainsVisibilityOverrides;

        // ビューを更新
        TraverceSavedViews(state, state.SavedViews(), viewpoints[i].DisplayName, hideOption);

    }
}

private static void TraverceSavedViews(InwOpState10 state, InwSavedViewsColl view_coll, string name, bool hidOpt)
{
    var index = 1;
    // Savedviewpointsの中を走査するのと同じ
    foreach (InwOpSavedView item in view_coll)
    {
        // itemのタイプを確認(ビューかフォルダか)
        switch (item.Type)
        {
            case nwESavedViewType.eSavedViewType_View:
                {
                    var view = (InwOpView)item;
                    // 指定した名前と同じ
                    if (name.Equals(view.name))
                    {
                        var saved_view = (InwOpView)state.ObjectFactory(nwEObjectType.eObjectType_nwOpView);
                        saved_view.name = name;
                        if (hidOpt)
                        {
                            saved_view.ApplyHideAttribs = true; // 外観のオーバーライドを保存設定に変更
                        }
                        var currentViewpoint = (InwNvViewPoint2)((InwOpAnonView)state.CurrentView).ViewPoint.Copy(); // 現在の視点をコピー
                        saved_view.anonview.ViewPoint = currentViewpoint; // 視点を設定
                        view_coll.Replace(index, saved_view);
                        return;
                    }
                }
                break;
            // フォルダの場合は再帰処理
            case nwESavedViewType.eSavedViewType_Folder:
                {
                    var folder = (InwOpFolderView)item;
                    TraverceSavedViews(state, folder.SavedViews(), name, hidOpt);
                }
                break;
            // アニメーションとカットの場合はなにも行わない
            case nwESavedViewType.eSavedViewType_Anim:
            case nwESavedViewType.eSavedViewType_Cut:
                break;
        }
        index++;
    }
}

private void ResetGrobalID(Collection<SavedItem> items)
{
    for (int i = 0; i < items.Count; i++)
    {
        // ビューポイントだった場合の処理
        if (items[i].GetType().ToString() == "Autodesk.Navisworks.Api.SavedViewpoint")
        {
            MessageBox.Show(String.Concat("変更前ガイド:", items[i].Guid));
            Guid newGuid = Guid.NewGuid();
            MessageBox.Show(String.Concat("新規作成ガイド:", newGuid));
            items[i].Guid = newGuid;
            MessageBox.Show(String.Concat("変更後ガイド:", items[i].Guid));
        }
        // フォルダだった場合は再帰処理
        else if (items[i].GetType().ToString() == "Autodesk.Navisworks.Api.FolderItem")
        {
            // フォルダの中にビューがある場合の処理
            if (items[i].IsGroup)
            {
                FolderItem fItem = items[i] as FolderItem;
                Collection<SavedItem> _items = new Collection<SavedItem>();
                foreach (SavedItem child in fItem.Children)
                {
                    _items.Add(child);
                }
                ResetGrobalID(_items);
            }
        }
    }
}

 

Labels (1)
5 REPLIES 5
Message 2 of 6

Hi @k-horiYJ27A ,

 

As far as I am aware, the saved viewpoints lack a unique identifier. Although the Viewpoints do possess a GUID parameter, it consistently returns a series of zeros for all saved viewpoints [00000000-0000-0000-0000-000000000000].

 

It seems that Navisworks does not currently generate unique GUIDs for viewpoints.

 

In a file, the full path (i.e. folder and name) should be unique. This approach can be utilized to precisely identify the viewpoints.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 3 of 6

Apparently so.
I was able to solve this problem by using an alternative method to get the index path of the viewpoint and use the index path to switch views!
It was quite hard, but I think I managed to implement it...!
I'm sharing the code for your reference, although it's not very pretty since I don't usually use C#.😁

 

Collection<SavedItem> copy = activeDocument.SavedViewpoints.CreateCopy();
List<IEnumerable<int>> viewIndexs = new List<IEnumerable<int>>();   // ビューポイントのインデックスのリストを保存する為のリスト
List<int> idx = new List<int>();
GetSavedViewpointIndex(copy, viewIndexs, idx); // インデックスを取得

// 全てのインデックスを取得する関数
private void GetSavedViewpointIndex(Collection<SavedItem> items, List<IEnumerable<int>> indexList, List<int> index)
{

    for (int i = 0; i < items.Count; i++)
    {
        // ビューポイントだった場合の処理
        if (items[i].GetType().ToString() == "Autodesk.Navisworks.Api.SavedViewpoint")
        {
            if (items[i].Parent == null)
            {
                indexList.Add(new List<int>() {i});
            }
            else
            {
                List<int> setIndex = new List<int>();
                for (int j = 0; j < index.Count; j++)
                {
                    setIndex.Add(index[j]);
                }
                setIndex.Add(i);
                indexList.Add(setIndex);
            }
        }
        // フォルダだった場合は再帰処理
        else if (items[i].GetType().ToString() == "Autodesk.Navisworks.Api.FolderItem")
        {
            // フォルダの中にビューがある場合の処理
            if (items[i].IsGroup)
            {
                FolderItem fItem = items[i] as FolderItem;
                Collection<SavedItem> _items = new Collection<SavedItem>();
                foreach (SavedItem child in fItem.Children)
                {
                    _items.Add(child);
                }
                if (items[i].Parent == null)
                {
                    List<int> parentIndex = new List<int>() { i };
                    GetSavedViewpointIndex(_items, indexList, parentIndex);
                }
                else
                {
                    index.Add(i);
                    GetSavedViewpointIndex(_items, indexList, index);
                }
            }
        }
    }
}


// 現在のビューポイントを更新する
IEnumerable<int> currentIndex = setIndexs[i];
var crVp = (SavedViewpoint)activeDocument.SavedViewpoints.ResolveIndexPath(currentIndex);
activeDocument.SavedViewpoints.CurrentSavedViewpoint = crVp;

 

Message 4 of 6

Incidentally, is there any way that the existing NavisWorks functionality would be adversely affected by the fact that the SavedViewpoint Guid is all a series of zeros?

Message 5 of 6

Just saying viewpoints can have comments so technically you could use the comments to store data on the viewpoints such as a custom GUID. May I ask what your goal is in using a GUID to identify a saved viewpoint?

Message 6 of 6

FYI 1: It seems that guids actually are not 0 for Viewpoints (at least in Navis 2023 that I am testing on). Just create a new viewpoint/animation/folder in UI and export XML of all the Viewpoints - the guids are there.
It might be that the guids are not existing (zero) for older models/viewpoints. I patch that situation by checking if the guid is 0 and replacing the SavedViewpoint in such a case using:
Autodesk.Navisworks.Api.Application.ActiveDocument.SavedViewpoints.ReplaceWithCopy(GroupItem, int, SavedItem); and it seems to work. Just make sure to get parent for SavedItem directly got from SavedViewpoints, NOT A COPY (by method Autodesk.Navisworks.Api.Application.ActiveDocument.SavedViewpoints.ToSavedItemCollection();)

maciej_paechR2QL5_0-1713444130419.png


FYI 2: There is a built in method that can get the index path for you:
Autodesk.Navisworks.Api.Application.ActiveDocument.SavedViewpoints.CreateIndexPath(SavedItem item)
Just make sure to provide a SavedItem directly got from SavedViewpoints, NOT A COPY.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


 

Autodesk Design & Make Report