CadQuery: A Python parametric CAD scripting framework based on OCCT
github.com
github.com
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.
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.
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
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.
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.
* 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.
You can use SymPy for constraint modeling, for example.
So I'm really interested in learning something like OpenSCAD or CadQuery, even if only for files I can use and read anywhere.
https://github.com/bernhard-42/jupyter-cadquery/blob/master/...
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 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.
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.
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.
https://wiki.freecadweb.org/Sketcher_Workbench
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.
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.
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.
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).
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.
[1] https://en.wikipedia.org/wiki/Robust_geometric_computation
I was a little perplexed at the OCCT acronym at first; it refers to the stress testing tool overwhelmingly more than this.
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
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.