2017-09-29 09:44:05 +00:00
|
|
|
/*
|
2017-04-15 16:08:23 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 KiCad Developers, see CHANGELOG.TXT for contributors.
|
2017-09-29 09:44:05 +00:00
|
|
|
* @author Kristoffer Ödmark
|
2017-04-15 16:08:23 +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 <wx/clipbrd.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <pcb_parser.h>
|
|
|
|
#include <class_netinfo.h>
|
|
|
|
#include <class_board.h>
|
|
|
|
#include <build_version.h>
|
|
|
|
|
|
|
|
#include <kicad_plugin.h>
|
|
|
|
#include <kicad_clipboard.h>
|
|
|
|
|
|
|
|
CLIPBOARD_IO::CLIPBOARD_IO():
|
2017-05-18 17:21:30 +00:00
|
|
|
PCB_IO( CTL_STD_LAYER_NAMES ),
|
2017-04-15 16:08:23 +00:00
|
|
|
m_formatter(),
|
|
|
|
m_parser( new CLIPBOARD_PARSER() )
|
|
|
|
{
|
|
|
|
m_out = &m_formatter;
|
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2017-04-15 16:08:23 +00:00
|
|
|
CLIPBOARD_IO::~CLIPBOARD_IO(){}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2017-04-15 16:08:23 +00:00
|
|
|
STRING_FORMATTER* CLIPBOARD_IO::GetFormatter()
|
|
|
|
{
|
|
|
|
return &m_formatter;
|
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
void CLIPBOARD_IO::SetBoard( BOARD* aBoard )
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
|
|
|
m_board = aBoard;
|
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2017-09-18 11:09:18 +00:00
|
|
|
void CLIPBOARD_IO::SaveSelection( const SELECTION& aSelected )
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
2017-09-29 09:44:05 +00:00
|
|
|
VECTOR2I refPoint( 0, 0 );
|
2017-04-15 16:08:23 +00:00
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
// dont even start if the selection is empty
|
2017-04-15 16:08:23 +00:00
|
|
|
if( aSelected.Empty() )
|
|
|
|
return;
|
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
if( aSelected.HasReferencePoint() )
|
|
|
|
refPoint = aSelected.GetReferencePoint();
|
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
// Prepare net mapping that assures that net codes saved in a file are consecutive integers
|
|
|
|
m_mapping->SetBoard( m_board );
|
2017-04-15 16:08:23 +00:00
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
// Differentiate how it is formatted depending on what selection contains
|
|
|
|
bool onlyModuleParts = true;
|
2017-09-18 23:07:28 +00:00
|
|
|
for( const auto i : aSelected )
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
2017-05-18 17:21:30 +00:00
|
|
|
// check if it not one of the module primitives
|
2017-04-15 16:08:23 +00:00
|
|
|
if( ( i->Type() != PCB_MODULE_EDGE_T ) &&
|
|
|
|
( i->Type() != PCB_MODULE_TEXT_T ) &&
|
|
|
|
( i->Type() != PCB_PAD_T ) )
|
|
|
|
{
|
2017-05-18 17:21:30 +00:00
|
|
|
onlyModuleParts = false;
|
|
|
|
continue;
|
2017-04-15 16:08:23 +00:00
|
|
|
}
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
2017-04-15 16:08:23 +00:00
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
// if there is only parts of a module selected, format it as a new module else
|
|
|
|
// format it as an entire board
|
2017-09-18 23:07:28 +00:00
|
|
|
MODULE partialModule( m_board );
|
2017-05-18 17:21:30 +00:00
|
|
|
|
|
|
|
// only a module selected.
|
|
|
|
if( aSelected.Size() == 1 && aSelected.Front()->Type() == PCB_MODULE_T )
|
|
|
|
{
|
|
|
|
// make the module safe to transfer to other pcbs
|
2017-09-18 23:07:28 +00:00
|
|
|
const MODULE* mod = static_cast<MODULE*>( aSelected.Front() );
|
|
|
|
// Do not modify existing board
|
|
|
|
MODULE newModule(*mod);
|
|
|
|
|
|
|
|
for( D_PAD* pad = newModule.PadsList().begin(); pad; pad = pad->Next() )
|
2017-05-18 17:21:30 +00:00
|
|
|
{
|
2017-09-22 15:17:38 +00:00
|
|
|
pad->SetNetCode( 0, 0 );
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
2017-09-17 18:29:48 +00:00
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
// locate the reference point at (0, 0) in the copied items
|
2017-09-29 09:44:05 +00:00
|
|
|
newModule.Move( wxPoint( -refPoint.x, -refPoint.y ) );
|
2017-09-22 15:17:38 +00:00
|
|
|
|
2017-09-18 23:07:28 +00:00
|
|
|
Format( static_cast<BOARD_ITEM*>( &newModule ) );
|
2017-04-15 16:08:23 +00:00
|
|
|
}
|
2017-05-18 17:21:30 +00:00
|
|
|
// partial module selected.
|
|
|
|
else if( onlyModuleParts )
|
|
|
|
{
|
2017-09-18 23:07:28 +00:00
|
|
|
for( const auto item : aSelected )
|
2017-05-18 17:21:30 +00:00
|
|
|
{
|
|
|
|
auto clone = static_cast<BOARD_ITEM*>( item->Clone() );
|
|
|
|
|
|
|
|
// Do not add reference/value - convert them to the common type
|
|
|
|
if( TEXTE_MODULE* text = dyn_cast<TEXTE_MODULE*>( clone ) )
|
|
|
|
text->SetType( TEXTE_MODULE::TEXT_is_DIVERS );
|
|
|
|
|
|
|
|
// If it is only a module, clear the nets from the pads
|
|
|
|
if( clone->Type() == PCB_PAD_T )
|
|
|
|
{
|
2017-05-21 20:18:22 +00:00
|
|
|
D_PAD* pad = static_cast<D_PAD*>( clone );
|
|
|
|
pad->SetNetCode( 0, 0 );
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
// locate the reference point at (0, 0) in the copied items
|
|
|
|
clone->Move( wxPoint(-refPoint.x, -refPoint.y ) );
|
|
|
|
|
2017-09-18 23:07:28 +00:00
|
|
|
partialModule.Add( clone );
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the new relative internal local coordinates of copied items
|
|
|
|
MODULE* editedModule = m_board->m_Modules;
|
2017-09-18 23:07:28 +00:00
|
|
|
wxPoint moveVector = partialModule.GetPosition() + editedModule->GetPosition();
|
2017-05-18 17:21:30 +00:00
|
|
|
|
2017-09-18 23:07:28 +00:00
|
|
|
partialModule.MoveAnchorPosition( moveVector );
|
2017-05-18 17:21:30 +00:00
|
|
|
|
2017-09-18 23:07:28 +00:00
|
|
|
Format( &partialModule, 0 );
|
2017-05-18 17:21:30 +00:00
|
|
|
|
|
|
|
}
|
2017-09-17 18:29:48 +00:00
|
|
|
// lots of stuff selected
|
2017-05-18 17:21:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// we will fake being a .kicad_pcb to get the full parser kicking
|
|
|
|
// This means we also need layers and nets
|
2017-09-29 09:44:05 +00:00
|
|
|
m_formatter.Print( 0, "(kicad_pcb (version %d) (host pcbnew %s)\n",
|
|
|
|
SEXPR_BOARD_FILE_VERSION, m_formatter.Quotew( GetBuildVersion() ).c_str() );
|
2017-04-15 16:08:23 +00:00
|
|
|
|
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
m_formatter.Print( 0, "\n" );
|
|
|
|
|
|
|
|
formatBoardLayers( m_board );
|
|
|
|
formatNetInformation( m_board );
|
|
|
|
|
|
|
|
m_formatter.Print( 0, "\n" );
|
|
|
|
|
|
|
|
|
2017-09-18 23:07:28 +00:00
|
|
|
for( const auto i : aSelected )
|
2017-05-18 17:21:30 +00:00
|
|
|
{
|
|
|
|
// Dont format stuff that cannot exist standalone!
|
|
|
|
if( ( i->Type() != PCB_MODULE_EDGE_T ) &&
|
|
|
|
( i->Type() != PCB_MODULE_TEXT_T ) &&
|
|
|
|
( i->Type() != PCB_PAD_T ) )
|
|
|
|
{
|
|
|
|
auto item = static_cast<BOARD_ITEM*>( i );
|
2017-09-22 15:17:38 +00:00
|
|
|
std::unique_ptr<BOARD_ITEM> clone( static_cast<BOARD_ITEM*> ( item->Clone() ) );
|
|
|
|
|
|
|
|
// locate the reference point at (0, 0) in the copied items
|
|
|
|
clone->Move( wxPoint(-refPoint.x, -refPoint.y ) );
|
|
|
|
|
|
|
|
Format( clone.get(), 1 );
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
m_formatter.Print( 0, "\n)" );
|
|
|
|
}
|
2017-04-15 16:08:23 +00:00
|
|
|
if( wxTheClipboard->Open() )
|
|
|
|
{
|
2017-09-29 09:44:05 +00:00
|
|
|
wxTheClipboard->SetData( new wxTextDataObject(
|
|
|
|
wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) );
|
2017-04-15 16:08:23 +00:00
|
|
|
wxTheClipboard->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
BOARD_ITEM* CLIPBOARD_IO::Parse()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if( wxTheClipboard->Open() )
|
|
|
|
{
|
|
|
|
if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
|
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
wxTheClipboard->GetData( data );
|
|
|
|
|
|
|
|
result = data.GetText().mb_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
BOARD_ITEM *item;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
item = PCB_IO::Parse( result );
|
|
|
|
} catch (...) {
|
|
|
|
item = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2017-04-15 16:08:23 +00:00
|
|
|
void CLIPBOARD_IO::Save( const wxString& aFileName, BOARD* aBoard,
|
|
|
|
const PROPERTIES* aProperties )
|
|
|
|
{
|
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
|
|
|
|
|
|
|
init( aProperties );
|
|
|
|
|
|
|
|
m_board = aBoard; // after init()
|
|
|
|
|
|
|
|
// Prepare net mapping that assures that net codes saved in a file are consecutive integers
|
|
|
|
m_mapping->SetBoard( aBoard );
|
|
|
|
|
|
|
|
STRING_FORMATTER formatter;
|
|
|
|
|
|
|
|
m_out = &formatter;
|
|
|
|
|
|
|
|
m_out->Print( 0, "(kicad_pcb (version %d) (host pcbnew %s)\n", SEXPR_BOARD_FILE_VERSION,
|
|
|
|
formatter.Quotew( GetBuildVersion() ).c_str() );
|
|
|
|
|
|
|
|
Format( aBoard, 1 );
|
|
|
|
|
|
|
|
m_out->Print( 0, ")\n" );
|
|
|
|
|
|
|
|
if( wxTheClipboard->Open() )
|
|
|
|
{
|
2017-09-29 09:44:05 +00:00
|
|
|
wxTheClipboard->SetData( new wxTextDataObject(
|
|
|
|
wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) );
|
2017-04-15 16:08:23 +00:00
|
|
|
wxTheClipboard->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
|
|
|
BOARD* CLIPBOARD_IO::Load( const wxString& aFileName,
|
|
|
|
BOARD* aAppendToMe, const PROPERTIES* aProperties )
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if( wxTheClipboard->Open() )
|
|
|
|
{
|
|
|
|
if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
|
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
wxTheClipboard->GetData( data );
|
|
|
|
|
|
|
|
result = data.GetText().mb_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
STRING_LINE_READER reader(result, wxT( "clipboard" ) );
|
|
|
|
|
|
|
|
init( aProperties );
|
|
|
|
|
|
|
|
m_parser->SetLineReader( &reader );
|
|
|
|
m_parser->SetBoard( aAppendToMe );
|
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
BOARD_ITEM* item;
|
2017-04-15 16:08:23 +00:00
|
|
|
BOARD* board;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-05-18 17:21:30 +00:00
|
|
|
item = m_parser->Parse();
|
2017-04-15 16:08:23 +00:00
|
|
|
}
|
|
|
|
catch( const FUTURE_FORMAT_ERROR& )
|
|
|
|
{
|
|
|
|
// Don't wrap a FUTURE_FORMAT_ERROR in another
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch( const PARSE_ERROR& parse_error )
|
|
|
|
{
|
|
|
|
if( m_parser->IsTooRecent() )
|
|
|
|
throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
if( item->Type() != PCB_T )
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
|
|
|
// The parser loaded something that was valid, but wasn't a board.
|
|
|
|
THROW_PARSE_ERROR( _( "Clipboard content is not Kicad compatible" ),
|
|
|
|
m_parser->CurSource(), m_parser->CurLine(),
|
|
|
|
m_parser->CurLineNumber(), m_parser->CurOffset() );
|
|
|
|
}
|
2017-05-18 17:21:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
board = dynamic_cast<BOARD*>( item );
|
|
|
|
}
|
2017-04-15 16:08:23 +00:00
|
|
|
// Give the filename to the board if it's new
|
|
|
|
if( !aAppendToMe )
|
|
|
|
board->SetFileName( aFileName );
|
|
|
|
|
|
|
|
return board;
|
|
|
|
}
|