iLogic check if part exists in assembly using its name

iLogic check if part exists in assembly using its name

Anonymous
Not applicable
4,388 Views
5 Replies
Message 1 of 6

iLogic check if part exists in assembly using its name

Anonymous
Not applicable

How can I check if a part/assembly exists in an assembly using it's name. I thought this code would do the trick but it's not working.

I have a part in my assembly which contains the text P1 inside it's browser name but PartExists still returns 0.

Dim doc As AssemblyDocument
doc = ThisDoc.Document

Dim oComp As ComponentOccurrence

Dim oComps As ComponentOccurrences
oComps = doc.ComponentDefinition.Occurrences

For Each oComp In oComps
	If oComp.Name.Contains("P1") Then
	PartExists = 1
	Else
	PartExists = 0
	End If
Next

MsgBox(PartExists)

 

0 Likes
Accepted solutions (1)
4,389 Views
5 Replies
Replies (5)
Message 2 of 6

MechMachineMan
Advisor
Advisor

Here is a quick practice for you.

 

Examine the rule below. Then run it from the top level assembly & examine the results.

 

Then ALSO run this rule within a Weldment type .iam. (Hint: You should notice something goofy here)

 

Good luck,

 

Sub Main()

Dim oCD As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition

MsgBox("Occs: " & oCD.Occurrences.Count & vbLf & _
       "Leaf Occs: " & oCD.Occurrences.AllLeafOccurrences.Count)

i = 0
   
For Each oLOcc In oCD.Occurrences.AllLeafOccurrences
	If i < 5
		oStr = oStr & " -- " & oLOcc.Name
		i = i + 1
	Else
		oStr = oStr & vbLf & oLOcc.Name
		i = 0
	End If
Next

MsgBox(oStr)
End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 6

Owner2229
Advisor
Advisor
Accepted solution

Hey, your initial code should work just fine, with just a little change (red highlighted are the actual changes I've made):

You can use "Dim PartExists As Integer = 0" and "PartExists = 1" instead if you wish.

 

Also, if you want to search all the sub-assemblies and sub parts, you should use ".AllLeafOccurrences", as @MechMachineMan suggested.

 

Dim oDoc As AssemblyDocument = ThisDoc.Document

Dim oComps As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences

Dim PartExists As Boolean = False
For Each oComp As ComponentOccurrence In oComps
	If oComp.Name.Contains("P1") Then
		PartExists = True
		Exit For
	End If
Next

MsgBox(PartExists)

 

Bear with me:

Your rule checks components in sequence (eg. looking for "P1"):

1) Checks "Something_P2" >> i = 0

2) Checks "Something_P1" >> i = 1

3) Checks "Something_P3" >> i = 0

So, the result you'll get is 0.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 4 of 6

Anonymous
Not applicable

@MechMachineMan Thanks Justin,

that helped me to understand the differences 

0 Likes
Message 5 of 6

Anonymous
Not applicable

@Owner2229 makes perfect sense. Thanks!

0 Likes
Message 6 of 6

Anonymous
Not applicable

Hi Mike,

 

i tried this code you helped to make a few years ago.

I am having issue with Exiting from the "FOR" Loop.

Error on Line 11 : Expression is not a method.

Error on Line 11 : Expression expected.

Error on Line 11 : Method arguments must be enclosed in parentheses.

 

Any chance you can point to where I screwed up would be greatly appreciated.

 

Dim oDoc As AssemblyDocument = ThisDoc.Document

Dim oComps As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences

Dim PartExists As Boolean = False
For Each oComp As ComponentOccurrence In oComps
	If oComp.Name.Contains("1009090") Then
		PartExists = True	
		Exit For
		Else
		End If			

Next

MsgBox(PartExists)

kind regards

MT

 

0 Likes