73 lines
2.7 KiB
Luau
73 lines
2.7 KiB
Luau
-- examples/cad.luau: reads a small STEP file, walks its entity graph (references, complex instances), adds an entity
|
|
-- and writes it back under another schema. Run from the repository root: luau examples/cad.luau
|
|
-- Solid geometry (B-rep bodies, meshes) needs a kernel plug-in, F.plugins.brep: see docs/guide/cad.md.
|
|
local F = require("@formats")
|
|
|
|
local STEP = [[
|
|
ISO-10303-21;
|
|
HEADER;
|
|
FILE_DESCRIPTION(('example'),'2;1');
|
|
FILE_NAME('bracket.step','2026-10-31T12:00:00',('me'),(''),'formats-luau','','');
|
|
FILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 1 1 1 1 }'));
|
|
ENDSEC;
|
|
DATA;
|
|
#10=CARTESIAN_POINT('',(0.,0.,0.));
|
|
#11=DIRECTION('',(0.,0.,1.));
|
|
#12=DIRECTION('',(1.,0.,0.));
|
|
#13=AXIS2_PLACEMENT_3D('',#10,#11,#12);
|
|
#14=PLANE('',#13);
|
|
#20=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));
|
|
#21=PRODUCT('bracket','Bracket','',(#22));
|
|
#22=PRODUCT_CONTEXT('',#23,'mechanical');
|
|
#23=APPLICATION_CONTEXT('automotive design');
|
|
ENDSEC;
|
|
END-ISO-10303-21;
|
|
]]
|
|
|
|
local cad = assert(F.cad.load(STEP))
|
|
local step = assert(F.format("step"))
|
|
print(string.format("schema %s, %d instances, file %s", cad.schema, cad.meta.count, cad.header.FILE_NAME[1]))
|
|
|
|
-- count instances per type
|
|
local counts: { [string]: number } = {}
|
|
for _, id in step.ids(cad) do
|
|
local t = cad.entities[id].type
|
|
counts[t] = (counts[t] or 0) + 1
|
|
end
|
|
local types: { string } = {}
|
|
for t in counts do
|
|
table.insert(types, t)
|
|
end
|
|
table.sort(types)
|
|
for _, t in types do
|
|
print(string.format(" %-22s %d", t, counts[t]))
|
|
end
|
|
|
|
-- follow references: PLANE -> AXIS2_PLACEMENT_3D -> its location point
|
|
local plane = cad.entities[step.byType(cad, "PLANE")[1]]
|
|
local placement = assert(step.get(cad, plane.args[2]))
|
|
local location = assert(step.get(cad, placement.args[2]))
|
|
print("plane origin:", table.concat(location.args[2], ", "))
|
|
|
|
-- the length unit is a complex instance
|
|
local si = assert(step.part(cad.entities[20], "SI_UNIT"))
|
|
print("length unit:", si[1].enum, si[2].enum) -- MILLI METRE
|
|
assert(si[1].enum == "MILLI")
|
|
|
|
-- the product name
|
|
local product = cad.entities[step.byType(cad, "PRODUCT")[1]]
|
|
print("product:", product.args[1], product.args[2])
|
|
|
|
-- add a point and write the file as AP242
|
|
local p = step.add(cad, "CARTESIAN_POINT", { "tip", step.real(step.real(step.real({ 40, 0, 8 }, 1), 2), 3) })
|
|
local text = assert(F.saveString(cad, { schema = "AP242" }))
|
|
assert(string.find(text, "#" .. p .. "=CARTESIAN_POINT('tip',(40.,0.,8.));", 1, true))
|
|
local back = assert(F.cad.load(text))
|
|
assert(back.schema == "AP242" and #step.ids(back) == 10) -- add() also updates meta.count
|
|
print(string.format("wrote %d bytes of AP242 with #%d added", #text, p))
|
|
|
|
-- without a kernel plug-in the B-rep calls say so
|
|
local _, err = F.cad.tessellate(cad)
|
|
print("tessellate without a plug-in:", err.code)
|
|
print("cad example: ok")
|