Refactor ExportSpecctraFile to be usable in API

Currently there is pcbnew.ExportSpecctraDSN() but it needs a pcbnew
frame set which makes it unusable outside of the scripting window or a
plugin.

This refactors actual exporting logic into separate helper and adds an
overload to ExportSpecctraDSN(board, filename) that doesn't need pcbnew
window.
This commit is contained in:
qu1ck 2021-07-28 12:48:03 -07:00 committed by jean-pierre charras
parent a2bb176b68
commit 7b4dcb6e10
4 changed files with 79 additions and 34 deletions

View File

@ -46,6 +46,7 @@
#include <pcbnew_scripting_helpers.h>
#include <project.h>
#include <settings/settings_manager.h>
#include <specctra.h>
#include <project/project_local_settings.h>
#include <wildcards_and_files_ext.h>
#include <locale_io.h>
@ -270,6 +271,22 @@ bool ExportSpecctraDSN( wxString& aFullFilename )
}
}
bool ExportSpecctraDSN( BOARD* aBoard, wxString& aFullFilename )
{
try
{
ExportBoardToSpecctraFile( aBoard, aFullFilename );
}
catch( ... )
{
return false;
}
return true;
}
bool ExportVRML( const wxString& aFullFileName, double aMMtoWRMLunit, bool aExport3DFiles,
bool aUseRelativePaths, const wxString& a3D_Subdir, double aXRef, double aYRef )
{

View File

@ -83,6 +83,17 @@ wxArrayString GetFootprints( const wxString& aNickName );
*/
bool ExportSpecctraDSN( wxString& aFullFilename );
/**
* Will export the BOARD to a specctra dsn file.
* Unlike first overload doesn't need a valid PCB_EDIT_FRAME set and can be used
* in a standalone python script.
*
* See http://www.autotraxeda.com/docs/SPECCTRA/SPECCTRA.pdf for the specification.
*
* @return true if OK
*/
bool ExportSpecctraDSN( BOARD* aBoard, wxString& aFullFilename );
/**
* Export the current BOARD to a VRML (wrl) file.
*

View File

@ -48,6 +48,15 @@ class SHAPE_POLY_SET;
typedef DSN::T DSN_T;
/**
* @brief Helper method to export board to DSN file
*
* @param aBoard board object
* @param aFullFilename specctra file name
*/
void ExportBoardToSpecctraFile( BOARD* aBoard, const wxString& aFullFilename );
/**
* This source file implements export and import capabilities to the
* specctra dsn file format. The grammar for that file format is documented

View File

@ -85,36 +85,14 @@ static const double safetyMargin = 0.1;
bool PCB_EDIT_FRAME::ExportSpecctraFile( const wxString& aFullFilename )
{
SPECCTRA_DB db;
bool ok = true;
wxString errorText;
BASE_SCREEN* screen = GetScreen();
bool wasModified = screen->IsContentModified();
db.SetPCB( SPECCTRA_DB::MakePCB() );
LOCALE_IO toggle; // Switch the locale to standard C
// Build the board outlines *before* flipping footprints
if( !db.BuiltBoardOutlines( GetBoard() ) )
{
wxLogWarning( _( "Board outline is malformed. Run DRC for a full analysis." ) );
}
// DSN Images (=KiCad FOOTPRINTs and PADs) must be presented from the top view. So we
// temporarily flip any footprints which are on the back side of the board to the front,
// and record this in the FOOTPRINT's flag field.
db.FlipFOOTPRINTs( GetBoard() );
BASE_SCREEN* screen = GetScreen();
bool wasModified = screen->IsContentModified();
wxString errorText;
bool ok = true;
try
{
GetBoard()->SynchronizeNetsAndNetClasses();
db.FromBOARD( GetBoard() );
db.ExportPCB( aFullFilename, true );
// if an exception is thrown by FromBOARD or ExportPCB(), then
// ~SPECCTRA_DB() will close the file.
ExportBoardToSpecctraFile( GetBoard(), aFullFilename );
}
catch( const IO_ERROR& ioe )
{
@ -124,10 +102,7 @@ bool PCB_EDIT_FRAME::ExportSpecctraFile( const wxString& aFullFilename )
errorText = ioe.What();
}
// done assuredly, even if an exception was thrown and caught.
db.RevertFOOTPRINTs( GetBoard() );
// The two calls below to FOOTPRINT::Flip(), both set the
// The two calls to FOOTPRINT::Flip() in ExportBoardToSpecctraFile both set the
// modified flag, yet their actions cancel each other out, so it should
// be ok to clear the modify flag.
if( !wasModified )
@ -139,15 +114,48 @@ bool PCB_EDIT_FRAME::ExportSpecctraFile( const wxString& aFullFilename )
}
else
{
DisplayErrorMessage( this,
_( "Unable to export, please fix and try again" ),
errorText );
DisplayErrorMessage( this, _( "Unable to export, please fix and try again" ), errorText );
}
return ok;
}
void ExportBoardToSpecctraFile( BOARD* aBoard, const wxString& aFullFilename )
{
SPECCTRA_DB db;
db.SetPCB( SPECCTRA_DB::MakePCB() );
LOCALE_IO toggle; // Switch the locale to standard C
// Build the board outlines *before* flipping footprints
if( !db.BuiltBoardOutlines( aBoard ) )
wxLogWarning( _( "Board outline is malformed. Run DRC for a full analysis." ) );
// DSN Images (=KiCad FOOTPRINTs and PADs) must be presented from the top view. So we
// temporarily flip any footprints which are on the back side of the board to the front,
// and record this in the FOOTPRINT's flag field.
db.FlipFOOTPRINTs( aBoard );
try
{
aBoard->SynchronizeNetsAndNetClasses();
db.FromBOARD( aBoard );
db.ExportPCB( aFullFilename, true );
db.RevertFOOTPRINTs( aBoard );
// if an exception is thrown by FromBOARD or ExportPCB(), then
// ~SPECCTRA_DB() will close the file.
}
catch( ... )
{
db.RevertFOOTPRINTs( aBoard );
throw;
}
}
namespace DSN {
const KICAD_T SPECCTRA_DB::scanPADs[] = { PCB_PAD_T, EOT };