This is a FreeCAD Workbench to render CadQuery 2.x script in FreeCAD.
As the easiest way to install CadQuery and its dependencies is using conda, it is recomanded to install both FreeCAD and CadQuery into a dedicated conda environment. Then download the [latest released version]() of this workbench, extract the archive file and copy the entire extracted directory to FreeCAD's Mod
directory on your environment.
For more details regarding conda installation refer to CadQuery and FreeCAD
# Set up a new environment
conda create -n freecad_cadquery
# Activate the new environment
conda activate freecad_cadquery
# Install both CadQuery and FreeCAD
conda install -c conda-forge freecad
conda install -c conda-forge -c cadquery cadquery=master
To preserve your system settings, it is suggested to create a dedicated home
folder in your conda environment and to set FreeCAD to use this folder.
For more details, refer to Conda Managing environments and FreeCAD Start up and Configuration.
# Create a home dir inside the conda environment
cd $CONDA_PREFIX
mkdir ./home
# Set environment variables
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh
Edit ./etc/conda/activate.d/env_vars.sh
as follows:
#!/bin/sh
export HOME=$CONDA_PREFIX/home
export FREECAD_USER_HOME=$CONDA_PREFIX/home
To apply the changes, deactivate from the environment then activate it again.
Then run FreeCAD, so that it will create in your new environment home folder the .FreeCAD/Mod
directory.
conda deactivate
conda activate freecad_cadquery
cd $Home
freecad
Note: having create a dedicated home folder, at first execution FreeCAD set all defaults settings. If you have FreeCAD already installed in your computer, and have already made some user setup, there are 3 configuration files you may find useful to copy from your system installation into your new conda environment.
Under ~/.FreeCAD
files system.cfg
and user.cfg
Under ~/.config/FreeCAD
file FreeCAD.conf
Mod
directory on your environment.In FreeCAD, select CadQuery workbench from the Workbench dropdown list in the Workbench toolbar.
It opens CadQuery Editor and CadQuery variables Editor panes on the right DockWidget of FreeCAD.
To Show/Hide the Editor, use the standard FreeCAD View
->Panels
from the menu.
New Scirpt (Alt+Shift+N)
Start a new script from a template fileOpen Script (Alt+Shift+O)
Open a pyton script fileSave Script (Alt+Shift+S)
Save current scriptSave Script As
Open a Save As dialog box to save current scriptValidate Script (F4)
Execute cadquery script but do not display the objects in FreeCAD. Use it to validate your script.Execute Script (F9)
Execute cadquery script and display objects in FreeCAD. Variables use values from the Variables Editor. If you add a new variable in the script and run again Execute, the new variable is not added into the Variables Editor.Rebuild Script (F5)
Rebuild and execute the script. Variables Editor is reset from the values in the script.Toggle Debug Script
when toggle on, debug()
objects will be displayed in FreeCAD.Clear Output (Alt+Shift+C)
clears all output from the Report View. This comes in handy during a heavy script debugging session.Settings
open the Settings dialog box, see below for details.Examples
Holds a list of all the CadQuery examples that are bundled with this workbench. They're a great tool for learning CadQuery. Those scripts come from CadQuery project.Expand Tree Groups
when using group in the show_object()
method, FreeCAD collapse all the groups. This button expand all groups in the Tree View.Key-binding
you can set your own shortcuts to Execute, Validate or Rebuild commands.Execute on Save
Automatically Rebuild a script every time you save it. The default is False.Show Line Numbers
show/hide the line numbers in cadquery editor. The default is True.Allow Reload
automatically reloads and executes the open script when an external change is made. This allows users to use their preferred code editor instead of the one included with this workbench. The default is False. When False is set, user will be prompt to reload or not the file when a change on disk is made.Dock Widget Layout
allow to change the FreeCAD dock widgets layout for more convenience.Use the standard FreeCAD Editor settings to change the Font, Font size and python color scheme.
You need use the show_object(cq_obbject [, options])
method to render your model in FreeCAD
String
the name of the partString
in FreeCAD you can organize your parts within Groups, providing a group nameCadQuery Color
see Assembly colors for a list of valid color nameString
css style
like '#RRGGBB' or '#RRGGBBAA' with RR,GG,BB,AA hexa in range [0-255]Tuple
(r, g, b, a) with r,g,b in range [0-255] and a in range [0.0-1.0]Tuple
(r, g, b) in range [0-255]Float
in range [0.0-1.0]It is possible to do visual debugging of objects by using the debug()
function to display an object instead of show_object()
.
import cadquery as cq
result = cq.Workplane().box(10, 10, 10)
highlight = result.faces('>Z')
show_object(result, name='box')
debug(highlight)
Objects displayed with debug()
are colored in red and have their alpha set so they are semi-transparent. This can be useful for checking for interference, clearance, or whether the expected face is being selected, as in the code above.
Optional name
and group
can be provided.
You have to toggle on the Toggle Debug Script
tool button in CadQuery Editor to display debug()
objects.
This workbench uses the following rules to find input variables for a script:
int
and float
statements are consideredFor example, in the following script:
h = 10
w = 2.0
d = h / 2.0
foo = 'bar'
def some_function():
x = 1
h
and w
will be overridable script variables, but d
, foo
and x
are not.
We can assign a description of the variables in 2 different way:
# Defines the cqvar(value, descrition) function
from freecad.cadquery2workbench.cq_variables import cqvar
size = cqvar(10, 'Cube Size') # set variable value and his description
thickness = 1.25 # Another way to setup a variable and his description
describe_parameter(thickness,'Walls Thickness')
Change values then press Enter
to update your model.
CadQuery 2.x Workbench for FreeCAD is licensed under the terms of the Apache Public License, version 2.0.
This workbench is a fork of the cadquery-freecad-module from Jeremy Wright, that was design for CadQuery v1.x
A great thanks to all contributors to the great tool that is CadQuery and in particular to David Cowden, Adam Arbanczyk and Jeremy Wright.