Batch import contraints to spreadsheet
Forum rules
and Helpful information
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help
Also, be nice to others! Read the FreeCAD code of conduct!
Also, be nice to others! Read the FreeCAD code of conduct!
Batch import contraints to spreadsheet
Is there a way to batch import constraints from a sketch or object to a spreadsheet, rather than having to name and import each one individually?
Re: Batch import contraints to spreadsheet
How should FreeCAD know which value to assign to what and which constraint? Do you have any concrete example of that?
Re: Batch import contraints to spreadsheet
Sort of:
- Create a CarbonCopy of the sketch,
- close Sketcher
- expand Constraints in the properties
- you see now the internal numbers of the constraints which are perhaps easier to copy/paste+adapt.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Re: Batch import contraints to spreadsheet
Here's some Python if you've a large number of named constraints (assuming you've got a spreadsheet called 'Spreadsheet' and the use has the sketch selected in the Tree View before running the macro):
Tested using:
OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of FreeCAD: 64-bit
Version: 0.20.26858 (Git)
Build type: Release
Branch: master
Hash: e209bc706d35121098f9bac779bc6b09c24ddd95
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.3
Locale: English/United Kingdom (en_GB)
Code: Select all
import FreeCAD
import Sketcher
try:
# Get the Sketch that the user has selected
s = FreeCADGui.Selection.getSelection()[0]
except:
print('Nothing selected')
aSSheet = FreeCAD.ActiveDocument.getObject('Spreadsheet')
# Error check just incase user has selected a Part, Mesh etc
for selection in FreeCADGui.Selection.getSelectionEx():
# Need to accommodate Python 3+ and Python 2_7 so check for both type names
if str(type(selection.Object)) == "<class 'Sketcher.SketchObject'>" or str(type(selection.Object)) == "<type 'Sketcher.SketchObject'>":
# print(s.Constraints)
# Start the Constraint counter at Zero
nextNumber = 0
j = 1
# Loop through all the Constraints to get the index
for eachConstraint in s.Constraints:
print('Index ' + str(nextNumber) + ' named "' + str(eachConstraint.Name) + '" of type ' + str(eachConstraint.Type) + ' with a value of ' + str(eachConstraint.Value))
if eachConstraint.Name != '':
aSSheet.set('A'+str(j),str(eachConstraint.Name))
aSSheet.set('B'+str(j),str(eachConstraint.Value))
aSSheet.recompute()
j = j + 1
nextNumber = nextNumber + 1
OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of FreeCAD: 64-bit
Version: 0.20.26858 (Git)
Build type: Release
Branch: master
Hash: e209bc706d35121098f9bac779bc6b09c24ddd95
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.3
Locale: English/United Kingdom (en_GB)
Re: Batch import contraints to spreadsheet
I don't have a specific example at the moment - it's more a general question. If I can get my head round things like this, I can develop my own workflow.
Thanks. I'll have a look at that.
Python macros? That's just a bit above my pay grade.
Re: Batch import contraints to spreadsheet
Pay grade for everyone in FreeCAD = 0.00 (denomination of your choice)
Running Python/macros with FreeCAD is SOP...better learn.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Re: Batch import contraints to spreadsheet
I misunderstood the question, and was thinking more the other way around - how to automatically set constraints based on spreadsheet names.
But now I dont see a use case for such automation. Do you want to have al dimensional constraints values (driving and driven) linked from that spreadsheet? Also all numerical parameters of an object (attachment parameters, placement, view properties, ...)?
-
- Veteran
- Posts: 5513
- Joined: Thu Apr 05, 2018 1:53 am
Re: Batch import contraints to spreadsheet
I made a macro to do this long ago, but I guess I never put it on the addon manager.
https://github.com/mwganson/ConstraintsToSpreadsheet
It uses named constraints and creates aliases from the names. To skip a named constraint add an underscore to the end of the name, for example: instead of height, name it height_. The macro will see this and know to skip it.
https://github.com/mwganson/ConstraintsToSpreadsheet
It uses named constraints and creates aliases from the names. To skip a named constraint add an underscore to the end of the name, for example: instead of height, name it height_. The macro will see this and know to skip it.
Re: Batch import contraints to spreadsheet
I'm thinking about rescaling sketches using spreadsheets.
I guess I'd better look up how to use macros next.TheMarkster wrote: ↑Wed Jan 19, 2022 5:52 pm I made a macro to do this long ago, but I guess I never put it on the addon manager.