Hacker News new | past | comments | ask | show | jobs | submit login
CadQuery: A Python parametric CAD scripting framework based on OCCT (github.com/cadquery)
134 points by OJFord on Sept 18, 2020 | hide | past | favorite | 46 comments



Big fan of Cadquery! I used it to generate circuit board enclosures in an old project of mine[1]. Once you have it set up it is much nicer to use than Openscad and you have the ability to export STEP files, which is pretty huge. I haven't played with it in a while but the new improvements look great. The maintainers are also very welcoming/helpful to newcomers. Overall a great open source project.

[1] https://enclosuregenerator.com/


Off-topic: I’ve seen a few posts about CAD programs recently, and it reminded me of a product that I want but haven’t found yet.

I often need to draw 2D vector graphics for digital or print. Since I suck a bezier curves (and they feel imprecise anyway) and manually arranging and combining geometric shapes can be tedious I want an interface for drawing that mimics the sketch function of SolidWorks or Fusion360 and the like with the constraint system and so on.


I completely agree with this! I used Solidworks heavily for years and am so accustomed to drawing some lines, adding some constraints and dimensions with custom hot keys and boom I have my drawing nice neat and tidy exactly as I need it in no time.

These days I don’t do any 3D modeling but I’ll need to jump into Inkscape every now and then to do some graphic design or 2D UI mockup type stuff and I wish I could easily replicate the Solidworks drawing workflow.

I’ve found that I can get pretty close to the workflow I’m looking for in FreeCAD, but then I don’t have any of the artistic stuff I need. And obviously Inkscape has all the artistic stuff, but building a drawing with constraints and dimensions isn’t possible...or certainly not easy at least.


I have also long desired something like this. I'd love a parametric vector (+ image) editor; it's an unfilled niche. (If any startup people are looking for ideas, here's your million dollar opportunity.)

There are the CAD programs, like SolveSpace/AutoCad/QCAD, but they aren't really designed for illustration: you would have to constantly export the blueprint to style it. I think the closest you can get now is UI design software with constraint features, but that software isn't really made for free-form work like Illustrator/Inkscape.

This discussion may be of interest to you, though it is not UI-based and none of the software has been open-sourced (yet): https://news.ycombinator.com/item?id=21774018

EDIT: Looks like Inkscape can do a few vaguely CAD-like things with "live path effects", but it's by no means a proper constraint solver. See the feature request here for a full constraint solver, which includes a summary of past progress and proposals: https://gitlab.com/inkscape/inbox/-/issues/1465


Just curious: in what sense is that startup niche not yet filled by any of the usual 3d parametric CAD programs like Rhino etc.?


The full blown 3D CAD programs _do_ have easily definable dimensions and a constraint solver for drawing sketches. The problem is that in these softwares, it’s typically assumed those sketches are going to be used to make a 3D part. So you’ll extrude the sketch along a line or sweep the sketch around an axis or something to create a 3D part. Consequently, what you can do artistically with the sketch ends there.*

I’m not looking for that 3D aspect of the software. Once I have the sketch dimensioned and constrained, I want to start doing artistic stuff with it like changing the line style or color, adding a gradient fill, adding text inside it—-stuff you’d use Inkscape and the like to do. But Inkscape doesn’t have the constraint solver or easy to use dimensioning tool.

*To be fair, you could probably export the line after fully defining it in CAD and import it into Inkscape, but that’s not much fun.


FreeCAD has an sketch [1] workbench with constrains. Once it's draw, you can extrude it, pad it, apply the geometric boolean operations, add/substract to create complex parts [2] from the result.

[1] https://wiki.freecadweb.org/Basic_Sketcher_Tutorial

[2] https://wiki.freecadweb.org/PartDesign_Workbench


This almost made me stop working on my own openscad inspired system in lua. It has almost everything that I think is missing from openscad: * A full programming language * Fillet and chamfer support * A better viewer

The thing that I'm planning to add to my lua cad is materials and PBR in the viewer. Also, I'm still thinking if native bom support is a good idea.

But this looks slick, will definitely give it a try. Python models should be shorter than lua. But I like the lua dsl so far.

Also I'm attempting to write my own cad kernel, which is a dubious choice. But will see where that gets me.


Damn I didn’t realize OpenSCAD didn’t have filet/chamfer! Lua certainly has its advantages over python. The dubious choices are usually the most fun even if you end up in the deep end though.


Yep. Lua has a bunch of advantages even over python.

* It can be embedded in the viewer, so the user doesn't have to install a separate distribution of libraries. Only the viewer.

* It should be faster than python, even without luajit. Currently everything is done in lua, but I'm planning to write the main loop (converting the object graph into meshes and the rendering) into Rust. And have lua dsl fill in some arrays via C FFI.

* Lua has a special syntax for function calls that pass an object. Ex: do_something(obj) can be written as do_something { field = value }. This and some partial application can be used to make some very nice DSLs. Ex:

difference ( sphere { r = 3 }, cube { w = 2, h = 2, d = 2 } )

* Lua is a very simple language, which can be easily picked up. That being said, Python as used in CadQuery is also simple.

And yes, fillet and chamfer seem to be missing openscad. There is a way to do a 2d shape then add rounded corners then extrude it. But that seems to be new, and it's a bit tricky. I'm thinking about adding a fillet modifier that finds all the sharp edges in an object and rounds them. It might be more difficult than it sounds, but, will see. Fillets and chamfers are also important for visualisations, since in nature there are no perfect sharp corners afaik.


The big advantage of Python over Lua is that the ecosystem is bigger and more mature.

You can use SymPy for constraint modeling, for example.


I wish more people would try this over openscad. I like openscad but the UI and with cadquery you use python! and now I noticed "CadQuery supports Jupyter notebook out of the box"


I actually like the GUI way of designing in FreeCAD/Fusion 360 (which I wouldn't say about much) but for me it's about the file format on disk. I would love it if they had a sane human-readable format, git-friendly. I appreciate that wouldn't be trivial.

So I'm really interested in learning something like OpenSCAD or CadQuery, even if only for files I can use and read anywhere.


I managed to integrate Jupyter notebook with FreeCAD to get some of both worlds. In the future I'll try to put CadQuery into that mix, also.


thing is freecad or openscad are not good at ergonomics, to the point that editing code is .. not far


The jupyter-cadquery extension renders models with three.js via pythreejs in a sidebar with jupyterlab-sidecar: https://github.com/bernhard-42/jupyter-cadquery#b-using-a-do...

https://github.com/bernhard-42/jupyter-cadquery/blob/master/...


Another open source alternative in the wake of the Fusion 360 debacle it seems.

I will have a look. I'm no CAD expert and I have tried FreeCAD but it seems it's almost there but not yet. My use cases are very simple and this may be enough but nevertheless I think a GUI is the way to go for these kind of applications.


I avoided it from the start. I never checked if it was available for Linux, but in general I prefer software that is free from the start.

Sort of a "Show your work" kind of attitude. Theoretically I could assemble my software stack (almost) without any external licenses or closed source code.


What is the Fusion 360 debacle?


A few days ago they announced changes to the free/hobbyist version - over the past year or so they've crippled some things and overall there's a bit of an off-putting vibe towards hobby users.

I used Fusion360 a ton for 3D printing and overall it was really nice (it has its warts but it's a pretty amazing app); the past few days I've started looking into alternatives unfortunately.


This is really interesting and compelling to me. I tried FreeCAD recently and couldn't get into it, and went back to onshape. I notice that it doesn't appear to have constraints in it, which is usually a significant part of the CAD workflow. What does one do about that?


Doesn't it being Python (any programming language - OpenSCAD/declaracad too) basically give you constraints for free?

I haven't actually played around with it yet, but surely it's just a matter of combining (or computing from) your (python) variables appropriately? And even having the ability to `assert x > 10` or whatever to validate allowable parameter ranges.


Most of the graphical 3D CAD programs include a constraints solver that work over the domain of real numbers. A quick search doesn't turn up such a python library.

https://wiki.freecadweb.org/Sketcher_Workbench

https://forum.freecadweb.org/viewtopic.php?t=17868

https://en.wikipedia.org/wiki/CLP%28R%29


Ah sure, I was thinking quite loosely about 'constraints'. I think you can get pretty far without a solver, with a dependency order in a procedural language like python. Of course it's not as expressive, but I think it goes a long way, and just isn't possible or at least wouldn't be natural in GUI CAD.

e.g. Scripted CAD pseudocode:

    x = param('length')
    y = param('width')

    z = 2*x + y

    base = rect(x, y)
    box = base.extrude(z)

    radius = y/2 if y < x else x/2
    cutout = circle(radius=radius, centre=(radius+base.x, radius+base.y)).extrude(z)

    obj = box - cutout
The circle cutout inherently has tangent constraints on the sides of the box, without needing library support or a solver.

I realise there are limits, I just think you could do a lot without hitting them, and when you do.. you're just programming, find a constraint solver to use.


Make a triangle with side lengths 7,8, and 9.

Also, one doesnt usually just find a constraint solver to use. You'll want a geometric constraint solver which will be built on top of a non-linear algebraic constraint solver. It really needs to be integrated into the CAD system.


> Make a triangle with side lengths 7,8, and 9.

    p, q, r = params("side1", "side2", "side3")

    pq = arccos((p^2+q^2-r^2)/(2*p*q))
    pr = # ... similar
    qr = # ... similar

    assert pq + pr + qr == 360, "Side lengths invalid"
    pside = line((0,0), (p,0))
    xy_qr = (r*cos(180-pr), r*sin(180-pr))
    qside = line((p,0), xy_qr)
    rside = line(xy_qr, (0,0))

    triangle = sketch(pside, qside, rside)
Or whatever this fictional API is to look like.

Obviously to do generic constraint solving, you need a generic constraint solver. My point is that if you have fixed known constraints, and a programming language, you can just compute what you want explicitly.


You could take a look at simpy.readthedocs.io, which provides some ways to handle constraints.


There's also declaracad, similar idea but seems to be in an earlier stage of development: https://github.com/codelv/declaracad


I don't really have experience with the architecture of CAD applications. What's the relationship between a CAD program and the OpenCascade kernel? Since both this and FreeCAD use OCCT are files compatible between them?


Open CASCADE is the geometry kernel that does all the heavy lifting (all the geometry related operations and mathematics etc). FreeCAD is a primarily a user interface plus Python script bindings around this kernel. CadQuery implements a Python-based scripting DSL around Open CASCADE to let you build models using code.

File formats between these tools do not necessarily have to be compatible. Open CASCADE ships with exporters for a small number of common CAD file formats, STL being the one that is compatible with most other CAD tools. It also has it's own format for exporting just the geometry (.brep files), and the API provides storage facilities for building your own application-specific container formats (OCAF). I think FreeCAD uses OCAF to implement its native file format.

I know FreeCAD can import/export STL using Open CASCADE, so that's one way transfer models from/to other CAD tools, I would not be surprised of CadQuery can also do this (but I haven't used it so I can't say for sure).


STL (and .OBJ) are common exchange formats for mesh output. For solids, which FreeCAD and OpenCASCADE mostly deals in, the typical exchange formats would be STEP (and IGES). Solids can have perfect circles, surfaces etc - where as meshes are just a bunch of 3d vertices with straight line between them. But still there is much information not present in a solid exchange format, like how the model was made: Constraints, feature operations, constraints, undo stack etc.


>> STL (and .OBJ) are common exchange formats for mesh output. For solids, which FreeCAD and OpenCASCADE mostly deals in, the typical exchange formats would be STEP (and IGES).

Ah yes I meant STEP instead of STL of course, always mix those up. OCCT does not even have support for STL or OBJ import/export.


STEP doesn't have modeling history though.


Thanks for the response! Is stuff like modeling history implemented in the kernel or in the FreeCAD code?


I’m not super familiar with the FreeCAD file format internals, but I assume it implements history using Open CASCADE API’s, in an application specific format. We have some internally developed CAD tooling based on Open CASCADE that uses the OCAF framework to store history inside a custom container format. Any code that links OCCT can in theory get the contents of the container using API calls, but to interpret it, you would have to write a lot of application specific logic on top of that (we wrote our own wrapper API’s around that to expose the file format to e.g. scripts etc)


It has been compared to FreeCAD for parametric open source scientific hardware: https://journals.plos.org/plosone/article?id=10.1371/journal...


How does it deal with the problem of geometric robustness, [1]?

[1] https://en.wikipedia.org/wiki/Robust_geometric_computation


If you like this sort of thing, I can recommend SALOME for its excellent Python bindings, each of which corresponds to a GUI action as well.


I like the use of Roots blower rotors in the example screenshot --- as something that can be mathematically described relatively simply, but far more tedious to draw manually.

I was a little perplexed at the OCCT acronym at first; it refers to the stress testing tool overwhelmingly more than this.


That looks similar to efforts I made with Blender (Vraag) and FreeCAD. But probably more mature.


I've never seen an actual money making project that would require case that was designed by script. It seems odd but maybe I'm in the wrong I dustry.


Here is one: shafts for power transmission equipment, in our case belt conveyors for bulk material handling. Shafts for pulleys come in all shapes and sizes, ideal use case for code generated models and Cadquery.

I'm very excited that such project exists, API is super nice, you can hook it up with a web server and anything else in python ecosystem. Then our sales people who are tech knowledgeable, but are not engineers can do this for customers without going to engineering. This saves time and shortens the sales cycle


How can you size shafts without engineers? Who does the calculation and review? Who is liable if it fails and someone gets hurt?


It comes from the customers. They know what they want.


You guys aren't manufacturing from the cad files, are you? Like convert to gcode and send to a CNC? Or is it just for models only.

I'm not in that industry, but it makes sense you'd have a use for it at least.

The thing is that fusion360/solidworks/etc does parametric modelling so can probably be used just as easily, with the added benefit of being industry standards.


Where I see potential for using tools as cadquery is with product type configurators which are often part of ERP software aka CPQ (Configure-Price-Quote). Cadquery generated model would be part of the Configure. Of course this wouldn't work for complex product, but there are companies out there that may need a configurator for relatively simple product




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: