Update material on virtual components with iLogic.

Update material on virtual components with iLogic.

Magnus-HW
Contributor Contributor
1,029 Views
7 Replies
Message 1 of 8

Update material on virtual components with iLogic.

Magnus-HW
Contributor
Contributor

Hi

 

I've been trying to chance the material of a virtual component with iLogic, without success. 

I can change any update other iproperties and it work perfectly on the part files. 

 

How can I update the material on virtual components?

' Update Material & Color
If MTRL = "IRON"
		SCREWMTRL = "S355JR"
		SCREWCOLOR = "Smooth - Red"
	Else If MTRL = "Stainless"
		SCREWMTRL = "EN 1.4301"
		SCREWCOLOR = "Steel"
	Else If MTRL = "Stainless2"
		SCREWMTRL = "EN 1.4404"
		SCREWCOLOR = "Stainless - Brushed"
	Else 
		MessageBox.Show("'Material' must be choose from list.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
	End If

'set materials
iProperties.MaterialOfComponent("DUMMY_SCREW") = SCREWMTRL
		Component.Color("DUMMY_SCREW") = SCREWCOLOR	
iProperties.MaterialOfComponent("Center_pipe") = SCREWMTRL 		'Virtual component
iProperties.MaterialOfComponent("Coil_reinforce")=SCREWMTRL		'Virtual component
iProperties.MaterialOfComponent("Brush_distance") = SCREWMTRL	'Virtual component
iProperties.MaterialOfComponent("Brush_Holder")=SCREWMTRL		'Virtual component

 

0 Likes
Accepted solutions (1)
1,030 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

That iLogic only snippet you are using to set the material of a component says that it will only work on a component that represents a Part.  I don't know if a 'virtual component' qualifies as a Part, so that may be why it is failing.  Pretty much anything that can be done with an iLogic only snippet like that, can also be done by straight Inventor API code.  Inventor API code uses the object model to step down to that component object through normal means (the long way - no shortcut), then gets its VirtualComponentDefinition.  If you are using the iProperty for Material to fill a column in your assembly's BOM, then you may be able to use the VirtualComponentDefinition.PropertySets then navigate to that property to get / set its property.  That property is normally ReadOnly, but is writable for a virtual component definition.

 

As an example, I created a new virtual component inside the BOM dialog of a test assembly.  Its material is automatically assigned by a setting within the Document Settings (Tools tab > Options panel > Document Settings > Standard tab > Virtual Components > Default Material).  In my case the default material was "Aluminum 6061".  Then I created this fairly simple iLogic rule to find that virtual component (any virtual components), then show a message with its name and material.  Then the rule sets a new value to the component's iProperty for Material.

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
oADef = oADoc.ComponentDefinition
oOccs = oADef.Occurrences
For Each oOcc As ComponentOccurrence In oOccs
	If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
		Dim oVCDef As VirtualComponentDefinition = oOcc.Definition
		Dim oMaterialProp As Inventor.Property = oVCDef.PropertySets.Item("Design Tracking Properties").Item("Material")
		Dim oMaterialName As String = oMaterialProp.Value
		MsgBox("Found a 'virtual' component named " & oOcc.Name & vbCrLf & _
		"Its current 'Material' name = " & oMaterialName, vbInformation, "")
		'now change it to what you want
		oMaterialProp.Value = "Stainless"
	End If
Next

 

After running that rule, it showed me its name and current material correctly.  Then when I opened the BOM dialog again and checked, its material was now set to "Stainless", as planned.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Since I was already on the subject, I went ahead and attempted to revise your existing code, so you could try this process out more easily in your current situation.  I left the first two iLogic snippets in there for the 'regular' component, but added the alternate code for dealing with the virtual components.  See if this will work for you.

' Update Material & Color
If MTRL = "IRON"
	SCREWMTRL = "S355JR"
	SCREWCOLOR = "Smooth - Red"
Else If MTRL = "Stainless"
	SCREWMTRL = "EN 1.4301"
	SCREWCOLOR = "Steel"
Else If MTRL = "Stainless2"
	SCREWMTRL = "EN 1.4404"
	SCREWCOLOR = "Stainless - Brushed"
Else 
	MessageBox.Show("'Material' must be choose from list.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If

'set material / Color for non-virtual components that represent a Part
iProperties.MaterialOfComponent("DUMMY_SCREW") = SCREWMTRL
Component.Color("DUMMY_SCREW") = SCREWCOLOR	

'use below process to set Material of Virtual Components
'get the 'local' assembly document
Dim oADoc As AssemblyDocument = ThisDoc.Document
oOccs = oADoc.ComponentDefinition.Occurrences
For Each oOcc As ComponentOccurrence In oOccs
	'of it is not 'virtual' then skip to next component
	If Not TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For
	Dim oVCDef As VirtualComponentDefinition = oOcc.Definition
	Dim oMaterialProp As Inventor.Property = Nothing
	oMaterialProp = oVCDef.PropertySets.Item("Design Tracking Properties").Item("Material")
	If oOcc.Name = "Center_pipe" Or _
		oOcc.Name = "Coil_reinforce" Or _
		oOcc.Name = "Brush_distance" Or _
		oOcc.Name = "Brush_Holder" Then
		oMaterialProp.Value = SCREWMTRL
	End If
Next

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 8

Magnus-HW
Contributor
Contributor

Thank you very much @WCrihfield.

This was magical.

0 Likes
Message 5 of 8

gabriel_deber
Enthusiast
Enthusiast

Hello @WCrihfield . I'm tring your code and isn't working for me. Gives an  "unspecified error" in the line

oMaterialProp.Value = "Stainless"

 I tried differents aproaches in that line, and none of them worked. 

Any idea how to solve this? 

0 Likes
Message 6 of 8

WCrihfield
Mentor
Mentor

Hi @gabriel_deber.  That 'standard' iProperty named "Material" in the third PropertySet named "Design Tracking Properties" is often ReadOnly, but some people seem to be able to write new values to it sometimes, for some reason.  I do not understand why it seems to work sometimes for some people, and not other times.  I have heard the same person say it used to work for a long time, then it stopped working for quite a while, then started working again later.  The online documentation about that property (at the PropertiesForDesignTrackingPropertiesEnum) says that it is ReadOnly, because its value seems to get set automatically by Inventor.

 

The 'normal' (Inventor API) way to set the material of a part by code is by setting the value of the PartDocument.ActiveMaterial property with an Asset (or more specifically a MaterialAsset) object that represents the material.  The Asset can be obtained from one of the AssetLibraries, or from the part's own internal Assets collection.  The iLogic shortcut snippet (either iProperties.Material or iProperties.Material(Object)) can also be used, but in a different way...they just ask for the 'name' of the material (a String), instead of the actual Asset object.  Same for the iProperties.PartColor iLogic shortcut snippet...it just asks for the 'name' of a color or apprearance, while the standard way of doing that is by setting the value of the PartDocument.ActiveAppearance property with an Asset representing that appearance.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 8

suneelkumarYX7P2
Contributor
Contributor

hi 

when i run the above ilogic code in my assembly i got an error in the line 

oMaterialProp.Value = "stainless"

 

and the error message is "Expecting object to be local"
is there any other way assigning the material to the virtualcomponent other than the above

 

Thanks

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor

Hi @suneelkumarYX7P2.  Yes.  Their material can be set similarly to how we set the material of a Part, using its VirtualComponentDefinition.ActiveMaterial property.  That property is expecting an 'Asset' or 'MaterialAsset' type object as its value though, not just a String.  Virtual components do not reference an external model file, unlike other assembly components though, so they do not contain any materials or appearances of their own.

We also have an opportunity to set the virtual component's material at the moment it is being created, by setting the AssemblyComponentDefinition.DefaultVirtualComponentMaterial property's value before we create the virtual component.  That property works the same way, and wants an 'Asset' or 'MaterialAsset' object as its value, instead of just a name.  Manually, that property can be found in the 'Document Settings', on the 'Standard' tab within that document settings dialog.  So, when you have an assembly visibly open on your screen (as the 'active' document), then go to the 'Tools' tab in the ribbon, then the 'Options' panel should be a button for the Document Settings.  If the 'active' assembly is a weldment sub-type of assembly, then we can also set the default material for any 'weld beads' that we create within that assembly, but that setting is actually within the standard iProperties dialog, where there will be a special tab in there named 'Weld Bead'.  On that tab are 3 settings...'Visible', 'Weld Bead Appearance', & 'End Fill Appearance'.  Then on the 'Physical' tab, at the very top, is where we can set the actual 'material' of the weld beads that get created.  Because the weld beads are essentially the only mass adding feature that we can add into an assembly besides components.  All the component-based geometry is 'proxy' geometry.

When setting the material of a virtual component, that component only exists within the definition of that assembly, so the material (or appearance) that we specify for it must exist within its parent/owning assembly.  Assets primarily exist within external libraries, but we can also create and keep copies of them within our parts directly (usually not in assemblies directly, other than for the two properties mentioned above).  So, when we manually assign a material or appearance to something in our part, and that material or appearance is only defined within the 'active' material library, but not in that part, then some automation happens behind the scenes for us to make a local copy of that Asset from the library into the part, then the geometry in the part will directly reference the version of that Asset that exists directly within the part.  Any time a model is being set to any material or appearance, that material or appearance must get copied locally into that part first, then it will use that local copy of the asset directly.  This eliminates a direct dependency on the external library file.  This may be what the error message was talking about, but I can not be sure.

You can check within the AssemblyDocument.MaterialAssets collection for local copies of material type assets, or within the AssemblyDocument.AppearanceAssets collection for local copies of appearance type assets, or just within the general AssemblyDocument.Assets collection, where both types can reside.  The general Assets collection object also gives us the ability to 'add' (create) a new asset, if we know one does not already exist the way we need it.  But before creating new assets, it is always best to check within the available material libraries first, because it is easier to copy an existing one, than to create a new one.

Application.AssetLibraries 

Application.ActiveMaterialLibrary 

Application.ActiveAppearanceLibrary 

And by the way, the same library file can contain all types of assets:  material assets, appearance assets, physical assets.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes