runtime: a notebook with no Part, Assembly or solid yet is no fault: find_product raises NoProductYetError (a ProductNotFoundError, still raised where a product is needed: export, a headless run with a product) and the editor previews it as an empty model ("no solid yet" in the log) instead of reporting ProductNotFoundError; the editor server's own empty-notebook case is gone (the kernel's empty preview covers it), and a notebook's first model is framed when it has one

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiJingzhe
2026-11-06 04:29:36 +08:00
parent d3eb5a6794
commit 5c2acac1d6
9 changed files with 56 additions and 35 deletions
@@ -91,6 +91,11 @@ if __name__ == "__main__":
Write a `make_part_rpart` cell only to add connectors or a material, or to
choose and name the bodies yourself; it then wins. The headless run and the
editor use the same rule (`runtime/projection.py`, `find_product`).
A notebook with no Part, Assembly or solid yet (a new one, or only sketches,
curves and workplanes so far) raises `NoProductYetError` (a
`ProductNotFoundError`) where a product is needed (export, a headless run
with `product=True`); the editor previews it as an empty model, not an error.
A Part or Assembly with another id is still a `ProductNotFoundError`.
- **The product** is the top-level variable whose id (`Part.part_id` or
`Assembly.assembly_id`) equals the notebook id. An assembly built up over
several cells (components, then constraints, then the solve) carries the
+10 -2
View File
@@ -264,12 +264,20 @@ export function App() {
partCache.current = preview.parts; // the parts on screen: the next frame may leave them out
view.loadPreview(preview);
setStatus(previewSummary(preview.nodes));
if (framedNotebook.current !== name) {
// the first model of a notebook is framed (a notebook with no solid yet has nothing to frame)
if (framedNotebook.current !== name && preview.nodes.length > 0) {
view.frame();
framedNotebook.current = name;
}
setSceneVersion((version) => version + 1);
log.push("ok", preview.nodes.length === 1 ? t("model shown: 1 part") : t("model shown: {{count}} parts", { count: preview.nodes.length }));
log.push(
"ok",
preview.nodes.length === 0
? t("no solid yet")
: preview.nodes.length === 1
? t("model shown: 1 part")
: t("model shown: {{count}} parts", { count: preview.nodes.length }),
);
} catch (error) {
log.push("error", error instanceof Error ? error.message : t("unable to open the preview"));
}
+1 -1
View File
@@ -887,7 +887,6 @@
"drv": "驱动",
"edge": "边",
"edit {{name}}": "编辑 {{name}}",
"empty notebook: add the first feature": "空白 notebook:请添加第一个特征",
"entity": "实体",
"error": "错误",
"explicit": "显式",
@@ -954,6 +953,7 @@
"no notebook": "未打开 notebook",
"no profile for the wire: {{why}}": "线框没有轮廓:{{why}}",
"no sketch yet": "还没有草图",
"no solid yet": "还没有实体",
"no {{what}} in this notebook yet": "此 notebook 中还没有 {{what}}",
"no {{what}} yet": "还没有 {{what}}",
"node": "节点",
-8
View File
@@ -1058,8 +1058,6 @@ class WorkspaceState:
"""
if not self._last_publish_error:
return None
if self._last_publish_error == "empty":
return {"type": "status", "info": "empty notebook: add the first feature"}
return {"type": "status", "error": self._last_publish_error[-300:]}
# -- settling: the cells ran; publish what they made -----------------------
@@ -1179,12 +1177,6 @@ class WorkspaceState:
self._last_publish_error = ""
self.hub.broadcast({"type": "status", "info": "stopped"})
return
if not kernel.setup_errors and not any(str(cell.get("code", "")).strip() for cell in self.cells):
# A notebook with no cells yet (a new one) has no product, and that is where it starts, not a fault.
if self._last_publish_error != "empty":
self._last_publish_error = "empty"
self.hub.broadcast({"type": "status", "info": "empty notebook: add the first feature"})
return
# A notebook mid-edit has no product yet; say so once, not per run.
if message != self._last_publish_error:
self._last_publish_error = message
+2 -1
View File
@@ -12,7 +12,7 @@ notebook file stays the only source of truth.
from .config import NotebookConfig, NotebookConfigError
from .executor import UnboundRequirementError, notebook_id
from .projection import ProductNotFoundError
from .projection import NoProductYetError, ProductNotFoundError
from .runner import CellReport, CellStatus, NotebookRun, run_notebook, use
__all__ = [
@@ -21,6 +21,7 @@ __all__ = [
"NotebookConfig",
"NotebookConfigError",
"NotebookRun",
"NoProductYetError",
"ProductNotFoundError",
"UnboundRequirementError",
"notebook_id",
+14 -10
View File
@@ -33,7 +33,7 @@ from ._marimo import safe_get_context
from .cells import NotebookCells
from .config import NotebookConfig, read_notebook_config
from .executor import finish_kernel_cache
from .projection import Product, ProductNotFoundError, find_product, open_bodies, product_reads, project_product
from .projection import NoProductYetError, Product, ProductNotFoundError, find_product, open_bodies, product_reads, project_product
from .workspace import features_in_cell
PACKAGE_MEDIA_TYPE = "application/vnd.simplecad.scadpkg"
@@ -265,15 +265,19 @@ def _live_scene() -> PreviewScene:
global _last_preview, _last_partial
started = time.perf_counter()
scope = live_scope()
product, partial = preview_product(
scope.config,
scope.cells,
scope.values,
failed=scope.failed,
skipped=scope.skipped,
lost_reads=scope.lost_reads,
)
scene = compile_preview(product)
try:
product, partial = preview_product(
scope.config,
scope.cells,
scope.values,
failed=scope.failed,
skipped=scope.skipped,
lost_reads=scope.lost_reads,
)
except NoProductYetError:
# no solid yet (only sketches, curves, workplanes): the model is empty, which is no fault
product, partial = None, None
scene = compile_preview(product) if product is not None else PreviewScene(parts={}, nodes=[], solids={}, definitions={})
last_timings["compile_ms"] = round((time.perf_counter() - started) * 1000, 1)
_last_preview, _last_partial = scene, partial
return scene
+9 -3
View File
@@ -54,6 +54,11 @@ class ProductNotFoundError(ValueError):
"""No top-level value, or more than one, is the notebook's product."""
class NoProductYetError(ProductNotFoundError):
"""The notebook makes no Part, no Assembly and no solid yet (a new notebook, or one with only sketches, curves and
workplanes so far). There is no product to export, but nothing is wrong: a preview of it is empty."""
def _product_id(value: Product) -> str:
return value.part_id if isinstance(value, Part) else value.assembly_id
@@ -127,10 +132,11 @@ def find_product(
implicit = _implicit_part(config, values, read)
if implicit is not None:
return implicit
found = ", ".join(sorted(others)) if others else "none"
if not others:
raise NoProductYetError(f"{config.path.name}: no Part, Assembly or solid yet")
raise ProductNotFoundError(
f"{config.path.name}: no top-level Part or Assembly has the notebook "
f"id {config.id!r}; Parts and Assemblies found: {found}"
f"id {config.id!r}; Parts and Assemblies found: {', '.join(sorted(others))}"
)
@@ -266,4 +272,4 @@ def project_product(
).value
__all__ = ["Product", "ProductNotFoundError", "find_product", "project_product"]
__all__ = ["NoProductYetError", "Product", "ProductNotFoundError", "find_product", "project_product"]
-9
View File
@@ -1012,15 +1012,6 @@ class PublishErrorReportingTest(unittest.TestCase):
self.fail_kernel(state)
return state, sent
def test_an_empty_notebook_is_where_it_starts_not_an_error(self) -> None:
state, sent = self.state()
state.cells = [] # a new notebook: no cells, so no product
state.settle()
state.settle()
self.assertEqual(self.told(sent), 0)
self.assertEqual([m["info"] for m in sent if m.get("info")], ["empty notebook: add the first feature"])
self.assertEqual(state.settled_error(), {"type": "status", "info": "empty notebook: add the first feature"})
def fail_kernel(self, state: WorkspaceState) -> None:
state_error = self.ERROR
state.kernel = type( # type: ignore[assignment]
+15 -1
View File
@@ -18,7 +18,7 @@ import pytest
import simplecadapi as scad
from simplecadapi.runtime.config import read_notebook_config
from simplecadapi.runtime.live import FailedCell, preview_product
from simplecadapi.runtime.projection import ProductNotFoundError
from simplecadapi.runtime.projection import NoProductYetError, ProductNotFoundError
NOTEBOOK = '''# /// script
# [tool.simplecadapi]
@@ -106,6 +106,20 @@ def test_the_body_the_failing_feature_was_applied_to_is_previewed_and_flagged(tm
preview_product(config, [_cell({"SIZE"})], {"SIZE": 40.0}, failed=[ROUNDED])
def test_a_notebook_with_no_solid_yet_has_no_product_and_that_is_no_fault(tmp_path):
path = tmp_path / "plate.py"
path.write_text(NOTEBOOK)
config = read_notebook_config(path)
# only numbers so far (a sketch or a curve is no solid either): no product, said apart from a wrong one
with pytest.raises(NoProductYetError, match="no Part, Assembly or solid yet"):
preview_product(config, [_cell({"SIZE"})], {"SIZE": 40.0})
with scad.GraphSession(graph_id="partial-other"):
other = scad.Part(part_id="other", body=scad.make_box_rsolid(1.0, 1.0, 1.0))
with pytest.raises(ProductNotFoundError, match="other") as raised:
preview_product(config, [_cell({"other"})], {"other": other})
assert not isinstance(raised.value, NoProductYetError)
def test_an_explicit_product_no_failed_cell_builds_on_is_whole(tmp_path):
path = tmp_path / "plate.py"
path.write_text(NOTEBOOK)