jscad / OpenJSCAD.org

JSCAD is an open source set of modular, browser and command line tools for creating parametric 2D and 3D designs with JavaScript code. It provides a quick, precise and reproducible method for generating 3D models, and is especially useful for 3D printing applications.
https://openjscad.xyz/
MIT License
2.58k stars 507 forks source link

OpenSCAD importer improvements #901

Open apla opened 2 years ago

apla commented 2 years ago

I did some OpenSCAD improvements to generate human readable results.

scad file:

module m3head()
{
    intersection()
    {
        translate([30,5,9])cylinder(r=3.1,h = 25, $fn=30); // head cut
        translate([30,5,17-3.5]) cube([6.2,3.4,1], center=true);
    }
    translate([30,5,17-4]) cube([3.4,3.4,1], center=true);
    translate([30,5,17-3.5])cylinder(r=3.1,h = 20, $fn=30); // head cut
}

m3head()

jscad

onst jscad = require('@jscad/modeling') // modeling comes from the included MODELING library
const { transforms,primitives,booleans } = jscad;
const { intersect,union,subtract } = booleans
const { cylinder,cuboid } = primitives
const { translate,translateX,translateY,translateZ,rotate,rotateX,rotateY,rotateZ } = transforms

function m3head() {
    return [

        intersect(
            translate([30,5,9], cylinder({radius: 3.1, height: 25, center: [0, 0, 12.5], resolution: 30})),
            translate([30,5,13.5], cuboid({size: [6.2,3.4,1]}))
        ),
        translate([30,5,13], cuboid({size: [3.4,3.4,1]})),
        translate([30,5,13.5], cylinder({radius: 3.1, height: 20, center: [0, 0, 10], resolution: 30}))

    ]

}

function main() {

    return m3head();

}

module.exports = {main};

It's still WIP, but allows me to convert some files from Prusa i3 printer repo.

Can't show my fork at this time; instead of using last version from git I've used last version from npm. It will take some time to commit my changes.

This issue just for indication I'm working on this part.

hrgdavor commented 2 years ago

Excellent, let me know if you will need help.

Join discord if you are in a mood to discuss jscad related stuff. Beware, tho, I am rather chatty and opinionated. https://discord.gg/UXtQcA6

hrgdavor commented 2 years ago

not sure if you changed your jscad impl, but current master branch param name for precision is segments

cylinder({radius: 3.1, height: 20, center: [0, 0, 10], resolution: 30}))

-->

cylinder({radius: 3.1, height: 20, center: [0, 0, 10], segments: 30}))
apla commented 2 years ago

No, I haven't changed anything in jscad. I'll fix it.

apla commented 2 years ago

This is my modifications https://github.com/protobor/OpenJSCAD.org/commit/d0e49bebe4b243af984047c7b09947eafdfd9f60

If somebody wants to try it out, I test changes with simple script

// const parser = require('@jscad/openscad-openjscad-translator');

const parser = require('scad-deserializer');

const fs = require('fs').promises;

fs.readFile(process.argv[2], "UTF8").then (openSCADText => {
    const openJSCADResult = parser.parse(openSCADText);
    const jscadFilename = process.argv[2].replace (/\.scad$/, '.jscad');
    return fs.writeFile (jscadFilename, openJSCADResult);
});

as an input file I'm using x-carriage-back.scad from Prusa i3 repo

z3dev commented 2 years ago

Some related issues just in case you feel motivated. 😃

188 #370 #392 #447

apla commented 2 years ago

@z3dev I've already found some bugs in jison file, some valid constructs like one below cannot be processed:

module selective_infill()
mirror([0,1,0]) translate([-50, -33, 0.6])
{ ... }

User needs to wrap module contents with {} like this

module selective_infill() {
mirror([0,1,0]) translate([-50, -33, 0.6])
{ ... }
}

So, jison grammar should be modified. Problem is — output is unreadable. Even slightest modification impossible. I modified the output to return same constructs in JSCAD. Now it lookalike and can be hacked by other people.

Adding diameter for cylinder #392 is trivial, others probably not.

z3dev commented 2 years ago

@apla Did you manage to create a new version of the serializer? Any chance that we can play with it and give some feedback?

apla commented 2 years ago

@z3dev I haven't touched scad converter for a while. Tried to convert some designs from thingiverse, but when those designs have global variables, result is ugly. You can try to play with my fork, but it is incomplete.