Module cad_viewer_widget.widget
This module is the Python part of the CAD Viewer widget
Classes
class AnimationTrack (path, action, times, values)-
Defining a three.js animation track.
Parameters
path:string- The path (or id) of the cad object for which this track is meant.
Usually of the form
/top-level/level2/... action:{"t", "tx", "ty", "tz", "q", "rx", "ry", "rz"}-
The action type:
- "tx", "ty", "tz" for translations along the x, y or z-axis
- "t" to add a position vector (3-dim array) to the current position of the CAD object
- "rx", "ry", "rz" for rotations around x, y or z-axis
- "q" to apply a quaternion to the location of the CAD object
times:listoffloatorint- An array of floats describing the points in time where CAD object (with id
path) should be at the location defined byactionandvalues values:listoffloatorint-
An array of same length as
timesdefining the locations where the CAD objects should be according to theactionprovided. Formats:- "tx", "ty", "tz": float distance to move
- "t": 3-dim tuples or lists defining the positions to move to
- "rx", "ry", "rz": float angle in degrees
- "q" quaternions of the form (x,y,z,w) the represent the rotation to be applied
Examples
AnimationTrack( '/bottom/left_middle/lower', # path 'rz', # action [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0], # times (seconds) [-15.0, -15.0, -15.0, 9.7, 20.0, 9.7, -15.0, -15.0, -15.0] # angles ) AnimationTrack( 'base/link_4_6', # path 't', # action [0.0, 1.0, 2.0, 3.0, 4.0], # times (seconds) [[0.0, 0.0, 0.0], [0.0, 1.9509, 3.9049], [0.0 , -3.2974, -16.7545], [0.0 , 0.05894 , -32.0217], [0.0 , -3.2212, -13.3424]] # 3-dim positions )See Also
Methods
def to_array(self)-
Create an array representation of the animation track
Returns
array-like- The 4 dim array comprising of the instance variables
path,action,timesandvalues
class CadViewer (cad_width=800, height=600, tree_width=240, theme='browser', glass=False, tools=True, pinning=False, title=None, anchor=None)-
The main class for the CAD Viewer encapsulating the three-cad-viewer Javascript module
Parameters
cad_width:int, default: 800- Width of the canvas element
height:int, default: 600- Height of the canvas element
tree_width:int, default: 240- Width of the navigation tree element
theme:string, default: 'light'- UI theme, can be 'dark' or 'light' (default)
tools:bool, default: True- Whether to show CAD tools (True) or not (False)
glass:bool, default: False- Whether to use glass mode (True) or not (False)
pinning:bool, default: False- Whether to allow replacing the CAD View by a canvas screenshot
See Also
Instance variables
var ambient_intensity-
Get or set the CadViewerWidget traitlet
ambient_intensity. see CadViewerWidget.ambient_intensity var axes-
Get or set the CadViewerWidget traitlet
axessee CadViewerWidget.axes var axes0-
Get or set the CadViewerWidget traitlet
axes0see CadViewerWidget.axes0 var black_edges-
Get or set the CadViewerWidget traitlet
black_edgessee CadViewerWidget.black_edges var cad_width-
Get or set the CadViewerWidget traitlet
cad_widthsee CadViewerWidget.tools var clip_intersection-
Get or set the CadViewerWidget traitlet
clip_intersectionsee CadViewerWidget.clip_intersection var clip_normal_0-
Get or set the CadViewerWidget traitlet
clip_normal_0see CadViewerWidget.clip_normal_0 var clip_normal_1-
Get or set the CadViewerWidget traitlet
clip_normal_1see CadViewerWidget.clip_normal_1 var clip_normal_2-
Get or set the CadViewerWidget traitlet
clip_normal_2see CadViewerWidget.clip_normal_2 var clip_planes-
Get or set the CadViewerWidget traitlet
clip_planessee CadViewerWidget.clip_planes var clip_value_0-
Get or set the CadViewerWidget traitlet
clip_slider_0see CadViewerWidget.clip_slider_0 var clip_value_1-
Get or set the CadViewerWidget traitlet
clip_slider_1see CadViewerWidget.clip_slider_1 var clip_value_2-
Get or set the CadViewerWidget traitlet
clip_slider_2see CadViewerWidget.clip_slider_2 var control-
Get or set the CadViewerWidget traitlet
controlsee CadViewerWidget.control var default_edge_color-
Get or set the CadViewerWidget traitlet
default_edge_colorsee CadViewerWidget.default_edge_color var default_opacity-
Get or set the CadViewerWidget traitlet
default_opacitysee CadViewerWidget.default_opacity var direct_intensity-
Get or set the CadViewerWidget traitlet
direct_intensitysee CadViewerWidget.direct_intensity var disposed-
Whether the Javascript viewer is disposed
var glass-
Get or set the CadViewerWidget traitlet
glasssee CadViewerWidget.tools var grid-
Get or set the CadViewerWidget traitlet
gridsee CadViewerWidget.grid var height-
Get or set the CadViewerWidget traitlet
heightsee CadViewerWidget.tools var js_debug-
Get or set the CadViewerWidget traitlet
js_debugsee CadViewerWidget.js_debug var last_pick-
Get or set the CadViewerWidget traitlet
lastPicksee CadViewerWidget.lastPick var normal_len-
Get or set the CadViewerWidget traitlet
normal_len var ortho-
Get or set the CadViewerWidget traitlet
orthosee CadViewerWidget.ortho var pan_speed-
Get or set the CadViewerWidget traitlet
pan_speedsee CadViewerWidget.pan_speed var pinning-
Get or set the CadViewerWidget traitlet
pinningsee CadViewerWidget.pinning var position-
Get or set the CadViewerWidget traitlet
positionsee CadViewerWidget.position var quaternion-
Get or set the CadViewerWidget traitlet
quaternionsee CadViewerWidget.quaternion var rotate_speed-
Get or set the CadViewerWidget traitlet
rotate_speedsee CadViewerWidget.rotate_speed var target-
Get or set the CadViewerWidget traitlet
targetsee CadViewerWidget.position var tools-
Get or set the CadViewerWidget traitlet
toolssee CadViewerWidget.tools var transparent-
Get or set the CadViewerWidget traitlet
transparentsee CadViewerWidget.transparent var tree_width-
Get or set the CadViewerWidget traitlet
tree_widthsee CadViewerWidget.tools var up-
Get or set the CadViewerWidget traitlet
upsee CadViewerWidget.up var zoom-
Get or set the CadViewerWidget traitlet
zoomsee CadViewerWidget.zoom var zoom_speed-
Get or set the CadViewerWidget traitlet
zoom_speedsee CadViewerWidget.zoom_speed
Methods
def add_shapes(self, shapes, states, tracks=None, normal_len=0, default_edge_color='#707070', default_opacity=0.5, ambient_intensity=0.5, direct_intensity=0.3, tools=None, glass=None, cad_width=None, tree_width=None, height=None, control='trackball', up='Z', ortho=True, axes=False, axes0=False, grid=None, ticks=10, transparent=False, black_edges=False, collapse=0, position=None, quaternion=None, target=None, zoom=None, reset_camera=True, zoom_speed=1.0, pan_speed=1.0, rotate_speed=1.0, timeit=False, js_debug=False)-
Adding shapes to the CAD view
Parameters
shapes:dict- Nested tessellated shapes
states:dict- State of the nested cad objects, key = object path, value = 2-dim tuple of 0/1 (hidden/visible) for object and edges
tracks:listortuple, defaultNone- List of animation track arrays, see AnimationTrack.to_array
title:str, default: None- Name of the title view to display the shapes.
ortho:bool, defaultTrue- Whether to use orthographic view (True) or perspective view (False)
cad_width:int, default: None- Width of the canvas element
height:int, default: None- Height of the canvas element
tree_width:int, default: None- Width of the navigation tree element
tools:bool, default: None- Whether to show CAD tools (True) or not (False)
glass:bool, default: None- Whether to use glass mode (True) or not (False)
control:string, default'trackball'- Whether to use trackball controls ('trackball') or orbit controls ('orbit')
up:string, default'Z'- Whether camera up direction is Z ('Z') or Y ('Y') or the lagacy Z orientation ('L')
axes:bool, defaultFalse- Whether to show coordinate axes (True) or not (False)
axes0:bool, defaultFalse- Whether to center coordinate axes at the origin [0,0,0] (True) or at the CAD object center (False)
grid:3-dim listofbool, defaultNone- Whether to show the grids for
xy,xz,yz(Nonemeans(False, False, False)) ticks:int, default10- Hint for the number of ticks for the grids (will be adjusted for nice intervals)
transparent:bool, defaultFalse- Whether to show the CAD objects transparently (True) or not (False)
black_edges:bool, defaultFalse- Whether to shows the edges in black (True) or not(False)
collapse:int, default0- Collapse CAD tree (1: collapse nodes with single leaf, 2: collapse all nodes)
normal_Len:int, default0- If > 0, the vertex normals will be rendered with the length given be this parameter
default_edge_color:string, default"#707070"- The default edge color in web format, e.g. '#ffaa88'
default_opacity:float, default0.5- The default opacity level for transparency between 0.0 an 1.0
ambient_intensity:float, default0.9- The intensity of the ambient light
direct_intensity:float, default0.12- The intensity of the 8 direct lights
position:3-dim listoffloat, defaultNone- Position of the camera as a 3-dim tuple of float (x,y,z)
quaternion:4-dim listoffloat, defaultNone- Rotation of the camera as 4-dim quaternion (x,y,z,w)
target:3-dim listoffloat, defaultNone- Camera target to look at, default is the center of the object's bounding box
zoom:float, defaultNone- Zoom value of the camera
reset_camera:bool, defaultTrue- Keep the camera position and rotation when showing new shapes (True) or not (False)
zoom_speed:float, default1.0- Speed of zooming with the mouse
pan_speed:float, default1.0- Speed of panning with the mouse
rotate_speed:float, default1.0- Speed of rotation with the mouse
timeit:bool, defaultFalse- Whether to output timing info to the browser console (True) or not (False)
Examples
A simple cube with edge len of 1 is tessellated like the
shapeelement of the first (and only) element of thepartslist:shapes = { "name": "Group", "id": "/Group", "loc": None, # would be (<position>, <quaternion>), e.g. ([0,0,0), (0,0,0,1)]), "bb": { "xmin": -0.5, "xmax": 0.5, "ymin": -0.5, "ymax": 0.5, "zmin": -0.5, "zmax": 0.5 } "parts": [{ "name": "Part_0", "id": "/Group/Part_0", "type": "shapes", "shape": {"vertices": [ [-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, -0.5], [-0.5, 0.5, 0.5], [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5], [-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [-0.5, 0.5, -0.5], [0.5, 0.5, -0.5], [-0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, -0.5, -0.5], [-0.5, 0.5, -0.5], [0.5, -0.5, -0.5], [0.5, 0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5], [0.5, -0.5, 0.5], [0.5, 0.5, 0.5]], "triangles": [ 1, 2, 0, 1, 3, 2, 5, 4, 6, 5, 6, 7, 11, 8, 9, 11, 10, 8, 15, 13, 12, 15, 12, 14, 19, 16, 17, 19, 18, 16, 23, 21, 20, 23, 20, 22 ], "normals": [ [-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, -1, 0], [0, -1, 0], [0, -1, 0], [0, -1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 0, -1], [0, 0, -1], [0, 0, -1], [0, 0, -1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1] ], "edges": [ [[-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5]], [[-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5]], [[-0.5, 0.5, -0.5], [-0.5, 0.5, 0.5]], [[-0.5, -0.5, -0.5], [-0.5, 0.5, -0.5]], [[0.5, -0.5, -0.5], [0.5, -0.5, 0.5]], [[0.5, -0.5, 0.5], [0.5, 0.5, 0.5]], [[0.5, 0.5, -0.5], [0.5, 0.5, 0.5]], [[0.5, -0.5, -0.5], [0.5, 0.5, -0.5]], [[-0.5, -0.5, -0.5], [0.5, -0.5, -0.5]], [[-0.5, -0.5, 0.5], [0.5, -0.5, 0.5]], [[-0.5, 0.5, -0.5], [0.5, 0.5, -0.5]], [[-0.5, 0.5, 0.5], [0.5, 0.5, 0.5]] ]}, "color": "#e8b024", "renderback": false }] } states = {'/Group/Part_0': [1, 1]}A nested object (with shapes shortened) looks like:
{ 'id': '/bottom', 'name': 'bottom', 'loc': ['<position>', '<quaternion>'], 'parts': [{ 'id': '/bottom/bottom_0', 'name': 'bottom_0', 'type': 'shapes', 'color': '#bfbfbf', 'shape': {'vertices': [...], 'triangles': [...], 'normals': [...], 'edges': [...]}, }, { 'id': '/bottom/top', 'name': 'top', 'loc': ['<position>', '<quaternion>'], 'parts': [{ 'id': '/bottom/top/top_0', 'name': 'top_0', 'type': 'shapes', 'color': '#bfbfbf', 'shape': {'vertices': [...], 'triangles': [...], 'normals': [...], 'edges': [...]}, }] }, { 'id': '/bottom/front_stand', 'name': 'front_stand', 'loc': ['<position>', '<quaternion>'], 'parts': [{ 'id': '/bottom/front_stand/front_stand_0', 'name': 'front_stand_0', 'type': 'shapes', 'color': '#7fcce5', 'shape': {'vertices': [...], 'triangles': [...], 'normals': [...], 'edges': [...]}, }] }, { 'id': '/bottom/back_stand', 'name': 'back_stand', 'loc': ['<position>', '<quaternion>'], 'parts': [{ 'id': '/bottom/back_stand/back_stand_0', 'name': 'back_stand_0', 'type': 'shapes', 'color': '#7fcce5', 'shape': {'vertices': [...], 'triangles': [...], 'normals': [...], 'edges': [...]}, }] }, { 'id': '/bottom/right_back', 'name': 'right_back', 'loc': ['<position>', '<quaternion>'], 'parts': [{ 'id': '/bottom/right_back/right_back_0', 'name': 'right_back_0', 'type': 'shapes', 'color': '#ffa500', 'shape': {'vertices': [...], 'triangles': [...], 'normals': [...], 'edges': [...]}, }, { 'id': '/bottom/right_back/lower', 'name': 'lower', 'loc': ['<position>', '<quaternion>'], 'parts': [{ 'id': '/bottom/right_back/lower/lower_0', 'name': 'lower_0', 'type': 'shapes', 'color': '#ffa500', 'shape': {'vertices': [...], 'triangles': [...], 'normals': [...], 'edges': [...]}, }] }] }, ... ] } states = { '/bottom/bottom_0': [1, 1], '/bottom/top/top_0': [1, 1], '/bottom/front_stand/front_stand_0': [1, 1], '/bottom/back_stand/back_stand_0': [1, 1], '/bottom/right_back/right_back_0': [1, 1], '/bottom/right_back/lower/lower_0': [1, 1], ... }Notes
Vector : float[3] := [x, y, z] VectorList : Vector[n] := [ [x0, y0, z0], [x1, xy1, z1], … ] Index : int[m] := [ i0, i1, i2, … ] Edge : Vector[2] := [ [x0, y0, z0], [x1, xy1, z1]] EdgeList : Edge[k] := [ [[x0, y0, z0], [x1, xy1, z1]], [[x2, y2, z2], [x3, xy3, z3]], … ]
Shape, Faces := { "id": "
", "name": " ", "type": "shapes", "color": "#ffffff", "renderback": false "shape": { "vertices": , "triangles": , "normals": , "edges": } } Edges := { "id": ">", "name": "
", "type": "edges", "color": "#ffffff", "width": 3, "renderback": false "shape": } Vertices := { "id": ">", "name": "
", "type": "vertices", "color": "#ffffff", "size": 6 "shape": } def add_track(self, track)-
Add an animation track to CAD view
Parameters
track:AnimationTrack- Animation track, see AnimationTrack
def add_tracks(self, tracks)-
Add a list of animation tracks to CAD view
Parameters
tracks:listofAnimationTrack- List of Animation tracks, see AnimationTrack
def animate(self, speed=1)-
Send animation tracks to CAD view
Parameters
speed:float, default: 1- Animation speed, will be forwarded via
animation_speedtraitlet
def clear_tracks(self)-
Remove animation tracks from CAD view
def close(self)-
Close the underlying Javascript viewer
def dispose(self)-
Dispose the CAD Viewer
def dump_model(self, shapes=False)def execute(self, method, args=None)-
Execute a method of a Javascript object
Parameters
method:string- A 'CadViewer' object based Javascrip object path, e.g.
abc.def[3].method(args)whereabc.def[3]is the object notation relative to the 'CadViewer' object andmethodis the method to call args:listofany- The arguments passed to
abc.def[3].method(args)
def export_html(self, filename='cadquery.html', title='CadQuery')def export_png(self, filename)-
Save CAD View as PNG
def pause(self)-
Pause or unpause animation
def pin_as_png(self)-
Pin CAD View as PNG
def play(self)-
Start or unpause animation
def remove_ui_elements(self, elements)def rotate_left(self, angle)-
Rotate CAD obj to the left (positive angle) and right (negative angle) - orbit controls only
Parameters
angle:float- The rotation angle in degrees
def rotate_up(self, angle)-
Rotate CAD obj up (positive angle) and down (negative angle) - orbit controls only
Parameters
angle:float- The rotation angle in degrees
def rotate_x(self, angle)-
Rotate CAD obj around x-axis - trackball controls only
Parameters
angle:float- The rotation angle in degrees
def rotate_y(self, angle)-
Rotate CAD obj around y-axis - trackball controls only
Parameters
angle:float- The rotation angle in degrees
def rotate_z(self, angle)-
Rotate CAD obj around z-axis - trackball controls only
Parameters
angle:float- The rotation angle in degrees
def select_clipping(self)-
Select Clipping tab
def select_tree(self)-
Select Navigation tree tab
def set_camera(self, direction)-
Set camera to one of the predefined locations
Parameters
direction:string- one of ["iso", "top", "bottom", "left", "right", "front", "rear"]
def stop(self)-
Stop animation
def update_camera_location(self)-
Sync position, quaternion and zoom of camera to Python
def update_states(self, states)-
Set navigation tree states for a CAD view
class CadViewerWidget (**kwargs)-
The CAD Viewer widget.
Public constructor
Ancestors
- ipywidgets.widgets.widget_output.Output
- ipywidgets.widgets.domwidget.DOMWidget
- ipywidgets.widgets.widget.Widget
- ipywidgets.widgets.widget.LoggingHasTraits
- traitlets.traitlets.HasTraits
- traitlets.traitlets.HasDescriptors
Instance variables
var ambient_intensity-
float: The intensity of the ambient light
var anchor-
unicode string whether to add a view to the right sidebar ('right') or as a tab to the main window ('tab')
var animation_speed-
float: Animation speed
var axes-
bool: Whether to show coordinate axes (True) or not (False)
var axes0-
bool: Whether to center coordinate axes at the origin [0,0,0] (True) or at the CAD object center (False)
var black_edges-
bool: Whether to shows the edges in black (True) or not(False)
var cad_width-
unicode string: Width of the canvas element
var clip_intersection-
bool: Whether to use intersection clipping (True) or not (False)
var clip_normal_0-
tuple: Normal of clipping plane 1 as a 3-dim tuple of float (x,y,z)
var clip_normal_1-
tuple: Normal of clipping plane 2 as a 3-dim tuple of float (x,y,z)
var clip_normal_2-
tuple: Normal of clipping plane 3 as a 3-dim tuple of float (x,y,z)
var clip_planes-
bool: Whether to show colored clipping planes (True) or not (False)
var clip_slider_0-
float: Slider value of clipping plane 1
var clip_slider_1-
float: Slider value of clipping plane 2
var clip_slider_2-
float: Slider value of clipping plane 3
var collapse-
int: Collapse CAD tree (1: collapse nodes with single leaf, 2: collapse all nodes)
var control-
unicode: Whether to use trackball controls ('trackball') or orbit controls ('orbit')
var default_edge_color-
unicode: The default edge color in web format, e.g. '#ffaa88'
var default_opacity-
unicode: The default opacity for transparent objects
var direct_intensity-
float: The intensity of the 8 direct lights
var disposed-
unicode string: Whether the Javascript viewer is disposed
var glass-
bool: Whether to use the glass mode (CAD navigation as transparent overlay) or not
var grid-
tuple: Whether to show the grids for
xy,xz,yz. var height-
int: Height of the canvas element
var image_id-
unicode string: the id of the image tag to use for pin as png
var initialize-
bool: internally used to control initialization of view. Do not use!
var js_debug-
bool: Whether to show infos in the browser console (True) or not (False)
var lastPick-
dict: Describes the last picked element of the CAD view
var normal_len-
float: If > 0, the vertex normals will be rendered with the length given be this parameter
var ortho-
bool: Whether to use orthographic view (True) or perspective view (False)
var pan_speed-
float: Speed of panning with the mouse
var pinning-
bool: Whether to show the pin a png button or not
var position-
tuple: Position of the camera as a 3-dim tuple of float (x,y,z)
var position0-
tuple: Initial position of the camera as a 3-dim tuple of float (x,y,z)
var quaternion-
tuple: Rotation of the camera as 4-dim quaternion (x,y,z,w)
var quaternion0-
tuple: Initial rotation of the camera as 4-dim quaternion (x,y,z,w)
var reset_camera-
bool: Whether to reset camera (True) or not (False)
var result-
unicode string: JSON serialized result from Javascript
var rotate_speed-
float: Speed of rotation with the mouse
var shapes-
unicode: Serialized nested tessellated shapes
var state_updates-
dict: Dict with paths as key and a 2-dim tuple of 0/1 (hidden/visible) for object and edges
var states-
dict: State of the nested cad objects, key = object path, value = 2-dim tuple of 0/1 (hidden/visible) for object and edges
var tab-
unicode: Whether to show the navigation tree ('tree') or the clipping UI ('clip')
var target-
tuple: Camera target to look at as 3-dim tuple (x,y,z)
var target0-
tuple: Initial camera target to look at as 3-dim tuple (x,y,z)
var theme-
unicode string: UI theme, can be 'dark' or 'light' (default)
var ticks-
integer: Hint for the number of ticks for the grids (will be adjusted for nice intervals)
var timeit-
bool: Whether to output timing info to the browser console (True) or not (False)
var title-
unicode string of the title of the sidecar to be used. None means CAD view will be opened in cell
var tools-
bool: Whether to show CAD tools (True) or not (False)
var tracks-
unicode: Serialized list of animation track arrays, see AnimationTrack.to_array
var transparent-
bool: Whether to show the CAD objects transparently (True) or not (False)
var tree_width-
int: Width of the navigation tree element
var up-
unicode: Whether camera up direction is Z ('Z') or Y ('Y') or the legacy Z orientation ('L')
var zoom-
float: Zoom value of the camera
var zoom0-
float: Initial zoom value of the camera
var zoom_speed-
float: Speed of zooming with the mouse
Methods
def func(...)