23
#include "PreCompiled.h"
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>
39
#include <Inventor/nodes/SoResetTransform.h>
41
#include <App/GeoFeature.h>
42
#include <App/PropertyGeo.h>
44
#include "ViewProviderGeometryObject.h"
45
#include "Application.h"
47
#include "SoFCBoundingBox.h"
48
#include "SoFCSelection.h"
49
#include "View3DInventorViewer.h"
54
PROPERTY_SOURCE(Gui::ViewProviderGeometryObject, Gui::ViewProviderDragger)
56
const App::PropertyIntegerConstraint::Constraints intPercent = {0, 100, 5};
58
ViewProviderGeometryObject::ViewProviderGeometryObject()
60
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
61
bool randomColor = hGrp->GetBool("RandomColor", false);
65
auto fMax = (float)RAND_MAX;
66
r = (float)rand() / fMax;
67
g = (float)rand() / fMax;
68
b = (float)rand() / fMax;
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;
77
int initialTransparency = hGrp->GetInt("DefaultShapeTransparency", 0);
79
static const char *dogroup = "Display Options";
80
static const char *sgroup = "Selection";
81
static const char *osgroup = "Object Style";
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");
92
bool enableSel = hGrp->GetBool("EnableSelection", true);
93
Selectable.setValue(enableSel);
95
pcShapeMaterial = new SoMaterial;
96
pcShapeMaterial->diffuseColor.setValue(r, g, b);
97
pcShapeMaterial->transparency = float(initialTransparency);
98
pcShapeMaterial->ref();
100
pcBoundingBox = new Gui::SoFCBoundingBox;
101
pcBoundingBox->ref();
103
pcBoundColor = new SoBaseColor();
109
ViewProviderGeometryObject::~ViewProviderGeometryObject()
111
pcShapeMaterial->unref();
112
pcBoundingBox->unref();
113
pcBoundColor->unref();
116
void ViewProviderGeometryObject::onChanged(const App::Property* prop)
121
if (prop == &Selectable) {
122
bool Sel = Selectable.getValue();
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);
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);
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);
157
else if (prop == &BoundingBox) {
158
showBoundingBox(BoundingBox.getValue());
161
ViewProviderDragger::onChanged(prop);
164
void ViewProviderGeometryObject::attach(App::DocumentObject *pcObj)
166
ViewProviderDragger::attach(pcObj);
169
void ViewProviderGeometryObject::updateData(const App::Property* prop)
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);
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();
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);
188
ViewProviderDragger::updateData(prop);
191
SoPickedPointList ViewProviderGeometryObject::getPickedPoints(const SbVec2s& pos, const View3DInventorViewer& viewer,bool pickAll) const
193
auto root = new SoSeparator;
195
root->addChild(viewer.getHeadlight());
196
root->addChild(viewer.getSoRenderManager()->getCamera());
197
root->addChild(getRoot());
199
SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion());
200
rp.setPickAll(pickAll);
201
rp.setRadius(viewer.getPickRadius());
207
return rp.getPickedPointList();
210
SoPickedPoint* ViewProviderGeometryObject::getPickedPoint(const SbVec2s& pos, const View3DInventorViewer& viewer) const
212
auto root = new SoSeparator;
214
root->addChild(viewer.getHeadlight());
215
root->addChild(viewer.getSoRenderManager()->getCamera());
216
root->addChild(getRoot());
218
SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion());
220
rp.setRadius(viewer.getPickRadius());
225
SoPickedPoint* pick = rp.getPickedPoint();
227
return (pick ? new SoPickedPoint(*pick) : nullptr);
230
unsigned long ViewProviderGeometryObject::getBoundColor() const
232
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
233
unsigned long bbcol = hGrp->GetUnsigned("BoundingBoxColor",4294967295UL);
238
float getBoundBoxFontSize()
240
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
241
return hGrp->GetFloat("BoundingBoxFontSize", 10.0);
245
void ViewProviderGeometryObject::showBoundingBox(bool show)
247
if (!pcBoundSwitch && show) {
248
unsigned long bbcol = getBoundColor();
250
r = ((bbcol >> 24) & 0xff) / 255.0; g = ((bbcol >> 16) & 0xff) / 255.0; b = ((bbcol >> 8) & 0xff) / 255.0;
252
pcBoundSwitch = new SoSwitch();
253
auto pBoundingSep = new SoSeparator();
254
auto lineStyle = new SoDrawStyle;
255
lineStyle->lineWidth = 2.0f;
256
pBoundingSep->addChild(lineStyle);
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);
264
pBoundingSep->addChild(new SoResetTransform());
265
pBoundingSep->addChild(pcBoundingBox);
266
pcBoundingBox->coordsOn.setValue(false);
267
pcBoundingBox->dimensionsOn.setValue(true);
270
pcBoundSwitch->addChild(pBoundingSep);
271
pcRoot->addChild(pcBoundSwitch);
275
pcBoundSwitch->whichChild = (show ? 0 : -1);
279
void ViewProviderGeometryObject::setSelectable(bool selectable)
282
sa.setInterest(SoSearchAction::ALL);
283
sa.setSearchingAll(true);
284
sa.setType(Gui::SoFCSelection::getClassTypeId());
287
SoPathList & pathList = sa.getPaths();
289
for (int i=0;i<pathList.getLength();i++) {
290
auto selNode = dynamic_cast<SoFCSelection*>(pathList[i]->getTail());
293
selNode->selectionMode = SoFCSelection::SEL_ON;
294
selNode->highlightMode = SoFCSelection::AUTO;
299
selNode->selectionMode = SoFCSelection::SEL_OFF;
300
selNode->highlightMode = SoFCSelection::OFF;
301
selNode->selected = SoFCSelection::NOTSELECTED;