kicad/plugins/3d/vrml/v2/vrml2_switch.cpp

287 lines
7.6 KiB
C++
Raw Normal View History

2016-01-04 07:10:15 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015-2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
2021-07-18 14:06:48 +00:00
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
2016-01-04 07:10:15 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <iostream>
#include <sstream>
#include <wx/log.h>
2016-01-04 07:10:15 +00:00
#include "vrml2_base.h"
#include "vrml2_switch.h"
#include "plugins/3dapi/ifsg_all.h"
WRL2SWITCH::WRL2SWITCH() : WRL2NODE()
{
2021-03-10 23:49:09 +00:00
m_Type = WRL2NODES::WRL2_SWITCH;
2016-01-04 07:10:15 +00:00
whichChoice = -1;
}
WRL2SWITCH::WRL2SWITCH( WRL2NODE* aParent ) : WRL2NODE()
{
2021-03-10 23:49:09 +00:00
m_Type = WRL2NODES::WRL2_SWITCH;
2016-01-04 07:10:15 +00:00
m_Parent = aParent;
whichChoice = -1;
2021-07-18 14:06:48 +00:00
if( nullptr != m_Parent )
2016-01-04 07:10:15 +00:00
m_Parent->AddChildNode( this );
}
WRL2SWITCH::~WRL2SWITCH()
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( " * [INFO] Destroying Switch node with %zu children, %zu"
"references, and %zu back pointers." ),
2021-07-24 17:27:35 +00:00
m_Children.size(), m_Refs.size(), m_BackPointers.size() );
2016-01-04 07:10:15 +00:00
}
bool WRL2SWITCH::isDangling( void )
{
// a Switch node is never dangling
return false;
}
bool WRL2SWITCH::Read( WRLPROC& proc, WRL2BASE* aTopNode )
{
/*
* Structure of a Switch node (p.113):
*
* Switch {
* exposedField MFNode choice []
* exposedField SFInt32 whichChoice -1
* }
*/
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( aTopNode, false, wxT( "Invalid top node." ) );
2016-01-04 07:10:15 +00:00
char tok = proc.Peek();
if( proc.eof() )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
" * [INFO] bad file format; unexpected eof %s." ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
2016-01-04 07:10:15 +00:00
return false;
}
if( '{' != tok )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n"
" * [INFO] bad file format; expecting '{' but got '%s' %s." ),
__FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
2016-01-04 07:10:15 +00:00
return false;
}
proc.Pop();
std::string glob;
while( true )
{
if( proc.Peek() == '}' )
{
proc.Pop();
break;
}
if( !proc.ReadName( glob ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
"%s" ),
__FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
2016-01-04 07:10:15 +00:00
return false;
}
// expecting one of:
// choice
// whichChoice
if( !glob.compare( "whichChoice" ) )
{
if( !proc.ReadSFInt( whichChoice ) )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
" * [INFO] invalid whichChoice %s\n"
" * [INFO] file: '%s'\n"
"%s" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName(), proc.GetError() );
2016-01-04 07:10:15 +00:00
return false;
}
}
else if( !glob.compare( "choice" ) )
{
if( !readChildren( proc, aTopNode ) )
return false;
}
else
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( "%s:%s:%d\n"
" * [INFO] invalid Switch %s.\n"
" * [INFO] file: '%s'\n" ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
proc.GetFileName() );
2016-01-04 07:10:15 +00:00
return false;
}
} // while( true ) -- reading contents of Switch{}
return true;
}
bool WRL2SWITCH::AddRefNode( WRL2NODE* aNode )
{
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( aNode, false, wxT( "Invalid node." ) );
2016-01-04 07:10:15 +00:00
// take possession if the node is dangling WRL2_SHAPE
2021-03-10 23:49:09 +00:00
if( WRL2NODES::WRL2_SHAPE == aNode->GetNodeType() && aNode->isDangling() )
2016-01-04 07:10:15 +00:00
{
WRL2NODE* np = aNode->GetParent();
2021-07-18 14:06:48 +00:00
if( nullptr != np )
2016-01-04 07:10:15 +00:00
aNode->SetParent( this );
if( !WRL2NODE::AddChildNode( aNode ) )
{
2021-07-18 14:06:48 +00:00
aNode->SetParent( nullptr );
return false;
}
2016-01-04 07:10:15 +00:00
}
if( !WRL2NODE::AddRefNode( aNode ) )
return false;
2016-01-04 07:10:15 +00:00
return true;
}
bool WRL2SWITCH::readChildren( WRLPROC& proc, WRL2BASE* aTopNode )
{
char tok = proc.Peek();
if( proc.eof() )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
" * [INFO] bad file format; unexpected eof %s." ),
__FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
2016-01-04 07:10:15 +00:00
return false;
}
2021-07-18 14:06:48 +00:00
WRL2NODE* child = nullptr;
2016-01-04 07:10:15 +00:00
if( '[' != tok )
{
// since there are no delimiters we expect a single child
2016-01-04 07:10:15 +00:00
if( !aTopNode->ReadNode( proc, this, &child ) )
return false;
2021-07-18 14:06:48 +00:00
if( nullptr != child )
2016-01-04 07:10:15 +00:00
choices.push_back( child );
if( proc.Peek() == ',' )
proc.Pop();
return true;
}
proc.Pop();
while( true )
{
if( proc.Peek() == ']' )
{
proc.Pop();
break;
}
if( !aTopNode->ReadNode( proc, this, &child ) )
return false;
2021-07-18 14:06:48 +00:00
if( nullptr != child )
2016-01-04 07:10:15 +00:00
choices.push_back( child );
if( proc.Peek() == ',' )
proc.Pop();
}
return true;
}
SGNODE* WRL2SWITCH::TranslateToSG( SGNODE* aParent )
2016-01-04 07:10:15 +00:00
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( " * [INFO] Translating Switch with %zu children, %zu references, and"
"%zu back pointers." ),
2021-07-24 17:27:35 +00:00
m_Children.size(), m_Refs.size(), m_BackPointers.size() );
2016-01-04 07:10:15 +00:00
if( choices.empty() )
{
2022-02-05 13:25:43 +00:00
wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Switch translation: no choices." ) );
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-04 07:10:15 +00:00
}
S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
2021-07-24 17:27:35 +00:00
wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_TRANSFORM ), nullptr,
wxString::Format( wxT( "Switch does not have a Transform parent (parent "
"ID: %d)." ), ptype ) );
2016-01-04 07:10:15 +00:00
if( whichChoice < 0 || whichChoice >= (int)choices.size() )
{
2021-07-24 17:27:35 +00:00
wxLogTrace( traceVrmlPlugin,
wxT( " * [INFO] Switch translation: no choice (choices = %zu), "
2021-07-24 17:27:35 +00:00
"whichChoice = %d." ), choices.size(), whichChoice );
2016-01-04 07:10:15 +00:00
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-04 07:10:15 +00:00
}
WRL2NODES type = choices[whichChoice]->GetNodeType();
switch( type )
{
2021-03-10 23:49:09 +00:00
case WRL2NODES::WRL2_SHAPE:
case WRL2NODES::WRL2_SWITCH:
case WRL2NODES::WRL2_INLINE:
case WRL2NODES::WRL2_TRANSFORM:
2016-01-04 07:10:15 +00:00
break;
default:
2021-07-18 14:06:48 +00:00
return nullptr;
2016-01-04 07:10:15 +00:00
}
return choices[whichChoice]->TranslateToSG( aParent );
2016-01-04 07:10:15 +00:00
}