iLogic count number of parts in assembly with certain iproperty

iLogic count number of parts in assembly with certain iproperty

jishee
Enthusiast Enthusiast
2,045 Views
4 Replies
Message 1 of 5

iLogic count number of parts in assembly with certain iproperty

jishee
Enthusiast
Enthusiast

I want to be able to count the number of parts in an assembly that have a certain custom iproperty value, i.e. I want to count the number of parts with an "Infill" iproperty that have the value of "I". I have the code below so far but it it not counting correctly. Any help is much appreciated.

'Define assembly
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument

'Define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Selected components collection
Dim oSelected As ObjectCollection
oSelected = ThisApplication.TransientObjects.CreateObjectCollection

Dim iQty As Integer

'Add to Object Collection
For iQty = 1 To asmDoc.SelectSet.Count
    If TypeOf asmDoc.SelectSet.Item(iQty) Is ComponentOccurrence Then
        oSelected.Add(asmDoc.SelectSet.Item(iQty))
    End If
Next

Dim Occ As ComponentOccurrence

If oSelected.Count >= 1 Then
        For iQty = 1 To oSelected.Count
        
            Occ = oSelected.Item(iQty)
            
            'Infill iProperty
            Dim propertyName1 As String = "Infill"
            Dim propertyValue1 As String = "I"
            customPropertySet = Occ.Definition.Document.PropertySets.Item("Inventor User Defined Properties")
            
            Try
            prop = customPropertySet.Item(propertyName1)
            prop.Value = propertyValue1
            Catch
            'Assume error means not found
            customPropertySet.Add("", propertyName1)
            prop = customPropertySet.Item(propertyName1)
            prop.Value = propertyValue1
            End Try
        Next
End If
Accepted solutions (1)
2,046 Views
4 Replies
Replies (4)
Message 2 of 5

JhoelForshav
Mentor
Mentor
Accepted solution

@jishee 

Are you simply trying to count all occurrences in the entire assembly with this property value?

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim nrOfOccs As Integer = 0
For Each oDoc As Document In oAsm.AllReferencedDocuments
	Try
		If oDoc.PropertySets.Item("Inventor User Defined Properties").Item("Infill").Value = "I"
			nrOfOccs += oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count
		End If
	Catch
	End Try
Next
MsgBox("Number of occurrences with Infill = I: " & nrOfOccs)
Message 3 of 5

bradeneuropeArthur
Mentor
Mentor

Hi,

 

Would you like to do this recursive or only for the direct children of the assembly?

 

Regards,

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 4 of 5

WCrihfield
Mentor
Mentor

From the last part of your original code, it looks like you are wanting to create this custom iProperty if it doesn't already exist.  Is that what you want?  It also looks like if that iProperty already exists, you want to change its value to "I".  Is that also what you want?

  • The following iLogic rule will search all Component Occurrences for that iProperty.
  • If found, it checks if its value =  "I".
    • If true, it adds to the iQty count.
    • If not, its value is changed to "I".
  • If not found, it is created, and set to your value.
  • Then the finishing MsgBox telling you how many initially had that specific iProperty, with that Value, before they were updated.

 

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
Dim oExists As Boolean = False
Dim iQty As Integer = 0
Dim oOccDoc As Document
Dim oPropSet As PropertySet
Dim oProp As [Property]
For Each oOcc As ComponentOccurrence In oADef.Occurrences
	oOccDoc = oOcc.Definition.Document
	oPropSet = oOccDoc.PropertySets.Item("Inventor User Defined Properties")
	For Each oProp In oPropSet
		If oProp.Name = "Infill" Then
			If oProp.Value = "I" Then iQty = iQty + 1
			oProp.Value = "I"
			oExists = True
		End If
	Next
	If oExists = False Then
		oProp = oPropSet.Add("I", "Infill")
	End If
Next
MsgBox("There were " & iQty & " Components with that Property Value.",vbOKOnly,"")

 

I hope this helps.
If this solves your problem, or answers your questions, please click 'Accept As Solution".
Or, if this helps you reach your goal, please click 'LIKES" 👍.

 

Also, if you're interested, here are a few of the 'Ideas' I'd like to get implemented.
If you agree with any of them, please vote for them.

  • Add more capabilities to the 'Customize' dialog box (exe. Add Tab & Add Panel) Click Here
  • Constrain & Dimension Images In Assembly Sketches & Drawing Sketches (TitleBlocks & SketchedSymbols) Click Here
  • Save Section View Status In DesignViewRepresentation (So It Can Be Used In The Drawing) Click Here
  • Add SolidBodies Folder In iLogic Rule Editor Model Tab Click Here
  • Convert All Views To Raster Before Autosave Stores To 'OldVersions' Folder Click Here
  • SetDesignViewRepresentation - Fix limitations for DrawingView of a Part Click Here
  • Create DocumentSubTypeEnum Click Here
  • Create Add kRevisionTag or kDrawingRevisionTag to ObjectTypeEnum Click Here

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 5

jishee
Enthusiast
Enthusiast

I just simply wanted to count the occurrences. I was really over complicating things I see now. Thanks for your help @JhoelForshav !

0 Likes