Change visibility at parts containing "BQ"

Change visibility at parts containing "BQ"

autodeskpelinder
Enthusiast Enthusiast
830 Views
12 Replies
Message 1 of 13

Change visibility at parts containing "BQ"

autodeskpelinder
Enthusiast
Enthusiast

I like to change visibility for all parts in a assembly (and sub assemblys) to unvisibility if the file namne contain "BQ".

I tried this, but it is not working:

 

Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition

Dim occ As Inventor.ComponentOccurrence
For Each occ In assemblyDef.Occurrences.AllLeafOccurrences
Dim refDoc As PartDocument = occ.Definition.Document

If iProperties.Value("Project", "Stock Number").Contains("BQ") Then
occ.Visible = False
Else
occ.Visible = True
End If
Next

Johan Sweden
0 Likes
Accepted solutions (1)
831 Views
12 Replies
Replies (12)
Message 2 of 13

bradeneuropeArthur
Mentor
Mentor

For VBA:

 

Public Sub main()
Dim assemblyDef As AssemblyComponentDefinition
Set assemblyDef = ThisDocument.ComponentDefinition
Dim occ As Inventor.ComponentOccurrence
For Each occ In assemblyDef.Occurrences.AllLeafOccurrences
Dim refDoc As PartDocument
On Error Resume Next
Set refDoc = occ.Definition.Document
MsgBox refDoc.PropertySets.Item("Design Tracking Properties").Item("Stock Number").Value
Dim pos As Integer

pos = InStr(1, refDoc.PropertySets.Item("Design Tracking Properties").Item("Stock Number").Value, "BQ", vbTextCompare)
If pos > 0 Then
'If refDoc.PropertySets.Item("Design Tracking Properties").Item("Stock Number").Value.Contains Then
'If iProperties.Value("Project", "Stock Number").Contains("BQ") Then
occ.Visible = False
Else
occ.Visible = True
End If
Next
End Sub

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 13

autodeskpelinder
Enthusiast
Enthusiast

Thank you.

But I get some errors:

Rule Compile Errors in Osynliga primärstöd, in 14325.iam

Error on Line 2 : Typen AssemblyComponentDefinition har inte definierats.
Error on Line 3 : Let- och Set-tilldelningssatser stöds inte längre.
Error on Line 3 : ThisDocument har inte deklarerats. Det är eventuellt inte tillgängligt på grund av dess skyddsnivå.
Error on Line 4 : Typen Inventor.ComponentOccurrence har inte definierats.
Error on Line 8 : Let- och Set-tilldelningssatser stöds inte längre.
Error on Line 9 : Metodargument måste omges av parenteser.

Johan Sweden
0 Likes
Message 4 of 13

bradeneuropeArthur
Mentor
Mentor

set your illogic project to "Straight VBA code" in the options...

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

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@autodeskpelinder,

 

Try below iLogic code to change visibility of parts.

 

Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
Dim occ As Inventor.ComponentOccurrence
For Each occ In assemblyDef.Occurrences.AllLeafOccurrences
	Dim refDoc As PartDocument = occ.Definition.Document

	If iProperties.Value(occ.Name,"Project", "Stock Number").Contains("BQ") Then
		occ.Visible = False
	Else
		occ.Visible = True
	End If
Next

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Like".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 6 of 13

autodeskpelinder
Enthusiast
Enthusiast

Sorry, bit I can´t get it to work.Nothing happends.

I tried to  change it to Part number instead (I have "BQ" at both places), but nothing happends then neither.

 

SyntaxEditor Code Snippet

Public Sub Main()
Dim assemblyDef As AssemblyComponentDefinition
Set assemblyDef = ThisDocument.ComponentDefinition
Dim occ As Inventor.ComponentOccurrence
For Each occ In assemblyDef.Occurrences.AllLeafOccurrences
Dim refDoc As PartDocument
On Error Resume Next
set refDoc = occ.Definition.Document
MsgBox refDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
Dim pos As Integer

pos = InStr(1, refDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value, "BQ", vbTextCompare)
If pos > 0 Then
	MessageBox.Show("pos > 0", "Title")
'If refDoc.PropertySets.Item("Design Tracking Properties").Item("Stock Number").Value.Contains Then
'If iProperties.Value("Project", "Stock Number").Contains("SSG") Then
occ.Visible = False
Else
occ.Visible = True
End If
Next
End Sub

 

 

Johan Sweden
0 Likes
Message 7 of 13

autodeskpelinder
Enthusiast
Enthusiast

Thank you very much!
It was exactley what I wanted to do, perfect.

Johan Sweden
0 Likes
Message 8 of 13

bferrell
Contributor
Contributor

Chandra,

Thank you for the code you posted. I believe it will work for what we want to do.

I am currently running this as a Rule.

But for some reason,  the code returns error:

HIDE-ERROR01.PNG

 

Part Number should be equal to the file name correct??

 

Part Number parameter will work best for us because each Bolt Assembly name contains "BT" (ie. A12345 BT01).

The only thing I have changed in your original code is "BT" instead of "BQ".

 

When I change "Part Number" to something like "Project", the code runs (for a very long time) but does not turn off visibility (on any bolts where BT is present in the Project field).

 

Also, can we have it search all sub-assemblies by using ".SubOccurrences" ?

 

Thank you very much for your help!

0 Likes
Message 9 of 13

Darkforce_the_ilogic_guy
Advisor
Advisor

I do not know what  wrong ... and I have to go to bed now.

 

but it seens that this code 

Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition

 

cause the error when the it try to check an assembly ...

 

Wait ... do you stand in master "Master" view rep. ? If so ... it is lock and may cause the Error

0 Likes
Message 10 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support

@bferrell ,

 

The iLogic code was customized to take care of part occurrence in assembly. Can you please provide non confidential sample data and occurrence name to invisible?

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 11 of 13

bferrell
Contributor
Contributor

Chandra,

I have attached a sample assembly per your request.

 

I see now how the previous code you provided only searches through an assembly to find PARTS.

I mislead you in my explanation of what we needed...my apologies.

 

We need code to search through an assembly to turn off visibility on any/all bolt sub-assemblies.

All of our bolt sub-assemblies will contain "BT" in the file name/Part Number.

 

The attached sample assembly contains the main assembly (7777) with 2 sub-assemblies (12345)/(24689).

The sub-assemblies both contain bolt assemblies (##### BT01) that the code should hide.

 

Obviously, the main assembly with all bolts hidden should be within a new View Representation (HDWR OFF), but I believe we have that part of the code figured out.

 

Thank you sir!

0 Likes
Message 12 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support

@bferrell,

 

Try this iLogic code.

 

Sub Main()

	Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
	Dim occ As Inventor.ComponentOccurrence
	For Each occ In assemblyDef.Occurrences  
		If occ.SubOccurrences.Count = 0 Then
			If  iProperties.Value(occ.Name,"Project", "Part Number").Contains("BT") Then 				 
				occ.Visible = False 
			End If
		Else
			processAllSubOcc(occ)
		End If
		
	Next

End Sub 

Private Sub processAllSubOcc(ByVal oCompOcc As ComponentOccurrence)
    
    Dim oSubCompOcc As ComponentOccurrence
    For Each oSubCompOcc In oCompOcc.SubOccurrences 
        If iProperties.Value(oSubCompOcc.Name,"Project", "Part Number").Contains("BT") Then
            If oSubCompOcc.Name.Contains("BT") = True  Then 
				oSubCompOcc.Visible = False 
			End If
        Else  
            Call processAllSubOcc(oSubCompOcc)
        End If
    Next
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 13 of 13

bferrell
Contributor
Contributor

EXCELLENT!

This seems to be working as intended.

I did make a couple of changes (removed some End If statements), but other than that it is working like a charm.

Now I need to spend some time looking at it to understand how this works 😉

 

We just need to add some lines to create a View Rep prior to the visibility state change, so will follow up here with updated code for other folks to reference.

 

Great job Chandra!

Thank you so much!!!

 

Revised code:

Sub Main()
	Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
	Dim occ As Inventor.ComponentOccurrence
	For Each occ In assemblyDef.Occurrences
		If occ.SubOccurrences.Count = 0 Then
			If iProperties.Value(occ.Name, "Project", "Part Number").Contains("BT") Then occ.Visible = False
			Else
			Call processAllSubOcc(occ)
			End If
			Next
			End Sub
Private Sub processAllSubOcc(ByVal oCompOcc As ComponentOccurrence)
	Dim oSubCompOcc As ComponentOccurrence
	For Each oSubCompOcc In oCompOcc.SubOccurrences
		If iProperties.Value(oSubCompOcc.Name, "Project", "Part Number").Contains("BT") Then
		If oSubCompOcc.Name.Contains("BT") = True Then oSubCompOcc.Visible = False
		    Else
			Call processAllSubOcc(oSubCompOcc)
			End If
			Next
			End Sub