Selected component(S) in assembly

Selected component(S) in assembly

pavol_krasnansky
Enthusiast Enthusiast
1,402 Views
7 Replies
Message 1 of 8

Selected component(S) in assembly

pavol_krasnansky
Enthusiast
Enthusiast

Hi
How can I check if a component is in an assembly is selcted? I need to create a list of the selected components including components in the pattern.
Thank you

 

Sub Main()
	
	Dim oDoc As Document
	oDoc = ThisDoc.Document
	
	If oDoc.DocumentType = kAssemblyDocumentObject Then
		
		Dim oAsmDoc As AssemblyDocument
		oAsmDoc = ThisApplication.ActiveDocument
		
		Count_Of_Selected_Item = oAsmDoc.SelectSet.Count
		MsgBox("Count_Of_Selected_Item" & vbCrLf & Count_Of_Selected_Item)
		
		Dim List_Of_Components As String
		List_Of_Components = ""
		
		Dim List_Of_Selected_Components As String
		List_Of_Selected_Components = ""
		
		For i = 1 To oAsmDoc.ComponentDefinition.Occurrences.Count
			
			List_Of_Components = List_Of_Components & vbCrLf & i & " - " & oAsmDoc.ComponentDefinition.Occurrences.Item(i).Name
			
			If oAsmDoc.ComponentDefinition.Occurrences.Item(i).IsSelected = True Then
				
				List_Of_Selected_Components = List_Of_Selected_Components & vbCrLf & i & " - " & oAsmDoc.ComponentDefinition.Occurrences.Item(i).Name
				
			End If
			
		Next
		
		MsgBox(List_Of_Components)
		MsgBox(List_Of_Selected_Components)
		
	End If
	
End Sub
0 Likes
Accepted solutions (1)
1,403 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @pavol_krasnansky.  Can you explain your overall goal here in more detail please.  What are you trying to achieve by creating two lists of component names? How do you plan on using those names after you get them?  Do you only want to search the top level components, or do you need to search into deeper levels of the assembly, like within sub assemblies?  Will you need to select a component name or names from a list later?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

jdasilvaS39UQ
Advocate
Advocate

You can get a list of selected occurrences with .SelectSet.

 

So in your case 

oAsmDoc.SelectSet

and then you can loop through all the items in that collection and add it to your string. 

0 Likes
Message 4 of 8

jdasilvaS39UQ
Advocate
Advocate

You can get a list of selected occurrences with .SelectSet.

 

So in your case 

 

oAsmDoc.SelectSet

 

and then you can loop through all the items in that collection and add it to your string. 

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

Here is a condensed version of a code very similar to what you currently have, just as a starting point.  But it would help to know what you are really hoping to achieve, and/or what you need two lists of component names for.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
Dim oADoc As AssemblyDocument = ThisDoc.Document
If oADoc.SelectSet.Count = 0 Then Exit Sub
Dim oSelectedOccNames As New List(Of String)
For Each oObj In oADoc.SelectSet
	If TypeOf oObj Is ComponentOccurrence Then
		Dim oSOcc As ComponentOccurrence = oObj
		oSelectedOccNames.Add(oSOcc.Name)
	End If
Next
If oSelectedOccNames.Count = 0 Then Exit Sub
oADef = oADoc.ComponentDefinition
oOccs = oADef.Occurrences
Dim oAllNames As New List(Of String)
For Each oOcc As ComponentOccurrence In oOccs
	oAllNames.Add(oOcc.Name)
Next
a = InputListBox("", oAllNames, "", "All Names", "List Of All Names")
b = InputListBox("", oSelectedOccNames, "", "Selected Names", "List Of Selected Names")

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8

pavol_krasnansky
Enthusiast
Enthusiast

Thank you for your reply. I want to create a macro for calculating mass properties (more properties in the future) of selected components in the assembly.Your code has the same problem as my code (see screenshots). I can not selected components in the pattern correctly.

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

OK.  Now that I know that you want to get Mass from the selected components, I can better help you achieve that.  The following iLogic code is designed to check within your SelectSet for both ComponentOccurrence objects and for OccurrencePattern objects.  And I changed the Type of data/object that is to be stored within the List object from String to ComponentOccurrence, so that it will be more useful to us in this task.  If a regular ComponentOccurrence is found in the SelectSet, it is directly added to the List.  But if a OccurrencePattern is found in the SelectSet, I start searching down within it to get the actual ComponentOccurrence objects, then add those to the List.  Once everything within the SelectSet is sorted through and all the ComponentOccurrence objects have been added to the List, the code then moves on to loop through that list and extract the Mass of each component, then add the individual masses together to show you the total mass of all components selected.  Then, because the mass is in 'database units' by default, the code checks if those units are the same as your document units.  If they are not the same, it proceeds to convert the units to document units for you.  Then uses a simple MsgBox to show you the result.

I hope this is closer to what you were trying to achieve.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
Dim oADoc As AssemblyDocument = ThisDoc.Document
If oADoc.SelectSet.Count = 0 Then Exit Sub
Dim oSelectedOccs As New List(Of ComponentOccurrence)
For Each oObj In oADoc.SelectSet
	If TypeOf oObj Is ComponentOccurrence Then
		Dim oSOcc As ComponentOccurrence = oObj
		oSelectedOccs.Add(oSOcc)
	ElseIf TypeOf oObj Is OccurrencePattern Then
		Dim oSOccPatt As OccurrencePattern = oObj
		For Each oOccPattE As OccurrencePatternElement In oSOccPatt.OccurrencePatternElements
			For Each oPattOcc As ComponentOccurrence In oOccPattE.Occurrences
				oSelectedOccs.Add(oPattOcc)
			Next
		Next
	End If
Next
If oSelectedOccs.Count = 0 Then Exit Sub
Dim oTotalMass, oMass As Double
For Each oOcc In oSelectedOccs
	'this will be in 'database units', so units conversion may be needed
	oMass = oOcc.MassProperties.Mass
	oTotalMass = oTotalMass + oMass
Next
UOM = oADoc.UnitsOfMeasure
If UOM.MassUnits <> UnitsTypeEnum.kDatabaseMassUnits Then
	oTotalMass = UOM.ConvertUnits(oTotalMass, UnitsTypeEnum.kDatabaseMassUnits, UOM.MassUnits)
End If
MsgBox("Total Mass of selected components = " & oTotalMass, vbInformation, "Mass Of Selected")

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 8

pavol_krasnansky
Enthusiast
Enthusiast

@WCrihfield Thank you very much! It works!

0 Likes