KiCad PCB EDA Suite
Loading...
Searching...
No Matches
x3d.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) 2013 Tuomas Vaherkoski <[email protected]>
6 * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26/*
27 * Description:
28 * This plugin implements the legacy KiCad X3D parser.
29 * Due to the rare use of X3D models, this plugin is a simple
30 * reimplementation of the legacy x3dmodelparser.cpp and is not
31 * intended to be a compliant X3D implementation.
32 */
33
34#include <vector>
35#include <wx/tokenzr.h>
36#include <wx/wfstream.h>
37#include <wx/xml/xml.h>
38#include <iostream>
39
40#include "x3d.h"
41#include "x3d_ops.h"
42#include "x3d_transform.h"
44
45
46SCENEGRAPH* X3DPARSER::Load( const wxString& aFileName )
47{
48 wxFFileInputStream stream( aFileName );
49 wxXmlDocument doc;
50
51 if( !stream.IsOk() || !doc.Load( stream ) )
52 return nullptr;
53
54 if( doc.GetRoot()->GetName() != wxT( "X3D" ) )
55 return nullptr;
56
57 NODE_LIST children; // VRML Grouping Nodes at top level
58
59 if( !getGroupingNodes( doc.GetRoot(), children ) )
60 return nullptr;
61
62 X3D_DICT dictionary; // dictionary for USE/DEF implementation
63 X3DNODE* topNode = new X3DTRANSFORM;
64 bool ok = false;
65
66 for( NODE_LIST::iterator node_it = children.begin(); node_it != children.end(); ++node_it )
67 {
68 wxXmlNode* node = *node_it;
69 wxString name = node->GetName();
70
71 if( name == wxT( "Transform" ) || name == wxT( "Group" ) )
72 {
73 // Read a Transform / Group
74 ok |= X3D::ReadTransform( node, topNode, dictionary );
75 }
76 else if( name == wxT( "Switch" ) )
77 {
78 ok |= X3D::ReadSwitch( node, topNode, dictionary );
79 }
80 }
81
82 SCENEGRAPH* sp = nullptr;
83
84 if( ok )
85 sp = (SCENEGRAPH*) topNode->TranslateToSG( nullptr );
86
87 delete topNode;
88 return sp;
89}
90
91
92bool X3DPARSER::getGroupingNodes( wxXmlNode* aNode, std::vector<wxXmlNode*>& aResult )
93{
94 aResult.clear();
95 wxXmlNode* scene = nullptr;
96
97 for( wxXmlNode* child = aNode->GetChildren(); child != nullptr; child = child->GetNext() )
98 {
99 if( child->GetName() == wxT( "Scene" ) )
100 {
101 scene = child;
102 break;
103 }
104 }
105
106 if( nullptr == scene )
107 return false;
108
109 for( wxXmlNode* child = scene->GetChildren(); child != nullptr; child = child->GetNext() )
110 {
111 const wxString& name = child->GetName();
112
113 if( name == wxT( "Transform" ) || name == wxT( "Switch" ) || name == wxT( "Group" ) )
114 aResult.push_back( child );
115 }
116
117 if( aResult.empty() )
118 return false;
119
120 return true;
121}
const char * name
Definition: DXF_plotter.cpp:57
Define the basic data set required to represent a 3D model.
Definition: scenegraph.h:45
The base class of all X3D nodes.
Definition: x3d_base.h:75
virtual SGNODE * TranslateToSG(SGNODE *aParent)=0
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
bool getGroupingNodes(wxXmlNode *aNode, std::vector< wxXmlNode * > &aResult)
Retrieve all permissible top-level nodes in an X3D/VRML file.
Definition: x3d.cpp:92
SCENEGRAPH * Load(const wxString &aFileName)
Definition: x3d.cpp:46
collects header files for all SG* wrappers and the API
bool ReadSwitch(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition: x3d_ops.cpp:72
bool ReadTransform(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition: x3d_ops.cpp:34
std::vector< wxXmlNode * > NODE_LIST
Definition: x3d_base.h:44