| 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 |
|
| 24 |
#include <Inventor/SoPickedPoint.h> |
| 25 |
#include <Inventor/actions/SoRayPickAction.h> |
| 26 |
#include <Inventor/actions/SoSearchAction.h> |
| 27 |
#include <Inventor/nodes/SoBaseColor.h> |
| 28 |
#include <Inventor/nodes/SoCamera.h> |
| 29 |
#include <Inventor/nodes/SoDirectionalLight.h> |
| 30 |
#include <Inventor/nodes/SoDrawStyle.h> |
| 31 |
#include <Inventor/nodes/SoFont.h> |
| 32 |
#include <Inventor/nodes/SoMaterial.h> |
| 33 |
#include <Inventor/nodes/SoPickStyle.h> |
| 34 |
#include <Inventor/nodes/SoResetTransform.h> |
| 35 |
#include <Inventor/nodes/SoSeparator.h> |
| 36 |
#include <Inventor/nodes/SoSwitch.h> |
| 37 |
|
| 38 |
|
| 39 |
#include <App/GeoFeature.h> |
| 40 |
#include <App/PropertyGeo.h> |
| 41 |
|
| 42 |
#include "Application.h" |
| 43 |
#include "Document.h" |
| 44 |
#include "Inventor/SoFCBoundingBox.h" |
| 45 |
#include "SoFCSelection.h" |
| 46 |
#include "View3DInventorViewer.h" |
| 47 |
#include "ViewProviderGeometryObject.h" |
| 48 |
#include "ViewProviderGeometryObjectPy.h" |
| 49 |
|
| 50 |
#include <Base/Tools.h> |
| 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 |
App::Material mat = App::Material::getDefaultAppearance(); |
| 61 |
|
| 62 |
long initialTransparency = Base::toPercent(mat.transparency); |
| 63 |
|
| 64 |
static const char* dogroup = "Display Options"; |
| 65 |
static const char* sgroup = "Selection"; |
| 66 |
static const char* osgroup = "Object Style"; |
| 67 |
|
| 68 |
ADD_PROPERTY_TYPE( |
| 69 |
Transparency, |
| 70 |
(initialTransparency), |
| 71 |
osgroup, |
| 72 |
App::Prop_None, |
| 73 |
"Set object transparency" |
| 74 |
); |
| 75 |
Transparency.setConstraints(&intPercent); |
| 76 |
|
| 77 |
ADD_PROPERTY_TYPE(ShapeAppearance, (mat), osgroup, App::Prop_None, "Shape appearance"); |
| 78 |
ADD_PROPERTY_TYPE(BoundingBox, (false), dogroup, App::Prop_None, "Display object bounding box"); |
| 79 |
ADD_PROPERTY_TYPE( |
| 80 |
Selectable, |
| 81 |
(true), |
| 82 |
sgroup, |
| 83 |
App::Prop_None, |
| 84 |
"Set if the object is selectable in the 3d view" |
| 85 |
); |
| 86 |
|
| 87 |
pickStyle = new SoPickStyle(); |
| 88 |
pickStyle->ref(); |
| 89 |
pickStyle->style.setValue(SoPickStyle::SHAPE); |
| 90 |
pcRoot->insertChild(pickStyle, 1); |
| 91 |
Selectable.setValue(isSelectionEnabled()); |
| 92 |
|
| 93 |
pcShapeMaterial = new SoMaterial; |
| 94 |
setCoinAppearance(mat); |
| 95 |
pcShapeMaterial->ref(); |
| 96 |
pcShapeMaterial->setName("ShapeMaterial"); |
| 97 |
|
| 98 |
pcBoundingBox = new Gui::SoFCBoundingBox; |
| 99 |
pcBoundingBox->ref(); |
| 100 |
pcBoundingBox->setName("BoundingBox"); |
| 101 |
|
| 102 |
pcBoundColor = new SoBaseColor(); |
| 103 |
pcBoundColor->ref(); |
| 104 |
pcBoundColor->setName("BoundColor"); |
| 105 |
|
| 106 |
sPixmap = "Feature"; |
| 107 |
} |
| 108 |
|
| 109 |
ViewProviderGeometryObject::~ViewProviderGeometryObject() |
| 110 |
{ |
| 111 |
pcShapeMaterial->unref(); |
| 112 |
pcBoundingBox->unref(); |
| 113 |
pcBoundColor->unref(); |
| 114 |
pickStyle->unref(); |
| 115 |
} |
| 116 |
|
| 117 |
bool ViewProviderGeometryObject::isSelectionEnabled() const |
| 118 |
{ |
| 119 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 120 |
"User parameter:BaseApp/Preferences/View" |
| 121 |
); |
| 122 |
return hGrp->GetBool("EnableSelection", true); |
| 123 |
} |
| 124 |
|
| 125 |
void ViewProviderGeometryObject::onChanged(const App::Property* prop) |
| 126 |
{ |
| 127 |
// Actually, the properties 'ShapeColor' and 'Transparency' are part of the property |
| 128 |
// 'ShapeMaterial'. Both redundant properties are kept due to more convenience for the user. But |
| 129 |
// we must keep the values consistent of all these properties. |
| 130 |
std::string propName = prop->getName(); |
| 131 |
if (prop == &Selectable) { |
| 132 |
bool Sel = Selectable.getValue(); |
| 133 |
setSelectable(Sel); |
| 134 |
} |
| 135 |
else if (prop == &Transparency) { |
| 136 |
long value = Base::toPercent(ShapeAppearance.getTransparency()); |
| 137 |
float trans = Base::fromPercent(Transparency.getValue()); |
| 138 |
if (value != Transparency.getValue()) { |
| 139 |
ShapeAppearance.setTransparency(trans); |
| 140 |
} |
| 141 |
|
| 142 |
pcShapeMaterial->transparency = trans; |
| 143 |
} |
| 144 |
else if (prop == &ShapeAppearance) { |
| 145 |
if (getObject() && getObject()->testStatus(App::ObjectStatus::TouchOnColorChange)) { |
| 146 |
getObject()->touch(true); |
| 147 |
} |
| 148 |
long value = Base::toPercent(ShapeAppearance.getTransparency()); |
| 149 |
if (value != Transparency.getValue()) { |
| 150 |
Transparency.setValue(value); |
| 151 |
} |
| 152 |
if (ShapeAppearance.getSize() == 1) { |
| 153 |
const App::Material& Mat = ShapeAppearance[0]; |
| 154 |
setCoinAppearance(Mat); |
| 155 |
} |
| 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>()) { |
| 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>()) { |
| 177 |
auto geometry = getObject<App::GeoFeature>(); |
| 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 |
if (auto geometry = getObject<App::GeoFeature>()) { |
| 190 |
/* |
| 191 |
* Change the appearance only if the appearance hasn't been set explicitly. A cached |
| 192 |
* material appearance is used to see if the current appearance matches the last |
| 193 |
* material. It is also compared against an empty material to see if the saved |
| 194 |
* material value has been initialized. |
| 195 |
*/ |
| 196 |
App::Material defaultMaterial; |
| 197 |
auto material = geometry->getMaterialAppearance(); |
| 198 |
if ((ShapeAppearance.getSize() == 1) |
| 199 |
&& (ShapeAppearance[0] == defaultMaterial || ShapeAppearance[0] == materialAppearance) |
| 200 |
&& (material != defaultMaterial)) { |
| 201 |
ShapeAppearance.setValue(material); |
| 202 |
} |
| 203 |
materialAppearance = material; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
ViewProviderDragger::updateData(prop); |
| 208 |
} |
| 209 |
|
| 210 |
SoPickedPointList ViewProviderGeometryObject::getPickedPoints( |
| 211 |
const SbVec2s& pos, |
| 212 |
const View3DInventorViewer& viewer, |
| 213 |
bool pickAll |
| 214 |
) const |
| 215 |
{ |
| 216 |
auto root = new SoSeparator; |
| 217 |
root->ref(); |
| 218 |
root->addChild(viewer.getHeadlight()); |
| 219 |
root->addChild(viewer.getSoRenderManager()->getCamera()); |
| 220 |
root->addChild(getRoot()); |
| 221 |
|
| 222 |
SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion()); |
| 223 |
rp.setPickAll(pickAll); |
| 224 |
rp.setRadius(viewer.getPickRadius()); |
| 225 |
rp.setPoint(pos); |
| 226 |
rp.apply(root); |
| 227 |
root->unref(); |
| 228 |
|
| 229 |
// returns a copy of the list |
| 230 |
return rp.getPickedPointList(); |
| 231 |
} |
| 232 |
|
| 233 |
SoPickedPoint* ViewProviderGeometryObject::getPickedPoint( |
| 234 |
const SbVec2s& pos, |
| 235 |
const View3DInventorViewer& viewer |
| 236 |
) const |
| 237 |
{ |
| 238 |
auto root = new SoSeparator; |
| 239 |
root->ref(); |
| 240 |
root->addChild(viewer.getHeadlight()); |
| 241 |
root->addChild(viewer.getSoRenderManager()->getCamera()); |
| 242 |
root->addChild(getRoot()); |
| 243 |
|
| 244 |
SoRayPickAction rp(viewer.getSoRenderManager()->getViewportRegion()); |
| 245 |
rp.setPoint(pos); |
| 246 |
rp.setRadius(viewer.getPickRadius()); |
| 247 |
rp.apply(root); |
| 248 |
root->unref(); |
| 249 |
|
| 250 |
// returns a copy of the point |
| 251 |
SoPickedPoint* pick = rp.getPickedPoint(); |
| 252 |
// return (pick ? pick->copy() : 0); // needs the same instance of CRT under MS Windows |
| 253 |
return (pick ? new SoPickedPoint(*pick) : nullptr); |
| 254 |
} |
| 255 |
|
| 256 |
unsigned long ViewProviderGeometryObject::getBoundColor() const |
| 257 |
{ |
| 258 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 259 |
"User parameter:BaseApp/Preferences/View" |
| 260 |
); |
| 261 |
// white (255,255,255) |
| 262 |
unsigned long bbcol = hGrp->GetUnsigned("BoundingBoxColor", 4294967295UL); |
| 263 |
return bbcol; |
| 264 |
} |
| 265 |
|
| 266 |
void ViewProviderGeometryObject::setCoinAppearance(const App::Material& source) |
| 267 |
{ |
| 268 |
pcShapeMaterial->ambientColor |
| 269 |
.setValue(source.ambientColor.r, source.ambientColor.g, source.ambientColor.b); |
| 270 |
pcShapeMaterial->diffuseColor |
| 271 |
.setValue(source.diffuseColor.r, source.diffuseColor.g, source.diffuseColor.b); |
| 272 |
pcShapeMaterial->specularColor |
| 273 |
.setValue(source.specularColor.r, source.specularColor.g, source.specularColor.b); |
| 274 |
pcShapeMaterial->emissiveColor |
| 275 |
.setValue(source.emissiveColor.r, source.emissiveColor.g, source.emissiveColor.b); |
| 276 |
pcShapeMaterial->shininess.setValue(source.shininess); |
| 277 |
pcShapeMaterial->transparency.setValue(source.transparency); |
| 278 |
} |
| 279 |
|
| 280 |
namespace |
| 281 |
{ |
| 282 |
float getBoundBoxFontSize() |
| 283 |
{ |
| 284 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 285 |
"User parameter:BaseApp/Preferences/View" |
| 286 |
); |
| 287 |
return hGrp->GetFloat("BoundingBoxFontSize", 10.0); |
| 288 |
} |
| 289 |
} // namespace |
| 290 |
|
| 291 |
void ViewProviderGeometryObject::showBoundingBox(bool show) |
| 292 |
{ |
| 293 |
if (!pcBoundSwitch && show) { |
| 294 |
unsigned long bbcol = getBoundColor(); |
| 295 |
float red {}; |
| 296 |
float green {}; |
| 297 |
float blue {}; |
| 298 |
red = ((bbcol >> 24) & 0xff) / 255.0F; |
| 299 |
green = ((bbcol >> 16) & 0xff) / 255.0F; |
| 300 |
blue = ((bbcol >> 8) & 0xff) / 255.0F; |
| 301 |
|
| 302 |
pcBoundSwitch = new SoSwitch(); |
| 303 |
pcBoundSwitch->setName("BoundSwitch"); |
| 304 |
auto pBoundingSep = new SoSeparator(); |
| 305 |
auto lineStyle = new SoDrawStyle; |
| 306 |
lineStyle->lineWidth = 2.0F; |
| 307 |
pBoundingSep->addChild(lineStyle); |
| 308 |
|
| 309 |
pcBoundColor->rgb.setValue(red, green, blue); |
| 310 |
pBoundingSep->addChild(pcBoundColor); |
| 311 |
auto font = new SoFont(); |
| 312 |
font->size.setValue(getBoundBoxFontSize()); |
| 313 |
pBoundingSep->addChild(font); |
| 314 |
|
| 315 |
pBoundingSep->addChild(new SoResetTransform()); |
| 316 |
pBoundingSep->addChild(pcBoundingBox); |
| 317 |
pcBoundingBox->coordsOn.setValue(false); |
| 318 |
pcBoundingBox->dimensionsOn.setValue(true); |
| 319 |
|
| 320 |
// add to the highlight node |
| 321 |
pcBoundSwitch->addChild(pBoundingSep); |
| 322 |
pcRoot->addChild(pcBoundSwitch); |
| 323 |
} |
| 324 |
|
| 325 |
if (pcBoundSwitch) { |
| 326 |
pcBoundSwitch->whichChild = (show ? 0 : -1); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
void ViewProviderGeometryObject::setSelectable(bool selectable) |
| 331 |
{ |
| 332 |
SoSearchAction sa; |
| 333 |
sa.setInterest(SoSearchAction::ALL); |
| 334 |
sa.setSearchingAll(true); |
| 335 |
sa.setType(Gui::SoFCSelection::getClassTypeId()); |
| 336 |
sa.apply(pcRoot); |
| 337 |
|
| 338 |
SoPathList& pathList = sa.getPaths(); |
| 339 |
|
| 340 |
for (int i = 0; i < pathList.getLength(); i++) { |
| 341 |
auto selNode = dynamic_cast<SoFCSelection*>(pathList[i]->getTail()); |
| 342 |
if (selectable) { |
| 343 |
if (selNode) { |
| 344 |
selNode->selectionMode = SoFCSelection::SEL_ON; |
| 345 |
selNode->preselectionMode = SoFCSelection::AUTO; |
| 346 |
} |
| 347 |
} |
| 348 |
else { |
| 349 |
if (selNode) { |
| 350 |
selNode->selectionMode = SoFCSelection::SEL_OFF; |
| 351 |
selNode->preselectionMode = SoFCSelection::OFF; |
| 352 |
selNode->selected = SoFCSelection::NOTSELECTED; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
pickStyle->style.setValue(selectable ? SoPickStyle::SHAPE : SoPickStyle::UNPICKABLE); |
| 358 |
} |
| 359 |
|
| 360 |
PyObject* ViewProviderGeometryObject::getPyObject() |
| 361 |
{ |
| 362 |
if (!pyViewObject) { |
| 363 |
pyViewObject = new ViewProviderGeometryObjectPy(this); |
| 364 |
} |
| 365 |
pyViewObject->IncRef(); |
| 366 |
return pyViewObject; |
| 367 |
} |
| 368 |
|
| 369 |
void ViewProviderGeometryObject::handleChangedPropertyName( |
| 370 |
Base::XMLReader& reader, |
| 371 |
const char* TypeName, |
| 372 |
const char* PropName |
| 373 |
) |
| 374 |
{ |
| 375 |
if (strcmp(PropName, "ShapeColor") == 0 |
| 376 |
&& strcmp(TypeName, App::PropertyColor::getClassTypeId().getName()) == 0) { |
| 377 |
App::PropertyColor prop; |
| 378 |
prop.Restore(reader); |
| 379 |
ShapeAppearance.setDiffuseColor(prop.getValue()); |
| 380 |
} |
| 381 |
else if (strcmp(PropName, "ShapeMaterial") == 0 |
| 382 |
&& strcmp(TypeName, App::PropertyMaterial::getClassTypeId().getName()) == 0) { |
| 383 |
App::PropertyMaterial prop; |
| 384 |
prop.Restore(reader); |
| 385 |
ShapeAppearance.setValue(prop.getValue()); |
| 386 |
} |
| 387 |
else { |
| 388 |
ViewProviderDragger::handleChangedPropertyName(reader, TypeName, PropName); |
| 389 |
} |
| 390 |
} |