jpmlt / freecad-cadquery2-workbench

Workbench to render CadQuery 2.x script in FreeCAD
Apache License 2.0
25 stars 5 forks source link

CadQuery 2.x Workbench for FreeCAD

GitHub version License

Introduction

This is a FreeCAD Workbench to render CadQuery 2.x script in FreeCAD.

Screenshot

Screenshot

Notable features

Installation

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.

Install cadQuery 2.x and FreeCAD in a conda 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

Seting up your conda environment

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

Install this cadquery2-workbench

Usage

Activate CadQuery workbench

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.

Commands

ToolBar

User Settings

Settings

Python editor

Use the standard FreeCAD Editor settings to change the Font, Font size and python color scheme.

Showing Objects

You need use the show_object(cq_obbject [, options]) method to render your model in FreeCAD

Debugging Objects

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 Toggle Button tool button in CadQuery Editor to display debug() objects.

Script variables

This workbench uses the following rules to find input variables for a script:

For 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')

Variables Editor Change values then press Enter to update your model.

License

CadQuery 2.x Workbench for FreeCAD is licensed under the terms of the Apache Public License, version 2.0.

Acknowledgment

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.