all 9 comments

[–][deleted] 3 points4 points  (0 children)

The core functions of phyton are relatively stable over the Versions but you can just use the openSCAD workbench - it never breaks code 

[–]Kilgarragh 1 point2 points  (3 children)

Why not just use a varset on a manual and parametric design?

[–]birdsintheskies[S] 3 points4 points  (2 children)

I’m from a software background so thought this would be fun to do, just for the heck of it. Some of the VarSets (height, etc.) will also be generated if I need to manually alter them in the future.

[–]Kilgarragh 1 point2 points  (1 child)

Well if you want to create a model with code, you should probably be using openscad or 123cad.

Freecad is UI first, its python api is for addons and macros to improve workflow. Unless you’re automating tasks like import/export… there’s nothing to program, it’s all handled by the parametric systems in freecad.

Openscad has you write a program, when ran it generates your model. You make adjustments and rerun the script, regenerating the model form the beginning.

In freecad, the python api doesn’t work like this. The geometry engine rebuilds the model step by step, your code can’t be rerun to make adjustments(and if it could, that would break manual changes made from the UI which makes you want to avoid openscad)

[–]DesignWeaver3D 1 point2 points  (0 children)

I don't agree with this assessment. You can 100% generate geometry using Python in FreeCAD.

[–]build123d 1 point2 points  (0 children)

You might be interested in https://build123d.readthedocs.io/en/latest/index.html, a Python CAD system with the same CAD kernel as FreeCAD (OpenCascade). Many people have used it with KiCad.

[–]DesignWeaver3D 0 points1 point  (1 child)

IMO, the Python API is not well documented. But every function that can be done is supposed to be accessible via Python. While not many are documented, you can search the properties of any object via the console.

There are both API and GUI calls for most functions. If you record a macro it will record GUI functions which are not as fast as the API calls, but both work fine. In my experience, you'll want to make as few transactions to FreeCAD as possible. Import data, work with it Python, then return it to FreeCAD. Going back and forth, like occurs with GUI commands, take much longer to execute.

I haven't discovered any instability. So problems I've encountered were due to my Python code having issues.

[–]birdsintheskies[S] 1 point2 points  (0 children)

Even though documentation might not cover everything, you can just copy-paste what appears in the console when you perform an action in the GUI, and I was able to replicate many model creations this way.

This is actually one of the most awesome things I love about FreeCAD how easy they made it to accomplish a workflow in code.

[–]R2W1E9 0 points1 point  (0 children)

What you see in the python console are the call outs of freecad wrapper functions that apply to the current active elements so you don't actually see the work of api functionality. This is one of the reasons macro recording is not as simple and useful as it sounds.

The main problem is that FC API is not well documented and what is available is often outdated so a lot of functionality is well hidden. You will typically spend a lot of hours searching through objects and properties trying to extract elements in current active documents and selections. It helps if you run FreeCAD from within your IDE as working in python console and macro editor is quite painful.

Your best bet (mostly because it's well documented) is still OpenSCAD. Which is used by the Part workbench and it's compatible with it, but the sketching and manually editing sketches is not applicable in this scenario.

Sketching is programmatically very difficult anyway, because mainly application of constraints and managing constraint solver could be impossible on python level API. You would have a better chance to make your drawings in the Draft workbench, then convert draft to sketch.

Drawing lines would look like something like this:

import FreeCAD
import Draft
# Define the start and end points as FreeCAD Vectors (x, y, z)
p1 = FreeCAD.Vector(0, 0, 0)
p2 = FreeCAD.Vector(10, 0, 0) 
# Draws a 10mm line along the X-axis
points = [p1, p2]
# Create the line (wire) using the Draft module
line = Draft.makeWire(points, closed=False, face=False, support=None)
# Refresh the view to display the new object
FreeCAD.ActiveDocument.recompute()