KiCad PCB EDA Suite
vrml2_inline.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) 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 
26 #include <iostream>
27 #include <sstream>
28 #include <wx/log.h>
29 
30 #include "vrml2_inline.h"
31 #include "vrml2_base.h"
32 #include "plugins/3dapi/ifsg_all.h"
33 
34 
36 {
37  m_VRML2Base = nullptr;
39  m_Parent = nullptr;
40 }
41 
42 
44 {
45  m_VRML2Base = nullptr;
47  m_Parent = aParent;
48 
49  if( nullptr != m_Parent )
50  m_Parent->AddChildNode( this );
51 }
52 
53 
55 {
56  wxLogTrace( traceVrmlPlugin, " * [INFO] Destroying Inline node." );
57 }
58 
59 
61 {
62  // this node is never dangling
63  return false;
64 }
65 
66 
67 bool WRL2INLINE::Read( WRLPROC& proc, WRL2BASE* aTopNode )
68 {
69  if( aTopNode == nullptr || aTopNode->GetNodeType() != WRL2NODES::WRL2_BASE )
70  return false;
71 
72  m_VRML2Base = aTopNode;
73  char tok = proc.Peek();
74 
75  if( proc.eof() )
76  {
77  wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
78  " * [INFO] bad file format; unexpected eof %s." ),
79  __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
80 
81  return false;
82  }
83 
84  if( '{' != tok )
85  {
86  wxLogTrace( traceVrmlPlugin,
87  wxT( "%s:%s:%d\n"
88  " * [INFO] bad file format; expecting '{' but got '%s' %s." ),
89  __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
90 
91  return false;
92  }
93 
94  proc.Pop();
95 
96  while( ( tok = proc.Peek() ) )
97  {
98  std::string glob;
99 
100  if( tok == '}' )
101  {
102  proc.Pop();
103  return true;
104  }
105 
106  if( !proc.ReadName( glob ) )
107  {
108  wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
109  "%s" ),
110  __FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
111 
112  return false;
113  }
114 
115  // expecting one of 'url', 'bboxCenter', 'bboxSize'
116  if( !glob.compare( "url" ) )
117  {
118  if( !proc.ReadMFString( url ) )
119  {
120  wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
121  " * [INFO] invalid url %s\n"
122  " * [INFO] file: '%s'\n"
123  "%s" ),
124  __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
125  proc.GetFileName(), proc.GetError() );
126 
127  return false;
128  }
129  }
130  else if( !glob.compare( "bboxCenter" ) )
131  {
132  if( !proc.ReadSFVec3f( bboxCenter ) )
133  {
134  wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
135  " * [INFO] invalid bboxCenter %s\n"
136  " * [INFO] file: '%s'\n"
137  "%s" ),
138  __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
139  proc.GetFileName(), proc.GetError() );
140 
141  return false;
142  }
143  }
144  else if( !glob.compare( "bboxSize" ) )
145  {
146  if( !proc.ReadSFVec3f( bboxSize ) )
147  {
148  wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
149  " * [INFO] invalid bboxSize %s\n"
150  " * [INFO] file: '%s'\n"
151  "%s" ),
152  __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
153  proc.GetFileName(), proc.GetError() );
154 
155  return false;
156  }
157  }
158  else
159  {
160  wxLogTrace( traceVrmlPlugin,
161  wxT( "%s:%s:%d\n"
162  " * [INFO] invalid Inline %s\n"
163  " * [INFO] file: '%s'\n" ),
164  __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
165  proc.GetFileName() );
166 
167  return false;
168  }
169  }
170 
171  wxLogTrace( traceVrmlPlugin,
172  wxT( "%s:%s:%d\n"
173  " * [INFO] invalid Inline %s (no closing brace)\n"
174  " * [INFO] file: '%s'\n" ),
175  __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
176  proc.GetFileName() );
177 
178  return false;
179 }
180 
181 
183 {
184  // this node may not own or reference any other node
185  wxCHECK_MSG( false, false, wxT( "AddRefNode is not applicable." ) );
186 }
187 
188 
190 {
191  // this node may not own or reference any other node
192  wxCHECK_MSG( false, false, wxT( "AddChildNode is not applicable." ) );
193 
194  return false;
195 }
196 
197 
199 {
200  if( nullptr == aParent || nullptr == m_VRML2Base )
201  return nullptr;
202 
203  if( url.empty() )
204  return nullptr;
205 
206  S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
207 
208  wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_TRANSFORM ), nullptr,
209  wxString::Format( wxT( " * [BUG] Appearance does not have a Transform parent "
210  "(parent ID: %d)." ), ptype ) );
211 
212  SGNODE* np = m_VRML2Base->GetInlineData( url.front() );
213 
214  if( nullptr == np )
215  return nullptr;
216 
217  bool OK = false;
218 
219  if( nullptr == S3D::GetSGNodeParent( np ) )
220  OK = S3D::AddSGNodeChild( aParent, np );
221  else
222  OK = S3D::AddSGNodeRef( aParent, np );
223 
224  if( !OK )
225  return nullptr;
226 
227  return np;
228 }
virtual ~WRL2INLINE()
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
void Pop(void)
Definition: wrlproc.cpp:2035
bool ReadSFVec3f(WRLVEC3F &aSFVec3f)
Definition: wrlproc.cpp:1082
bool AddRefNode(WRL2NODE *aNode) override
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
Definition: ifsg_api.cpp:492
WRL2NODES m_Type
Definition: vrml2_node.h:170
The base class of all Scene Graph nodes.
Definition: sg_node.h:74
collects header files for all SG* wrappers and the API
SGLIB_API bool AddSGNodeRef(SGNODE *aParent, SGNODE *aChild)
Definition: ifsg_api.cpp:501
WRLVEC3F bboxSize
Definition: vrml2_inline.h:56
SGNODE * GetInlineData(const std::string &aName)
Definition: vrml2_base.cpp:97
bool isDangling(void) override
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
std::vector< std::string > url
Definition: vrml2_inline.h:54
WRL2NODE * m_Parent
Definition: vrml2_node.h:169
SGLIB_API bool AddSGNodeChild(SGNODE *aParent, SGNODE *aChild)
Definition: ifsg_api.cpp:510
std::string GetFilePosition() const
Definition: wrlproc.cpp:1982
WRL2NODES GetNodeType(void) const
Definition: vrml2_node.cpp:204
char Peek(void)
Definition: wrlproc.cpp:2007
std::string GetFileName(void)
Definition: wrlproc.cpp:1995
The top node of a VRML2 model.
Definition: vrml2_base.h:59
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition: vrml.cpp:63
SGTYPES
Definition: sg_types.h:34
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
Definition: ifsg_api.cpp:483
virtual bool AddChildNode(WRL2NODE *aNode)
Definition: vrml2_node.cpp:356
bool ReadName(std::string &aName)
Definition: wrlproc.cpp:289
WRL2BASE * m_VRML2Base
Definition: vrml2_inline.h:53
WRLVEC3F bboxCenter
Definition: vrml2_inline.h:55
bool Read(WRLPROC &proc, WRL2BASE *aTopNode) override
std::string GetError(void)
Definition: wrlproc.cpp:1960
bool eof(void)
Definition: wrlproc.cpp:1954
bool ReadMFString(std::vector< std::string > &aMFString)
Definition: wrlproc.cpp:1162
bool AddChildNode(WRL2NODE *aNode) override