Select all instances of a component in the model browser

Select all instances of a component in the model browser

dlunaEVTVX
Participant Participant
1,894 Views
11 Replies
Message 1 of 12

Select all instances of a component in the model browser

dlunaEVTVX
Participant
Participant

Hello all,

I'm trying to select all instances of a part/assembly in the model browser. I know I can select all instances by several methods but for this case it is really important not only to see, but select all instances from the model browser (just like searching for them in the search textbox, then selecting each one of them).

Here's my attempt:

Dim oName As String = "A200"
Dim oDoc As Document = ThisApplication.ActiveDocument oDoc.BrowserPanes.ActivePane.SearchBox.Search(oName)

This searches for the part in the browser, but I need to select all instances.

Does anyone know how to do this?

0 Likes
1,895 Views
11 Replies
Replies (11)
Message 2 of 12

bradeneuropeArthur
Mentor
Mentor
Add this to a selection set.
Inventor.selectset!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 12

dlunaEVTVX
Participant
Participant

Hello,

I forgot to mention something really important, I'm working on a drawing document, so I want to select all instances of a part in the browser but since I have no parts displayed, the normal selectionset method is not working.

Sorry for not mentioning it before.

0 Likes
Message 4 of 12

jjstr8
Collaborator
Collaborator

It looks like this is a case that Autodesk didn't (or didn't want to) account for in the API.  SelectSet returns components in a drawing view as kUnknownObject/kGenericObject. Clearly Inventor internally knows what the selection is based on the context menu, but it's not exposed correctly in the API.  Even the context menu doesn't let you select all occurrences.  Your only option may be to brute-force your way through the browser nodes and set the Selected property.  You may have to scan through the browser nodes first to see what's selected unless you're using a different method to decide what to select.

0 Likes
Message 5 of 12

dlunaEVTVX
Participant
Participant

Yes I can select them with the name while in part document but it doesn't work inside the drawing. Do you know any way of selecting components from nodes? I can select, expand and manipulate nodes but I can't find a way to select components from the selected node.

0 Likes
Message 6 of 12

WCrihfield
Mentor
Mentor

Hi @dlunaEVTVX.  Out of curiosity, what do you plan to accomplish by selecting all of those browser nodes in your drawing?  Do you plan on doing something with them after you get them selected?  If you simply loop through the browser nodes and use DoSelect, it will just select one at a time (when you select the next one, the previous one is then no longer selected).  And BrowserNode objects are not really proper for adding to an ObjectCollection, or to the Document.SelectSet.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 12

WCrihfield
Mentor
Mentor

To select the ComponentOccurrence object that the node represents, you can use the BrowserNode.NativeObject.  But that will not work on every BrowserNode, so you will have to use something like a Try...Catch block around where you test its type and/or get the 'Object' within it.  Again though, even if you have a collection of ComponentOccurrence objects, keep in mind that each drawing view of that model will have copies of the same model nodes, so what you may be thinking of doing to each, might effect each one multiple times, depending on how/what you are trying to do.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 12

jjstr8
Collaborator
Collaborator

@dlunaEVTVX, After more digging, I found that the Selected property for a BrowserNode is read-only.  You would need to use the DoSelect method.  I'm not sure I understand your last statement "but I can't find a way to select components from the selected node"

0 Likes
Message 9 of 12

dlunaEVTVX
Participant
Participant

Hello

My goal is to highlight all of the component occurrences inside all of the the drawing views. After some attempts I found out the fastest way to achieve this is by looking for them in the browser.

0 Likes
Message 10 of 12

nedeljko.sovljanski
Advocate
Advocate

Hi @dlunaEVTVX 

If I understood you correctly, you are in the drawing document and you want to select all occurrences of some component given by Name in some particular view. That is not to much complicated. First you need to get model for that view. In that model find occurrences, and now is important part. Each occurrence has sub occurrences and you need to implement recursive method on that sub occurrences. In that method you are looking for occurrence with specific name.

0 Likes
Message 11 of 12

A.Acheson
Mentor
Mentor

Here is a post that will do what your looking for.  You will just need to change the filter criteria in the recursive sub routine to what you need. In my case I was looking for assemblies. It will also bring back workplanes, sketches/anything connected with that native object. It will place all valid nodes in a collection where you can run a command or highlight in your case. 

https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/running-a-command-on-t-amp-p-add-in-occ...

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 12 of 12

greggeorgi
Contributor
Contributor

Hi, when dealing with drawing selections that return the kUnknown / generic type, it can be much easier/faster to use the function ProcessViewSelection to get the underlying object instead of trying to scan the model tree.

 

https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-2C0A3F36-7F35-4C3F-BD3E-D02FE56735F1

 

 

Here is an example snippet. based on my own code which adds the component or document file path to a running list.

 

'If in drawing document many objects in select set return unknown type.
'There is a function built in to process this
'selectedObject is part of the document select set

Dim doc As Document = ThisDoc.Document
Dim selSet As SelectSet = doc.SelectSet

Dim filePaths As New List(Of String)

For Each selectedObject In selSet
	If selectedObject.Type = ObjectTypeEnum.kGenericObject Then
		If doc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			Dim obj As Object = Nothing ' Will store the processed object
			Dim drawingView As DrawingView = Nothing 'Will store the drawing view that the object is found in.
			
			CType(doc, DrawingDocument).ProcessViewSelection(selectedObject, oView, obj) 'oView & obj are byref parameters
			If obj IsNot Nothing Then
				'Use TypeOf because it captures sub types e.g. PartComponentDefinition/AssemblyComponentDefinition
				'Can test "obj.Type" as well. (Make sure to null check first!)
				If TypeOf obj Is ComponentOccurrence Then
					filePaths.Add(CType(obj, ComponentOccurrence).Definition.Document.FullFileName) 'Assumes FileSaveCounter > 0 as it's in a drawing
				ElseIf TypeOf obj Is Document Then
					filePaths.Add(CType(obj, Document).FullFileName) 'Assumes FileSaveCounter > 0 as it's in a drawing
				End If
			End If
		End If
	End If
Next

 

Hope this helps!