Extracting material description as an iproperty

Extracting material description as an iproperty

williamRPTGK
Participant Participant
677 Views
1 Reply
Message 1 of 2

Extracting material description as an iproperty

williamRPTGK
Participant
Participant

Hi,

 

I am trying to find a way of extracting the description of a material into an iProperty so that I can use it in automated drawing notes for specifying the material. 

 

As per the screenshot below I have the material grade defined in the name field and then the material description is the ISO/EN standard that specifies the material. I haven't put the standard into the material name purely because it ends up being a bit clumsy and overly long when browsing materials in the library. However when calling out a material on a drawing I do want both the material name and then its description (the ISO/EN standard) put together as 2 strings in the notes/title block.

 

Can anyone help with the code needed to extract the material description? 

 

I'm using Inventor Pro 2021.

 

williamRPTGK_1-1641308837335.png

 

 

0 Likes
Accepted solutions (1)
678 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor
Accepted solution

Hi @williamRPTGK.  That can actually be a fairly complicated task, because it means exploring Assets and AssetValues of various types to find that specific bit of data about that material.  And right now, I'm actually thinking that we may not be able to get that specific bit of information by code.  We have access to much of the information you can see in the material editor screen, but apparently not all of it yet.  I have a fairly complex iLogic rule I use for exploring all available data pertaining to the 'active' material in a part document, and writing all that data out to a text file (because there is a lot of it).  Within the resulting text file, I am not seeing a listing for an AssetValue named "Description", or even an AssetValue with a name containing the word "Description".  I will let you play with this yourself, so you can see what I am seeing for yourself.  It is currently set to list out info about the Material, Appearance, and Physical aspects of the currently active material in a part.

Here is the code for that:

Sub Main
	Dim oPDoc As PartDocument = ThisDoc.Document
	Dim oMat As MaterialAsset = oPDoc.ActiveMaterial
	oApp = oMat.AppearanceAsset
	oPhys = oMat.PhysicalPropertiesAsset
		
	oTxtFile = "C:\Temp\Asset Data.txt"
	Dim oSW As System.IO.StreamWriter
	oSW = GetCreateTextFile(oTxtFile)
	If IsNothing(oSW) Then Exit Sub
	
	Dim AllData As String
	AllData = "ALL DATA FOR ACTIVE MATERIAL, ITS APPEARANCE, AND ITS PHYSICAL PROPERTIES"
	AllData = AllData & vbCrLf & vbCrLf
	
	AllData = AllData & "<<<< MATERIAL ASSET DATA ONLY >>>>" & vbCrLf
	oMatData = GetAssetData(oMat)
	AllData = AllData & oMatData & vbCrLf & vbCrLf
	
	AllData = AllData & "<<<< APPEARANCE ASSET DATA ONLY >>>>" & vbCrLf
	oAppData = GetAssetData(oApp)
	AllData = AllData & oAppData & vbCrLf & vbCrLf
	
	AllData = AllData & "<<<< PHYSICAL PROPERTIES ASSET DATA ONLY >>>>" & vbCrLf
	oPhysData = GetAssetData(oPhys)
	AllData = AllData & oPhysData
	
	oSW.Write(AllData)
	oSW.Close()
	ThisDoc.Launch(oTxtFile)
End Sub

Function GetAssetData(oAsset As Asset) As String
	Dim oAData As String
	oAData = "DispayName = " & oAsset.DisplayName
	oAData = oAData & vbCrLf & "Name = " & oAsset.Name
	oAData = oAData & vbCrLf & "AssetType.ToString = " & oAsset.AssetType.ToString
	oAData = oAData & vbCrLf & "Type.ToString = " & oAsset.Type.ToString
	oAData = oAData & vbCrLf & "CategoryName = " & oAsset.CategoryName
	oAData = oAData & vbCrLf & "IsReadOnly = " & oAsset.IsReadOnly
	oAData = oAData & vbCrLf & "IsUsed = " & oAsset.IsUsed
	oAData = oAData & vbCrLf & "HasTexture = " & oAsset.HasTexture
	oAData = oAData & vbCrLf
	oAData = oAData & vbCrLf & vbTab & "<<< ASSET VALUE DATA >>>"
	oAData = oAData & vbCrLf
	'For Each oAVal As AssetValue In oAsset
	For i As Integer = 1 To oAsset.Count
		oReturnedAVData = GetAssetValueData(oAsset.Item(i))
		oAData = oAData & vbCrLf & oReturnedAVData
	Next
	oAData = oAData & vbCrLf
	Return oAData
End Function

Function GetAssetValueData(oAV As AssetValue) As String
	Dim oAVData As String
	oAVData = vbTab & "DispayName = " & oAV.DisplayName
	oAVData = oAVData & vbCrLf & vbTab & "Name = " & oAV.Name
	oAVData = oAVData & vbCrLf & vbTab & "ValueType.ToString = " & oAV.ValueType.ToString
	oAVData = oAVData & vbCrLf & vbTab & "Type.ToString = " & oAV.Type.ToString
	oAVData = oAVData & vbCrLf & vbTab & "IsReadOnly = " & oAV.IsReadOnly
	'routine to check ValueType then create the appropriate Derived AssetValue Type object
	'so we can better extract data from it
	Select Case oAV.ValueType
		Case kAssetValueTextureType
			Dim oTAV As TextureAssetValue = oAV
			oAT = oTAV.Value
			oAVData = oAVData & vbCrLf & vbTab & "TextureType.ToString = " & oAT.TextureType.ToString
		Case kAssetValueTypeBoolean
			Dim oBool As BooleanAssetValue = oAV
			oAVData = oAVData & vbCrLf & vbTab & "Value = " & oBool.Value
		Case kAssetValueTypeChoice
			Dim oChoice As ChoiceAssetValue = oAV
			oAVData = oAVData & vbCrLf & vbTab & "Value = " & oChoice.Value
			Try
				Dim oChoiceNames() As String
				Dim oChoiceValues() As String
				oChoice.GetChoices(oChoiceNames, oChoiceValues)
				oAVData = oAVData & vbCrLf & vbTab & vbTab & "Other Possible Choices:"
				For i As Integer = 0 To UBound(oChoiceNames)
					oAVData = oAVData & vbCrLf & vbTab & vbTab & oChoiceNames(i) & " = " & oChoiceValues(i)
				Next
			Catch oEx As Exception
				Logger.Info("{0}" & "{1}", oEx.Message, oEx.StackTrace)
			End Try
		Case kAssetValueTypeColor
			Dim oColorVal As ColorAssetValue = oAV
			oAVData = oAVData & vbCrLf & vbTab & "HasConnectedTexture = " & oColorVal.HasConnectedTexture
			oAVData = oAVData & vbCrLf & vbTab & "HasMultipleValues = " & oColorVal.HasMultipleValues
			If Not oColorVal.HasMultipleValues Then
				oAVData = oAVData & vbCrLf & vbTab & "Color = " & GetColorString(oColorVal.Value)
			Else
				oColors = oColorVal.Values
				For i As Integer = 0 To UBound(oColors)
					oAVData = oAVData & vbCrLf & vbTab & "Color = " & GetColorString(oColors(i))
				Next
			End If
		Case kAssetValueTypeFilename
			Dim oFileNameVal As FilenameAssetValue = oAV
			oAVData = oAVData & vbCrLf & vbTab & "HasMultipleValues = " & oFileNameVal.HasMultipleValues
			If Not oFileNameVal.HasMultipleValues Then
				oAVData = oAVData & vbCrLf & vbTab & "Value = " & oFileNameVal.Value
			Else
				oFNames = oFileNameVal.Values
				For i As Integer = 0 To UBound(oFNames)
					oAVData = oAVData & vbCrLf & vbTab & "Value = " & oFNames(i)
				Next
			End If
		Case kAssetValueTypeFloat
			Dim oFloat As FloatAssetValue = oAV
			oAVData = oAVData & vbCrLf & vbTab & "HasConnectedTexture = " & oFloat.HasConnectedTexture
			oAVData = oAVData & vbCrLf & vbTab & "HasMultipleValues = " & oFloat.HasMultipleValues
			oAVData = oAVData & vbCrLf & vbTab & "IsPercentage = " & oFloat.IsPercentage
			oAVData = oAVData & vbCrLf & vbTab & "HasLimits = " & oFloat.HasLimits
			If oFloat.HasLimits Then
				Dim oHasLowLimit, oHasHighLimit As Boolean
				Dim oLowLimit, oHighLimit As Double
				oFloat.GetLimits(oHasLowLimit, oLowLimit, oHasHighLimit, oHighLimit)
				oAVData = oAVData & vbCrLf & vbTab & vbTab & "HasLowLimit = " & oHasLowLimit
				If oHasLowLimit Then oAVData = oAVData & vbCrLf & vbTab & vbTab & "LowLimit = " & oLowLimit
				oAVData = oAVData & vbCrLf & vbTab & vbTab & "HasHighLimit = " & oHasHighLimit
				If oHasHighLimit Then oAVData = oAVData & vbCrLf & vbTab & vbTab & "HighLimit = " & oHighLimit
			End If
			If Not oFloat.HasMultipleValues Then
				oAVData = oAVData & vbCrLf & vbTab & "Value = " & oFloat.Value
			Else
				oVals = oFloat.Values
				For i As Integer = 0 To UBound(oVals)
					oAVData = oAVData & vbCrLf & vbTab & "Value = " & oVals(i)
				Next
			End If
		Case kAssetValueTypeInteger
			Dim oIntVal As IntegerAssetValue = oAV
			oAVData = oAVData & vbCrLf & vbTab & "HasMultipleValues = " & oIntVal.HasMultipleValues
			oAVData = oAVData & vbCrLf & vbTab & "HasLimits = " & oIntVal.HasLimits
			If oIntVal.HasLimits Then
				Dim oHasLowLimit, oHasHighLimit As Boolean
				Dim oLowLimit, oHighLimit As Double
				oIntVal.GetLimits(oHasLowLimit, oLowLimit, oHasHighLimit, oHighLimit)
				oAVData = oAVData & vbCrLf & vbTab & vbTab & "HasLowLimit = " & oHasLowLimit
				If oHasLowLimit Then oAVData = oAVData & vbCrLf & vbTab & vbTab & "LowLimit = " & oLowLimit
				oAVData = oAVData & vbCrLf & vbTab & vbTab & "HasHighLimit = " & oHasHighLimit
				If oHasHighLimit Then oAVData = oAVData & vbCrLf & vbTab & vbTab & "HighLimit = " & oHighLimit
			End If
			If Not oIntVal.HasMultipleValues Then
				oAVData = oAVData & vbCrLf & vbTab & "Value = " & oIntVal.Value
			Else
				oVals = oIntVal.Values
				For i As Integer = 0 To UBound(oVals)
					oAVData = oAVData & vbCrLf & vbTab & "Value = " & oVals(i)
				Next
			End If
		Case kAssetValueTypeReference
			Dim oRefVal As ReferenceAssetValue = oAV
			oAVData = oAVData & vbCrLf & vbTab & "(The 'Value' of a 'ReferenceAssetValue' is another 'Asset'.)"
			oAVData = oAVData & vbCrLf & vbTab & "HasMultipleValues = " & oRefVal.HasMultipleValues
		Case kAssetValueTypeString
			Dim oStVal As StringAssetValue = oAV
			oAVData = oAVData & vbCrLf & vbTab & "Value = " & oStVal.Value
		Case kAssetValueUnknownType
			'do nothing here
	End Select
	oAVData = oAVData & vbCrLf 
	Return oAVData
End Function

Function GetColorString(oColor As Color) As String
    GetColorString = oColor.Red & "," & oColor.Green & "," & oColor.Blue & "," & oColor.Opacity
End Function

Function GetCreateTextFile(oFullFileName As String) As System.IO.StreamWriter
	Dim oWriter As System.IO.StreamWriter
	If Not System.IO.File.Exists(oFullFileName) Then
		'file does not exist, so create it
		oWriter = System.IO.File.CreateText(oFullFileName)
	Else 'file already exists
		oAns = MsgBox("The text file already exists." & vbCrLf & _
		"Delete it, then recreate it? [Yes] ; Append to it? [No] ; Exit rule? [Cancel]", vbYesNoCancel + vbQuestion, "FILE EXISTS")
		If oAns = vbCancel Then
			Exit Function
		ElseIf oAns = vbYes Then 'delete existing file then recreate it
			System.IO.File.Delete(oFullFileName)
			oWriter = System.IO.File.CreateText(oFullFileName)
		ElseIf oAns = vbNo Then 'append to existing file
			oWriter = System.IO.File.AppendText(oFullFileName)
		End If
	End If
	Return oWriter
End Function

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)