From 8bd1a9327c4c00a0fca3971fcc07e8ff69b1d9bb Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sun, 1 Nov 2026 01:51:17 -0500 Subject: [PATCH] cad formats: comments are specs only --- src/Rosetta/cad/step/Brep.luau | 27 ++++------------- src/Rosetta/cad/step/Parse.luau | 24 ++------------- src/Rosetta/cad/step/Write.luau | 10 ++----- src/Rosetta/cad/step/init.luau | 52 +++++++-------------------------- 4 files changed, 22 insertions(+), 91 deletions(-) diff --git a/src/Rosetta/cad/step/Brep.luau b/src/Rosetta/cad/step/Brep.luau index de05024..00da05c 100644 --- a/src/Rosetta/cad/step/Brep.luau +++ b/src/Rosetta/cad/step/Brep.luau @@ -1,16 +1,7 @@ --!strict --- cad/step/Brep: the injected B-rep plug-in (Rosetta.plugins.brep; rosetta never requires a kernel). Normalises --- the plug-in table, calls it under pcall (kernels throw on bad geometry) and turns its tessellated meshes into a --- Scene. Interpreted on purpose: the plug-in's errors unwind through here. --- --- The interface (every function may throw; results are checked): --- read(text: string, opts) -> { bodies = { Body }, stats = { ... }? } STEP text -> B-rep bodies (body.name?, --- body.color? = Color3 | { r, g, b } | { 1, 2, 3 }) --- write(bodies: { Body }, opts) -> string bodies -> STEP text --- tessellate(body: Body, opts) -> { positions = { vector | {x, y, z} }, normals = { ... }?, indices = { 1-based --- triples }, faceOf = { [triangle] = Face (face.color?) }? } --- or brep-luau's modules as they are: { Step = , StepBrep = , StepWrite = , Tess = } (adapted here: read = --- StepBrep.read(Step.parse(text)), write = StepWrite.write with options.colors from body.color, tessellate = Tess.body). +-- cad/step/Brep: the injected B-rep plug-in (Rosetta.plugins.brep; never required by rosetta), called under pcall. +-- Interface (each may throw): read(text, opts) -> { bodies, stats? }; write(bodies, opts) -> text; +-- tessellate(body, opts) -> { positions, normals?, indices (1-based triples), faceOf? }; or brep-luau's modules as-is. local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") @@ -30,7 +21,7 @@ local function rgbOf(c: any): (number?, number?, number?) if type(c) == "table" then return c.r or c.R or c[1], c.g or c.G or c[2], c.b or c.B or c[3] end - return (c :: any).R, (c :: any).G, (c :: any).B -- Color3 + return (c :: any).R, (c :: any).G, (c :: any).B end) if ok and type(r) == "number" and type(g) == "number" and type(b) == "number" then return r, g, b @@ -38,7 +29,6 @@ local function rgbOf(c: any): (number?, number?, number?) return nil, nil, nil end --- plugin(p) -> the normalised interface, or nil + the reason function Brep.plugin(p: any): (Plugin?, string?) if type(p) ~= "table" then return nil, "needs the B-rep plug-in (Rosetta.plugins.brep = { read, write, tessellate }, e.g. brep-luau's { Step, StepBrep, StepWrite, Tess })" @@ -82,7 +72,6 @@ function Brep.plugin(p: any): (Plugin?, string?) return nil, "the B-rep plug-in has neither read() nor brep-luau's Step + StepBrep modules" end --- find(opts) -> the plug-in from opts.brep or opts.plugins.brep function Brep.find(opts: any): (Plugin?, Err.Err?) local p = nil if type(opts) == "table" then @@ -98,7 +87,6 @@ function Brep.find(opts: any): (Plugin?, Err.Err?) return plug, nil end --- read(plug, text, opts) -> bodies, result | nil, Err function Brep.read(plug: Plugin, text: string, opts: any): (({ any })?, any, Err.Err?) local ok, res = pcall(plug.read, text, opts) if not ok then @@ -110,7 +98,6 @@ function Brep.read(plug: Plugin, text: string, opts: any): (({ any })?, any, Err return res.bodies, res, nil end --- write(plug, bodies, opts) -> text | nil, Err function Brep.write(plug: Plugin, bodies: { any }, opts: any): (string?, Err.Err?) local w = plug.write if not w then @@ -130,7 +117,7 @@ local function xyz(p: any): (number, number, number) if type(p) == "table" then return tonumber(p.x or p.X or p[1]) or 0, tonumber(p.y or p.Y or p[2]) or 0, tonumber(p.z or p.Z or p[3]) or 0 end - return (p :: any).X, (p :: any).Y, (p :: any).Z -- vector / Vector3 + return (p :: any).X, (p :: any).Y, (p :: any).Z end local function linear(c: number): number @@ -138,8 +125,7 @@ local function linear(c: number): number return if c <= 0.04045 then c / 12.92 else ((c + 0.055) / 1.055) ^ 2.4 end --- scene(plug, bodies, opts) -> Scene: one node + mesh per body, one primitive per face colour (materials shared by --- colour; baseColor = the STEP colour converted from sRGB to linear, extras.color = the colour as written) +-- baseColor = the STEP colour converted from sRGB to linear; extras.color = the colour as written function Brep.scene(plug: Plugin, bodies: { any }, opts: any): (Types.Scene?, Err.Err?) local tess = plug.tessellate if not tess then @@ -196,7 +182,6 @@ function Brep.scene(plug: Plugin, bodies: { any }, opts: any): (Types.Scene?, Er end end local br, bg, bb = rgbOf(type(body) == "table" and body.color or nil) - -- triangles grouped by colour, in first-seen order local groups: { { mat: number, tris: { number } } } = {} local byMat: { [number]: { mat: number, tris: { number } } } = {} local faceOf = if type(mesh.faceOf) == "table" then mesh.faceOf else {} diff --git a/src/Rosetta/cad/step/Parse.luau b/src/Rosetta/cad/step/Parse.luau index 1a86bf3..6080b3d 100644 --- a/src/Rosetta/cad/step/Parse.luau +++ b/src/Rosetta/cad/step/Parse.luau @@ -1,15 +1,7 @@ --!strict --!native --- cad/step/Parse: ISO 10303-21 (STEP physical file, editions 1-3) text -> header + entity instances. A single pass --- over the text: comments, the HEADER section, any number of DATA sections (edition 3 `DATA('name', ('schema'));`), --- ANCHOR / REFERENCE / SIGNATURE sections kept as raw text, simple and complex (external mapping) instances, every --- parameter kind (integer, real, string with its \S\ \X\ \X2\ \X4\ escapes decoded to UTF-8, enumeration, binary, --- entity reference, $, *, list, typed parameter). Never throws: failures return nil, message, offset. --- --- Values: numbers; strings; { enum = "T" }; { ref = 12 }; { binary = "0FF" }; { typed = "LENGTH_MEASURE", value = v }; --- lists as arrays; NULL / DERIVED sentinels (from the caller). A real literal with an integral value (`0.`, `1.E2`) --- is recorded in the metatable of the list (or typed table) holding it (`__reals = { [index | "value"] = true }`), so --- the writer prints it back as a real; integral numbers without that mark are written as integers. +-- cad/step/Parse: ISO 10303-21 text -> header + entity instances, one pass; never throws (nil, message, offset). +-- An integral real (`0.`, `1.E2`) is marked in its holder's metatable (`__reals = { [index | "value"] = true }`). local Parse = {} local sbyte, ssub, sfind, schar, floor = string.byte, string.sub, string.find, string.char, math.floor @@ -32,7 +24,6 @@ local function fail(ctx: Ctx, msg: string, at: number): nil return nil end --- the next position that is not white space or inside a /* comment */ (n + 1 at the end; an open comment -> error) local function skip(ctx: Ctx, i: number): number local s, n = ctx.s, ctx.n while i <= n do @@ -60,7 +51,6 @@ local function isWord(c: number): boolean return (c >= 65 and c <= 90) or (c >= 97 and c <= 122) or c == 95 or (c >= 48 and c <= 57) end --- keyword at i ([!]A-Z_ A-Z0-9_*) -> word, next; nil when there is none local function keyword(ctx: Ctx, i: number): (string?, number) local s, n = ctx.s, ctx.n local j = i @@ -77,7 +67,6 @@ local function keyword(ctx: Ctx, i: number): (string?, number) return string.upper(ssub(s, i, j - 1)), j end --- UTF-8 of a code point local function utf8char(cp: number): string if cp < 0x80 then return schar(cp) @@ -100,9 +89,7 @@ local function hexAt(s: string, i: number, k: number): number? return tonumber(h, 16) end --- the text between the quotes of a string literal -> its value ('' -> ', \\ -> \, \S\c, \X\hh, \X2\..\X0\, \X4\..\X0\, --- \P?\ page switches ignored: every page reads as ISO 8859-1). Bytes >= 0x80 written raw (common, though not allowed) --- pass through. +-- Part 21 escapes: '' \\ \S\c \X\hh \X2\..\X0\ \X4\..\X0\; \P?\ ignored (ISO 8859-1); raw bytes >= 0x80 pass through function Parse.unescape(raw: string): string if not sfind(raw, "[\\']") then return raw @@ -185,11 +172,9 @@ local function markReal(t: { [any]: any }, key: any) mt.__reals[key] = true end --- one parameter at i -> value, next, integralReal (nil value + ctx.err on failure) local value: (ctx: Ctx, i: number, depth: number) -> (any, number, boolean) local function list(ctx: Ctx, i: number, depth: number): ({ any }?, number) - -- i is just past "(" local s = ctx.s local out = {} i = skip(ctx, i) @@ -309,7 +294,6 @@ value = function(ctx: Ctx, i: number, depth: number): (any, number, boolean) end Parse.value = value --- the simple record at i: NAME(params) -> type, args, next local function record(ctx: Ctx, i: number): (string?, { any }?, number) local word, j = keyword(ctx, i) if not word then @@ -326,7 +310,6 @@ local function record(ctx: Ctx, i: number): (string?, { any }?, number) return word, args, k end --- expect(ctx, i, ch) -> next position after ch (skipping blanks), or nil local function expect(ctx: Ctx, i: number, ch: number, what: string): number? i = skip(ctx, i) if sbyte(ctx.s, i) ~= ch then @@ -353,7 +336,6 @@ export type Result = { terminated: boolean, } --- parse(s, opts) -> Result | nil, message, offset (1-based position in s) function Parse.parse(s: string, null: any, derived: any, maxEntities: number): (Result?, string?, number?) local ctx: Ctx = { s = s, n = #s, err = nil, errAt = nil, null = null, derived = derived } local res: Result = { diff --git a/src/Rosetta/cad/step/Write.luau b/src/Rosetta/cad/step/Write.luau index 0045126..6543a5c 100644 --- a/src/Rosetta/cad/step/Write.luau +++ b/src/Rosetta/cad/step/Write.luau @@ -1,16 +1,13 @@ --!strict --!native --- cad/step/Write: header + entity instances -> ISO 10303-21 text. Values as cad/step/Parse produces them; strings --- re-escaped (' -> '', \ -> \\, UTF-8 outside printable ASCII -> \X2\..\X0\ / \X4\..\X0\, control bytes and invalid --- UTF-8 -> \X\hh), reals printed with the fewest digits (15..17) that read back to the same double. Never throws: --- failures return nil, message. +-- cad/step/Write: header + entity instances -> ISO 10303-21 text (escaped strings, shortest round-trip reals). local Write = {} local sformat, sbyte, ssub, sfind, floor, huge = string.format, string.byte, string.sub, string.find, math.floor, math.huge local MAX_DEPTH = 100 --- the shortest %g form that reads back exactly, in Part 21 syntax (a '.' always, upper-case E) +-- Part 21 real: a '.' always, upper-case E function Write.real(v: number): string? if v ~= v or v == huge or v == -huge then return nil @@ -45,7 +42,6 @@ local function hex(v: number, k: number): string return table.concat(out) end --- a string's Part 21 literal (with the quotes) function Write.str(s: string): string if not sfind(s, "[^ -&(-[%]^-~]") then -- printable ASCII without ' and \ return "'" .. s .. "'" @@ -72,7 +68,6 @@ function Write.str(s: string): string end i += 1 else - -- a UTF-8 sequence -> code point local cp: number? = nil local len = 1 if c >= 0xC2 and c < 0xE0 then @@ -235,7 +230,6 @@ value = function(ctx: Ctx, v: any, real: boolean, depth: number): boolean end Write.value = value --- record(ctx, type, args) -> appends TYPE(args) function Write.record(ctx: Ctx, t: any, args: any): boolean if not isName(t) then ctx.err = "bad entity type " .. tostring(t) diff --git a/src/Rosetta/cad/step/init.luau b/src/Rosetta/cad/step/init.luau index da6c4e6..1d05f65 100644 --- a/src/Rosetta/cad/step/init.luau +++ b/src/Rosetta/cad/step/init.luau @@ -1,24 +1,7 @@ --!strict --- cad/step: STEP physical files (ISO 10303-21 editions 1-3, "Part 21": AP203 / AP214 / AP242 and any other schema). --- New code. Reads and writes the entity graph (Types.Cad) fully in-library: the header (FILE_DESCRIPTION / FILE_NAME / --- FILE_SCHEMA + any other header entity), every DATA section (edition 3 named sections: entity.section), simple and --- complex instances (type "COMPLEX", parts = { { type, args } }), every parameter kind: integers and reals (numbers; --- integral reals such as `0.` keep their real-ness through a metatable mark, see Parse), strings (escapes decoded to --- UTF-8: \S\ \X\ \X2\ \X4\, '' and \\), { enum = "T" } (.T. / .F. booleans too), { ref = id }, { binary = "0FF" }, --- { typed = "LENGTH_MEASURE", value = v }, lists, Step.NULL ($) and Step.DERIVED (*), comments; ANCHOR / REFERENCE / --- SIGNATURE sections kept as raw text (meta.source.extra) and written back. parse -> write -> parse gives the same --- entities. Writes one instance per line in id order, strings escaped as Part 21 requires (\X2\ for non-ASCII). --- --- B-rep bodies and tessellation come from an injected kernel, never required by rosetta: --- F.plugins.brep = { read = fn(text, opts) -> { bodies, stats? }, write = fn(bodies, opts) -> text, --- tessellate = fn(body, opts) -> { positions, normals?, indices (1-based), faceOf? } } --- or brep-luau's modules as they are: F.plugins.brep = { Step = , StepBrep = , StepWrite = , Tess = } --- (the full contract is in cad/step/Brep). Step.toBodies(cad, opts) / Step.fromBodies(bodies, opts) / --- Step.tessellate(cad | bodies, opts) take it from opts.brep or opts.plugins.brep; F.cad.toBodies / fromBodies / --- tessellate pass F.plugins. Tessellated scenes: one node + mesh per body, one primitive per face colour, z up, --- millimetres (meta.unit = 0.001). --- Not supported: STEP-XML (ISO 10303-28), compressed .stpZ, edition 3 value instances (#NAME, @constants: CORRUPT), --- code pages other than ISO 8859-1 in \S\ escapes (\P?\ is ignored). +-- cad/step: STEP physical files (ISO 10303-21 eds 1-3; AP203 / AP214 / AP242 / any schema). Supports: header, DATA +-- sections, simple and complex instances, every parameter kind, ANCHOR / REFERENCE / SIGNATURE kept raw; B-rep via the +-- injected plug-in (cad/step/Brep). Not supported: STEP-XML (10303-28), .stpZ, ed. 3 value instances, \P?\ code pages. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") @@ -41,19 +24,19 @@ local Step = { -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { - maxBytes: number?, -- refuse larger files (default 2^30) - maxEntities: number?, -- refuse more instances (default 2^24) + maxBytes: number?, -- default 2^30 + maxEntities: number?, -- default 2^24 } export type EncodeOptions = { - schema: string?, -- "AP203" | "AP214" | "AP242" | a schema name: overrides the header's FILE_SCHEMA - name: string?, -- FILE_NAME name (default: the header's, else "model") - timestamp: string?, -- FILE_NAME time_stamp when the header has none (default "1970-01-01T00:00:00") - crlf: boolean?, -- CRLF line ends (default LF) + schema: string?, -- overrides the header's FILE_SCHEMA + name: string?, -- default: the header's, else "model" + timestamp: string?, -- default "1970-01-01T00:00:00" + crlf: boolean?, } export type BrepOptions = { - brep: any?, -- the plug-in (else opts.plugins.brep) + brep: any?, plugins: { [string]: any }?, - chord: number?, -- tessellation sag in millimetres (default 0.05); opts.tess = the kernel's own options instead + chord: number?, -- millimetres (default 0.05) tess: { [string]: any }?, } @@ -68,7 +51,6 @@ local SCHEMAS = { } Step.SCHEMAS = SCHEMAS --- a FILE_SCHEMA name -> "AP203" | "AP214" | "AP242" | the name itself local function schemaOf(name: string): string local n = string.upper(string.match(name, "^%s*([^%s{]+)") or name) if n == "CONFIG_CONTROL_DESIGN" or string.find(n, "^AP203") then @@ -203,7 +185,6 @@ function Step.encode(cad: Types.Cad, opts: EncodeOptions?): (buffer?, Err.Err?) end end end - -- entities by section, in id order local ids = {} for id, e in cad.entities do if type(id) ~= "number" or id ~= math.floor(id) or id < 0 or type(e) ~= "table" then @@ -257,7 +238,6 @@ function Step.encode(cad: Types.Cad, opts: EncodeOptions?): (buffer?, Err.Err?) end -- ------------------------------------------------------------------------------------------------ extras --- get(cad, id | { ref = id }) -> the entity function Step.get(cad: Types.Cad, id: any): Types.StepEntity? if type(id) == "table" then id = id.ref @@ -265,7 +245,6 @@ function Step.get(cad: Types.Cad, id: any): Types.StepEntity? return cad.entities[id] end --- ids(cad) -> the instance ids in ascending order function Step.ids(cad: Types.Cad): { number } local out = {} for id in cad.entities do @@ -275,7 +254,6 @@ function Step.ids(cad: Types.Cad): { number } return out end --- byType(cad, "ADVANCED_FACE") -> the ids of the instances of that type (complex ones with such a part too), ascending function Step.byType(cad: Types.Cad, name: string): { number } local n = string.upper(name) local out = {} @@ -295,7 +273,6 @@ function Step.byType(cad: Types.Cad, name: string): { number } return out end --- part(entity, "TYPE") -> the args of that type (the entity's own, or one part of a complex instance) function Step.part(e: Types.StepEntity?, name: string): { any }? if not e then return nil @@ -313,7 +290,6 @@ function Step.part(e: Types.StepEntity?, name: string): { any }? return nil end --- add(cad, type, args) -> id: a new instance after the highest id function Step.add(cad: Types.Cad, type: string, args: { any }): number local top = 0 for id in cad.entities do @@ -327,7 +303,6 @@ function Step.add(cad: Types.Cad, type: string, args: { any }): number return id end --- real(list, index) -> list: marks list[index] as a real (an integral value is then written `3.`, not `3`) function Step.real(t: { [any]: any }, key: any): { [any]: any } local mt: any = getmetatable(t) if not mt then @@ -338,12 +313,10 @@ function Step.real(t: { [any]: any }, key: any): { [any]: any } return t end --- new(schema?) -> an empty Cad ("AP214" by default) function Step.new(schema: string?): Types.Cad return { kind = "cad", schema = schema or "AP214", header = {}, entities = {}, meta = { count = 0 } } end --- plugin(p) -> the normalised B-rep plug-in interface (see cad/step/Brep) | nil, reason Step.plugin = Brep.plugin local function textOf(cad: Types.Cad): (string?, Err.Err?) @@ -354,7 +327,6 @@ local function textOf(cad: Types.Cad): (string?, Err.Err?) return buffer.tostring(buf), nil end --- toBodies(cad, opts) -> bodies, result (the plug-in's read result: stats, warnings, ...) | nil, Err function Step.toBodies(cad: Types.Cad, opts: BrepOptions?): (({ any })?, any) local plug, perr = Brep.find(opts) if not plug then @@ -371,7 +343,6 @@ function Step.toBodies(cad: Types.Cad, opts: BrepOptions?): (({ any })?, any) return bodies, res end --- fromBodies(bodies, opts) -> Cad (the plug-in writes STEP text, parsed here) | nil, Err function Step.fromBodies(bodies: { any }, opts: BrepOptions?): (Types.Cad?, Err.Err?) if type(bodies) ~= "table" then return nil, Err.new(Err.ARG, "needs a list of bodies") @@ -387,7 +358,6 @@ function Step.fromBodies(bodies: { any }, opts: BrepOptions?): (Types.Cad?, Err. return Step.decode(Bytes.reader(buffer.fromstring(text))) end --- tessellate(cad | bodies, opts) -> Scene (one node + mesh per body, z up, millimetres) | nil, Err function Step.tessellate(value: any, opts: BrepOptions?): (Types.Scene?, Err.Err?) local plug, perr = Brep.find(opts) if not plug then