KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml2_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 <wx/log.h>
24
25#include "vrml2_base.h"
26#include "vrml2_switch.h"
28
29
35
36
38{
40 m_Parent = aParent;
41 whichChoice = -1;
42
43 if( nullptr != m_Parent )
44 m_Parent->AddChildNode( this );
45}
46
47
49{
50 wxLogTrace( traceVrmlPlugin,
51 wxT( " * [INFO] Destroying Switch node with %zu children, %zu"
52 "references, and %zu back pointers." ),
53 m_Children.size(), m_Refs.size(), m_BackPointers.size() );
54}
55
56
58{
59 // a Switch node is never dangling
60 return false;
61}
62
63
64bool WRL2SWITCH::Read( WRLPROC& proc, WRL2BASE* aTopNode )
65{
66 /*
67 * Structure of a Switch node (p.113):
68 *
69 * Switch {
70 * exposedField MFNode choice []
71 * exposedField SFInt32 whichChoice -1
72 * }
73 */
74
75 wxCHECK_MSG( aTopNode, false, wxT( "Invalid top node." ) );
76
77 char tok = proc.Peek();
78
79 if( proc.eof() )
80 {
81 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
82 " * [INFO] bad file format; unexpected eof %s." ),
83 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
84
85 return false;
86 }
87
88 if( '{' != tok )
89 {
90 wxLogTrace( traceVrmlPlugin,
91 wxT( "%s:%s:%d\n"
92 " * [INFO] bad file format; expecting '{' but got '%s' %s." ),
93 __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
94
95 return false;
96 }
97
98 proc.Pop();
99 std::string glob;
100
101 while( true )
102 {
103 if( proc.Peek() == '}' )
104 {
105 proc.Pop();
106 break;
107 }
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 // expecting one of:
119 // choice
120 // whichChoice
121 if( !glob.compare( "whichChoice" ) )
122 {
123 if( !proc.ReadSFInt( whichChoice ) )
124 {
125 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
126 " * [INFO] invalid whichChoice %s\n"
127 " * [INFO] file: '%s'\n"
128 "%s" ),
129 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
130 proc.GetFileName(), proc.GetError() );
131
132 return false;
133 }
134 }
135 else if( !glob.compare( "choice" ) )
136 {
137 if( !readChildren( proc, aTopNode ) )
138 return false;
139 }
140 else
141 {
142 wxLogTrace( traceVrmlPlugin,
143 wxT( "%s:%s:%d\n"
144 " * [INFO] invalid Switch %s.\n"
145 " * [INFO] file: '%s'\n" ),
146 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
147 proc.GetFileName() );
148
149 return false;
150 }
151 } // while( true ) -- reading contents of Switch{}
152
153 return true;
154}
155
156
158{
159 wxCHECK_MSG( aNode, false, wxT( "Invalid node." ) );
160
161 // take possession if the node is dangling WRL2_SHAPE
162 if( WRL2NODES::WRL2_SHAPE == aNode->GetNodeType() && aNode->isDangling() )
163 {
164 WRL2NODE* np = aNode->GetParent();
165
166 if( nullptr != np )
167 aNode->SetParent( this );
168
169 if( !WRL2NODE::AddChildNode( aNode ) )
170 {
171 aNode->SetParent( nullptr );
172 return false;
173 }
174 }
175
176 if( !WRL2NODE::AddRefNode( aNode ) )
177 return false;
178
179 return true;
180}
181
182
184{
185 char tok = proc.Peek();
186
187 if( proc.eof() )
188 {
189 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
190 " * [INFO] bad file format; unexpected eof %s." ),
191 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
192
193 return false;
194 }
195
196 WRL2NODE* child = nullptr;
197
198 if( '[' != tok )
199 {
200 // since there are no delimiters we expect a single child
201 if( !aTopNode->ReadNode( proc, this, &child ) )
202 return false;
203
204 if( nullptr != child )
205 choices.push_back( child );
206
207 if( proc.Peek() == ',' )
208 proc.Pop();
209
210 return true;
211 }
212
213 proc.Pop();
214
215 while( true )
216 {
217 if( proc.Peek() == ']' )
218 {
219 proc.Pop();
220 break;
221 }
222
223 if( !aTopNode->ReadNode( proc, this, &child ) )
224 return false;
225
226 if( nullptr != child )
227 choices.push_back( child );
228
229 if( proc.Peek() == ',' )
230 proc.Pop();
231
232 }
233
234 return true;
235}
236
237
239{
240 wxLogTrace( traceVrmlPlugin,
241 wxT( " * [INFO] Translating Switch with %zu children, %zu references, and"
242 "%zu back pointers." ),
243 m_Children.size(), m_Refs.size(), m_BackPointers.size() );
244
245 if( choices.empty() )
246 {
247 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Switch translation: no choices." ) );
248
249 return nullptr;
250 }
251
252 S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
253
254 wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_TRANSFORM ), nullptr,
255 wxString::Format( wxT( "Switch does not have a Transform parent (parent "
256 "ID: %d)." ), ptype ) );
257
259 {
260 wxLogTrace( traceVrmlPlugin,
261 wxT( " * [INFO] Switch translation: no choice (choices = %zu), "
262 "whichChoice = %d." ), choices.size(), whichChoice );
263
264 return nullptr;
265 }
266
267 WRL2NODES type = choices[whichChoice]->GetNodeType();
268
269 switch( type )
270 {
275 break;
276
277 default:
278 return nullptr;
279 }
280
281 return choices[whichChoice]->TranslateToSG( aParent );
282}
The base class of all Scene Graph nodes.
Definition sg_node.h:71
The top node of a VRML2 model.
Definition vrml2_base.h:56
bool ReadNode(WRLPROC &proc, WRL2NODE *aParent, WRL2NODE **aNode)
virtual bool isDangling(void)=0
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
std::list< WRL2NODE * > m_BackPointers
Definition vrml2_node.h:169
WRL2NODE * GetParent(void) const
virtual bool SetParent(WRL2NODE *aParent, bool doUnlink=true)
Set the parent WRL2NODE of this object.
WRL2NODE * m_Parent
Definition vrml2_node.h:165
std::list< WRL2NODE * > m_Children
Definition vrml2_node.h:170
WRL2NODES m_Type
Definition vrml2_node.h:166
virtual bool AddRefNode(WRL2NODE *aNode)
WRL2NODES GetNodeType(void) const
std::list< WRL2NODE * > m_Refs
Definition vrml2_node.h:171
virtual bool AddChildNode(WRL2NODE *aNode)
bool isDangling(void) override
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
bool Read(WRLPROC &proc, WRL2BASE *aTopNode) override
bool AddRefNode(WRL2NODE *aNode) override
bool readChildren(WRLPROC &proc, WRL2BASE *aTopNode)
virtual ~WRL2SWITCH()
std::vector< WRL2NODE * > choices
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
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
Definition ifsg_api.cpp:481
SGTYPES
Definition sg_types.h:32
@ SGTYPE_TRANSFORM
Definition sg_types.h:33
WRL2NODES
Definition wrltypes.h:121
@ WRL2_TRANSFORM
Definition wrltypes.h:174
@ WRL2_INLINE
Definition wrltypes.h:147
@ WRL2_SWITCH
Definition wrltypes.h:168