# util

# Functions

identity(solid)object

A function that reutrns the first argument. Useful when passing in a callback to modify something, and you want a default functiont hat does nothing.

result(object, f)object

If f is a funciton, it is executed with object as the parameter. This is used in CSG.unionIf and CSG.subtractIf, allowing you to pass a function instead of an object. Since the function isn't exeuted until called, the object to union or subtract can be assembled only if the conditional is true.

defaults(target, defaults)object

Returns target object with default values assigned. If values already exist, they are not set.

print(msg, o)

Print a message and CSG object bounds and size to the conosle.

depreciated(method, error, message)

Shows a warning or error message. Used to indicate a method has been depricated and what to use instead.

inch(x)Number

Convert an imperial inch to metric mm.

cm(x)Number

Convert metric cm to imperial inch.

segment(object, segments, axis)Array

Returns an array of positions along an object on a given axis.

map(o, f)array

Object map function, returns an array of the object mapped into an array.

size(o)CSG.Vector3D

Returns a Vector3D with the size of the object.

scale(size, value)number

Returns a scale factor (0.0-1.0) for an object that will resize it by a value in size units instead of percentages.

enlarge(object, x, y, z)CSG

Enlarge an object by scale units, while keeping the same centroid. For example enlarge(mycsg, 1, 1, 1) enlarges object mycsg by 1mm in each axis, while the centroid stays the same.

fit(object, x, y, z, keep_aspect_ratio)CSG

Fit an object inside a bounding box. Often used with text labels.

flush(moveobj, withobj, axis, mside, wside)CSG

Moves an object flush with another object

calcmidlineTo(o, axis, to)Array.<Number>

Calculates the transform array to move the midline of an object by value. This is useful when you have a diagram that provides the distance to an objects midline instead of the edge.

getDelta(size, bounds, axis, offset, [nonzero])Point

Given an size, bounds and an axis, a Point along the axis will be returned. If no offset is given, then the midway point on the axis is returned. When the offset is positive, a point offset from the mininum axis is returned. When the offset is negative, the offset is subtracted from the axis maximum.

bisect(object, axis, [offset], [angle], rotateaxis, rotateoffset, options)JsCadUtilsGroup

Cut an object into two pieces, along a given axis. The offset allows you to move the cut plane along the cut axis. For example, a 10mm cube with an offset of 2, will create a 2mm side and an 8mm side.

Negative offsets operate off of the larger side of the axes. In the previous example, an offset of -2 creates a 8mm side and a 2mm side.

You can angle the cut plane and position the rotation point.

bisect example

slice(object, angle, axis, rotateaxis, options)JsCadUtilsGroup

Slices an object with the cutting plane oriented through the origin [0,0,0]

wedge(object, start, end, axis)JsCadUtilsGroup

Creates a JsCadUtilsGroup object that has body and wedge objects. The wedge object is created by radially cutting the object from the start to the end angle.

wedge example

stretch(object, axis, distance, offset)CSG

Wraps the stretchAtPlane call using the same logic as bisect.

poly2solid(top, bottom, height)CSG

Takes two CSG polygons and createds a solid of height. Similar to CSG.extrude, excdept you can resize either polygon.

addConnector(object, name, point, point, point)CSG

# identity(solid) ⇒ object

A function that reutrns the first argument. Useful when passing in a callback to modify something, and you want a default functiont hat does nothing.

Kind: global function
Returns: object - the first parameter passed into the function.

Param Type Description
solid object an object that will be returned

# result(object, f) ⇒ object

If f is a funciton, it is executed with object as the parameter. This is used in CSG.unionIf and CSG.subtractIf, allowing you to pass a function instead of an object. Since the function isn't exeuted until called, the object to union or subtract can be assembled only if the conditional is true.

Kind: global function
Returns: object - the result of the function or the object.

Param Type Description
object object the context to run the function with.
f function | object if a funciton it is executed, othewise the object is returned.

# defaults(target, defaults) ⇒ object

Returns target object with default values assigned. If values already exist, they are not set.

Kind: global function
Returns: object - Target object with default values assigned.
Depricated:

Param Type Description
target object The target object to return.
defaults object Defalut values to add to the object if they don't already exist.

Print a message and CSG object bounds and size to the conosle.

Kind: global function
Depricated: use Debug instead

Param Type Description
msg String Message to print
o CSG A CSG object to print the bounds and size of.

# depreciated(method, error, message)

Shows a warning or error message. Used to indicate a method has been depricated and what to use instead.

Kind: global function

Param Type Description
method string The name of the method being depricated.
error boolean Throws an error if called when true.
message string Instructions on what to use instead of the depricated method.

# inch(x) ⇒ Number

Convert an imperial inch to metric mm.

Kind: global function
Returns: Number - Result in mm

Param Type Description
x Number Value in inches

# cm(x) ⇒ Number

Convert metric cm to imperial inch.

Kind: global function
Returns: Number - Result in inches

Param Type Description
x Number Value in cm

# segment(object, segments, axis) ⇒ Array

Returns an array of positions along an object on a given axis.

Kind: global function
Returns: Array - An array of segment positions.

Param Type Description
object CSG The object to calculate the segments on.
segments number The number of segments to create.
axis string Axis to create the sgements on.

# map(o, f) ⇒ array

Object map function, returns an array of the object mapped into an array.

Kind: global function
Returns: array - an array of the mapped object.

Param Type Description
o object Object to map
f function function to apply on each key

# size(o) ⇒ CSG.Vector3D

Returns a Vector3D with the size of the object.

Kind: global function
Returns: CSG.Vector3D - Vector3d with the size of the object

Param Type Description
o CSG A CSG like object or an array of CSG.Vector3D objects (the result of getBounds()).

# scale(size, value) ⇒ number

Returns a scale factor (0.0-1.0) for an object that will resize it by a value in size units instead of percentages.

Kind: global function
Returns: number - Scale factor

Param Type Description
size number Object size
value number Amount to add (negative values subtract) from the size of the object.

# enlarge(object, x, y, z) ⇒ CSG

Enlarge an object by scale units, while keeping the same centroid. For example enlarge(mycsg, 1, 1, 1) enlarges object mycsg by 1mm in each axis, while the centroid stays the same.

Kind: global function
Returns: CSG - [description]

Param Type Description
object CSG [description]
x number [description]
y number [description]
z number [description]

# fit(object, x, y, z, keep_aspect_ratio) ⇒ CSG

Fit an object inside a bounding box. Often used with text labels.

Kind: global function
Returns: CSG - [description]

Param Type Description
object CSG [description]
x number | array [description]
y number [description]
z number [description]
keep_aspect_ratio boolean [description]

# flush(moveobj, withobj, axis, mside, wside) ⇒ CSG

Moves an object flush with another object

Kind: global function
Returns: CSG - [description]

Param Type Description
moveobj CSG Object to move
withobj CSG Object to make flush with
axis String Which axis: 'x', 'y', 'z'
mside Number 0 or 1
wside Number 0 or 1

# calcmidlineTo(o, axis, to) ⇒ Array.<Number>

Calculates the transform array to move the midline of an object by value. This is useful when you have a diagram that provides the distance to an objects midline instead of the edge.

Kind: global function
Returns: Array.<Number> - The tranform array needed to move the object.

Param Type Description
o CSG A CSG object.
axis String A string with the axis to operate on.
to Number Value to move the midline of the object to.

# getDelta(size, bounds, axis, offset, [nonzero]) ⇒ Point

Given an size, bounds and an axis, a Point along the axis will be returned. If no offset is given, then the midway point on the axis is returned. When the offset is positive, a point offset from the mininum axis is returned. When the offset is negative, the offset is subtracted from the axis maximum.

Kind: global function
Returns: Point - The point along the axis.

Param Type Description
size Size Size array of the object
bounds Bounds Bounds of the object
axis String Axis to find the point on
offset Number Offset from either end
[nonzero] Boolean When true, no offset values under 1e-4 are allowed.

# bisect(object, axis, [offset], [angle], rotateaxis, rotateoffset, options) ⇒ JsCadUtilsGroup

Cut an object into two pieces, along a given axis. The offset allows you to move the cut plane along the cut axis. For example, a 10mm cube with an offset of 2, will create a 2mm side and an 8mm side.

Negative offsets operate off of the larger side of the axes. In the previous example, an offset of -2 creates a 8mm side and a 2mm side.

You can angle the cut plane and position the rotation point.

bisect example

Kind: global function
Returns: JsCadUtilsGroup - Returns a group object with a parts object.

Param Type Default Description
object CSG object to bisect
axis string axis to cut along
[offset] number offset to cut at
[angle] number angle to rotate the cut plane to
rotateaxis string
rotateoffset number
options Object
[options.addRotationCenter] boolean false
[options.cutDelta] Array
[options.rotationCenter] CSG.Vector3D

# slice(object, angle, axis, rotateaxis, options) ⇒ JsCadUtilsGroup

Slices an object with the cutting plane oriented through the origin [0,0,0]

Kind: global function
Returns: JsCadUtilsGroup - A group with a positive and negative CSG object.

Param Type Description
object CSG The object to slice.
angle number The slice angle.
axis 'x' | 'y' | 'z' The slice axis.
rotateaxis 'x' | 'y' | 'z' The rotation axis of the slice.
options object The slice angle.
[options.color] boolean Colors the slices.
[options.addRotationCenter] boolean Adds a unitAxis object at the rotation center to the group.
[options.rotationCenter] CSG.Vector3D The location of the rotation center, defaults to [0,0,0].

# wedge(object, start, end, axis) ⇒ JsCadUtilsGroup

Creates a JsCadUtilsGroup object that has body and wedge objects. The wedge object is created by radially cutting the object from the start to the end angle.

wedge example

Kind: global function
Returns: JsCadUtilsGroup - A group object with a body and wedge parts.

Param Type Description
object CSG The object to slice.
start number The starting angle to slice.
end number The ending angle of the slice.
axis 'x' | 'y' | 'z' The axis to cut the wedge in.

Example

util.init(CSG);
var wedge = util.wedge(
  CSG.cube({
    radius: 10
  }),
  30,
  -30,
  'x'
);

return wedge
  .map((part, name) => {
    if (name == 'wedge') return part.translate([0, 5, 0]);
    return part;
  })
  .combine();

# stretch(object, axis, distance, offset) ⇒ CSG

Wraps the stretchAtPlane call using the same logic as bisect.

Kind: global function
Returns: CSG - The stretched object.

Param Type Description
object CSG Object to stretch
axis String Axis to streatch along
distance Number Distance to stretch
offset Number Offset along the axis to cut the object

# poly2solid(top, bottom, height) ⇒ CSG

Takes two CSG polygons and createds a solid of height. Similar to CSG.extrude, excdept you can resize either polygon.

Kind: global function
Returns: CSG - generated solid

Param Type Description
top CAG Top polygon
bottom CAG Bottom polygon
height number heigth of solid

# addConnector(object, name, point, point, point) ⇒ CSG

Kind: global function
Returns: CSG - The CSG object with the new connector added.

Param Type Default Description
object CSG The object to add the connector to.
name String The name of the connector, this is added to the object properties.
point Array [0,0,0 a 3 axis array defining the connection point in 3D space.
point Array [0,0,0 a 3 axis array defining the direction vector of the connection (in the case of the servo motor example it would point in the direction of the shaft).
point Array [0,0,0 a 3 axis array direction vector somewhat perpendicular to axis; this defines the “12 o'clock” orientation of the connection.