Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
70 lines (56 sloc) 2.46 KB
# Copyright 2016, Jon Nordby <>. 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.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),))
image, measurement = None, None
if isImagePlane(selection[0]):
image, measurement = selection
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)
current = measurement.P1.distanceToPoint(measurement.P2)
actual, ok = getInput(title="Actual length", default=current)
if not ok:
# User aborted
return scaleReference(image, measurement, current, actual)