FreeCAD

Форк
0
/
ViewProviderGeometryObject.cpp 
305 строк · 12.0 Кб
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/SoDrawStyle.h>
32
# include <Inventor/nodes/SoFont.h>
33
# include <Inventor/nodes/SoMaterial.h>
34
# include <Inventor/nodes/SoSeparator.h>
35
# include <Inventor/nodes/SoSwitch.h>
36
# include <Inventor/nodes/SoDirectionalLight.h>
37
#endif
38

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

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

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

51

52
using namespace Gui;
53

54
PROPERTY_SOURCE(Gui::ViewProviderGeometryObject, Gui::ViewProviderDragger)
55

56
const App::PropertyIntegerConstraint::Constraints intPercent = {0, 100, 5};
57

58
ViewProviderGeometryObject::ViewProviderGeometryObject()
59
{
60
    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
61
    bool randomColor = hGrp->GetBool("RandomColor", false);
62
    float r, g, b;
63

64
    if (randomColor){
65
        auto fMax = (float)RAND_MAX;
66
        r = (float)rand() / fMax;
67
        g = (float)rand() / fMax;
68
        b = (float)rand() / fMax;
69
    }
70
    else {
71
        unsigned long shcol = hGrp->GetUnsigned("DefaultShapeColor", 3435980543UL);
72
        r = ((shcol >> 24) & 0xff) / 255.0;
73
        g = ((shcol >> 16) & 0xff) / 255.0;
74
        b = ((shcol >> 8) & 0xff) / 255.0;
75
    }
76

77
    int initialTransparency = hGrp->GetInt("DefaultShapeTransparency", 0);
78

79
    static const char *dogroup = "Display Options";
80
    static const char *sgroup = "Selection";
81
    static const char *osgroup = "Object Style";
82

83
    ADD_PROPERTY_TYPE(ShapeColor, (r, g, b), osgroup, App::Prop_None, "Set shape color");
84
    ADD_PROPERTY_TYPE(Transparency, (initialTransparency), osgroup, App::Prop_None, "Set object transparency");
85
    Transparency.setConstraints(&intPercent);
86
    App::Material mat(App::Material::DEFAULT);
87
    mat.transparency = (float)initialTransparency / 100.0f;
88
    ADD_PROPERTY_TYPE(ShapeMaterial,(mat), osgroup, App::Prop_None, "Shape material");
89
    ADD_PROPERTY_TYPE(BoundingBox, (false), dogroup, App::Prop_None, "Display object bounding box");
90
    ADD_PROPERTY_TYPE(Selectable, (true), sgroup, App::Prop_None, "Set if the object is selectable in the 3d view");
91

92
    bool enableSel = hGrp->GetBool("EnableSelection", true);
93
    Selectable.setValue(enableSel);
94

95
    pcShapeMaterial = new SoMaterial;
96
    pcShapeMaterial->diffuseColor.setValue(r, g, b);
97
    pcShapeMaterial->transparency = float(initialTransparency);
98
    pcShapeMaterial->ref();
99

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

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

106
    sPixmap = "Feature";
107
}
108

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

116
void ViewProviderGeometryObject::onChanged(const App::Property* prop)
117
{
118
    // Actually, the properties 'ShapeColor' and 'Transparency' are part of the property 'ShapeMaterial'.
119
    // Both redundant properties are kept due to more convenience for the user. But we must keep the values
120
    // consistent of all these properties.
121
    if (prop == &Selectable) {
122
        bool Sel = Selectable.getValue();
123
        setSelectable(Sel);
124
    }
125
    else if (prop == &ShapeColor) {
126
        const App::Color &c = ShapeColor.getValue();
127
        pcShapeMaterial->diffuseColor.setValue(c.r, c.g, c.b);
128
        if (c != ShapeMaterial.getValue().diffuseColor)
129
            ShapeMaterial.setDiffuseColor(c);
130
    }
131
    else if (prop == &Transparency) {
132
        const App::Material &Mat = ShapeMaterial.getValue();
133
        long value = (long)(100 * Mat.transparency);
134
        if (value != Transparency.getValue()) {
135
            float trans = Transparency.getValue() / 100.0f;
136
            pcShapeMaterial->transparency = trans;
137
            ShapeMaterial.setTransparency(trans);
138
        }
139
    }
140
    else if (prop == &ShapeMaterial) {
141
        if (getObject() && getObject()->testStatus(App::ObjectStatus::TouchOnColorChange))
142
            getObject()->touch(true);
143
        const App::Material &Mat = ShapeMaterial.getValue();
144
        long value = (long)(100 * Mat.transparency);
145
        if (value != Transparency.getValue())
146
            Transparency.setValue(value);
147
        const App::Color &color = Mat.diffuseColor;
148
        if (color != ShapeColor.getValue())
149
            ShapeColor.setValue(Mat.diffuseColor);
150
        pcShapeMaterial->ambientColor.setValue(Mat.ambientColor.r, Mat.ambientColor.g, Mat.ambientColor.b);
151
        pcShapeMaterial->diffuseColor.setValue(Mat.diffuseColor.r, Mat.diffuseColor.g, Mat.diffuseColor.b);
152
        pcShapeMaterial->specularColor.setValue(Mat.specularColor.r, Mat.specularColor.g, Mat.specularColor.b);
153
        pcShapeMaterial->emissiveColor.setValue(Mat.emissiveColor.r, Mat.emissiveColor.g, Mat.emissiveColor.b);
154
        pcShapeMaterial->shininess.setValue(Mat.shininess);
155
        pcShapeMaterial->transparency.setValue(Mat.transparency);
156
    }
157
    else if (prop == &BoundingBox) {
158
        showBoundingBox(BoundingBox.getValue());
159
    }
160

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

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

169
void ViewProviderGeometryObject::updateData(const App::Property* prop)
170
{
171
    if (prop->isDerivedFrom(App::PropertyComplexGeoData::getClassTypeId())) {
172
        Base::BoundBox3d box = 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

188
    ViewProviderDragger::updateData(prop);
189
}
190

191
SoPickedPointList ViewProviderGeometryObject::getPickedPoints(const SbVec2s& pos, const View3DInventorViewer& viewer,bool pickAll) const
192
{
193
    auto root = new SoSeparator;
194
    root->ref();
195
    root->addChild(viewer.getHeadlight());
196
    root->addChild(viewer.getSoRenderManager()->getCamera());
197
    root->addChild(getRoot());
198

199
    SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion());
200
    rp.setPickAll(pickAll);
201
    rp.setRadius(viewer.getPickRadius());
202
    rp.setPoint(pos);
203
    rp.apply(root);
204
    root->unref();
205

206
    // returns a copy of the list
207
    return rp.getPickedPointList();
208
}
209

210
SoPickedPoint* ViewProviderGeometryObject::getPickedPoint(const SbVec2s& pos, const View3DInventorViewer& viewer) const
211
{
212
    auto root = new SoSeparator;
213
    root->ref();
214
    root->addChild(viewer.getHeadlight());
215
    root->addChild(viewer.getSoRenderManager()->getCamera());
216
    root->addChild(getRoot());
217

218
    SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion());
219
    rp.setPoint(pos);
220
    rp.setRadius(viewer.getPickRadius());
221
    rp.apply(root);
222
    root->unref();
223

224
    // returns a copy of the point
225
    SoPickedPoint* pick = rp.getPickedPoint();
226
    //return (pick ? pick->copy() : 0); // needs the same instance of CRT under MS Windows
227
    return (pick ? new SoPickedPoint(*pick) : nullptr);
228
}
229

230
unsigned long ViewProviderGeometryObject::getBoundColor() const
231
{
232
    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
233
    unsigned long bbcol = hGrp->GetUnsigned("BoundingBoxColor",4294967295UL); // white (255,255,255)
234
    return bbcol;
235
}
236

237
namespace {
238
float getBoundBoxFontSize()
239
{
240
    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
241
    return hGrp->GetFloat("BoundingBoxFontSize", 10.0);
242
}
243
}
244

245
void ViewProviderGeometryObject::showBoundingBox(bool show)
246
{
247
    if (!pcBoundSwitch && show) {
248
        unsigned long bbcol = getBoundColor();
249
        float r,g,b;
250
        r = ((bbcol >> 24) & 0xff) / 255.0; g = ((bbcol >> 16) & 0xff) / 255.0; b = ((bbcol >> 8) & 0xff) / 255.0;
251

252
        pcBoundSwitch = new SoSwitch();
253
        auto pBoundingSep = new SoSeparator();
254
        auto lineStyle = new SoDrawStyle;
255
        lineStyle->lineWidth = 2.0f;
256
        pBoundingSep->addChild(lineStyle);
257

258
        pcBoundColor->rgb.setValue(r, g, b);
259
        pBoundingSep->addChild(pcBoundColor);
260
        auto font = new SoFont();
261
        font->size.setValue(getBoundBoxFontSize());
262
        pBoundingSep->addChild(font);
263

264
        pBoundingSep->addChild(new SoResetTransform());
265
        pBoundingSep->addChild(pcBoundingBox);
266
        pcBoundingBox->coordsOn.setValue(false);
267
        pcBoundingBox->dimensionsOn.setValue(true);
268

269
        // add to the highlight node
270
        pcBoundSwitch->addChild(pBoundingSep);
271
        pcRoot->addChild(pcBoundSwitch);
272
    }
273

274
    if (pcBoundSwitch) {
275
        pcBoundSwitch->whichChild = (show ? 0 : -1);
276
    }
277
}
278

279
void ViewProviderGeometryObject::setSelectable(bool selectable)
280
{
281
    SoSearchAction sa;
282
    sa.setInterest(SoSearchAction::ALL);
283
    sa.setSearchingAll(true);
284
    sa.setType(Gui::SoFCSelection::getClassTypeId());
285
    sa.apply(pcRoot);
286

287
    SoPathList & pathList = sa.getPaths();
288

289
    for (int i=0;i<pathList.getLength();i++) {
290
        auto selNode = dynamic_cast<SoFCSelection*>(pathList[i]->getTail());
291
        if (selectable) {
292
            if (selNode) {
293
                selNode->selectionMode = SoFCSelection::SEL_ON;
294
                selNode->highlightMode = SoFCSelection::AUTO;
295
            }
296
        }
297
        else {
298
            if (selNode) {
299
                selNode->selectionMode = SoFCSelection::SEL_OFF;
300
                selNode->highlightMode = SoFCSelection::OFF;
301
                selNode->selected = SoFCSelection::NOTSELECTED;
302
            }
303
        }
304
    }
305
}
306

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

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

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

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