Count all parts with specific iProperties in main assembly and subasemblies

Count all parts with specific iProperties in main assembly and subasemblies

ILU
Enthusiast Enthusiast
319 Views
2 Replies
Message 1 of 3

Count all parts with specific iProperties in main assembly and subasemblies

ILU
Enthusiast
Enthusiast

hi There,

I am trying to figure out a easy way of counting all visible parts with certain iProperties. Until now I made the bellow code which is working OK but it is counting only parts that are in main assembly, I can't figure it out how to access the parts from mine subassemblies.

Tanks in advance for your help

 

 


'Assembly object as ComponentDefinition
Dim oAsmComp As AssemblyDocument = ThisDoc.Document
oAsmCompDef = oAsmComp.ComponentDefinition

Dim iQtyTotlal As Integer = 0
Dim iQty18MB As Integer = 0
Dim iQtyMDF As Integer = 0
Dim iQty38SP As Integer = 0
Dim iQty10MB As Integer = 0

For Each oOccurrence In oAsmCompDef.Occurrences
	Dim oName As String
	oName = oOccurrence.Name

	'18 MB
	Dim MB18 As String = iProperties.Value(oName, "Project", "Description")
	If MB18.Contains("18 MB") Then
		If Component.Visible(oName) = True Then
			iQty18MB += 1
			iQtyTotlal+=1
		End If
	End If
	
	Dim MB10 As String = iProperties.Value(oName, "Project", "Description")
	If MB18.Contains("10 MB") Then
		If Component.Visible(oName) = True Then
			iQty10MB += 1
			iQtyTotlal+=1
		End If
	End If

	If iProperties.Value(oName, "Project", "Description") = "38 SP" Then
		If Component.Visible(oName) = True Then
			iQty38SP += 1
			iQtyTotlal+=1
		End If
	End If

	If iProperties.Value(oName, "Project", "Description") = "18 MDF" Then
		If Component.Visible(oName) = True Then
			iQtyMDF += 1
			iQtyTotlal+=1
		End If
	End If
	'	End if	
Next
MsgBox("Into this assembly are: " & vbCrLf & vbCrLf & vbCrLf _
& iQty38SP & " - Visible 38mm Block-Panels." & vbCrLf & vbCrLf _
& iQty18MB & " - Visible 18mm Panels." & vbCrLf & vbCrLf _
& iQty10MB & " - Visible 10mm Panels." & vbCrLf & vbCrLf _
& iQtyMDF & " - Visible MDF's." & vbCrLf & vbCrLf & vbCrLf _
& iQtyTotlal & " - In Total." _
, vbOKOnly, "QTY of visible panels into this assemly")

 

 

0 Likes
Accepted solutions (1)
320 Views
2 Replies
Replies (2)
Message 2 of 3

A.Acheson
Mentor
Mentor
Accepted solution

Here is the recursive sub routines you are needing to traverse through the sub assemblies. Working with assemblies here is a good article. You will notice a function for working with the property sets. They give you more access to modify and to check iProperties and are a little more robust when working with multiple documents like drawings etc.  Here is a good article about working with API property sets and a there is a sub routine shown below  to  list all  of Property sets and Properties. 

 

Sub Main
	
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
	Dim oTransaction As Transaction = ThisApplication.TransactionManager.StartTransaction(oAsmDoc, "Search for visible components") 'Make this a single transaction

	TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences)

	oTransaction.End 'End the transaction
	
	MsgBox("Into this assembly are: " & vbCrLf & vbCrLf & vbCrLf _
	& iQty38SP & " - Visible 38mm Block-Panels." & vbCrLf & vbCrLf _
	& iQty18MB & " - Visible 18mm Panels." & vbCrLf & vbCrLf _
	& iQty10MB & " - Visible 10mm Panels." & vbCrLf & vbCrLf _
	& iQtyMDF & " - Visible MDF's." & vbCrLf & vbCrLf & vbCrLf _
	& iQtyTotlal & " - In Total." _
	, vbOKOnly, "QTY of visible panels into this assemly")

End Sub

Private Sub TraverseAssembly(Occurrences As ComponentOccurrences)
    ' Iterate through all of the occurrence in this collection.  This
    ' represents the occurrences at the top level of an assembly.

    Dim oOccDoc As Document
	For Each oOcc As ComponentOccurrence In Occurrences
		If oOcc.DefinitionDocumentType = kPartDocumentObject Then
			oOccDoc = oOcc.Definition.Document
			
			'Get iproperty using API Property Sets
			Dim sDescription As String = ReadProperty(oOccDoc, "Design Tracking Properties", "Description")
			
			'Get iproperty using ilogic
			'Dim sDescription As String = iProperties.Value(oOcc.Name, "Project", "Description")
		
			    If oOcc.Visible = True AndAlso sDescription.Contains("18 MB") Then
					iQty18MB += 1
					iQtyTotlal+=1
				ElseIf oOcc.Visible = True AndAlso sDescription.Contains("10 MB")Then
					iQty10MB += 1
					iQtyTotlal += 1
				ElseIf oOcc.Visible = True AndAlso sDescription.Contains("38 SP")Then
			 		iQty38SP += 1
					iQtyTotlal += 1
				ElseIf oOcc.Visible = True AndAlso sDescription.Contains("18 MDF")Then
					iQtyMDF += 1
					iQtyTotlal+=1
				End If 

		' Check to see if this occurrence represents a subassembly 
		'And recursively Call this Function To traverse through it.
        ElseIf oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			TraverseAssembly(oOcc.SubOccurrences)
        End If
    Next
End Sub

Dim iQtyTotlal As Integer 
Dim iQty18MB As Integer 
Dim iQtyMDF As Integer 
Dim iQty38SP As Integer 
Dim iQty10MB As Integer 

Public Function ReadProperty(doc As Document, setName As String,name As String) As String
    Dim propSet = doc.PropertySets.Item(setName)
    Dim prop = propSet.Item(name)
    Return prop.Value
End Function


Sub ListallPropertysets
  Dim doc As Document
 doc = ThisApplication.ActiveDocument
  
  Dim ps As PropertySet
  For Each ps In doc.PropertySets
   Logger.Info( ps.Name + " / " + ps.InternalName)

    Dim p As [Property]
    For Each p In ps
      Logger.Info( "  " + p.Name + " /" + Str(p.PropId))
    Next
  Next
End Sub
  

 

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

ILU
Enthusiast
Enthusiast

Thanks @A.Acheson it work like charm.

I do not really get what is with " function for the property sets " or how can it help me in mine future projects, but I am going to have a look over your links and try to understand as much as possible.

Thanks once again