SelectSet.Select method how to use?

SelectSet.Select method how to use?

Cris-Ideas
Advisor Advisor
7,353 Views
25 Replies
Message 1 of 26

SelectSet.Select method how to use?

Cris-Ideas
Advisor
Advisor

Hi

I am trying to use Select.Set  object but I am failing to add objects to it.

 

Can someone advice please:

 

    Dim oComponentList As Inventor.SelectSet
    Dim obj As Object

        For Each obj In _invApp.ActiveDocument.SelectSet


            oComponentList.Select(obj)
            Debug.Print(obj.Name)
        Next
            Debug.Print(obj.Name)

Works fine, but for

oComponentList.Select(obj)

I an getting an error:plugin 1.png

 

 

Can someone explain me how shod I use .Select Method to add objects to Select.Set?

 

Cris.

 

Cris,
https://simply.engineering
0 Likes
7,354 Views
25 Replies
Replies (25)
Message 2 of 26

Cris-Ideas
Advisor
Advisor

I have tried something else.

 

This time second button gives an exception. firs one works fine.

 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        oComponentList = _invApp.ActiveDocument.SelectSet
        For Each obj In oComponentList
            Debug.Print(obj.Name)
        Next


    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        For Each obj In oComponentList
            Debug.Print(obj.Name)
        Next
    End Sub

 

Just as oComponentList was nothing while second button clicked.

 

Cris.

Cris,
https://simply.engineering
0 Likes
Message 3 of 26

MjDeck
Autodesk
Autodesk

Add the line:

oComponentList = _invApp.ActiveDocument.SelectSet

to the click routine for the second button as well. By default, that member will be Nothing. If yo uwant to use it, a value must be assigned to it.


Mike Deck
Software Developer
Autodesk, Inc.

Message 4 of 26

Cris-Ideas
Advisor
Advisor

OK.

But I thought I have assigned it in the first button. And I hoped it will store selection that was active in the assembly while first button was clicked.

Than when second button is clicked I hoped this will list all objects that had been selected while first button was clicked regardless of what is currently selected in the assembly modelling environment.

 

How can I achieve that?

I want to have set of objects that I can assign, in this case I want to assign to this set all objects that are selected while first button is clicked. An have this set available in the form.

 

I thought SelectSet is just an object I could create in my form an that would be different than app. SelectSet.

But is seems not be the case. I am forbidden to use NEW operator.

 

Cris.

 

 

Cris,
https://simply.engineering
0 Likes
Message 5 of 26

dgreatice
Collaborator
Collaborator

Hi,

 

Please explain more detail you workflow.

 

1. Show Form?

2. in Inventor select object(what object type?)

3. Click First Button to get object collection with selectset?

4. Click Second Button to Get information in collection selectset?

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 6 of 26

Cris-Ideas
Advisor
Advisor

Hi,

In the current state of my "program" development I need as follows:

 

1) form is showing

2) In inventor I select object. in assembly environment.  Any type, any number. No restrictions on what is selected

3) Click Button1. All objects in app.SelectSet are to be added to some object collection within my form. If my oObjectsList is not empty this objects are to be added to already existing OObjectsList

4) Selection in assembly is relist or changed during assembly inventor actions. (I do anything I in the assembly, as normal)

5) Click Button2 - I need my objects from oObjectsList available. (for the time being only to debug)

 

I am slowly making progress building what I need. Currently developing basing frame for the "program". (sorry lacking better word to describe).

 

Cris.

 

Cris,
https://simply.engineering
0 Likes
Message 7 of 26

Frederick_Law
Mentor
Mentor

@Cris-Ideas wrote:

 

 

I thought SelectSet is just an object I could create in my form an that would be different than app. SelectSet.

But is seems not be the case. I am forbidden to use NEW operator.

 

Cris.

 


Button2 might work if you click button1 first.

Move 

oComponentList = _invApp.ActiveDocument.SelectSet

 before the private sub.

Or put it as early as possible in the main sub or class.

0 Likes
Message 8 of 26

Cris-Ideas
Advisor
Advisor

But will this not make my oComponentList fixed so it will always be the same as current SelectSet of the active assembly?

 

 I nee a list of objects I can manipulate regardless of current selection in the assembly.

 

Cris.

Cris,
https://simply.engineering
0 Likes
Message 9 of 26

Frederick_Law
Mentor
Mentor

Since you code get SelectSet when program is run, oComponentList will keep its value until program is run again.

You could add code to check if oComponetList is Empty, if it is Empty then oComponentList = _invApp.ActiveDocument.SelectSet.

Or prompt user if they want to overwrite the list with new selection.

 

I think, you need to copy SelectSet or add item in SelectSet to oComponentList.  Setting oComponentList = SelectSet might result in Empty when SelectSet is cleared in Inventor.  Its like linking a file in Inventor instead of making a copy.

0 Likes
Message 10 of 26

Cris-Ideas
Advisor
Advisor

will check that.

 

But:

When I click Button1 I get proper debug print of the names of the objects being selected.

also in Button1 click even I am assigning

oComponentList = _invApp.ActiveDocument.SelectSet

but when I click Button2 after it seems that oComponentList does not have any contents and exception is thrown.

So obviously changes made to oComponentList when Button1 is clicked do not stay after event sub end.

 

So it looks more like oComponentList only points to SelectSet of the assembly rather than being an independent object in my form. And it only points to assembly SelectSet in button1 click event locally.

 

Cris.

Cris,
https://simply.engineering
0 Likes
Message 11 of 26

Frederick_Law
Mentor
Mentor

 

Dim oComponentList As ObjectCollection <- This in your main sub.

Use following in Botton1Click ->
Set oComponentList = ThisApplication.TransientObjects.CreateObjectCollection
Dim i As Long
For i = 1 To oDoc.SelectSet.Count
oComponentList.Add oDoc.SelectSet.Item(i)
Next

 

Message 12 of 26

MjDeck
Autodesk
Autodesk

That's right: SelectSet is not an independent object. If you want to save the current contents of the select set, you can create another object to do that.

Declare it in your form class (not within a Sub or Function)

Private savedSelectSet As New List(Of Object)

Then when you want to save the currently selected objects:

Dim doc = _invApp.ActiveDocument
savedSelectSet.Clear()
For Each obj In doc.SelectSet
	savedSelectSet.Add(obj)
Next

Or use CreateObjectCollection, as described by @Frederick_Law

But it must be declared at the class level. It can't be declared in Main or other Sub or Function. 


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 13 of 26

Cris-Ideas
Advisor
Advisor

@MjDeck

So I need further explanation in this regard.

Help for inventor API in the API reference section states:selectset.png

 

How than reading help can I distinguish objects that are "independent" and I can create them from those "not independent"

 

Cris.

 

 

 

Cris,
https://simply.engineering
0 Likes
Message 14 of 26

MjDeck
Autodesk
Autodesk

Scroll down on the SelectSet help page to the Accessed From section. It says:

Accessed From 
AssemblyDocument.SelectSet, Document.SelectSet, DrawingDocument.SelectSet, PartDocument.SelectSet, PresentationDocument.SelectSet

Notice that it doesn't list any functions to create a SelectSet. All the things it lists are properties of documents. So a select set is tied to a document while the document is open.

 

Almost all of the types in the API can't be created with a New statement in VB.  Instead, you get them from another API call. Many things can be created, but they're not independent. They're stored in the Inventor model. But a few things are independent, such as the objects in TransientGeometry and TransientObjects.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 15 of 26

Cris-Ideas
Advisor
Advisor

OK,

Will pay attention and post questions.

 

Will try solution you guys provided.

 

Cris.

Cris,
https://simply.engineering
0 Likes
Message 16 of 26

Cris-Ideas
Advisor
Advisor

@MjDeck

Sorry, I realised I need more help.

 

So assuming I will have object collection in my form and assign all objects from SelectSet to this collection.

 

What actually is stored in ObjectCollection? Are this references to assembly objects? or objects them selves (it sounds rather stupid but I really do not know).

 

My concern is what happens if after assigning some objects to my ObjectsCollection they will be lets say deleted, demoted, renamed, ,.....

 

Are there any problems I should expect?

 

Cris.

Cris,
https://simply.engineering
0 Likes
Message 17 of 26

Frederick_Law
Mentor
Mentor

It is "Assign By Reference".  The other is "Assign By Value".

All variables are memory location.

There are 2 ways to get info from them: By Value and By Reference.

If the variable is one of the basic type (integer, float), they're assign by value in default.

Otherwise most objects are assign by reference. ie you get memory location of the object instead of getting its contain.

Getting an object's properties is usually by value.

 

You might need a VB.net book to get the basic and detail on how it handles different variable types.

0 Likes
Message 18 of 26

Frederick_Law
Mentor
Mentor

@Cris-Ideas wrote:

 

What actually is stored in ObjectCollection? Are this references to assembly objects? or objects them selves (it sounds rather stupid but I really do not know).

Reference of the object, not a copy

 

My concern is what happens if after assigning some objects to my ObjectsCollection they will be lets say deleted, demoted, renamed, ,.....

If deleted, you'll have a null reference.  Otherwise you'll get the current object. 

 

Are there any problems I should expect?

 

Cris.


 

0 Likes
Message 19 of 26

Cris-Ideas
Advisor
Advisor

OK I expected ByReference

But still my concern is what happens when I delete object.

 

Than its reference points nowhere, or rather to some random data. 

But how can I test if Object I had is still there when I am trying to reference it? (Not is Nothing ?)

 

Also what happens when object, lets say part is demoted or promoted. Does it than becomes different object? or maybe sometimes yes and sometimes no.

 

Do I need to do anything to track this kind of changes or something is dealign with it internally?

 

Cris.

 

 

Cris,
https://simply.engineering
0 Likes
Message 20 of 26

MjDeck
Autodesk
Autodesk

The objects you get from the Inventor API are mostly references. Inventor doesn't give you a copy of the object at that moment in time. It gives you a reference to the object in the session. And things can change while you're holding on to it. For instance, if the name of a ComponentOccurrence gets changed, that will show up as a change to the Name property. But the reference to the ComponentOccurrence object itself will still be valid after the name change.
I'd have to check to see what happens when it's demoted or promoted.

If it gets deleted, your saved reference won't be any good. You can detect this condition by using a Try Catch block around statements that access the reference. If access to a simple property like Name fails, then it's likely that the reference has been deleted.

Or you can react to the delete operation when it happens, and use it to take the object out of your list. Add an event handler for the AssemblyEvents.OnDelete event.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes