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.
python
and pip
available in the Python enviroment that will be used for CAD developmentgit
needs to be availablemamba
needs to be available. You might want to consider using Mambaforge.Notes:
mamba
, see 3. below.Open the VS Code Marketplace, and search and install OCP CAD Viewer 2.4.1.
Afterwards the OCP viewer is available in the VS Code sidebar:
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
On Silicon Macs (ARM CPU)
homebrew
: brew install miniforge
mamba
: mamba init $(basename "$SHELL")
code_cad
with Python 3.10: mamba create -n code_cad python=3.10
mamba activate code_cad
code .
Note: Do not use the OCP CAD Viewer logo to verify your OCP CAD Viewer settings! The logo overwrites all your settings in VS Code with its own settings to always look the same on each instance. Use a simple own model for checking your conmfiguration
ocp_vscode
entry to see the button).After each step, the debugger checks all variables in locals()
for being CAD objects and displays them with their variable name.
Note:
OCP:on
is visible in the status barshow
and show_object
are disabled. They interfere with the visual debuggingYou 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)
show
commandshow_object
commandset_viewer_config
commandUse the Jupyter extension for a more interactive experience. This allows to have one cell (separated by # %%
) at the beginning to import all libraries
# %%
from build123d import *
from ocp_vscode import *
# %%
b = Box(1,2,3)
show(b)
# %%
and then only execute the code in the cell you are currently working on repeatedly.
The config system of OCP CAD Viewer
There are 3 levels:
set_defaults
per Python fileshow
or show_object
per commandset_defaults
overrides the Workspace settings and parameters in show
and show_config
override the other two.
Note that not all parameters are available in the global Workspace config, since they don't make sense globally (e.g. helper_scale
which depends on the size of the boundary box of the currently shown object)
A common setup would be
# %%
from build123d import *
import cadquery as cq
from ocp_vscode import *
set_port(3939)
set_defaults(reset_camera=False, helper_scale=5)
# %%
...
Explanation
set_port
is only needed when you have more than one viewer open and can be omitted for the first viewer)Debugging build123d with show_all
and the visual debugger
If you name your contexts (including Location
contexts), the visual debugger will show the CAD objects assigned to the context.
Use show_all
to show all cad objects in the current scope (locals()
) of the Python interpreter (btw. the visual debugger uses show_all
at each step)
# %%
from build123d import *
set_defaults(helper_scale=1, transparent=True)
with BuildPart() as bp:
with PolarLocations(3,8) as locs:
Box(1,1,1)
show_all()
# %%
Keep camera orientation of an object with reset_camera
Sometimes it is helpful to keep the orientation of an object across code changes. This is what reset_camera
does:
reset_camera=Camera.Center
will keep position and rotation, but ignore panning. This means the new object will be repositioned to the center (most robust approach)reset_camera=Camera.KEEP
will keep position, rotation and panning. However, panning can be problematic. When the next object to be shown is much larger or smaller and the object before was panned, it can happen that nothing is visible (the new object at the pan location is outside of the viewer frustum). OCP CAD Viewer checks whether the bounding box of an object is 2x smaller or larger than the one of the last shown object. If so, it falls back to Camera.CENTER
. A notification is written to the OCP CAD Viewer output panel.reset_camera=Camera.RESET
will ensure that position, rotation and panning will be reset to the initial defaultTesting:
Native tessellator can be set via NATIVE_TESSELLATOR=1
and 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/
CAD Models almost always are invisible in the OCP viewer window
three-cad-viewer.esm.js:20276 THREE.WebGLProgram: Shader Error 0 - VALIDATE_STATUS false
Material Name:
Material Type: LineBasicMaterial
Program Info Log: Program binary could not be loaded. Binary is not compatible with current driver/hardware combination. Driver build date Mar 19 2024. Please check build information of source that generated the binary.
Location of variable pc_fragColor conflicts with another variable.
VS Code internal browser that renders the viewer component uses a cache for code and other artifacts. This includes WebGL artefacts like compiled shaders. It can happen that e.g. due to a graphic driver update the compiled version in the cache does not fit to the new driver. Then this error message appears.
Solution: Delete the VS Code browser cache on Linux (go to the section for your operating system)
v2.4.1
v2.4.0
New features
measure_tools
parameter and integrated switching between normal and measure mode into the three-cad-viewer (#58).center_grid
to workspace configuration to ensure grid planes always go through world origin (#77).save_screenshot(filename)
to save a PNG of the currently shown object(s). Canvas only, no CAD tools (#86).LocationList
s.Fixes
Location
class used by measurement backend.to_tuple
for build123d Color objects (#69).color
or alpha
was ignored).show_all
failing on empty ShapeList (#90).full change log see CHANGELOG.md