FreeCAD

Форк
0
/
ViewProviderBoolean.cpp 
426 строк · 16.5 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 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 <Gui/BitmapFactory.h>
32
#include <Mod/Part/App/FeaturePartCommon.h>
33
#include <Mod/Part/App/FeaturePartFuse.h>
34

35
#include "ViewProviderBoolean.h"
36

37

38
using namespace PartGui;
39

40
PROPERTY_SOURCE(PartGui::ViewProviderBoolean,PartGui::ViewProviderPart)
41

42
ViewProviderBoolean::ViewProviderBoolean() = default;
43

44
ViewProviderBoolean::~ViewProviderBoolean() = default;
45

46
std::vector<App::DocumentObject*> ViewProviderBoolean::claimChildren()const
47
{
48
    std::vector<App::DocumentObject*> temp;
49
    temp.push_back(static_cast<Part::Boolean*>(getObject())->Base.getValue());
50
    temp.push_back(static_cast<Part::Boolean*>(getObject())->Tool.getValue());
51

52
    return temp;
53
}
54

55
QIcon ViewProviderBoolean::getIcon() const
56
{
57
    App::DocumentObject* obj = getObject();
58
    if (obj) {
59
        Base::Type type = obj->getTypeId();
60
        if (type == Base::Type::fromName("Part::Common"))
61
            return Gui::BitmapFactory().iconFromTheme("Part_Common");
62
        else if (type == Base::Type::fromName("Part::Fuse"))
63
            return Gui::BitmapFactory().iconFromTheme("Part_Fuse");
64
        else if (type == Base::Type::fromName("Part::Cut"))
65
            return Gui::BitmapFactory().iconFromTheme("Part_Cut");
66
        else if (type == Base::Type::fromName("Part::Section"))
67
            return Gui::BitmapFactory().iconFromTheme("Part_Section");
68
    }
69

70
    return ViewProviderPart::getIcon();
71
}
72

73
void ViewProviderBoolean::updateData(const App::Property* prop)
74
{
75
    PartGui::ViewProviderPart::updateData(prop);
76
    if (prop->is<Part::PropertyShapeHistory>()) {
77
        const std::vector<Part::ShapeHistory>& hist = static_cast<const Part::PropertyShapeHistory*>
78
            (prop)->getValues();
79
        if (hist.size() != 2)
80
            return;
81
        Part::Boolean* objBool = dynamic_cast<Part::Boolean*>(getObject());
82
        if (!objBool)
83
            return;
84
        Part::Feature* objBase = dynamic_cast<Part::Feature*>(
85
                Part::Feature::getShapeOwner(objBool->Base.getValue()));
86
        Part::Feature* objTool = dynamic_cast<Part::Feature*>(
87
                Part::Feature::getShapeOwner(objBool->Tool.getValue()));
88
        if (objBase && objTool) {
89
            const TopoDS_Shape& baseShape = objBase->Shape.getValue();
90
            const TopoDS_Shape& toolShape = objTool->Shape.getValue();
91
            const TopoDS_Shape& boolShape = objBool->Shape.getValue();
92

93
            TopTools_IndexedMapOfShape baseMap, toolMap, boolMap;
94
            TopExp::MapShapes(baseShape, TopAbs_FACE, baseMap);
95
            TopExp::MapShapes(toolShape, TopAbs_FACE, toolMap);
96
            TopExp::MapShapes(boolShape, TopAbs_FACE, boolMap);
97

98
            auto vpBase = dynamic_cast<PartGui::ViewProviderPart*>(
99
                    Gui::Application::Instance->getViewProvider(objBase));
100
            auto vpTool = dynamic_cast<PartGui::ViewProviderPart*>(
101
                    Gui::Application::Instance->getViewProvider(objTool));
102
            if (vpBase && vpTool) {
103
                std::vector<App::Material> colBase = vpBase->ShapeAppearance.getValues();
104
                std::vector<App::Material> colTool = vpTool->ShapeAppearance.getValues();
105
                std::vector<App::Material> colBool;
106
                colBool.resize(boolMap.Extent(), this->ShapeAppearance[0]);
107
                applyTransparency(vpBase->Transparency.getValue(),colBase);
108
                applyTransparency(vpTool->Transparency.getValue(),colTool);
109

110
                if (static_cast<int>(colBase.size()) == baseMap.Extent()) {
111
                    applyMaterial(hist[0], colBase, colBool);
112
                }
113
                else if (!colBase.empty() && colBase[0] != this->ShapeAppearance[0]) {
114
                    colBase.resize(baseMap.Extent(), colBase[0]);
115
                    applyMaterial(hist[0], colBase, colBool);
116
                }
117

118
                if (static_cast<int>(colTool.size()) == toolMap.Extent()) {
119
                    applyMaterial(hist[1], colTool, colBool);
120
                }
121
                else if (!colTool.empty() && colTool[0] != this->ShapeAppearance[0]) {
122
                    colTool.resize(toolMap.Extent(), colTool[0]);
123
                    applyMaterial(hist[1], colTool, colBool);
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(), colBool);
130
                }
131

132
                this->ShapeAppearance.setValues(colBool);
133
            }
134
        }
135
    }
136
    else if (prop->isDerivedFrom<App::PropertyLink>()) {
137
        App::DocumentObject *pBase = static_cast<const App::PropertyLink*>(prop)->getValue();
138
        if (pBase)
139
            Gui::Application::Instance->hideViewProvider(pBase);
140
    }
141
}
142

143
bool ViewProviderBoolean::onDelete(const std::vector<std::string> &)
144
{
145
    // get the input shapes
146
    Part::Boolean* pBool = static_cast<Part::Boolean*>(getObject());
147
    App::DocumentObject *pBase = pBool->Base.getValue();
148
    App::DocumentObject *pTool = pBool->Tool.getValue();
149

150
    if (pBase)
151
        Gui::Application::Instance->showViewProvider(pBase);
152
    if (pTool)
153
        Gui::Application::Instance->showViewProvider(pTool);
154

155
    return true;
156
}
157

158
PROPERTY_SOURCE(PartGui::ViewProviderMultiFuse,PartGui::ViewProviderPart)
159

160
ViewProviderMultiFuse::ViewProviderMultiFuse() = default;
161

162
ViewProviderMultiFuse::~ViewProviderMultiFuse() = default;
163

164
std::vector<App::DocumentObject*> ViewProviderMultiFuse::claimChildren()const
165
{
166
    return static_cast<Part::MultiFuse*>(getObject())->Shapes.getValues();
167
}
168

169
QIcon ViewProviderMultiFuse::getIcon() const
170
{
171
    return Gui::BitmapFactory().iconFromTheme("Part_Fuse");
172
}
173

174
void ViewProviderMultiFuse::updateData(const App::Property* prop)
175
{
176
    PartGui::ViewProviderPart::updateData(prop);
177
    if (prop->is<Part::PropertyShapeHistory>()) {
178
        const std::vector<Part::ShapeHistory>& hist = static_cast<const Part::PropertyShapeHistory*>
179
            (prop)->getValues();
180
        Part::MultiFuse* objBool = static_cast<Part::MultiFuse*>(getObject());
181
        std::vector<App::DocumentObject*> sources = objBool->Shapes.getValues();
182
        if (hist.size() != sources.size())
183
            return;
184

185
        const TopoDS_Shape& boolShape = objBool->Shape.getValue();
186
        TopTools_IndexedMapOfShape boolMap;
187
        TopExp::MapShapes(boolShape, TopAbs_FACE, boolMap);
188

189
        std::vector<App::Material> colBool;
190
        colBool.resize(boolMap.Extent(), this->ShapeAppearance[0]);
191

192
        int index=0;
193
        for (std::vector<App::DocumentObject*>::iterator it = sources.begin(); it != sources.end(); ++it, ++index) {
194
            Part::Feature* objBase = dynamic_cast<Part::Feature*>(Part::Feature::getShapeOwner(*it));
195
            if (!objBase)
196
                continue;
197
            const TopoDS_Shape& baseShape = objBase->Shape.getValue();
198

199
            TopTools_IndexedMapOfShape baseMap;
200
            TopExp::MapShapes(baseShape, TopAbs_FACE, baseMap);
201

202
            auto vpBase = dynamic_cast<PartGui::ViewProviderPart*>(Gui::Application::Instance->getViewProvider(objBase));
203
            if (vpBase) {
204
                std::vector<App::Material> colBase = vpBase->ShapeAppearance.getValues();
205
                applyTransparency(vpBase->Transparency.getValue(),colBase);
206
                if (static_cast<int>(colBase.size()) == baseMap.Extent()) {
207
                    applyMaterial(hist[index], colBase, colBool);
208
                }
209
                else if (!colBase.empty() && colBase[0] != this->ShapeAppearance[0]) {
210
                    colBase.resize(baseMap.Extent(), colBase[0]);
211
                    applyMaterial(hist[index], colBase, colBool);
212
                }
213
            }
214
        }
215

216
        // If the view provider has set a transparency then override the values
217
        // of the input shapes
218
        if (Transparency.getValue() > 0) {
219
            applyTransparency(Transparency.getValue(), colBool);
220
        }
221

222
        this->ShapeAppearance.setValues(colBool);
223
    }
224
    else if (prop->isDerivedFrom<App::PropertyLinkList>()) {
225
        std::vector<App::DocumentObject*> pShapes = static_cast<const App::PropertyLinkList*>(prop)->getValues();
226
        for (auto it : pShapes) {
227
            if (it) {
228
                Gui::Application::Instance->hideViewProvider(it);
229
            }
230
        }
231
    }
232
}
233

234
bool ViewProviderMultiFuse::onDelete(const std::vector<std::string> &)
235
{
236
    // get the input shapes
237
    Part::MultiFuse* pBool = static_cast<Part::MultiFuse*>(getObject());
238
    std::vector<App::DocumentObject*> pShapes = pBool->Shapes.getValues();
239
    for (auto it : pShapes) {
240
        if (it) {
241
            Gui::Application::Instance->showViewProvider(it);
242
        }
243
    }
244

245
    return true;
246
}
247

248
bool ViewProviderMultiFuse::canDragObjects() const
249
{
250
    return true;
251
}
252

253
bool ViewProviderMultiFuse::canDragObject(App::DocumentObject* obj) const
254
{
255
    (void)obj;
256
    // return Part::Feature::hasShapeOwner(obj);
257
    return true;
258
}
259

260
void ViewProviderMultiFuse::dragObject(App::DocumentObject* obj)
261
{
262
    Part::MultiFuse* pBool = static_cast<Part::MultiFuse*>(getObject());
263
    std::vector<App::DocumentObject*> pShapes = pBool->Shapes.getValues();
264
    for (std::vector<App::DocumentObject*>::iterator it = pShapes.begin(); it != pShapes.end(); ++it) {
265
        if (*it == obj) {
266
            pShapes.erase(it);
267
            pBool->Shapes.setValues(pShapes);
268
            break;
269
        }
270
    }
271
}
272

273
bool ViewProviderMultiFuse::canDropObjects() const
274
{
275
    return true;
276
}
277

278
bool ViewProviderMultiFuse::canDropObject(App::DocumentObject* obj) const
279
{
280
    (void)obj;
281
    // return Part::Feature::hasShapeOwner(obj);
282
    return true;
283
}
284

285
void ViewProviderMultiFuse::dropObject(App::DocumentObject* obj)
286
{
287
    Part::MultiFuse* pBool = static_cast<Part::MultiFuse*>(getObject());
288
    std::vector<App::DocumentObject*> pShapes = pBool->Shapes.getValues();
289
    pShapes.push_back(obj);
290
    pBool->Shapes.setValues(pShapes);
291
}
292

293
PROPERTY_SOURCE(PartGui::ViewProviderMultiCommon,PartGui::ViewProviderPart)
294

295
ViewProviderMultiCommon::ViewProviderMultiCommon() = default;
296

297
ViewProviderMultiCommon::~ViewProviderMultiCommon() = default;
298

299
std::vector<App::DocumentObject*> ViewProviderMultiCommon::claimChildren()const
300
{
301
    return static_cast<Part::MultiCommon*>(getObject())->Shapes.getValues();
302
}
303

304
QIcon ViewProviderMultiCommon::getIcon() const
305
{
306
    return Gui::BitmapFactory().iconFromTheme("Part_Common");
307
}
308

309
void ViewProviderMultiCommon::updateData(const App::Property* prop)
310
{
311
    PartGui::ViewProviderPart::updateData(prop);
312
    if (prop->is<Part::PropertyShapeHistory>()) {
313
        const std::vector<Part::ShapeHistory>& hist = static_cast<const Part::PropertyShapeHistory*>
314
            (prop)->getValues();
315
        Part::MultiCommon* objBool = static_cast<Part::MultiCommon*>(getObject());
316
        std::vector<App::DocumentObject*> sources = objBool->Shapes.getValues();
317
        if (hist.size() != sources.size())
318
            return;
319

320
        const TopoDS_Shape& boolShape = objBool->Shape.getValue();
321
        TopTools_IndexedMapOfShape boolMap;
322
        TopExp::MapShapes(boolShape, TopAbs_FACE, boolMap);
323

324
        std::vector<App::Material> colBool;
325
        colBool.resize(boolMap.Extent(), this->ShapeAppearance[0]);
326

327
        int index=0;
328
        for (std::vector<App::DocumentObject*>::iterator it = sources.begin(); it != sources.end(); ++it, ++index) {
329
            Part::Feature* objBase = dynamic_cast<Part::Feature*>(Part::Feature::getShapeOwner(*it));
330
            if (!objBase)
331
                continue;
332
            const TopoDS_Shape& baseShape = objBase->Shape.getValue();
333

334
            TopTools_IndexedMapOfShape baseMap;
335
            TopExp::MapShapes(baseShape, TopAbs_FACE, baseMap);
336

337
            auto vpBase = dynamic_cast<PartGui::ViewProviderPart*>(Gui::Application::Instance->getViewProvider(objBase));
338
            if (vpBase) {
339
                std::vector<App::Material> colBase = vpBase->ShapeAppearance.getValues();
340
                applyTransparency(vpBase->Transparency.getValue(),colBase);
341
                if (static_cast<int>(colBase.size()) == baseMap.Extent()) {
342
                    applyMaterial(hist[index], colBase, colBool);
343
                }
344
                else if (!colBase.empty() && colBase[0] != this->ShapeAppearance[0]) {
345
                    colBase.resize(baseMap.Extent(), colBase[0]);
346
                    applyMaterial(hist[index], colBase, colBool);
347
                }
348
            }
349
        }
350

351
        // If the view provider has set a transparency then override the values
352
        // of the input shapes
353
        if (Transparency.getValue() > 0) {
354
            applyTransparency(Transparency.getValue(), colBool);
355
        }
356

357
        this->ShapeAppearance.setValues(colBool);
358
    }
359
    else if (prop->isDerivedFrom<App::PropertyLinkList>()) {
360
        std::vector<App::DocumentObject*> pShapes = static_cast<const App::PropertyLinkList*>(prop)->getValues();
361
        for (auto it : pShapes) {
362
            if (it) {
363
                Gui::Application::Instance->hideViewProvider(it);
364
            }
365
        }
366
    }
367
}
368

369
bool ViewProviderMultiCommon::onDelete(const std::vector<std::string> &)
370
{
371
    // get the input shapes
372
    Part::MultiCommon* pBool = static_cast<Part::MultiCommon*>(getObject());
373
    std::vector<App::DocumentObject*> pShapes = pBool->Shapes.getValues();
374
    for (auto it : pShapes) {
375
        if (it) {
376
            Gui::Application::Instance->showViewProvider(it);
377
        }
378
    }
379

380
    return true;
381
}
382

383
bool ViewProviderMultiCommon::canDragObjects() const
384
{
385
    return true;
386
}
387

388
bool ViewProviderMultiCommon::canDragObject(App::DocumentObject* obj) const
389
{
390
    (void)obj;
391
    // return Part::Feature::hasShapeOwner(obj);
392
    return true;
393
}
394

395
void ViewProviderMultiCommon::dragObject(App::DocumentObject* obj)
396
{
397
    Part::MultiCommon* pBool = static_cast<Part::MultiCommon*>(getObject());
398
    std::vector<App::DocumentObject*> pShapes = pBool->Shapes.getValues();
399
    for (std::vector<App::DocumentObject*>::iterator it = pShapes.begin(); it != pShapes.end(); ++it) {
400
        if (*it == obj) {
401
            pShapes.erase(it);
402
            pBool->Shapes.setValues(pShapes);
403
            break;
404
        }
405
    }
406
}
407

408
bool ViewProviderMultiCommon::canDropObjects() const
409
{
410
    return true;
411
}
412

413
bool ViewProviderMultiCommon::canDropObject(App::DocumentObject* obj) const
414
{
415
    (void)obj;
416
    // return Part::Feature::hasShapeOwner(obj);
417
    return true;
418
}
419

420
void ViewProviderMultiCommon::dropObject(App::DocumentObject* obj)
421
{
422
    Part::MultiCommon* pBool = static_cast<Part::MultiCommon*>(getObject());
423
    std::vector<App::DocumentObject*> pShapes = pBool->Shapes.getValues();
424
    pShapes.push_back(obj);
425
    pBool->Shapes.setValues(pShapes);
426
}
427

428

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

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

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

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