User Tools


Table of Contents

Other Primitives

Path

A path is a series of points, connected by very thin (invisible) line segments. Paths can be created by giving an array of points, or by calling various functions to create or extend another path.

let path = new CSG.Path2D([[10, 10], [-10, 10]], false);
let anotherpath = new CSG.Path2D([[-10, -10]])
path = path.concat(anotherpath)
path = path.appendPoint([10,-10])
path = path.close()

In addition, a path can be open or closed. An open path is typically used to create another, by appending additional points. A closed path is final, and has a line between first and last points.

Curved paths can also be created. Paths can be created as an arc, or arcs can be appended to an existing path.

let p1 = new CSG.Path2D([[27,-22],[27,-3]],false);
p1 = p1.appendArc([12,-22],{xradius: 15,yradius: -20,xaxisrotation: 0,clockwise: false,large: false});
p1 = p1.close();

Which include:

arc(endpoint, options): return a circular or ellipsoid curved path (see example below for usage).

appendPoint([x,y]): create and return a new Path containing the callee's points followed by the given point.

appendPoints(x,y): return a new Path containing the callee's points followed by the given points

appendBezier(x,y, options): create and return a new Path containing the callee's points followed by a Bezier curve ending at the last point given; all but the last point given are the control points of the Bezier; a null initial control point means use the last two points of the callee as control points for the new Bezier curve.

options can specify {resolution: 55}. Paths can be concatenated with .concat(), the result is a new path.

Note: The difference between Paths and 2D shapes are that Paths are composed of very thin lines, whereas a two dimensional shapes always enclose an area.

Conversion to 2D Shape

Paths can be converted to two dimensional shapes in various forms.

First, a Path can be converted directly to a two dimensional shape but the Path must be closed.

// create a closed path in shape of triangle
let path = new CSG.Path2D([[10,10], [-10,10], [-10,-10]], true)
let shape = path.innerToCAG()

Second, a path can be expanded into a two dimensional shape by tracing the path with a circle. The result is a path with rounded ends and thick lines segments. The path can be either open or closed.

// create an open path in shape of L
let path = new CSG.Path2D([[10,10], [-10,10], [-10,-10]], false)
let shape = path.expandToCAG(3)

Conversion to 3D Shape

Creating a 3D solid is currently supported by the rectangularExtrude() function. This creates a 3D shape by following the path with a 2D rectangle (upright, perpendicular to the path direction):

var path = new CSG.Path2D([ [10,10], [-10,10] ], /* closed = */ false);
var anotherpath = new CSG.Path2D([ [-10,-10] ]);
path = path.concat(anotherpath);
path = path.appendPoint([10,-10]);
path = path.close(); // close the path
 
// of course we could simply have done:
// var path = new CSG.Path2D([ [10,10], [-10,10], [-10,-10], [10,-10] ], /* closed = */ true);
 
// We can make arcs and circles:
var curvedpath = CSG.Path2D.arc({
  center: [0,0,0],
  radius: 10,
  startangle: 0,
  endangle: 180,
  resolution: 16,
});

Text

Text can be used in designs to create the outline of charaters, which can then be used to create either 2D or 3D shapes.

NOTE: Only the ASCII characters can be used today.

Single Characters

A single character can be converted into an outline, i.e. segments. The character height as well as the character position can be requested by specifying options. The height is the height of UPPERCASE characters.

Defaults:

  • xOffset : 0
  • yOffset : 0
  • height : 21
  • extrudeOffset : 0
  • font : 'hershey simplex' (built in)
let char1 = vectorChar('H');
let char2 = vectorChar({ height: 50 }, 'i');

Note: The old function vector_char(x,y,char) is also available but will be deprecated in the V2 API.

The above example only creates the outlines of the characters. In order to convert to something useful, the following can be used to extrude a rectangle along the outlines, i.e. segments of each character. This example creates 3D shapes from the outlines of each character.

let shape1 = csgFromSegments(char1.segments);
let shape2 = csgFromSegments(char2.segments);
 
function csgFromSegments (segments) {
  let output = [];
  segments.forEach(segment => output.push(
    rectangular_extrude(segment, { w:2, h:1 })
  ));
  return union(output);
}

Text Strings

In addition to single characters, complete text strings can be converted to a series of outlines, i.e. segments. Text can be split into multiple lines by including '\n'. And of course, text can be aligned and spaced by specifying options.

Defaults:

  • xOffset : 0
  • yOffset : 0
  • height : 21
  • lineSpacing = 1.4
  • letterSpacing = 1
  • align = 'left'
  • extrudeOffset : 0
  • font : 'hershey simplex' (built in)

Note: The old function vector_text(x,y,string) is also available but will be deprecated in the V2 API.

let text1 = vectorText('OpenJSCAD');
let text2 = vectorText({ yOffset: -90, height: 10, extrudeOffset: 2, input: 'OpenJSCAD\nLOVE<3' });
let text3 = vectorText({ height: 25, align: 'right', lineSpacing: 2.2, extrudeOffset: 2 }, 'OpenJSCAD\nRocks!!');

Using Other Fonts

Other fonts can be used when creating the text outlines. Here are the steps.

  1. Download one or more font files from the fonts/vector directory
  2. Include the font file in the design
  3. Reference the font object
  4. Specify the 'font' in the options to vectorChar() or vectorText()

Note: Font objects append 'Font' to the font name, e.g. camBamStick1Font.

Default:

include('https://www.openjscad.org/fonts/camBamStick1.js');
 
function main (params) {
  let myfont = camBamStick1Font; // font object (! NOTE the "Font" suffix)
  let text = vectorText({font: myfont, height: 5},'OpenJSCAD');
  return csgFromSegments(text);
}
 
function csgFromSegments (segments) {
  let output = [];
  segments.forEach(segment => output.push(
    rectangular_extrude(segment, { w:2, h:1 })
  ));
  return union(output);
}

Special Note: These fonts are NOT TrueType fonts. And TrueType fonts cannot be used today. Look for full TrueType fonts in the V2 API.