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 The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22/*
23 * Description:
24 * This plugin implements the legacy KiCad X3D parser.
25 * Due to the rare use of X3D models, this plugin is a simple
26 * reimplementation of the legacy x3dmodelparser.cpp and is not
27 * intended to be a compliant X3D implementation.
28 */
29
30#include <vector>
31#include <wx/tokenzr.h>
32#include <wx/wfstream.h>
33#include <wx/xml/xml.h>
34#include <iostream>
35
36#include "x3d.h"
37#include "x3d_ops.h"
38#include "x3d_transform.h"
40
41
42SCENEGRAPH* X3DPARSER::Load( const wxString& aFileName )
43{
44 wxFFileInputStream stream( aFileName );
45 wxXmlDocument doc;
46
47 if( !stream.IsOk() || !doc.Load( stream ) )
48 return nullptr;
49
50 if( doc.GetRoot()->GetName() != wxT( "X3D" ) )
51 return nullptr;
52
53 NODE_LIST children; // VRML Grouping Nodes at top level
54
55 if( !getGroupingNodes( doc.GetRoot(), children ) )
56 return nullptr;
57
58 X3D_DICT dictionary; // dictionary for USE/DEF implementation
59 X3DNODE* topNode = new X3DTRANSFORM;
60 bool ok = false;
61
62 for( NODE_LIST::iterator node_it = children.begin(); node_it != children.end(); ++node_it )
63 {
64 wxXmlNode* node = *node_it;
65 wxString name = node->GetName();
66
67 if( name == wxT( "Transform" ) || name == wxT( "Group" ) )
68 {
69 // Read a Transform / Group
70 ok |= X3D::ReadTransform( node, topNode, dictionary );
71 }
72 else if( name == wxT( "Switch" ) )
73 {
74 ok |= X3D::ReadSwitch( node, topNode, dictionary );
75 }
76 }
77
78 SCENEGRAPH* sp = nullptr;
79
80 if( ok )
81 sp = (SCENEGRAPH*) topNode->TranslateToSG( nullptr );
82
83 delete topNode;
84 return sp;
85}
86
87
88bool X3DPARSER::getGroupingNodes( wxXmlNode* aNode, std::vector<wxXmlNode*>& aResult )
89{
90 aResult.clear();
91 wxXmlNode* scene = nullptr;
92
93 for( wxXmlNode* child = aNode->GetChildren(); child != nullptr; child = child->GetNext() )
94 {
95 if( child->GetName() == wxT( "Scene" ) )
96 {
97 scene = child;
98 break;
99 }
100 }
101
102 if( nullptr == scene )
103 return false;
104
105 for( wxXmlNode* child = scene->GetChildren(); child != nullptr; child = child->GetNext() )
106 {
107 const wxString& name = child->GetName();
108
109 if( name == wxT( "Transform" ) || name == wxT( "Switch" ) || name == wxT( "Group" ) )
110 aResult.push_back( child );
111 }
112
113 if( aResult.empty() )
114 return false;
115
116 return true;
117}
const char * name
Define the basic data set required to represent a 3D model.
Definition scenegraph.h:41
The base class of all X3D nodes.
Definition x3d_base.h:71
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:88
SCENEGRAPH * Load(const wxString &aFileName)
Definition x3d.cpp:42
collects header files for all SG* wrappers and the API
bool ReadSwitch(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition x3d_ops.cpp:68
bool ReadTransform(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition x3d_ops.cpp:30
std::vector< wxXmlNode * > NODE_LIST
Definition x3d_base.h:40