/pid-to-kg:pid-to-kgfrom pid-to-kg
Converts P&ID and PFD PDFs (vector or scanned) into a process knowledge graph with equipment connectivity, instrument locations, and control loops. Outputs JSON, DuckDB, Markdown summary, and DEXPI 2.0.1 XML.
Ships in pid-to-kg. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Work is split between deterministic scripts and you:
All scripts are in scripts/ next to this file. Call them by their full path (the commands below abbreviate it as scripts/), and keep the work directory in the user's current folder, e.g. work/<plant>.
Setup. Many users are engineers, not programmers, so handle setup yourself. The scripts need Python 3.10+. Before the first run, install the dependencies with pip install -r <skill dir>/scripts/requirements.txt (use a virtual environment if the user's Python is shared with other work). If Python is missing, tell the user to install it from python.org and stop there.
--pages. If not, and the PDF has more than a few pages, probe it, look at the page overviews or ask the user, then re-run the probe with --pages so later steps skip reports, legends and non-P&ID drawings.possible_duplicate_revision. Record each sheet's issue_status from the title block or stamps.confidence, or into unreadable.Copy this checklist into your notes and tick it off:
- [ ] 1. Probe: python scripts/probe_pdf.py <pdf> --work <work> --plant-name "<name>" [--pages 47-60]
- [ ] 2. Render: python scripts/render_pages.py <work>
- [ ] 3. Orient: open each pages/page_NN/overview_grid.png; fix rotation if text is not upright
- [ ] 4. Hints: python scripts/make_hints.py <work>
- [ ] 5. Extract: for each sheet, write <work>/extract/page_NN.json (references/extraction_procedure.md)
- [ ] 6. Build: python scripts/build_kg.py <work>
- [ ] 7. QA loop: fix every error, re-check every warning, rebuild (repeat)
- [ ] 8. Report: summarize the results and open issues for the user
probe_pdf.py assigns each page a class and an extraction route, and writes <work>/manifest.json. With --pages, the later steps render and read only those pages.
| Class | What it means | Route |
|---|---|---|
vector_text | Vector drawing with a text layer | Render from vectors; exact text from the text layer as hints |
vector_outlined | Vector drawing, text drawn as outlines (no text layer) | High-resolution vector render; OCR hints |
raster | Scanned image | Native-resolution render, upscaled and contrast-enhanced; OCR hints |
raster_with_text | Scan with an OCR text layer | As raster; text layer as (untrusted) hints |
hybrid | Vectors plus large images | Both routes |
Report the probe result to the user in one line, e.g. "14 vector sheets with outlined text."
Low scan resolution is flagged in the notes. Expect to record more items in unreadable on those sheets.
render_pages.py writes the following for each page:
overview.pngoverview_grid.png, which shows the tile namestiles/tiles.json, which records which tiles are blankOrientation is detected with OCR when it is installed. Always confirm it on overview_grid.png. If the main drawing text is not upright, re-render that page with an explicit rotation, given clockwise: render_pages.py <work> --pages N --rotation 90.
Title blocks are sometimes rotated differently from the drawing. Orient by the drawing labels, not by the title block.
make_hints.py writes hints/page_NN.json. Each file holds candidate tags with page positions, taken from the text layer or from OCR. They are only hints: OCR misreads, and it misses things. You don't need to read the hint files during extraction. QA uses them to flag tags you may have missed. If OCR is not installed the step is skipped; note this in the final report.
This is the core of the job. Follow references/extraction_procedure.md exactly. It covers:
Reference material, to read when needed:
kind valuesreferences/schema/extraction.schema.json: the exact JSON schema of a page fileWrite extract/page_NN.json and save it before starting the next sheet, so a long job never holds more than one sheet in context. With many sheets, and if your environment supports subagents, you can extract sheets in parallel. Give each subagent the procedure file and its own page directory.
build_kg.py validates every page file and stops with a precise message on schema errors. It then writes these files to <work>/kg/:
graph.jsongraph.duckdbsummary.mdqa_report.mdgraph.dexpi.xml, the DEXPI 2.0.1 native XML export. It is written only when it passes both the official XSD and the DEXPI model checkdexpi_export_report.md, which gives the validation result and everything DEXPI could not representThe DEXPI export runs on every build; it needs no flag or extra request. The build's last line reports it:
dexpi: valid DEXPI 2.0.1 ... means the file is current. If it mentions mapping warnings, the file is still valid; the report lists the items affected, such as nozzles and tees added to satisfy the DEXPI model.dexpi: FAILED ... means no graph.dexpi.xml exists for this build, and any older copy was removed. The other outputs are fine. Read dexpi_export_report.md, tell the user, and do not hand over an old DEXPI file.Work through qa_report.md:
unresolved_reference, are mistakes in the page files. Fix them and rebuild.crop.py for a zoomed view, and correct the extraction if the drawing shows something different. Common warnings:
instrument_not_located: a sensor with no measuresequipment_not_connectedunmatched_off_pagepossible_missed_tags: an OCR tag that is not in your extractiontiles_not_reviewedStop when there are no errors and every warning has been checked.
python scripts/crop.py <work> --page 5 --bbox 0.40 0.20 0.55 0.35 # zoomed view of a region
Tell the user:
summary.mdReport the DEXPI status: valid, valid with mapping warnings (name the main kinds from dexpi_export_report.md), or failed. Say that the DEXPI file is semantic only, with no drawing graphics, so CAD tools will not redraw it as a P&ID.
Point to qa_report.md for the details. State plainly that the graph is machine-extracted and needs engineering review before it is used for design, operations or safety.
summary.md is the compact, token-efficient context for an LLM: equipment-level flow, an instrument table with locations, and a loop table.graph.json is the canonical graph, which any tool can read.graph.duckdb is for SQL. Its views are equipment, equipment_flow, instruments, loops and signals, and its table macros are upstream(id) and downstream(id).graph.dexpi.xml is for DEXPI-capable engineering tools.Use kg_query.py to answer questions; add --json when another tool will consume the output:
python scripts/kg_query.py <work>/kg downstream 660-TK-001 --equipment
python scripts/kg_query.py <work>/kg upstream FV-0502
python scripts/kg_query.py <work>/kg path 660-TK-001 660-TK-004
python scripts/kg_query.py <work>/kg instruments --on 660-PP-011
python scripts/kg_query.py <work>/kg loop FIC-0502
python scripts/kg_query.py <work>/kg sql "SELECT * FROM equipment_flow"
To measure extraction quality on a sheet, hand-check it into a small gold file (format in scripts/eval_kg.py) and run python scripts/eval_kg.py <work>/kg gold.json: it reports tag, flow and loop precision/recall and instrument placement.
To move a graph into other tools: python scripts/export.py <work>/kg graphml produces a file for yEd, Gephi or NetworkX, and python scripts/export.py <work>/kg neo4j produces CSVs plus a Cypher import script. For a graph.json built before DEXPI export existed, python scripts/export.py <work>/kg dexpi writes the DEXPI file and report using the same mapping and validation as the build.
references/dexpi/2.0.1/Builtin.xml (opens in a new tab)references/dexpi/2.0.1/Core.xml (opens in a new tab)references/dexpi/2.0.1/DEXPI_XML_Schema.xsd (opens in a new tab)references/dexpi/2.0.1/Plant.xml (opens in a new tab)references/dexpi/2.0.1/SOURCE.md (opens in a new tab)references/extraction_procedure.md (opens in a new tab)references/isa_5_1.md (opens in a new tab)references/ontology.md (opens in a new tab)references/schema/extraction.schema.json (opens in a new tab)references/schema/graph.schema.json (opens in a new tab)references/symbol_guide.md (opens in a new tab)scripts/build_kg.py (opens in a new tab)scripts/crop.py (opens in a new tab)scripts/eval_kg.py (opens in a new tab)scripts/export.py (opens in a new tab)scripts/kg_query.py (opens in a new tab)scripts/make_hints.py (opens in a new tab)scripts/pidkg/__init__.py (opens in a new tab)scripts/pidkg/build.py (opens in a new tab)scripts/pidkg/dexpi/__init__.py (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Generated from the plugin's indexed repository (scottduncanai/pid-to-knowledge-graph) — no native marketplace selected
npx claudepluginhub p/scottduncanai-pid-to-kgOther plugins' skills for the same job