Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

how to assign the material to the part from the assembly

khadeerkruthi
Contributor

how to assign the material to the part from the assembly

khadeerkruthi
Contributor
Contributor

i have the rule in part document for assigning material 

Dim selectedMaterial As String = Parameter("flangeMaterial")
    
    ' Get the active document
    Dim oDoc As Object
    oDoc = ThisApplication.ActiveDocument
    
    ' Check if the document is a PartDocument
    If TypeOf oDoc Is Inventor.PartDocument Then
        ' It's a PartDocument, so cast it
        Dim oPartDoc As Inventor.PartDocument
        oPartDoc = oDoc
        
        ' Get the component definition
        Dim oComponentDefinition As PartComponentDefinition = oPartDoc.ComponentDefinition
        
        ' Assign the material to the component definition
        oComponentDefinition.Material = oPartDoc.Materials.Item(selectedMaterial)
        
    Else
        ' It's not a PartDocument, so something went wrong
        MsgBox("Error: The document is not a PartDocument.")
    End If


the above rule will work when i work in the active part document.

i have written the rule in assembly and created the fx parameter for the flangeMaterial and i am pushing that value into the part document but the material is assigned or not am not sure.

Parameter("MountingFlange:1", "flangeMaterial") = Parameter("flangeMaterial")
	iLogicVb.RunRule("MountingFlange:1", "AssignMaterial"

 please guide me.

0 Likes
Reply
139 Views
1 Reply
Reply (1)

WCrihfield
Mentor
Mentor

Hi @khadeerkruthi.  I assume that first code is within an internal iLogic rule that is saved within the flange part, and not within an external iLogic rule, right?  If so, then one additional thing that may be needed is a way to trigger this rule to run automatically when the value of that parameter changes.  One way to do that, when you are working with an 'internal' iLogic rule only, is to include the blue, unquoted name of that parameter somewhere within that rule.  That will cause the rule to run automatically whenever its value changes.  I often just use a 'dummy variable' for this, which is a variable that is not used for anything, and I am simply setting its value to that parameter's value.  That is usually enough to enable this behavior, as long as you do not have automatic triggering turned off in the settings.

 

Next, I would highly advise you to update your code to the newer system of dealing with materials.  The system you are using has been outdated since around 2013, but for some reason they continue to let codes like that keep working.  See the following online help documentation about this issue:

Consistent Materials (Materials and Appearances) 

I have included a code example below that could be used to replace your existing iLogic rule code, with updated objects and methods being used within it.  I customized it a bit to suit your custom situation.  You will notice on Line 2 that I am just including that line of code to enable the automatic triggering behavior.  As mentioned, that will only work in internal rules, not in external rules.  I put all the 'feedback' as input into the iLogic Log window, instead of MessageBox.Show or MsgBox messages, since you want to be able to run this remotely (from an assembly).

 

PS.  The other important difference is my code is using the 'ThisDoc.Document' phrase to get the Document that the rule is supposed to be focused on, while your code was using 'ThisApplication.ActiveDocument' phrase.  The phrase your code was using will be pointing to the assembly when the rule gets ran from the assembly, because that is still the 'active' document in that situation, not the part.  The phrase I am using will refer to the 'local' document (the document that the rule is saved within, if used from an internal rule, but if used from an external rule, then it will refer to essentially the same document as ThisApplication.ActiveEditDocument instead.  But it is a very dynamic term, with a whole block of code supporting how it works in the background, in various scenarios.

 

Sub Main
	oTrigger = flangeMaterial
	Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
	If oPDoc Is Nothing Then
		Logger.Debug("The iLogic rule named '" & iLogicVb.RuleName & _
		"' exited, because no PartDocument was obtained!")
		Return
	End If
	Dim oParams As Inventor.Parameters = oPDoc.ComponentDefinition.Parameters
	Dim oMaterialParam As Inventor.Parameter = Nothing
	Dim sParamName As String = "flangeMaterial"
	Try : oMaterialParam = oParams.Item(sParamName) : Catch : End Try
	If oMaterialParam Is Nothing Then
		Logger.Debug("Could not find the Parameter named " & sParamName)
		Return
	End If
	Dim sMaterialName As String = oMaterialParam.Value
	Dim oFlangeMaterialAsset As MaterialAsset = Nothing
	For Each oMaterialAsset As MaterialAsset In oPDoc.MaterialAssets
		If oMaterialAsset.DisplayName = sParamName Then
			oFlangeMaterialAsset = oMaterialAsset
			Exit For
		End If
	Next oMaterialAsset
	If oFlangeMaterialAsset Is Nothing Then
		Logger.Debug("Could not find specified material asset within Part material assets!")
		Return
	End If
	Try
		oPDoc.ActiveMaterial = oFlangeMaterialAsset
	Catch ex As Exception
		Logger.Error("Error setting ActiveMaterial of Part!" & vbCrLf & ex.Message)
	End Try
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