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: 

Autodesk.Navisworks.Api.Application.ActiveDocument.Models sorting

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
ulski1
4896 Views, 12 Replies

Autodesk.Navisworks.Api.Application.ActiveDocument.Models sorting

hi Navis API gurus,

I'm wrinting code to maintain nwf files. I'm appending and removing files in the nwf files. When I append files they always appear last in the selection tree even if they start with the letter a. In the GUI interface it is possible to do a sort manually but I havn't been able to do a "in place" sort of activedocument from code. I see that there are internal methods that might be useful like InternalInsert(int index, Model item); but I guess they are internal only.  I also tryed Autodesk.Navisworks.Api.Application.ActiveDocument.Models.OrderBy but that seems to create a new sorted list ( it is not a in place sort).

Any suggestions. I'm currently coding towards the 2012 api.

Ulrik

Tags (1)
12 REPLIES 12
Message 2 of 13
xiaodong_liang
in reply to: ulski1

Hi,

 

It looks to me the collection of Models in .NET API is read-only. The following is a workaround. It deletes all files and append them again with the new order. I know you would hate such way, but this may be the way we have to go currently.

 

using ComApi = Autodesk.Navisworks.Api.Interop.ComApi;
using ComBridge = Autodesk.Navisworks.Api.ComApi.ComApiBridge ;
 
namespace MyDemoPlugin
{
    [PluginAttribute("MyDemoPlugin", "ADSK", DisplayName = "demo")]
    public class Class1:AddInPlugin 
    {
        public override int Execute(params string[] parameters)
        {
            Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
 
            try
            {
                //sorting the names of the models             
                IEnumerable<Model> oNewSortedModels = oDoc.Models.OrderBy(per => per.RootItem.DisplayName);
                List<string> fileArray = new List<string>();
                foreach (Model oEachModel in oNewSortedModels)
                {
                    fileArray.Add(oEachModel.FileName );
                }
 
                //delete all files.
                
                foreach (Model oEachModel in oDoc.Models)
                {
                    // State.DeleteSelectedFiles failed to delete 
                    // all files at one time. 
                    // so have to delete them one by one.
                    ModelItemCollection oMC = new ModelItemCollection();
                    oMC.Add(oEachModel.RootItem);
                    oDoc.CurrentSelection.CopyFrom(oMC); 
                    ComBridge.State.DeleteSelectedFiles();
                } 
 
                //append them again with the new order
                oDoc.AppendFiles(fileArray);
            }
            catch(Exception ex)
            {
 
            }
 
            return 0;             
        }
    }
}
Message 3 of 13
ulski1
in reply to: xiaodong_liang

Hi Xiaodong Liang,

I'm already done with the code. I ended up with a solution where I first detect if the change only involves removal - in that case I don't need sorting (the objects are normally already sorted). In case I need to append files I need to remove all objects (as you also found out) expect index 0 (in order to not get an exception error) and then I append all the objects I need and after this I can delete index 0. The code looks really awful and I wanted to avoid this mess.

 

bg

Ulrik

 

 

 

Message 4 of 13
xiaodong_liang
in reply to: ulski1

Hi Ulrik,

 

Yes, this is not elegant and removing&appending models will cause performance issue.

 

One more workaround, if it is fine to you, is to build you own selection tree, thus you can manage them easily. For the built-in tree, the way is what we have found..

Message 5 of 13
ulski1
in reply to: xiaodong_liang

a custom selection tree might be a good idea. Are there any draw backs when creating/using a custom selection trees?  

I haven't looked into custom selection trees and I haven't found any sample code on how to work with a them perhaps you could direct me to some code samples.

 

Ulrik

 

Message 6 of 13
xiaodong_liang
in reply to: ulski1

Hi,

 

To build the custom tree, you just need to iterate the model items, children, decendents, and create the tree node one by one in the Tree View control of Windows Form.

 

If you work with WPF, the Tree View control can bind the hierarchy of Navisworks model items. Please refer to the blog below.

 

http://adndevblog.typepad.com/aec/2013/04/use-navisworks-api-with-wpf-binding-model-hierarchy-to-tre...

Message 7 of 13
ulski1
in reply to: xiaodong_liang

hi Xiaodong Liang,

Interesting blog post, I'll look more into it, but using wpf and having to work around issues with the render tread just to do a simple sort seems like overkill and I'm not sure it will work in nogui mode. I can always hope that someday they will add a (file level) in-place sort capability to the API

 

Ulrik

Message 8 of 13
xiaodong_liang
in reply to: ulski1

Hi Ulrik,

 

I logged a wish #44723: in-place sort capability (file level). But I cannot guarantee anything until it is implemented in the future release(s). 

 

 

 

Message 9 of 13
koeltzsch
in reply to: xiaodong_liang

Dear all,

 

can anyone say which is status of the wish #44723: in-place sort capability (file level)  ?

 

I come from my post here and I'm facing similar issue

http://forums.autodesk.com/t5/navisworks-general-discussion/sort-order-of-the-navisworks-selection-t...

 

One possible idea for me also is to have a workaorund coming from a little API program sorting the model tree (1st level only) whereas a built-in solution to enhance the scene-->sort function would be nice at all.

 

 

Regards,

danko

 

Message 10 of 13
xiaodong_liang
in reply to: koeltzsch

Hi Danko,

There is not any update about the wish #44723. Sorry if this is a bad news to you.
Message 11 of 13
jeremyltw
in reply to: xiaodong_liang

Hi XiaoDong,

 

I've tried to use your code to remove exsiting models and Navisworks crashes at the line 

 

 ComBridge.State.DeleteSelectedFiles();

I'm currently using Navisworks 2018; has there been any changes ?

 

Thanks.

Message 12 of 13
ulski1
in reply to: jeremyltw

hi,

perhaps you get this error because you try to delete the last remaining model - Navisworks doesn't allow you to have 0 models in a loaded nwf - so you need to have a check in your code to prevent this from happening (if count = 1 do not delete). If you want to unload all models from Navisworks like a "file new" you need to use Autodesk.Navisworks.Api.Application.MainDocument.Clear();

 

 edit: forgot to mention the delete statement works perfectly fine in 2018 - we run it on our rather massiv nwd production line.

br

Ulrik

Message 13 of 13
jeremyltw
in reply to: ulski1

Thanks ulski. The clear solved my issue.

 

Couldn't use the comapi to delete though despite using NW 2018.

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

Post to forums  

Rail Community


 

Autodesk Design & Make Report