Build123d: A Python CAD programming library

(github.com)

84 points | by Ivoah 23 hours ago

10 comments

  • injidup 4 hours ago
    These types of CAD scripting tools are great but always try to position themselves as an “alternative” to GUI-driven CAD, whereas in reality they are complementary. OnShape got it right with FeatureScript (https://cad.onshape.com/FsDoc/ ), which provides a very similar experience to Build123d at the scripting level. However, the insight that OnShape got right is that these scripts automatically become available as possible nodes within the history-based modeller. The OnShape UI is infinitely extendable beyond the fixed set of tools that comes with the base modeller.

    Build an FOSS CAD front end using something like Build123d as the extension engine, and then add hooks so the user can select edges, surfaces, objects, etc., and feed them to inputs on the scripts. The output of the script is then the new state of the history-based modeller. That would be killer

    • CountVonGuetzli 4 hours ago
      I didn't know OnShape had such a feature. Will check it out!

      What you describe is one of the main reasons why I use Rhino3D. It can be scripted via the Grasshopper plugin, which integrates really nicely with Rhino and its primitives. Sadly, Rhino isn't open source and is quite pricy

      - https://www.rhino3d.com/ - https://www.grasshopper3d.com/

      • injidup 3 hours ago
        The fun thing is that onshape itself has a very thin kernel. Most of what you see as built in features are actually featurescript based. Onshape provides the source code for their built in feature set as a reference. https://cad.onshape.com/documents/12312312345abcabcabcdeff/w... You do need an account login ( free ) to view it.
        • aaronblohowiak 2 hours ago
          Kernel here is ambiguous.. I get what you mean, but parasolid is usually the thing described as the cad kernel.
          • injidup 1 hour ago
            You are right but I also kind of did mean it that way. I believe that Parasolid is at heart of Onshape, the true kernel. Then on top of that is a compatibility layer describing the set of low level operations available to featurescript. I'm sure that not everything in Parasolid is available to featurescript and perhaps there are some things added that are not in Parasolid. Featurescript also contains the selector/query logic for programatically picking geometry. Whether that comes from Parasolid I am not sure. I haven't worked with featurescript for a number of years now but when I did I was amazed. I managed to make an operation for taking any solid from the UI and generating customized interlocking ribbing. The idea was hollow surfboard design. It worked and I left it at that. Never built the surfboard!

            However the downside with featurescript and I think a big mistake on their part was to use a custom language rather than python or javascript. Featurescript is almost javascript but with some syntax changes and magic DSL's. You are also forced to use the inbuilt editor which is horrible and if you have burned VIM keybinding into your nerve endings, going back to non modal editing is horrible.

            Also the discovery of featurescript modules in the community has terrible UX. It's super weird that they have such a great system but finding useful extensions is horrible.

    • rao-v 3 hours ago
      I’d love to see this. It was a frustrating learning curve for me to realize that I couldn’t STEP export work from OpenSCAD to something like Fusion.

      Build123d is much better (supports STEP export and import) but a tightly integrated CAD frontend would be ideal!

    • alnwlsn 2 hours ago
      I think most GUI CADs have some kind of API like this. In FreeCAD it's Python. In Solidworks, it's VBA or C#. I don't think any are particularly well documented or supported by tutorials.
      • unholiness 55 minutes ago
        Solidworks has VBA macros, which (on top of being poorly documented and unstable) subvert to the whole benefit of parametric CAD. Once you're creating features with a macro, you naturally want to edit them, but you also naturally want to rerun the macro itself to create them differently. It's like editing generated code and it's not a viable long-term setup.

        FeatureScript is a different beast. It actually runs as part of regeneration in Onshape. Standard features (extrude, loft...) are also defined in FeatureScript, so your custom features are the same first-class citizens with a interactive GUIs and stable updates to upstream changes. You can freely mix interactive CAD and custom code by adding standard features and custom features.

      • injidup 1 hour ago
  • nakedneuron 4 hours ago
    as someone mentioned recently somebody made this build123d-playground on the web:

    https://jojain.github.io/build123d-sandbox/

    learning curve is steep, but the examples get you going in no time..

    though not really CAD, favorite example: https://build123d.readthedocs.io/en/latest/examples_1.html#c...

    shows the ability of this implementation of the open cascade kernel.. i havent found this kind of projection function too often in other cad programs, so this is really cool.. i remember trying to do similar with ptc creo and it was a pain..

  • latenode 4 hours ago
    CAD has needed a proper code-first workflow for years. The existing options always felt like they were built for the GUI first and scripting was bolted on after.
    • IshKebab 57 minutes ago
      I disagree. Most CAD is inherently visual. These code-based systems work great for highly parametric and regular objects like fasteners and gears, or for procedural art, but those are really the tiny minority of CAD tasks. Most of the time you need to see what you're doing and click on stuff.

      Most CAD is more similar to graphic design, painting, etc. You wouldn't expect a "code first workflow" for Illustrator, and as far as I know nobody has ever successfully done anything like that.

      The closest I've seen are things like UI design tools that can generate code. But a) they usually suck, and b) that is a much simpler problem than CAD.

      • hrmtst93837 55 minutes ago
        GUI CAD is fine for freeform work. For parts that live in a repo and share dimensions with other parts, code wins because you can diff changes and change one number without replaying a pile of clicks six months later.
        • IshKebab 4 minutes ago
          Yeah that would be amazing, but sadly nobody has made that work for any CAD of reasonable complexity.

          In reality, CAD version control is done just like assets in the artistic world (game design, animation, films, etc): with file locks.

          I used to work for Dyson. There's no way you're designing a vacuum cleaner with code-based CAD. We used TeamCenter (which is probably the worst software I've ever used, but for unrelated reasons).

  • Tagbert 1 hour ago
    I used to do a lot with AutoLisp in AutoCAD back when it ran in DOS. Did a lot of dynamic creation and manipulation of the models with it. It was useful and a lot of fun (aside from parenthesis nesting).
    • crispyambulance 5 minutes ago
      I use a scheme variant in an optics simulation product.

      It's actually very pleasing to work with. I wish there was more stuff like this. Lispy programming languages and CAD seems like a natural fit.

      That said, python is preferable for most people.

    • antod 18 minutes ago
      I did that back in the 90s too. A modern IDE like set of features for lisp would've been awesome. Notepad on NT4 didn't cut it :)
  • copirate 1 hour ago
    Why do these tools never have the equivalent of sketch contraints in FreeCAD? That's how I build my models and it avoids a lot of math.

    I'd really like a "CAD as code" tool that's basically the FreeCAD Part Design workflow but with code. I know FreeCAD has a python interface but it's far from developer friendly.

    • deckar01 26 minutes ago
      It has constraints, it just doesn’t have a declarative constraint solver.

      https://build123d.readthedocs.io/en/latest/tutorial_constrai...

      Traditionally CAD programs require declaring geometry, then defining constraint relationships between them. That leaves ambiguity. I often create under-constrained sketches in Fusion, then change a dimension, which breaks the geometry in ways that technically respect the constraints.

      They designed an imperative constraint system. A lot of constraints are linear, so you can just order the sketches and reference as needed. For circular or bi-directional references you probably have to define some construction geometry first, solve the constrains there, and reference it.

      Something I haven’t seen before is their filter syntax for disambiguating solutions. You can express how the desired solution relates to the surrounding geometry. This constrains correctly across large parameter changes and will error when there is no longer a solution rather than switching to an unexpected solution to the constraint.

      https://build123d.readthedocs.io/en/latest/tutorial_constrai...

    • jwagenet 1 hour ago
      The reality is build123d is a fairly thin layer over the OCCT kernel with some pythonic affordances. I'm not sure OCCT has a robust constraint solver, so there is little development there. FreeCAD on the other hand (besides being significantly more mature) is also build on OCCT, but also does a lot on top of the kernel to make it more featured and stable.
    • Cargo4286 38 minutes ago
      Also worth looking at this project https://gitlab.com/dmytrylk/solve123d which adds constraints on top of build123d using a jax-based numerical solver.
    • maouida 1 hour ago
      I'm working on one with features you mentioned, with the main goal to make the workflow similar to mainstream CAD. Will release the first dev version in less than a month.
    • htgb 1 hour ago
      build123d has constraints for avoiding math. I'm not familiar with the sketch constraints in FreeCAD though, how do they compare?

      https://build123d.readthedocs.io/en/latest/tutorial_constrai...

    • numbers_guy 42 minutes ago
      FreeCAD has a Python API that you can use to too. It's their "macro" functionality.
  • z3ugma 4 hours ago
    I have been using this library for a few months alongside Gemini 3.1 Fast

    It's really useful to get an iteration loop going with an LLM.

    The OCCP viewer extension for VS Code helps make sure you can see and manipulate the resulting model

  • jpleger 3 hours ago
    This is cool, seems like a next gen cadquery, which was really cool to see.
  • htgb 4 hours ago
    Despite being aware of its existence, I stuck with OpenSCAD out of habit. Only last week did I read through the documentation, and feel strongly that I've been missing out… it seems to solve all of my gripes with OpenSCAD. I'm excited to try it out!
    • beering 4 hours ago
      Mainly OpenSCAD is not a BRep modeling tool! It is not on the same level of power as CAD tools with a BRep kernel and this especially shows when you want to do a fillet over an arbitrary edge. Unfortunately these kernels are hard to make and integrate and I only know of two open-source BRep kernels out there: OpenCASCADE (used by FreeCAD and build123) and truck (not sure what the status of it is).
    • plumbees 4 hours ago
      I've been wanting to get into OpenScad. Wondering what you've identified as an issue. I dabbled a little and I think I remember wishing it was more object oriented to make it a bit easier to make reusable adjustable pieces and I had found a python library that does openscad conversions; Wondering what gripes it solves for you.
      • htgb 1 hour ago
        Off the top of my head, in no particular order:

        As the sibling comment mentioned, the classic problem of chamfer/fillet. Inconvenient in OpenSCAD, trivial in build123d.

        There are various features I've missed: polylines with rounded corners, extruding along a path, and more I can't recall.

        As you mention: code organization. I didn't have the need early on, but over time I often wanted to do add custom things, for example my own hack of an implementation for extruding along a path. And passing data around is just painful and ugly... since you can't read any of your input shapes, you have to pass around all data explicitly -- alongside or instead of shapes -- and then only actually render them in the latest stage. Generally I found it hard to make reusable code, and generally it has to make many assumptions about the calling code.

        The OpenSCAD editor is one big IDE, and it's not a great one. My workflow was to keep VSCodium on one side and OpenSCAD on the other, just to use a better editor. I actually thought to myself that a good project direction would be to make it more modular and focus on the unique parts rather than the IDE. And that's indeed how build123d does it: primary repo only handles the rendering, and then suggest compatible viewers, with one shipped as a VS Code extension being the primary suggestion.

        Speaking of workflow, a huge difference is local coordinate systems: lacked in OpenSCAD and encouraged in build123d. It fits my brain really well. I just made a shape, now I want to make some holes in it. Previously I've often had to think about what global coordinates to put some volume, but now I'll just select that face and work with those local 2D coordinates.

        And another workflow annoyance before is when I'm designing something to be printed in separate parts. First, meticulously fit things together in the global coordinate system so that I can see them in the assembled state. Then to allow printing, also conditionally move all parts to manually specified other coordinates. With build123d one can define the parts separately, place joints in desired locations, and have them automatically snap together for the assembled view. It looks useful for integrating third-party parts as well.

        Minor thing, but I'm always slightly annoyed by the fact that a rectangle is called a square and a slab/box called a cube in OpenSCAD...

        Oh, and it's often useful to use polar coordinates. More passing around custom data and manually calling your custom conversion function whenever passing to OpenSCAD. build123d has first-party suitable objects for it.

        OpenSCAD development seems fairly dormant as well. Latest stable release was five (!) years ago, but by reading around you see that you're supposed to use the nightly version because it's superior. Not very friendly for newcomers. By contrast, build123d seems very active.

        I should stop now, because this already got pretty long. As you can see, I had some bottled up though -- thanks for letting me vent!

      • greenavocado 2 hours ago
        B-rep (boundary representation) modelers (like build123d via OpenCascade) represent solids using faces, edges, topology geometry

        OpenSCAD uses Constructive Solid Geometry (CSG) which represents objects as boolean combinations of primitives (union, difference, intersection). It does not maintain an explicit boundary/face structure internally

  • wisemanwillhear 3 hours ago
    In many ways this looks fun. I love the precise control and programming power of tools like this, but when I need something in real life, I never use them any more. The productivity of graphical tools is so much greater (as far as my brain works).

    When I was younger I used POVray for a few small projects, but once I had access to graphical interfaces the difference in output quantity and quality was huge. I still keep tools like POVray installed, but all I ever do with them is tinker once in a while.

    • bityard 2 hours ago
      I remember running across POVray in the early 90's and not having a clue how to do anything more complex than a sphere with a light source.

      Some artists have done some truly magical things with it: https://hof.povray.org

  • CarVac 4 hours ago
    I like Build123d but I really want a hybrid mouse/code CAD built around it. I want to be able to click on entities and have them show up in the code editor instead of blindly trying to select edges.
    • Cargo4286 58 minutes ago
      This feature technically already exists in build123d + OCP CAD Viewer (ocp_vscode). You can click a feature in GUI and it will copy the index to clipboard, you can then apply e.g. fillets to it on the code side. See here https://github.com/bernhard-42/vscode-ocp-cad-viewer/blob/ma...

      The biggest caveat is that there is currently absolutely zero mitigation for toponaming. This feature is extremely brittle, so I tend not to use it very much -- but I am still glad it exists for those situations when composing a selector is too annoying.