dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / src / modeling / direct_api.lucb
## Direct modeling (Fusion's Modify tools for faces and bodies): Offset
## Face (Press Pull on a face), Move Face, Replace Face and Delete with
## healing, built without booleans (face_moves.lucb, face_delete.lucb).
## Faces are named as face_frame's are (CadModeling.face_name_of), several
## apart by `;`; a name split upstream into pieces is all of them.
## Direct modeling's operations, as static functions on closed solids.
pub struct CadDirect:
let reserved: u8 = 0
## `model` with faces `faces` moved `distance` along their outward
## normals (negative: in): planes moved, cylinders', cones', spheres'
## and tori's radii changed, the faces around extended or shrunk to
## meet them. With `chain`, faces meeting them tangentially move too
## (an earlier fillet's strips with the face they round).
pub static func offset_faces(model: interop.Reference[CadModel], faces: str, distance: f64, chain: bool = true) -> interop.Reference[CadModel]!:
if not math.is_finite(distance): error(invalid, "an offset needs a finite distance")
var w = try shape_of(model.value())
defer w.close()
var picked = Growing[usize]()
defer picked.close()
try faces_named(&w, faces, &picked)
if chain: try grow_tangent(&w, &picked)
let m = FaceMove(move_offset, Vector3(), Vector3(), Vector3(), 0.0, distance)
var targets = Growing[Surface]()
defer targets.close()
var senses = Growing[bool]()
defer senses.close()
var distances = Growing[f64]()
defer distances.close()
return try substitute(&w, picked.view(), &m, targets.view(), senses.view(), distances.view())
## `model` with faces `faces` moved by `by`, the faces around following.
pub static func moved_faces(model: interop.Reference[CadModel], faces: str, by: Vector3) -> interop.Reference[CadModel]!:
if not by.is_finite(): error(invalid, "a move needs a finite distance")
var w = try shape_of(model.value())
defer w.close()
var picked = Growing[usize]()
defer picked.close()
try faces_named(&w, faces, &picked)
let m = FaceMove(move_translate, by, Vector3(), Vector3(), 0.0, 0.0)
var targets = Growing[Surface]()
defer targets.close()
var senses = Growing[bool]()
defer senses.close()
var distances = Growing[f64]()
defer distances.close()
return try substitute(&w, picked.view(), &m, targets.view(), senses.view(), distances.view())
## `model` with faces `faces` turned `angle` radians about the line
## through `origin` along `axis`, the faces around following.
pub static func rotated_faces(model: interop.Reference[CadModel], faces: str, origin: Vector3, axis: Vector3, angle: f64) -> interop.Reference[CadModel]!:
let along = axis.normalized() catch failure: error(invalid, "a rotation needs an axis")
var w = try shape_of(model.value())
defer w.close()
var picked = Growing[usize]()
defer picked.close()
try faces_named(&w, faces, &picked)
let m = FaceMove(move_rotate, Vector3(), origin, along, angle, 0.0)
var targets = Growing[Surface]()
defer targets.close()
var senses = Growing[bool]()
defer senses.close()
var distances = Growing[f64]()
defer distances.close()
return try substitute(&w, picked.view(), &m, targets.view(), senses.view(), distances.view())
## Replace Face: `model` with faces `faces` on the surface of
## `target`'s faces `target_faces` instead (names apart by `;`; empty:
## every face of `target`, a whole surface body), the faces around
## extended or trimmed to it. With `chain` (Fusion's Tangent chain) the
## faces meeting the replaced ones tangentially are replaced too.
## Targets on one surface move the faces onto it (the surface's outside
## is the side the faces faced); targets on several surfaces matching
## the faces one for one move each onto its own, else (faces of a
## solid, or of a surface body) trim the body to them (face_replace.lucb).
pub static func replaced_faces(model: interop.Reference[CadModel], faces: str, target: interop.Reference[CadModel], target_faces: str, chain: bool = false) -> interop.Reference[CadModel]!:
var w = try shape_of(model.value())
defer w.close()
var t = try shape_of(target.value())
defer t.close()
var picked = Growing[usize]()
defer picked.close()
try faces_named(&w, faces, &picked)
if chain: try grow_tangent(&w, &picked)
var found = Growing[usize]()
defer found.close()
if trimmed(target_faces) == "":
for f in 0..<t.faces.count: try found.push(f)
if found.count == 0: error(invalid, "the target body has no faces")
else:
try faces_named(&t, target_faces, &found)
if not (try on_one_surface(&t, found.view())):
# Face for face onto like targets, locally (face_replace.lucb).
let matched = replaced_face_by_face(&w, picked.view(), &t, found.view()) catch unmatched:
_ = unmatched
return try replaced_by_booleans(model, &w, picked.view(), target, &t, found.view())
return matched
let s = &t.surfaces.items[(usize)t.faces.items[found.items[0]].surface]
if s.kind == surface_spline: error(invalid, "Replace Face takes a plane, cylinder, cone, sphere or torus face")
var surfaces = Growing[Surface]()
defer:
for surface in surfaces.view():
var owned = surface
owned.close()
surfaces.close()
var senses = Growing[bool]()
defer senses.close()
for f in picked.view():
try surfaces.push(try surface_copy(s))
# Facing the way the face did, at its middle.
let middle = try point_on_face(&w, f)
let outward = try w.face_normal(f, middle)
let (_, n) = try signed_gap(s, middle, w.tolerance)
try senses.push(n.dot(outward) > 0.0)
let m = FaceMove(move_replace, Vector3(), Vector3(), Vector3(), 0.0, 0.0)
var distances = Growing[f64]()
defer distances.close()
return try substitute(&w, picked.view(), &m, surfaces.view(), senses.view(), distances.view())
## `model` without faces `faces`, healed (Fusion's Delete): holes and
## bosses whole, or one fillet, chamfer or corner face, the faces around
## extended to close the gap; between flat faces, any number of fillet,
## chamfer and corner faces at once (a rounded chain with its corners,
## strips apart); a band between one face and a ring round it, curved
## ones too (a boss's foot fillet, a hole's rounded rim); faces in
## groups apart from each other, each group in turn. An error says when
## they cannot.
pub static func deleted_faces(model: interop.Reference[CadModel], faces: str) -> interop.Reference[CadModel]!:
var w = try shape_of(model.value())
defer w.close()
var picked = Growing[usize]()
defer picked.close()
try faces_named(&w, faces, &picked)
var deleted = Growing[bool]()
defer deleted.close()
try deleted.fill(w.faces.count, false)
for f in picked.view(): deleted.items[f] = true
let local = healed(&w, deleted.view()) catch failure:
# Not holes or bosses whole, nor one strip or corner: caps
# between flat faces (face_heal.lucb), several at once.
let more = healed_by_caps(model, deleted.view()) catch second:
# A band between one face and a ring of faces, curved
# ones too (face_band.lucb): extended until they meet.
var again = try shape_of(model.value())
defer again.close()
let band = healed_band(&again, deleted.view()) catch third:
_ = third
# Faces apart from each other (strips apart, a ring
# beside curved faces and a strip elsewhere): each
# group healed on its own, in turn.
var text = Growing[u8]()
defer text.close()
var ends = Growing[usize]()
defer ends.close()
try deleted_groups(&w, deleted.view(), &text, &ends)
if ends.count < 2: error(invalid, failure.message if picked.count == 1 else second.message)
var current = model.clone()
defer current.release()
var begin: usize = 0
for end in ends.view():
let next = try CadDirect.deleted_faces(current, (str)text.view()[begin..<end])
current.release()
current = next
begin = end
return current.clone()
return band
return more
return local
## `model`'s blends `faces` (identified_blends' names; empty: every
## blend) given `radius`: deleted with healing and filleted again, the
## blends they meet tangentially keeping their own radii, the new faces
## named for `tag` (blend_find.lucb's reblended).
pub static func reblended(model: interop.Reference[CadModel], faces: str, radius: f64, tag: str) -> interop.Reference[CadModel]!:
return try reblended(model, faces, radius, tag)
## Fusion's Split Face: `model` with faces `faces` split where the
## surface of `tool`'s face `tool_face` (a plane, cylinder, cone,
## sphere or torus), extended past the face, crosses them; one closed
## solid with the new edges. The pieces on the surface's outer side (a
## plane's side the tool face faces; outside a curved surface) are
## named `<tag>/split[<name>]`, the others keep their name
## (face_split.lucb). With `extend` off (Fusion's Extend splitting
## tool) only the tool face itself cuts, a B-spline face too: it must
## reach across each face it splits.
pub static func split_faces(model: interop.Reference[CadModel], faces: str, tool: interop.Reference[CadModel], tool_face: str, tag: str, extend: bool = true) -> interop.Reference[CadModel]!:
var t = try shape_of(tool.value())
defer t.close()
var found = Growing[usize]()
defer found.close()
try faces_named(&t, tool_face, &found)
let f = found.items[0]
let s = &t.surfaces.items[(usize)t.faces.items[f].surface]
if s.kind == surface_spline and extend: error(invalid, "Split Face extends a plane, cylinder, cone, sphere or torus face; turn Extend splitting tool off to split by a B-spline face")
var chosen = try chosen_faces(model, faces)
defer chosen.close()
var one = Growing[i64]()
defer one.close()
try one.push((i64)f)
if s.kind == surface_plane and not t.faces.items[f].sense:
# The plane turned to face the way the face does.
var turned = try plane_surface(s.origin, s.x, s.z.multiply_scalar(-1.0))
defer turned.close()
if not extend: return try split_faces_by_face(model.value(), chosen.view(), &turned, tool.value(), one.view(), tag)
return try split_faces_by(model.value(), chosen.view(), &turned, tag)
if not extend: return try split_faces_by_face(model.value(), chosen.view(), s, tool.value(), one.view(), tag)
return try split_faces_by(model.value(), chosen.view(), s, tag)
## Split Face by the plane through `origin` facing `normal`: the pieces
## on the normal's side are named `<tag>/split[<name>]`.
pub static func split_faces_by_plane(model: interop.Reference[CadModel], faces: str, origin: Vector3, normal: Vector3, tag: str) -> interop.Reference[CadModel]!:
let n = normal.normalized() catch failure: error(invalid, "a split plane needs a normal")
let seed = Vector3(1.0, 0.0, 0.0) if math.abs(n.x) < 0.9 else Vector3(0.0, 1.0, 0.0)
let x = try seed.subtract(n.multiply_scalar(seed.dot(n))).normalized()
var plane = try plane_surface(origin, x, n)
defer plane.close()
var chosen = try chosen_faces(model, faces)
defer chosen.close()
return try split_faces_by(model.value(), chosen.view(), &plane, tag)
## Fusion's Tangent chain: faces `faces` and every face meeting one of
## them tangentially, and so on (a fillet's strips with the faces they
## round onto), by name apart by `;`, the faces named first.
pub static func tangent_chain(model: interop.Reference[CadModel], faces: str) -> interop.Owned[str]!:
var w = try shape_of(model.value())
defer w.close()
var picked = Growing[usize]()
defer picked.close()
try faces_named(&w, faces, &picked)
try grow_tangent(&w, &picked)
var out = Growing[u8]()
defer out.close()
for f in picked.view():
if out.count > 0: try out.push(59)
for byte in w.face_name(f).bytes: try out.push(byte)
return try interop.copy_text((str)out.view())
## Move/Copy of faces with Create copy: an open surface body of copies
## of `model`'s faces `faces`, their names (every alias) with `prefix`
## before them so they are told from the originals.
pub static func copied_faces(model: interop.Reference[CadModel], faces: str, prefix: str) -> interop.Reference[CadModel]!:
var w = try shape_of(model.value())
defer w.close()
var picked = Growing[usize]()
defer picked.close()
try faces_named(&w, faces, &picked)
var ids = Growing[i64]()
defer ids.close()
for f in picked.view(): try ids.push((i64)f)
let part = try model.value().subset(ids.view())
defer part.release()
return try CadModeling.prefixed(part, prefix)
## The names of `model`'s faces `faces` (B-rep face numbers, as a pick
## gives them), apart by `;`: what the operations here take.
pub static func face_names(model: interop.Reference[CadModel], faces: const i64[]) -> interop.Owned[str]!:
var out = Growing[u8]()
defer out.close()
for at in 0..<faces.length:
if at > 0: try out.push(59)
let name = try model.value().face_name(faces[at])
if name == "": error(invalid, "a face without a name cannot be referred to: name the body's faces first (CadModeling.tagged)")
for byte in name.bytes: try out.push(byte)
return try interop.copy_text((str)out.view())
## The faces of `w` that `text` names (names apart by `;`, each any alias),
## each once, onto `out`; an error when a name finds none.
func faces_named(w: const Shape*, text: str, out: Growing[usize]*) -> !:
out.clear()
var parts = Growing[(usize, usize)]()
defer parts.close()
try top_parts(text, 59, &parts)
for part in parts.view():
let name = trimmed(part_of(text, part))
if name == "": continue
var found = false
for f in 0..<w.faces.count:
if not (try names_meet(w.face_name(f), name)): continue
found = true
var known = false
for g in out.view():
if g == f: known = true
if not known: try out.push(f)
if not found: error(invalid, "a face this operation refers to is gone: pick it again")
if out.count == 0: error(invalid, "pick the faces")
## `faces` with every face meeting one of them tangentially, and so on.
func grow_tangent(w: const Shape*, faces: Growing[usize]*) -> !:
var taken = Growing[bool]()
defer taken.close()
try taken.fill(w.faces.count, false)
for f in faces.view(): taken.items[f] = true
var grew = true
while grew:
grew = false
for e in 0..<w.edges.count:
let fa = w.sides.items[e * 2]
let fb = w.sides.items[e * 2 + 1]
if fa < 0 or fb < 0 or taken.items[(usize)fa] == taken.items[(usize)fb]: continue
if not (try tangent_edge(w, e)): continue
let other = (usize)(fb if taken.items[(usize)fa] else fa)
taken.items[other] = true
try faces.push(other)
grew = true
## A point of face `f`: its boundary's middle brought onto its surface,
## or, where that has no nearest point (a whole cylinder's wall, whose
## middle is on its axis), the middle of its first edge.
pub func point_on_face(w: const Shape*, f: usize) -> Vector3!:
let on = nearest_on_surface(w, f, try face_middle(w, f)) catch failure:
_ = failure
return try w.edge_at(edge_index(w.face_uses(f)[0]), 0.5)
return on
## The point of face `f`'s surface nearest `p`.
pub func nearest_on_surface(w: const Shape*, f: usize, p: Vector3) -> Vector3!:
let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
# A B-spline's nearest point itself (p moved along the normal there
# lands off a curved surface).
if s.kind == surface_spline: return try evaluate(*s, try nearest_uv(s, p, w.tolerance))
let (gap, n) = try signed_gap(s, p, w.tolerance)
return p.subtract(n.multiply_scalar(gap))
## The faces of `model` that `faces` names, marked.
## The names of `w`'s faces `deleted` in groups joined across their edges,
## each group's apart by `;`, back to back in `text`, each group ending at
## its place in `ends`.
func deleted_groups(w: const Shape*, deleted: const bool[], text: Growing[u8]*, ends: Growing[usize]*) -> !:
var group = Growing[i64]()
defer group.close()
try group.fill(w.faces.count, -1)
var stack = Growing[usize]()
defer stack.close()
var count: i64 = 0
for f in 0..<w.faces.count:
if not deleted[f] or group.items[f] >= 0: continue
group.items[f] = count
try stack.push(f)
while stack.count > 0:
let g = stack.items[stack.count - 1]
stack.count -= 1
for use in w.face_uses(g):
let e = edge_index(use)
for side in [w.sides.items[e * 2], w.sides.items[e * 2 + 1]]:
if side < 0 or not deleted[(usize)side] or group.items[(usize)side] >= 0: continue
group.items[(usize)side] = count
try stack.push((usize)side)
count += 1
for k in 0..<count:
var first = true
for f in 0..<w.faces.count:
if group.items[f] != k: continue
if not first: try text.push(59)
first = false
for byte in w.face_name(f).bytes: try text.push(byte)
try ends.push(text.count)
func chosen_faces(model: interop.Reference[CadModel], faces: str) -> Growing[bool]!:
var w = try shape_of(model.value())
defer w.close()
var picked = Growing[usize]()
defer picked.close()
try faces_named(&w, faces, &picked)
var marked = Growing[bool]()
errdefer marked.close()
try marked.fill(w.faces.count, false)
for f in picked.view(): marked.items[f] = true
return marked
## The plane through `origin` with unit axes `x` and normal `z`.
func plane_surface(origin: Vector3, x: Vector3, z: Vector3) -> Surface!:
var no_points = Growing[Vector3]()
defer no_points.close()
var knots = Growing[f64]()
defer knots.close()
return try Surface(surface_plane, origin, x, z, 0.0, 0.0, no_points.view(), knots.view(), knots.view(), knots.view(), 0, 0, 0, 0)