mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-19 11:43:53 +00:00
ifcviewer-web: JavaScript scripting API (camera, selection, visibility, colour)
Give host pages a real API over the web viewer, not just "embed it and listen for picks": read/set the camera, read/set multi-selection, enumerate every object with its IFC identity, drive per-object visibility, and override object colours. The wasm boundary keeps to object_ids (u32 arrays marshalled through the heap, with an "ask twice" convention on the getters); web/ifcviewer.js layers IFC GlobalId resolution on top, from the element table getObjects() fetches. Every id-taking call accepts an objectId, a GlobalId, or an element object. Colour override needed no new mechanism: color_override_rgba8 was already plumbed through the sidecar, the instance SSBO, the WGSL shader and the opaque/transparent cull classifier, but nothing ever wrote a non-zero value into it. setObjectsColor is the missing writer, which is why an alpha below 255 correctly reclassifies the instance into the transparent pass. Two bugs surfaced while wiring this up: - wgpu_initialized_ was only ever set by the Qt desktop host, so on web every upload guarded on it was a silent no-op — including the pre-existing recomposeAndUploadModel that federation transforms depend on. The core now latches it in its own web init. - The demo pages were copied into the build dir by a POST_BUILD command on the wasm target, so they only refreshed when the wasm itself relinked; editing a page left a stale copy that the dev server (and the Playwright suite) kept serving. Each page now has its own copy rule with a real dependency, and sample.ifcview is a LINK_DEPENDS so regenerating it forces a relink. applyCachedModel also now keeps the element metadata it already parses on the path-based load (it was being dropped), so the embedded sample has GUIDs and the demo works with no file to pick. The sample model was three coincident cubes, which made per-object hide and colour look like no-ops — whatever you hid was still drawn by the box behind it. make_sample.py regenerates it as a slab, a wall and a beam in distinct places, so the fixture is reproducible rather than an opaque blob. Demoed by web/scripting.html (linked from the index; viewer is on window.viewer) and covered by tests/scripting.spec.mjs — 6 cases against a real GPU, asserting visibility and colour at the pixels, not just at the API. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Author the demo model the web viewer embeds (sample.ifcview).
|
||||
|
||||
The web build bakes one small sidecar into MEMFS (--embed-file in
|
||||
CMakeLists.txt) so every example page renders something before the user picks a
|
||||
file. This script is how that fixture is produced, so it can be regenerated
|
||||
rather than being an opaque committed blob:
|
||||
|
||||
python3 make_sample.py # writes sample.ifc + sample.ifcview
|
||||
ninja -C ../../build-web # re-embeds it
|
||||
|
||||
It needs ifcopenshell (Python) to author the IFC, and the sidecar_bake tool from
|
||||
the desktop build to convert it:
|
||||
|
||||
ninja -C ../../build-viewer sidecar_bake
|
||||
|
||||
Three elements — a slab, a wall and a beam — deliberately in DIFFERENT places
|
||||
and different shapes. The scripting examples hide and re-colour objects one at a
|
||||
time, so coincident geometry would make those calls look like no-ops: whatever
|
||||
you hid would still be there, drawn by the object behind it.
|
||||
"""
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import ifcopenshell
|
||||
import ifcopenshell.api.aggregate
|
||||
import ifcopenshell.api.context
|
||||
import ifcopenshell.api.geometry
|
||||
import ifcopenshell.api.project
|
||||
import ifcopenshell.api.root
|
||||
import ifcopenshell.api.spatial
|
||||
import ifcopenshell.api.unit
|
||||
|
||||
HERE = Path(__file__).parent
|
||||
BAKE = HERE / "../../build-viewer/ifcviewer/sidecar_bake"
|
||||
|
||||
# (class, name, footprint w x d in m, height in m, placement x/y/z in m)
|
||||
ELEMENTS = [
|
||||
("IfcSlab", "Slab", (6.0, 6.0), 0.2, (-3.0, -3.0, 0.0)),
|
||||
("IfcWall", "Wall", (5.0, 0.3), 3.0, (-2.5, -2.5, 0.2)),
|
||||
("IfcBeam", "Beam", (0.3, 5.0), 0.3, (2.0, -2.5, 3.2)),
|
||||
]
|
||||
|
||||
|
||||
def build_ifc(path: Path) -> None:
|
||||
f = ifcopenshell.api.project.create_file(version="IFC4")
|
||||
project = ifcopenshell.api.root.create_entity(f, ifc_class="IfcProject", name="Web viewer sample")
|
||||
ifcopenshell.api.unit.assign_unit(f, length={"is_metric": True, "raw": "METERS"})
|
||||
body = ifcopenshell.api.context.add_context(f, context_type="Model")
|
||||
body = ifcopenshell.api.context.add_context(
|
||||
f, context_type="Model", context_identifier="Body",
|
||||
target_view="MODEL_VIEW", parent=body)
|
||||
|
||||
site = ifcopenshell.api.root.create_entity(f, ifc_class="IfcSite", name="Site")
|
||||
storey = ifcopenshell.api.root.create_entity(f, ifc_class="IfcBuildingStorey", name="Ground floor")
|
||||
ifcopenshell.api.aggregate.assign_object(f, products=[site], relating_object=project)
|
||||
ifcopenshell.api.aggregate.assign_object(f, products=[storey], relating_object=site)
|
||||
|
||||
for ifc_class, name, (w, d), height, (x, y, z) in ELEMENTS:
|
||||
element = ifcopenshell.api.root.create_entity(f, ifc_class=ifc_class, name=name)
|
||||
ifcopenshell.api.spatial.assign_container(
|
||||
f, products=[element], relating_structure=storey)
|
||||
|
||||
# A rectangular profile extruded up — enough to be a recognisable,
|
||||
# distinctly-placed solid without dragging in a whole modelling stack.
|
||||
profile = f.create_entity(
|
||||
"IfcRectangleProfileDef", ProfileType="AREA", XDim=w, YDim=d,
|
||||
Position=f.create_entity(
|
||||
"IfcAxis2Placement2D",
|
||||
Location=f.create_entity("IfcCartesianPoint", Coordinates=(w / 2, d / 2))))
|
||||
solid = f.create_entity(
|
||||
"IfcExtrudedAreaSolid", SweptArea=profile, Depth=height,
|
||||
Position=f.create_entity(
|
||||
"IfcAxis2Placement3D",
|
||||
Location=f.create_entity("IfcCartesianPoint", Coordinates=(0.0, 0.0, 0.0))),
|
||||
ExtrudedDirection=f.create_entity("IfcDirection", DirectionRatios=(0.0, 0.0, 1.0)))
|
||||
representation = f.create_entity(
|
||||
"IfcShapeRepresentation", ContextOfItems=body, RepresentationIdentifier="Body",
|
||||
RepresentationType="SweptSolid", Items=[solid])
|
||||
ifcopenshell.api.geometry.assign_representation(
|
||||
f, product=element, representation=representation)
|
||||
ifcopenshell.api.geometry.edit_object_placement(
|
||||
f, product=element,
|
||||
matrix=ifcopenshell.util.placement.a2p((x, y, z), (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)))
|
||||
|
||||
f.write(str(path))
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if not BAKE.exists():
|
||||
sys.exit(f"{BAKE} not found — build it with: ninja -C ../../build-viewer sidecar_bake")
|
||||
|
||||
ifc = HERE / "sample.ifc"
|
||||
build_ifc(ifc)
|
||||
subprocess.run([str(BAKE), str(ifc)], check=True)
|
||||
|
||||
# sidecar_bake writes <name>.ifcview next to the input.
|
||||
baked = ifc.with_suffix(".ifcview")
|
||||
if not baked.exists():
|
||||
sys.exit(f"sidecar_bake did not produce {baked}")
|
||||
shutil.move(baked, HERE / "sample.ifcview")
|
||||
print(f"wrote {HERE / 'sample.ifcview'}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user