Metadata-Version: 2.4
Name: ocp-viewer-core
Version: 1.3.0
Summary: Shared viewer core for ocp_vscode, ocp_viewer, Jupyter CadQuery and build123d Studio
Author-email: Bernhard Walter <b_walter@arcor.de>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/bernhard-42/ocp-viewer-core
Project-URL: Bug Tracker, https://github.com/bernhard-42/ocp-viewer-core/issues
Keywords: 3d models,3d viewing,3d,brep,cad,cadquery,opencascade,python
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: ocp-tessellate<3.8.0,>=3.7.0
Requires-Dist: orjson
Requires-Dist: pillow
Requires-Dist: threejs-materials<1.3.0,>=1.2.0
Requires-Dist: websockets>=16.0
Provides-Extra: cli
Requires-Dist: questionary<3.0,>=2.1; extra == "cli"
Provides-Extra: mcp
Requires-Dist: mcp<3,>=2.3; extra == "mcp"
Provides-Extra: dev
Requires-Dist: build; extra == "dev"
Requires-Dist: bump-my-version; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

# ocp-viewer-core

The shared half of the OCP viewer ecosystem: one show suite, one tessellation, one set of config semantics, used by four viewers that each keep their own transport and their own settings storage.

Currently supported hosts (viewers):

- [ocp_vscode](https://github.com/bernhard-42/vscode-ocp-cad-viewer) — the VS Code extension
- [ocp_viewer](https://github.com/bernhard-42/ocp_viewer) — the standalone viewer, `python -m ocp_viewer`
- [Jupyter CadQuery](https://github.com/bernhard-42/jupyter-cadquery) — The viewer for Jupyter Lab through [cad-viewer-widget](https://github.com/bernhard-42/cad-viewer-widget)
- [build123d Studio](https://github.com/bernhard-42/build123d-studio) — The standalone app based on [neutralinojs](https://neutralino.js.org/)

![viewer ecosystem](./docs/assets/viewer-ecosystem.png)

The user-facing documentation of the shared behavior — the show commands, the config system, the viewer window and its tools — lives in [docs/](./docs/index.md). Each host documents only its own specifics and refers there for the rest.

Published as one project to two registries: `ocp-viewer-core` on PyPI for the Python half, `ocp-viewer-core` on npm for the JavaScript half. Their major.minor is the contract and must match; the patch level is each half's own, so a one-sided fix ships without an artificial release of the other half. See [Development.md](./Development.md).

## The idea

Each host provides a `Comms` — a Python encoder and a JavaScript decoder, shipped as a pair — and a settings source. Everything above that is shared and knows nothing about which host it is running in.

Per-host imports stay per host:

- `from ocp_vscode       import *` or `from ocp_vscode       import show, set_defaults, ...`
- `from build123d_studio import *` or `from build123d_studio import show, set_defaults, ...`
- `from jupyter_cadquery import *` or `from jupyter_cadquery import show, set_defaults, ...`
- `from ocp_viewer       import *` or `from ocp_viewer       import show, set_defaults, ...`

## Layout

| path                        | what                                                                                                |
| --------------------------- | --------------------------------------------------------------------------------------------------- |
| `ocp_viewer_core/`          | the Python half                                                                                     |
| `ocp_viewer_core/config.py` | the config keys, each mapped to the name three-cad-viewer knows it by, and the precedence over them |
| `ocp_viewer_core/comms.py`  | the transport a host implements, and the session that caches what it answers                        |
| `ocp_viewer_core/logo.py`   | the splash logo as measurable geometry, for a host's measurement backend                            |
| `ocp_viewer_core/plugins.py` | the kernel plugins of the measurement backend: the registry, their entry point, and what their bodies travel as |
| `ocp_viewer_core/mcp/`      | the MCP server for agents, an add-on nothing else imports (see [Agents](#agents-the-mcp-server))   |
| `js/src/logo.js`            | the splash logo as tessellated data plus its config, for the renderer                               |
| `js/`                       | the JavaScript half, published to npm                                                               |
| `tests/`                    | including the conformance kit                                                                       |

`ocp_viewer_core/__init__.py` is import-free by design: hosts import the submodule they need, so that importing the package never pulls in the tessellator or OCP.

## Use

A host writes one class and supplies one list. `exclude_keys` names the keywords it may not be told, because its surface decides them - the show signature is the superset of every host's, so a keyword one host owns is a keyword another has to refuse by name rather than ignore.

```python
comms = MyComms()
session = Session(comms)                        # sets session cache to None
config = Config(session, ("cad_width", "height"))
show(objects)
```

What a host _persists_ between sessions is its own business and needs no key list here: it answers with values, from `Comms.workspace_config()`. Which of the viewer's reported state counts as configuration - and so survives into the next show - is `keys.CONFIG`, derived from the vocabulary, because that set is a property of three-cad-viewer's state and not of any host's settings.

## Agents: the MCP server

An agent CLI that runs MCP servers over stdio, Claude Code for example, can work with the user through the viewer the user is looking at, OCP CAD Viewer for VS Code or the standalone OCP Viewer, over an MCP server in this package. Jupyter CadQuery and build123d Studio are not covered yet.

Install it with the `mcp` extra into the Python environment the user's scripts run in. It is part of ocp-viewer-core 1.3, so that environment needs a viewer release that requires 1.3; each viewer requires the core's major.minor it was built with.

```bash
pip install 'ocp-viewer-core[mcp]'
```

Register it once with the agent, giving that environment's Python, e.g. for Claude Code:

```bash
claude mcp add --scope user ocp-viewer -- /path/to/python -m ocp_viewer_core.mcp
```

Other agent CLIs register a stdio server the same way: the command is `<python> -m ocp_viewer_core.mcp`. The server finds the open viewer for every call (`OCP_PORT`, the viewer registry `~/.ocpvscode`, then 3939); with several viewers open it says so and the agent asks which one. `--port N` pins one viewer.

The tools:

- `get_selection`: what the user selected (faces, edges, vertices, solids, grouped by object) and last double-clicked, with the model it belongs to (`model_seq`)
- `highlight`, `select`, `clear_selection`: mark components in the agent's own colour, set or clear the user's selection
- `get_view`, `set_view`, `restore_view`: read the user's camera with its screen directions (for "the top left edge"), change camera, visibility, clipping and the active tool, and put it all back
- `render`: an image of the user's view, or from a camera of its own (a preset, any direction, framing given components) that does not move the user's view
- `scene`, `measure`: the objects with their face and edge counts; properties and distances from the viewer's mesh (exact types, values exact for planes and lines, within the tessellation tolerance otherwise)
- `list_viewers`, and `guide`: the rules and step-by-step patterns for working with the user (act on what the user marked, let the user answer by selecting, exact measurement in the agent's own code)

In the user's code, `select_edges(part, [3])` (and `select_faces`, `select_vertices`) from the viewer package turns viewer indices into shapes, and `edge_ids(part, shapes)` (and `face_ids`, `vertex_ids`) from `ocp_viewer_core.selectors` turns shapes into viewer indices, e.g. to highlight what a selector selects.

`render` with a direction, `fit` or `zoom`, `measure`, and the face and edge counts of `scene` need a viewer built with ocp-viewer-core JavaScript 1.3.0 and three-cad-viewer 5.2.2. With an older viewer, `render` of the user's view falls back to a plain screenshot, which may show the hover highlight.

## Dependencies

- The Python package [ocp-tessellate](https://github.com/bernhard-42/ocp-tessellate) for tessellation of [OCP based CAD objects](https://github.com/cadquery/OCP)
- The Javascript package [three-cad-viewer](https://github.com/bernhard-42/three-cad-viewer), the actual viewer component.

No OCP provider is declared — the same `OCP` namespace is supplied by `cadquery_ocp`, `cadquery_ocp_novtk` and conda's `OCP`, and naming one would break users of the other two. The host or the user chooses.

## Licence

Apache-2.0.
