dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / src / modeling / api.lucb
## Luce's CAD modeling operations: features that make and change closed
## B-reps (see docs/MODELING.md for the plan and what each supports).
## A sketch plane on a body's face (CadModeling.face_frame): its origin and
## the directions of the sketch's x and y (the normal, x × y, out of the
## body); `found` false when there is no such flat face.
pub struct FaceFrame:
pub var found: bool
pub var origin: Vector3
pub var u: Vector3
pub var v: Vector3
## The modeling features, as static functions on closed solids.
pub struct CadModeling:
let reserved: u8 = 0
## The sketch in `set` (its curves on its sketch plane) as profiles:
## the faces `profiles` names (profile_keys' keys apart by `;`; empty:
## islands alternating from the outside in), faces next to each other
## joined, every region extruded `distance` along the plane's normal
## (negative: the other way) from `start` along it (a symmetric
## extrusion starts at minus half its distance): one closed solid
## each. Its faces are named for feature `tag` (naming.lucb).
pub static func extruded(set: interop.Reference[GeometrySet], distance: f64, start: f64, tag: str, profiles: str = "") -> interop.Reference[CadModel]!:
let source = try set.get()
let size = try sketch_size(source)
var p = try profiles_of(source, size * 0.0001, profiles)
defer p.close()
if start != 0.0: p.moved(p.normal.multiply_scalar(start))
let solid = try BrepModel.sized(size)
defer solid.release()
try extrude_into(solid.value(), &p, distance, tag)
return try CadModel.of_brep(solid)
## `a` and `b` combined: `operation` 0 their union, 1 `a` without
## `b`, 2 their intersection (both closed solids).
pub static func combined(a: interop.Reference[CadModel], b: interop.Reference[CadModel], operation: i64) -> interop.Reference[CadModel]!:
if operation < 0 or operation > 2: error(invalid, "a boolean operation is 0 union, 1 subtract or 2 intersect")
return try combine(a.value(), b.value(), operation)
## `model` with the edges `references` names (edge_references'; empty:
## every straight convex edge between two flat faces) chamfered
## `distance` along each face, the chamfers named for feature `tag`.
pub static func chamfered(model: interop.Reference[CadModel], references: str, distance: f64, tag: str, chain: bool = true) -> interop.Reference[CadModel]!:
return try blended(model.value(), references, distance, false, tag, 1, chain)
## `model` with the edges `references` names (as chamfered) filleted to
## `radius`, of `continuity` 1 (G1: circular), 2 (G2: curvature
## continuous) or 3 (G3: its change continuous too). With `chain` (as
## chamfered's), each edge brings the edges continuing it tangentially.
pub static func filleted(model: interop.Reference[CadModel], references: str, radius: f64, tag: str, continuity: i64 = 1, chain: bool = true) -> interop.Reference[CadModel]!:
return try blended(model.value(), references, radius, true, tag, continuity, chain)
## A fillet of `model`'s edges `references` (as filleted's) at
## `continuity`, kept between radii: a feature whose radius is being
## changed keeps it (made_for tells when its inputs changed) and asks
## it for each radius (FilletPlan.filleted).
pub static func fillet_plan(model: interop.Reference[CadModel], references: str, continuity: i64, tag: str, chain: bool = true) -> interop.Reference[FilletPlan]!:
return try fillet_plan_of(model, references, continuity, tag, chain)
## References to `model`'s edges `edges` (B-rep edge numbers), apart by
## `;`: each the names of its two faces and where it is, so a feature
## finds the same edges after changes upstream renumber them.
pub static func edge_references(model: interop.Reference[CadModel], edges: const i64[]) -> interop.Owned[str]!:
var w = try shape_of(model.value())
defer w.close()
var text = Growing[u8]()
defer text.close()
try references_of(&w, edges, &text)
return try interop.copy_text((str)text.view())
## How many of `model`'s edges `references` resolves to (an error when
## one is gone).
pub static func resolved_count(model: interop.Reference[CadModel], references: str) -> i64!:
var w = try shape_of(model.value())
defer w.close()
var edges = Growing[usize]()
defer edges.close()
try resolve_edges(&w, references, &edges)
return (i64)edges.count
## `model` with its unnamed faces named `tag/f<i>` (i the face's
## number), as a feature that makes a body without naming its faces
## (a primitive) gives them.
pub static func tagged(model: interop.Reference[CadModel], tag: str) -> interop.Reference[CadModel]!:
var w = try shape_of(model.value())
defer w.close()
var name = Growing[u8]()
defer name.close()
for f in 0..<w.faces.count:
if w.faces.items[f].name >= 0: continue
try role_name(&name, tag, "f")
try push_decimal(&name, (i64)f)
w.faces.items[f].name = try w.add_name((str)name.view())
return try shape_model(&w)
## `model` with `prefix` before each of its faces' names (every alias):
## a copy (a pattern's, a mirror's) distinct from its original.
pub static func prefixed(model: interop.Reference[CadModel], prefix: str) -> interop.Reference[CadModel]!:
var w = try shape_of(model.value())
defer w.close()
var name = Growing[u8]()
defer name.close()
for f in 0..<w.faces.count:
try prefixed_name(w.face_name(f), prefix, &name)
w.faces.items[f].name = try w.add_name((str)name.view())
return try shape_model(&w)
## The closed curves of the sketch in `set` as profiles, every region
## turned a full turn about its plane's `axis` (0 the plane's u
## direction, 1 its v direction) through the plane's origin, into
## closed solids, or turned `angle` radians (toward the plane's normal)
## and closed by the profile at each end. The profile must lie on one
## side of the axis. Its faces are named for feature `tag`.
pub static func revolved(set: interop.Reference[GeometrySet], axis: i64, tag: str, angle: f64 = 2.0 * math.pi, profiles: str = "") -> interop.Reference[CadModel]!:
let source = try set.get()
let size = try sketch_size(source)
var p = try profiles_of(source, size * 0.0001, profiles)
defer p.close()
let solid = try BrepModel.sized(size)
defer solid.release()
try revolve_into(solid.value(), &p, p.origin, p.u if axis == 0 else p.v, size * 0.000001, tag, angle)
return try CadModel.of_brep(solid)
## The keys of the sketch's profiles in `set` (the smallest regions its
## curves bound, as Fusion's profiles), apart by `;`: each names its
## region by the curves round it and the way each runs, so it holds as
## dimensions change.
pub static func profile_keys(set: interop.Reference[GeometrySet]) -> interop.Owned[str]!:
let source = try set.get()
var out = Growing[u8]()
defer out.close()
try face_keys(source, try sketch_size(source) * 0.0001, false, Vector3(), &out)
return try interop.copy_text((str)out.view())
## The key of the profile holding world point `at` (on the sketch
## plane), or "" when none does: what a click picks.
pub static func profile_at(set: interop.Reference[GeometrySet], at: Vector3) -> interop.Owned[str]!:
let source = try set.get()
var out = Growing[u8]()
defer out.close()
try face_keys(source, try sketch_size(source) * 0.0001, true, at, &out)
return try interop.copy_text((str)out.view())
## The outlines of the sketch's profiles in `set` that `keys` names
## (apart by `;`), as closed curves on its plane, to draw them.
pub static func profile_outlines(set: interop.Reference[GeometrySet], keys: str) -> interop.Reference[GeometrySet]!:
let source = try set.get()
var out = CurvesBuilder()
defer out.close()
try face_outlines(source, try sketch_size(source) * 0.0001, keys, &out)
let made = try out.finish()
let blank = try empty_set()
defer dispose_set(blank)
return try adopt_set(try with_curves(blank, made))
## The plane of `model`'s flat face named `name` (face_name's, any of its
## aliases): the face's own surface frame, turned so its normal leaves
## the body, for a sketch drawn on it. Names keep it the same face when
## features upstream change the body.
pub static func face_frame(model: interop.Reference[CadModel], name: str) -> FaceFrame!:
var w = try shape_of(model.value())
defer w.close()
for f in 0..<w.faces.count:
let face = w.faces.items[f]
if face.name < 0 or not (try names_meet(w.face_name(f), name)): continue
let s = w.surfaces.items[(usize)face.surface]
if s.kind != surface_plane: continue
# The surface's z leaves the body when the face runs with it;
# otherwise y turns over so x × y still leaves the body.
return FaceFrame(true, s.origin, s.x, s.y if face.sense else s.y.multiply_scalar(-1.0))
return FaceFrame(false, Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0))
## The name of `model`'s face `face` (a B-rep face number, as a pick
## mesh's cad_face says), the way face_frame finds it again.
pub static func face_name_of(model: interop.Reference[CadModel], face: i64) -> interop.Owned[str]!:
return try interop.copy_text(try model.value().face_name(face))
## How many profile regions the sketch in `set` has.
pub static func profile_count(set: interop.Reference[GeometrySet]) -> i64!:
let source = try set.get()
var p = try profiles_of(source, try sketch_size(source) * 0.0001)
defer p.close()
return (i64)p.region_count()
## The extent of the sketch's curves (at least 1), which scales tolerances.
func sketch_size(set: const GeometrySet*) -> f64!:
let curves = set_curves(set) else return 1.0
var low = Vector3(1.0e300, 1.0e300, 1.0e300)
var high = Vector3(-1.0e300, -1.0e300, -1.0e300)
for at in 0..<curves.point_count():
let p = curves.point(at)
low = Vector3(math.min(low.x, p.x), math.min(low.y, p.y), math.min(low.z, p.z))
high = Vector3(math.max(high.x, p.x), math.max(high.y, p.y), math.max(high.z, p.z))
if curves.point_count() == 0: return 1.0
return math.max(1.0, high.subtract(low).length())
## A model of shape `w`.
func shape_model(w: const Shape*) -> interop.Reference[CadModel]!:
let b = try brep_of(w)
defer b.release()
return try CadModel.of_brep(b)