2015-12-20 07:59:26 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-02-05 04:32:52 +00:00
|
|
|
* Copyright (C) 2015-2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2021-10-06 02:46:53 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.TXT for contributors.
|
2015-12-20 07:59:26 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Description:
|
2021-07-18 14:06:48 +00:00
|
|
|
* This plugin implements the legacy KiCad VRML1/VRML2 and X3D parsers
|
2016-02-05 04:32:52 +00:00
|
|
|
* The plugin will invoke a VRML1 or VRML2 parser depending on the
|
|
|
|
* identifying information in the file header:
|
2015-12-20 07:59:26 +00:00
|
|
|
*
|
2021-07-18 14:06:48 +00:00
|
|
|
* #VRML V1.0 ASCII
|
2015-12-20 07:59:26 +00:00
|
|
|
* #VRML V2.0 utf8
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "plugins/3d/3d_plugin.h"
|
|
|
|
#include "plugins/3dapi/ifsg_all.h"
|
2019-12-05 14:03:15 +00:00
|
|
|
#include "richio.h"
|
2016-01-05 07:55:54 +00:00
|
|
|
#include "vrml1_base.h"
|
2015-12-22 08:02:18 +00:00
|
|
|
#include "vrml2_base.h"
|
2019-12-05 14:03:15 +00:00
|
|
|
#include "wrlproc.h"
|
2016-02-05 04:32:52 +00:00
|
|
|
#include "x3d.h"
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <clocale>
|
|
|
|
#include <wx/filename.h>
|
2020-08-17 22:48:20 +00:00
|
|
|
#include <wx/stdpaths.h>
|
|
|
|
#include <wx/string.h>
|
|
|
|
#include <wx/wfstream.h>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <wx/log.h>
|
2015-12-20 07:59:26 +00:00
|
|
|
|
2020-08-17 22:48:20 +00:00
|
|
|
#include <decompress.hpp>
|
|
|
|
|
2015-12-20 07:59:26 +00:00
|
|
|
|
|
|
|
#define PLUGIN_VRML_MAJOR 1
|
2016-02-22 08:59:07 +00:00
|
|
|
#define PLUGIN_VRML_MINOR 3
|
2016-04-03 00:56:41 +00:00
|
|
|
#define PLUGIN_VRML_PATCH 2
|
2016-06-13 16:50:42 +00:00
|
|
|
#define PLUGIN_VRML_REVNO 2
|
2015-12-20 07:59:26 +00:00
|
|
|
|
|
|
|
|
2021-07-24 17:27:35 +00:00
|
|
|
/**
|
|
|
|
* Flag to enable VRML plugin trace output.
|
|
|
|
*
|
|
|
|
* @ingroup trace_env_vars
|
|
|
|
*/
|
|
|
|
const wxChar* const traceVrmlPlugin = wxT( "KICAD_VRML_PLUGIN" );
|
|
|
|
|
|
|
|
|
2015-12-20 07:59:26 +00:00
|
|
|
const char* GetKicadPluginName( void )
|
|
|
|
{
|
|
|
|
return "PLUGIN_3D_VRML";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
void GetPluginVersion( unsigned char* Major, unsigned char* Minor, unsigned char* Patch,
|
|
|
|
unsigned char* Revision )
|
2015-12-20 07:59:26 +00:00
|
|
|
{
|
|
|
|
if( Major )
|
|
|
|
*Major = PLUGIN_VRML_MAJOR;
|
|
|
|
|
|
|
|
if( Minor )
|
|
|
|
*Minor = PLUGIN_VRML_MINOR;
|
|
|
|
|
|
|
|
if( Patch )
|
|
|
|
*Patch = PLUGIN_VRML_PATCH;
|
|
|
|
|
|
|
|
if( Revision )
|
|
|
|
*Revision = PLUGIN_VRML_REVNO;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct FILE_DATA
|
|
|
|
{
|
2020-08-17 22:48:20 +00:00
|
|
|
std::vector<std::string> extensions;
|
|
|
|
std::vector<std::string> filters;
|
2015-12-20 07:59:26 +00:00
|
|
|
|
|
|
|
FILE_DATA()
|
|
|
|
{
|
|
|
|
|
2020-08-17 22:48:20 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
extensions = { "wrl", "wrz", "x3d" };
|
|
|
|
filters = { "VRML 1.0/2.0 (*.wrl;*.wrz)|*.wrl;*.wrz",
|
|
|
|
"X3D (*.x3d)|*.x3d" };
|
|
|
|
#else
|
|
|
|
extensions = { "wrl", "WRL", "wrz", "WRZ", "x3d", "X3D" };
|
|
|
|
filters = { "VRML 1.0/2.0 (*.wrl;*.WRL;*.wrz;*.WRZ)|*.wrl;*.WRL;*.wrz;*.WRZ",
|
|
|
|
"X3D (*.x3d;*.X3D)|*.x3d;*.X3D" };
|
|
|
|
#endif
|
2015-12-20 07:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} file_data;
|
|
|
|
|
|
|
|
|
|
|
|
int GetNExtensions( void )
|
|
|
|
{
|
2020-08-17 22:48:20 +00:00
|
|
|
return file_data.extensions.size();
|
2015-12-20 07:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char const* GetModelExtension( int aIndex )
|
|
|
|
{
|
2020-08-17 22:48:20 +00:00
|
|
|
if( aIndex < 0 || aIndex >= int( file_data.extensions.size() ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2015-12-20 07:59:26 +00:00
|
|
|
|
2020-08-17 22:48:20 +00:00
|
|
|
return file_data.extensions[aIndex].c_str();
|
2015-12-20 07:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GetNFilters( void )
|
|
|
|
{
|
2020-08-17 22:48:20 +00:00
|
|
|
return file_data.filters.size();
|
2015-12-20 07:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char const* GetFileFilter( int aIndex )
|
|
|
|
{
|
2020-08-17 22:48:20 +00:00
|
|
|
if( aIndex < 0 || aIndex >= int( file_data.filters.size() ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2015-12-20 07:59:26 +00:00
|
|
|
|
2020-08-17 22:48:20 +00:00
|
|
|
return file_data.filters[aIndex].c_str();
|
2015-12-20 07:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CanRender( void )
|
|
|
|
{
|
|
|
|
// this plugin supports rendering of IDF component outlines
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LOCALESWITCH
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LOCALESWITCH()
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
m_locale = setlocale( LC_NUMERIC, nullptr );
|
2015-12-20 07:59:26 +00:00
|
|
|
setlocale( LC_NUMERIC, "C" );
|
|
|
|
}
|
|
|
|
|
|
|
|
~LOCALESWITCH()
|
|
|
|
{
|
2016-08-24 12:37:30 +00:00
|
|
|
setlocale( LC_NUMERIC, m_locale.c_str() );
|
2015-12-20 07:59:26 +00:00
|
|
|
}
|
2021-07-24 17:27:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Store the user locale name, to restore this locale later, in dtor
|
|
|
|
std::string m_locale;
|
2015-12-20 07:59:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-22 08:59:07 +00:00
|
|
|
SCENEGRAPH* LoadVRML( const wxString& aFileName, bool useInline )
|
2015-12-20 07:59:26 +00:00
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
FILE_LINE_READER* modelFile = nullptr;
|
|
|
|
SCENEGRAPH* scene = nullptr;
|
2020-08-17 22:48:20 +00:00
|
|
|
wxString filename = aFileName;
|
|
|
|
wxFileName tmpfilename;
|
|
|
|
|
|
|
|
if( aFileName.Upper().EndsWith( "WRZ" ) )
|
|
|
|
{
|
2020-12-12 23:51:35 +00:00
|
|
|
wxFFileInputStream ifile( aFileName );
|
2020-08-17 22:48:20 +00:00
|
|
|
tmpfilename = wxFileName( aFileName );
|
2020-08-25 19:32:26 +00:00
|
|
|
|
|
|
|
tmpfilename.SetPath( wxStandardPaths::Get().GetTempDir() );
|
2020-08-17 22:48:20 +00:00
|
|
|
tmpfilename.SetExt( "WRL" );
|
|
|
|
|
|
|
|
wxFileOffset size = ifile.GetLength();
|
|
|
|
|
|
|
|
if( size == wxInvalidOffset )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
{
|
2020-12-12 23:51:35 +00:00
|
|
|
wxFFileOutputStream ofile( tmpfilename.GetFullPath() );
|
2020-08-17 22:48:20 +00:00
|
|
|
|
|
|
|
if( !ofile.IsOk() )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
char *buffer = new char[size];
|
|
|
|
|
|
|
|
ifile.Read( buffer, size);
|
2020-08-26 21:18:54 +00:00
|
|
|
std::string expanded;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
expanded = gzip::decompress( buffer, size );
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
delete[] buffer;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-08-17 22:48:20 +00:00
|
|
|
|
2020-08-19 10:34:30 +00:00
|
|
|
delete[] buffer;
|
2020-08-17 22:48:20 +00:00
|
|
|
|
|
|
|
ofile.Write( expanded.data(), expanded.size() );
|
|
|
|
ofile.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = tmpfilename.GetFullPath();
|
|
|
|
}
|
2015-12-20 07:59:26 +00:00
|
|
|
|
2016-01-11 23:12:50 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// set the max char limit to 8MB; if a VRML file contains
|
|
|
|
// longer lines then perhaps it shouldn't be used
|
2020-08-25 19:32:26 +00:00
|
|
|
modelFile = new FILE_LINE_READER( filename, 0, 8388608 );
|
2016-01-11 23:12:50 +00:00
|
|
|
}
|
2016-10-10 15:04:48 +00:00
|
|
|
catch( IO_ERROR & )
|
2016-01-11 23:12:50 +00:00
|
|
|
{
|
2020-10-20 19:05:04 +00:00
|
|
|
wxLogError( wxS( " * " ) + _( "[INFO] load failed: input line too long\n" ) );
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-01-11 23:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// VRML file processor
|
|
|
|
WRLPROC proc( modelFile );
|
2015-12-20 07:59:26 +00:00
|
|
|
|
2020-08-17 22:48:20 +00:00
|
|
|
// Cleanup our temporary file
|
|
|
|
if( tmpfilename.IsOk() )
|
|
|
|
wxRemoveFile( tmpfilename.GetFullPath() );
|
|
|
|
|
2021-03-10 23:56:28 +00:00
|
|
|
if( proc.GetVRMLType() == WRLVERSION::VRML_V1 )
|
2015-12-30 23:31:35 +00:00
|
|
|
{
|
2021-07-24 17:27:35 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin, " * [INFO] Processing VRML 1.0 file" );
|
2016-01-05 07:55:54 +00:00
|
|
|
|
|
|
|
WRL1BASE* bp = new WRL1BASE;
|
|
|
|
|
|
|
|
if( !bp->Read( proc ) )
|
|
|
|
{
|
2021-07-24 17:27:35 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin, " * [INFO] load failed" );
|
2016-01-05 07:55:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-24 17:27:35 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin, " * [INFO] load completed" );
|
2016-01-05 07:55:54 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
scene = (SCENEGRAPH*)bp->TranslateToSG( nullptr, nullptr );
|
2016-01-05 07:55:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete bp;
|
2015-12-30 23:31:35 +00:00
|
|
|
}
|
2015-12-20 07:59:26 +00:00
|
|
|
else
|
2015-12-22 08:02:18 +00:00
|
|
|
{
|
2021-07-24 17:27:35 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin, " * [INFO] Processing VRML 2.0 file" );
|
2016-01-05 07:55:54 +00:00
|
|
|
|
2015-12-22 08:02:18 +00:00
|
|
|
WRL2BASE* bp = new WRL2BASE;
|
|
|
|
|
2016-01-04 07:10:15 +00:00
|
|
|
// allow Inline{} files to be included
|
|
|
|
bp->SetEnableInline( true );
|
|
|
|
|
2015-12-22 08:02:18 +00:00
|
|
|
if( !bp->Read( proc ) )
|
2015-12-30 23:31:35 +00:00
|
|
|
{
|
2021-07-24 17:27:35 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin, " * [INFO] load failed" );
|
2015-12-30 23:31:35 +00:00
|
|
|
}
|
2015-12-22 08:02:18 +00:00
|
|
|
else
|
2015-12-30 23:31:35 +00:00
|
|
|
{
|
2021-07-24 17:27:35 +00:00
|
|
|
wxLogTrace( traceVrmlPlugin, " * [INFO] load completed" );
|
2016-01-01 07:55:43 +00:00
|
|
|
|
2016-01-05 07:55:54 +00:00
|
|
|
// for now we recalculate all normals per-vertex per-face
|
2021-07-18 14:06:48 +00:00
|
|
|
scene = (SCENEGRAPH*)bp->TranslateToSG( nullptr );
|
2015-12-30 23:31:35 +00:00
|
|
|
}
|
2015-12-22 08:02:18 +00:00
|
|
|
|
|
|
|
delete bp;
|
|
|
|
}
|
2015-12-20 07:59:26 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != modelFile )
|
2016-01-11 23:12:50 +00:00
|
|
|
delete modelFile;
|
|
|
|
|
2016-01-02 01:21:02 +00:00
|
|
|
// DEBUG: WRITE OUT VRML2 FILE TO CONFIRM STRUCTURE
|
2021-07-18 14:06:48 +00:00
|
|
|
#if ( defined( DEBUG_VRML1 ) && DEBUG_VRML1 > 3 ) \
|
|
|
|
|| ( defined( DEBUG_VRML2 ) && DEBUG_VRML2 > 3 )
|
2016-01-02 01:21:02 +00:00
|
|
|
if( scene )
|
|
|
|
{
|
|
|
|
wxFileName fn( wxString::FromUTF8Unchecked( aFileName ) );
|
2016-01-02 03:04:28 +00:00
|
|
|
wxString output;
|
|
|
|
|
|
|
|
if( proc.GetVRMLType() == VRML_V1 )
|
|
|
|
output = wxT( "_vrml1-" );
|
|
|
|
else
|
|
|
|
output = wxT( "_vrml2-" );
|
|
|
|
|
2016-01-02 01:21:02 +00:00
|
|
|
output.append( fn.GetName() );
|
|
|
|
output.append( wxT(".wrl") );
|
2016-01-29 00:52:42 +00:00
|
|
|
S3D::WriteVRML( output.ToUTF8(), true, (SGNODE*)(scene), true, true );
|
2016-01-02 01:21:02 +00:00
|
|
|
}
|
2021-07-18 14:06:48 +00:00
|
|
|
#endif
|
2016-01-02 01:21:02 +00:00
|
|
|
|
2016-01-01 07:55:43 +00:00
|
|
|
return scene;
|
2015-12-20 07:59:26 +00:00
|
|
|
}
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
SCENEGRAPH* LoadX3D( const wxString& aFileName )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
SCENEGRAPH* scene = nullptr;
|
2016-02-05 04:32:52 +00:00
|
|
|
X3DPARSER model;
|
|
|
|
scene = model.Load( aFileName );
|
|
|
|
|
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCENEGRAPH* Load( char const* aFileName )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == aFileName )
|
|
|
|
return nullptr;
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
wxString fname = wxString::FromUTF8Unchecked( aFileName );
|
|
|
|
|
|
|
|
if( !wxFileName::FileExists( fname ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
LOCALESWITCH switcher;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
SCENEGRAPH* scene = nullptr;
|
2016-02-05 04:32:52 +00:00
|
|
|
wxString ext = wxFileName( fname ).GetExt();
|
|
|
|
|
|
|
|
if( ext == "x3d" || ext == "X3D" )
|
|
|
|
scene = LoadX3D( fname );
|
|
|
|
else
|
2016-02-22 08:59:07 +00:00
|
|
|
scene = LoadVRML( fname, true );
|
2016-02-05 04:32:52 +00:00
|
|
|
|
|
|
|
return scene;
|
|
|
|
}
|