1
/***************************************************************************
2
* Copyright (c) 2021 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
29
#include <QRegularExpression>
32
#include <App/Application.h>
33
#include <Base/Quantity.h>
34
#include <Base/Tools.h>
35
#include <Base/UnitsApi.h>
36
#include <Gui/CommandT.h>
37
#include <Gui/Document.h>
38
#include <Gui/Selection.h>
39
#include <Mod/Sketcher/App/GeometryFacade.h>
40
#include <Mod/Sketcher/App/SketchObject.h>
42
#include "DrawSketchHandler.h"
44
#include "ViewProviderSketch.h"
48
using namespace SketcherGui;
49
using namespace Sketcher;
51
bool Sketcher::isCircle(const Part::Geometry& geom)
53
return geom.is<Part::GeomCircle>();
56
bool Sketcher::isArcOfCircle(const Part::Geometry& geom)
58
return geom.is<Part::GeomArcOfCircle>();
61
bool Sketcher::isEllipse(const Part::Geometry& geom)
63
return geom.is<Part::GeomEllipse>();
66
bool Sketcher::isArcOfEllipse(const Part::Geometry& geom)
68
return geom.is<Part::GeomArcOfEllipse>();
71
bool Sketcher::isLineSegment(const Part::Geometry& geom)
73
return geom.is<Part::GeomLineSegment>();
76
bool Sketcher::isArcOfHyperbola(const Part::Geometry& geom)
78
return geom.is<Part::GeomArcOfHyperbola>();
81
bool Sketcher::isArcOfParabola(const Part::Geometry& geom)
83
return geom.is<Part::GeomArcOfParabola>();
86
bool Sketcher::isBSplineCurve(const Part::Geometry& geom)
88
return geom.is<Part::GeomBSplineCurve>();
91
bool Sketcher::isPeriodicBSplineCurve(const Part::Geometry& geom)
93
if (geom.is<Part::GeomBSplineCurve>()) {
94
auto* spline = static_cast<const Part::GeomBSplineCurve*>(&geom);
95
return spline->isPeriodic();
100
bool Sketcher::isPoint(const Part::Geometry& geom)
102
return geom.is<Part::GeomPoint>();
105
bool Sketcher::isCircleOrArc(const Part::Geometry& geo)
107
return isCircle(geo) || isArcOfCircle(geo);
110
std::tuple<double, Base::Vector3d> Sketcher::getRadiusCenterCircleArc(const Part::Geometry* geo)
112
if (isArcOfCircle(*geo)) {
113
auto arc = static_cast<const Part::GeomArcOfCircle*>(geo); // NOLINT
114
return std::tuple<double, Base::Vector3d>(arc->getRadius(), arc->getCenter());
116
else if (isCircle(*geo)) {
117
auto circ = static_cast<const Part::GeomCircle*>(geo); // NOLINT
118
return std::tuple<double, Base::Vector3d>(circ->getRadius(), circ->getCenter());
121
THROWM(Base::TypeError, "getRadiusCenterCircleArc - Neither an arc nor a circle")
124
bool SketcherGui::tryAutoRecompute(Sketcher::SketchObject* obj, bool& autoremoveredundants)
126
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
127
"User parameter:BaseApp/Preferences/Mod/Sketcher");
128
bool autoRecompute = hGrp->GetBool("AutoRecompute", false);
129
bool autoRemoveRedundants = hGrp->GetBool("AutoRemoveRedundants", false);
131
// We need to make sure the solver has right redundancy information before trying to remove the
132
// redundants. for example if a non-driving constraint has been added.
133
if (autoRemoveRedundants && autoRecompute) {
137
if (autoRemoveRedundants) {
138
obj->autoRemoveRedundants();
142
Gui::Command::updateActive();
145
autoremoveredundants = autoRemoveRedundants;
147
return autoRecompute;
150
bool SketcherGui::tryAutoRecompute(Sketcher::SketchObject* obj)
152
bool autoremoveredundants;
154
return tryAutoRecompute(obj, autoremoveredundants);
157
void SketcherGui::tryAutoRecomputeIfNotSolve(Sketcher::SketchObject* obj)
159
bool autoremoveredundants;
161
if (!tryAutoRecompute(obj, autoremoveredundants)) {
164
if (autoremoveredundants) {
165
obj->autoRemoveRedundants();
170
std::string SketcherGui::getStrippedPythonExceptionString(const Base::Exception& e)
172
std::string msg = e.what();
174
if (msg.length() > 26 && msg.substr(0, 26) == "FreeCAD exception thrown (") {
175
return msg.substr(26, msg.length() - 27);
182
bool SketcherGui::ReleaseHandler(Gui::Document* doc)
186
&& doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
187
SketcherGui::ViewProviderSketch* vp =
188
static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
190
if (static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit())->getSketchMode()
191
== ViewProviderSketch::STATUS_SKETCH_UseHandler) {
201
void SketcherGui::getIdsFromName(const std::string& name,
202
const Sketcher::SketchObject* Obj,
206
GeoId = GeoEnum::GeoUndef;
207
PosId = Sketcher::PointPos::none;
209
if (name.size() > 4 && name.substr(0, 4) == "Edge") {
210
GeoId = std::atoi(name.substr(4, 4000).c_str()) - 1;
212
else if (name.size() == 9 && name.substr(0, 9) == "RootPoint") {
213
GeoId = Sketcher::GeoEnum::RtPnt;
214
PosId = Sketcher::PointPos::start;
216
else if (name.size() == 6 && name.substr(0, 6) == "H_Axis") {
217
GeoId = Sketcher::GeoEnum::HAxis;
219
else if (name.size() == 6 && name.substr(0, 6) == "V_Axis") {
220
GeoId = Sketcher::GeoEnum::VAxis;
222
else if (name.size() > 12 && name.substr(0, 12) == "ExternalEdge") {
223
GeoId = Sketcher::GeoEnum::RefExt + 1 - std::atoi(name.substr(12, 4000).c_str());
225
else if (name.size() > 6 && name.substr(0, 6) == "Vertex") {
226
int VtId = std::atoi(name.substr(6, 4000).c_str()) - 1;
227
Obj->getGeoVertexIndex(VtId, GeoId, PosId);
231
std::vector<int> SketcherGui::getGeoIdsOfEdgesFromNames(const Sketcher::SketchObject* Obj,
232
const std::vector<std::string>& names)
234
std::vector<int> geoids;
236
for (const auto& name : names) {
237
if (name.size() > 4 && name.substr(0, 4) == "Edge") {
238
geoids.push_back(std::atoi(name.substr(4, 4000).c_str()) - 1);
240
else if (name.size() > 12 && name.substr(0, 12) == "ExternalEdge") {
241
geoids.push_back(Sketcher::GeoEnum::RefExt + 1
242
- std::atoi(name.substr(12, 4000).c_str()));
244
else if (name.size() > 6 && name.substr(0, 6) == "Vertex") {
245
int VtId = std::atoi(name.substr(6, 4000).c_str()) - 1;
247
Sketcher::PointPos PosId;
248
Obj->getGeoVertexIndex(VtId, GeoId, PosId);
249
const Part::Geometry* geo = Obj->getGeometry(GeoId);
250
if (geo->is<Part::GeomPoint>()) {
251
geoids.push_back(GeoId);
259
bool SketcherGui::checkBothExternal(int GeoId1, int GeoId2)
261
if (GeoId1 == GeoEnum::GeoUndef || GeoId2 == GeoEnum::GeoUndef) {
265
return (GeoId1 < 0 && GeoId2 < 0);
269
bool SketcherGui::isPointOrSegmentFixed(const Sketcher::SketchObject* Obj, int GeoId)
271
const std::vector<Sketcher::Constraint*>& vals = Obj->Constraints.getValues();
273
if (GeoId == GeoEnum::GeoUndef) {
277
return checkConstraint(vals, Sketcher::Block, GeoId, Sketcher::PointPos::none)
278
|| GeoId <= Sketcher::GeoEnum::RtPnt;
282
bool SketcherGui::areBothPointsOrSegmentsFixed(const Sketcher::SketchObject* Obj,
286
const std::vector<Sketcher::Constraint*>& vals = Obj->Constraints.getValues();
288
if (GeoId1 == GeoEnum::GeoUndef || GeoId2 == GeoEnum::GeoUndef) {
292
return ((checkConstraint(vals, Sketcher::Block, GeoId1, Sketcher::PointPos::none)
293
|| GeoId1 <= Sketcher::GeoEnum::RtPnt)
294
&& (checkConstraint(vals, Sketcher::Block, GeoId2, Sketcher::PointPos::none)
295
|| GeoId2 <= Sketcher::GeoEnum::RtPnt));
299
bool SketcherGui::areAllPointsOrSegmentsFixed(const Sketcher::SketchObject* Obj,
304
const std::vector<Sketcher::Constraint*>& vals = Obj->Constraints.getValues();
306
if (GeoId1 == GeoEnum::GeoUndef || GeoId2 == GeoEnum::GeoUndef || GeoId3 == GeoEnum::GeoUndef) {
310
return ((checkConstraint(vals, Sketcher::Block, GeoId1, Sketcher::PointPos::none)
311
|| GeoId1 <= Sketcher::GeoEnum::RtPnt)
312
&& (checkConstraint(vals, Sketcher::Block, GeoId2, Sketcher::PointPos::none)
313
|| GeoId2 <= Sketcher::GeoEnum::RtPnt)
314
&& (checkConstraint(vals, Sketcher::Block, GeoId3, Sketcher::PointPos::none)
315
|| GeoId3 <= Sketcher::GeoEnum::RtPnt));
319
bool SketcherGui::isSimpleVertex(const Sketcher::SketchObject* Obj, int GeoId, PointPos PosId)
321
if (PosId == Sketcher::PointPos::start
322
&& (GeoId == Sketcher::GeoEnum::HAxis || GeoId == Sketcher::GeoEnum::VAxis)) {
325
const Part::Geometry* geo = Obj->getGeometry(GeoId);
326
if (geo->is<Part::GeomPoint>()) {
329
else if (PosId == Sketcher::PointPos::mid) {
337
bool SketcherGui::isBsplineKnot(const Sketcher::SketchObject* Obj, int GeoId)
339
auto gf = Obj->getGeometryFacade(GeoId);
340
return (gf && gf->getInternalType() == Sketcher::InternalType::BSplineKnotPoint);
343
bool SketcherGui::isBsplineKnotOrEndPoint(const Sketcher::SketchObject* Obj,
345
Sketcher::PointPos PosId)
347
// check first using geometry facade
348
if (isBsplineKnot(Obj, GeoId)) {
352
const Part::Geometry* geo = Obj->getGeometry(GeoId);
353
// end points of B-Splines are also knots
354
if (geo->is<Part::GeomBSplineCurve>()
355
&& (PosId == Sketcher::PointPos::start || PosId == Sketcher::PointPos::end)) {
362
bool SketcherGui::IsPointAlreadyOnCurve(int GeoIdCurve,
364
Sketcher::PointPos PosIdPoint,
365
Sketcher::SketchObject* Obj)
367
// This func is a "smartness" behind three-element tangent-, perp.- and angle-via-point.
368
// We want to find out, if the point supplied by user is already on
369
// both of the curves. If not, necessary point-on-object constraints
370
// are to be added automatically.
371
// Simple geometric test seems to be the best, because a point can be
372
// constrained to a curve in a number of ways (e.g. it is an endpoint of an
373
// arc, or is coincident to endpoint of an arc, or it is an endpoint of an
374
// ellipse's major diameter line). Testing all those possibilities is way
375
// too much trouble, IMO(DeepSOIC).
376
// One exception: check for knots on their B-splines, at least until point on B-spline is
377
// implemented. (Ajinkya)
378
if (isBsplineKnot(Obj, GeoIdPoint)) {
379
const Part::Geometry* geoCurve = Obj->getGeometry(GeoIdCurve);
380
if (geoCurve->is<Part::GeomBSplineCurve>()) {
381
const std::vector<Constraint*>& constraints = Obj->Constraints.getValues();
382
for (const auto& constraint : constraints) {
383
if (constraint->Type == Sketcher::ConstraintType::InternalAlignment
384
&& constraint->First == GeoIdPoint && constraint->Second == GeoIdCurve) {
391
Base::Vector3d p = Obj->getPoint(GeoIdPoint, PosIdPoint);
392
return Obj->isPointOnCurve(GeoIdCurve, p.x, p.y);
395
bool SketcherGui::isBsplinePole(const Part::Geometry* geo)
397
auto gf = GeometryFacade::getFacade(geo);
400
return gf->getInternalType() == InternalType::BSplineControlPoint;
403
THROWM(Base::ValueError, "Null geometry in isBsplinePole - please report")
406
bool SketcherGui::isBsplinePole(const Sketcher::SketchObject* Obj, int GeoId)
409
auto geom = Obj->getGeometry(GeoId);
411
return isBsplinePole(geom);
414
bool SketcherGui::checkConstraint(const std::vector<Sketcher::Constraint*>& vals,
419
for (std::vector<Sketcher::Constraint*>::const_iterator itc = vals.begin(); itc != vals.end();
421
if ((*itc)->Type == type && (*itc)->First == geoid && (*itc)->FirstPos == pos) {
429
/* helper functions ======================================================*/
431
// Return counter-clockwise angle from horizontal out of p1 to p2 in radians.
432
double SketcherGui::GetPointAngle(const Base::Vector2d& p1, const Base::Vector2d& p2)
434
double dX = p2.x - p1.x;
435
double dY = p2.y - p1.y;
436
return dY >= 0 ? atan2(dY, dX) : atan2(dY, dX) + 2 * M_PI;
439
// Set the two points on circles at minimal distance
440
// in concentric case set points on relative X axis
441
void SketcherGui::GetCirclesMinimalDistance(const Part::Geometry* geom1,
442
const Part::Geometry* geom2,
443
Base::Vector3d& point1,
444
Base::Vector3d& point2)
446
// This will throw if geom1 or geom2 are not circles or arcs
447
auto [radius1, center1] = getRadiusCenterCircleArc(geom1);
448
auto [radius2, center2] = getRadiusCenterCircleArc(geom2);
453
Base::Vector3d v = point2 - point1;
454
double length = v.Length();
456
if (length == 0) { // concentric case
462
if (length <= std::max(radius1, radius2)) { // inner case
463
if (radius1 > radius2) {
464
point1 += v * radius1;
465
point2 += v * radius2;
468
point1 += -v * radius1;
469
point2 += -v * radius2;
473
point1 += v * radius1;
474
point2 += -v * radius2;
479
void SketcherGui::ActivateHandler(Gui::Document* doc, std::unique_ptr<DrawSketchHandler> handler)
483
&& doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
484
SketcherGui::ViewProviderSketch* vp =
485
static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
487
vp->activateHandler(std::move(handler));
492
bool SketcherGui::isSketchInEdit(Gui::Document* doc)
495
// checks if a Sketch Viewprovider is in Edit and is in no special mode
496
auto* vp = dynamic_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
497
return (vp != nullptr);
502
bool SketcherGui::isCommandActive(Gui::Document* doc, bool actsOnSelection)
504
if (isSketchInEdit(doc)) {
506
static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit())->getSketchMode();
508
if (mode == ViewProviderSketch::STATUS_NONE
509
|| mode == ViewProviderSketch::STATUS_SKETCH_UseHandler) {
511
if (!actsOnSelection) {
514
else if (Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId())
524
bool SketcherGui::isSketcherBSplineActive(Gui::Document* doc, bool actsOnSelection)
527
// checks if a Sketch Viewprovider is in Edit and is in no special mode
529
&& doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
530
if (static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit())->getSketchMode()
531
== ViewProviderSketch::STATUS_NONE) {
532
if (!actsOnSelection) {
535
else if (Gui::Selection().countObjectsOfType(
536
Sketcher::SketchObject::getClassTypeId())
546
SketcherGui::ViewProviderSketch*
547
SketcherGui::getInactiveHandlerEditModeSketchViewProvider(Gui::Document* doc)
550
return dynamic_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
556
SketcherGui::ViewProviderSketch* SketcherGui::getInactiveHandlerEditModeSketchViewProvider()
558
Gui::Document* doc = Gui::Application::Instance->activeDocument();
560
return getInactiveHandlerEditModeSketchViewProvider(doc);
563
void SketcherGui::removeRedundantHorizontalVertical(Sketcher::SketchObject* psketch,
564
std::vector<AutoConstraint>& sug1,
565
std::vector<AutoConstraint>& sug2)
567
if (!sug1.empty() && !sug2.empty()) {
569
bool rmvhorvert = false;
572
// 1. Coincident to external on both endpoints
573
// 2. Coincident in one endpoint to origin and pointonobject/tangent to an axis on the other
574
auto detectredundant =
575
[psketch](std::vector<AutoConstraint>& sug, bool& ext, bool& orig, bool& axis) {
580
for (std::vector<AutoConstraint>::const_iterator it = sug.begin(); it != sug.end();
582
if ((*it).Type == Sketcher::Coincident && !ext) {
583
const std::map<int, Sketcher::PointPos> coincidents =
584
psketch->getAllCoincidentPoints((*it).GeoId, (*it).PosId);
586
if (!coincidents.empty()) {
587
// the keys are ordered, so if the first is negative, it is coincident
589
ext = coincidents.begin()->first < 0;
591
std::map<int, Sketcher::PointPos>::const_iterator geoId1iterator;
593
geoId1iterator = coincidents.find(-1);
595
if (geoId1iterator != coincidents.end()) {
596
if ((*geoId1iterator).second == Sketcher::PointPos::start) {
601
else { // it may be that there is no constraint at all, but there is
603
ext = (*it).GeoId < 0;
604
orig = ((*it).GeoId == -1 && (*it).PosId == Sketcher::PointPos::start);
607
else if ((*it).Type == Sketcher::PointOnObject && !axis) {
608
axis = (((*it).GeoId == -1 && (*it).PosId == Sketcher::PointPos::none)
609
|| ((*it).GeoId == -2 && (*it).PosId == Sketcher::PointPos::none));
614
bool firstext = false, secondext = false, firstorig = false, secondorig = false,
615
firstaxis = false, secondaxis = false;
617
detectredundant(sug1, firstext, firstorig, firstaxis);
618
detectredundant(sug2, secondext, secondorig, secondaxis);
622
((firstext && secondext) || // coincident with external on both endpoints
623
(firstorig && secondaxis) || // coincident origin and point on object on other
624
(secondorig && firstaxis));
627
for (std::vector<AutoConstraint>::reverse_iterator it = sug2.rbegin();
630
if ((*it).Type == Sketcher::Horizontal || (*it).Type == Sketcher::Vertical) {
631
sug2.erase(std::next(it).base());
632
it = sug2.rbegin(); // erase invalidates the iterator
639
void SketcherGui::ConstraintToAttachment(Sketcher::GeoElementId element,
640
Sketcher::GeoElementId attachment,
642
App::DocumentObject* obj)
644
if (distance == 0.) {
646
if (attachment.isCurve()) {
647
Gui::cmdAppObjectArgs(obj,
648
"addConstraint(Sketcher.Constraint('PointOnObject',%d,%d,%d)) ",
650
element.posIdAsInt(),
654
Gui::cmdAppObjectArgs(obj,
655
"addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
657
element.posIdAsInt(),
659
attachment.posIdAsInt());
663
if (attachment == Sketcher::GeoElementId::VAxis) {
664
Gui::cmdAppObjectArgs(obj,
665
"addConstraint(Sketcher.Constraint('DistanceX',%d,%d,%f)) ",
667
element.posIdAsInt(),
670
else if (attachment == Sketcher::GeoElementId::HAxis) {
671
Gui::cmdAppObjectArgs(obj,
672
"addConstraint(Sketcher.Constraint('DistanceY',%d,%d,%f)) ",
674
element.posIdAsInt(),
681
// convenience functions for cursor display
682
bool SketcherGui::hideUnits()
684
Base::Reference<ParameterGrp> hGrp = App::GetApplication()
687
->GetGroup("Preferences")
688
->GetGroup("Mod/Sketcher");
689
return hGrp->GetBool("HideUnits", false);
692
bool SketcherGui::showCursorCoords()
694
Base::Reference<ParameterGrp> hGrp = App::GetApplication()
697
->GetGroup("Preferences")
698
->GetGroup("Mod/Sketcher");
699
return hGrp->GetBool("ShowCursorCoords", true); // true for testing. set to false for prod.
702
bool SketcherGui::useSystemDecimals()
704
Base::Reference<ParameterGrp> hGrp = App::GetApplication()
707
->GetGroup("Preferences")
708
->GetGroup("Mod/Sketcher");
709
return hGrp->GetBool("UseSystemDecimals", true);
712
// convert value to display format %0.[digits]f. Units are displayed if
713
// preference "ShowUnits" is true, or if the unit schema in effect uses
714
// multiple units (ex. Ft/In). Digits parameter is ignored for multi-unit
716
// TODO:: if the user string is delivered in 1.23e45 format, this might not work
718
std::string SketcherGui::lengthToDisplayFormat(double value, int digits)
720
Base::Quantity asQuantity;
721
asQuantity.setValue(value);
722
asQuantity.setUnit(Base::Unit::Length);
723
QString qUserString = asQuantity.getUserString();
724
if (Base::UnitsApi::isMultiUnitLength() || (!hideUnits() && useSystemDecimals())) {
725
// just return the user string
726
return Base::Tools::toStdString(qUserString);
729
// find the unit of measure
732
QString qtranslate = Base::UnitsApi::schemaTranslate(asQuantity, factor, qUnitString);
733
QString unitPart = QString::fromUtf8(" ") + qUnitString;
735
// get the numeric part of the user string
736
QRegularExpression rxNoUnits(
737
QString::fromUtf8("(.*) \\D*$")); // text before space + any non digits at end of string
738
QRegularExpressionMatch match = rxNoUnits.match(qUserString);
739
if (!match.hasMatch()) {
740
// no units in userString?
741
return Base::Tools::toStdString(qUserString);
743
QString matched = match.captured(1); // matched is the numeric part of user string
744
int dpPos = matched.indexOf(QLocale().decimalPoint());
746
// no decimal separator (ie an integer), return all the digits
748
return Base::Tools::toStdString(matched);
751
return Base::Tools::toStdString(matched + unitPart);
756
if (useSystemDecimals() && hideUnits()) {
757
// return just the numeric part of the user string
758
return Base::Tools::toStdString(matched);
761
// real number and not using system decimals
762
int requiredLength = dpPos + digits + 1;
763
if (requiredLength > matched.size()) {
764
// just take the whole thing
765
requiredLength = matched.size();
767
QString numericPart = matched.left(requiredLength);
769
return Base::Tools::toStdString(numericPart);
771
return Base::Tools::toStdString(numericPart + unitPart);
774
// convert value to display format %0.[digits]f. Units are always displayed for
775
// angles - 123.456° or 12°34'56". Digits parameter is ignored for multi-unit
776
// schemata. Note small differences between this method and lengthToDisplyFormat
777
// TODO:: if the user string is delivered in 1.23e45 format, this might not work
779
std::string SketcherGui::angleToDisplayFormat(double value, int digits)
781
Base::Quantity asQuantity;
782
asQuantity.setValue(value);
783
asQuantity.setUnit(Base::Unit::Angle);
784
QString qUserString = asQuantity.getUserString();
785
if (Base::UnitsApi::isMultiUnitAngle()) {
786
// just return the user string
787
// Coin SbString doesn't handle utf8 well, so we convert to ascii
788
QString schemeMinute = QString::fromUtf8("\xE2\x80\xB2"); // prime symbol
789
QString schemeSecond = QString::fromUtf8("\xE2\x80\xB3"); // double prime symbol
790
QString escapeMinute = QString::fromLatin1("\'"); // substitute ascii single quote
791
QString escapeSecond = QString::fromLatin1("\""); // substitute ascii double quote
792
QString displayString = qUserString.replace(schemeMinute, escapeMinute);
793
displayString = displayString.replace(schemeSecond, escapeSecond);
794
return Base::Tools::toStdString(displayString);
797
// we always use use U+00B0 (°) as the unit of measure for angles in
798
// single unit schema. Will need a change to support rads or grads.
799
auto qUnitString = QString::fromUtf8("°");
800
auto decimalSep = QLocale().decimalPoint();
802
// get the numeric part of the user string
803
QRegularExpression rxNoUnits(QString::fromUtf8("(\\d*\\%1?\\d*)(\\D*)$")
804
.arg(decimalSep)); // number + non digits at end of string
805
QRegularExpressionMatch match = rxNoUnits.match(qUserString);
806
if (!match.hasMatch()) {
807
// no units in userString?
808
return Base::Tools::toStdString(qUserString);
810
QString matched = match.captured(1); // matched is the numeric part of user string
811
int dpPos = matched.indexOf(decimalSep);
813
// no decimal separator (ie an integer), return all the digits
814
return Base::Tools::toStdString(matched + qUnitString);
818
if (useSystemDecimals()) {
819
// return just the numeric part of the user string + degree symbol
820
return Base::Tools::toStdString(matched + qUnitString);
823
// real number and not using system decimals
824
int requiredLength = dpPos + digits + 1;
825
if (requiredLength > matched.size()) {
826
// just take the whole thing
827
requiredLength = matched.size();
829
QString numericPart = matched.left(requiredLength);
830
return Base::Tools::toStdString(numericPart + qUnitString);
834
bool SketcherGui::areCollinear(const Base::Vector2d& p1,
835
const Base::Vector2d& p2,
836
const Base::Vector2d& p3)
838
Base::Vector2d u = p2 - p1;
839
Base::Vector2d v = p3 - p2;
840
Base::Vector2d w = p1 - p3;
846
double eps2 = Precision::SquareConfusion();
847
if (uu < eps2 || vv < eps2 || ww < eps2) {
851
double uv = -(u * v);
852
double vw = -(v * w);
853
double uw = -(u * w);
855
double w0 = (2 * sqrt(abs(uu * ww - uw * uw)) * uw / (uu * ww));
856
double w1 = (2 * sqrt(abs(uu * vv - uv * uv)) * uv / (uu * vv));
857
double w2 = (2 * sqrt(abs(vv * ww - vw * vw)) * vw / (vv * ww));
859
double wx = w0 + w1 + w2;
861
if (abs(wx) < Precision::Confusion()) {
868
int SketcherGui::indexOfGeoId(const std::vector<int>& vec, int elem)
870
if (elem == GeoEnum::GeoUndef) {
871
return GeoEnum::GeoUndef;
873
for (size_t i = 0; i < vec.size(); i++) {
874
if (vec[i] == elem) {
875
return static_cast<int>(i);