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/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>
39
#include <Inventor/nodes/SoResetTransform.h>
41
#include <App/GeoFeature.h>
42
#include <App/PropertyGeo.h>
44
#include "Application.h"
46
#include "SoFCBoundingBox.h"
47
#include "SoFCSelection.h"
48
#include "View3DInventorViewer.h"
49
#include "ViewProviderGeometryObject.h"
50
#include "ViewProviderGeometryObjectPy.h"
56
float fromPercent(long value)
58
return std::roundf(value) / 100.0F;
61
long toPercent(float value)
63
return std::lround(100.0 * value);
67
PROPERTY_SOURCE(Gui::ViewProviderGeometryObject, Gui::ViewProviderDragger)
69
const App::PropertyIntegerConstraint::Constraints intPercent = {0, 100, 5};
71
ViewProviderGeometryObject::ViewProviderGeometryObject()
73
App::Material mat = App::Material::getDefaultAppearance();
74
long initialTransparency = toPercent(mat.transparency);
76
static const char* dogroup = "Display Options";
77
static const char* sgroup = "Selection";
78
static const char* osgroup = "Object Style";
80
ADD_PROPERTY_TYPE(Transparency,
81
(initialTransparency),
84
"Set object transparency");
85
Transparency.setConstraints(&intPercent);
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,
93
"Set if the object is selectable in the 3d view");
95
Selectable.setValue(isSelectionEnabled());
97
pcShapeMaterial = new SoMaterial;
98
setCoinAppearance(mat);
99
pcShapeMaterial->ref();
101
pcBoundingBox = new Gui::SoFCBoundingBox;
102
pcBoundingBox->ref();
104
pcBoundColor = new SoBaseColor();
110
ViewProviderGeometryObject::~ViewProviderGeometryObject()
112
pcShapeMaterial->unref();
113
pcBoundingBox->unref();
114
pcBoundColor->unref();
117
bool ViewProviderGeometryObject::isSelectionEnabled() const
119
ParameterGrp::handle hGrp =
120
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
121
return hGrp->GetBool("EnableSelection", true);
124
void ViewProviderGeometryObject::onChanged(const App::Property* prop)
129
std::string propName = prop->getName();
130
if (prop == &Selectable) {
131
bool Sel = Selectable.getValue();
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);
141
pcShapeMaterial->transparency = trans;
143
else if (prop == &ShapeAppearance) {
144
if (getObject() && getObject()->testStatus(App::ObjectStatus::TouchOnColorChange)) {
145
getObject()->touch(true);
147
long value = toPercent(ShapeAppearance.getTransparency());
148
if (value != Transparency.getValue()) {
149
Transparency.setValue(value);
151
if (ShapeAppearance.getSize() == 1) {
152
const App::Material& Mat = ShapeAppearance[0];
153
setCoinAppearance(Mat);
156
else if (prop == &BoundingBox) {
157
showBoundingBox(BoundingBox.getValue());
160
ViewProviderDragger::onChanged(prop);
163
void ViewProviderGeometryObject::attach(App::DocumentObject* pcObj)
165
ViewProviderDragger::attach(pcObj);
168
void ViewProviderGeometryObject::updateData(const App::Property* prop)
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);
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);
187
else if (std::string(prop->getName()) == "ShapeMaterial") {
189
auto geometry = dynamic_cast<App::GeoFeature*>(getObject());
191
auto material = geometry->getMaterialAppearance();
192
ShapeAppearance.setValue(material);
196
ViewProviderDragger::updateData(prop);
199
SoPickedPointList ViewProviderGeometryObject::getPickedPoints(const SbVec2s& pos,
200
const View3DInventorViewer& viewer,
203
auto root = new SoSeparator;
205
root->addChild(viewer.getHeadlight());
206
root->addChild(viewer.getSoRenderManager()->getCamera());
207
root->addChild(getRoot());
209
SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion());
210
rp.setPickAll(pickAll);
211
rp.setRadius(viewer.getPickRadius());
217
return rp.getPickedPointList();
220
SoPickedPoint* ViewProviderGeometryObject::getPickedPoint(const SbVec2s& pos,
221
const View3DInventorViewer& viewer) const
223
auto root = new SoSeparator;
225
root->addChild(viewer.getHeadlight());
226
root->addChild(viewer.getSoRenderManager()->getCamera());
227
root->addChild(getRoot());
229
SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion());
231
rp.setRadius(viewer.getPickRadius());
236
SoPickedPoint* pick = rp.getPickedPoint();
238
return (pick ? new SoPickedPoint(*pick) : nullptr);
241
unsigned long ViewProviderGeometryObject::getBoundColor() const
243
ParameterGrp::handle hGrp =
244
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
246
unsigned long bbcol = hGrp->GetUnsigned("BoundingBoxColor", 4294967295UL);
250
void ViewProviderGeometryObject::setCoinAppearance(const App::Material& source)
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);
270
float getBoundBoxFontSize()
272
ParameterGrp::handle hGrp =
273
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
274
return hGrp->GetFloat("BoundingBoxFontSize", 10.0);
278
void ViewProviderGeometryObject::showBoundingBox(bool show)
280
if (!pcBoundSwitch && show) {
281
unsigned long bbcol = getBoundColor();
285
red = ((bbcol >> 24) & 0xff) / 255.0F;
286
green = ((bbcol >> 16) & 0xff) / 255.0F;
287
blue = ((bbcol >> 8) & 0xff) / 255.0F;
289
pcBoundSwitch = new SoSwitch();
290
auto pBoundingSep = new SoSeparator();
291
auto lineStyle = new SoDrawStyle;
292
lineStyle->lineWidth = 2.0F;
293
pBoundingSep->addChild(lineStyle);
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);
301
pBoundingSep->addChild(new SoResetTransform());
302
pBoundingSep->addChild(pcBoundingBox);
303
pcBoundingBox->coordsOn.setValue(false);
304
pcBoundingBox->dimensionsOn.setValue(true);
307
pcBoundSwitch->addChild(pBoundingSep);
308
pcRoot->addChild(pcBoundSwitch);
312
pcBoundSwitch->whichChild = (show ? 0 : -1);
316
void ViewProviderGeometryObject::setSelectable(bool selectable)
319
sa.setInterest(SoSearchAction::ALL);
320
sa.setSearchingAll(true);
321
sa.setType(Gui::SoFCSelection::getClassTypeId());
324
SoPathList& pathList = sa.getPaths();
326
for (int i = 0; i < pathList.getLength(); i++) {
327
auto selNode = dynamic_cast<SoFCSelection*>(pathList[i]->getTail());
330
selNode->selectionMode = SoFCSelection::SEL_ON;
331
selNode->highlightMode = SoFCSelection::AUTO;
336
selNode->selectionMode = SoFCSelection::SEL_OFF;
337
selNode->highlightMode = SoFCSelection::OFF;
338
selNode->selected = SoFCSelection::NOTSELECTED;
344
PyObject* ViewProviderGeometryObject::getPyObject()
347
pyViewObject = new ViewProviderGeometryObjectPy(this);
349
pyViewObject->IncRef();
353
void ViewProviderGeometryObject::handleChangedPropertyName(Base::XMLReader& reader,
354
const char* TypeName,
355
const char* PropName)
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());
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());
370
ViewProviderDragger::handleChangedPropertyName(reader, TypeName, PropName);