1
/***************************************************************************
2
* Copyright (c) 2023 David Friedli <david[at]friedli-be.ch> *
4
* This file is part of FreeCAD. *
6
* FreeCAD is free software: you can redistribute it and/or modify it *
7
* under the terms of the GNU Lesser General Public License as *
8
* published by the Free Software Foundation, either version 2.1 of the *
9
* License, or (at your option) any later version. *
11
* FreeCAD is distributed in the hope that it will be useful, but *
12
* WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14
* Lesser General Public License for more details. *
16
* You should have received a copy of the GNU Lesser General Public *
17
* License along with FreeCAD. If not, see *
18
* <https://www.gnu.org/licenses/>. *
20
**************************************************************************/
23
#include "PreCompiled.h"
25
#include <App/PropertyContainer.h>
26
#include <App/Application.h>
27
#include <App/MeasureManager.h>
28
#include <App/Document.h>
30
#include "MeasurePosition.h"
33
using namespace Measure;
35
PROPERTY_SOURCE(Measure::MeasurePosition, Measure::MeasureBase)
38
MeasurePosition::MeasurePosition()
40
ADD_PROPERTY_TYPE(Element,
44
"Element to get the position from");
45
Element.setScope(App::LinkScope::Global);
46
Element.setAllowExternal(true);
49
ADD_PROPERTY_TYPE(Position,
52
App::PropertyType(App::Prop_ReadOnly | App::Prop_Output),
53
"The absolute position");
56
MeasurePosition::~MeasurePosition() = default;
59
bool MeasurePosition::isValidSelection(const App::MeasureSelection& selection)
62
if (selection.empty() || selection.size() > 1) {
66
for (auto element : selection) {
67
auto type = App::MeasureManager::getMeasureElementType(element);
69
if (type == App::MeasureElementType::INVALID) {
73
if (type != App::MeasureElementType::POINT) {
80
void MeasurePosition::parseSelection(const App::MeasureSelection& selection)
82
// Set properties from selection, method is only invoked when isValid Selection returns true
84
for (auto element : selection) {
85
auto objT = element.object;
87
std::vector<std::string> subElements {objT.getSubName()};
88
Element.setValue(objT.getObject(), subElements);
94
App::DocumentObjectExecReturn* MeasurePosition::execute()
96
const App::DocumentObject* object = Element.getValue();
97
const std::vector<std::string>& subElements = Element.getSubValues();
99
App::SubObjectT subject {object, subElements.front().c_str()};
100
auto info = getMeasureInfo(subject);
102
if (!info || !info->valid) {
103
return new App::DocumentObjectExecReturn("Cannot calculate position");
106
auto positionInfo = std::dynamic_pointer_cast<Part::MeasurePositionInfo>(info);
107
Position.setValue(positionInfo->position);
108
return DocumentObject::StdReturn;
112
void MeasurePosition::onChanged(const App::Property* prop)
114
if (isRestoring() || isRemoving()) {
118
if (prop == &Element) {
119
auto ret = recompute();
122
DocumentObject::onChanged(prop);
126
QString MeasurePosition::getResultString()
128
App::Property* prop = this->getResultProp();
129
if (prop == nullptr) {
133
Base::Vector3d value = Position.getValue();
134
QString unit = Position.getUnit().getString();
137
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
138
QTextStream(&text) << "X: " << QString::number(value.x, 'f', precision) << " " << unit << endl
139
<< "Y: " << QString::number(value.y, 'f', precision) << " " << unit << endl
140
<< "Z: " << QString::number(value.z, 'f', precision) << " " << unit;
142
QTextStream(&text) << "X: " << QString::number(value.x, 'f', precision) << " " << unit
144
<< "Y: " << QString::number(value.y, 'f', precision) << " " << unit
146
<< "Z: " << QString::number(value.z, 'f', precision) << " " << unit;
152
Base::Placement MeasurePosition::getPlacement()
154
Base::Placement placement;
155
placement.setPosition(Position.getValue());
160
//! Return the object we are measuring
161
//! used by the viewprovider in determining visibility
162
std::vector<App::DocumentObject*> MeasurePosition::getSubject() const
164
return {Element.getValue()};