roipoussiere / cadquery-vscode

Build parametric 2D/3D CAD models in VSCode with the CadQuery library.
https://open-vsx.org/extension/roipoussiere/cadquery
MIT License
17 stars 4 forks source link

Consider a more robust exec approach #5

Open bernhard-42 opened 2 years ago

bernhard-42 commented 2 years ago

exec fails with simple (an natural) approaches:

import cadquery as cq

l = 1
w = 2
h = 3

def box():
    return cq.Workplane().box(l, w, h).edges().fillet(0.1)

show(box())

fails with

  File "<string>", line 9, in box
NameError: name 'cq' is not defined

So let's change it (just for the test, I would never program like that):

l = 1
w = 2
h = 3

def box():
    import cadquery as cq

    return cq.Workplane().box(l, w, h).edges().fillet(0.1)

show(box())

Now, this fails with

  File "<string>", line 9, in box
NameError: name 'l' is not defined

exec just doesn't play well with global variables

Of course, this works:

def box(l=1, w=2, h=3):
    import cadquery as cq

    return cq.Workplane().box(l, w, h).edges().fillet(0.1)

show(box())

However I would need to adapt my style of coding - especially for quick hacks - to the limitations of exec.

Maybe there is a more robust way?

roipoussiere commented 2 years ago

It should be fixed in the last version of pip package / docker image. Do you want to give a try?

bernhard-42 commented 2 years ago

It behaves pretty strange:

I start with

l = 1
w = 2
h = 3

Then I change l, save, change w, save and change h and save. Looks good up to now. However, when I press save then without changes, the view show kind of a random view of the four views I have created with my process, see animated gif, where I just press save before the view changes.

error

roipoussiere commented 2 years ago

I cannot not reproduce this. Do you use the docker version of cadquery-server or the pip one?

bernhard-42 commented 2 years ago

I use pip. I anyhow have cadquery installed and don't need the whole docker infrastructure on my Mac

bernhard-42 commented 2 years ago

... and the pip version allows to load step files. Not sure this is supported with docker where the interpreter runs in the container

bernhard-42 commented 2 years ago

It would be great if you could get rid of the server component. My viewer approach uses zmq. There is a version for Javascript: https://github.com/zeromq/zeromq.js It seems that the server component in javascript or typescript running in VS Code can receive messages from python zmq lib. Then one would only need a show function that does the tessellation and then sends the tessellated arrays via zmq to VS Code. Just an idea, since for me the server component is as inconvenient as my approach with the separate viewer. And, with the zmq approach, one could use the Python debugger in VS Code to do step-by-step debugging of the code and every now and then call show to check the interims results. Just my 2 cent ...

roipoussiere commented 2 years ago

I use pip. I anyhow have cadquery installed and don't need the whole docker infrastructure on my Mac

Yes, this is totally legit, I just want to understand how to reproduce the bug, I don't have it on the docker version.

Then one would only need a show function that does the tessellation and then sends the tessellated arrays via zmq to VS Code.

The solution with zmq is interesting. I made the choice of the server part because of these two benefits:

The first point could be eventually solved with https://github.com/CadQuery/cadquery/issues/1048. I don't see how to adress the second point with the zmq architecture. Do you?

bernhard-42 commented 2 years ago

avoid complex installation

I agree, installing CadQuery is not always fun. I have spent quite some time on it in the past. So the docker approach is good, but is a little bit inflexible: CadQuery doesn't have a short term release strategy (2.1. is more than 1 year old and everyone works with the latest master) and with your docker container I am restricted to the version you provide. I cannot quickly pip install the git repo (If OCP doesn't change, pip of Cadquery repo is sufficient).

By the way, can I load e.g. step files with the docker version? Or save STL results for my 3d printer?

I personally don't really trust the exec approach in Python due to the locals, globals handling. To me it feels like one can mess up things easily with unclear side effects. Maybe it is just because I don't understand it enough ...

reduce the rendering delay: since the cadquery server already loaded the cadquery module.

I understand this issue. I solved it in my workflow by working in Jupyter. I import cadquery only once for a session and even better, really complex interims results can be calculated once and then used in the cells below without recalculation. Big benefit for my workflows. I don't want to open the IDE vs. Notebook discussion. Just explain my approach to avoid the loading times (latency is an issue in general, so my latest version of Jupyter CadQuery - nearly ready - will have caching for bounding box calculations and tessellation results and uses multiprocessing for large CAD assemblies. That is a much bigger performance booster than avoiding the import of cadquery. Note this is only available in Jupyter-CadQuery not in three-cad-viewer).

But agreed, it is a difficult decision, and one has to go one route ...

roipoussiere commented 2 years ago

By the way, can I load e.g. step files with the docker version? Or save STL results for my 3d printer?

Yes, using a docker volume: docker run --rm -d -v $(pwd):/home/cq -p 5000:5000 cadquery/cadquery-server, but I agree it's not very practical. But anyway cadquery can now be installed with pip, so the use of docker is not really necessary.

I agree that the server and exec approach is not perfect.

I could eventually give the choice between using the cadquery server or not.

bernhard-42 commented 2 years ago

One way to overcome the latency is to use "shift-enter" approach in VS Code. Instead of just saving (very convenient with cadquery-server), one could use Ctrl-A + Shift-Enter (or Cmd-A + Shift-Enter on the Mac). This will incrementally run the code in VS Code in the attached Python interpreter. Not as easy as your cadquery-server approach, but no exec needed and same speed. Additionally, one could leverage pre-calculated objects by reusing the variables and just selecting the rest of the code before pressing Shift Enter. Again, no optimal solution, however you had asked about ideas how to avoid the import latency.

However, both the "cadquery-server" and the "shift-enter" approach do not allow step-by-step debugging.

bernhard-42 commented 2 years ago

At least from a debugging perspective, I don't feel that the import cadquery is a big issue (at least on my laptop, which admittedly is a fast machine)

debug

Also a "Run without Debugging" is really fast on my laptop:

run

Given that experience, on my laptop I definitely would prefer to work without cadquery-server and be able to properly debug my code step by step

roipoussiere commented 2 years ago

I made some work on this!

It consists on a huge change on the cadquery-server behavior.

The exec approach is not used anymore, instead the server executes the CadQuery code and renders the model using three-cad-viewer. Finally, it includes a file watcher that reloads the Python code and update the web page when the file is updated:

cq-server_v0-2-1_demo.webm

This approach allows to debug the code in VSCode. It also makes the project compatible with any IDE and any web browser. And also allow a user to display the model in an other screen, or even in an other computer on the same local network.

In VSCode, the web page can be displayed using LivePreview extension (ctrl+shift+P -> Simple Browser: Show). This might turn the whole VSCode extension deprecated, as all the job is made by cq-server.

I moved it in a dedicated repository and can be installed with pip install cadquery-server (it doesn't include cadquery itself, which is an extra-dependency).

Do you want to give a try? :)

bernhard-42 commented 1 year ago

Hi @roipoussiere having been away on vacation for the last two weeks, I how wanted to give it a try. Unfortunately minify-html 0.10.0 doesn't seem to exist for Mac M1, only 0.9.2. So I haven't managed to install it. Is 0.10.0 necessary?

bernhard-42 commented 1 year ago

https://github.com/wilsonzlin/minify-html/commit/29e7b1c18705cbb534c2d8cabe11f66731524ac0 doesn't sound promising

bernhard-42 commented 1 year ago

Cloned it and gave it a try:

roipoussiere commented 1 year ago

So I haven't managed to install it. Is 0.10.0 necessary?

I think you can use minify html 0.9.2. And it's only used for export when --minify option is passed to cli.

I don't understand how I could use step-by-step debugging in VS Code

In fact I realized it's not possible currently, the issue has been reported here: https://github.com/roipoussiere/cadquery-server/issues/27. The idea is to actually execute the script when using the debugger, instead of reloading it using cq-server.

bernhard-42 commented 1 year ago

@roipoussiere Just for information: The main reason for me to switch to and IDE like VSCode is step-by-step debugging. I have now created my own VSCode extension, which is loosely coupled between Python and the Typescript VSCode extension: The extension starts a web server on port 3939 and the Python show command sends tessellated objects to this web server via HTTP POST. No external server that needs to be run separately.

Both vsix and wheel can be found in https://github.com/bernhard-42/vscode-cadquery-viewer/releases at the moment.

This gif shows both, using "Run in Python Terminal" (very fast for small experimental code snippets) and proper "Debugging" with VSCode debugger:

cq-vscode