mirror of
https://github.com/revarbat/BOSL2.git
synced 2025-08-24 15:12:56 +02:00
Merge pull request #1720 from adrianVmariano/master
rename part_geometry to define_part and add parent_part()
This commit is contained in:
@@ -16,13 +16,14 @@ Requires OpenSCAD 2021.01 or later.
|
||||
The BOSL2 library is an enormous library that provides many different kinds of capabilities to simplify the development of models in OpenSCAD, and to make things possible that are difficult in native OpenSCAD. Some of the things BOSL2 provides are:
|
||||
|
||||
* **Attachments.** Unless you make models containing just one object, the attachments features can revolutionize your modeling. They let you position components of a model relative to other components so you don't have to keep track of the positions and orientations of parts of the model. You can instead place something on the TOP of something else, perhaps aligned to the RIGHT. For a full introduction to attachments, consult the [Attachments Tutorial.](https://github.com/BelfrySCAD/BOSL2/wiki/Tutorial-Attachments)
|
||||
* **Rounding and filleting.** Rounding and filleting is hard in OpenSCAD. The library provides modules like [cuboid()](https://github.com/BelfrySCAD/BOSL2/wiki/shapes3d.scad#module-cuboid) to make a cube with any of the edges rounded, [offset_sweep()](https://github.com/BelfrySCAD/BOSL2/wiki/rounding.scad#functionmodule-offset_sweep) to round the ends of a linear extrusion, and [prism_connector()](https://github.com/BelfrySCAD/BOSL2/wiki/rounding.scad#module-prism_connector) which works with the attachments feature to create filleted prisms between a variety of objects, or holes through a single object with rounded edges at the ends. You can also use [edge_profile()](https://github.com/BelfrySCAD/BOSL2/wiki/attachments.scad#module-edge_profile) to apply a variety of different mask profiles to chosen edges of a cubic shape, or you can directly subtract 3d mask shapes from an edge of objects that are not cubes.
|
||||
* **Rounding and filleting.** Rounding and filleting is hard in OpenSCAD. The library provides modules like [cuboid()](https://github.com/BelfrySCAD/BOSL2/wiki/shapes3d.scad#module-cuboid) to make a cube with any of the edges rounded, [offset_sweep()](https://github.com/BelfrySCAD/BOSL2/wiki/rounding.scad#functionmodule-offset_sweep) to round the ends of a linear extrusion, and [prism_connector()](https://github.com/BelfrySCAD/BOSL2/wiki/rounding.scad#module-prism_connector) which works with the attachments feature to create filleted prisms between a variety of objects, or holes through a single object with rounded edges at the ends. You can also use [edge_profile()](https://github.com/BelfrySCAD/BOSL2/wiki/attachments.scad#module-edge_profile) to apply a variety of different mask profiles to chosen edges of a cubic shape, or you can directly subtract 3d mask shapes from an edge of objects that are not cubes.
|
||||
* **Shorthands.** The shorthands make your code a little shorter, and more importantly, they can make it significantly easier to read. Compare `up(z)` to `translate([0,0,z])`. The shorthands include operations for creating [copies of objects](https://github.com/BelfrySCAD/BOSL2/wiki/distributors.scad) and for applying [transformations](https://github.com/BelfrySCAD/BOSL2/wiki/transforms.scad) to objects, including [rot()](https://github.com/BelfrySCAD/BOSL2/wiki/transforms.scad#functionmodule-rot) which extends `rotate()` in some useful ways that are not easy to do directly.
|
||||
* **Complex object support.** The [path_sweep()](https://github.com/BelfrySCAD/BOSL2/wiki/skin.scad#functionmodule-path_sweep) function/module takes a 2d polygon moves it through space along a path and sweeps out a 3d shape as it moves. You can link together a series of arbitrary polygons with [skin()](https://github.com/BelfrySCAD/BOSL2/wiki/skin.scad#functionmodule-skin) or [vnf_vertex_array().](https://github.com/BelfrySCAD/BOSL2/wiki/vnf.scad#functionmodule-vnf_vertex_array) Support for [beziers](https://github.com/BelfrySCAD/BOSL2/wiki/beziers.scad) and [NURBS](https://github.com/BelfrySCAD/BOSL2/wiki/nurbs.scad) can help you construct the building blocks you need. [Metaballs](https://github.com/BelfrySCAD/BOSL2/wiki/isosurface.scad#functionmodule-metaballs) can create organic surfaces that blend shapes together.
|
||||
* **Building Blocks.** OpenSCAD provides cubes, cones and spheres. The BOSL2 library extends this to provide different kinds of prisms, tubes, and other abstract geometrical building blocks. In many cases the BOSL2 objects include options to round their edges. Basic objects have extensions like the ability to specify the **inner** radius of a circle to create holes with a guaranteed minimum size.
|
||||
* **Texturing.** Many kinds of objects can be created with [textures](https://github.com/BelfrySCAD/BOSL2/wiki/skin.scad#section-texturing) applied. This can create knurling, but it can do much more than that. A texture can be any repeating pattern, and applying a texture can actually replace the base object with something different based on repeating copies of the texture element. A texture can also be an image; using texturing you can emboss an arbitrary image onto your model.
|
||||
* **Parts library.** The parts library includes many useful specific functional parts including [gears](https://github.com/BelfrySCAD/BOSL2/wiki/gears.scad), generic [threading](https://github.com/BelfrySCAD/BOSL2/wiki/threading.scad#section-generic-threading), and specific threading to match plastic [bottles](https://github.com/BelfrySCAD/BOSL2/wiki/bottlecaps.scad), [pipe fittings](https://github.com/BelfrySCAD/BOSL2/wiki/threading.scad#module-npt_threaded_rod), or standard [screws.](https://github.com/BelfrySCAD/BOSL2/wiki/screws.scad) Also included are [clips](https://github.com/BelfrySCAD/BOSL2/wiki/joiners.scad#section-tension-clips), [hinges](https://github.com/BelfrySCAD/BOSL2/wiki/hinges.scad), and [dovetail joints.](https://github.com/BelfrySCAD/BOSL2/wiki/joiners.scad#section-dovetails)
|
||||
* **Shorthands.** The shorthands make your code a little shorter, and more importantly, they can make it significantly easier to read. Compare `up(z)` to `translate([0,0,z])`. The shorthands include operations for creating [copies of objects](https://github.com/BelfrySCAD/BOSL2/wiki/distributors.scad) and for applying [transformations](https://github.com/BelfrySCAD/BOSL2/wiki/transforms.scad) to objects, including [rot()](https://github.com/BelfrySCAD/BOSL2/wiki/transforms.scad#functionmodule-rot) which extends `rotate()` in some useful ways that are not easy to do directly.
|
||||
* **Geometrical operations on data.** In OpenSCAD, geometrical operations happen on geometry, and information can never be extracted from geometry. The BOLS2 library provides operations on 2d point lists (called "paths" or "regions") to make [rounded paths](https://github.com/BelfrySCAD/BOSL2/wiki/rounding.scad#function-round_corners) from ones with corners or do operations like [intersection](https://github.com/BelfrySCAD/BOSL2/wiki/regions.scad#functionmodule-intersection) and [offset](https://github.com/BelfrySCAD/BOSL2/wiki/regions.scad#function-offset). It can also do some limited operations on three dimensional data.
|
||||
* **Geometrical operations on curves** In OpenSCAD, geometrical operations happen on geometry, and information can never be extracted from geometry. The BOLS2 library provides operations on 2d point lists (called "paths" or "regions") to make [rounded paths](https://github.com/BelfrySCAD/BOSL2/wiki/rounding.scad#function-round_corners) from ones with corners or do operations like [intersection](https://github.com/BelfrySCAD/BOSL2/wiki/regions.scad#functionmodule-intersection) and [offset](https://github.com/BelfrySCAD/BOSL2/wiki/regions.scad#function-offset). It can also do some limited operations on three dimensional data.
|
||||
* **Geometrical operations on polyhedra** Openscad describes a polyhedron using a list of vertices and faces, which BOSL2 refers to as a VNF (for Vertices 'N' Faces). When working directly with polyhedra it is tedious to ensure a valid result. The library provides tools to create VNFs that are guaranteed to be valid, and it offers some limited geometrical operations on VNFs.
|
||||
* **Programming aids.** The library provides basic [mathematical operations](https://github.com/BelfrySCAD/BOSL2/wiki/math.scad) including solutions to [linear systems of equations](https://github.com/BelfrySCAD/BOSL2/wiki/linalg.scad#function-linear_solve) and [generic](https://github.com/BelfrySCAD/BOSL2/wiki/math.scad#function-root_find) and [polynomial](https://github.com/BelfrySCAD/BOSL2/wiki/math.scad#function-real_roots) numerical root finding. It provides [geometrical operations](https://github.com/BelfrySCAD/BOSL2/wiki/geometry.scad) like [line intersection](https://github.com/BelfrySCAD/BOSL2/wiki/geometry.scad#function-line_intersection) or [circle intersection](https://github.com/BelfrySCAD/BOSL2/wiki/geometry.scad#function-circle_circle_intersection), [coordinate transformations](https://github.com/BelfrySCAD/BOSL2/wiki/coords.scad), [string manipulation,](https://github.com/BelfrySCAD/BOSL2/wiki/strings.scad) and [list processing](https://github.com/BelfrySCAD/BOSL2/wiki/lists.scad).
|
||||
|
||||
|
||||
|
137
attachments.scad
137
attachments.scad
@@ -1073,6 +1073,45 @@ module attach(parent, child, overlap, align, spin=0, norot, inset=0, shiftout=0,
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Module: attach_part()
|
||||
// Synopsis: Select a named attachable part for subsequent attachment operations
|
||||
// Topics: Attachment
|
||||
// See Also: attach(), align(), attachable(), define_part(), parent_part()
|
||||
// Usage:
|
||||
// PARENT() attach_part(name) CHILDREN;
|
||||
// Description:
|
||||
// Most attachable objects have a single geometry defined that is used by the attachment commands,
|
||||
// but some objects also define attachable parts. This module selects
|
||||
// an attachable part using a name defined by the parent object. Any operations
|
||||
// that use the parent geometry such as {{attach()}}, {{align()}}, {{position()}} or {{parent()}}
|
||||
// will reference the geometry for the specified part. This allows you to access the inner wall
|
||||
// of tubes, for example. Note that you cannot call `attach_part()` as a child of another `attach_part()`.
|
||||
// Arguments:
|
||||
// name = name of part to use for subsequent attachments.
|
||||
// Example: This example shows attaching the light blue cube normally, on the outside of the tube, and the pink cube using the "inside" attachment part.
|
||||
// tube(ir1=10,ir2=20,h=20, wall=3){
|
||||
// color("lightblue")attach(RIGHT,BOT) cuboid(4);
|
||||
// color("pink")
|
||||
// attach_part("inside")
|
||||
// attach(BACK,BOT) cuboid(4);
|
||||
// }
|
||||
|
||||
module attach_part(name)
|
||||
{
|
||||
req_children($children);
|
||||
dummy=assert(!is_undef($parent_parts), "Parent does not exist or does not have any parts");
|
||||
ind = search([name], $parent_parts, 1,0)[0];
|
||||
dummy2 = assert(ind!=[], str("Parent does not have a part named ",name));
|
||||
$parent_geom = $parent_parts[ind][1];
|
||||
$anchor_inside = $parent_parts[ind][2];
|
||||
T = $parent_parts[ind][3];
|
||||
$parent_parts = [];
|
||||
multmatrix(T)
|
||||
children();
|
||||
}
|
||||
|
||||
|
||||
// Section: Tagging
|
||||
|
||||
// Module: tag()
|
||||
@@ -1350,7 +1389,7 @@ module tag_scope(scope){
|
||||
}
|
||||
|
||||
|
||||
// Section: Attachment Modifiers
|
||||
// Section: Tagged Operations with Attachable Objects
|
||||
|
||||
// Module: diff()
|
||||
// Synopsis: Performs a differencing operation using tags rather than hierarchy to control what happens.
|
||||
@@ -3157,40 +3196,6 @@ module _show_ghost()
|
||||
}
|
||||
|
||||
|
||||
// Module: attach_part()
|
||||
// Synopsis: Select a named attachable part for subsequent attachment operations
|
||||
// Topics: Attachment
|
||||
// See Also: attach(), align(), attachable(), part_geometry()
|
||||
// Usage:
|
||||
// PARENT() attach_part(name) CHILDREN;
|
||||
// Description:
|
||||
// Selects an attachable part using a name defined by the parent object. Any operations
|
||||
// that use the parent geometry such as {{attach()}}, {{align()}}, {{position()}} or {{parent()}}
|
||||
// will reference the geometry for the specified part. This allows you to access the inner wall
|
||||
// of tubes, for example.
|
||||
// Arguments:
|
||||
// name = name of part to use for subsequent attachments.
|
||||
// Example: This example shows attaching the light blue cube normally, on the outside of the tube, and the pink cube using the "inside" attachment part.
|
||||
// tube(ir1=10,ir2=20,h=20, wall=3){
|
||||
// color("lightblue")attach(RIGHT,BOT) cuboid(4);
|
||||
// color("pink")
|
||||
// attach_part("inside")
|
||||
// attach(BACK,BOT) cuboid(4);
|
||||
// }
|
||||
|
||||
module attach_part(name)
|
||||
{
|
||||
req_children($children);
|
||||
dummy=assert(!is_undef($parent_parts), "Parent does not exist or does not have any parts");
|
||||
ind = search([name], $parent_parts, 1,0)[0];
|
||||
dummy2 = assert(ind!=[], str("Parent does not have a part named ",name));
|
||||
$parent_geom = $parent_parts[ind][1];
|
||||
$anchor_inside = $parent_parts[ind][2];
|
||||
multmatrix($parent_parts[ind][3])
|
||||
children();
|
||||
}
|
||||
|
||||
|
||||
|
||||
function _is_geometry(entry) = is_list(entry) && is_string(entry[0]);
|
||||
|
||||
@@ -3583,12 +3588,12 @@ function attach_geom(
|
||||
: ["point", cp, offset, anchors];
|
||||
|
||||
|
||||
// Function: part_geometry()
|
||||
// Function: define_part()
|
||||
// Synopsis: Creates an attachable part data structure.
|
||||
// Topics: Attachments
|
||||
// See Also: attachable()
|
||||
// Usage:
|
||||
// part = part_geometry(name, geom, [inside=], [T=]);
|
||||
// part = define_part(name, geom, [inside=], [T=]);
|
||||
// Description:
|
||||
// Create a named attachable part that can be passed in the `parts` parameter of {{attachable()}}
|
||||
// and then selected using {{attach_part()}}.
|
||||
@@ -3602,8 +3607,8 @@ function attach_geom(
|
||||
// module twocyl(d, sep, h, ang=20)
|
||||
// {
|
||||
// parts = [
|
||||
// part_geometry("left", attach_geom(r=d/2,h=h), T=left(sep/2)*yrot(-ang)),
|
||||
// part_geometry("right", attach_geom(r=d/2,h=h), T=right(sep/2)*yrot(ang)),
|
||||
// define_part("left", attach_geom(r=d/2,h=h), T=left(sep/2)*yrot(-ang)),
|
||||
// define_part("right", attach_geom(r=d/2,h=h), T=right(sep/2)*yrot(ang)),
|
||||
// ];
|
||||
// attachable(size=[sep+d,d,h], parts=parts){
|
||||
// union(){
|
||||
@@ -3619,7 +3624,7 @@ function attach_geom(
|
||||
// color("green")attach_part("right")attach(TOP,BOT) cuboid(3);
|
||||
// }
|
||||
|
||||
function part_geometry(name, geom, inside=false, T=IDENT) =
|
||||
function define_part(name, geom, inside=false, T=IDENT) =
|
||||
assert(is_string(name), "name must be a string")
|
||||
assert(_is_geometry(geom), "geometry appears invalid")
|
||||
assert(is_bool(inside), "inside must be boolean")
|
||||
@@ -5143,7 +5148,7 @@ function _canonical_edge(edge) =
|
||||
|
||||
// Function: parent()
|
||||
// Topics: Transforms, Attachments, Descriptions
|
||||
// See Also: restore()
|
||||
// See Also: restore(), parent_part()
|
||||
// Synopsis: Returns a description (transformation state and attachment geometry) of the parent
|
||||
// Usage:
|
||||
// PARENT() let( desc = parent() ) CHILDREN;
|
||||
@@ -5164,6 +5169,56 @@ function parent() =
|
||||
[$transform, geom];
|
||||
|
||||
|
||||
|
||||
// Function: parent_part()
|
||||
// Topics: Transforms, Attachments, Descriptions
|
||||
// See Also: restore(), parent()
|
||||
// Synopsis: Returns a description (transformation state and attachment geometry) of a part defined by the parent
|
||||
// Usage:
|
||||
// PARENT() let( desc = parent_part(name) ) CHILDREN;
|
||||
// Usage: in development releases only
|
||||
// PARENT() { desc=parent_part(name); CHILDREN; }
|
||||
// Description:
|
||||
// Returns a description of the parent part with the specified name. You can use this
|
||||
// description to create new objects based on the described object or perform computations based on the described object. You can also use it to
|
||||
// restore the context of the parent object and transformation state using {{restore()}}. Note that with OpenSCAD 2021.01 you need to use `let` for
|
||||
// this function to work, and the definition of the variable is scoped to the children of the let module.
|
||||
// (In development versions the use of let is no longer necessary.) Note that if OpenSCAD displays any warnings
|
||||
// related to transformation operations then the transformation that parent_part() returns is likely to be incorrect, even if OpenSCAD
|
||||
// continues to run and produces a valid result.
|
||||
// Example(3D): This example defines an object with two parts and then uses `parent_part()` to create a {{prism_connector()}} between the two parts of the object.
|
||||
// $fn=48;
|
||||
// module twocyl(d, sep, h, ang=20)
|
||||
// {
|
||||
// parts = [
|
||||
// define_part("left", attach_geom(r=d/2,h=h),
|
||||
// T=left(sep/2)*yrot(-ang)),
|
||||
// define_part("right", attach_geom(r=d/2,h=h),
|
||||
// T=right(sep/2)*yrot(ang)),
|
||||
// ];
|
||||
// attachable(size=[sep+d,d,h], parts=parts){
|
||||
// union(){
|
||||
// left(sep/2) yrot(-ang) cyl(d=d,h=h);
|
||||
// right(sep/2) yrot(ang) cyl(d=d,h=h);
|
||||
// }
|
||||
// children();
|
||||
// }
|
||||
// }
|
||||
// twocyl(d=10,sep=20,h=10)
|
||||
// prism_connector(circle(r=2,$fn=32),
|
||||
// parent_part("left"), RIGHT,
|
||||
// parent_part("right"), LEFT,
|
||||
// fillet=1);
|
||||
|
||||
function parent_part(name) =
|
||||
assert(!is_undef($parent_parts), "Parent does not exist or does not have any parts")
|
||||
let(
|
||||
ind = search([name], $parent_parts, 1,0)[0]
|
||||
)
|
||||
assert(ind!=[], str("Parent does not have a part named ",name))
|
||||
[$transform * $parent_parts[ind][3], $parent_parts[ind][1]];
|
||||
|
||||
|
||||
// Module: restore()
|
||||
// Synopsis: Restores transformation state and attachment geometry from a description
|
||||
// Topics: Transforms, Attachments, Descriptions
|
||||
|
@@ -4360,6 +4360,17 @@ function _prism_fillet_prism(name, basepoly, bot, top, d, k, N, overlap, uniform
|
||||
// tag("remove") cyl(d=75,h=40,$fn=128)
|
||||
// tag("keep") zrot_copies(n=4)
|
||||
// prism_connector(circ,parent(),[-1,.2],parent(),[1,.4],shift1=12,shift2=-12,fillet=2);
|
||||
// Example(3D): You can also make a connection between the "inside" part of a tube and the outside of a tube
|
||||
// diff()
|
||||
// tube(or=10,wall=2,h=10,rounding=1,$fn=40)
|
||||
// let(outside=parent())
|
||||
// attach_part("inside")
|
||||
// tag("remove")
|
||||
// for(where = [LEFT,RIGHT,FRONT,BACK])
|
||||
// prism_connector(circle(3,$fn=100),
|
||||
// outside, where,
|
||||
// parent(), where,
|
||||
// fillet=1);
|
||||
// Example(3D,Med,NoAxes,VPT=[1.42957,2.47871,-3.63111],VPR=[40.3,0,29.2],VPD=263.435): Here we use the {{zrot_copies()}} distributor to create copies of objects and create a connector to a non-symmetrically placed object. All all the connectors are different because we change the anchor point that goes with the second description.
|
||||
// circ = circle(r=3, $fn=64);
|
||||
// right(4)up(25)xrot(15) cyl(r=20,h=30,circum=true,$fn=64) let(cyl=parent())
|
||||
|
@@ -1689,7 +1689,7 @@ module rect_tube(
|
||||
ichamfer2 = _rect_tube_rounding(1/sqrt(2),ichamfer2_temp, chamfer2, irounding2_temp, size2, isize2);
|
||||
anchor = get_anchor(anchor, center, BOT, BOT);
|
||||
parts = [
|
||||
part_geometry("inside", attach_geom(size=[each isize1, h], size2=isize2, shift=shift), inside=true)
|
||||
define_part("inside", attach_geom(size=[each isize1, h], size2=isize2, shift=shift), inside=true)
|
||||
];
|
||||
attachable(anchor,spin,orient, size=[each size1, h], size2=size2, shift=shift, parts=parts) {
|
||||
down(h/2) {
|
||||
@@ -2971,7 +2971,7 @@ module tube(
|
||||
[0,h/2+1]
|
||||
];
|
||||
parts = [
|
||||
part_geometry("inside", attach_geom(r1=ir1, r2=ir2, l=h), inside=true)
|
||||
define_part("inside", attach_geom(r1=ir1, r2=ir2, l=h), inside=true)
|
||||
];
|
||||
attachable(anchor,spin,orient, r1=r1, r2=r2, l=h, parts=parts) {
|
||||
down(h/2) skew(sxz=shift.x/h, syz=shift.y/h) up(h/2)
|
||||
|
Reference in New Issue
Block a user