KiCad PCB EDA Suite
vrml1_separator.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 <cirilo.bernardo@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <iostream>
25 #include <sstream>
26 #include <wx/log.h>
27 
28 #include "vrml1_base.h"
29 #include "vrml1_separator.h"
30 #include "plugins/3dapi/ifsg_all.h"
31 
32 
33 WRL1SEPARATOR::WRL1SEPARATOR( NAMEREGISTER* aDictionary ) : WRL1NODE( aDictionary )
34 {
36  return;
37 }
38 
39 
41  WRL1NODE( aDictionary )
42 {
44  m_Parent = aParent;
45 
46  if( NULL != m_Parent )
47  m_Parent->AddChildNode( this );
48 
49  return;
50 }
51 
52 
54 {
55  #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 2 )
56  do {
57  std::ostringstream ostr;
58  ostr << " * [INFO] Destroying Separator with " << m_Children.size();
59  ostr << " children, " << m_Refs.size() << " references and ";
60  ostr << m_BackPointers.size() << " backpointers";
61  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
62  } while( 0 );
63  #endif
64 
65  return;
66 }
67 
68 
69 // functions inherited from WRL1NODE
70 bool WRL1SEPARATOR::Read( WRLPROC& proc, WRL1BASE* aTopNode )
71 {
72  if( NULL == aTopNode )
73  {
74  #ifdef DEBUG_VRML1
75  do {
76  std::ostringstream ostr;
77  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
78  ostr << " * [BUG] aTopNode is NULL";
79  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
80  } while( 0 );
81  #endif
82 
83  return false;
84  }
85 
86  size_t line, column;
87  proc.GetFilePosData( line, column );
88 
89  char tok = proc.Peek();
90 
91  if( proc.eof() )
92  {
93  #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
94  do {
95  std::ostringstream ostr;
96  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
97  ostr << " * [INFO] bad file format; unexpected eof at line ";
98  ostr << line << ", column " << column;
99  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
100  } while( 0 );
101  #endif
102 
103  return false;
104  }
105 
106  if( '{' != tok )
107  {
108  #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
109  do {
110  std::ostringstream ostr;
111  ostr << proc.GetError() << "\n";
112  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
113  ostr << " * [INFO] bad file format; expecting '{' but got '" << tok;
114  ostr << "' at line " << line << ", column " << column;
115  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
116  } while( 0 );
117  #endif
118 
119  return false;
120  }
121 
122  proc.Pop();
123 
124  while( true )
125  {
126  if( proc.Peek() == '}' )
127  {
128  proc.Pop();
129  break;
130  }
131 
132  proc.GetFilePosData( line, column );
133 
134  if( !aTopNode->ReadNode( proc, this, NULL ) )
135  {
136  #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
137  do {
138  std::ostringstream ostr;
139  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
140  ostr << " * [INFO] bad file format; unexpected eof at line ";
141  ostr << line << ", column " << column;
142  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
143  } while( 0 );
144  #endif
145 
146  return false;
147  }
148 
149  if( proc.Peek() == ',' )
150  proc.Pop();
151 
152  } // while( true ) -- reading contents of Separator{}
153 
154  return true;
155 }
156 
157 
159 {
160  #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 2 )
161  do {
162  std::ostringstream ostr;
163  ostr << " * [INFO] Translating Separator with " << m_Children.size();
164  ostr << " children, " << m_Refs.size() << " references and ";
165  ostr << m_BackPointers.size() << " backpointers (total ";
166  ostr << m_Items.size() << " items)";
167  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
168  } while( 0 );
169  #endif
170 
171  if( !m_Parent )
172  {
173  #ifdef DEBUG
174  do {
175  std::ostringstream ostr;
176  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
177  ostr << " * [BUG] Separator has no parent";
178  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
179  } while( 0 );
180  #endif
181 
182  return NULL;
183  }
184 
185  if( sp != NULL )
186  m_current = *sp;
187  else
188  m_current.Init();
189 
190  S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
191 
192  if( NULL != aParent && ptype != S3D::SGTYPE_TRANSFORM )
193  {
194  #ifdef DEBUG_VRML1
195  do {
196  std::ostringstream ostr;
197  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
198  ostr << " * [BUG] Separator does not have a Transform parent (parent ID: ";
199  ostr << ptype << ")";
200  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
201  } while( 0 );
202  #endif
203 
204  return NULL;
205  }
206 
207  IFSG_TRANSFORM txNode( aParent );
208  bool hasContent = false;
209 
210  std::list< WRL1NODE* >::iterator sI = m_Items.begin();
211  std::list< WRL1NODE* >::iterator eI = m_Items.end();
212 
213  SGNODE* node = txNode.GetRawPtr();
214 
215  while( sI != eI )
216  {
217  if( NULL != (*sI)->TranslateToSG( node, &m_current ) )
218  hasContent = true;
219 
220  ++sI;
221  }
222 
223  if( !hasContent )
224  {
225  txNode.Destroy();
226  return NULL;
227  }
228 
229  return node;
230 }
WRL1BASE represents the top node of a VRML1 model.
Definition: vrml1_base.h:45
void Pop(void)
Definition: wrlproc.cpp:2007
bool GetFilePosData(size_t &line, size_t &column)
Definition: wrlproc.cpp:1951
#define MASK_VRML
Definition: wrltypes.h:37
WRL1STATUS m_current
Definition: vrml1_node.h:128
virtual ~WRL1SEPARATOR()
WRL1SEPARATOR(NAMEREGISTER *aDictionary)
SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
collects header files for all SG* wrappers and the API
void Init()
Definition: vrml1_node.h:92
SGNODE * GetRawPtr(void) noexcept
Function GetRawPtr() returns the raw internal SGNODE pointer.
Definition: ifsg_node.cpp:66
std::list< WRL1NODE * > m_BackPointers
Definition: vrml1_node.h:122
#define NULL
std::list< WRL1NODE * > m_Refs
Definition: vrml1_node.h:124
bool ReadNode(WRLPROC &proc, WRL1NODE *aParent, WRL1NODE **aNode)
Definition: vrml1_base.cpp:314
WRL1NODE represents the base class of all VRML1 nodes.
Definition: vrml1_node.h:111
char Peek(void)
Definition: wrlproc.cpp:1979
virtual bool AddChildNode(WRL1NODE *aNode)
Definition: vrml1_node.cpp:442
SGTYPES
Definition: sg_types.h:34
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
Definition: ifsg_api.cpp:627
WRL1NODES m_Type
Definition: vrml1_node.h:119
SGNODE * TranslateToSG(SGNODE *aParent, WRL1STATUS *sp) override
Function TranslateToSG produces a representation of the data using the intermediate scenegraph struct...
std::list< WRL1NODE * > m_Items
Definition: vrml1_node.h:125
IFSG_TRANSFORM is the wrapper for the VRML compatible TRANSFORM block class SCENEGRAPH.
WRL1NODE * m_Parent
Definition: vrml1_node.h:118
std::string GetError(void)
Definition: wrlproc.cpp:1945
void Destroy(void)
Function Destroy deletes the object held by this wrapper.
Definition: ifsg_node.cpp:54
bool eof(void)
Definition: wrlproc.cpp:1939
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
std::list< WRL1NODE * > m_Children
Definition: vrml1_node.h:123