Batch import contraints to spreadsheet

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
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!
Post Reply
idris
Posts: 28
Joined: Wed Jun 30, 2021 4:28 pm

Batch import contraints to spreadsheet

Post by idris »

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?
kisolre
Veteran
Posts: 4164
Joined: Wed Nov 21, 2018 1:13 pm

Re: Batch import contraints to spreadsheet

Post by kisolre »

How should FreeCAD know which value to assign to what and which constraint? Do you have any concrete example of that?
chrisb
Veteran
Posts: 53945
Joined: Tue Mar 17, 2015 9:14 am

Re: Batch import contraints to spreadsheet

Post by chrisb »

idris wrote: Wed Jan 19, 2022 12:52 pm 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?
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.
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: Batch import contraints to spreadsheet

Post by Syres »

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):

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
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)
idris
Posts: 28
Joined: Wed Jun 30, 2021 4:28 pm

Re: Batch import contraints to spreadsheet

Post by idris »

kisolre wrote: Wed Jan 19, 2022 1:09 pm How should FreeCAD know which value to assign to what and which constraint? Do you have any concrete example of that?
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.
chrisb wrote: Wed Jan 19, 2022 2:43 pm 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.
Thanks. I'll have a look at that.
Syres wrote: Wed Jan 19, 2022 3:30 pm 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):
Python macros? :shock: That's just a bit above my pay grade. :lol:
drmacro
Veteran
Posts: 8872
Joined: Sun Mar 02, 2014 4:35 pm

Re: Batch import contraints to spreadsheet

Post by drmacro »

idris wrote: Wed Jan 19, 2022 4:35 pm ...
Syres wrote: Wed Jan 19, 2022 3:30 pm 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):
Python macros? :shock: That's just a bit above my pay grade. :lol:
Pay grade for everyone in FreeCAD = 0.00 (denomination of your choice) 8-)

Running Python/macros with FreeCAD is SOP...better learn. :mrgreen:
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
kisolre
Veteran
Posts: 4164
Joined: Wed Nov 21, 2018 1:13 pm

Re: Batch import contraints to spreadsheet

Post by kisolre »

idris wrote: Wed Jan 19, 2022 4:35 pm I don't have a specific example at the moment
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, ...)?
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Batch import contraints to spreadsheet

Post by TheMarkster »

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.
idris
Posts: 28
Joined: Wed Jun 30, 2021 4:28 pm

Re: Batch import contraints to spreadsheet

Post by idris »

kisolre wrote: Wed Jan 19, 2022 5:08 pm I dont see a use case for such automation.
I'm thinking about rescaling sketches using spreadsheets.

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.
I guess I'd better look up how to use macros next. :D
kisolre
Veteran
Posts: 4164
Joined: Wed Nov 21, 2018 1:13 pm

Re: Batch import contraints to spreadsheet

Post by kisolre »

idris wrote: Wed Jan 19, 2022 10:48 pm I'm thinking about rescaling sketches using spreadsheets.
So you want to create a sketch with named constraints, then create corresponding spreadsheet aliases and link those constraints to them?
Post Reply