Quotes/quotes.scad

180 lines
6.8 KiB
OpenSCAD

//////////////////////////////////////////////////////////////////////
// LibFile: quotes.scad
// Includes:
// include <BOSL2/std.scad>;
// include <quotes.scad>;
// FileGroup: Quotes
// FileSummary: Quotes,Dimensions
//////////////////////////////////////////////////////////////////////
include <BOSL2/std.scad>
quotes_debugging = false;
// Module: quote()
//
// Synopsis: Draws a dimension line with optional text labeling the length.
// Topics: Dimensioning, Annotation
// Description:
// Creates a labeled dimension line with extension lines and an optional
// length label. The dimension can be customized in terms of placement,
// orientation, text formatting, and appearance.
// Arguments:
// length = The length of the dimension line.
// textSize = Size of the text label [default: 60].
// offsetFromOrigin = Distance of the dimension line from the reference [default: textSize].
// extendBeyondDimLines = Length beyond the extension lines [default: textSize / 5].
// textOffset = Offset of the text from the dimension line [default: textSize / 3].
// color = Color of the dimension line and text [default: "Red"].
// placement = Position relative to the model (TOP or BOTTOM) [default: TOP].
// strokeWidth = Thickness of the lines [default: 1].
// font = Font used for text [default: "Saira Stencil One"].
// orient = Text orientation (TOP or BOTTOM) [default: TOP].
// anchor = Text alignment reference (CENTER, LEFT, RIGHT) [default: CENTER].
// spin = Spin.
// Example(3D,ColorScheme=Nature): Simple quote
// quote(length=100, textSize=50);
// Example(3D,ColorScheme=Nature): Spin -90
// quote(length=100, textSize=50,spin=-90);
// Example(3D,ColorScheme=Nature): Orient FRONT
// quote(length=100, textSize=50,orient=FRONT);
// Example(3D,ColorScheme=Nature): Orient TOP
// quote(length=100, orient=TOP);
// Example(3D,ColorScheme=Nature): offsetFromOrigin 20
// quote(length=100, offsetFromOrigin=20);
// Example(3D,ColorScheme=Nature): extendBeyondDimLines 20
// quote(length=100, extendBeyondDimLines=20);
module quote(
length,
textSize = 40,
offsetFromOrigin = undef,
extendBeyondDimLines = undef,
textOffset = undef,
color = "Red",
placement = TOP,
strokeWidth = 0.2,
font = "Roboto Condensed", //"Saira Stencil One",
orient = TOP,
anchor = CENTER,
spin = 0
) {
// Default values if undefined
_textOffset = (textOffset != undef) ? textOffset : textSize / 3;
_extendBeyondDimLines = (extendBeyondDimLines != undef) ? extendBeyondDimLines : textSize / 5;
_offsetFromOrigin = (offsetFromOrigin != undef) ? offsetFromOrigin : textSize;
// Calculate total height including text and extension lines
totalHeight = _offsetFromOrigin + abs(_extendBeyondDimLines) + textSize;
// Size for attachable bounding box
size = [length, totalHeight, strokeWidth];
attachable(anchor, spin, orient, size=size) {
// Compute shift based on anchor point
shift =
anchor == RIGHT ? length/2 :
anchor == LEFT ? -length/2 : 0;
// Adjust placement offset
actualOffset = (placement == BOTTOM) ? -_offsetFromOrigin : _offsetFromOrigin;
extOffset = (placement == BOTTOM) ? -_extendBeyondDimLines : _extendBeyondDimLines;
// Draw the dimension
recolor(color) {
//zrot(angle)
left(shift) {
arrowed_line([-length / 2, actualOffset, 0], [length / 2, actualOffset, 0],width=strokeWidth); // Parallel dimension line
ext_line([-length / 2, 0, 0], [-length / 2, actualOffset + extOffset, 0],width=strokeWidth); // Left extension line
ext_line([length / 2, 0, 0], [length / 2, actualOffset + extOffset, 0],width=strokeWidth); // Right extension line
back(actualOffset + _textOffset)
linear_extrude(0.1)
text(str(length), size = textSize, font = font, halign = "center"); // Label
}
}
children();
}
}
// Module: arrowed_line()
//
// Synopsis: Draws a straight line with arrowheads at both ends.
// Topics: Geometry, Arrows, Lines
// Description:
// Creates a linear extrusion representing a line with arrowheads at both ends.
// The line thickness, length, and arrow size can be customized.
// Arguments:
// point1 = [x, y] start point of the line.
// point2 = [x, y] end point of the line.
// width = Thickness of the line [default: 0.1].
// endcap_width = Width of the arrowhead at each end [default: 15].
// Example(3D,ColorScheme=Nature):
// arrowed_line([0,0], [50,50], width=0.5, endcap_width=5);
module arrowed_line( p1, p2 , width=0.5, endcap_width = 15) {
dir = p2 - p1;
angle = atan2(dir.y, dir.x);
length = norm(dir);
// Draw the line
translate(p1)
rotate([0, 0, atan2(p2.y - p1.y, p2.x - p1.x)])
translate([length / 2, 0])
linear_extrude(height = width)
square([length, width], center = true);
// Arrowhead shape
arrow = [
[ 0, 0],
[ -endcap_width, -endcap_width / 2 ],
[ -endcap_width, +endcap_width / 2 ]
];
// Draw arrowheads
for (pt = [p1, p2])
translate(pt)
rotate(angle + (pt == p1 ? 180 : 0))
linear_extrude(height = width)
polygon(arrow);
}
// Module: ext_line()
//
// Synopsis: Draws a straight extruded line between two points.
// Topics: Geometry, Lines
// Description:
// Creates a linear extrusion representing a straight line segment with a
// specified width between two given points.
// Arguments:
// point1 = [x, y] start point of the line.
// point2 = [x, y] end point of the line.
// width = Thickness of the line [default: 0.1].
// Example(3D,ColorScheme=Nature):
// ext_line([0,0], [50,50], width=0.5);
module ext_line(p1, p2, width = 0.5) {
dir = p2 - p1;
angle = atan2(dir.y, dir.x);
length = norm(dir);
// Draw the line
translate(p1)
rotate([0, 0, atan2(p2.y - p1.y, p2.x - p1.x)])
translate([length / 2, 0])
linear_extrude(height = width)
square([length, width], center = true);
}
if ( quotes_debugging ) {
quote(
500,
textSize=80,
strokeWidth=2,
orient=UP,
spin=-90,
anchor=CENTER
);
left (600) quote(500);
}