Inventor Vb.Net "Stand Alone" Application - Set Part Material

Inventor Vb.Net "Stand Alone" Application - Set Part Material

isocam
Collaborator Collaborator
218 Views
2 Replies
Message 1 of 3

Inventor Vb.Net "Stand Alone" Application - Set Part Material

isocam
Collaborator
Collaborator

Can anybody help?

 

Does anybody know how, using an Inventor Vb.Net "Stand Alone" application, how to set the part material?

 

For example:

 

Say the part material is called "Generic", when I run the exe file, the part material will be set to "BRASS".

 

Many thanks in advance!

 

Darren

0 Likes
219 Views
2 Replies
Replies (2)
Message 2 of 3

Frederick_Law
Mentor
Mentor

An old Macro:

Private Sub SetPartMaterial(oMatDoc As PartDocument, oMatName As String)
      Dim oPartCompDef As PartComponentDefinition
      Set oPartCompDef = oMatDoc.ComponentDefinition
    
      ' Get the selected material
      Dim oMaterial As Material
      Set oMaterial = oMatDoc.Materials.Item(oMatName)
   
      ' Change the part material
      oPartCompDef.Material = oMaterial
      oMatDoc.Save
End Sub
0 Likes
Message 3 of 3

WCrihfield
Mentor
Mentor

Hi @isocam.  Here is another example Sub routine you could review / test for that task.  It uses updated object types (Asset & MaterialAsset) and properties.  The following online help documentation link may help in explaining the differences.

Consistent Materials (Materials and Appearances) 

First of all, the material (a MaterialAsset) must exist within the Document itself (in the PartDocument.MaterialAssets), before it can be applied to anything within that Document.  So, if the material you are specifying is only defined/available within a MaterialLibrary (an AssetLibrary), then you will need to copy it into the Document first, then use the Document version.  This code example below will first look for the specified material within the part, then if not found there, it will look for it in the 'active' MaterialLibrary.  If it is not found, nothing happens, but if found, it will try to set that as the active material of the part.

 

At one point within this routine (Lines 18 & 19), it is getting access to the Inventor.Application object, though other local object properties, but your standalone app will likely already have a variable representing this object which was defined outside of this routine.  In that case, you could substitute your variable in Line 19, then delete Line 18.

 

Private Sub SetMaterial(oDoc As Inventor.Document, sMaterialName As String)
	If (oDoc Is Nothing) OrElse (Not oDoc.IsModifiable) OrElse
		(Not TypeOf oDoc Is PartDocument) Then
		Return
	End If
	If sMaterialName Is Nothing OrElse sMaterialName = "" Then Return
	Dim oPDoc As PartDocument = oDoc
	Dim oDocMAs, oLibMAs As AssetsEnumerator, oMyMA As Inventor.MaterialAsset
	Try : oDocMAs = oPDoc.MaterialAssets : Catch : End Try
	If oDocMAs IsNot Nothing AndAlso oDocMAs.Count > 0 Then
		For Each oDocMA As Inventor.MaterialAsset In oDocMAs
			If oDocMA.Name = sMaterialName OrElse oDocMA.DisplayName = sMaterialName Then
				oMyMA = oDocMA
				Exit For
			End If
		Next oDocMA
	End If
	If oMyMA Is Nothing Then
		Dim oInvApp As Inventor.Application = oPDoc.DocumentEvents.Application
		oLibMAs = oInvApp.ActiveMaterialLibrary.MaterialAssets
		For Each oLibMA As Inventor.MaterialAsset In oLibMAs
			If oLibMA.Name = sMaterialName OrElse oLibMA.DisplayName = sMaterialName Then
				Try : oMyMA = oLibMA.CopyTo(oPDoc, True) : Catch : End Try
				Exit For
			End If
		Next oLibMA
	End If
	If oMyMA IsNot Nothing Then
		Try : oPDoc.ActiveMaterial = oMyMA : Catch : End Try
	End If
End Sub

 

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes