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