Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get specific texture image (.png or .jpeg) file path used on a face.

1 REPLY 1
Reply
Message 1 of 2
aniket_pachore
123 Views, 1 Reply

How to get specific texture image (.png or .jpeg) file path used on a face.

foreach (Face face in surfaceBody.Faces)
{
    Asset faceAppearance = face.Appearance;

    if (faceAppearance != null)
    {
        AssetTypeEnum assetType = faceAppearance.AssetType;

        if (assetType == AssetTypeEnum.kAssetTypeAppearance)
        {
			GLTFMaterial gltfMaterial = new GLTFMaterial();
			GLTFPBR pbr = new GLTFPBR();
			float opacity = 1;
			string matId = null;

			if (faceAppearance.HasTexture)
			{
				foreach (AssetValue assetValue in faceAppearance)
				{
					string textureImgName = faceAppearance.DisplayName;
					AssetValueTypeEnum assetValueEnum  = assetValue.ValueType;

					if (assetValueEnum == AssetValueTypeEnum.kAssetValueTypeColor)
					{
						ColorAssetValue colorValue = (ColorAssetValue)assetValue;
						AssetTexture assetTextureValue = colorValue.ConnectedTexture as AssetTexture;

						if (assetTextureValue != null)
						{
							AssetTextureTypeEnum assetTextureTypeEnum = assetTextureValue.TextureType;

							How to get a full path e.g(C:\Program Files\Common Files\Autodesk Shared\Materials\Textures\3\Mats\Finishes.Flooring.Carpet.DarkGray.Colour.png)
							string textureFilePath = 

							if (!string.IsNullOrEmpty(textureFilePath))
							{

								string folderName = "Textures";
								string folderPath = CreateFolderAtRoot(folderName);
								string destinationTexturePath = CopyTextureToFolder(textureFilePath, folderPath);
								string textureFileName = System.IO.Path.GetFileName(destinationTexturePath);
								string texPath = folderName + "/" + textureFileName;

								gltfMaterial.alphaMode = opacity != 1 ? BLEND : OPAQUE;
								gltfMaterial.alphaCutoff = null;
								gltfMaterial.name = faceAppearance.DisplayName;

								// Handle bitmap textures
								if (assetTextureTypeEnum == AssetTextureTypeEnum.kTextureTypeBitmap)
								{

									matId = string.Concat(InvAddIn.Core.Constants.faceId, assetTextureValue.Type);

									setPBRMaterialsPropertiesContainsTexture(faceAppearance, texPath, opacity, ref images, ref textures, ref pbr, ref gltfMaterial);

									break; 
								}
							}
						}
					}
				}
			}
			
        }

    }
}

 

As I am working on a Inventor part file to GLTF file convertor.

At line number 33, I want to get the exact path of file texture file which is used on a face using Inventor API.
Can anyone help me to get this location with the help of API?

aniket_pachore_0-1727441937766.png

 

1 REPLY 1
Message 2 of 2

I used the following iLogic rule to print all (or at least most) AssetValue for a face appearance.  (Disclaimer: I only tested it on one face. Reslut may be different on your faces.)

Sub main()

    Dim doc As PartDocument = ThisDoc.Document
    Dim body As SurfaceBody = doc.ComponentDefinition.SurfaceBodies.Item(1)

    Dim face = body.Faces.Item(1)
    Dim appearance = face.Appearance


    LogAssetValues(appearance.Cast(Of AssetValue))

End Sub

Private Sub LogAssetValues(assetValues As IEnumerable(Of AssetValue), Optional preText As String = "")

    For Each assetValue As AssetValue In assetValues



        Select Case AssetValue.ValueType
            Case AssetValueTypeEnum.kAssetValueTypeString
                PrintAssetValueDefault(preText, AssetValue)

            Case AssetValueTypeEnum.kAssetValueTypeBoolean
                PrintAssetValueDefault(preText, AssetValue)

            Case AssetValueTypeEnum.kAssetValueTypeInteger
                PrintAssetValueDefault(preText, AssetValue)

            Case AssetValueTypeEnum.kAssetValueTypeFloat
                PrintAssetValueDefault(preText, AssetValue)
                'logger.Info(preText + assetValue.DisplayName + ": " + assetValue.Value.ToString())

            Case AssetValueTypeEnum.kAssetValueTypeFilename
                Dim a As FilenameAssetValue = AssetValue
                If (a.HasMultipleValues) Then
                    logger.Info(preText + a.DisplayName + " (" + a.Name + ") has values: ")
                    For Each val As String In a.Values
                        logger.Info(preText + " - " + Val)
                    Next
                Else
                    logger.Info(preText + a.DisplayName + " (" + a.Name + ") has value: " + a.Value)
                End If


            Case AssetValueTypeEnum.kAssetValueTypeColor
                Dim a As ColorAssetValue = AssetValue

                If (a.HasMultipleValues) Then
                    logger.Info(preText + a.DisplayName + " (" + a.Name + ") has values: -----------------------------------------------")
                    For Each color As Color In a.Values
                        Dim colorString = String.Format("Red:{0}, Green:{1}, Blue:{2}, Opacity:{3}",
                                                Color.Red, Color.Green, Color.Blue, Color.Opacity)

                        If (a.HasConnectedTexture) Then
                            logger.Info(preText + "  - " + a.DisplayName + ": " + colorString + "   (Has connected texture:)")
                            Dim textureAsset As AssetTexture = a.ConnectedTexture
                            LogAssetValues(textureAsset.Cast(Of AssetValue), "      " + preText)
                        Else
                            logger.Info(preText + a.DisplayName + " (" + a.Name + "): " + colorString + "   (NONE connected texture)")
                        End If
                    Next
                Else
                    Dim color = a.Value
                    Dim colorString = String.Format("Red:{0}, Green:{1}, Blue:{2}, Opacity:{3}",
                                                color.Red, color.Green, color.Blue, color.Opacity)

                    If (a.HasConnectedTexture) Then
                        logger.Info(preText + a.DisplayName + " (" + a.Name + "): " + colorString + "   Has connected texture:")
                        Dim textureAsset As AssetTexture = a.ConnectedTexture
                        LogAssetValues(textureAsset.Cast(Of AssetValue), "      " + preText)
                    Else
                        logger.Info(preText + a.DisplayName + " (" + a.Name + "): " + colorString + "   (NONE connected texture)")
                    End If
                End If

            Case AssetValueTypeEnum.kAssetValueTypeChoice
                Dim a As ChoiceAssetValue = AssetValue

                Dim names As String() = {}
                Dim choises As String() = {}

                a.GetChoices(names, choises)
                Dim allChoises = String.Join("//", choises)

                logger.Info(preText + a.DisplayName + " (" + a.Name + ") has value: " + a.Value + "(" + allChoises + ")")

            Case Else

                logger.Info("----------> " + AssetValue.DisplayName + ": " + CType(AssetValue.Type, ObjectTypeEnum).ToString())
        End Select

    Next

End Sub

Private Sub PrintAssetValueDefault(preText As String, assetValue As AssetValue)
    Dim template = preText + "{0} ({1}): {2}"

    logger.Info(String.Format(template, assetValue.DisplayName, assetValue.Name, assetValue.Value.ToString()))
End Sub

With this I came to the conclusion that you can get the filename using this line of code:

appearance.Item("generic_diffuse").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value

As a warning, I found that the file path can be a relative path. and there can be multiple paths. Have a look at what I found for my "Bump" texture:

JelteDeJong_2-1727632364666.png

JelteDeJong_3-1727632374477.png

 

 

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report