JSCAD User Group
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    2d line length (or perimeter)

    Scheduled Pinned Locked Moved General Discussions
    7 Posts 3 Posters 1.5k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • AshishA Offline
      Ashish
      last edited by

      I have a 2D design

      1. 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.

      1. how to get the overall area of my design?

      Thanks in advance!

      gilboonetG z3devZ 2 Replies Last reply Reply Quote 0
      • gilboonetG Offline
        gilboonet @Ashish
        last edited by

        @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 }
        
        
        AshishA 1 Reply Last reply Reply Quote 0
        • z3devZ Offline
          z3dev @Ashish
          last edited by z3dev

          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.

          AshishA 1 Reply Last reply Reply Quote 0
          • AshishA Offline
            Ashish @z3dev
            last edited by Ashish

            @z3dev

            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

            z3devZ 1 Reply Last reply Reply Quote 0
            • AshishA Offline
              Ashish @gilboonet
              last edited by

              @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 cuboid

              for now I have used :
              let bounds = measurements.measureBoundingBox(contents)
              but not exactly sure what to do with the output.

              gilboonetG 1 Reply Last reply Reply Quote 0
              • gilboonetG Offline
                gilboonet @Ashish
                last edited by z3dev

                @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.

                1 Reply Last reply Reply Quote 0
                • z3devZ Offline
                  z3dev @Ashish
                  last edited by

                  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.

                  1 Reply Last reply Reply Quote 0

                  Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                  Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                  With your input, this post could be even better đź’—

                  Register Login
                  • First post
                    Last post
                  Powered by NodeBB | Contributors