Update the sketch quotas and read the dimensions of the model

153 views Asked by At

I have a sketch with a fixed angle between two lines. I also have a dimensions (from draft) which are linked to the sketch partitions. I need to change the angle on sketch and get the actual values of the dimensions. I also need to do it from external python module (embedding)

The code is:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys
import numpy as np

FREECADPATH = r'/usr/lib/freecad/lib'
sys.path.append(FREECADPATH)

import FreeCAD as App
import FreeCADGui as Gui

filePath = r"thePath..."
if App.ActiveDocument:
    doc = App.ActiveDocument
else:
    doc = App.open(filePath)

angSmpl = np.linspace(10, 40, num=10)

sk = doc.Sketch

for angle in angSmpl:
    a = sk.getDatum('rotHex').Value
    sk = doc.Sketch
    sk.setDatum('rotHex', App.Units.Quantity(str(int(angle))+' deg'))
    doc.recompute()
    b = sk.getDatum('rotHex').Value
    res = doc.getObjectsByLabel('heightHex')[0].Distance
    print a, b, res

The output is:

FreeCAD 0.16, Libs: 0.16R6707 (Git)
Fasteners workbench Loaded
Sheet Metal workbench loaded
Importing project files......
                  (100.0 %)   

32.0 10.0 330.599 mm
10.0 13.0 330.599 mm
13.0 16.0 330.599 mm
16.0 20.0 330.599 mm
20.0 23.0 330.599 mm
23.0 26.0 330.599 mm
26.0 30.0 330.599 mm
30.0 33.0 330.599 mm
33.0 36.0 330.599 mm
36.0 40.0 330.599 mm

Why the variable "res" not changing its value? How to solve this task properly?

Thanks for a help.

The freecad model is here: MyModel

1

There are 1 answers

1
ickby On BEST ANSWER

By default draft dimensions are not parametric, this means they do not update when the dimensioned shape changes. To create a parametric dimension you can use the "alt" key, according to the FreeCAD documentation at http://www.freecadweb.org/wiki/index.php?title=Draft_Dimension

You need to make sure that the dimension is parametric to get your script working. The simplest way of checking this is to update the sketch in the GUI and check if the dimension updates too. If so, and the script does not work, it is a bug.