/
SoFCDB.cpp
373 lines (333 loc) · 13.7 KB
/
SoFCDB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/***************************************************************************
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <Inventor/actions/SoToVRML2Action.h>
# include <Inventor/VRMLnodes/SoVRMLGroup.h>
# include <Inventor/VRMLnodes/SoVRMLParent.h>
# include <Inventor/SbString.h>
# include <Inventor/nodes/SoGroup.h>
#endif
#include <Base/FileInfo.h>
#include <Base/Stream.h>
#include <zipios++/gzipoutputstream.h>
#include "SoFCDB.h"
#include "SoFCColorBar.h"
#include "SoFCColorLegend.h"
#include "SoFCColorGradient.h"
#include "SoFCSelection.h"
#include "SoFCBackgroundGradient.h"
#include "SoFCBoundingBox.h"
#include "SoFCSelection.h"
#include "SoFCUnifiedSelection.h"
#include "SoFCSelectionAction.h"
#include "SoFCInteractiveElement.h"
#include "SoFCUnifiedSelection.h"
#include "SoFCVectorizeSVGAction.h"
#include "SoFCVectorizeU3DAction.h"
#include "SoAxisCrossKit.h"
#include "SoTextLabel.h"
#include "SoNavigationDragger.h"
#include "Inventor/SoDrawingGrid.h"
#include "Inventor/SoAutoZoomTranslation.h"
#include "Inventor/MarkerBitmaps.h"
#include "Inventor/SmSwitchboard.h"
#include "SoFCCSysDragger.h"
#include "propertyeditor/PropertyItem.h"
#include "NavigationStyle.h"
#include "Flag.h"
#include "SelectionObject.h"
using namespace Gui;
using namespace Gui::Inventor;
using namespace Gui::PropertyEditor;
static SbBool init_done = false;
static SoGroup *storage = nullptr;
SbBool Gui::SoFCDB::isInitialized(void)
{
return init_done;
}
void Gui::SoFCDB::init()
{
SoInteraction ::init();
RotTransDragger ::initClass();
SoGLRenderActionElement ::initClass();
SoFCInteractiveElement ::initClass();
SoGLWidgetElement ::initClass();
SoFCColorBarBase ::initClass();
SoFCColorBar ::initClass();
SoFCColorLegend ::initClass();
SoFCColorGradient ::initClass();
SoFCBackgroundGradient ::initClass();
SoFCBoundingBox ::initClass();
SoFCSelection ::initClass();
SoFCUnifiedSelection ::initClass();
SoFCHighlightAction ::initClass();
SoFCSelectionAction ::initClass();
SoFCDocumentAction ::initClass();
SoGLWidgetNode ::initClass();
SoGLVBOActivatedElement ::initClass();
SoFCEnableSelectionAction ::initClass();
SoFCEnableHighlightAction ::initClass();
SoFCSelectionColorAction ::initClass();
SoFCHighlightColorAction ::initClass();
SoFCDocumentObjectAction ::initClass();
SoGLSelectAction ::initClass();
SoVisibleFaceAction ::initClass();
SoUpdateVBOAction ::initClass();
SoBoxSelectionRenderAction ::initClass();
SoFCVectorizeSVGAction ::initClass();
SoFCVectorizeU3DAction ::initClass();
SoHighlightElementAction ::initClass();
SoSelectionElementAction ::initClass();
SoVRMLAction ::initClass();
SoSkipBoundingGroup ::initClass();
SoTextLabel ::initClass();
SoStringLabel ::initClass();
SoFrameLabel ::initClass();
TranslateManip ::initClass();
SoShapeScale ::initClass();
SoAxisCrossKit ::initClass();
SoRegPoint ::initClass();
SoDrawingGrid ::initClass();
SoAutoZoomTranslation ::initClass();
MarkerBitmaps ::initClass();
SoFCCSysDragger ::initClass();
SmSwitchboard ::initClass();
PropertyItem ::init();
PropertySeparatorItem ::init();
PropertyStringItem ::init();
PropertyFontItem ::init();
PropertyIntegerItem ::init();
PropertyIntegerConstraintItem ::init();
PropertyFloatItem ::init();
PropertyUnitItem ::init();
PropertyFloatConstraintItem ::init();
PropertyPrecisionItem ::init();
PropertyUnitConstraintItem ::init();
PropertyAngleItem ::init();
PropertyBoolItem ::init();
PropertyVectorItem ::init();
PropertyVectorDistanceItem ::init();
PropertyPositionItem ::init();
PropertyDirectionItem ::init();
PropertyMatrixItem ::init();
PropertyPlacementItem ::init();
PropertyEnumItem ::init();
PropertyStringListItem ::init();
PropertyFloatListItem ::init();
PropertyIntegerListItem ::init();
PropertyColorItem ::init();
PropertyMaterialItem ::init();
PropertyMaterialListItem ::init();
PropertyFileItem ::init();
PropertyPathItem ::init();
PropertyTransientFileItem ::init();
PropertyLinkItem ::init();
PropertyLinkListItem ::init();
NavigationStyle ::init();
UserNavigationStyle ::init();
InventorNavigationStyle ::init();
CADNavigationStyle ::init();
RevitNavigationStyle ::init();
BlenderNavigationStyle ::init();
MayaGestureNavigationStyle ::init();
TouchpadNavigationStyle ::init();
GestureNavigationStyle ::init();
OpenCascadeNavigationStyle ::init();
GLGraphicsItem ::init();
GLFlagWindow ::init();
SelectionObject ::init();
qRegisterMetaType<Base::Vector3f>("Base::Vector3f");
qRegisterMetaType<Base::Vector3d>("Base::Vector3d");
qRegisterMetaType<Base::Quantity>("Base::Quantity");
qRegisterMetaType<QList<Base::Quantity> >("Base::QuantityList");
init_done = true;
assert(!storage);
storage = new SoGroup();
storage->ref();
}
void Gui::SoFCDB::finish()
{
// Coin doesn't provide a mechanism to free static members of own data types.
// Hence, we need to define a static method e.g. 'finish()' for all new types
// to invoke the private member function 'atexit_cleanup()'.
SoFCColorBarBase ::finish();
SoFCColorBar ::finish();
SoFCColorLegend ::finish();
SoFCColorGradient ::finish();
SoFCBackgroundGradient ::finish();
SoFCBoundingBox ::finish();
SoFCSelection ::finish();
SoFCHighlightAction ::finish();
SoFCSelectionAction ::finish();
SoFCDocumentAction ::finish();
SoFCDocumentObjectAction ::finish();
SoFCEnableSelectionAction ::finish();
SoFCEnableHighlightAction ::finish();
SoFCSelectionColorAction ::finish();
SoUpdateVBOAction ::finish();
SoFCHighlightColorAction ::finish();
storage->unref();
storage = nullptr;
}
// buffer acrobatics for inventor ****************************************************
static char * buffer;
static size_t buffer_size = 0;
static std::string cReturnString;
static void *
buffer_realloc(void * bufptr, size_t size)
{
buffer = (char *)realloc(bufptr, size);
buffer_size = size;
return buffer;
}
const std::string& Gui::SoFCDB::writeNodesToString(SoNode * root)
{
SoOutput out;
buffer = (char *)malloc(1024);
buffer_size = 1024;
out.setBuffer(buffer, buffer_size, buffer_realloc);
if (root && root->getTypeId().isDerivedFrom(SoVRMLParent::getClassTypeId()))
out.setHeaderString("#VRML V2.0 utf8");
SoWriteAction wa(&out);
wa.apply(root);
cReturnString = buffer;
free(buffer);
return cReturnString;
}
SoNode* replaceSwitches(SoNodeList* children, SoGroup* parent)
{
if (!children) {
return parent;
}
for (int i=0; i<children->getLength(); i++) {
SoNode* node = (*children)[i];
if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
if (node->getTypeId().isDerivedFrom(SoSwitch::getClassTypeId())) {
SoSwitch* group = static_cast<SoSwitch*>(node);
int which = group->whichChild.getValue();
if (which == SO_SWITCH_NONE)
continue;
SoGroup* newParent = new SoGroup();
SoNodeList c;
if (which >= 0) {
c.append(group->getChild(which));
}
else {
// SO_SWITCH_INHERIT or SO_SWITCH_ALL
for (int i=0; i<group->getNumChildren(); i++)
c.append(group->getChild(i));
}
replaceSwitches(&c, newParent);
parent->addChild(newParent);
}
else {
SoGroup* newParent = static_cast<SoGroup*>(node->getTypeId().createInstance());
replaceSwitches(node->getChildren(), newParent);
parent->addChild(newParent);
}
}
else {
parent->addChild(node);
}
}
return parent;
}
SoNode* replaceSwitchesInSceneGraph(SoNode* node)
{
if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
return replaceSwitches(node->getChildren(), new SoSeparator);
}
return node;
}
bool Gui::SoFCDB::writeToVRML(SoNode* node, const char* filename, bool binary)
{
SoNode* noSwitches = replaceSwitchesInSceneGraph(node);
noSwitches->ref();
SoVRMLAction vrml2;
vrml2.setOverrideMode(true);
vrml2.apply(noSwitches);
SoToVRML2Action tovrml2;
tovrml2.apply(noSwitches);
SoVRMLGroup* vrmlRoot = tovrml2.getVRML2SceneGraph();
vrmlRoot->setInstancePrefix(SbString("o"));
vrmlRoot->ref();
std::string buffer = SoFCDB::writeNodesToString(vrmlRoot);
vrmlRoot->unref(); // release the memory as soon as possible
// restore old settings
vrml2.setOverrideMode(false);
vrml2.apply(noSwitches);
noSwitches->unref();
Base::FileInfo fi(filename);
if (binary) {
// We want to write compressed VRML but Coin 2.4.3 doesn't do it even though
// SoOutput::getAvailableCompressionMethods() delivers a string list that
// contains 'GZIP'. setCompression() was called directly after opening the file,
// returned true and no error message appeared but anyway it didn't work.
// Strange is that reading GZIPped VRML files works.
// So, we do the compression on our own.
Base::ofstream str(fi, std::ios::out | std::ios::binary);
zipios::GZIPOutputStream gzip(str);
if (gzip) {
gzip << buffer;
gzip.close();
return true;
}
}
else {
Base::ofstream str(fi, std::ios::out);
if (str) {
str << buffer;
str.close();
return true;
}
}
return false;
}
bool Gui::SoFCDB::writeToFile(SoNode* node, const char* filename, bool binary)
{
bool ret = false;
Base::FileInfo fi(filename);
// Write VRML V2.0
if (fi.hasExtension("wrl") || fi.hasExtension("vrml") || fi.hasExtension("wrz")) {
// If 'wrz' is set then force compression
if (fi.hasExtension("wrz"))
binary = true;
ret = SoFCDB::writeToVRML(node, filename, binary);
}
else if (fi.hasExtension("iv")) {
// Write Inventor in ASCII
std::string buffer = SoFCDB::writeNodesToString(node);
Base::ofstream str(Base::FileInfo(filename), std::ios::out);
if (str) {
str << buffer;
str.close();
ret = true;
}
}
return ret;
}
SoGroup* Gui::SoFCDB::getStorage()
{
assert(storage); //call init first.
return storage;
}