Skip to content

Commit

Permalink
Support transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Mar 7, 2024
1 parent 43d7c6c commit 3ff1715
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
15 changes: 6 additions & 9 deletions ifc_generator.py
Expand Up @@ -146,8 +146,6 @@ def generate_shape(ifcfile, elements, cached=False):

# get colors
sstyle = item.geometry.surface_styles
# color = (color[0], color[1], color[2], 1.0 - color[3])
# TODO temp workaround for tranparency bug
scolors = []
if (
(len(sstyle) > 4)
Expand All @@ -158,13 +156,13 @@ def generate_shape(ifcfile, elements, cached=False):
for i in range(len(shape.Solids)):
for j in range(len(shape.Solids[i].Faces)):
scolors.append(
(sstyle[i * 4], sstyle[i * 4 + 1], sstyle[i * 4 + 2], 0.0)
(sstyle[i * 4], sstyle[i * 4 + 1], sstyle[i * 4 + 2], 1.0 - sstyle[i * 4 + 3])
)
if len(colors) < len(shape.Faces):
for i in range(len(shape.Faces) - len(colors)):
scolors.append((sstyle[0], sstyle[1], sstyle[2], 0.0))
scolors.append((sstyle[0], sstyle[1], sstyle[2], 1.0 - sstyle[3]))
else:
color = (sstyle[0], sstyle[1], sstyle[2], 0.0)
color = (sstyle[0], sstyle[1], sstyle[2], 1.0 - sstyle[3])
for f in shape.Faces:
scolors.append(color)

Expand Down Expand Up @@ -213,7 +211,6 @@ def generate_coin(ifcfile, elements, cached=False):
if element.id() in cache["Coin"]:
node = cache["Coin"][element.id()]
if grouping:
print("applying placement")
node = apply_placement(node, placement)
nodes.append(node)
else:
Expand Down Expand Up @@ -244,9 +241,9 @@ def generate_coin(ifcfile, elements, cached=False):
if item.geometry.materials:
color = item.geometry.materials[0].diffuse
color = (float(color[0]), float(color[1]), float(color[2]))
# TODO treat transparency
# mat.transparency.setValue(0.8)
# TODO treat multiple materials
trans = item.geometry.materials[0].transparency
if trans >= 0:
color += (float(trans),)
else:
color = (0.85, 0.85, 0.85)

Expand Down
11 changes: 10 additions & 1 deletion ifc_tools.py
Expand Up @@ -735,10 +735,19 @@ def set_colors(obj, colors):
if hasattr(obj.ViewObject, "ShapeColor"):
if isinstance(colors[0], (tuple, list)):
obj.ViewObject.ShapeColor = colors[0][:3]
if len(colors[0]) > 3:
obj.ViewObject.Transparency = int(colors[0][3]*100)
else:
obj.ViewObject.ShapeColor = colors[:3]
if len(colors) > 3:
obj.ViewObject.Transparency = int(colors[3]*100)
if hasattr(obj.ViewObject, "DiffuseColor"):
obj.ViewObject.DiffuseColor = colors
# strip out transparency value because it currently gives ugly
# results in FreeCAD when combining transparent and non-transparent objects
if all([len(c) > 3 and c[3] != 0 for c in colors]):
obj.ViewObject.DiffuseColor = colors
else:
obj.ViewObject.DiffuseColor = [c[:3] for c in colors]


def get_body_context_ids(ifcfile):
Expand Down

0 comments on commit 3ff1715

Please sign in to comment.