Skip to content

Commit

Permalink
Merge pull request #34 from CadQuery/freecad_plugin
Browse files Browse the repository at this point in the history
First draft of FreeCAD import plugin
  • Loading branch information
jmwright committed Jan 19, 2024
2 parents 45f456c + 2475af4 commit de88a66
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 26 deletions.
17 changes: 4 additions & 13 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,14 @@ jobs:
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: '3.10'

- name: Install Python dependencies
# grab the currently pinned black version from CQ. The `curl | grep`
# result will be an empty string if black is not pinned. If black is
# not pinned in CQ, final command will reduce to
# `pip install black flake8`.
# CadQuery runs a customized version of black
run: |
echo "grabbing black version from CQ"
black_ver=$(curl "https://raw.githubusercontent.com/CadQuery/cadquery/master/environment.yml" | grep -oP '(?<=black=).*')
echo "got: $black_ver"
if [[ -n "$black_ver" ]]; then
black_ver="==$black_ver";
fi
pip install black$black_ver flake8 click==8.0.4
pip install git+https://github.com/cadquery/black.git@cq
# Runs the lint check against the repo
- name: Run black
Expand Down
27 changes: 15 additions & 12 deletions .github/workflows/tests-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ jobs:
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: conda-incubator/setup-miniconda@v2
- uses: conda-incubator/setup-miniconda@v3
with:
miniconda-version: "latest"
python-version: 3.8
activate-environment: test
python-version: "3.10"
activate-environment: "freecad"

# Installs CadQuery and pytest so that the test can be run
- name: Install CadQuery and pytest
shell: bash --login {0}
run: |
conda install conda-forge::freecad
conda install -c cadquery -c conda-forge cadquery=master
conda install -c anaconda pytest
Expand All @@ -48,14 +48,14 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: conda-incubator/setup-miniconda@v2
- uses: conda-incubator/setup-miniconda@v3
with:
miniconda-version: "latest"
python-version: 3.8
activate-environment: test
python-version: "3.10"
activate-environment: "freecad"
- name: Install CadQuery and pytest
shell: bash --login {0}
run: |
conda install conda-forge::freecad
conda install -c cadquery -c conda-forge cadquery=master
conda install -c anaconda pytest
- name: Run tests
Expand All @@ -68,14 +68,17 @@ jobs:
runs-on: "windows-latest"
steps:
- uses: actions/checkout@v2
- uses: conda-incubator/setup-miniconda@v2
- uses: conda-incubator/setup-miniconda@v3
with:
miniconda-version: "latest"
python-version: 3.8
activate-environment: test
mamba-version: "*"
channels: conda-forge,defaults
channel-priority: true
python-version: "3.10"
activate-environment: "freecad"
- name: Install CadQuery and pytest
shell: pwsh
run: |
conda install conda-forge::freecad
conda install -c cadquery -c conda-forge cadquery=master
conda install -c anaconda pytest
- name: Run tests
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ dmypy.json
#vscode
.vscode/


# Unwanted FreeCAD files
*.FCBak
updated_part.FCStd
55 changes: 55 additions & 0 deletions plugins/freecad_import/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# FreeCAD Importer Plugin

This plugin allows users to import FreeCAD models into CadQuery, and will apply parameters to the model if they are provided and the model is a parametric one (contains a FreeCAD spreadsheet document). At this time this plugin does not handle FreeCAD assemblies.

## Installation

Something like an Anaconda environment with FreeCAD installed as a conda package may be required on some Linux distros like Ubuntu because of the requirement to use the FreeCAD Snap to get the latest version of FreeCAD. The Snap does not seem to allow FreeCAD to be imported properly in Python. If you do use Anaconda, name the environment `freecad` to help this plugin find it. See the [documentation here](https://cadquery.readthedocs.io/en/latest/installation.html#install-the-conda-package-manager) for instructions on how to set up Anaconda without messing up your local Python installation. A example conda installation to get this plugin working is shown below.
```bash
mamba create -n freecad python=3.10
mamba install -c cadquery cadquery=master
mamba install freecad:freecad
```

Assuming that you have pip and git installed, the following line can be used to install this plugin.

```
pip install -e "git+https://github.com/CadQuery/cadquery-plugins.git#egg=freecad_importer&subdirectory=plugins/freecad_importer"
```

## Dependencies

FreeCAD must be installed and importable via Python in order for this plugin to work. CadQuery is also required. To install CadQuery, follow the [instructions here](https://cadquery.readthedocs.io/en/latest/installation.html), or use the conda instructions above.

## Usage

To use this plugin after it has been installed, import it to automatically patch its functions into the `cadquery.importers` package.

Here is an example of importing a parametric FreeCAD part.

```python
import cadquery as cq
# The below adds the plugin's functions to cadquery.importers
from freecad_importer import import_freecad_part

# Imports a FreeCAD part while altering its parameters.
# The parameter must exist for the part or an errorr will be thrown.
result = import_freecad_part(
"path_to_freecad_part_file.FCStd", parameters={"mount_dia": {"value": 4.8, "units": "mm"}}
)
```

Here is an example of retrieving the parameters from a parametric FreeCAD part.

```python
import cadquery as cq
# The below adds the plugin's functions to cadquery.importers
from freecad_importer import get_freecad_part_parameters

# Get the parameters from the objectr
params = get_freecad_part_parameters(
"path_to_freecad_part_file.FCStd", name_column_letter="A", value_column_letter="B"
)
```

The tests associated with this plugin have additional code that also might be useful to review as examples.
Empty file.
Loading

0 comments on commit de88a66

Please sign in to comment.