FreeCAD

Форк
0
/
MeasureLength.cpp 
164 строки · 5.2 Кб
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/Application.h>
26
#include <App/Document.h>
27
#include <App/MeasureManager.h>
28

29
#include <Mod/Part/App/PartFeature.h>
30

31
#include "MeasureLength.h"
32

33

34
using namespace Measure;
35

36
PROPERTY_SOURCE(Measure::MeasureLength, Measure::MeasureBase)
37

38

39
MeasureLength::MeasureLength()
40
{
41
    ADD_PROPERTY_TYPE(Elements,
42
                      (nullptr),
43
                      "Measurement",
44
                      App::Prop_None,
45
                      "Elements to get the length from");
46
    Elements.setScope(App::LinkScope::Global);
47
    Elements.setAllowExternal(true);
48

49
    ADD_PROPERTY_TYPE(Length,
50
                      (0.0),
51
                      "Measurement",
52
                      App::PropertyType(App::Prop_ReadOnly | App::Prop_Output),
53
                      "Length of selection");
54
}
55

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

58

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

62
    if (selection.empty()) {
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::LINESEGMENT && type != App::MeasureElementType::CIRCLE
74
             && type != App::MeasureElementType::ARC && type != App::MeasureElementType::CURVE)) {
75
            return false;
76
        }
77
    }
78
    return true;
79
}
80

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

85
    std::vector<App::DocumentObject*> objects;
86
    std::vector<std::string> subElements;
87

88
    for (auto element : selection) {
89
        auto objT = element.object;
90

91
        objects.push_back(objT.getObject());
92
        subElements.push_back(objT.getSubName());
93
    }
94

95
    Elements.setValues(objects, subElements);
96
}
97

98

99
App::DocumentObjectExecReturn* MeasureLength::execute()
100
{
101
    const std::vector<App::DocumentObject*>& objects = Elements.getValues();
102
    const std::vector<std::string>& subElements = Elements.getSubValues();
103

104
    double result(0.0);
105

106
    // Loop through Elements and call the valid geometry handler
107
    for (std::vector<App::DocumentObject*>::size_type i = 0; i < objects.size(); i++) {
108

109
        App::SubObjectT subject {objects.at(i), subElements.at(i).c_str()};
110
        auto info = getMeasureInfo(subject);
111
        if (!info || !info->valid) {
112
            return new App::DocumentObjectExecReturn("Cannot calculate length");
113
        }
114

115
        auto lengthInfo = std::dynamic_pointer_cast<Part::MeasureLengthInfo>(info);
116
        result += lengthInfo->length;
117
    }
118

119
    Length.setValue(result);
120
    return DocumentObject::StdReturn;
121
}
122

123

124
void MeasureLength::onChanged(const App::Property* prop)
125
{
126
    if (isRestoring() || isRemoving()) {
127
        return;
128
    }
129

130
    if (prop == &Elements) {
131
        auto ret = recompute();
132
        delete ret;
133
    }
134

135
    MeasureBase::onChanged(prop);
136
}
137

138

139
Base::Placement MeasureLength::getPlacement()
140
{
141
    const std::vector<App::DocumentObject*>& objects = Elements.getValues();
142
    const std::vector<std::string>& subElements = Elements.getSubValues();
143

144
    if (!objects.size() || !subElements.size()) {
145
        return Base::Placement();
146
    }
147

148
    App::SubObjectT subject {objects.front(), subElements.front().c_str()};
149
    auto info = getMeasureInfo(subject);
150

151
    if (!info || !info->valid) {
152
        return {};
153
    }
154
    auto lengthInfo = std::dynamic_pointer_cast<Part::MeasureLengthInfo>(info);
155
    return lengthInfo->placement;
156
}
157

158

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

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

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

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

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