Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Common_glTF_Exporter/Common_glTF_Exporter.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Materials\BitmapsUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Materials\MaterialProperties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Materials\MaterialTextures.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Materials\TextureLocation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Model\GlbComponents.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Model\Links.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Model\LoadContext.cs" />
Expand Down Expand Up @@ -68,6 +69,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Utils\MaterialUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\GeometryUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\GLTFBinaryDataUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\RevitIniReader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\SettingsConfig.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Theme.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Version\DownloadFile.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Common_glTF_Exporter/Core/GlTFExportContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ public void OnPolymesh(PolymeshTopology polymesh)
if (preferences.materials == MaterialsEnum.textures && currentMaterial?.EmbeddedTexturePath != null)
{
UV uv = uvs[index];
geomItem.Uvs.Add(uv);
UV flippedUV = new UV(uv.U, 1.0 - uv.V);
geomItem.Uvs.Add(flippedUV);
}
}
}
Expand Down
46 changes: 38 additions & 8 deletions Common_glTF_Exporter/Materials/AssetProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Revit_glTF_Exporter;

namespace Common_glTF_Exporter.Materials
{
Expand All @@ -15,7 +16,7 @@ public static class AssetPropertiesUtils
Autodesk.Revit.DB.Visual.Hardwood.HardwoodColor,
Autodesk.Revit.DB.Visual.AdvancedMetal.SurfaceAlbedo
};
private const string AUTODESKPATHTEXTURES = @"Autodesk Shared\Materials\Textures\";


public static Asset GetDiffuseBitmap(Asset theAsset)
{
Expand All @@ -41,17 +42,25 @@ public static string GetTexturePath(Asset connectedAsset)

if (bitmapPathProp != null && !string.IsNullOrEmpty(bitmapPathProp.Value))
{
string texturePath = bitmapPathProp.Value.Split('|')[0].Replace("/", "\\");
string relativeOrAbsolutePath = bitmapPathProp.Value.Split('|')[0].Replace("/", "\\");

if (!Path.IsPathRooted(texturePath))
// If already absolute and the file exists, return it directly
if (Path.IsPathRooted(relativeOrAbsolutePath) && File.Exists(relativeOrAbsolutePath))
{
string materialsPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles),
AUTODESKPATHTEXTURES);
texturePath = Path.Combine(materialsPath, texturePath);
return relativeOrAbsolutePath;
}

return texturePath;
// Otherwise, search each base path
foreach (string basePath in MainWindow.TexturePaths)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add defensive null check for MainWindow.TexturePaths.

Consider adding a null check for MainWindow.TexturePaths to prevent potential NullReferenceException if the static property isn't properly initialized.

// Otherwise, search each base path
+if (MainWindow.TexturePaths != null)
+{
    foreach (string basePath in MainWindow.TexturePaths)

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In Common_glTF_Exporter/Materials/AssetProperties.cs at line 54, add a null
check before iterating over MainWindow.TexturePaths to prevent a
NullReferenceException. Modify the code to verify that MainWindow.TexturePaths
is not null before the foreach loop, and only proceed with the iteration if it
is initialized.

{
string candidatePath = Path.Combine(basePath, relativeOrAbsolutePath);
string fullPath = Path.GetFullPath(candidatePath);

if (File.Exists(fullPath))
{
return fullPath;
}
}
}
}

Expand Down Expand Up @@ -110,6 +119,27 @@ public static float GetScale(Asset connectedAsset, string textureName)
return 1;
}

public static float GetOffset(Asset connectedAsset, string textureName)
{
AssetPropertyDistance offset =
connectedAsset.FindByName(textureName) as AssetPropertyDistance;

if (offset != null)
{
double offsetValue;

#if REVIT2019 || REVIT2020
offsetValue = UnitUtils.Convert(offset.Value, offset.DisplayUnitType, DisplayUnitType.DUT_DECIMAL_FEET);
#else
offsetValue = UnitUtils.Convert(offset.Value, offset.GetUnitTypeId(), UnitTypeId.Feet);
#endif

return (float)offsetValue;
}

return 0;
}

public static Autodesk.Revit.DB.Color GetTint(Asset asset)
{
bool tintOn = true;
Expand Down
1 change: 1 addition & 0 deletions Common_glTF_Exporter/Materials/BitmapsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify the necessity of the System.Linq using directive.

The using System.Linq; directive has been added, but the current code in this file doesn't appear to use any LINQ operations. Ensure this directive is actually needed or remove it if it's not being used.


🏁 Script executed:

#!/bin/bash
# Description: Check if LINQ operations are used in BitmapsUtils.cs
# Expected: Find LINQ method calls or confirm the directive is unnecessary

# Search for common LINQ methods in the file
ast-grep --pattern 'Where($$$)' Common_glTF_Exporter/Materials/BitmapsUtils.cs
ast-grep --pattern 'Select($$$)' Common_glTF_Exporter/Materials/BitmapsUtils.cs  
ast-grep --pattern 'Any($$$)' Common_glTF_Exporter/Materials/BitmapsUtils.cs
ast-grep --pattern 'First($$$)' Common_glTF_Exporter/Materials/BitmapsUtils.cs
rg -n '\.(Where|Select|Any|First|Last|Count|Sum|Min|Max|OrderBy|GroupBy)\(' Common_glTF_Exporter/Materials/BitmapsUtils.cs

Length of output: 627


Remove the unused System.Linq directive

The using System.Linq; on line 6 of Common_glTF_Exporter/Materials/BitmapsUtils.cs isn’t needed—no LINQ methods are used in this file. Please remove it:

- using System.Linq;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
using System.Linq;
🤖 Prompt for AI Agents
In Common_glTF_Exporter/Materials/BitmapsUtils.cs at line 6, remove the unused
using directive 'using System.Linq;' since no LINQ methods are utilized in this
file.

using System.Runtime.InteropServices;
using System.Text;

Expand Down
58 changes: 41 additions & 17 deletions Common_glTF_Exporter/Materials/MaterialProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void SetProperties(MaterialNode node, float opacity, ref GLTFPBR p
gl_mat.alphaCutoff = null;
}

public static void SetMaterialColour(MaterialNode node,
public static void SetMaterialColour(MaterialNode node,
float opacity, ref GLTFPBR pbr, ref GLTFMaterial gl_mat)
{
if (gl_mat.EmbeddedTexturePath == null)
Expand All @@ -28,33 +28,57 @@ public static void SetMaterialColour(MaterialNode node,
float sg = node.Color.Green / 255f;
float sb = node.Color.Blue / 255f;

// A linear conversion is needed to reflect the real colour
float lr = SrgbToLinear(sr);
float lg = SrgbToLinear(sg);
float lb = SrgbToLinear(sb);
if (gl_mat.TintColour == null)
{
// A linear conversion is needed to reflect the real colour
float lr = SrgbToLinear(sr);
float lg = SrgbToLinear(sg);
float lb = SrgbToLinear(sb);

pbr.baseColorFactor = new List<float>(4)
pbr.baseColorFactor = new List<float>(4)
{
lr,
lg,
lb,
opacity
};
}
else
{
lr,
lg,
lb,
opacity
};
// Apply tint by multiplying base color by tint color
float tr = gl_mat.TintColour.Red / 255f;
float tg = gl_mat.TintColour.Green / 255f;
float tb = gl_mat.TintColour.Blue / 255f;

// Convert to linear space after applying tint
float lr = SrgbToLinear(sr * tr);
float lg = SrgbToLinear(sg * tg);
float lb = SrgbToLinear(sb * tb);

pbr.baseColorFactor = new List<float>(4)
{
lr,
lg,
lb,
opacity
};
}
}
else
{
gl_mat.pbrMetallicRoughness.baseColorFactor = new List<float>(4)
{
1,
1,
1,
opacity
};
{
1,
1,
1,
opacity
};
}

gl_mat.pbrMetallicRoughness = pbr;
}


public static float SrgbToLinear(float srgb)
{
return srgb <= 0.04045f
Expand Down
18 changes: 15 additions & 3 deletions Common_glTF_Exporter/Materials/MaterialTextures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
using System.Windows.Media.Media3D;
using Material = Autodesk.Revit.DB.Material;
using System;
using Common_glTF_Exporter.Utils;

namespace Common_glTF_Exporter.Materials
{
public static class MaterialTextures
{

public static GLTFMaterial SetMaterialTextures(Material material, GLTFMaterial gl_mat,
Document doc, float opacity)
{
Expand All @@ -37,7 +39,7 @@ public static GLTFMaterial SetMaterialTextures(Material material, GLTFMaterial g

Asset connectedAsset = AssetPropertiesUtils.GetDiffuseBitmap(theAsset);
string texturePath = AssetPropertiesUtils.GetTexturePath(connectedAsset);

gl_mat.TintColour = AssetPropertiesUtils.GetTint(theAsset);

if (!string.IsNullOrEmpty(texturePath) && File.Exists(texturePath))
{
Expand All @@ -54,19 +56,29 @@ private static void SetTextureProperties(GLTFMaterial gl_mat, string texturePath

float scaleX = AssetPropertiesUtils.GetScale(connectedAsset, UnifiedBitmap.TextureRealWorldScaleX);
float scaleY = AssetPropertiesUtils.GetScale(connectedAsset, UnifiedBitmap.TextureRealWorldScaleY);
float offsetX = AssetPropertiesUtils.GetOffset(connectedAsset, UnifiedBitmap.TextureRealWorldOffsetX);
float offsetY = AssetPropertiesUtils.GetOffset(connectedAsset, UnifiedBitmap.TextureRealWorldOffsetY);
float rotation = AssetPropertiesUtils.GetRotationRadians(connectedAsset);

gl_mat.Fadevalue = AssetPropertiesUtils.GetFade(theAsset);
gl_mat.TintColour = AssetPropertiesUtils.GetTint(theAsset);
gl_mat.BaseColor = AssetPropertiesUtils.GetAppearenceColor(theAsset);

float[] gltfScale = new float[] { 1f / scaleX, 1f / scaleY };
float[] gltfOffset = new float[]
{
-(offsetX / scaleX),
offsetY / scaleY - gltfScale[1]
};

gl_mat.pbrMetallicRoughness.baseColorTexture = new GLTFTextureInfo
{
index = -1,
extensions = new GLTFTextureExtensions
{
TextureTransform = new GLTFTextureTransform
{
scale = new float[] { 1f / scaleX, 1f / scaleY },
offset = gltfOffset,
scale = gltfScale,
rotation = rotation
}
}
Expand Down
30 changes: 30 additions & 0 deletions Common_glTF_Exporter/Materials/TextureLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using Common_glTF_Exporter.Utils;

namespace Common_glTF_Exporter.Materials
{
public static class TextureLocation
{
private const string AUTODESK_TEXTURES = @"Autodesk Shared\Materials\Textures\";

public static List<string> GetPaths()
{
var paths = new List<string>
{
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles),
AUTODESK_TEXTURES)
};

var externalPaths = RevitIniReader.GetAdditionalRenderAppearancePaths();
if (externalPaths?.Count > 0)
{
paths.AddRange(externalPaths);
}

return paths;
}
}
}
8 changes: 7 additions & 1 deletion Common_glTF_Exporter/Service/RevitCollectorService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
Expand All @@ -23,5 +24,10 @@
{
return uiApplication.ActiveUIDocument.Document;
}

public Application GetApplication()

Check failure on line 28 in Common_glTF_Exporter/Service/RevitCollectorService.cs

View workflow job for this annotation

GitHub Actions / build

'Application' is an ambiguous reference between 'Autodesk.Revit.ApplicationServices.Application' and 'System.Windows.Forms.Application'

Check failure on line 28 in Common_glTF_Exporter/Service/RevitCollectorService.cs

View workflow job for this annotation

GitHub Actions / build

'Application' is an ambiguous reference between 'Autodesk.Revit.ApplicationServices.Application' and 'System.Windows.Forms.Application'
{
return uiApplication.Application;
}
}
}
64 changes: 64 additions & 0 deletions Common_glTF_Exporter/Utils/RevitIniReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Text;
using Revit_glTF_Exporter;
using System.IO;
using System.Linq;
using Autodesk.Revit.ApplicationServices;

namespace Common_glTF_Exporter.Utils
{
public static class RevitIniReader
{
public static List<string> GetAdditionalRenderAppearancePaths()
{
string revitVersion = ExternalApplication.RevitCollectorService.GetApplication().VersionNumber;
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

string iniDir = Path.Combine(
appData,
"Autodesk",
"Revit",
$"Autodesk Revit {revitVersion}"
);

string iniPath = Path.Combine(iniDir, "Revit.ini");

if (!File.Exists(iniPath))
return null;

foreach (var line in File.ReadAllLines(iniPath))
{
if (line.StartsWith("AdditionalRenderAppearancePaths="))
{
string pathString = line.Substring("AdditionalRenderAppearancePaths=".Length);
var paths = pathString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

var absolutePaths = new List<string>();
string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

foreach (var p in paths)
{
string trimmedPath = p.Trim();

// If already rooted, normalize it
if (Path.IsPathRooted(trimmedPath))
{
absolutePaths.Add(Path.GetFullPath(trimmedPath));
}
else
{
// Combine with user profile to resolve relative path
string fullPath = Path.GetFullPath(Path.Combine(userProfile, trimmedPath));
absolutePaths.Add(fullPath);
}
}

return absolutePaths;
}
}

return null;
}
}
}
Loading
Loading