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: 

from string to class selection

7 REPLIES 7
Reply
Message 1 of 8
swarochisa
845 Views, 7 Replies

from string to class selection

Hello,

 

I have converted the current selection of an active document to string using this :

 

Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.ToString();

 

Now i want to convert this string back to a selection. how do i do this?

 

I have come across the method ToSelection in the DocumentCurrentSelection class. I couldn't find much information about it and I not sure how to use it correctly.

 

 

 

Thanks,

Swarochisa.

 

 

 

 

 

 

7 REPLIES 7
Message 2 of 8
ngombault
in reply to: swarochisa

Hi swarochisa,

 

could you elaborate a little on what you are trying to do by saving the selection?

 

A couple ideas to get you started:

 

- You can "save" the selection for later reuse in your code, you should store the Selection.SelectedItems into a ModelItemCollection. You can restore it using the Selection.CopyFrom(collection)

 

- If you need to be able to close Navisworks and retrieve the selection once you restart it, you can trying saving it as a SelectionSet.

 

- You can't really use ToString() to save an object to text, ToString is a generic function that returns useless data most of the time. If you want to save your object as text you will need to use Serialization, but I don't know how well it will work with ModelItems (the content of your selection)

 

Nic

Message 3 of 8
swarochisa
in reply to: ngombault

Hello Nic,

 

Thank you for the ideas , they were really helpful ,especially the serialization part. After reading a bit about it I think I can use it to get the job done(i'm not sure how) but I'll just go ahead and explain my situation in case you have another simpler solution.

 

I have 3 computer on a network that can communicate with each other through custom applications . I open the same navisworks file on each of the computers. I then select a few model items(save them as a collection) and decide to hide them on one computer . I want to achieve the same result on the remaining 2 computers without essentially repeating the steps manually i.e, Replicate a function that is performed on one computer, using API ,on the remaining two computers.

 

I already have the code in place to send data(using ToString) between the computers. I just need to convert that string data back to type selction so that i can use it to get my work done.I thought ToSelection  might get the job done but seems like it does not. Is there another way of doing this.

 

I hope you understood my problem . Also, could you redirect me toward some links that can help me with the serialization thing?

 

Thanks,

Swarochisa.

Message 4 of 8
ngombault
in reply to: swarochisa

Hi swarochisa,

 

Looks like you have to use Serialization for your project yes.

From my understanding though, serialization is not automatic and you depend on Navisworks developpers to have implemented it on the objects you are trying to serialize.

 

A work arround would be to create your own "serialization/deserialization", your own string representation of the selection to pass from one machine to another. Different options will depend on your models and your requirements but here are some ideas:

 

  • If nodes name in the tree are unique, you can save the tree path of each selected item when serializing on the source machine, and then follow these tree paths on the target machine to retrieve the items and add them one after another to the selection. something like: filename.nwd\node1\node2\selecteditemnode3
  • if node names are not unique you can use node indexes like: 1\1\2\3
  • if your items have unique values for a given property, you could pass the values for the selected items and create a Search on the target machine to retrieve them
  • from the AppInfo Plugin autodesk provides, it looks like each item has a InstanceHashCode that might be unique. I don't know to which extent you can use it but it might be worth looking into.

 

good luck, it sounds like an interesting project.

 

cheers,

Nic

 

Message 5 of 8
swarochisa
in reply to: ngombault

Thank you for your suggestions. They were really helpful, especially the 3rd point , I found some furthur reading on it and decided to proceed in that direction.

 

http://adndevblog.typepad.com/aec/2012/05/navisworks-net-api-find-item.html

 

I wanted to test it out and decided to write some code which will display the unique value each time I select a modelitem .But I am having a few problems with it . I have a method in place that gets exectued each time there is a change in the currentselection in the active document. When I start to debug the code It returns a NullReferenceException. I found out that the ActiveDocument is null in the code. 

public void oncurrentSelectionChanged()

{

Using Nw= Autodesk.Navisworks.Api;

Nw.ModelItemCollection osel = Nw.Application.ActiveDocument.CurrentSelection.SelectedItems;


try
{

if (!(osel.IsEmpty))
{
foreach (Nw.ModelItem oItem in osel)
{
   if (oItem.HasGeometry &&
oItem.PropertyCategories.FindPropertyByDisplayName
("Entity Handle", "Value").Value.ToInt32() != 0)
{
System.Windows.Forms.MessageBox.Show(oItem.PropertyCategories.FindPropertyByDisplayName("Entity Handle", "Value").Value.ToString());

}

}
}

}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}

 

Also, I am unable to use anything related to ComApi  inside foreach with the following error:

 

foreach statement cannot operate on variables of type 'Autodesk.Navisworks.Api.Interop.ComApi.InwOpSelection' because 'Autodesk.Navisworks.Api.Interop.ComApi.InwOpSelection' does not contain a public definition for 'GetEnumerator' 

 

Is there another way to about it?

 

Thanks,

Swaro.

Message 6 of 8
ngombault
in reply to: swarochisa

Hi swaro,

 

Regarding the debug nullreference I am guessing it is when you try to run the project directly in visual studio?

Unfortunately this is not possible (as far as I know) and you can only run your code from within Navisworks, either by loading your plugin in, OR using the CodeRun plugin that autodesk provides.

The CodeRun plugin is very primitive in its functionality but at least it lets you test some code on the fly without having to go through the entire plugin deployment.

 

I have limited knowledge of the ComApi library, but the foreach operator can only be used on objects that implement the IEnumerable interface, that includes a function called GetEnumerator. My guess is that the InwOpSelection object, like the CurrentSelection object, is not itself an enumeration but probably has a property that is.

So if you try foreach on the CurrentSelection object you will most likely get the same error; but if you try the foreach on the CurrentSelection.SelectedItems object it should work (provided that you are in Navisworks as explained in my first point).

 

On a side note, the code you posted is testing the HasGeometry property, which is only true if the selected node in the tree has direct geometry.

By selecting in the tree directly, or depending on the selection resolution option that is active, user can select 'high level' nodes that do not have geometry directly attached to them. I think you still want these items to be picked up...

 

hope that helps,

Nic

Message 7 of 8
ngombault
in reply to: ngombault

Actually I just find out that it is possible to debug line by line in Visual Studio (VS):

http://lezhang.net/2013/11/12/how-to-debugrun-navisworks-2013-api-programs/

 

You will need VS Pro or Ultimate to be able to do the setup from VS' user interface, otherwise you'll have to edit the proj file manually outside VS and then reopen VS.

 

lesson 4 of MyFirtRevitPlugin explains in details how to set it up for Revit even if you don't have VS Pro or Ultimate. Weird that it is so well documented by Autodesk for Revit and not Navisworks. Or I have missed something somewhere.

Message 8 of 8
swarochisa
in reply to: ngombault

Thank you for your replies Nic. though I've dropped the idea in this post and took another directon your ideas have been really helpful in solving my issues . I'm still stuck with the null reference exception . I've made a new post about it here : http://forums.autodesk.com/t5/navisworks-api/please-help-null-ref-error/td-p/5317825

If you could take a look into it and suggest something it would be of immense help. 

 

Thanks again.

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

Post to forums  

Rail Community


 

Autodesk Design & Make Report