Skip to content

Commit a2df6ff

Browse files
Transparency/material fix (#375)
* Rough fix * Use correct var name * Setup own shading aspect explicitly * Add test
1 parent 97e0829 commit a2df6ff

File tree

3 files changed

+76
-22
lines changed

3 files changed

+76
-22
lines changed

cq_editor/cq_utils.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
from OCP.TopoDS import TopoDS_Shape
1010
from OCP.AIS import AIS_InteractiveObject, AIS_Shape
1111
from OCP.Quantity import \
12-
Quantity_TOC_RGB as TOC_RGB, Quantity_Color
13-
12+
Quantity_TOC_RGB as TOC_RGB, Quantity_Color, Quantity_NOC_GOLD as GOLD
13+
from OCP.Graphic3d import Graphic3d_NOM_JADE, Graphic3d_MaterialAspect
14+
1415
from PyQt5.QtGui import QColor
1516

17+
DEFAULT_FACE_COLOR = Quantity_Color(GOLD)
18+
DEFAULT_MATERIAL = Graphic3d_MaterialAspect(Graphic3d_NOM_JADE)
19+
1620
def find_cq_objects(results : dict):
1721

1822
return {k:SimpleNamespace(shape=v,options={}) for k,v in results.items() if isinstance(v,cq.Workplane)}
@@ -30,7 +34,7 @@ def to_compound(obj : Union[cq.Workplane, List[cq.Workplane], cq.Shape, List[cq.
3034
elif isinstance(obj,list) and isinstance(obj[0],cq.Shape):
3135
vals.extend(obj)
3236
elif isinstance(obj, TopoDS_Shape):
33-
vals.append(cq.Shape.cast(obj))
37+
vals.append(cq.Shape.cast(obj))
3438
elif isinstance(obj,list) and isinstance(obj[0],TopoDS_Shape):
3539
vals.extend(cq.Shape.cast(o) for o in obj)
3640
elif isinstance(obj, cq.Sketch):
@@ -63,15 +67,18 @@ def make_AIS(obj : Union[cq.Workplane, List[cq.Workplane], cq.Shape, List[cq.Sha
6367
else:
6468
shape = to_compound(obj)
6569
ais = AIS_Shape(shape.wrapped)
66-
70+
71+
set_material(ais, DEFAULT_MATERIAL)
72+
set_color(ais, DEFAULT_FACE_COLOR)
73+
6774
if 'alpha' in options:
68-
ais.SetTransparency(options['alpha'])
75+
set_transparency(ais, options['alpha'])
6976
if 'color' in options:
70-
ais.SetColor(to_occ_color(options['color']))
77+
set_color(ais, to_occ_color(options['color']))
7178
if 'rgba' in options:
7279
r,g,b,a = options['rgba']
73-
ais.SetColor(to_occ_color((r,g,b)))
74-
ais.SetTransparency(a)
80+
set_color(ais, to_occ_color((r,g,b)))
81+
set_transparency(ais, a)
7582

7683
return ais,shape
7784

@@ -88,7 +95,7 @@ def export(obj : Union[cq.Workplane, List[cq.Workplane]], type : str,
8895
comp.exportBrep(file)
8996

9097
def to_occ_color(color) -> Quantity_Color:
91-
98+
9299
if not isinstance(color, QColor):
93100
if isinstance(color, tuple):
94101
if isinstance(color[0], int):
@@ -118,12 +125,29 @@ def get_occ_color(obj : Union[AIS_InteractiveObject, Quantity_Color]) -> QColor:
118125
def set_color(ais : AIS_Shape, color : Quantity_Color) -> AIS_Shape:
119126

120127
drawer = ais.Attributes()
128+
drawer.SetupOwnShadingAspect()
121129
drawer.ShadingAspect().SetColor(color)
122130

123131
return ais
124132

133+
def set_material(ais : AIS_Shape, material: Graphic3d_MaterialAspect) -> AIS_Shape:
134+
135+
drawer = ais.Attributes()
136+
drawer.SetupOwnShadingAspect()
137+
drawer.ShadingAspect().SetMaterial(material)
138+
139+
return ais
140+
141+
def set_transparency(ais : AIS_Shape, alpha: float) -> AIS_Shape:
142+
143+
drawer = ais.Attributes()
144+
drawer.SetupOwnShadingAspect()
145+
drawer.ShadingAspect().SetTransparency(alpha)
146+
147+
return ais
148+
125149
def reload_cq():
126-
150+
127151
# NB: order of reloads is important
128152
reload(cq.types)
129153
reload(cq.occ_impl.geom)
@@ -147,13 +171,13 @@ def reload_cq():
147171
reload(cq.occ_impl.exporters)
148172
reload(cq.assembly)
149173
reload(cq)
150-
151-
174+
175+
152176
def is_obj_empty(obj : Union[cq.Workplane,cq.Shape]) -> bool:
153-
177+
154178
rv = False
155-
179+
156180
if isinstance(obj, cq.Workplane):
157181
rv = True if isinstance(obj.val(), cq.Vector) else False
158-
182+
159183
return rv

cq_editor/widgets/viewer.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
Graphic3d_MaterialAspect
88
from OCP.AIS import AIS_Shaded,AIS_WireFrame, AIS_ColoredShape, AIS_Axis
99
from OCP.Aspect import Aspect_GDM_Lines, Aspect_GT_Rectangular
10-
from OCP.Quantity import Quantity_NOC_BLACK as BLACK, Quantity_NOC_GOLD as GOLD,\
11-
Quantity_TOC_RGB as TOC_RGB, Quantity_Color
10+
from OCP.Quantity import Quantity_NOC_BLACK as BLACK, Quantity_TOC_RGB as TOC_RGB,\
11+
Quantity_Color
1212
from OCP.Geom import Geom_Axis1Placement
1313
from OCP.gp import gp_Ax3, gp_Dir, gp_Pnt, gp_Ax1
1414

1515
from ..utils import layout, get_save_filename
1616
from ..mixins import ComponentMixin
1717
from ..icons import icon
18-
from ..cq_utils import to_occ_color, make_AIS
18+
from ..cq_utils import to_occ_color, make_AIS, DEFAULT_FACE_COLOR
1919

2020
from .occt_widget import OCCTWidget
2121

2222
from pyqtgraph.parametertree import Parameter
2323
import qtawesome as qta
2424

2525

26-
DEFAULT_FACE_COLOR = Quantity_Color(GOLD)
26+
2727
DEFAULT_EDGE_COLOR = Quantity_Color(BLACK)
2828
DEFAULT_EDGE_WIDTH = 2
2929

@@ -62,7 +62,7 @@ def __init__(self,parent=None):
6262
[self.canvas,],
6363
top_widget=self,
6464
margin=0)
65-
65+
6666
self.setup_default_drawer()
6767
self.updatePreferences()
6868

@@ -88,9 +88,9 @@ def updatePreferences(self,*args):
8888
if not self.preferences['Use gradient']:
8989
color2 = color1
9090
self.canvas.view.SetBgGradientColors(color1,color2,theToUpdate=True)
91-
91+
9292
self.canvas.update()
93-
93+
9494
ctx = self.canvas.context
9595
ctx.SetDeviationCoefficient(self.preferences['Deviation'])
9696
ctx.SetDeviationAngle(self.preferences['Angular deviation'])

tests/test_app.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,37 @@ def test_render_colors_console(main_clean):
10921092
# check if error occured
10931093
qtbot.wait(100)
10941094
assert('Unknown color format' in log.toPlainText().splitlines()[-1])
1095+
1096+
code_shading = \
1097+
'''
1098+
import cadquery as cq
1099+
1100+
res1 = cq.Workplane('XY').box(5, 7, 5)
1101+
res2 = cq.Workplane('XY').box(8, 5, 4)
1102+
show_object(res1)
1103+
show_object(res2,options={"alpha":0})
1104+
'''
1105+
1106+
def test_shading_aspect(main_clean):
10951107

1108+
qtbot, win = main_clean
1109+
1110+
obj_tree = win.components['object_tree']
1111+
editor = win.components['editor']
1112+
debugger = win.components['debugger']
1113+
1114+
editor.set_text(code_shading)
1115+
debugger._actions['Run'][0].triggered.emit()
1116+
1117+
CQ = obj_tree.CQ
1118+
1119+
# get material aspects
1120+
ma1 = CQ.child(0).ais.Attributes().ShadingAspect().Material()
1121+
ma2 = CQ.child(1).ais.Attributes().ShadingAspect().Material()
1122+
1123+
# verify that they are the same
1124+
assert ma1.Shininess() == ma2.Shininess()
1125+
10961126
def test_confirm_new(monkeypatch,editor):
10971127

10981128
qtbot, editor = editor

0 commit comments

Comments
 (0)