2d line length (or perimeter)
-
I have a 2D design
- how do I get the line length of each point and for all lines in the object?
For example I might have a L shape object, and inside that L, 3 small circles.
I need to know the lenght of each side of the L, and the circumference length of all three circles.
- how to get the overall area of my design?
Thanks in advance!
-
You seem to be stuck in the old documentation.
Please see the new website at www.openjscad.xyz, and from the help there are links to the User Guide and the API documentation.
-
@Ashish Hello,
let shapes = require('./lshape.dxf')
should work if your project is into a local directory containing the .dxf file and having its code into index.js. To run it you must open openjscad.xyz and drag the project directory into it.
-
@gilboonet
How do I take a dxf file and put it through this code. replacing the 'shapes' variable with "let shapes = require('./lshape.dxf')" doesnt exactly work. I believe the code is expecting cuboidfor now I have used :
let bounds = measurements.measureBoundingBox(contents)
but not exactly sure what to do with the output. -
Is there a sample I can look at to get access to the Paths?
I need the paths to determine the overall length which is an input to determining the time to execute on the machine and ultimately the cost to print.
I did come across the CSG API which has a measureArea method provided geometries. I was hoping to take the content s of a DXF file (via require('myfile.dxf')) and have it calculate the area. However the current issue is loading hte CSG module doesnt work. I put this at the top of my index.js:
const csg = require('@jscad/csg')
which results in
ERROR:
Uncaught Error: Cannot find module @jscad/csg
Line: 38 -
Hi @Ashish
Did you find some solutions?
The library doesn’t have a function to measure length, but this could be added. Paths have a set of ordered points so the length between points can be calculated. Not sure about 2D geometry but the length of the outlines should be possible.
May I ask what you will do with the length? There are other measurements for area and bounding box.
-
@Ashish Hello, did you look at the examples, there is one called 'Measure Aggregate Bounding Box' that could be helpful to you :
* Measure Aggregate Bounding Box * @category Manipulating Shapes * @skillLevel 10 * @description Examples of measureAggregateBoundingBox function * @tags measurements, bounds, boundingbox, aggregate * @authors Simon Clark * @licence MIT License */ const { cuboid } = require('@jscad/modeling').primitives const { translate, rotate } = require('@jscad/modeling').transforms const { measureAggregateBoundingBox } = require('@jscad/modeling').measurements const { colorize } = require('@jscad/modeling').colors const { subtract } = require('@jscad/modeling').booleans const getParameterDefinitions = () => { return [ { name: 'rotatex', type: 'slider', initial: 0, min: -3.14, max: 3.14, step: 0.01, caption: 'X Rotation:' }, { name: 'rotatey', type: 'slider', initial: 0, min: -3.14, max: 3.14, step: 0.01, caption: 'Y Rotation:' }, { name: 'rotatez', type: 'slider', initial: 0, min: -3.14, max: 3.14, step: 0.01, caption: 'Z Rotation:' } ] } const buildBoundingBox = (bounds) => { // Bounding box format is an array of arrays of values, eg: // [LowerBoundsValues, UpperBoundsValues] // [[left, front, bottom], [right, back, top]] // [[-3, 2, 0], [3, 6, 10]] const top = bounds[1][2] const bottom = bounds[0][2] const left = bounds[0][0] const right = bounds[1][0] const front = bounds[0][1] const back = bounds[1][1] const width = right - left const height = top - bottom const depth = back - front let boundingBox = subtract( cuboid({ size: [width + 1, depth + 1, height + 1] }), cuboid({ size: [width + 1, depth, height] }), cuboid({ size: [width, depth + 1, height] }), cuboid({ size: [width, depth, height + 1] }) ) boundingBox = translate([(left + right) / 2, (front + back) / 2, (top + bottom) / 2], boundingBox) boundingBox = colorize([0.5, 0, 0], boundingBox) return boundingBox } const main = (params) => { let shapes = [ cuboid({ size: [8, 45, 4] }), cuboid({ size: [38, 17, 6], center: [2, -4, 12] }) ] shapes = rotate([params.rotatex, params.rotatey, params.rotatez], shapes) const boundingBox = buildBoundingBox(measureAggregateBoundingBox(shapes)) return [shapes, boundingBox] } module.exports = { main, getParameterDefinitions }