# How Patent-Disclosure-Skill Processes STEP/STP Files for CadQuery Extraction

> Learn how the patent-disclosure-skill processes STEP/STP files with CadQuery and OpenCASCADE to extract assembly trees and generate SVG/PNG projections for patent documentation.

- Repository: [handsomestWei/patent-disclosure-skill](https://github.com/handsomestWei/patent-disclosure-skill)
- Tags: how-to-guide
- Published: 2026-09-06

---

**The patent-disclosure-skill processes STEP/STP files through an opt-in pipeline that uses CadQuery and OpenCASCADE to extract assembly trees, render multi-view SVG projections, and convert them to PNGs for patent documentation generation.**

The `handsomestWei/patent-disclosure-skill` repository treats **STEP (`.step` / `.stp`)** files as its primary neutral 3-D exchange format for CAD data ingestion. When handling STEP/STP files for CadQuery extraction, the skill coordinates three specialized components—file classification, STEP parsing with view generation, and runtime environment management—to transform geometric models into visual assets suitable for patent disclosure drafts.

## STEP File Processing Architecture

The system delegates responsibilities across three Python modules that handle detection, parsing, and execution environment setup.

### File Classification and Detection

The [`cad_formats.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/cad_formats.py) module provides the entry point for STEP file handling. According to the source code in [`skills/patent-disclosure/tools/cad_formats.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/cad_formats.py), the functions `is_step`, `classify_path`, and `recommend_action` determine whether STEP files exist in a case directory and whether to prompt the user for parsing.

When [`cad_scan.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/cad_scan.py) scans a directory, it calls `cad_formats.iter_classified` to collect STEP files. If any are found, `recommend_action` returns `"ask_enable_step_parse"`, triggering a confirmation prompt before processing begins.

### Core Extraction Engine

The [`step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/step_to_views.py) module contains the primary logic for STEP/STP file processing. Key functions include `extract_assembly_tree` for hierarchical assembly analysis, `render_views` for generating orthogonal projections, and `build_figure_plan_seed` and `build_structure_seed` for creating metadata YAML files that describe the extracted geometry.

This module attempts to use the **OpenCASCADE XCAF API** (via the `OCP` package) as its preferred parsing path, falling back to standard CadQuery importers if XCAF is unavailable.

### Runtime Environment Management

Because CadQuery requires specific native dependencies, the skill isolates the CAD environment using [`bootstrap_cad_venv.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/bootstrap_cad_venv.py) and [`cad_venv.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/cad_venv.py). The CLI wrapper [`run_step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/run_step_to_views.py) ensures the `cad-env` virtual environment is active before invoking the extraction pipeline, aborting with installation hints if CadQuery cannot be imported.

## The STEP-to-Views Pipeline

The workflow follows a strict eight-stage process from file discovery to metadata generation.

### Discovery and User Opt-In

STEP parsing is **disabled by default** to prevent accidental processing of large CAD datasets. Users must explicitly enable extraction using either the command-line flag `--enable-step-parse` or by setting the environment variable `PATENT_SKILL_STEP_PARSE=1`. The function `step_to_views.parse_enabled` checks both conditions before proceeding.

### Assembly Tree Extraction with XCAF

The `extract_assembly_tree` function in [`step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/step_to_views.py) implements a dual-path strategy:

- **Preferred path**: Uses `OCP` (OpenCASCADE Python bindings) to read the STEP file via the XCAF API, enumerate free shapes, and build a hierarchical list of parts with proper assembly relationships.
- **Fallback path**: If XCAF is unavailable, the script imports the STEP file using `cadquery.importers.importStep` and counts solid bodies via `_fallback_solids_tree`, providing a flat solid count rather than a tree structure.

### Multi-View Rendering and Rasterization

Once the geometry is loaded, `render_views` generates visual documentation by iterating over `DEFAULT_VIEWS` (iso, front, top, right). For each view:

1. The function calls `cq.exporters.export()` with `ExportTypes.SVG` and projection parameters to create vector graphics.
2. It attempts PNG rasterization using **cairosvg** via `_svg_to_png_cairo`.
3. If Cairo is missing, the SVG remains without a PNG counterpart until the browser fallback stage.

The `_rasterize_svgs_browser` function launches [`svg_screenshot.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/svg_screenshot.py) via Playwright to convert any remaining SVGs to PNGs using a headless browser when native rasterization fails.

### Metadata Generation for Patent Drafts

After rendering, the pipeline produces two YAML seed files:

- **Figure Plan**: `build_figure_plan_seed` creates entries with `kind: "cad"` for each view, explicitly marking them as *material only* (never line-art).
- **Structure Schema**: `build_structure_seed` converts the assembly tree into a hierarchical description, flagging uncertainties and noting the parsing method used (`xcaf` or `solid_count`).

Outputs are written to the user-specified directory (e.g., `outputs/case/cad_views`).

## Code Examples

### Running the STEP-to-Views Pipeline

```bash

# Initialize the CadQuery virtual environment (one-time setup)

python tools/bootstrap_cad_venv.py

# Parse a STEP file and generate multi-view assets

python tools/run_step_to_views.py \
    --enable-step-parse \
    -i model.step \
    -o outputs/case/cad_views

```

### Programmatic API Usage

```python
from pathlib import Path
from skills.patent_disclosure.tools.step_to_views import render_views, extract_assembly_tree

step_file = Path("model.step")
out_dir = Path("outputs/cad_views")

# Generate SVG and PNG projections

views = render_views(step_file, out_dir)

# Extract hierarchical assembly description

assembly = extract_assembly_tree(step_file)

```

### Classifying Files Before Parsing

```python
from skills.patent_disclosure.tools.cad_formats import iter_classified, recommend_action

result = iter_classified(["./case"], recursive=True)
action = recommend_action(
    result["step"], 
    result["native_cad"], 
    result["iges"]
)

# Returns "ask_enable_step_parse" if STEP files are present

print(action)

```

## Summary

- **STEP/STP files** are the only neutral 3-D format the skill parses directly, as implemented in `handsomestWei/patent-disclosure-skill`.
- Parsing requires explicit opt-in via `--enable-step-parse` or the `PATENT_SKILL_STEP_PARSE=1` environment variable.
- The [`step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/step_to_views.py) module uses OpenCASCADE XCAF for assembly tree extraction, falling back to basic CadQuery solid counting if necessary.
- The pipeline generates orthogonal SVG views (iso, front, top, right) and converts them to PNGs using Cairo or a Playwright-based browser fallback.
- Output includes both visual assets and YAML metadata seeds (`figure_plan` and `structure`) for patent documentation workflows.

## Frequently Asked Questions

### What CAD formats does patent-disclosure-skill support besides STEP/STP?

According to the source code in [`cad_formats.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/cad_formats.py), the system can detect and classify various CAD formats, but **STEP is the only neutral format processed for CadQuery extraction**. Native proprietary formats are acknowledged during classification but require conversion to STEP before the pipeline can process them.

### Why is STEP parsing disabled by default in the skill?

STEP parsing is computationally expensive and requires a heavyweight virtual environment with native OpenCASCADE dependencies. Disabling it by default—controlled by `parse_enabled` checking for flags or environment variables—prevents accidental resource consumption when scanning case directories that may contain large or numerous CAD files.

### How does the tool handle large STEP assemblies?

The tool handles large assemblies through hierarchical extraction using the XCAF API in `extract_assembly_tree`, which preserves assembly structure rather than flattening the geometry. If memory constraints occur, the system falls back to `_fallback_solids_tree`, which simply counts solids without building a full hierarchy, reducing memory overhead at the cost of structural detail.

### What dependencies are required to run the CadQuery extraction pipeline?

The pipeline requires **CadQuery** and its native dependencies (OpenCASCADE), isolated within a virtual environment managed by [`bootstrap_cad_venv.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/bootstrap_cad_venv.py). For PNG generation, **cairosvg** is preferred, but the system can fall back to **Playwright** (via [`svg_screenshot.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/svg_screenshot.py)) if Cairo is unavailable. The host Python environment must support subprocess execution to manage the isolated `cad-env`.