| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2005 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/SbSphere.h> |
| 25 |
#include <Inventor/SbString.h> |
| 26 |
#include <Inventor/SoInteraction.h> |
| 27 |
#include <Inventor/actions/SoGetBoundingBoxAction.h> |
| 28 |
#include <Inventor/actions/SoToVRML2Action.h> |
| 29 |
#include <Inventor/actions/SoWriteAction.h> |
| 30 |
#include <Inventor/fields/SoMFNode.h> |
| 31 |
#include <Inventor/fields/SoSFNode.h> |
| 32 |
#include <Inventor/nodes/SoGroup.h> |
| 33 |
#include <Inventor/VRMLnodes/SoVRMLGroup.h> |
| 34 |
#include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h> |
| 35 |
#include <Inventor/VRMLnodes/SoVRMLNormal.h> |
| 36 |
#include <Inventor/VRMLnodes/SoVRMLParent.h> |
| 37 |
#include <Inventor/VRMLnodes/SoVRMLShape.h> |
| 38 |
#include <QDir> |
| 39 |
#include <QProcess> |
| 40 |
#include <QTemporaryFile> |
| 41 |
#include <sstream> |
| 42 |
|
| 43 |
|
| 44 |
#include <Base/FileInfo.h> |
| 45 |
#include <Base/Persistence.h> |
| 46 |
#include <Base/Stream.h> |
| 47 |
#include <Base/Tools.h> |
| 48 |
#include <zipios++/gzipoutputstream.h> |
| 49 |
|
| 50 |
#include "SoFCDB.h" |
| 51 |
#include "Camera.h" |
| 52 |
#include "Flag.h" |
| 53 |
#include "Inventor/Draggers/SoTransformDragger.h" |
| 54 |
#include "Inventor/Draggers/SoLinearDragger.h" |
| 55 |
#include "Inventor/Draggers/SoLinearDraggerGeometry.h" |
| 56 |
#include "Inventor/Draggers/SoRotationDragger.h" |
| 57 |
#include "Inventor/Draggers/SoRotationDraggerGeometry.h" |
| 58 |
#include "Inventor/Draggers/Gizmo.h" |
| 59 |
#include "Navigation/GestureNavigationStyle.h" |
| 60 |
#include "Navigation/NavigationStyle.h" |
| 61 |
#include "Navigation/SiemensNXNavigationStyle.h" |
| 62 |
#include "SelectionObject.h" |
| 63 |
#include "SoDevicePixelRatioElement.h" |
| 64 |
#include "SoFCColorBar.h" |
| 65 |
#include "SoFCColorGradient.h" |
| 66 |
#include "SoFCColorLegend.h" |
| 67 |
#include "SoFCInteractiveElement.h" |
| 68 |
#include "SoFCSelection.h" |
| 69 |
#include "SoFCSelectionAction.h" |
| 70 |
#include "SoFCUnifiedSelection.h" |
| 71 |
#include "SoFCVectorizeSVGAction.h" |
| 72 |
#include "SoFCVectorizeU3DAction.h" |
| 73 |
#include "SoTextLabel.h" |
| 74 |
#include "SoDatumLabel.h" |
| 75 |
#include "Inventor/MarkerBitmaps.h" |
| 76 |
#include "Inventor/SmSwitchboard.h" |
| 77 |
#include "Inventor/So3DAnnotation.h" |
| 78 |
#include "Inventor/SoAutoZoomTranslation.h" |
| 79 |
#include "Inventor/SoAxisCrossKit.h" |
| 80 |
#include "Inventor/SoDrawingGrid.h" |
| 81 |
#include "Inventor/SoFCBackgroundGradient.h" |
| 82 |
#include "Inventor/SoFCBoundingBox.h" |
| 83 |
#include "Inventor/SoMouseWheelEvent.h" |
| 84 |
#include "Inventor/SoFCTransform.h" |
| 85 |
#include "Inventor/SoToggleSwitch.h" |
| 86 |
#include "propertyeditor/PropertyItem.h" |
| 87 |
#include "ArcEngine.h" |
| 88 |
|
| 89 |
#include <Inventor/SoFCPlacementIndicatorKit.h> |
| 90 |
|
| 91 |
|
| 92 |
using namespace Gui; |
| 93 |
using namespace Gui::Inventor; |
| 94 |
using namespace Gui::PropertyEditor; |
| 95 |
|
| 96 |
static SbBool init_done = false; |
| 97 |
static SoGroup* storage = nullptr; |
| 98 |
|
| 99 |
SbBool Gui::SoFCDB::isInitialized() |
| 100 |
{ |
| 101 |
return init_done; |
| 102 |
} |
| 103 |
|
| 104 |
void Gui::SoFCDB::init() |
| 105 |
{ |
| 106 |
SoInteraction ::init(); |
| 107 |
SoDevicePixelRatioElement ::initClass(); |
| 108 |
SoGLRenderActionElement ::initClass(); |
| 109 |
SoFCInteractiveElement ::initClass(); |
| 110 |
SoGLWidgetElement ::initClass(); |
| 111 |
SoFCColorBarBase ::initClass(); |
| 112 |
SoFCColorBar ::initClass(); |
| 113 |
SoFCColorLegend ::initClass(); |
| 114 |
SoFCColorGradient ::initClass(); |
| 115 |
SoFCBackgroundGradient ::initClass(); |
| 116 |
SoFCBoundingBox ::initClass(); |
| 117 |
SoFCSelection ::initClass(); |
| 118 |
SoFCUnifiedSelection ::initClass(); |
| 119 |
SoFCPreselectionAction ::initClass(); |
| 120 |
SoFCSelectionAction ::initClass(); |
| 121 |
SoFCDocumentAction ::initClass(); |
| 122 |
SoGLWidgetNode ::initClass(); |
| 123 |
SoGLVBOActivatedElement ::initClass(); |
| 124 |
SoFCEnableSelectionAction ::initClass(); |
| 125 |
SoFCEnablePreselectionAction ::initClass(); |
| 126 |
SoFCSelectionColorAction ::initClass(); |
| 127 |
SoFCHighlightColorAction ::initClass(); |
| 128 |
SoFCDocumentObjectAction ::initClass(); |
| 129 |
SoGLSelectAction ::initClass(); |
| 130 |
SoVisibleFaceAction ::initClass(); |
| 131 |
SoUpdateVBOAction ::initClass(); |
| 132 |
SoBoxSelectionRenderAction ::initClass(); |
| 133 |
SoFCVectorizeSVGAction ::initClass(); |
| 134 |
SoFCVectorizeU3DAction ::initClass(); |
| 135 |
SoHighlightElementAction ::initClass(); |
| 136 |
SoSelectionElementAction ::initClass(); |
| 137 |
SoVRMLAction ::initClass(); |
| 138 |
SoSkipBoundingGroup ::initClass(); |
| 139 |
SoTextLabel ::initClass(); |
| 140 |
SoDatumLabel ::initClass(); |
| 141 |
SoColorBarLabel ::initClass(); |
| 142 |
SoStringLabel ::initClass(); |
| 143 |
SoFrameLabel ::initClass(); |
| 144 |
TranslateManip ::initClass(); |
| 145 |
SoShapeScale ::initClass(); |
| 146 |
SoAxisCrossKit ::initClass(); |
| 147 |
SoRegPoint ::initClass(); |
| 148 |
SoDrawingGrid ::initClass(); |
| 149 |
SoFCTransform ::initClass(); |
| 150 |
SoAutoZoomTranslation ::initClass(); |
| 151 |
MarkerBitmaps ::initClass(); |
| 152 |
SoTransformDragger ::initClass(); |
| 153 |
SoLinearGeometryKit ::initClass(); |
| 154 |
SoArrowGeometry ::initClass(); |
| 155 |
SoLinearGeometryBaseKit ::initClass(); |
| 156 |
SoArrowBase ::initClass(); |
| 157 |
SoRotatorGeometryKit ::initClass(); |
| 158 |
SoRotatorGeometry ::initClass(); |
| 159 |
SoRotatorGeometry2 ::initClass(); |
| 160 |
SoRotatorArrow ::initClass(); |
| 161 |
SoRotatorGeometryBaseKit ::initClass(); |
| 162 |
SoRotatorBase ::initClass(); |
| 163 |
SoToggleSwitch ::initClass(); |
| 164 |
GizmoContainer ::initClass(); |
| 165 |
SmSwitchboard ::initClass(); |
| 166 |
SoFCSeparator ::initClass(); |
| 167 |
SoFCSelectionRoot ::initClass(); |
| 168 |
SoFCPathAnnotation ::initClass(); |
| 169 |
SoMouseWheelEvent ::initClass(); |
| 170 |
So3DAnnotation ::initClass(); |
| 171 |
SoDelayedAnnotationsElement ::initClass(); |
| 172 |
SoFCPlacementIndicatorKit ::initClass(); |
| 173 |
|
| 174 |
PropertyItem ::init(); |
| 175 |
PropertySeparatorItem ::init(); |
| 176 |
PropertyStringItem ::init(); |
| 177 |
PropertyFontItem ::init(); |
| 178 |
PropertyIntegerItem ::init(); |
| 179 |
PropertyIntegerConstraintItem ::init(); |
| 180 |
PropertyFloatItem ::init(); |
| 181 |
PropertyUnitItem ::init(); |
| 182 |
PropertyFloatConstraintItem ::init(); |
| 183 |
PropertyPrecisionItem ::init(); |
| 184 |
PropertyUnitConstraintItem ::init(); |
| 185 |
PropertyAngleItem ::init(); |
| 186 |
PropertyBoolItem ::init(); |
| 187 |
PropertyVectorItem ::init(); |
| 188 |
PropertyVectorListItem ::init(); |
| 189 |
PropertyVectorDistanceItem ::init(); |
| 190 |
PropertyPositionItem ::init(); |
| 191 |
PropertyDirectionItem ::init(); |
| 192 |
PropertyMatrixItem ::init(); |
| 193 |
PropertyPlacementItem ::init(); |
| 194 |
PropertyRotationItem ::init(); |
| 195 |
PropertyEnumItem ::init(); |
| 196 |
PropertyStringListItem ::init(); |
| 197 |
PropertyFloatListItem ::init(); |
| 198 |
PropertyIntegerListItem ::init(); |
| 199 |
PropertyColorItem ::init(); |
| 200 |
PropertyMaterialItem ::init(); |
| 201 |
PropertyMaterialListItem ::init(); |
| 202 |
PropertyFileItem ::init(); |
| 203 |
PropertyPathItem ::init(); |
| 204 |
PropertyTransientFileItem ::init(); |
| 205 |
PropertyLinkItem ::init(); |
| 206 |
PropertyLinkListItem ::init(); |
| 207 |
|
| 208 |
NavigationStyle ::init(); |
| 209 |
UserNavigationStyle ::init(); |
| 210 |
NavigationStateChart ::init(); |
| 211 |
BlenderNavigationStyle ::init(); |
| 212 |
CADNavigationStyle ::init(); |
| 213 |
GestureNavigationStyle ::init(); |
| 214 |
MayaGestureNavigationStyle ::init(); |
| 215 |
OpenCascadeNavigationStyle ::init(); |
| 216 |
InventorNavigationStyle ::init(); |
| 217 |
OpenSCADNavigationStyle ::init(); |
| 218 |
RevitNavigationStyle ::init(); |
| 219 |
SiemensNXNavigationStyle ::init(); |
| 220 |
SolidWorksNavigationStyle ::init(); |
| 221 |
TinkerCADNavigationStyle ::init(); |
| 222 |
TouchpadNavigationStyle ::init(); |
| 223 |
|
| 224 |
GLGraphicsItem ::init(); |
| 225 |
GLFlagWindow ::init(); |
| 226 |
|
| 227 |
SelectionObject ::init(); |
| 228 |
|
| 229 |
ArcEngine ::initClass(); |
| 230 |
|
| 231 |
qRegisterMetaType<Base::Vector3f>("Base::Vector3f"); |
| 232 |
qRegisterMetaType<Base::Vector3d>("Base::Vector3d"); |
| 233 |
qRegisterMetaType<Base::Quantity>("Base::Quantity"); |
| 234 |
qRegisterMetaType<QList<Base::Quantity>>("Base::QuantityList"); |
| 235 |
init_done = true; |
| 236 |
|
| 237 |
assert(!storage); |
| 238 |
storage = new SoGroup(); |
| 239 |
storage->ref(); |
| 240 |
} |
| 241 |
|
| 242 |
void Gui::SoFCDB::finish() |
| 243 |
{ |
| 244 |
// Coin doesn't provide a mechanism to free static members of own data types. |
| 245 |
// Hence, we need to define a static method e.g. 'finish()' for all new types |
| 246 |
// to invoke the private member function 'atexit_cleanup()'. |
| 247 |
SoFCColorBarBase ::finish(); |
| 248 |
SoFCColorBar ::finish(); |
| 249 |
SoFCColorLegend ::finish(); |
| 250 |
SoFCColorGradient ::finish(); |
| 251 |
SoFCBackgroundGradient ::finish(); |
| 252 |
SoFCBoundingBox ::finish(); |
| 253 |
SoFCSelection ::finish(); |
| 254 |
SoFCPreselectionAction ::finish(); |
| 255 |
SoFCSelectionAction ::finish(); |
| 256 |
SoFCDocumentAction ::finish(); |
| 257 |
SoFCDocumentObjectAction ::finish(); |
| 258 |
SoFCEnableSelectionAction ::finish(); |
| 259 |
SoFCEnablePreselectionAction ::finish(); |
| 260 |
SoFCSelectionColorAction ::finish(); |
| 261 |
SoUpdateVBOAction ::finish(); |
| 262 |
SoFCHighlightColorAction ::finish(); |
| 263 |
SoFCSeparator ::finish(); |
| 264 |
SoFCSelectionRoot ::finish(); |
| 265 |
SoFCPathAnnotation ::finish(); |
| 266 |
|
| 267 |
storage->unref(); |
| 268 |
storage = nullptr; |
| 269 |
} |
| 270 |
|
| 271 |
// buffer acrobatics for inventor **************************************************** |
| 272 |
|
| 273 |
namespace |
| 274 |
{ |
| 275 |
static std::vector<char> static_buffer; |
| 276 |
|
| 277 |
static void* buffer_realloc(void* /*bufptr*/, std::size_t size) |
| 278 |
{ |
| 279 |
static_buffer.resize(size); |
| 280 |
return static_buffer.data(); |
| 281 |
} |
| 282 |
} // namespace |
| 283 |
|
| 284 |
const std::string& Gui::SoFCDB::writeNodesToString(SoNode* root) |
| 285 |
{ |
| 286 |
SoOutput out; |
| 287 |
static_buffer.resize(1024); |
| 288 |
out.setBuffer(static_buffer.data(), static_buffer.size(), buffer_realloc); |
| 289 |
if (root && root->getTypeId().isDerivedFrom(SoVRMLParent::getClassTypeId())) { |
| 290 |
out.setHeaderString("#VRML V2.0 utf8"); |
| 291 |
} |
| 292 |
|
| 293 |
SoWriteAction wa(&out); |
| 294 |
wa.apply(root); |
| 295 |
|
| 296 |
static std::string cReturnString; |
| 297 |
cReturnString = static_buffer.data(); |
| 298 |
return cReturnString; |
| 299 |
} |
| 300 |
|
| 301 |
SoNode* replaceSwitches(SoNodeList* children, SoGroup* parent) |
| 302 |
{ |
| 303 |
if (!children) { |
| 304 |
return parent; |
| 305 |
} |
| 306 |
|
| 307 |
for (int i = 0; i < children->getLength(); i++) { |
| 308 |
SoNode* node = (*children)[i]; |
| 309 |
if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) { |
| 310 |
if (node->getTypeId().isDerivedFrom(SoSwitch::getClassTypeId())) { |
| 311 |
auto group = static_cast<SoSwitch*>(node); |
| 312 |
int which = group->whichChild.getValue(); |
| 313 |
if (which == SO_SWITCH_NONE) { |
| 314 |
continue; |
| 315 |
} |
| 316 |
auto newParent = new SoGroup(); |
| 317 |
SoNodeList c; |
| 318 |
if (which >= 0) { |
| 319 |
c.append(group->getChild(which)); |
| 320 |
} |
| 321 |
else { |
| 322 |
// SO_SWITCH_INHERIT or SO_SWITCH_ALL |
| 323 |
for (int i = 0; i < group->getNumChildren(); i++) { |
| 324 |
c.append(group->getChild(i)); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
replaceSwitches(&c, newParent); |
| 329 |
parent->addChild(newParent); |
| 330 |
} |
| 331 |
else { |
| 332 |
auto newParent = static_cast<SoGroup*>(node->getTypeId().createInstance()); |
| 333 |
replaceSwitches(node->getChildren(), newParent); |
| 334 |
parent->addChild(newParent); |
| 335 |
} |
| 336 |
} |
| 337 |
else { |
| 338 |
parent->addChild(node); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
return parent; |
| 343 |
} |
| 344 |
|
| 345 |
SoNode* replaceSwitchesInSceneGraph(SoNode* node) |
| 346 |
{ |
| 347 |
if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) { |
| 348 |
return replaceSwitches(node->getChildren(), new SoSeparator); |
| 349 |
} |
| 350 |
|
| 351 |
return node; |
| 352 |
} |
| 353 |
|
| 354 |
SoNode* Gui::SoFCDB::replaceSwitches(SoNode* node) |
| 355 |
{ |
| 356 |
return replaceSwitchesInSceneGraph(node); |
| 357 |
} |
| 358 |
|
| 359 |
void Gui::SoFCDB::writeToVRML(SoNode* node, std::string& buffer) |
| 360 |
{ |
| 361 |
SoNode* noSwitches = replaceSwitchesInSceneGraph(node); |
| 362 |
noSwitches->ref(); |
| 363 |
SoVRMLAction vrml2; |
| 364 |
vrml2.setOverrideMode(true); |
| 365 |
vrml2.apply(noSwitches); |
| 366 |
SoToVRML2Action tovrml2; |
| 367 |
tovrml2.apply(noSwitches); |
| 368 |
SoVRMLGroup* vrmlRoot = tovrml2.getVRML2SceneGraph(); |
| 369 |
|
| 370 |
vrmlRoot->setInstancePrefix(SbString("o")); |
| 371 |
vrmlRoot->ref(); |
| 372 |
buffer = SoFCDB::writeNodesToString(vrmlRoot); |
| 373 |
vrmlRoot->unref(); // release the memory as soon as possible |
| 374 |
|
| 375 |
// restore old settings |
| 376 |
vrml2.setOverrideMode(false); |
| 377 |
vrml2.apply(noSwitches); |
| 378 |
noSwitches->unref(); |
| 379 |
} |
| 380 |
|
| 381 |
bool Gui::SoFCDB::writeToVRML(SoNode* node, const char* filename, bool binary) |
| 382 |
{ |
| 383 |
std::string buffer; |
| 384 |
writeToVRML(node, buffer); |
| 385 |
|
| 386 |
Base::FileInfo fi(filename); |
| 387 |
if (binary) { |
| 388 |
// We want to write compressed VRML but Coin 2.4.3 doesn't do it even though |
| 389 |
// SoOutput::getAvailableCompressionMethods() delivers a string list that |
| 390 |
// contains 'GZIP'. setCompression() was called directly after opening the file, |
| 391 |
// returned true and no error message appeared but anyway it didn't work. |
| 392 |
// Strange is that reading GZIPped VRML files works. |
| 393 |
// So, we do the compression on our own. |
| 394 |
Base::ofstream str(fi, std::ios::out | std::ios::binary); |
| 395 |
zipios::GZIPOutputStream gzip(str); |
| 396 |
|
| 397 |
if (gzip) { |
| 398 |
gzip << buffer; |
| 399 |
gzip.close(); |
| 400 |
return true; |
| 401 |
} |
| 402 |
} |
| 403 |
else { |
| 404 |
Base::ofstream str(fi, std::ios::out); |
| 405 |
|
| 406 |
if (str) { |
| 407 |
str << buffer; |
| 408 |
str.close(); |
| 409 |
return true; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
bool Gui::SoFCDB::writeToX3D(SoNode* node, const char* filename, bool binary) |
| 417 |
{ |
| 418 |
std::string buffer; |
| 419 |
writeToX3D(node, false, buffer); |
| 420 |
|
| 421 |
Base::FileInfo fi(filename); |
| 422 |
if (binary) { |
| 423 |
Base::ofstream str(fi, std::ios::out | std::ios::binary); |
| 424 |
zipios::GZIPOutputStream gzip(str); |
| 425 |
|
| 426 |
if (gzip) { |
| 427 |
gzip << buffer; |
| 428 |
gzip.close(); |
| 429 |
return true; |
| 430 |
} |
| 431 |
} |
| 432 |
else { |
| 433 |
Base::ofstream str(fi, std::ios::out); |
| 434 |
|
| 435 |
if (str) { |
| 436 |
str << buffer; |
| 437 |
str.close(); |
| 438 |
return true; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
return false; |
| 443 |
} |
| 444 |
|
| 445 |
bool Gui::SoFCDB::writeToX3D(SoNode* node, bool exportViewpoints, std::string& buffer) |
| 446 |
{ |
| 447 |
SoNode* noSwitches = replaceSwitchesInSceneGraph(node); |
| 448 |
noSwitches->ref(); |
| 449 |
SoVRMLAction vrml2; |
| 450 |
vrml2.setOverrideMode(true); |
| 451 |
vrml2.apply(noSwitches); |
| 452 |
SoToVRML2Action tovrml2; |
| 453 |
tovrml2.apply(noSwitches); |
| 454 |
SoVRMLGroup* vrmlRoot = tovrml2.getVRML2SceneGraph(); |
| 455 |
|
| 456 |
vrmlRoot->setInstancePrefix(SbString("o")); |
| 457 |
vrmlRoot->ref(); |
| 458 |
|
| 459 |
// Search for SoVRMLIndexedFaceSet nodes and set creaseAngle to 0.5 |
| 460 |
{ |
| 461 |
SoSearchAction sa; |
| 462 |
sa.setType(SoVRMLShape::getClassTypeId()); |
| 463 |
sa.setInterest(SoSearchAction::ALL); |
| 464 |
sa.setSearchingAll(true); |
| 465 |
sa.apply(vrmlRoot); |
| 466 |
SoPathList& paths = sa.getPaths(); |
| 467 |
for (int i = 0; i < paths.getLength(); i++) { |
| 468 |
SoPath* path = paths[i]; |
| 469 |
auto shape = static_cast<SoVRMLShape*>(path->getTail()); |
| 470 |
SoNode* geom = shape->geometry.getValue(); |
| 471 |
if (geom && geom->getTypeId() == SoVRMLIndexedFaceSet::getClassTypeId()) { |
| 472 |
SoNode* norm = static_cast<SoVRMLIndexedFaceSet*>(geom)->normal.getValue(); |
| 473 |
if (norm && norm->getTypeId() == SoVRMLNormal::getClassTypeId()) { |
| 474 |
// if empty then nullify the normal field node |
| 475 |
if (static_cast<SoVRMLNormal*>(norm)->vector.getNum() == 0) { |
| 476 |
static_cast<SoVRMLIndexedFaceSet*>(geom)->normal.setValue(nullptr); |
| 477 |
} |
| 478 |
} |
| 479 |
else { |
| 480 |
static_cast<SoVRMLIndexedFaceSet*>(geom)->creaseAngle.setValue(0.5f); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
sa.reset(); // clear the internal cache |
| 485 |
} |
| 486 |
|
| 487 |
std::stringstream out; |
| 488 |
writeX3D(vrmlRoot, exportViewpoints, out); |
| 489 |
buffer = out.str(); |
| 490 |
|
| 491 |
vrmlRoot->unref(); // release the memory as soon as possible |
| 492 |
|
| 493 |
// restore old settings |
| 494 |
vrml2.setOverrideMode(false); |
| 495 |
vrml2.apply(noSwitches); |
| 496 |
noSwitches->unref(); |
| 497 |
|
| 498 |
return true; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
void Gui::SoFCDB::writeX3DFields( |
| 503 |
SoNode* node, |
| 504 |
std::map<SoNode*, std::string>& nodeMap, |
| 505 |
bool isRoot, |
| 506 |
int& numDEF, |
| 507 |
int spaces, |
| 508 |
std::ostream& out |
| 509 |
) |
| 510 |
{ |
| 511 |
// remove the VRML prefix from the type name |
| 512 |
std::string type(node->getTypeId().getName().getString()); |
| 513 |
type = type.substr(4); |
| 514 |
|
| 515 |
out << Base::blanks(spaces) << "<" << type; |
| 516 |
if (node->getRefCount() > 1 && !isRoot) { |
| 517 |
SbName name = node->getName(); |
| 518 |
std::stringstream str; |
| 519 |
if (name.getLength() == 0) { |
| 520 |
str << "o" << numDEF++; |
| 521 |
} |
| 522 |
else { |
| 523 |
str << name.getString(); |
| 524 |
} |
| 525 |
|
| 526 |
nodeMap[node] = str.str(); |
| 527 |
std::string escapedName = Base::Persistence::encodeAttribute(str.str()); |
| 528 |
out << " DEF=\"" << escapedName << "\""; |
| 529 |
} |
| 530 |
|
| 531 |
const SoFieldData* fielddata = node->getFieldData(); |
| 532 |
if (fielddata) { |
| 533 |
int numFieldNodes = 0; |
| 534 |
|
| 535 |
// process non-SoSFNode and non-SoMFNode fields |
| 536 |
for (int i = 0; i < fielddata->getNumFields(); i++) { |
| 537 |
SoField* field = fielddata->getField(node, i); |
| 538 |
if (!field->isDefault()) { |
| 539 |
if (!field->isOfType(SoSFNode::getClassTypeId()) |
| 540 |
&& !field->isOfType(SoMFNode::getClassTypeId())) { |
| 541 |
SbString value; |
| 542 |
field->get(value); |
| 543 |
QByteArray ba(value.getString(), value.getLength()); |
| 544 |
ba.replace('\n', ' '); |
| 545 |
if (field->isOfType(SoMField::getClassTypeId())) { |
| 546 |
ba.replace('[', ' '); |
| 547 |
ba.replace(']', ' '); |
| 548 |
ba = ba.simplified(); |
| 549 |
} |
| 550 |
|
| 551 |
// escape XML special characters in attribute value |
| 552 |
std::string escapedValue = Base::Persistence::encodeAttribute( |
| 553 |
std::string(ba.data()) |
| 554 |
); |
| 555 |
|
| 556 |
out << '\n' |
| 557 |
<< Base::blanks(spaces + 2) << fielddata->getFieldName(i).getString() |
| 558 |
<< "=\"" << escapedValue << "\" "; |
| 559 |
} |
| 560 |
else { |
| 561 |
numFieldNodes++; |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
if (numFieldNodes > 0) { |
| 567 |
out << ">\n"; |
| 568 |
} |
| 569 |
else { |
| 570 |
out << "/>\n"; |
| 571 |
} |
| 572 |
|
| 573 |
// process SoSFNode or SoMFNode fields |
| 574 |
for (int i = 0; i < fielddata->getNumFields(); i++) { |
| 575 |
SoField* field = fielddata->getField(node, i); |
| 576 |
if (!field->isDefault()) { |
| 577 |
if (field->isOfType(SoSFNode::getClassTypeId())) { |
| 578 |
auto sfNode = static_cast<SoSFNode*>(field); |
| 579 |
writeX3DChild(sfNode->getValue(), nodeMap, numDEF, spaces + 2, out); |
| 580 |
} |
| 581 |
else if (field->isOfType(SoMFNode::getClassTypeId())) { |
| 582 |
auto mfNode = static_cast<SoMFNode*>(field); |
| 583 |
for (int j = 0; j < mfNode->getNum(); j++) { |
| 584 |
writeX3DChild(mfNode->getNode(j), nodeMap, numDEF, spaces + 2, out); |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
if (numFieldNodes > 0) { |
| 591 |
out << Base::blanks(spaces) << "</" << type << ">\n"; |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
void Gui::SoFCDB::writeX3DChild( |
| 597 |
SoNode* node, |
| 598 |
std::map<SoNode*, std::string>& nodeMap, |
| 599 |
int& numDEF, |
| 600 |
int spaces, |
| 601 |
std::ostream& out |
| 602 |
) |
| 603 |
{ |
| 604 |
if (!node) { |
| 605 |
return; |
| 606 |
} |
| 607 |
|
| 608 |
// check if the node is already used |
| 609 |
auto mapIt = nodeMap.find(node); |
| 610 |
if (mapIt == nodeMap.end()) { |
| 611 |
writeX3DFields(node, nodeMap, false, numDEF, spaces, out); |
| 612 |
} |
| 613 |
else { |
| 614 |
// remove the VRML prefix from the type name |
| 615 |
std::string sftype(node->getTypeId().getName().getString()); |
| 616 |
sftype = sftype.substr(4); |
| 617 |
std::string escapedRef = Base::Persistence::encodeAttribute(mapIt->second); |
| 618 |
out << Base::blanks(spaces) << "<" << sftype << " USE=\"" << escapedRef << "\" />\n"; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
void Gui::SoFCDB::writeX3D(SoVRMLGroup* node, bool exportViewpoints, std::ostream& out) |
| 623 |
{ |
| 624 |
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 625 |
out << "<!DOCTYPE X3D PUBLIC \"ISO//Web3D//DTD X3D 3.3//EN\" " |
| 626 |
"\"http://www.web3d.org/specifications/x3d-3.3.dtd\">\n"; |
| 627 |
out << "<X3D profile=\"Immersive\" version=\"3.3\" " |
| 628 |
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema-instance\" " |
| 629 |
"xsd:noNamespaceSchemaLocation=\"http://www.web3d.org/specifications/x3d-3.3.xsd\">\n"; |
| 630 |
out << " <head>\n" |
| 631 |
" <meta name=\"generator\" content=\"FreeCAD\"/>\n" |
| 632 |
" <meta name=\"author\" content=\"\"/>\n" |
| 633 |
" <meta name=\"company\" content=\"\"/>\n" |
| 634 |
" </head>\n"; |
| 635 |
|
| 636 |
std::map<SoNode*, std::string> nodeMap; |
| 637 |
out << " <Scene>\n"; |
| 638 |
|
| 639 |
// compute a sensible view point |
| 640 |
SoGetBoundingBoxAction bboxAction(SbViewportRegion(1280, 1024)); |
| 641 |
bboxAction.apply(node); |
| 642 |
SbBox3f bbox = bboxAction.getBoundingBox(); |
| 643 |
SbSphere bs; |
| 644 |
bs.circumscribe(bbox); |
| 645 |
const SbVec3f& cnt = bs.getCenter(); |
| 646 |
float dist = 2.4f * bs.getRadius(); |
| 647 |
float dist3 = 0.577350f * dist; // sqrt(1/3) * dist |
| 648 |
|
| 649 |
if (exportViewpoints) { |
| 650 |
auto viewpoint = |
| 651 |
[&out](const char* text, const SbVec3f& cnt, const SbVec3f& pos, const SbRotation& rot) { |
| 652 |
SbVec3f axis; |
| 653 |
float angle; |
| 654 |
rot.getValue(axis, angle); |
| 655 |
out << " <Viewpoint id=\"" << text << "\" centerOfRotation=\"" << cnt[0] << " " |
| 656 |
<< cnt[1] << " " << cnt[2] << "\" position=\"" << pos[0] << " " << pos[1] << " " |
| 657 |
<< pos[2] << "\" orientation=\"" << axis[0] << " " << axis[1] << " " << axis[2] |
| 658 |
<< " " << angle << R"(" description="camera" fieldOfView="0.9">)" |
| 659 |
<< "</Viewpoint>\n"; |
| 660 |
}; |
| 661 |
|
| 662 |
viewpoint( |
| 663 |
"Iso", |
| 664 |
cnt, |
| 665 |
SbVec3f(cnt[0] + dist3, cnt[1] - dist3, cnt[2] + dist3), |
| 666 |
Camera::rotation(Camera::Isometric) |
| 667 |
); |
| 668 |
viewpoint("Front", cnt, SbVec3f(cnt[0], cnt[1] - dist, cnt[2]), Camera::rotation(Camera::Front)); |
| 669 |
viewpoint("Back", cnt, SbVec3f(cnt[0], cnt[1] + dist, cnt[2]), Camera::rotation(Camera::Rear)); |
| 670 |
viewpoint("Right", cnt, SbVec3f(cnt[0] + dist, cnt[1], cnt[2]), Camera::rotation(Camera::Right)); |
| 671 |
viewpoint("Left", cnt, SbVec3f(cnt[0] - dist, cnt[1], cnt[2]), Camera::rotation(Camera::Left)); |
| 672 |
viewpoint("Top", cnt, SbVec3f(cnt[0], cnt[1], cnt[2] + dist), Camera::rotation(Camera::Top)); |
| 673 |
viewpoint("Bottom", cnt, SbVec3f(cnt[0], cnt[1], cnt[2] - dist), Camera::rotation(Camera::Bottom)); |
| 674 |
} |
| 675 |
|
| 676 |
int numDEF = 0; |
| 677 |
writeX3DFields(node, nodeMap, true, numDEF, 4, out); |
| 678 |
out << " </Scene>\n"; |
| 679 |
out << "</X3D>\n"; |
| 680 |
} |
| 681 |
|
| 682 |
bool Gui::SoFCDB::writeToX3DOM(SoNode* node, std::string& buffer) |
| 683 |
{ |
| 684 |
std::string x3d; |
| 685 |
if (!writeToX3D(node, true, x3d)) { |
| 686 |
return false; |
| 687 |
} |
| 688 |
|
| 689 |
// remove the first two lines from the x3d output as this duplicates |
| 690 |
// the xml and doctype header |
| 691 |
std::size_t pos = x3d.find('\n'); |
| 692 |
pos = x3d.find('\n', pos + 1); |
| 693 |
x3d = x3d.erase(0, pos + 1); |
| 694 |
|
| 695 |
std::stringstream out; |
| 696 |
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 697 |
out << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " |
| 698 |
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"; |
| 699 |
out << "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" |
| 700 |
<< " <head>\n" |
| 701 |
<< " <meta charset=\"utf-8\"/>\n" |
| 702 |
<< " <title>FreeCAD X3DOM Export</title>\n" |
| 703 |
<< " <script src=\"http://www.x3dom.org/download/x3dom.js\"> </script>\n" |
| 704 |
<< " <link rel=\"stylesheet\" type=\"text/css\" " |
| 705 |
"href=\"http://www.x3dom.org/download/x3dom.css\"/>\n" |
| 706 |
<< " </head>\n" |
| 707 |
<< " <body>\n" |
| 708 |
<< " <div>\n"; |
| 709 |
|
| 710 |
auto onclick = [&out](const char* text) { |
| 711 |
out << " <button onclick=\"document.getElementById('" << text |
| 712 |
<< "').setAttribute('set_bind','true');\">" << text << "</button>\n"; |
| 713 |
}; |
| 714 |
|
| 715 |
onclick("Iso"); |
| 716 |
onclick("Front"); |
| 717 |
onclick("Back"); |
| 718 |
onclick("Right"); |
| 719 |
onclick("Left"); |
| 720 |
onclick("Top"); |
| 721 |
onclick("Bottom"); |
| 722 |
|
| 723 |
out << " </div>\n" << x3d; |
| 724 |
|
| 725 |
out << " </body>\n" |
| 726 |
<< "</html>\n"; |
| 727 |
|
| 728 |
buffer = out.str(); |
| 729 |
|
| 730 |
return true; |
| 731 |
} |
| 732 |
|
| 733 |
bool Gui::SoFCDB::writeToFile(SoNode* node, const char* filename, bool binary) |
| 734 |
{ |
| 735 |
bool ret = false; |
| 736 |
Base::FileInfo fi(filename); |
| 737 |
|
| 738 |
// Write VRML V2.0 |
| 739 |
if (fi.hasExtension({"wrl", "vrml", "wrz"})) { |
| 740 |
// If 'wrz' is set then force compression |
| 741 |
if (fi.hasExtension("wrz")) { |
| 742 |
binary = true; |
| 743 |
} |
| 744 |
|
| 745 |
ret = SoFCDB::writeToVRML(node, filename, binary); |
| 746 |
} |
| 747 |
else if (fi.hasExtension({"x3d", "x3dz"})) { |
| 748 |
// If 'x3dz' is set then force compression |
| 749 |
if (fi.hasExtension("x3dz")) { |
| 750 |
binary = true; |
| 751 |
} |
| 752 |
|
| 753 |
ret = SoFCDB::writeToX3D(node, filename, binary); |
| 754 |
} |
| 755 |
else if (fi.hasExtension("xhtml")) { |
| 756 |
std::string buffer; |
| 757 |
if (SoFCDB::writeToX3DOM(node, buffer)) { |
| 758 |
Base::ofstream str(Base::FileInfo(filename), std::ios::out); |
| 759 |
|
| 760 |
if (str) { |
| 761 |
str << buffer; |
| 762 |
str.close(); |
| 763 |
ret = true; |
| 764 |
} |
| 765 |
} |
| 766 |
} |
| 767 |
else if (fi.hasExtension("iv")) { |
| 768 |
// Write Inventor in ASCII |
| 769 |
std::string buffer = SoFCDB::writeNodesToString(node); |
| 770 |
Base::ofstream str(Base::FileInfo(filename), std::ios::out); |
| 771 |
|
| 772 |
if (str) { |
| 773 |
str << buffer; |
| 774 |
str.close(); |
| 775 |
ret = true; |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
return ret; |
| 780 |
} |
| 781 |
|
| 782 |
SoGroup* Gui::SoFCDB::getStorage() |
| 783 |
{ |
| 784 |
assert(storage); // call init first. |
| 785 |
return storage; |
| 786 |
} |