KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml1_switch.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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <iostream>
22#include <sstream>
23#include <iterator>
24#include <wx/log.h>
25
26#include "vrml1_base.h"
27#include "vrml1_switch.h"
29
30
31WRL1SWITCH::WRL1SWITCH( NAMEREGISTER* aDictionary ) : WRL1NODE( aDictionary )
32{
34 whichChild = -1;
35}
36
37
38WRL1SWITCH::WRL1SWITCH( NAMEREGISTER* aDictionary, WRL1NODE* aParent ) :
39 WRL1NODE( aDictionary )
40{
42 m_Parent = aParent;
43 whichChild = -1;
44
45 if( nullptr != m_Parent )
46 m_Parent->AddChildNode( this );
47}
48
49
51{
52 wxLogTrace( traceVrmlPlugin,
53 wxT( " * [INFO] Destroying Switch node with %zu children, %zu"
54 "references, and %zu back pointers." ),
55 m_Children.size(), m_Refs.size(), m_BackPointers.size() );
56}
57
58
59bool WRL1SWITCH::Read( WRLPROC& proc, WRL1BASE* aTopNode )
60{
61 /*
62 * Structure of a Switch node:
63 *
64 * Switch {
65 * exposedField SFInt32 whichChild -1
66 * children
67 * }
68 */
69
70 wxCHECK_MSG( aTopNode, false, wxT( "Invalid top node." ) );
71
72 char tok = proc.Peek();
73
74 if( proc.eof() )
75 {
76 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
77 " * [INFO] bad file format; unexpected eof %s." ),
78 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
79
80 return false;
81 }
82
83 if( '{' != tok )
84 {
85 wxLogTrace( traceVrmlPlugin,
86 wxT( "%s:%s:%d\n"
87 " * [INFO] bad file format; expecting '{' but got '%s' %s.\n"
88 "%s" ),
89 __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition(),
90 proc.GetError() );
91
92 return false;
93 }
94
95 proc.Pop();
96 std::string glob;
97
98 while( true )
99 {
100 char pchar = proc.Peek();
101
102 if( pchar == '}' )
103 {
104 proc.Pop();
105 break;
106 }
107 else if ( pchar == 'w' )
108 {
109 if( !proc.ReadName( glob ) )
110 {
111 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
112 "%s" ),
113 __FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
114
115 return false;
116 }
117
118 if( !glob.compare( "whichChild" ) )
119 {
120 if( !proc.ReadSFInt( whichChild ) )
121 {
122 wxLogTrace( traceVrmlPlugin,
123 wxT( "%s:%s:%d"
124 " * [INFO] invalid whichChild %s (invalid value '%s')\n"
125 " * [INFO] file: '%s'\n"
126 "%s" ),
127 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(), glob,
128 proc.GetFileName(), proc.GetError() );
129
130 return false;
131 }
132
133 continue;
134 }
135
136 wxLogTrace( traceVrmlPlugin,
137 wxT( "%s:%s:%d\n"
138 " * [INFO] invalid Switch %s (unexpected 'whichChild')\n"
139 " * [INFO] file: '%s'" ),
140 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
141 proc.GetFileName() );
142
143 return false;
144 }
145
146 if( !aTopNode->ReadNode( proc, this, nullptr ) )
147 {
148 wxLogTrace( traceVrmlPlugin,
149 wxT( "%s:%s:%d\n"
150 " * [INFO] bad file format; unexpected eof %s."),
151 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
152
153 return false;
154 }
155
156 if( proc.Peek() == ',' )
157 proc.Pop();
158
159 } // while( true ) -- reading contents of Switch{}
160
161 return true;
162}
163
164
166{
167 wxLogTrace( traceVrmlPlugin,
168 wxT( " * [INFO] Translating Switch node with %zu children, %zu"
169 "references, and %zu back pointers (%zu total items)." ),
170 m_Children.size(), m_Refs.size(), m_BackPointers.size(), m_Items.size() );
171
172 if( m_Items.empty() )
173 return nullptr;
174
175 if( whichChild < 0 || whichChild >= (int)m_Items.size() )
176 return nullptr;
177
178 if( sp == nullptr )
179 {
180 m_current.Init();
181 sp = &m_current;
182 }
183
184 std::list< WRL1NODE* >::iterator ip = m_Items.begin();
185 std::advance( ip, whichChild );
186
187 IFSG_TRANSFORM txNode( aParent );
188
189 SGNODE* np = (*ip)->TranslateToSG( aParent, sp );
190
191 return np;
192}
The wrapper for the VRML compatible TRANSFORM block class SCENEGRAPH.
The base class of all Scene Graph nodes.
Definition sg_node.h:71
Represent the top node of a VRML1 model.
Definition vrml1_base.h:42
bool ReadNode(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
WRL1NODE(NAMEREGISTER *aDictionary)
WRL1NODES m_Type
Definition vrml1_node.h:223
std::list< WRL1NODE * > m_Items
Definition vrml1_node.h:229
WRL1STATUS m_current
Definition vrml1_node.h:232
std::list< WRL1NODE * > m_BackPointers
Definition vrml1_node.h:226
std::list< WRL1NODE * > m_Children
Definition vrml1_node.h:227
WRL1NODE * m_Parent
Definition vrml1_node.h:222
std::list< WRL1NODE * > m_Refs
Definition vrml1_node.h:228
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
WRL1SWITCH(NAMEREGISTER *aDictionary)
SGNODE * TranslateToSG(SGNODE *aParent, WRL1STATUS *sp) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
virtual ~WRL1SWITCH()
bool ReadSFInt(int &aSFInt32)
Definition wrlproc.cpp:863
void Pop(void)
Definition wrlproc.cpp:2031
char Peek(void)
Definition wrlproc.cpp:2003
std::string GetFileName(void)
Definition wrlproc.cpp:1991
std::string GetError(void)
Definition wrlproc.cpp:1956
bool eof(void)
Definition wrlproc.cpp:1950
bool ReadName(std::string &aName)
Definition wrlproc.cpp:285
std::string GetFilePosition() const
Definition wrlproc.cpp:1978
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition vrml.cpp:59
collects header files for all SG* wrappers and the API
@ WRL1_SWITCH
Definition wrltypes.h:82