Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

change sected parts colour in an assembley

Anonymous

change sected parts colour in an assembley

Anonymous
Not applicable

hi all,

 

I am trying to put a bit of code together to over ride the appearance colour of parts in an assembly.

 

we are using cable tray and we change the colour of the tray in one view rep to show if it is for power or signal, we also want everyone who works on the project to do it the same way (this is where iLogic come in)

 

I want to put together a button (for example 24v needing to be red) so you select the parts ( that could be in the top level or in sub-assemblies) and then click the button and the rule changes the appearance colour to red.

 

I have looked all over and seam to find different codes but none does or can be easily changed to do what I need 

 

 

any help would be great.

 

Thank you 

0 Likes
Reply
Accepted solutions (1)
1,143 Views
6 Replies
Replies (6)

MechMachineMan
Advisor
Advisor

1. Post your sample code.

2. Post links of things that might be similar to what you want if you don't know how to code

 

 

'define the assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument

'define the appearance library
Dim oLib As AssetLibrary
oLib = ThisApplication.AssetLibraries("Autodesk Appearance Library")

'make sure colors are in assembly doc
Dim libAsset As Asset
libAsset = oLib.AppearanceAssets.Item("Smooth - Red")
Try 
	localAsset = libAsset.CopyTo(oAssyDoc)
Catch
'catch error if it's already local
End Try

libAsset = oLib.AppearanceAssets.Item("Smooth - Black")
Try 
	localAsset = libAsset.CopyTo(oAssyDoc)
Catch

End Try

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAssyDoc.ComponentDefinition
 
Dim oOcc As ComponentOccurrence
Dim oAsset As Asset

Dim oSS As SelectSet
oSS = oAssyDoc.SelectSet

Dim oSelCollection As New Collection
For Each oItem In oSS
	oSelCollection.Add(oItem)
Next

'set color overrides of components at the assembly level
For Each oOcc In oAsmCompDef.Occurrences.AllLeafOccurrences
	Dim MatchFound As Boolean = False
	
	For Each oItem In oSelCollection
		If oOcc.Equals(oItem) Then
			MatchFound = True
		End If
	Next
	
	If MatchFound = True Then
		oAsset = oAssyDoc.Assets.Item("Smooth - Red")
	Else
		oAsset = oAssyDoc.Assets.Item("Smooth - Black")
	End If
	oOcc.Appearance = oAsset
Next 

 

 Edited by
Discussion_Admin


--------------------------------------
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

Anonymous
Not applicable

thank you,

 

first off sorry I didn't attach the code I already have 

 

what you have done is grate.

 

I am still learning how to code,

 

I was wondering if you would be able to alter it to only change the selected part? and explain it to help me understand it better? ( at the moment it changes the selected parts red and the not selected parts back)

 

I have had a go changing it myself and have had mixed results.

 

 

 

Kind Regards

 

0 Likes

MechMachineMan
Advisor
Advisor

You are likely better off buying and reading a book on programming in vb.net or object oriented programming.

 

There are many blogs/guides/documents online as well.

 

 

Remove these 2 lines from the code to avoid setting everything else to black... As the "If/Else" construct implies....

 

	Else
		oAsset = oAssyDoc.Assets.Item("Smooth - Black")

 


--------------------------------------
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

Anonymous
Not applicable

I thought that would do it as well but I get this error

 

 

Error in rule: change, in document: Assembly1

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

 

I have put a message box to check/ debug that it is not the rest of the code. all that works fine up until the message box ( I have commented it out.

 

 

 

'define the assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument

'define the appearance library
Dim oLib As AssetLibrary
oLib = ThisApplication.AssetLibraries("Autodesk Appearance Library")

'make sure colors are in assembly doc
Dim libAsset As Asset
libAsset = oLib.AppearanceAssets.Item("Smooth - Red")
Try 
	localAsset = libAsset.CopyTo(oAssyDoc)
Catch
'catch error if it's already local
End Try

libAsset = oLib.AppearanceAssets.Item("Smooth - Black")
Try 
	localAsset = libAsset.CopyTo(oAssyDoc)
Catch

End Try

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAssyDoc.ComponentDefinition
 
Dim oOcc As ComponentOccurrence
Dim oAsset As Asset

Dim oSS As SelectSet
oSS = oAssyDoc.SelectSet

Dim oSelCollection As New Collection
For Each oItem In oSS
	oSelCollection.Add(oItem)
Next

'set color overrides of components at the assembly level
For Each oOcc In oAsmCompDef.Occurrences.AllLeafOccurrences
	Dim MatchFound As Boolean = False
	
	For Each oItem In oSelCollection
		If oOcc.Equals(oItem) Then
			MatchFound = True
		End If
	Next
	' this works fine till this point' MessageBox.Show("Message", "Title")

	If MatchFound = True Then
		oAsset = oAssyDoc.Assets.Item("Smooth - Red")


	End If
	oOcc.Appearance = oAsset
Next 

 

 

Kind Regards

0 Likes

MechMachineMan
Advisor
Advisor
Accepted solution

Ok, so if the code works good up until that point, you need to very careful examine everything that happens after.

 

In this case:

 

	If MatchFound = True Then
		oAsset = oAssyDoc.Assets.Item("Smooth - Red")
	End If
	oOcc.Appearance = oAsset
Next 

What we set is that oASSET is assigned a value inside an if loop. If TRUE, the oASSET value gets assigned, if FALSE, the oAsset value isn't assigned. Look back through all of the code before this, and we see there is no default value assigned to the oASSET OBJECT.

 

If we look past the IF Statement, we see that the program is trying to assign the oASSET to an object, so of course it will fail.

 

There's really only 2 ways to fix it: ensure the oAsset variable DOES have a value when we try to assign it, or don't assign it.

 

We could add an if statement before the 

 

oOcc.Appearance = oAsset 

line to ensure that the line is only run if  NOT (oAsset is Nothing)... but that would be redundant, since we can just move the line inside of the if loop that assigns the oAsset value.

 


--------------------------------------
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

Anonymous
Not applicable

thank you for your help, in this it works when I put 

oOcc.Appearance = oAsset 

inside the if statement.

 

Kind regards