FreeCAD

Форк
0
/
MeasureArea.cpp 
161 строка · 5.1 Кб
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/MeasureManager.h>
27
#include <App/Document.h>
28

29
#include "MeasureArea.h"
30

31

32
using namespace Measure;
33

34
PROPERTY_SOURCE(Measure::MeasureArea, Measure::MeasureBase)
35

36

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

47
    ADD_PROPERTY_TYPE(Area,
48
                      (0.0),
49
                      "Measurement",
50
                      App::PropertyType(App::Prop_ReadOnly | App::Prop_Output),
51
                      "Area of element");
52
}
53

54
MeasureArea::~MeasureArea() = default;
55

56

57
bool MeasureArea::isValidSelection(const App::MeasureSelection& selection)
58
{
59

60
    if (selection.empty()) {
61
        return false;
62
    }
63

64
    for (auto element : selection) {
65
        auto type = App::MeasureManager::getMeasureElementType(element);
66

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

71
        // TODO: Also support Cylinder & Volume?
72
        if ((type != App::MeasureElementType::PLANE && type != App::MeasureElementType::CYLINDER)) {
73
            return false;
74
        }
75
    }
76
    return true;
77
}
78

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

83
    std::vector<App::DocumentObject*> objects;
84
    std::vector<std::string> subElements;
85

86
    for (auto element : selection) {
87
        auto objT = element.object;
88

89
        objects.push_back(objT.getObject());
90
        subElements.push_back(objT.getSubName());
91
    }
92

93
    Elements.setValues(objects, subElements);
94
}
95

96

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

102
    double result(0.0);
103

104
    // Loop through Elements and call the valid geometry handler
105
    for (std::vector<App::DocumentObject*>::size_type i = 0; i < objects.size(); i++) {
106
        App::SubObjectT subject {objects.at(i), subElements.at(i).c_str()};
107

108
        auto info = getMeasureInfo(subject);
109
        if (!info || !info->valid) {
110
            return new App::DocumentObjectExecReturn("Cannot calculate area");
111
        }
112
        auto areaInfo = std::dynamic_pointer_cast<Part::MeasureAreaInfo>(info);
113
        result += areaInfo->area;
114
    }
115

116
    Area.setValue(result);
117
    return DocumentObject::StdReturn;
118
}
119

120

121
void MeasureArea::onChanged(const App::Property* prop)
122
{
123
    if (isRestoring() || isRemoving()) {
124
        return;
125
    }
126

127
    if (prop == &Elements) {
128
        auto ret = recompute();
129
        delete ret;
130
    }
131

132
    MeasureBase::onChanged(prop);
133
}
134

135

136
Base::Placement MeasureArea::getPlacement()
137
{
138
    const std::vector<App::DocumentObject*>& objects = Elements.getValues();
139
    const std::vector<std::string>& subElements = Elements.getSubValues();
140

141
    if (objects.empty() || subElements.empty()) {
142
        return Base::Placement();
143
    }
144

145
    App::SubObjectT subject {objects.front(), subElements.front().c_str()};
146

147
    auto info = getMeasureInfo(subject);
148
    if (!info) {
149
        return {};
150
    }
151
    auto areaInfo = std::dynamic_pointer_cast<Part::MeasureAreaInfo>(info);
152
    return areaInfo->placement;
153
}
154

155

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

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

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

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

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