Skip to content

Commit 6b3b14a

Browse files
cq.vis.show improvements (#1726)
* Handle vtkActors * Mypy fix * Update Showables * Add specular lighting * Make show non-blocking and add title * Return only qwin * Revert * Test fix * Better coverage * Adding vis docs * Add references * Primer tweaks * Cleanup * Correct file names * Tweaks * Try with -q * Try with always_yes * Remove -q * Try with mamba * Use mamba run * Set MAMBA_ROOT_PREFIX * Fix prefix * Apply suggestions from code review Co-authored-by: Jeremy Wright <[email protected]> * Do not use full screen * State correct version --------- Co-authored-by: Jeremy Wright <[email protected]>
1 parent e99a15d commit 6b3b14a

File tree

10 files changed

+311
-172
lines changed

10 files changed

+311
-172
lines changed

appveyor.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@ init:
2020
- cmd: Miniforge.exe /InstallationType=JustMe /RegisterPython=0 /S /D=%MINICONDA_DIRNAME%
2121
- cmd: set "PATH=%MINICONDA_DIRNAME%;%MINICONDA_DIRNAME%\\Scripts;%PATH%"
2222
- cmd: activate
23+
- cmd: set MAMBA_ROOT_PREFIX=C:/Miniforge/Library
2324
- sh: curl -sL https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$OS-x86_64.sh > miniconda.sh
2425
- sh: bash miniconda.sh -b -p $HOME/miniconda;
2526
- sh: export PATH="$HOME/miniconda/bin:$HOME/miniconda/lib:$PATH";
2627
- sh: source $HOME/miniconda/bin/activate
28+
- sh: export MAMBA_ROOT_PREFIX=$HOME/miniconda
2729

2830
install:
31+
- conda config --set always_yes yes
2932
- mamba env create -f environment.yml
30-
- conda activate cadquery
31-
- conda list
33+
- mamba list -n cadquery
3234

3335
build: false
3436

3537
test_script:
36-
- black . --diff --check
37-
- mypy cadquery
38-
- pytest -v --cov
38+
- mamba run -n cadquery black . --diff --check
39+
- mamba run -n cadquery mypy cadquery
40+
- mamba run -n cadquery pytest -v --cov
3941

4042
on_success:
41-
- codecov
43+
- mamba run -n cadquery codecov
4244

4345
#on_finish:
4446
# - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))

cadquery/vis.py

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from . import Shape, Workplane, Assembly, Sketch, Compound, Color, Vector, Location
2-
from .occ_impl.exporters.assembly import _vtkRenderWindow
3-
from .occ_impl.assembly import _loc2vtk
2+
from .occ_impl.assembly import _loc2vtk, toVTK
43

54
from typing import Union, Any, List, Tuple
65

@@ -15,8 +14,10 @@
1514
vtkMapper,
1615
vtkRenderWindowInteractor,
1716
vtkActor,
17+
vtkProp,
1818
vtkPolyDataMapper,
1919
vtkAssembly,
20+
vtkRenderWindow,
2021
)
2122
from vtkmodules.vtkCommonCore import vtkPoints
2223
from vtkmodules.vtkCommonDataModel import vtkCellArray, vtkPolyData
@@ -27,8 +28,14 @@
2728
DEFAULT_PT_SIZE = 7.5
2829
DEFAULT_PT_COLOR = "darkviolet"
2930

31+
SPECULAR = 0.3
32+
SPECULAR_POWER = 100
33+
SPECULAR_COLOR = vtkNamedColors().GetColor3d("White")
34+
3035
ShapeLike = Union[Shape, Workplane, Assembly, Sketch, TopoDS_Shape]
31-
Showable = Union[ShapeLike, List[ShapeLike], Vector, List[Vector]]
36+
Showable = Union[
37+
ShapeLike, List[ShapeLike], Vector, List[Vector], vtkProp, List[vtkProp]
38+
]
3239

3340

3441
def _to_assy(*objs: ShapeLike, alpha: float = 1) -> Assembly:
@@ -50,14 +57,17 @@ def _to_assy(*objs: ShapeLike, alpha: float = 1) -> Assembly:
5057
return assy
5158

5259

53-
def _split_showables(objs) -> Tuple[List[ShapeLike], List[Vector], List[Location]]:
60+
def _split_showables(
61+
objs,
62+
) -> Tuple[List[ShapeLike], List[Vector], List[Location], List[vtkProp]]:
5463
"""
5564
Split into showables and others.
5665
"""
5766

5867
rv_s: List[ShapeLike] = []
5968
rv_v: List[Vector] = []
6069
rv_l: List[Location] = []
70+
rv_a: List[vtkProp] = []
6171

6272
for el in objs:
6373
if instance_of(el, ShapeLike):
@@ -66,21 +76,24 @@ def _split_showables(objs) -> Tuple[List[ShapeLike], List[Vector], List[Location
6676
rv_v.append(el)
6777
elif isinstance(el, Location):
6878
rv_l.append(el)
79+
elif isinstance(el, vtkProp):
80+
rv_a.append(el)
6981
elif isinstance(el, list):
70-
tmp1, tmp2, tmp3 = _split_showables(el) # split recursively
82+
tmp1, tmp2, tmp3, tmp4 = _split_showables(el) # split recursively
7183

7284
rv_s.extend(tmp1)
7385
rv_v.extend(tmp2)
7486
rv_l.extend(tmp3)
87+
rv_a.extend(tmp4)
7588

76-
return rv_s, rv_v, rv_l
89+
return rv_s, rv_v, rv_l, rv_a
7790

7891

7992
def _to_vtk_pts(
8093
vecs: List[Vector], size: float = DEFAULT_PT_SIZE, color: str = DEFAULT_PT_COLOR
8194
) -> vtkActor:
8295
"""
83-
Convert vectors to vtkActor.
96+
Convert Vectors to vtkActor.
8497
"""
8598

8699
rv = vtkActor()
@@ -110,7 +123,7 @@ def _to_vtk_pts(
110123

111124
def _to_vtk_axs(locs: List[Location], scale: float = 0.1) -> vtkActor:
112125
"""
113-
Convert vectors to vtkActor.
126+
Convert Locations to vtkActor.
114127
"""
115128

116129
rv = vtkAssembly()
@@ -135,14 +148,16 @@ def show(
135148
alpha: float = 1,
136149
tolerance: float = 1e-3,
137150
edges: bool = False,
151+
specular: bool = True,
152+
title: str = "CQ viewer",
138153
**kwrags: Any,
139154
):
140155
"""
141156
Show CQ objects using VTK.
142157
"""
143158

144159
# split objects
145-
shapes, vecs, locs = _split_showables(objs)
160+
shapes, vecs, locs, props = _split_showables(objs)
146161

147162
# construct the assy
148163
assy = _to_assy(*shapes, alpha=alpha)
@@ -151,19 +166,28 @@ def show(
151166
pts = _to_vtk_pts(vecs)
152167
axs = _to_vtk_axs(locs, scale=scale)
153168

154-
# create a VTK window
155-
win = _vtkRenderWindow(assy, tolerance=tolerance)
169+
# assy+renderer
170+
renderer = toVTK(assy, tolerance=tolerance)
156171

157-
win.SetWindowName("CQ viewer")
172+
# VTK window boilerplate
173+
win = vtkRenderWindow()
174+
win.SetWindowName(title)
175+
win.AddRenderer(renderer)
158176

159177
# get renderer and actor
160-
if edges:
161-
ren = win.GetRenderers().GetFirstRenderer()
162-
for act in ren.GetActors():
163-
act.GetProperty().EdgeVisibilityOn()
178+
for act in renderer.GetActors():
179+
180+
propt = act.GetProperty()
181+
182+
if edges:
183+
propt.EdgeVisibilityOn()
184+
185+
if specular:
186+
propt.SetSpecular(SPECULAR)
187+
propt.SetSpecularPower(SPECULAR_POWER)
188+
propt.SetSpecularColor(SPECULAR_COLOR)
164189

165190
# rendering related settings
166-
win.SetMultiSamples(16)
167191
vtkMapper.SetResolveCoincidentTopologyToPolygonOffset()
168192
vtkMapper.SetResolveCoincidentTopologyPolygonOffsetParameters(1, 0)
169193
vtkMapper.SetResolveCoincidentTopologyLineOffsetParameters(-1, 0)
@@ -193,7 +217,7 @@ def show(
193217
orient_widget.InteractiveOff()
194218

195219
# use gradient background
196-
renderer = win.GetRenderers().GetFirstRenderer()
220+
renderer.SetBackground(1, 1, 1)
197221
renderer.GradientBackgroundOn()
198222

199223
# use FXXAA
@@ -209,9 +233,15 @@ def show(
209233
renderer.AddActor(pts)
210234
renderer.AddActor(axs)
211235

236+
# add other vtk actors
237+
for p in props:
238+
renderer.AddActor(p)
239+
212240
# initialize and set size
213241
inter.Initialize()
214-
win.SetSize(*win.GetScreenSize())
242+
243+
w, h = win.GetScreenSize()
244+
win.SetSize((w // 2, h // 2))
215245
win.SetPosition(-10, 0)
216246

217247
# show and return

doc/_static/show.PNG

314 KB
Loading

doc/_static/show_demo.PNG

290 KB
Loading

doc/_static/show_jupyter.PNG

152 KB
Loading

doc/_static/show_vtk.PNG

107 KB
Loading

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Table Of Contents
4141
sketch.rst
4242
assy.rst
4343
free-func.rst
44+
vis.rst
4445
fileformat.rst
4546
examples.rst
4647
apireference.rst

0 commit comments

Comments
 (0)