An integrated OCP CAD viewer for CadQuery and build123d with visual debugging capabilities
pkg:vscode-extension/ocp-cad-viewer@1.2.x
npx @tessl/cli install tessl/vscode-extension-ocp-cad-viewer@1.2.0OCP CAD Viewer is an integrated 3D CAD viewer extension for Visual Studio Code that provides seamless visualization and debugging capabilities for OpenCASCADE (OCP) based projects. It specifically supports CadQuery and build123d libraries, enabling developers to visualize 3D CAD models directly within their development environment with interactive debugging features and comprehensive library management.
code --install-extension bernhard-42.ocp-cad-viewerpip install ocp_vscodePython Library (ocp_vscode):
from ocp_vscode import show, show_object, reset_show, set_port, set_defaultsCommon display functions:
from ocp_vscode import show, show_object, show_all, show_clearConfiguration and debugging:
from ocp_vscode import set_defaults, get_defaults, Camera, CollapseColor management:
from ocp_vscode import ColorMap, set_colormap, get_colormapVS Code Extension Commands:
Python Display:
import cadquery as cq
from ocp_vscode import show
# Create a simple CAD object
box = cq.Workplane().box(10, 10, 10)
# Display in VS Code viewer
show(box)Multiple Objects with Styling:
from ocp_vscode import show, ColorMap
import cadquery as cq
# Create multiple objects
box = cq.Workplane().box(10, 10, 10)
cylinder = cq.Workplane().cylinder(5, 20).translate((15, 0, 0))
sphere = cq.Workplane().sphere(8).translate((0, 15, 0))
# Display with custom colors and names
show(
box, cylinder, sphere,
names=["Box", "Cylinder", "Sphere"],
colors=ColorMap.tab10(),
axes=True,
transparent=True
)Interactive Development:
from ocp_vscode import show_object, reset_show
# Clear previous objects and add incrementally
reset_show()
show_object(part1, name="Base", clear=True)
show_object(part2, name="Feature", options={"color": (255, 0, 0), "alpha": 0.7})The OCP CAD Viewer consists of two integrated components:
Core functions for displaying CAD objects in the VS Code viewer with extensive styling and configuration options.
def show(*cad_objs, names=None, colors=None, alphas=None, **kwargs): ...
def show_object(obj, name=None, options=None, parent=None, clear=False, **kwargs): ...
def reset_show(): ...
def show_clear(): ...
def show_all(variables=None, exclude=None, **kwargs): ...Commands and integrations provided by the VS Code extension for managing the viewer, libraries, and development workflow.
// Command IDs accessible via vscode.commands.executeCommand()
"ocpCadViewer.ocpCadViewer" // Open viewer
"ocpCadViewer.toggleWatch" // Toggle visual debugging
"ocpCadViewer.installLibrary" // Install CAD library
"ocpCadViewer.quickstart" // Quick installation
"ocpCadViewer.preferences" // Open settingsFunctions for managing viewer settings, defaults, and runtime configuration with VS Code settings integration.
def set_defaults(**kwargs): ...
def get_defaults(): ...
def get_default(key): ...
def reset_defaults(): ...
def set_viewer_config(**kwargs): ...
def workspace_config(port=None): ...
def combined_config(port=None, use_status=True): ...
def status(port=None, debug=False): ...Color management system with built-in colormaps, custom color generation, and styling utilities for enhanced visualization.
class ColorMap:
@staticmethod
def tab10(alpha=1.0, reverse=False): ...
@staticmethod
def set1(alpha=1.0, reverse=False): ...
@staticmethod
def golden_ratio(colormap="hsv", alpha=1.0, reverse=False): ...
def set_colormap(colormap): ...
def get_colormap(): ...
def unset_colormap(): ...
def web_to_rgb(name): ...WebSocket-based communication functions for connecting the Python library to the VS Code extension viewer.
def set_port(port): ...
def get_port(): ...
def send_data(data, port=None, timeit=False): ...
def send_command(data, port=None, timeit=False): ...
def listener(callback): ...Animation framework for creating animated sequences of CAD objects with timeline-based keyframe animation.
class Animation:
def __init__(self, assembly): ...
def add_track(self, path, action, times, values): ...
def animate(self, speed): ...class Camera(Enum):
RESET = "reset" # Reset camera position, rotation, zoom and target
CENTER = "center" # Keep camera settings, look at center
KEEP = "keep" # Keep all camera settings
class Collapse(Enum):
NONE = 0 # Expand all nodes
LEAVES = 1 # Collapse leaf nodes
ALL = 2 # Collapse all nodes
ROOT = 3 # Expand root only
class MessageType(IntEnum):
data = 1 # Data message
command = 2 # Command message
updates = 3 # Update message
listen = 4 # Listen message# Display configuration parameters (used in show(), show_object(), set_defaults())
DisplayConfig = {
# UI Settings
"glass": bool, # Glass mode overlay (default: False)
"tools": bool, # Show tools (default: True)
"tree_width": int, # Object tree width (default: 240)
# Viewer Settings
"axes": bool, # Show axes (default: False)
"axes0": bool, # Show axes at origin (default: False)
"grid": bool, # Show grid (default: False)
"ortho": bool, # Orthographic projection (default: True)
"transparent": bool, # Transparent objects (default: False)
"default_opacity": float, # Opacity for transparent objects (default: 0.5)
"black_edges": bool, # Black edge color (default: False)
"orbit_control": bool, # Orbit vs trackball control (default: False)
"collapse": Collapse, # Tree collapse mode
"explode": bool, # Explode mode (default: False)
"ticks": int, # Grid ticks hint (default: 10)
"up": str, # Up direction 'Z' or 'Y' (default: "Z")
# Camera Settings
"zoom": float, # Zoom factor (default: 1.0)
"position": tuple, # Camera position (x, y, z)
"quaternion": tuple, # Camera orientation quaternion (x, y, z, w)
"target": tuple, # Camera look-at target (x, y, z)
"reset_camera": Camera, # Camera reset mode
"pan_speed": float, # Pan speed (default: 1)
"rotate_speed": float, # Rotation speed (default: 1)
"zoom_speed": float, # Zoom speed (default: 1)
# Rendering Settings
"deviation": float, # Tessellation deviation (default: 0.1)
"angular_tolerance": float, # Angular tolerance (default: 0.2)
"edge_accuracy": float, # Edge discretization precision
"default_color": str, # Default mesh color
"default_edgecolor": str, # Default edge color
"default_facecolor": str, # Default face color
"default_thickedgecolor": str, # Default thick edge color
"default_vertexcolor": str, # Default vertex color
"ambient_intensity": float, # Ambient light intensity (default: 1.0)
"direct_intensity": float, # Direct light intensity (default: 1.1)
"metalness": float, # Material metalness (default: 0.3)
"roughness": float, # Material roughness (default: 0.65)
"render_edges": bool, # Render edges (default: True)
"render_normals": bool, # Render normals (default: False)
"render_mates": bool, # Render mates for assemblies (default: False)
"render_joints": bool, # Render build123d joints (default: False)
"parallel": bool, # Parallel tessellation (default: False)
"show_parent": bool, # Show parent wireframe (default: False)
"helper_scale": float, # Helper scale factor (default: 1)
# Debug Settings
"debug": bool, # Show debug info (default: False)
"timeit": bool, # Show timing info (default: False)
}# Animation action types for add_track()
AnimationAction = Literal[
"t", # Position vector (3-dim array)
"tx", # Translation along x-axis
"ty", # Translation along y-axis
"tz", # Translation along z-axis
"q", # Quaternion rotation (x,y,z,w)
"rx", # Rotation around x-axis (degrees)
"ry", # Rotation around y-axis (degrees)
"rz", # Rotation around z-axis (degrees)
]