FreeCAD

Форк
0
/
MeasurePosition.cpp 
165 строк · 5.5 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2023 David Friedli <david[at]friedli-be.ch>             *
3
 *                                                                         *
4
 *   This file is part of FreeCAD.                                         *
5
 *                                                                         *
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.                       *
10
 *                                                                         *
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.                       *
15
 *                                                                         *
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/>.                                      *
19
 *                                                                         *
20
 **************************************************************************/
21

22

23
#include "PreCompiled.h"
24

25
#include <App/PropertyContainer.h>
26
#include <App/Application.h>
27
#include <App/MeasureManager.h>
28
#include <App/Document.h>
29

30
#include "MeasurePosition.h"
31

32

33
using namespace Measure;
34

35
PROPERTY_SOURCE(Measure::MeasurePosition, Measure::MeasureBase)
36

37

38
MeasurePosition::MeasurePosition()
39
{
40
    ADD_PROPERTY_TYPE(Element,
41
                      (nullptr),
42
                      "Measurement",
43
                      App::Prop_None,
44
                      "Element to get the position from");
45
    Element.setScope(App::LinkScope::Global);
46
    Element.setAllowExternal(true);
47

48

49
    ADD_PROPERTY_TYPE(Position,
50
                      (0.0, 0.0, 0.0),
51
                      "Measurement",
52
                      App::PropertyType(App::Prop_ReadOnly | App::Prop_Output),
53
                      "The absolute position");
54
}
55

56
MeasurePosition::~MeasurePosition() = default;
57

58

59
bool MeasurePosition::isValidSelection(const App::MeasureSelection& selection)
60
{
61

62
    if (selection.empty() || selection.size() > 1) {
63
        return false;
64
    }
65

66
    for (auto element : selection) {
67
        auto type = App::MeasureManager::getMeasureElementType(element);
68

69
        if (type == App::MeasureElementType::INVALID) {
70
            return false;
71
        }
72

73
        if (type != App::MeasureElementType::POINT) {
74
            return false;
75
        }
76
    }
77
    return true;
78
}
79

80
void MeasurePosition::parseSelection(const App::MeasureSelection& selection)
81
{
82
    // Set properties from selection, method is only invoked when isValid Selection returns true
83

84
    for (auto element : selection) {
85
        auto objT = element.object;
86

87
        std::vector<std::string> subElements {objT.getSubName()};
88
        Element.setValue(objT.getObject(), subElements);
89
        break;
90
    }
91
}
92

93

94
App::DocumentObjectExecReturn* MeasurePosition::execute()
95
{
96
    const App::DocumentObject* object = Element.getValue();
97
    const std::vector<std::string>& subElements = Element.getSubValues();
98

99
    App::SubObjectT subject {object, subElements.front().c_str()};
100
    auto info = getMeasureInfo(subject);
101

102
    if (!info || !info->valid) {
103
        return new App::DocumentObjectExecReturn("Cannot calculate position");
104
    }
105

106
    auto positionInfo = std::dynamic_pointer_cast<Part::MeasurePositionInfo>(info);
107
    Position.setValue(positionInfo->position);
108
    return DocumentObject::StdReturn;
109
}
110

111

112
void MeasurePosition::onChanged(const App::Property* prop)
113
{
114
    if (isRestoring() || isRemoving()) {
115
        return;
116
    }
117

118
    if (prop == &Element) {
119
        auto ret = recompute();
120
        delete ret;
121
    }
122
    DocumentObject::onChanged(prop);
123
}
124

125

126
QString MeasurePosition::getResultString()
127
{
128
    App::Property* prop = this->getResultProp();
129
    if (prop == nullptr) {
130
        return {};
131
    }
132

133
    Base::Vector3d value = Position.getValue();
134
    QString unit = Position.getUnit().getString();
135
    int precision = 2;
136
    QString text;
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;
141
#else
142
    QTextStream(&text) << "X: " << QString::number(value.x, 'f', precision) << " " << unit
143
                       << Qt::endl
144
                       << "Y: " << QString::number(value.y, 'f', precision) << " " << unit
145
                       << Qt::endl
146
                       << "Z: " << QString::number(value.z, 'f', precision) << " " << unit;
147
#endif
148
    return text;
149
}
150

151

152
Base::Placement MeasurePosition::getPlacement()
153
{
154
    Base::Placement placement;
155
    placement.setPosition(Position.getValue());
156
    return placement;
157
}
158

159

160
//! Return the object we are measuring
161
//! used by the viewprovider in determining visibility
162
std::vector<App::DocumentObject*> MeasurePosition::getSubject() const
163
{
164
    return {Element.getValue()};
165
}
166

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.