Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 backwards compatibility with V1 #1326

Open
gfwilliams opened this issue Mar 16, 2024 · 15 comments
Open

V2 backwards compatibility with V1 #1326

gfwilliams opened this issue Mar 16, 2024 · 15 comments

Comments

@gfwilliams
Copy link

Sorry if this has been dealt with before, but I did have a didn't find it in my searches.

I just came back to an old script that I wanted to make a minor change to, and found it no longer works and there's no obvious way of using it any more. Is there any solution or easy way to 'upgrade' old scripts?

Expected Behavior

Script I made a year ago is still usable on https://openjscad.xyz/:

function main(params) {
return difference(
    union(
      cube({size:[20,20,20],center:true}),
      cube({size:[50,8,8],center:true}).translate([0,0,14]),
      cube({size:[20,8.5,8.5],center:true}).rotateX(45).translate([0,4,10]),
      cube({size:[20,8.5,8.5],center:true}).rotateX(45).translate([0,-4,10]),
      //cylinder({start: [-10,5,10], end: [10,5,10], r: 5, fn: 20}),
      //cylinder({start: [-10,-5,10], end: [10,-5,10], r: 5, fn: 20}),
      cube({size:[10,8,20],center:true}).rotateY(20).translate([9,0,5]),
      cube({size:[10,8,20],center:true}).rotateY(-20).translate([-9,0,5])
     ).expand(2,20),
      cube({size:[20,20,40],center:true}).translate([0,0,-10])
    );
}

Actual Behavior

Script fails with error about exports.main, after that, difference, after fixing that, size for cube not being a number

Steps to Reproduce the Problem

  1. Go to https://openjscad.xyz/:
  2. Try script above and shift+enter to execute

Specifications

@z3dev
Copy link
Member

z3dev commented Mar 16, 2024

@gfwilliams the example is definitely V1, but it must have been from way before as V2 was the only version available at OpenJSCAD.xyz (Apr 2021).

Anyway, V2 has been the main version for a few years now. You should definitely push on as V3 is coming this year.

And sorry, but backwards compatibility was never a goal of V2 because the V1 API was a total wreck.

@z3dev
Copy link
Member

z3dev commented Mar 16, 2024

There are several Discussions about V1 to V2 conversation. Hopefully, those can help.

#883

@gfwilliams
Copy link
Author

Thanks! That's really handy - I'm not sure how many people are like me and had old designs written in V1, but personally it would have been really good if when that error about exports.main appeared it said something like "are you tying to use a V1 file? See #883" - or a note in the README of the repo, or the user guide at https://openjscad.xyz/dokuwiki/doku.php

Also if you google Openjscad one of the top hits for it is https://en.wikibooks.org/wiki/OpenJSCAD_User_Guide which still uses all the V1 syntax and references the old .org URL, which added to my confusion quite a bit.

Obviously this is your project, provided for free - and I really like it. But as a user, there's a certain expectation that if I create something in one version of an tool, it'll be usable in subsequent versions - it's kind of at odds with JavaScript development where generally library developers are very willing to break things to make the overall design better.

Closing as there's obviously a discussion about this already - and I'm not a big user... I just had ~10 designs written in JSCAD v1 and I needed to tweak one of them to 3D print it again - and for that, using one of the older hosted versions will do what I need.

@platypii
Copy link
Contributor

There is a helper shim which can help you to migrate designs from V1 to V2 incrementally. I found this project helpful when I was updating my designs to V2:
https://github.com/jscad-community/jscad-migrate

@gfwilliams
Copy link
Author

Thanks! Yes, that's extremely helpful! Am I right in thinking that's not part of jscad itself though, so I can't use it directly from https://openjscad.xyz with something like require("@jscad-migrate/v1shim")?

It looks like the way I was writing code (with .translate) isn't compatible with that shim (although it looks like it'd be trivial to fix up with Array.prototype.translate = ...).

Is V3 going to be backwards compatible with V2?

@z3dev
Copy link
Member

z3dev commented Mar 19, 2024

Actually, we are currently discussing a new 'shim' for V2 to V3 at #1323
Please leave any thoughts.

@z3dev z3dev reopened this Mar 19, 2024
@gfwilliams
Copy link
Author

Personally it might be nice to move to import { ... } from '@jscad/modeling2' (or similar) such that you could write and target a specific API level, and have it stay the same (I know that's a maintenance headache though).

But if I'm honest, it seems unlikely that OpenJSCAD is the right tool for me.

I love the idea of writing models in JS rather than OpenSCAD, but part of that is the convenience of going to a website and using a language I know. I'll make something, then need to tweak it and re-print it a year or so later, but if things are changing such that I can't load models I'd made previously without having to make modifications to them, most of that convenience is gone.

I'm not saying it's a bad thing - I love what you're doing and it's good you're making things better. However I think for me, I need to write my v1 models to v2, and then in a few months to v3, and probably when I next need to tweak them again I'll need to write for v4 (unless I download and self-host a version) - and it's just not really worth it for me when OpenSCAD appears to be neglected enough that models I wrote 12 years ago still work fine :)

@hrgdavor
Copy link
Contributor

@gfwilliams I have a plan of supporting multiple versions of jscad with some simple annotations, so the new runtime for jscad will support old versions too. That way old scripts can be used even without refactoring, by just adding an annotation in a comment line.

@hrgdavor
Copy link
Contributor

hrgdavor commented Mar 19, 2024

I have a basic jscad example working, no colors yet, and shim is visible in the code for testing

image

I took the code sample from https://neorama.de that is hosting a V1 instance

@hrgdavor
Copy link
Contributor

also tried your code

image

@hrgdavor
Copy link
Contributor

hrgdavor commented Mar 19, 2024

draft candidate for full shim:

const csg = require('@jscad/csg/api.js')
const {circle, square, polygon, triangle} = csg.primitives2d
const {cube, sphere, cylinder, geodesicSphere, torus, polyhedron} = csg.primitives3d
const {union, difference, intersection} = csg.booleanOps
const {translate, center, scale, rotate, transform, mirror, expand, contract, minkowski, hull, chain_hull} = csg.transformations
const {extrudeInOrthonormalBasis, extrudeInPlane, extrude, linear_extrude, rotate_extrude, rotateExtrude, rectangular_extrude} = csg.extrusions
const {css2rgb, color, rgb2hsl, hsl2rgb, rgb2hsv, hsv2rgb, html2rgb, rgb2html} = csg.color
const {sin, cos, asin, acos, tan, atan, atan2, ceil, floor, abs, min, max, rands, log, lookup, pow, sign, sqrt, round} = csg.maths
const {vector_char, vector_text, vectorChar, vectorText} = csg.text

@z3dev
Copy link
Member

z3dev commented Mar 19, 2024

@gfwilliams thanks for the feed back.

Basically, JSCAD has to move forward just because the language and supporting libraries move forward. The JS engines are also moving as well, including Node. So, there's not much choice.

JSCAD has a minimal amount of dependencies which makes maintenance easier. For V3, the big change is import/export, which makes the libraries far more useful to those creating websites, applications, etc.

@hrgdavor has been working on the new web UI, which has transcompiling. And a far better design for the web worker, which can import libraries. This will allow designs to use almost anything, which the demos prove.

The bad news is that V3 uses imports, and has data structure and API changes. The good news is that V2 designs can be supported by a compatibility shim, and transcompling.

As for V1, the overall API is a mess, combining functional and object API. And a interlocking dependencies between everything. But, @hrgdavor likes to support everything so there's still some possibilities.

By the way, there are issues raised against V1 still. Those become WILL NOT FIX.

Hopefully, you can find some time to move some designs to V2 at least. There's probably a few years of support still.

@z3dev z3dev pinned this issue Mar 20, 2024
@gfwilliams
Copy link
Author

@hrgdavor that looks great - it'd be amazing if that could get into the main openjscad.xyz ... even better if as you say there could be some annotations that would then cause that code to be added automatically.

@z3dev thanks! Yes, that makes sense - interesting you're moving to transpiling now.

I'll definitely start trying to use the v2 API for new stuff, and it's nice to think that maybe when v3 comes out, it won't be quite as difficult to update.

@hrgdavor
Copy link
Contributor

@gfwilliams @z3dev @platypii there is a draft implementation WIP
hrgdavor/jscadui#102

test url at https://3d.hrg.hr/jscad/v1-support/

most of the examples from v1 branch work, I tried drag drop each that does not use include and no colors ATM

@hrgdavor
Copy link
Contributor

hrgdavor commented Mar 20, 2024

interesting you're moving to transpiling now.

transpilation is necessary because there is no way to execute ESM scripts that use import.
And the bonus of transpiling is that you can have a folder with ESM script, one with require, and a Typescript file and you can combine them freely when executing in jscad.app ... and it was working before bun made it cool.

sidenote on require:

runtime require can be implemented only with sync IO to block until source is loaded, and then to eval it to extract the module exports. XMLHttpRequest in sync mode is used (it is ok to that in worker, but is not advisable on main thread).

For drag&drop, service worker is used to expose dropped files to the worker via temporary url.

Even though reading files from disk is async, worker thinks files are online on some url .. and again sync XMLHttpRequest is used and require can block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants