Merge pull request #27616 from WandererFan/nearestFractionFillTemplateFields

TechDraw: Fix fillTemplateFields returns unusual fractional scale
This commit is contained in:
Max Wilfinger
2026-02-21 16:01:46 +01:00
committed by GitHub
2 changed files with 21 additions and 24 deletions
+16
View File
@@ -192,6 +192,10 @@ public:
add_varargs_method("makeLeader", &Module::makeLeader,
"makeLeader(parent - DrawViewPart, points - [Vector], startSymbol - int, endSymbol - int) - Creates a leader line attached to parent. Points are in page coordinates with (0, 0) at lowerleft.s"
);
add_varargs_method("nearestFraction", &Module::nearestFraction,
"nearestFraction(float) - returns the numerator and denominator of the nearest fraction as a tuple."
);
initialize("This is a module for making drawings"); // register with Python
}
~Module() override {}
@@ -1348,6 +1352,18 @@ private:
return Py::asObject(new DrawLeaderLinePy(newLeader));
}
Py::Object nearestFraction(const Py::Tuple& args)
{
double valueWithDecimals{0.0};
if (!PyArg_ParseTuple(args.ptr(), "d", &valueWithDecimals)) {
throw Py::TypeError("expected (valueWithDecimals)");
}
std::pair<int, int> numAndDen = DrawUtil::nearestFraction(valueWithDecimals);
PyObject* pyNumAndDen = Py_BuildValue("(ii)", numAndDen.first, numAndDen.second);
return Py::asObject(pyNumAndDen);
}
};
PyObject* initModule()
@@ -37,6 +37,7 @@ import csv
import codecs
from fractions import Fraction
import os.path
import TechDraw
CreatedByChkLst = []
ScaleChkLst = []
@@ -196,31 +197,11 @@ class TaskFillTemplateFields:
self.checkBoxList.append(self.cb2)
self.lineTextList.append(self.s2)
self.cb2.clicked.connect(self.on_cb2_clicked)
if projgrp_view.Scale < 1:
fracScale = Fraction(projgrp_view.Scale).limit_denominator()
self.s2.setText(
str(fracScale.numerator)
fracScale = TechDraw.nearestFraction(projgrp_view.Scale)
self.s2.setText(
str(fracScale[0])
+ " : "
+ str(fracScale.denominator)
)
elif int(projgrp_view.Scale) == 1 or (
projgrp_view.Scale > 1
and int(projgrp_view.Scale) == projgrp_view.Scale
):
self.s2.setText(str(int(projgrp_view.Scale)) + " : 1")
else: # must be something like 2.5 = 5 : 2
for x in range(2, 10):
if (
int(projgrp_view.Scale * x)
== projgrp_view.Scale * x
):
fracScale = Fraction(projgrp_view.Scale)
self.s2.setText(
str(fracScale.numerator)
+ " : "
+ str(fracScale.denominator)
)
break
+ str(fracScale[1]))
dialogRow += 1
if str(key).lower() in LabelChkLst:
t3 = QtGui.QLabel(value)