FreeCAD

Форк
0
/
ViewProviderCompound.cpp 
181 строка · 7.0 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <TopExp.hxx>
27
# include <TopTools_IndexedMapOfShape.hxx>
28
#endif
29

30
#include <Gui/Application.h>
31
#include <Mod/Part/App/FeatureCompound.h>
32

33
#include "ViewProviderCompound.h"
34

35

36
using namespace PartGui;
37

38
PROPERTY_SOURCE(PartGui::ViewProviderCompound,PartGui::ViewProviderPart)
39

40
ViewProviderCompound::ViewProviderCompound()
41
{
42
    sPixmap = "Part_Compound.svg";
43
}
44

45
ViewProviderCompound::~ViewProviderCompound() = default;
46

47
std::vector<App::DocumentObject*> ViewProviderCompound::claimChildren() const
48
{
49
    return static_cast<Part::Compound*>(getObject())->Links.getValues();
50
}
51

52
bool ViewProviderCompound::onDelete(const std::vector<std::string> &)
53
{
54
    // get the input shapes
55
    Part::Compound* pComp = static_cast<Part::Compound*>(getObject());
56
    std::vector<App::DocumentObject*> pLinks = pComp->Links.getValues();
57
    for (auto pLink : pLinks) {
58
        if (pLink)
59
            Gui::Application::Instance->showViewProvider(pLink);
60
    }
61

62
    return true;
63
}
64

65
void ViewProviderCompound::updateData(const App::Property* prop)
66
{
67
    PartGui::ViewProviderPart::updateData(prop);
68
    if (prop->is<Part::PropertyShapeHistory>()) {
69
        const std::vector<Part::ShapeHistory>& hist = static_cast<const Part::PropertyShapeHistory*>
70
            (prop)->getValues();
71
        Part::Compound* objComp = static_cast<Part::Compound*>(getObject());
72
        std::vector<App::DocumentObject*> sources = objComp->Links.getValues();
73

74
        if (hist.size() != sources.size()) {
75
            // avoid duplicates without changing the order
76
            // See also Compound::execute
77
            std::set<App::DocumentObject*> tempSources;
78
            std::vector<App::DocumentObject*> filter;
79
            for (auto source : sources) {
80
                Part::Feature* objBase = dynamic_cast<Part::Feature*>(source);
81
                if (objBase) {
82
                    auto pos = tempSources.insert(objBase);
83
                    if (pos.second) {
84
                        filter.push_back(objBase);
85
                    }
86
                }
87
            }
88

89
            sources = filter;
90
        }
91
        if (hist.size() != sources.size())
92
            return;
93

94
        const TopoDS_Shape& compShape = objComp->Shape.getValue();
95
        TopTools_IndexedMapOfShape compMap;
96
        TopExp::MapShapes(compShape, TopAbs_FACE, compMap);
97

98
        std::vector<App::Material> compCol;
99
        compCol.resize(compMap.Extent(), this->ShapeAppearance[0]);
100

101
        int index=0;
102
        for (std::vector<App::DocumentObject*>::iterator it = sources.begin(); it != sources.end(); ++it, ++index) {
103
            Part::Feature* objBase = dynamic_cast<Part::Feature*>(Part::Feature::getShapeOwner(*it));
104
            if (!objBase)
105
                continue;
106

107
            const TopoDS_Shape& baseShape = objBase->Shape.getValue();
108

109
            TopTools_IndexedMapOfShape baseMap;
110
            TopExp::MapShapes(baseShape, TopAbs_FACE, baseMap);
111

112
            auto vpBase = dynamic_cast<PartGui::ViewProviderPart*>(Gui::Application::Instance->getViewProvider(objBase));
113
            if (vpBase) {
114
                std::vector<App::Material> baseCol = vpBase->ShapeAppearance.getValues();
115
                applyTransparency(vpBase->Transparency.getValue(), baseCol);
116
                if (static_cast<int>(baseCol.size()) == baseMap.Extent()) {
117
                    applyMaterial(hist[index], baseCol, compCol);
118
                }
119
                else if (!baseCol.empty() && baseCol[0] != this->ShapeAppearance[0]) {
120
                    baseCol.resize(baseMap.Extent(), baseCol[0]);
121
                    applyMaterial(hist[index], baseCol, compCol);
122
                }
123
            }
124
        }
125

126
        // If the view provider has set a transparency then override the values
127
        // of the input shapes
128
        if (Transparency.getValue() > 0) {
129
            applyTransparency(Transparency.getValue(), compCol);
130
        }
131

132
        this->ShapeAppearance.setValues(compCol);
133
    }
134
    else if (prop->isDerivedFrom<App::PropertyLinkList>()) {
135
        const std::vector<App::DocumentObject *>& pBases = static_cast<const App::PropertyLinkList*>(prop)->getValues();
136
        for (auto pBase : pBases) {
137
            if (pBase) Gui::Application::Instance->hideViewProvider(pBase);
138
        }
139
    }
140
}
141

142
bool ViewProviderCompound::canDragObjects() const
143
{
144
    return true;
145
}
146

147
bool ViewProviderCompound::canDragObject(App::DocumentObject* obj) const
148
{
149
    return obj->isDerivedFrom<Part::Feature>();
150
}
151

152
void ViewProviderCompound::dragObject(App::DocumentObject* obj)
153
{
154
    Part::Compound* pComp = static_cast<Part::Compound*>(getObject());
155
    std::vector<App::DocumentObject*> pShapes = pComp->Links.getValues();
156
    for (std::vector<App::DocumentObject*>::iterator it = pShapes.begin(); it != pShapes.end(); ++it) {
157
        if (*it == obj) {
158
            pShapes.erase(it);
159
            pComp->Links.setValues(pShapes);
160
            break;
161
        }
162
    }
163
}
164

165
bool ViewProviderCompound::canDropObjects() const
166
{
167
    return true;
168
}
169

170
bool ViewProviderCompound::canDropObject(App::DocumentObject* obj) const
171
{
172
    return obj->isDerivedFrom<Part::Feature>();
173
}
174

175
void ViewProviderCompound::dropObject(App::DocumentObject* obj)
176
{
177
    Part::Compound* pComp = static_cast<Part::Compound*>(getObject());
178
    std::vector<App::DocumentObject*> pShapes = pComp->Links.getValues();
179
    pShapes.push_back(obj);
180
    pComp->Links.setValues(pShapes);
181
}
182

183

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

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

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

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