bernhard-42 / vscode-ocp-cad-viewer

A viewer for OCP based Code-CAD (CadQuery, build123d) integrated into VS Code
Apache License 2.0
118 stars 11 forks source link

OCP CAD Viewer for VS Code

OCP CAD Viewer for VS Code is an extension to show CadQuery and build123d objects in VS Code via the three-cad-viewer viewer component.

Installation

Prerequisites

Notes:

Installation

  1. Open the VS Code Marketplace, and search and install OCP CAD Viewer 2.6.1.

    Afterwards the OCP viewer is available in the VS Code sidebar:

  2. Clicking on it shows the OCP CAD Viewer UI with the viewer manager and the library manager:

    You have 3 options:

    • Prepare OCP CAD Viewer for working with build123d: Presse the Quickstart build123d button.

      This will install OCP, build123d, ipykernel (_jupyterclient), _ocptessellate and _ocpvscode via pip (except for Apple Silicon machines that require mamba and will also install cadquery)

    • Prepare OCP CAD Viewer for working with CadQuery: Presse the Quickstart CadQuery button.

      This will install OCP, CadQuery, ipykernel (_jupyterclient), _ocptessellate and _ocpvscode via pip (except for Apple Silicon machines that require mamba)

    • Ignore the quick starts and use the "Library Manager" to install the libraries. Doing so, OCP CAD Viewer let's you select whether to use pip, mamba, conda or poetry. Install the needed library by pressing the down-arrow behind the library name (hover over the library name to see the button) in the "Library Manager" section of the OCP CAD Viewer sidebar. For more details, see here

    The Quickstarts will also

  3. On Silicon Macs (ARM CPU)

    • Install Miniforge
      • Option 1: Download and install from the Miniforge github page
      • Option 2: Use homebrew: brew install miniforge
    • Initialize your shell for mamba: mamba init $(basename "$SHELL")
    • Create an environment, e.g named code_cad with Python 3.10: mamba create -n code_cad python=3.10
    • Activate the environment: mamba activate code_cad
    • Start VS Code from your working folder: code .
    • Install the Python extension in VS Code
    • Continue with 1. above

Notes:

Usage

Running code using Jupyter

Running code

Debugging code with visual debugging

After each step, the debugger checks all variables in locals() for being CAD objects and displays them with their variable name. Note:

Debugging code

Library Manager

You can also use "Library Manager" in the OCP CAD Viewer sidebar to manage the Python libraries for build123d, cadquery, ipython and _ocptessellate (Presse the down-arrow when hovering over a library name to install/upgrade it)

Extra topics

Standalone mode

Standalone mode allows to use OCP CAD Viewer without VS Code: python -m ocp_vscode. This will start a Flask server and the viewer can be reached under http://127.0.0.1/viewer. All client side feature of the VS Code variant (i.e. show* features) should be available (including measurement mode) except visual debugging (see above) which relies on VS Code.

Use python -m ocp_vscode --help to understand the command line args:

Usage: python -m ocp_vscode [OPTIONS]

Options:
  --create_configfile            Create the configlie .ocpvscode_standalone in
                                 the home directory
  --host TEXT                    The host to start OCP CAD with
  --port INTEGER                 The port to start OCP CAD with
  --debug                        Show debugging information
  --timeit                       Show timing information
  --tree_width TEXT              OCP CAD Viewer navigation tree width
                                 (default: 240)
  --no_glass                     Do not use glass mode with transparent
                                 navigation tree
  --theme TEXT                   Use theme 'light' or 'dark' (default:
                                 'light')
  --no_tools                     Do not show toolbar
  --tree_width INTEGER           Width of the CAD navigation tree (default:
                                 240)
  --control TEXT                 Use control mode 'orbit'or 'trackball'
  --up TEXT                      Provides up direction, 'Z', 'Y' or 'L'
                                 (legacy) (default: Z)
  --rotate_speed INTEGER         Rotation speed (default: 1)
  --zoom_speed INTEGER           Zoom speed (default: 1)
  --pan_speed INTEGER            Pan speed (default: 1)
  --axes                         Show axes
  --axes0                        Show axes at the origin (0, 0, 0)
  --black_edges                  Show edges in black
  --grid_xy                      Show grid on XY plane
  --grid_yz                      Show grid on YZ plane
  --grid_xz                      Show grid on XZ plane
  --center_grid                  Show grid planes crossing at center of object
                                 or global origin(default: False)
  --collapse INTEGER             leaves: collapse all leaf nodes, all:
                                 collapse all nodes, none: expand all nodes,
                                 root: expand root only (default: leaves)
  --perspective                  Use perspective camera
  --ticks INTEGER                Default number of ticks (default: 10)
  --transparent                  Show objects transparent
  --default_opacity FLOAT        Default opacity for transparent objects
                                 (default: 0.5)
  --explode                      Turn explode mode on
  --angular_tolerance FLOAT      Angular tolerance for tessellation algorithm
                                 (default: 0.2)
  --deviation FLOAT              Deviation of for tessellation algorithm
                                 (default: 0.1)
  --default_color TEXT           Default shape color, CSS3 color names are
                                 allowed (default: #e8b024)
  --default_edgecolor TEXT       Default color of the edges of shapes, CSS3
                                 color names are allowed (default: #707070)
  --default_thickedgecolor TEXT  Default color of lines, CSS3 color names are
                                 allowed (default: MediumOrchid)
  --default_facecolor TEXT       Default color of faces, CSS3 color names are
                                 allowed (default: Violet)
  --default_vertexcolor TEXT     Default color of vertices, CSS3 color names
                                 are allowed (default: MediumOrchid)
  --ambient_intensity INTEGER    Intensity of ambient light (default: 1.00)
  --direct_intensity FLOAT       Intensity of direct light (default: 1.10)
  --metalness FLOAT              Metalness property of material (default:
                                 0.30)
  --roughness FLOAT              Roughness property of material (default:
                                 0.65)
  --help                         Show this message and exit.

Best practices

Development

Testing:

Native tessellator can be set via NATIVE_TESSELLATOR=1and Python tessellator via NATIVE_TESSELLATOR=0.

When OCP_VSCODE_PYTEST=1 is set, show will not send the tessellated results to the viewer, but return it to the caller for inspection.

A full test cycle consist of:

NATIVE_TESSELLATOR=0 OCP_VSCODE_PYTEST=1 pytest -v -s pytests/
NATIVE_TESSELLATOR=1 OCP_VSCODE_PYTEST=1 pytest -v -s pytests/

Troubleshooting

Changes

v2.6.1

Fixes:

v2.6.0

New features

Fixes: