Skip to content

Instantly share code, notes, and snippets.

@mibli
Created February 23, 2022 03:08
Show Gist options
  • Save mibli/28635bf6d5b3999ca5427138aee1f5ce to your computer and use it in GitHub Desktop.
Save mibli/28635bf6d5b3999ca5427138aee1f5ce to your computer and use it in GitHub Desktop.
Generate Copies of Parametrized FreeCAD projects with Spreadsheet
# vim: ft=python
"""
Macro that changes values to presets in columns, and saves modified copies.
This macro is mainly useful for Assembly Workbench (A2Plus)
The sheet should have the following format:
| A | B | C | D | ... | m |
-------------------------------------------------
1 |Width | 200| 300| 200| | |
-------------------------------------------------
2 |Height | 100| 100| | | |
-------------------------------------------------
3 |OtherValues | 40| | 30| | |
-------------------------------------------------
. . .
-------------------------------------------------
n | | | | | | | < empty row marks an end of original values
-------------------------------------------------
^ empty column marks an end of data
Column A should have parameter names
Column B should have parameter values
Columns C+ should contain optional variety values
Stops on the first blank row it encounters
If GeneratorFormat (or custom format_fld name) is encountered, that format will be used for naming
Currently can replace original values (TODO)
"""
import FreeCAD
import itertools
import string
from datetime import datetime
from pathlib import Path
document = App.ActiveDocument
sheet = Gui.activeView().getSheet()
print(f"Sheet found: {sheet.Label}")
# Configure these if You want
value_col = "B"
format_str = "{name}{0}"
format_fld = "GeneratorFormat"
date = datetime.now()
file_name = doc_path.stem
base_path = doc_path.parent
extension = ".FCStd"
# If someone provides format string, use it
try:
format_str = sheet.get(format_fld)
except:
pass
print("Format string is:", format_str)
# Store original vals, we'll need them
original_vals = []
for row in itertools.count(1):
cell = f"{value_col}{row}"
try:
value = sheet.get(cell)
except:
break
original_vals.append(value)
print("Original values:", repr(original_vals))
# Iterate all alphabet starting from original values
start_from = string.ascii_uppercase.index(value_col)
for col in string.ascii_uppercase[start_from:]:
values = []
value_found = False
for row in range(0, len(original_vals)):
cell = f"{col}{row + 1}"
value = None
try:
value = sheet.get(cell)
value_found = True
except:
value = original_vals[row]
values.append(value)
value_cell = f"{value_col}{row + 1}"
sheet.set(value_cell, str(value))
# if no value found it means end of presets
if not value_found:
break
# recompute and save the file
document.recompute()
# TODO: should restore original values if recompute or savecopy throws
path = doc_path.parent.joinpath(Path(format_str.format(*values, name=doc_path.stem)).with_suffix(extension))
doc.saveCopy(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment