Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 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
14 changes: 8 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,27 @@ init:
- cmd: Miniforge.exe /InstallationType=JustMe /RegisterPython=0 /S /D=%MINICONDA_DIRNAME%
- cmd: set "PATH=%MINICONDA_DIRNAME%;%MINICONDA_DIRNAME%\\Scripts;%PATH%"
- cmd: activate
- cmd: set MAMBA_ROOT_PREFIX=C:/Miniforge/Library
- sh: curl -sL https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$OS-x86_64.sh > miniconda.sh
- sh: bash miniconda.sh -b -p $HOME/miniconda;
- sh: export PATH="$HOME/miniconda/bin:$HOME/miniconda/lib:$PATH";
- sh: source $HOME/miniconda/bin/activate
- sh: export MAMBA_ROOT_PREFIX=$HOME/miniconda

install:
- conda config --set always_yes yes
- mamba env create -f environment.yml
- conda activate cadquery
- conda list
- mamba list -n cadquery

build: false

test_script:
- black . --diff --check
- mypy cadquery
- pytest -v --cov
- mamba run -n cadquery black . --diff --check
- mamba run -n cadquery mypy cadquery
- mamba run -n cadquery pytest -v --cov

on_success:
- codecov
- mamba run -n cadquery codecov

#on_finish:
# - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
Expand Down
64 changes: 46 additions & 18 deletions cadquery/vis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from . import Shape, Workplane, Assembly, Sketch, Compound, Color, Vector, Location
from .occ_impl.exporters.assembly import _vtkRenderWindow
from .occ_impl.assembly import _loc2vtk
from .occ_impl.assembly import _loc2vtk, toVTK

from typing import Union, Any, List, Tuple

Expand All @@ -15,8 +14,10 @@
vtkMapper,
vtkRenderWindowInteractor,
vtkActor,
vtkProp,
vtkPolyDataMapper,
vtkAssembly,
vtkRenderWindow,
)
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonDataModel import vtkCellArray, vtkPolyData
Expand All @@ -27,8 +28,14 @@
DEFAULT_PT_SIZE = 7.5
DEFAULT_PT_COLOR = "darkviolet"

SPECULAR = 0.3
SPECULAR_POWER = 100
SPECULAR_COLOR = vtkNamedColors().GetColor3d("White")

ShapeLike = Union[Shape, Workplane, Assembly, Sketch, TopoDS_Shape]
Showable = Union[ShapeLike, List[ShapeLike], Vector, List[Vector]]
Showable = Union[
ShapeLike, List[ShapeLike], Vector, List[Vector], vtkProp, List[vtkProp]
]


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


def _split_showables(objs) -> Tuple[List[ShapeLike], List[Vector], List[Location]]:
def _split_showables(
objs,
) -> Tuple[List[ShapeLike], List[Vector], List[Location], List[vtkProp]]:
"""
Split into showables and others.
"""

rv_s: List[ShapeLike] = []
rv_v: List[Vector] = []
rv_l: List[Location] = []
rv_a: List[vtkProp] = []

for el in objs:
if instance_of(el, ShapeLike):
Expand All @@ -66,21 +76,24 @@ def _split_showables(objs) -> Tuple[List[ShapeLike], List[Vector], List[Location
rv_v.append(el)
elif isinstance(el, Location):
rv_l.append(el)
elif isinstance(el, vtkProp):
rv_a.append(el)
elif isinstance(el, list):
tmp1, tmp2, tmp3 = _split_showables(el) # split recursively
tmp1, tmp2, tmp3, tmp4 = _split_showables(el) # split recursively

rv_s.extend(tmp1)
rv_v.extend(tmp2)
rv_l.extend(tmp3)
rv_a.extend(tmp4)

return rv_s, rv_v, rv_l
return rv_s, rv_v, rv_l, rv_a


def _to_vtk_pts(
vecs: List[Vector], size: float = DEFAULT_PT_SIZE, color: str = DEFAULT_PT_COLOR
) -> vtkActor:
"""
Convert vectors to vtkActor.
Convert Vectors to vtkActor.
"""

rv = vtkActor()
Expand Down Expand Up @@ -110,7 +123,7 @@ def _to_vtk_pts(

def _to_vtk_axs(locs: List[Location], scale: float = 0.1) -> vtkActor:
"""
Convert vectors to vtkActor.
Convert Locations to vtkActor.
"""

rv = vtkAssembly()
Expand All @@ -135,14 +148,16 @@ def show(
alpha: float = 1,
tolerance: float = 1e-3,
edges: bool = False,
specular: bool = True,
title: str = "CQ viewer",
**kwrags: Any,
):
"""
Show CQ objects using VTK.
"""

# split objects
shapes, vecs, locs = _split_showables(objs)
shapes, vecs, locs, props = _split_showables(objs)

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

# create a VTK window
win = _vtkRenderWindow(assy, tolerance=tolerance)
# assy+renderer
renderer = toVTK(assy, tolerance=tolerance)

win.SetWindowName("CQ viewer")
# VTK window boilerplate
win = vtkRenderWindow()
win.SetWindowName(title)
win.AddRenderer(renderer)

# get renderer and actor
if edges:
ren = win.GetRenderers().GetFirstRenderer()
for act in ren.GetActors():
act.GetProperty().EdgeVisibilityOn()
for act in renderer.GetActors():

propt = act.GetProperty()

if edges:
propt.EdgeVisibilityOn()

if specular:
propt.SetSpecular(SPECULAR)
propt.SetSpecularPower(SPECULAR_POWER)
propt.SetSpecularColor(SPECULAR_COLOR)

# rendering related settings
win.SetMultiSamples(16)
vtkMapper.SetResolveCoincidentTopologyToPolygonOffset()
vtkMapper.SetResolveCoincidentTopologyPolygonOffsetParameters(1, 0)
vtkMapper.SetResolveCoincidentTopologyLineOffsetParameters(-1, 0)
Expand Down Expand Up @@ -193,7 +217,7 @@ def show(
orient_widget.InteractiveOff()

# use gradient background
renderer = win.GetRenderers().GetFirstRenderer()
renderer.SetBackground(1, 1, 1)
renderer.GradientBackgroundOn()

# use FXXAA
Expand All @@ -209,6 +233,10 @@ def show(
renderer.AddActor(pts)
renderer.AddActor(axs)

# add other vtk actors
for p in props:
renderer.AddActor(p)

# initialize and set size
inter.Initialize()
win.SetSize(*win.GetScreenSize())
Expand Down
Binary file added doc/_static/show.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/_static/show_demo.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/_static/show_jupyter.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/_static/show_vtk.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Table Of Contents
sketch.rst
assy.rst
free-func.rst
vis.rst
fileformat.rst
examples.rst
apireference.rst
Expand Down
Loading