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

ilogic form to change material of certain solids

Anonymous

ilogic form to change material of certain solids

Anonymous
Not applicable

Hi,

 

I am trying to make an ilogic form to change material of certain solids in a part file. I have attached a test which I've done however can only get the whole part to change colour; not certain solids. I also want the material selection to be custom, i.e. i have two materials in the appearance brower named 'ral 7016' and 'white' which i want the user to soley choose from; not the whole material library. Here's the code so far:

iLogicForm.Show("Form 1")

MultiValue.List("material") = iProperties.Materials
iProperties.Material=material

 Any help would be great!

0 Likes
Reply
Accepted solutions (1)
1,741 Views
8 Replies
Replies (8)

Anonymous
Not applicable

Here's the file:

0 Likes

WCrihfield
Mentor
Mentor

You can only have one material per part file, but can assign multiple different Appearances to different Bodies, & faces.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Anonymous
Not applicable

So for the part I have attached you can't make Solid 1 and 2 one colour and Solid 3 another colour through Ilogic?

0 Likes

Anonymous
Not applicable
mydoc = ThisDoc.Document
If mydoc.DocumentType <> kPartDocumentObject Then
    MessageBox.Show("This rule can only be run in a part file!", "Cadline iLogic", _
    MessageBoxButtons.OK,MessageBoxIcon.Exclamation, _
    MessageBoxDefaultButton.Button1)
Return
End If

' Define stuff...
Dim oDef As ComponentDefinition = mydoc.ComponentDefinition
Dim oFeature As PartFeature
Dim oFeatures As PartFeatures = oDef.Features

' Define new dictionary to hold colours required for each feature name
Dim oColourDict As New Dictionary(Of String, String)
' Add all your colour combinations in here, by copying existing lines
oColourDict.Add("Extrusion", "ral 7016")


' Loop through all features in part
For Each oFeature In oFeatures
	For Each Combo As KeyValuePair(Of String, String) In oColourDict
		' Check dictionary entry to see if it matches name of feature (e.g. does feature name contain HOLE)
		' Note that this is CASE INSENSITIVE
		If oFeature.Name.IndexOf((Combo.Key), 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
			' Set feature colour
			Try
			Feature.Color(oFeature.Name) = Combo.Value
			Catch
				MsgBox("You are trying to set feature '" & oFeature.Name & "' to colour '" & Combo.Value & "'." & vbLf & vbLf & _
				"This colour does not exist in the current appearance library. Please correct this...", 64, "Cadline iLogic")
				Exit Sub
			End Try
		End If
	Next
Next

 

I've managed to get this which changes the colour to 'ral 7016' if it contain certain text which could work. However can i get this to have an ilogic form which you can choose a different material apart from 'ral 7016' ?

0 Likes

WCrihfield
Mentor
Mentor

There's something very odd going on within the Appearances of the file you supplied.

This sort of thing is usually very easy to do, but not for the file you supplied.

So I used a bit of code to write out a detailed list of all the documents 'Assets' (The Assets are either Material, Appearance, or Physical Properties type Assets, used to store the settings of each variation.) What it revealed is very puzzling. It can't find an appearance type asset named "White".

None of the Assets are named "ral 7016".

And two of the Assets are named "1:Black" (both of their DisplayNames are "ral 7016").

Here's the code I used to investigate:

Dim oFileName As String = SpecialDirectories.Desktop & "\" & "Appearance Assets Info.txt"
Dim oWrite As System.IO.StreamWriter = System.IO.File.CreateText(oFileName)
Dim oDoc As PartDocument = ThisApplication.ActiveDocument

oWrite.WriteLine("List of Appearance type Assets within this document:")

For Each oAsset As Asset In oDoc.Assets
'	If oAsset.AssetType = AssetTypeEnum.kAssetTypeAppearance
		oWrite.WriteLine("")
		oWrite.WriteLine("oAsset.Name = " & oAsset.Name)
		oWrite.WriteLine("oAsset.DisplayName = " & oAsset.DisplayName)
		oWrite.WriteLine("oAsset.AssetType.ToString = " & oAsset.AssetType.ToString)
		oWrite.WriteLine("oAsset.CategoryName = " & oAsset.CategoryName)
		oWrite.WriteLine("oAsset.HasTexture = " & oAsset.HasTexture)
		oWrite.WriteLine("oAsset.IsReadOnly = " & oAsset.IsReadOnly)
		oWrite.WriteLine("oAsset.IsUsed = " & oAsset.IsUsed)
		oWrite.WriteLine("oAsset.Count (Asset Values Count) = " & oAsset.Count)
		oWrite.WriteLine("Detailed List of its Asset Values:")
		If oAsset.Count > 0 Then
			For i As Integer = 1 To oAsset.Count
				oWrite.WriteLine(vbTab & "oAsset.Item(" & i & ").Name = " & oAsset.Item(i).Name)
				oWrite.WriteLine(vbTab & "oAsset.Item(" & i & ").DisplayName = " & oAsset.Item(i).DisplayName)
				oWrite.WriteLine(vbTab & "oAsset.Item(" & i & ").IsReadOnly = " & oAsset.Item(i).IsReadOnly)
				oWrite.WriteLine(vbTab & "oAsset.Item(" & i & ").ValueType.ToString = " & oAsset.Item(i).ValueType.ToString)
				oWrite.WriteLine("")
			Next
		End If
Next
oWrite.Close()
ThisDoc.Launch(SpecialDirectories.Desktop & "\" & "Appearance Assets Info.txt")

 

The resulting TXT file is attached.

 

Within the Appearance Browser, things look just fine, but when trying to assign an Asset as the value of oBody.Appearance, with the name "White", it won't work.  So I tried looping through the Assets, and if the DisplayName.Contained("White"), I tried to assign that Asset to the value, but that would't work either.

The DisplayName route works for the "ral 7016" one though.  Very odd.

Here's the code I was trying to use to ask you which Appearance you wanted to assign to each solid body, then assign it.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Part Documents.",vbOKOnly + vbExclamation , "WRONG DOCUMENT TYPE")
	Return
End If

Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition

'Set the list of Options to choose from
Dim oChoices As List(Of String) = New List(Of String)
oChoices.Add("ral 7016")
oChoices.Add("White")

'Loop through the solid bodies and ask which option to use for each
For Each oBody As SurfaceBody In oPDef.SurfaceBodies
	Dim oChoice As String = InputListBox("Choose a color for " & oBody.Name, oChoices, oChoices(1), "CHANGE COLOR", "COLORS")
	For Each oAsset As Asset In oPDoc.Assets
		If oAsset.DisplayName.Contains(oChoice) Then
			oBody.Appearance = oAsset
		End If
	Next
Next

But, it's still not working right, for some reason.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Anonymous
Not applicable

Hi,

For me this code works great! I can assign each solid to either 'ral 7016' and 'white' and interchange between them for each solid. For my real model, i have 30+ solids however 27 of them need to be the same colour with the other 3 solide not changing colour. Is there a way to group the 27 together so that it only asks for each group of solids. For the test which I have attached can you group solid 1 and 3 together so that it asks for a material for these? I cannot attach the real model for as it is a work related project.

Thanks!

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

Try this version.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Part Documents.",vbOKOnly + vbExclamation , "WRONG DOCUMENT TYPE")
	Return
End If

Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition

'Set the list of Options to choose from
Dim oChoices As List(Of String) = New List(Of String)
oChoices.Add("ral 7016")
oChoices.Add("white")

Dim oBodies1 As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim oBody As SurfaceBody
For Each oBody In oPDef.SurfaceBodies
	If oBody.Name.Contains("1") Or oBody.Name.Contains("3") Then
		oBodies1.Add(oBody)
	End If
Next

Dim oChoice As String = InputListBox("Choose a color for "& oBodies1.Count & " bodies.", oChoices, oChoices(1), "CHANGE COLOR", "COLORS")
Dim oAsset As Asset
For Each oObj In oBodies1
	oBody = oObj
	For Each oAsset In oPDoc.Assets
		If oAsset.DisplayName.Contains(oChoice) Then
			oBody.Appearance = oAsset
		End If
	Next
Next

For some reason, even though the "White" appearance is there in the Appearances Browser, it is not recognized by the rule when I run it.  But if the last rule worked for you, then it must just be something on my end.

There are several ways we could add those bodies to a collection, but if you're talking about possibly adding 27+ bodies into the collection, you would need to determine what about those specific 27 bodies is unique to them, and not the remaining bodies, so they can be singled out for inclusion into the collection.  Then just change the If...Then line of the SurfaceBodies loop to whatever you need it to be, for that situation.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Anonymous
Not applicable

Thanks again for the response. This works great again! I think this will definitely work well for my project. 

0 Likes