Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to the `IfcIO` library are documented in this file.
## [Unreleased]

### Fixed
- IFC import reads wrapped and direct surface colours, rendering transparency and material definition styles.
Explicit geometry styles take precedence over associated material colours.
- IFC import recognizes IFC4.3 separately and reports unknown, missing and multiple schema identifiers. IFC4.3
is read as a subset and is not silently rewritten as IFC4.
- Element factories allocate IDs across the model rather than per storey; the writer avoids generated GlobalId
Expand Down
4 changes: 3 additions & 1 deletion docs/scene-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ rebases them, for example on `ComputeBounds().Center`, before narrowing them to
unit (for example centimetres). Area, volume and weight quantities retain the file's values.
- An element without a GlobalId in the file gets a new random GlobalId from IfcIO each time it is read, so its
`SourceId` is not stable.
- Transparency is not read from files by IfcIO; materials built in memory keep their alpha.
- Colours and transparency from IFC surface styles are read separately from physical materials. The imported mesh
still uses one colour per element: the first readable styled item takes precedence over an associated material or
the material of a single layer/profile.
- Tasks without planned start and finish are left out (IfcIO reads task times from IFC4 files only); their subtasks
are kept. Task costs and progress are not converted. Openings, ports, grids, zones, systems and cost schedules are
not converted.
Expand Down
4 changes: 2 additions & 2 deletions src/IfcIO.Scene/IfcSceneReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace IfcIO.Scene;
/// <see cref="SceneValue.Unit"/> means dimensionless and the adapter converts no value to SI. Property values are text
/// as IfcIO reads them, because IfcIO does not report the measure of a value read from a file. A count quantity is a
/// number with a null unit; length, area, volume and weight quantities are in the file's units, so they are text with
/// the number as IfcIO reads it. Colour and transparency come from <see cref="IfcElement.Material"/>;
/// the number as IfcIO reads it. Colour and transparency come from <see cref="IfcElement.GetEffectiveMaterial"/>;
/// elements without one get a colour for their IFC class. Work schedules become schedules. Openings are not converted.
/// </para>
/// </remarks>
Expand Down Expand Up @@ -240,7 +240,7 @@ private static bool IsNotProduct(string ifcType) =>
int geometryId = doc.Geometries.Count + 1;
doc.Geometries.Add(new SceneGeometry { Id = geometryId, Topology = SceneTopology.Triangles, Vertices = vertices, Indices = [.. source.Indices] });
var instance = new SceneInstance { ElementId = element.Id, GeometryId = geometryId };
if (element.Material is { } material) instance.MaterialId = MaterialId(doc, material, materials);
if (element.GetEffectiveMaterial() is { } material) instance.MaterialId = MaterialId(doc, material, materials);
else instance.Color = DefaultColor(element);
doc.Instances.Add(instance);
return null;
Expand Down
10 changes: 5 additions & 5 deletions src/IfcIO/Compatibility/Exporters/IfcGlbExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ public static void Write(IfcModel model, Stream output)
{

int? matId = null;
if (elem.Material is not null)
if (elem.GetEffectiveMaterial() is { } material)
{
if (!matIndexMap.TryGetValue(elem.Material, out int existingMat))
if (!matIndexMap.TryGetValue(material, out int existingMat))
{
existingMat = gltfMaterials.Count;
matIndexMap[elem.Material] = existingMat;
matIndexMap[material] = existingMat;
gltfMaterials.Add(new MaterialDef
{
Name = elem.Material.Name,
Name = material.Name,
Pbr = new PbrDef
{
BaseColorFactor = [(float)elem.Material.R, (float)elem.Material.G, (float)elem.Material.B, (float)elem.Material.Alpha],
BaseColorFactor = [(float)material.R, (float)material.G, (float)material.B, (float)material.Alpha],
MetallicFactor = 0.1f,
RoughnessFactor = 0.7f,
},
Expand Down
4 changes: 2 additions & 2 deletions src/IfcIO/Compatibility/Exporters/IfcHtmlViewerExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,9 +1078,9 @@ private static string BuildJsonPayload(IfcModel model, IfcHtmlViewerOptions opti

private static (double r, double g, double b, double a) GetElementColor(IfcElement elem)
{
if (elem.Material is not null)
if (elem.GetEffectiveMaterial() is { } material)
{
return (elem.Material.R, elem.Material.G, elem.Material.B, elem.Material.Alpha);
return (material.R, material.G, material.B, material.Alpha);
}

string type = elem.IfcType.ToUpperInvariant();
Expand Down
8 changes: 4 additions & 4 deletions src/IfcIO/Compatibility/Exporters/IfcObjExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public static void Write(IfcModel model, TextWriter objWriter, TextWriter mtlWri
objWriter.WriteLine($"g {safeName}_{elem.Id}");
objWriter.WriteLine($"o {safeName}_{elem.Id}");

if (elem.Material is not null)
if (elem.GetEffectiveMaterial() is { } material)
{
if (writtenMaterials.Add(elem.Material.Name))
if (writtenMaterials.Add(material.Name))
{
WriteMaterial(mtlWriter, elem.Material, inv);
WriteMaterial(mtlWriter, material, inv);
}
objWriter.WriteLine($"usemtl {SanitizeName(elem.Material.Name)}");
objWriter.WriteLine($"usemtl {SanitizeName(material.Name)}");
}

var vertices = mesh.Vertices;
Expand Down
85 changes: 85 additions & 0 deletions src/IfcIO/IfcModel.SurfaceStyles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using IfcIO.Core;
using IfcIO.Model;

namespace IfcIO;

public sealed partial class IfcModel
{
private static (Dictionary<int, IfcMaterial> Geometry, Dictionary<int, IfcMaterial> Materials)
ReadSurfaceStyles(StepFile step)
{
var styles = new Dictionary<int, IfcMaterial>();
foreach (var style in step.OfType("IFCSURFACESTYLE"))
{
foreach (int appearanceId in style.GetRefList(2))
{
var appearance = step.Find(appearanceId);
if (appearance is null || !(appearance.Type.Equals("IFCSURFACESTYLESHADING", StringComparison.OrdinalIgnoreCase)
|| appearance.Type.Equals("IFCSURFACESTYLERENDERING", StringComparison.OrdinalIgnoreCase)))
continue;

var rgb = step.Find(appearance.GetRef(0));
if (rgb is null || !rgb.Type.Equals("IFCCOLOURRGB", StringComparison.OrdinalIgnoreCase)) continue;

var material = new IfcMaterial
{
Name = style.GetString(0) ?? "Material",
R = RestrictToUnitInterval(rgb.GetReal(1), 0.8),
G = RestrictToUnitInterval(rgb.GetReal(2), 0.8),
B = RestrictToUnitInterval(rgb.GetReal(3), 0.8),
Alpha = 1.0 - RestrictToUnitInterval(appearance.GetReal(1), 0.0),
};
styles.Add(style.Id, material);
break;
}
}

// A styled item can reference a surface style directly, or through the IFC2x3 assignment wrapper.
var assignments = new Dictionary<int, IfcMaterial>();
foreach (var assignment in step.OfType("IFCPRESENTATIONSTYLEASSIGNMENT"))
{
foreach (int styleId in assignment.GetRefList(0))
{
if (!styles.TryGetValue(styleId, out var material)) continue;
assignments.Add(assignment.Id, material);
break;
}
}

var byStyledItem = new Dictionary<int, IfcMaterial>();
var byGeometry = new Dictionary<int, IfcMaterial>();
foreach (var item in step.OfType("IFCSTYLEDITEM"))
{
foreach (int styleId in item.GetRefList(1))
{
if (!styles.TryGetValue(styleId, out var material) && !assignments.TryGetValue(styleId, out material))
continue;
byStyledItem.Add(item.Id, material);
if (item.GetRef(0) is int geometryId) byGeometry[geometryId] = material;
break;
}
}

// In a material representation the styled item itself is the appearance; its Item may be unset.
var byMaterial = new Dictionary<int, IfcMaterial>();
foreach (var definition in step.OfType("IFCMATERIALDEFINITIONREPRESENTATION"))
{
if (definition.GetRef(3) is not int materialId || byMaterial.ContainsKey(materialId)) continue;
foreach (int representationId in definition.GetRefList(2))
{
var representation = step.Find(representationId);
if (representation is null || !representation.Type.Equals("IFCSTYLEDREPRESENTATION", StringComparison.OrdinalIgnoreCase))
continue;
var appearance = representation.GetRefList(3).Select(id => byStyledItem.GetValueOrDefault(id)).FirstOrDefault(m => m is not null);
if (appearance is null) continue;
byMaterial.Add(materialId, appearance);
break;
}
}

return (byGeometry, byMaterial);
}

private static double RestrictToUnitInterval(double? value, double fallback) =>
value is double channel && double.IsFinite(channel) ? Math.Clamp(channel, 0, 1) : fallback;
}
Loading