-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathimageScaleReference.FCMacro
69 lines (56 loc) · 2.46 KB
/
imageScaleReference.FCMacro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright 2016, Jon Nordby <[email protected]>. Licensed LGPLv2+ & MIT
#
# scaleReference:
# Given a selection of an ImagePlane and a MeasureDistance,
# rescales the ImagePlane to make the distance between two points of the MeasureDistance
# match a given real-life distance.
# The MeasureDistance is then deleted.
import FreeCADGui
script = "scaleReference"
def show(title=None, text=None):
title = script if title is None else title
text = title if text is None else text
from PySide import QtGui
QtGui.QMessageBox.information(None, title, text)
def getInput(title=None, text=None, default=0.0):
title = script if title is None else title
text = title if text is None else text
from PySide import QtGui
val = QtGui.QInputDialog.getDouble(None, title, text, default)
return val
def isImagePlane(obj):
return obj.TypeId == 'Image::ImagePlane'
def isMeasurement(obj):
return obj.TypeId == 'App::MeasureDistance'
def scaleReference(image, measurement, current, actual):
# Calculate image scaling and apply
scale = actual/current
image.XSize = image.XSize*scale
image.YSize = image.YSize*scale
# TODO: Update measurement to match instead of deleting
measurement.Document.removeObject(measurement.Name)
#measurement.P1 = (measurement.P1-image.Placement.Base).scale(scale, scale, 1)
#measurement.P2 = (measurement.P2-image.Placement.Base).scale(scale, scale, 1)
#measurement.Label = "Distance: %.3d" % (measurement.Distance,)
def scaleReferenceFromSelection():
# Extract objects from in selection
selection = FreeCADGui.Selection.getSelection()
if len(selection) != 2:
show(text= script + ": %d objects were selected. Need to select 2: one ImagePlane and one Distance." % (len(selection),))
return
image, measurement = None, None
if isImagePlane(selection[0]):
image, measurement = selection
else:
measurement, image = selection
if not isMeasurement(measurement) or not isImagePlane(image):
err = script+": Selected objects have wrong type. Got [%s, %s] - expected [Image::ImagePlane, App::MeasureDistance]" \
% (measurement.TypeId, image.TypeId)
show(text=err)
current = measurement.P1.distanceToPoint(measurement.P2)
actual, ok = getInput(title="Actual length", default=current)
if not ok:
# User aborted
return
return scaleReference(image, measurement, current, actual)
scaleReferenceFromSelection()