FreeCAD

Форк
0
/
ViewProviderGeometryObject.cpp 
372 строки · 13.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2006 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 <Inventor/SoPickedPoint.h>
27
#include <Inventor/actions/SoRayPickAction.h>
28
#include <Inventor/actions/SoSearchAction.h>
29
#include <Inventor/nodes/SoBaseColor.h>
30
#include <Inventor/nodes/SoCamera.h>
31
#include <Inventor/nodes/SoDirectionalLight.h>
32
#include <Inventor/nodes/SoDrawStyle.h>
33
#include <Inventor/nodes/SoFont.h>
34
#include <Inventor/nodes/SoMaterial.h>
35
#include <Inventor/nodes/SoSeparator.h>
36
#include <Inventor/nodes/SoSwitch.h>
37
#endif
38

39
#include <Inventor/nodes/SoResetTransform.h>
40

41
#include <App/GeoFeature.h>
42
#include <App/PropertyGeo.h>
43

44
#include "Application.h"
45
#include "Document.h"
46
#include "SoFCBoundingBox.h"
47
#include "SoFCSelection.h"
48
#include "View3DInventorViewer.h"
49
#include "ViewProviderGeometryObject.h"
50
#include "ViewProviderGeometryObjectPy.h"
51

52
using namespace Gui;
53

54
// Helper functions to consistently convert between float and long
55
namespace {
56
float fromPercent(long value)
57
{
58
    return std::roundf(value) / 100.0F;
59
}
60

61
long toPercent(float value)
62
{
63
    return std::lround(100.0 * value);
64
}
65
}
66

67
PROPERTY_SOURCE(Gui::ViewProviderGeometryObject, Gui::ViewProviderDragger)
68

69
const App::PropertyIntegerConstraint::Constraints intPercent = {0, 100, 5};
70

71
ViewProviderGeometryObject::ViewProviderGeometryObject()
72
{
73
    App::Material mat = App::Material::getDefaultAppearance();
74
    long initialTransparency = toPercent(mat.transparency);
75

76
    static const char* dogroup = "Display Options";
77
    static const char* sgroup = "Selection";
78
    static const char* osgroup = "Object Style";
79

80
    ADD_PROPERTY_TYPE(Transparency,
81
                      (initialTransparency),
82
                      osgroup,
83
                      App::Prop_None,
84
                      "Set object transparency");
85
    Transparency.setConstraints(&intPercent);
86

87
    ADD_PROPERTY_TYPE(ShapeAppearance, (mat), osgroup, App::Prop_None, "Shape appearrance");
88
    ADD_PROPERTY_TYPE(BoundingBox, (false), dogroup, App::Prop_None, "Display object bounding box");
89
    ADD_PROPERTY_TYPE(Selectable,
90
                      (true),
91
                      sgroup,
92
                      App::Prop_None,
93
                      "Set if the object is selectable in the 3d view");
94

95
    Selectable.setValue(isSelectionEnabled());
96

97
    pcShapeMaterial = new SoMaterial;
98
    setCoinAppearance(mat);
99
    pcShapeMaterial->ref();
100

101
    pcBoundingBox = new Gui::SoFCBoundingBox;
102
    pcBoundingBox->ref();
103

104
    pcBoundColor = new SoBaseColor();
105
    pcBoundColor->ref();
106

107
    sPixmap = "Feature";
108
}
109

110
ViewProviderGeometryObject::~ViewProviderGeometryObject()
111
{
112
    pcShapeMaterial->unref();
113
    pcBoundingBox->unref();
114
    pcBoundColor->unref();
115
}
116

117
bool ViewProviderGeometryObject::isSelectionEnabled() const
118
{
119
    ParameterGrp::handle hGrp =
120
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
121
    return hGrp->GetBool("EnableSelection", true);
122
}
123

124
void ViewProviderGeometryObject::onChanged(const App::Property* prop)
125
{
126
    // Actually, the properties 'ShapeColor' and 'Transparency' are part of the property
127
    // 'ShapeMaterial'. Both redundant properties are kept due to more convenience for the user. But
128
    // we must keep the values consistent of all these properties.
129
    std::string propName = prop->getName();
130
    if (prop == &Selectable) {
131
        bool Sel = Selectable.getValue();
132
        setSelectable(Sel);
133
    }
134
    else if (prop == &Transparency) {
135
        long value = toPercent(ShapeAppearance.getTransparency());
136
        float trans = fromPercent(Transparency.getValue());
137
        if (value != Transparency.getValue()) {
138
            ShapeAppearance.setTransparency(trans);
139
        }
140

141
        pcShapeMaterial->transparency = trans;
142
    }
143
    else if (prop == &ShapeAppearance) {
144
        if (getObject() && getObject()->testStatus(App::ObjectStatus::TouchOnColorChange)) {
145
            getObject()->touch(true);
146
        }
147
        long value = toPercent(ShapeAppearance.getTransparency());
148
        if (value != Transparency.getValue()) {
149
            Transparency.setValue(value);
150
        }
151
        if (ShapeAppearance.getSize() == 1) {
152
            const App::Material& Mat = ShapeAppearance[0];
153
            setCoinAppearance(Mat);
154
        }
155
    }
156
    else if (prop == &BoundingBox) {
157
        showBoundingBox(BoundingBox.getValue());
158
    }
159

160
    ViewProviderDragger::onChanged(prop);
161
}
162

163
void ViewProviderGeometryObject::attach(App::DocumentObject* pcObj)
164
{
165
    ViewProviderDragger::attach(pcObj);
166
}
167

168
void ViewProviderGeometryObject::updateData(const App::Property* prop)
169
{
170
    if (prop->isDerivedFrom(App::PropertyComplexGeoData::getClassTypeId())) {
171
        Base::BoundBox3d box =
172
            static_cast<const App::PropertyComplexGeoData*>(prop)->getBoundingBox();
173
        pcBoundingBox->minBounds.setValue(box.MinX, box.MinY, box.MinZ);
174
        pcBoundingBox->maxBounds.setValue(box.MaxX, box.MaxY, box.MaxZ);
175
    }
176
    else if (prop->isDerivedFrom(App::PropertyPlacement::getClassTypeId())) {
177
        auto geometry = dynamic_cast<App::GeoFeature*>(getObject());
178
        if (geometry && prop == &geometry->Placement) {
179
            const App::PropertyComplexGeoData* data = geometry->getPropertyOfGeometry();
180
            if (data) {
181
                Base::BoundBox3d box = data->getBoundingBox();
182
                pcBoundingBox->minBounds.setValue(box.MinX, box.MinY, box.MinZ);
183
                pcBoundingBox->maxBounds.setValue(box.MaxX, box.MaxY, box.MaxZ);
184
            }
185
        }
186
    }
187
    else if (std::string(prop->getName()) == "ShapeMaterial") {
188
        // Set the appearance from the material
189
        auto geometry = dynamic_cast<App::GeoFeature*>(getObject());
190
        if (geometry) {
191
            auto material = geometry->getMaterialAppearance();
192
            ShapeAppearance.setValue(material);
193
        }
194
    }
195

196
    ViewProviderDragger::updateData(prop);
197
}
198

199
SoPickedPointList ViewProviderGeometryObject::getPickedPoints(const SbVec2s& pos,
200
                                                              const View3DInventorViewer& viewer,
201
                                                              bool pickAll) const
202
{
203
    auto root = new SoSeparator;
204
    root->ref();
205
    root->addChild(viewer.getHeadlight());
206
    root->addChild(viewer.getSoRenderManager()->getCamera());
207
    root->addChild(getRoot());
208

209
    SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion());
210
    rp.setPickAll(pickAll);
211
    rp.setRadius(viewer.getPickRadius());
212
    rp.setPoint(pos);
213
    rp.apply(root);
214
    root->unref();
215

216
    // returns a copy of the list
217
    return rp.getPickedPointList();
218
}
219

220
SoPickedPoint* ViewProviderGeometryObject::getPickedPoint(const SbVec2s& pos,
221
                                                          const View3DInventorViewer& viewer) const
222
{
223
    auto root = new SoSeparator;
224
    root->ref();
225
    root->addChild(viewer.getHeadlight());
226
    root->addChild(viewer.getSoRenderManager()->getCamera());
227
    root->addChild(getRoot());
228

229
    SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion());
230
    rp.setPoint(pos);
231
    rp.setRadius(viewer.getPickRadius());
232
    rp.apply(root);
233
    root->unref();
234

235
    // returns a copy of the point
236
    SoPickedPoint* pick = rp.getPickedPoint();
237
    // return (pick ? pick->copy() : 0); // needs the same instance of CRT under MS Windows
238
    return (pick ? new SoPickedPoint(*pick) : nullptr);
239
}
240

241
unsigned long ViewProviderGeometryObject::getBoundColor() const
242
{
243
    ParameterGrp::handle hGrp =
244
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
245
    // white (255,255,255)
246
    unsigned long bbcol = hGrp->GetUnsigned("BoundingBoxColor", 4294967295UL);
247
    return bbcol;
248
}
249

250
void ViewProviderGeometryObject::setCoinAppearance(const App::Material& source)
251
{
252
    pcShapeMaterial->ambientColor.setValue(source.ambientColor.r,
253
                                           source.ambientColor.g,
254
                                           source.ambientColor.b);
255
    pcShapeMaterial->diffuseColor.setValue(source.diffuseColor.r,
256
                                           source.diffuseColor.g,
257
                                           source.diffuseColor.b);
258
    pcShapeMaterial->specularColor.setValue(source.specularColor.r,
259
                                            source.specularColor.g,
260
                                            source.specularColor.b);
261
    pcShapeMaterial->emissiveColor.setValue(source.emissiveColor.r,
262
                                            source.emissiveColor.g,
263
                                            source.emissiveColor.b);
264
    pcShapeMaterial->shininess.setValue(source.shininess);
265
    pcShapeMaterial->transparency.setValue(source.transparency);
266
}
267

268
namespace
269
{
270
float getBoundBoxFontSize()
271
{
272
    ParameterGrp::handle hGrp =
273
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
274
    return hGrp->GetFloat("BoundingBoxFontSize", 10.0);
275
}
276
}  // namespace
277

278
void ViewProviderGeometryObject::showBoundingBox(bool show)
279
{
280
    if (!pcBoundSwitch && show) {
281
        unsigned long bbcol = getBoundColor();
282
        float red {};
283
        float green {};
284
        float blue {};
285
        red = ((bbcol >> 24) & 0xff) / 255.0F;
286
        green = ((bbcol >> 16) & 0xff) / 255.0F;
287
        blue = ((bbcol >> 8) & 0xff) / 255.0F;
288

289
        pcBoundSwitch = new SoSwitch();
290
        auto pBoundingSep = new SoSeparator();
291
        auto lineStyle = new SoDrawStyle;
292
        lineStyle->lineWidth = 2.0F;
293
        pBoundingSep->addChild(lineStyle);
294

295
        pcBoundColor->rgb.setValue(red, green, blue);
296
        pBoundingSep->addChild(pcBoundColor);
297
        auto font = new SoFont();
298
        font->size.setValue(getBoundBoxFontSize());
299
        pBoundingSep->addChild(font);
300

301
        pBoundingSep->addChild(new SoResetTransform());
302
        pBoundingSep->addChild(pcBoundingBox);
303
        pcBoundingBox->coordsOn.setValue(false);
304
        pcBoundingBox->dimensionsOn.setValue(true);
305

306
        // add to the highlight node
307
        pcBoundSwitch->addChild(pBoundingSep);
308
        pcRoot->addChild(pcBoundSwitch);
309
    }
310

311
    if (pcBoundSwitch) {
312
        pcBoundSwitch->whichChild = (show ? 0 : -1);
313
    }
314
}
315

316
void ViewProviderGeometryObject::setSelectable(bool selectable)
317
{
318
    SoSearchAction sa;
319
    sa.setInterest(SoSearchAction::ALL);
320
    sa.setSearchingAll(true);
321
    sa.setType(Gui::SoFCSelection::getClassTypeId());
322
    sa.apply(pcRoot);
323

324
    SoPathList& pathList = sa.getPaths();
325

326
    for (int i = 0; i < pathList.getLength(); i++) {
327
        auto selNode = dynamic_cast<SoFCSelection*>(pathList[i]->getTail());
328
        if (selectable) {
329
            if (selNode) {
330
                selNode->selectionMode = SoFCSelection::SEL_ON;
331
                selNode->highlightMode = SoFCSelection::AUTO;
332
            }
333
        }
334
        else {
335
            if (selNode) {
336
                selNode->selectionMode = SoFCSelection::SEL_OFF;
337
                selNode->highlightMode = SoFCSelection::OFF;
338
                selNode->selected = SoFCSelection::NOTSELECTED;
339
            }
340
        }
341
    }
342
}
343

344
PyObject* ViewProviderGeometryObject::getPyObject()
345
{
346
    if (!pyViewObject) {
347
        pyViewObject = new ViewProviderGeometryObjectPy(this);
348
    }
349
    pyViewObject->IncRef();
350
    return pyViewObject;
351
}
352

353
void ViewProviderGeometryObject::handleChangedPropertyName(Base::XMLReader& reader,
354
                                                           const char* TypeName,
355
                                                           const char* PropName)
356
{
357
    if (strcmp(PropName, "ShapeColor") == 0
358
        && strcmp(TypeName, App::PropertyColor::getClassTypeId().getName()) == 0) {
359
        App::PropertyColor prop;
360
        prop.Restore(reader);
361
        ShapeAppearance.setDiffuseColor(prop.getValue());
362
    }
363
    else if (strcmp(PropName, "ShapeMaterial") == 0
364
             && strcmp(TypeName, App::PropertyMaterial::getClassTypeId().getName()) == 0) {
365
        App::PropertyMaterial prop;
366
        prop.Restore(reader);
367
        ShapeAppearance.setValue(prop.getValue());
368
    }
369
    else {
370
        ViewProviderDragger::handleChangedPropertyName(reader, TypeName, PropName);
371
    }
372
}
373

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

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

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

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