Skip to content

Commit ec81ad9

Browse files
Better default material and color handling (#371)
* Better default material and color handling * Trying to fix the tests NB: default color changed * Another test fix
1 parent 7f9739d commit ec81ad9

File tree

4 files changed

+60
-41
lines changed

4 files changed

+60
-41
lines changed

cq_editor/cq_utils.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from OCP.XCAFPrs import XCAFPrs_AISObject
99
from OCP.TopoDS import TopoDS_Shape
10-
from OCP.AIS import AIS_InteractiveObject, AIS_Shape, AIS_ColoredShape
10+
from OCP.AIS import AIS_InteractiveObject, AIS_Shape
1111
from OCP.Quantity import \
1212
Quantity_TOC_RGB as TOC_RGB, Quantity_Color
1313

@@ -62,7 +62,7 @@ def make_AIS(obj : Union[cq.Workplane, List[cq.Workplane], cq.Shape, List[cq.Sha
6262
ais = obj
6363
else:
6464
shape = to_compound(obj)
65-
ais = AIS_ColoredShape(shape.wrapped)
65+
ais = AIS_Shape(shape.wrapped)
6666

6767
if 'alpha' in options:
6868
ais.SetTransparency(options['alpha'])
@@ -105,13 +105,23 @@ def to_occ_color(color) -> Quantity_Color:
105105
color.blueF(),
106106
TOC_RGB)
107107

108-
def get_occ_color(ais : AIS_ColoredShape) -> QColor:
109-
110-
color = Quantity_Color()
111-
ais.Color(color)
112-
108+
def get_occ_color(obj : Union[AIS_InteractiveObject, Quantity_Color]) -> QColor:
109+
110+
if isinstance(obj, AIS_InteractiveObject):
111+
color = Quantity_Color()
112+
obj.Color(color)
113+
else:
114+
color = obj
115+
113116
return QColor.fromRgbF(color.Red(), color.Green(), color.Blue())
114117

118+
def set_color(ais : AIS_Shape, color : Quantity_Color) -> AIS_Shape:
119+
120+
drawer = ais.Attributes()
121+
drawer.ShadingAspect().SetColor(color)
122+
123+
return ais
124+
115125
def reload_cq():
116126

117127
# NB: order of reloads is important

cq_editor/widgets/object_tree.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
from ..mixins import ComponentMixin
1111
from ..icons import icon
12-
from ..cq_utils import make_AIS, export, to_occ_color, is_obj_empty, get_occ_color
12+
from ..cq_utils import make_AIS, export, to_occ_color, is_obj_empty, get_occ_color, set_color
13+
from .viewer import DEFAULT_FACE_COLOR
1314
from ..utils import splitter, layout, get_save_filename
1415

1516
class TopTreeItem(QTreeWidgetItem):
@@ -49,14 +50,19 @@ def __init__(self,
4950

5051
self.properties['Name'] = name
5152
self.properties['Alpha'] = ais.Transparency()
52-
self.properties['Color'] = get_occ_color(ais) if ais else color
53+
self.properties['Color'] = get_occ_color(ais) if ais and ais.HasColor() else get_occ_color(DEFAULT_FACE_COLOR)
5354
self.properties.sigTreeStateChanged.connect(self.propertiesChanged)
5455

55-
def propertiesChanged(self,*args):
56+
def propertiesChanged(self, properties, changed):
57+
58+
changed_prop = changed[0][0]
5659

5760
self.setData(0,0,self.properties['Name'])
5861
self.ais.SetTransparency(self.properties['Alpha'])
59-
self.ais.SetColor(to_occ_color(self.properties['Color']))
62+
63+
if changed_prop.name() == 'Color':
64+
set_color(self.ais, to_occ_color(self.properties['Color']))
65+
6066
self.ais.Redisplay()
6167

6268
if self.properties['Visible']:

cq_editor/widgets/viewer.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
# -*- coding: utf-8 -*-
1+
from PyQt5.QtWidgets import QWidget, QDialog, QTreeWidgetItem, QApplication, QAction
22

3-
from OCP.Graphic3d import Graphic3d_Camera, Graphic3d_StereoMode
4-
from PyQt5.QtWidgets import (QWidget, QPushButton, QDialog, QTreeWidget,
5-
QTreeWidgetItem, QVBoxLayout, QFileDialog,
6-
QHBoxLayout, QFrame, QLabel, QApplication,
7-
QToolBar, QAction)
8-
9-
from PyQt5.QtCore import QSize, pyqtSlot, pyqtSignal, QMetaObject, Qt
3+
from PyQt5.QtCore import pyqtSlot, pyqtSignal
104
from PyQt5.QtGui import QIcon
115

12-
from OCP.AIS import AIS_Shaded,AIS_WireFrame, AIS_ColoredShape, \
13-
AIS_Axis, AIS_Line
14-
from OCP.Aspect import Aspect_GDM_Lines, Aspect_GT_Rectangular, Aspect_GFM_VER
15-
from OCP.Quantity import Quantity_NOC_BLACK as BLACK, \
6+
from OCP.Graphic3d import Graphic3d_Camera, Graphic3d_StereoMode, Graphic3d_NOM_JADE,\
7+
Graphic3d_MaterialAspect
8+
from OCP.AIS import AIS_Shaded,AIS_WireFrame, AIS_ColoredShape, AIS_Axis
9+
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,\
1611
Quantity_TOC_RGB as TOC_RGB, Quantity_Color
17-
from OCP.Geom import Geom_CylindricalSurface, Geom_Plane, Geom_Circle,\
18-
Geom_TrimmedCurve, Geom_Axis1Placement, Geom_Axis2Placement, Geom_Line
19-
from OCP.gp import gp_Trsf, gp_Vec, gp_Ax3, gp_Dir, gp_Pnt, gp_Ax1
12+
from OCP.Geom import Geom_Axis1Placement
13+
from OCP.gp import gp_Ax3, gp_Dir, gp_Pnt, gp_Ax1
2014

2115
from ..utils import layout, get_save_filename
2216
from ..mixins import ComponentMixin
@@ -29,6 +23,10 @@
2923
import qtawesome as qta
3024

3125

26+
DEFAULT_FACE_COLOR = Quantity_Color(GOLD)
27+
DEFAULT_EDGE_COLOR = Quantity_Color(BLACK)
28+
DEFUALT_EDGE_WIDTH = 2
29+
3230
class OCCViewer(QWidget,ComponentMixin):
3331

3432
name = '3D Viewer'
@@ -65,8 +63,23 @@ def __init__(self,parent=None):
6563
top_widget=self,
6664
margin=0)
6765

66+
self.setup_defualt_drawer()
6867
self.updatePreferences()
6968

69+
def setup_defualt_drawer(self):
70+
71+
# set the default color and material
72+
material = Graphic3d_MaterialAspect(Graphic3d_NOM_JADE)
73+
74+
shading_aspect = self.canvas.context.DefaultDrawer().ShadingAspect()
75+
shading_aspect.SetMaterial(material)
76+
shading_aspect.SetColor(DEFAULT_FACE_COLOR)
77+
78+
# face edge lw
79+
line_aspect = self.canvas.context.DefaultDrawer().FaceBoundaryAspect()
80+
line_aspect.SetWidth(DEFUALT_EDGE_WIDTH)
81+
line_aspect.SetColor(DEFAULT_EDGE_COLOR)
82+
7083
def updatePreferences(self,*args):
7184

7285
color1 = to_occ_color(self.preferences['Background color'])

tests/test_app.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def get_rgba(ais):
114114
alpha = ais.Transparency()
115115
color = get_occ_color(ais)
116116

117-
return color.redF(),color.redF(),color.redF(),alpha
117+
return color.redF(), color.greenF(), color.blueF(), alpha
118118

119119
@pytest.fixture
120120
def main(qtbot,mocker):
@@ -1017,14 +1017,13 @@ def test_render_colors(main_clean):
10171017
CQ = obj_tree.CQ
10181018

10191019
# object 1 (defualt color)
1020-
r,g,b,a = get_rgba(CQ.child(0).ais)
1021-
assert( a == 0 )
1022-
assert( r != 1.0 )
1023-
1020+
assert not CQ.child(0).ais.HasColor()
1021+
10241022
# object 2
10251023
r,g,b,a = get_rgba(CQ.child(1).ais)
10261024
assert( a == 0.5 )
10271025
assert( r == 1.0 )
1026+
assert( g == 0.0 )
10281027

10291028
# object 3
10301029
r,g,b,a = get_rgba(CQ.child(2).ais)
@@ -1059,20 +1058,11 @@ def test_render_colors_console(main_clean):
10591058
console = win.components['console']
10601059

10611060
console.execute_command(code_color)
1062-
1063-
def get_rgba(ais):
1064-
1065-
alpha = ais.Transparency()
1066-
color = get_occ_color(ais)
1067-
1068-
return color.redF(),color.redF(),color.redF(),alpha
10691061

10701062
CQ = obj_tree.CQ
10711063

10721064
# object 1 (defualt color)
1073-
r,g,b,a = get_rgba(CQ.child(0).ais)
1074-
assert( a == 0 )
1075-
assert( r != 1.0 )
1065+
assert not CQ.child(0).ais.HasColor()
10761066

10771067
# object 2
10781068
r,g,b,a = get_rgba(CQ.child(1).ais)

0 commit comments

Comments
 (0)