KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml1_base.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015-2016 Cirilo Bernardo <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <iostream>
26#include <sstream>
27#include <wx/log.h>
28
29#include "vrml1_base.h"
30#include "vrml1_group.h"
31#include "vrml1_separator.h"
32#include "vrml1_material.h"
33#include "vrml1_matbinding.h"
34#include "vrml1_coords.h"
35#include "vrml1_switch.h"
36#include "vrml1_faceset.h"
37#include "vrml1_transform.h"
38#include "vrml1_shapehints.h"
40
41
43{
46 return;
47}
48
49
51{
52 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying virtual base node." ) );
53
54 cancelDict();
55}
56
57
58bool WRL1BASE::SetParent( WRL1NODE* aParent, bool /* doUnlink */ )
59{
60 wxCHECK_MSG( false, false, wxT( "Attempt to set parent on WRL1BASE node." ) );
61}
62
63
64std::string WRL1BASE::GetName( void )
65{
66 wxCHECK_MSG( false, std::string( "" ),
67 wxT( "Attempt to extract name from virtual base node." ) );
68}
69
70
71bool WRL1BASE::SetName( const std::string& aName )
72{
73 wxCHECK_MSG( false, false, wxT( "Attempt to set name on virtual base node." ) );
74}
75
76
78{
79 wxCHECK_MSG( proc.GetVRMLType() == WRLVERSION::VRML_V1, false,
80 wxT( "No open file or file is not a VRML1 file" ) );
81
82 // Note: according to the VRML1 specification, a file may contain
83 // only one grouping node at the top level. The following code
84 // supports non-conformant VRML1 files by processing all top level
85 // nodes as if the vrml1_base were the equivalent of a vrml1_separator
86
87 while( proc.Peek() )
88 {
89 if( !ReadNode( proc, this, nullptr ) )
90 {
91 wxLogTrace( traceVrmlPlugin,
92 wxT( "%s:%s:%d\n * [INFO] bad file format; unexpected eof %s" ),
93 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
94
95 return false;
96 }
97 }
98
99 if( !proc.eof() )
100 {
101 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n%s" ),
102 __FILE__, __FUNCTION__, __LINE__, proc.GetError() );
103
104 return false;
105 }
106
107 return true;
108}
109
110
111bool WRL1BASE::implementUse( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
112{
113 if( nullptr != aNode )
114 *aNode = nullptr;
115
116 wxCHECK_MSG( aParent, false, wxT( "Invoked with invalid parent." ) );
117
118 std::string glob;
119
120 if( !proc.ReadName( glob ) )
121 {
122 wxLogTrace( traceVrmlPlugin, wxT( "%s" ), proc.GetError() );
123
124 return false;
125 }
126
127 WRL1NODE* ref = aParent->FindNode( glob );
128
129 // return 'true' - the file may be defective but it may still be somewhat OK
130 if( nullptr == ref )
131 {
132 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n * [INFO] node '%s' not found." ),
133 __FILE__, __FUNCTION__, __LINE__, glob );
134
135 return true;
136 }
137
138 if( !aParent->AddRefNode( ref ) )
139 {
140 wxLogTrace( traceVrmlPlugin,
141 wxT( "%s:%s:%d\n * [INFO] failed to add node '%s' (%s) to parent of type %s." ),
142 __FILE__, __FUNCTION__, __LINE__, glob,
143 ref->GetNodeTypeName( ref->GetNodeType() ),
144 aParent->GetNodeTypeName( aParent->GetNodeType() ) );
145
146 return false;
147 }
148
149 if( nullptr != aNode )
150 *aNode = ref;
151
152 return true;
153}
154
155
156bool WRL1BASE::implementDef( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
157{
158 if( nullptr != aNode )
159 *aNode = nullptr;
160
161 wxCHECK_MSG( nullptr != aParent, false, wxT( "Invalid parent pointer." ) );
162
163 std::string glob;
164 WRL1NODE* lnode = nullptr;
165
166 if( !proc.ReadName( glob ) )
167 {
168 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n%s" ),
169 __FILE__, __FUNCTION__, __LINE__, proc.GetError() );
170
171 return false;
172 }
173
174 if( ReadNode( proc, aParent, &lnode ) )
175 {
176 if( nullptr != aNode )
177 *aNode = lnode;
178
179 if( lnode && !lnode->SetName( glob ) )
180 {
181 wxLogTrace( traceVrmlPlugin,
182 wxT( "%s:%s:%d\n * [INFO] bad formatting (invalid name) %s." ),
183 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
184
185 return false;
186 }
187
188 if( !m_dictionary )
189 return false;
190
191 m_dictionary->AddName( glob, lnode );
192
193 return true;
194 }
195
196 return false;
197}
198
199
200bool WRL1BASE::ReadNode( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
201{
202 // This function reads a node and stores a pointer to it in aNode.
203 // A value 'true' is returned if a node is successfully read or,
204 // if the node is not supported, successfully discarded. Callers
205 // must always check the value of aNode when the function returns
206 // 'true' since it will be NULL if the node type is not supported.
207
208 if( nullptr != aNode )
209 *aNode = nullptr;
210
211 wxCHECK_MSG( aParent, false, wxT( "Invalid parent pointer." ) );
212
213 std::string glob;
214 WRL1NODES ntype;
215
216 if( !proc.ReadName( glob ) )
217 {
218 if( !proc.eof() )
219 {
220 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n%s" ),
221 __FILE__, __FUNCTION__, __LINE__, proc.GetError() );
222 }
223
224 return false;
225 }
226
227 // Process node name:
228 // the names encountered at this point should be one of the
229 // built-in node names or one of:
230 // DEF, USE
231 if( !glob.compare( "USE" ) )
232 {
233 if( !implementUse( proc, aParent, aNode ) )
234 return false;
235
236 return true;
237 }
238
239 if( !glob.compare( "DEF" ) )
240 {
241 if( !implementDef( proc, aParent, aNode ) )
242 return false;
243
244 return true;
245 }
246
247 ntype = getNodeTypeID( glob );
248
249 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Processing node '%s' ID: %d." ), glob, ntype );
250
251 switch( ntype )
252 {
254 if( !readGroup( proc, aParent, aNode ) )
255 return false;
256
257 break;
258
260 if( !readSeparator( proc, aParent, aNode ) )
261 return false;
262
263 break;
264
266 if( !readSwitch( proc, aParent, aNode ) )
267 return false;
268
269 break;
270
272 if( !readMaterial( proc, aParent, aNode ) )
273 return false;
274
275 break;
276
278 if( !readMatBinding( proc, aParent, aNode ) )
279 return false;
280
281 break;
282
284 if( !readCoords( proc, aParent, aNode ) )
285 return false;
286
287 break;
288
290 if( !readFaceSet( proc, aParent, aNode ) )
291 return false;
292
293 break;
294
299 if( !readTransform( proc, aParent, aNode ) )
300 return false;
301
302 break;
303
305 if( !readShapeHints( proc, aParent, aNode ) )
306 return false;
307
308 break;
309
310 //
311 // items not implemented or for optional future implementation:
312 //
313 default:
314 if( !proc.DiscardNode() )
315 {
316 wxLogTrace( traceVrmlPlugin,
317 wxT( "%s:%s:%d\n * [INFO] could not discard node %s." ),
318 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
319
320 return false;
321 }
322 else
323 {
324 wxLogTrace( traceVrmlPlugin,
325 wxT( " * [INFO] discarded node '%s' %s (currently unsupported)." ),
326 glob, proc.GetFilePosition() );
327 }
328
329 break;
330 }
331
332 return true;
333}
334
335
336bool WRL1BASE::Read( WRLPROC& proc, WRL1BASE* aTopNode )
337{
338 wxCHECK_MSG( false, false, wxT( "This method must never be invoked on a WRL1BASE object" ) );
339}
340
341
342bool WRL1BASE::readGroup( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
343{
344 if( nullptr != aNode )
345 *aNode = nullptr;
346
347 WRL1GROUP* np = new WRL1GROUP( m_dictionary, aParent );
348
349 if( !np->Read( proc, this ) )
350 {
351 delete np;
352 return false;
353 }
354
355 if( nullptr != aNode )
356 *aNode = (WRL1NODE*) np;
357
358 return true;
359}
360
361
362bool WRL1BASE::readSeparator( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
363{
364 if( nullptr != aNode )
365 *aNode = nullptr;
366
367 WRL1SEPARATOR* np = new WRL1SEPARATOR( m_dictionary, aParent );
368
369 if( !np->Read( proc, this ) )
370 {
371 delete np;
372 return false;
373 }
374
375 if( nullptr != aNode )
376 *aNode = (WRL1NODE*) np;
377
378 return true;
379}
380
381
382bool WRL1BASE::readSwitch( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
383{
384 WRL1SWITCH* np = new WRL1SWITCH( m_dictionary, aParent );
385
386 if( !np->Read( proc, this ) )
387 {
388 delete np;
389 return false;
390 }
391
392 if( nullptr != aNode )
393 *aNode = (WRL1NODE*) np;
394
395 return true;
396}
397
398
399bool WRL1BASE::readMaterial( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
400{
401 if( nullptr != aNode )
402 *aNode = nullptr;
403
404 WRL1MATERIAL* np = new WRL1MATERIAL( m_dictionary, aParent );
405
406 if( !np->Read( proc, this ) )
407 {
408 delete np;
409 return false;
410 }
411
412 if( nullptr != aNode )
413 *aNode = (WRL1NODE*) np;
414
415 return true;
416}
417
418
419bool WRL1BASE::readMatBinding( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
420{
421 if( nullptr != aNode )
422 *aNode = nullptr;
423
424 WRL1MATBINDING* np = new WRL1MATBINDING( m_dictionary, aParent );
425
426 if( !np->Read( proc, this ) )
427 {
428 delete np;
429 return false;
430 }
431
432 if( nullptr != aNode )
433 *aNode = (WRL1NODE*) np;
434
435 return true;
436}
437
438
439bool WRL1BASE::readCoords( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
440{
441 if( nullptr != aNode )
442 *aNode = nullptr;
443
444 WRL1COORDS* np = new WRL1COORDS( m_dictionary, aParent );
445
446 if( !np->Read( proc, this ) )
447 {
448 delete np;
449 return false;
450 }
451
452 if( nullptr != aNode )
453 *aNode = (WRL1NODE*) np;
454
455 return true;
456}
457
458
459bool WRL1BASE::readFaceSet( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
460{
461 if( nullptr != aNode )
462 *aNode = nullptr;
463
464 WRL1FACESET* np = new WRL1FACESET( m_dictionary, aParent );
465
466 if( !np->Read( proc, this ) )
467 {
468 delete np;
469 return false;
470 }
471
472 if( nullptr != aNode )
473 *aNode = (WRL1NODE*) np;
474
475 return true;
476}
477
478
479bool WRL1BASE::readTransform( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
480{
481 if( nullptr != aNode )
482 *aNode = nullptr;
483
484 WRL1TRANSFORM* np = new WRL1TRANSFORM( m_dictionary, aParent );
485
486 if( !np->Read( proc, this ) )
487 {
488 delete np;
489 return false;
490 }
491
492 if( nullptr != aNode )
493 *aNode = (WRL1NODE*) np;
494
495 return true;
496}
497
498
499bool WRL1BASE::readShapeHints( WRLPROC& proc, WRL1NODE* aParent, WRL1NODE** aNode )
500{
501 if( nullptr != aNode )
502 *aNode = nullptr;
503
504 WRL1SHAPEHINTS* np = new WRL1SHAPEHINTS( m_dictionary, aParent );
505
506 if( !np->Read( proc, this ) )
507 {
508 delete np;
509 return false;
510 }
511
512 if( nullptr != aNode )
513 *aNode = (WRL1NODE*) np;
514
515 return true;
516}
517
518
520{
521 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Translating VRML1 Base with %zu items." ),
522 m_Items.size() );
523
524 if( m_Items.empty() )
525 return nullptr;
526
527 if( m_Items.size() == 1 )
528 return ( *m_Items.begin() )->TranslateToSG( nullptr, nullptr );
529
530 // Note: according to the VRML1 specification, a file may contain
531 // only one grouping node at the top level. The following code
532 // supports non-conformant VRML1 files.
533
534 m_current.Init();
535
536 IFSG_TRANSFORM txNode( true );
537 bool hasContent = false;
538
539 std::list< WRL1NODE* >::iterator sI = m_Items.begin();
540 std::list< WRL1NODE* >::iterator eI = m_Items.end();
541
542 SGNODE* node = txNode.GetRawPtr();
543
544 while( sI != eI )
545 {
546 if( nullptr != (*sI)->TranslateToSG( node, &m_current ) )
547 hasContent = true;
548
549 ++sI;
550 }
551
552 if( !hasContent )
553 {
554 txNode.Destroy();
555 return nullptr;
556 }
557
558 return node;
559}
SGNODE * GetRawPtr(void) noexcept
Return the raw internal SGNODE pointer.
Definition ifsg_node.cpp:65
void Destroy(void)
Delete the object held by this wrapper.
Definition ifsg_node.cpp:55
The wrapper for the VRML compatible TRANSFORM block class SCENEGRAPH.
The base class of all Scene Graph nodes.
Definition sg_node.h:75
bool readMaterial(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool implementUse(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool readMatBinding(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool readFaceSet(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool readCoords(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool ReadNode(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool readTransform(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool readShapeHints(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
virtual std::string GetName(void) override
bool SetParent(WRL1NODE *aParent, bool doUnlink=true) override
Set the parent WRL1NODE of this object.
bool readGroup(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool readSwitch(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
SGNODE * TranslateToSG(SGNODE *aParent, WRL1STATUS *sp) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
virtual bool SetName(const std::string &aName) override
virtual ~WRL1BASE()
bool implementDef(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool Read(WRLPROC &proc)
bool readSeparator(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
WRL1NODE(NAMEREGISTER *aDictionary)
WRL1NODES m_Type
Definition vrml1_node.h:227
WRL1NODES getNodeTypeID(const std::string &aNodeName)
Return the ID based on the given aNodeName or WRL1_INVALID (WRL1_END) if no such node name exists.
std::list< WRL1NODE * > m_Items
Definition vrml1_node.h:233
NAMEREGISTER * m_dictionary
Definition vrml1_node.h:246
virtual WRL1NODE * FindNode(const std::string &aNodeName)
Search the tree of linked nodes and returns a reference to the current node with the given name.
void cancelDict(void)
WRL1STATUS m_current
Definition vrml1_node.h:236
virtual bool AddRefNode(WRL1NODE *aNode)
virtual bool SetName(const std::string &aName)
const char * GetNodeTypeName(WRL1NODES aNodeType) const
WRL1NODES GetNodeType(void) const
Return the type of this node instance.
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
char Peek(void)
Definition wrlproc.cpp:2007
WRLVERSION GetVRMLType(void)
Definition wrlproc.cpp:230
std::string GetError(void)
Definition wrlproc.cpp:1960
bool eof(void)
Definition wrlproc.cpp:1954
bool ReadName(std::string &aName)
Definition wrlproc.cpp:289
std::string GetFilePosition() const
Definition wrlproc.cpp:1982
bool DiscardNode(void)
Definition wrlproc.cpp:368
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition vrml.cpp:63
collects header files for all SG* wrappers and the API
WRL1NODES
Definition wrltypes.h:56
@ WRL1_SEPARATOR
Definition wrltypes.h:82
@ WRL1_INDEXEDFACESET
Definition wrltypes.h:67
@ WRL1_COORDINATE3
Definition wrltypes.h:61
@ WRL1_TRANSLATION
Definition wrltypes.h:91
@ WRL1_GROUP
Definition wrltypes.h:66
@ WRL1_BASE
Definition wrltypes.h:57
@ WRL1_TRANSFORM
Definition wrltypes.h:90
@ WRL1_MATERIALBINDING
Definition wrltypes.h:72
@ WRL1_MATERIAL
Definition wrltypes.h:71
@ WRL1_SHAPEHINTS
Definition wrltypes.h:83
@ WRL1_SWITCH
Definition wrltypes.h:86
@ WRL1_ROTATION
Definition wrltypes.h:80
@ WRL1_SCALE
Definition wrltypes.h:81