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.
|
|
|
|
*
|
2021-10-06 02:46:53 +00:00
|
|
|
* Copyright (C) 2017 KiCad Developers, see AUTHORS.TXT for contributors.
|
2023-03-30 11:49:23 +00:00
|
|
|
* Copyright (C) 2017-2023 KiCad Developers, see AUTHORS.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>
|
2021-06-01 05:17:57 +00:00
|
|
|
#include <wx/log.h>
|
2019-12-06 14:26:59 +00:00
|
|
|
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2023-11-18 22:34:09 +00:00
|
|
|
#include <build_version.h>
|
2023-04-19 02:44:04 +00:00
|
|
|
#include <core/ignore.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <pad.h>
|
2021-06-03 17:03:25 +00:00
|
|
|
#include <pcb_group.h>
|
2024-01-16 23:37:12 +00:00
|
|
|
#include <pcb_generator.h>
|
2020-10-04 23:34:59 +00:00
|
|
|
#include <pcb_text.h>
|
2024-03-28 13:33:44 +00:00
|
|
|
#include <pcb_table.h>
|
2021-06-03 18:05:43 +00:00
|
|
|
#include <zone.h>
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2023-12-24 01:21:58 +00:00
|
|
|
#include <pcb_io/kicad_sexpr/pcb_io_kicad_sexpr.h>
|
|
|
|
#include <pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.h>
|
2017-04-15 16:08:23 +00:00
|
|
|
#include <kicad_clipboard.h>
|
2024-04-27 19:57:24 +00:00
|
|
|
#include <kidialog.h>
|
2017-04-15 16:08:23 +00:00
|
|
|
|
|
|
|
CLIPBOARD_IO::CLIPBOARD_IO():
|
2023-12-24 01:21:58 +00:00
|
|
|
PCB_IO_KICAD_SEXPR(CTL_FOR_CLIPBOARD ),
|
2021-11-25 12:22:40 +00:00
|
|
|
m_formatter()
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
|
|
|
m_out = &m_formatter;
|
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2017-11-01 09:20:13 +00:00
|
|
|
CLIPBOARD_IO::~CLIPBOARD_IO()
|
|
|
|
{
|
|
|
|
}
|
2017-04-15 16:08:23 +00:00
|
|
|
|
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
|
|
|
|
2020-12-16 13:31:32 +00:00
|
|
|
void CLIPBOARD_IO::SaveSelection( const PCB_SELECTION& aSelected, bool isFootprintEditor )
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
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
|
|
|
|
2024-03-28 13:33:44 +00:00
|
|
|
auto deleteUnselectedCells =
|
|
|
|
[]( PCB_TABLE* aTable )
|
|
|
|
{
|
|
|
|
int minCol = aTable->GetColCount();
|
|
|
|
int maxCol = -1;
|
|
|
|
int minRow = aTable->GetRowCount();
|
|
|
|
int maxRow = -1;
|
|
|
|
|
|
|
|
for( int row = 0; row < aTable->GetRowCount(); ++row )
|
|
|
|
{
|
|
|
|
for( int col = 0; col < aTable->GetColCount(); ++col )
|
|
|
|
{
|
|
|
|
PCB_TABLECELL* cell = aTable->GetCell( row, col );
|
|
|
|
|
|
|
|
if( cell->IsSelected() )
|
|
|
|
{
|
|
|
|
minRow = std::min( minRow, row );
|
|
|
|
maxRow = std::max( maxRow, row );
|
|
|
|
minCol = std::min( minCol, col );
|
|
|
|
maxCol = std::max( maxCol, col );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell->SetFlags( STRUCT_DELETED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCHECK_MSG( maxCol >= minCol && maxRow >= minRow, /*void*/,
|
|
|
|
wxT( "No selected cells!" ) );
|
|
|
|
|
|
|
|
// aTable is always a clone in the clipboard case
|
|
|
|
int destRow = 0;
|
|
|
|
|
|
|
|
for( int row = minRow; row <= maxRow; row++ )
|
|
|
|
aTable->SetRowHeight( destRow++, aTable->GetRowHeight( row ) );
|
|
|
|
|
|
|
|
int destCol = 0;
|
|
|
|
|
|
|
|
for( int col = minCol; col <= maxCol; col++ )
|
|
|
|
aTable->SetColWidth( destCol++, aTable->GetColWidth( col ) );
|
|
|
|
|
|
|
|
aTable->DeleteMarkedCells();
|
|
|
|
aTable->SetColCount( ( maxCol - minCol ) + 1 );
|
|
|
|
aTable->Normalize();
|
|
|
|
};
|
|
|
|
|
|
|
|
std::set<PCB_TABLE*> promotedTables;
|
|
|
|
|
|
|
|
auto parentIsPromoted =
|
|
|
|
[&]( PCB_TABLECELL* cell ) -> bool
|
|
|
|
{
|
|
|
|
for( PCB_TABLE* table : promotedTables )
|
|
|
|
{
|
|
|
|
if( table->m_Uuid == cell->GetParent()->m_Uuid )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2020-11-13 12:21:02 +00:00
|
|
|
if( aSelected.Size() == 1 && aSelected.Front()->Type() == PCB_FOOTPRINT_T )
|
2017-05-18 17:21:30 +00:00
|
|
|
{
|
2020-11-10 21:20:03 +00:00
|
|
|
// make the footprint safe to transfer to other pcbs
|
2020-11-13 15:15:52 +00:00
|
|
|
const FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aSelected.Front() );
|
2017-09-18 23:07:28 +00:00
|
|
|
// Do not modify existing board
|
2020-11-13 15:15:52 +00:00
|
|
|
FOOTPRINT newFootprint( *footprint );
|
2017-09-18 23:07:28 +00:00
|
|
|
|
2020-11-12 22:30:02 +00:00
|
|
|
for( PAD* pad : newFootprint.Pads() )
|
2019-11-03 16:09:23 +00:00
|
|
|
pad->SetNetCode( 0 );
|
|
|
|
|
|
|
|
// locked means "locked in place"; copied items therefore can't be locked
|
2020-11-10 21:20:03 +00:00
|
|
|
newFootprint.SetLocked( false );
|
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
|
2022-01-11 00:49:49 +00:00
|
|
|
newFootprint.Move( VECTOR2I( -refPoint.x, -refPoint.y ) );
|
2017-09-22 15:17:38 +00:00
|
|
|
|
2020-11-10 21:20:03 +00:00
|
|
|
Format( static_cast<BOARD_ITEM*>( &newFootprint ) );
|
2023-01-18 00:40:14 +00:00
|
|
|
|
|
|
|
newFootprint.SetParent( nullptr );
|
2023-06-14 22:33:41 +00:00
|
|
|
newFootprint.SetParentGroup( nullptr );
|
2017-04-15 16:08:23 +00:00
|
|
|
}
|
2020-11-16 00:45:43 +00:00
|
|
|
else if( isFootprintEditor )
|
2017-05-18 17:21:30 +00:00
|
|
|
{
|
2020-11-13 15:15:52 +00:00
|
|
|
FOOTPRINT partialFootprint( m_board );
|
2020-07-27 17:56:15 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// Useful to copy the selection to the board editor (if any), and provides
|
2020-11-25 16:09:23 +00:00
|
|
|
// a dummy lib id.
|
|
|
|
// Perhaps not a good Id, but better than a empty id
|
|
|
|
KIID dummy;
|
|
|
|
LIB_ID id( "clipboard", dummy.AsString() );
|
|
|
|
partialFootprint.SetFPID( id );
|
|
|
|
|
2024-03-28 13:33:44 +00:00
|
|
|
for( EDA_ITEM* item : aSelected )
|
2017-05-18 17:21:30 +00:00
|
|
|
{
|
2024-03-28 13:33:44 +00:00
|
|
|
BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( item );
|
|
|
|
BOARD_ITEM* copy = nullptr;
|
2023-07-13 10:24:33 +00:00
|
|
|
|
|
|
|
wxCHECK2( boardItem, continue );
|
2020-10-20 11:37:38 +00:00
|
|
|
|
2024-03-28 13:33:44 +00:00
|
|
|
if( PCB_FIELD* field = dynamic_cast<PCB_FIELD*>( item ) )
|
2021-07-01 18:57:29 +00:00
|
|
|
{
|
2023-06-06 17:30:03 +00:00
|
|
|
if( field->IsMandatoryField() )
|
2021-07-01 18:57:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-03-28 13:33:44 +00:00
|
|
|
if( boardItem->Type() == PCB_GROUP_T )
|
|
|
|
{
|
|
|
|
copy = static_cast<PCB_GROUP*>( boardItem )->DeepClone();
|
|
|
|
}
|
|
|
|
else if( boardItem->Type() == PCB_GENERATOR_T )
|
|
|
|
{
|
|
|
|
copy = static_cast<PCB_GENERATOR*>( boardItem )->DeepClone();
|
|
|
|
}
|
|
|
|
else if( item->Type() == PCB_TABLECELL_T )
|
|
|
|
{
|
|
|
|
if( parentIsPromoted( static_cast<PCB_TABLECELL*>( item ) ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
copy = static_cast<BOARD_ITEM*>( item->GetParent()->Clone() );
|
|
|
|
promotedTables.insert( static_cast<PCB_TABLE*>( copy ) );
|
|
|
|
}
|
2020-10-20 11:37:38 +00:00
|
|
|
else
|
2024-03-28 13:33:44 +00:00
|
|
|
{
|
|
|
|
copy = static_cast<BOARD_ITEM*>( boardItem->Clone() );
|
|
|
|
}
|
2017-05-18 17:21:30 +00:00
|
|
|
|
2020-11-10 21:20:03 +00:00
|
|
|
// If it is only a footprint, clear the nets from the pads
|
2024-03-28 13:33:44 +00:00
|
|
|
if( PAD* pad = dynamic_cast<PAD*>( copy ) )
|
2019-11-03 16:09:23 +00:00
|
|
|
pad->SetNetCode( 0 );
|
2017-05-18 17:21:30 +00:00
|
|
|
|
2024-03-28 13:33:44 +00:00
|
|
|
// Don't copy group membership information for the 1st level objects being copied
|
|
|
|
// since the group they belong to isn't being copied.
|
|
|
|
copy->SetParentGroup( nullptr );
|
2023-06-14 22:33:41 +00:00
|
|
|
|
2020-11-10 21:20:03 +00:00
|
|
|
// Add the pad to the new footprint before moving to ensure the local coords are
|
|
|
|
// correct
|
2024-03-28 13:33:44 +00:00
|
|
|
partialFootprint.Add( copy );
|
2019-01-13 21:20:29 +00:00
|
|
|
|
2021-08-18 13:34:32 +00:00
|
|
|
// A list of not added items, when adding items to the footprint
|
2023-03-30 11:49:23 +00:00
|
|
|
// some PCB_TEXT (reference and value) cannot be added to the footprint
|
2021-08-18 13:34:32 +00:00
|
|
|
std::vector<BOARD_ITEM*> skipped_items;
|
|
|
|
|
2024-03-28 13:33:44 +00:00
|
|
|
if( copy->Type() == PCB_GROUP_T || copy->Type() == PCB_GENERATOR_T )
|
2020-10-20 11:37:38 +00:00
|
|
|
{
|
2024-03-28 13:33:44 +00:00
|
|
|
copy->RunOnDescendants(
|
2020-10-20 11:37:38 +00:00
|
|
|
[&]( BOARD_ITEM* descendant )
|
|
|
|
{
|
2023-06-06 17:30:03 +00:00
|
|
|
// One cannot add an additional mandatory field to a given footprint:
|
|
|
|
// only one is allowed. So add only non-mandatory fields.
|
2021-08-18 13:34:32 +00:00
|
|
|
bool can_add = true;
|
|
|
|
|
2023-06-24 14:48:27 +00:00
|
|
|
if( const PCB_FIELD* field = dynamic_cast<const PCB_FIELD*>( item ) )
|
2021-08-18 13:34:32 +00:00
|
|
|
{
|
2023-06-06 17:30:03 +00:00
|
|
|
if( field->IsMandatoryField() )
|
2021-08-18 13:34:32 +00:00
|
|
|
can_add = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( can_add )
|
|
|
|
partialFootprint.Add( descendant );
|
|
|
|
else
|
|
|
|
skipped_items.push_back( descendant );
|
2020-10-20 11:37:38 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
// locate the reference point at (0, 0) in the copied items
|
2024-03-28 13:33:44 +00:00
|
|
|
copy->Move( -refPoint );
|
2021-08-18 13:34:32 +00:00
|
|
|
|
|
|
|
// Now delete items, duplicated but not added:
|
2024-03-28 13:33:44 +00:00
|
|
|
for( BOARD_ITEM* skipped_item : skipped_items )
|
2023-06-14 22:33:41 +00:00
|
|
|
{
|
2024-03-28 13:33:44 +00:00
|
|
|
static_cast<PCB_GROUP*>( copy )->RemoveItem( skipped_item );
|
|
|
|
skipped_item->SetParentGroup( nullptr );
|
|
|
|
delete skipped_item;
|
2023-06-14 22:33:41 +00:00
|
|
|
}
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the new relative internal local coordinates of copied items
|
2020-11-13 15:15:52 +00:00
|
|
|
FOOTPRINT* editedFootprint = m_board->Footprints().front();
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I moveVector = partialFootprint.GetPosition() + editedFootprint->GetPosition();
|
2017-05-18 17:21:30 +00:00
|
|
|
|
2020-11-10 21:20:03 +00:00
|
|
|
partialFootprint.MoveAnchorPosition( moveVector );
|
2017-05-18 17:21:30 +00:00
|
|
|
|
2024-03-28 13:33:44 +00:00
|
|
|
for( PCB_TABLE* table : promotedTables )
|
|
|
|
deleteUnselectedCells( table );
|
|
|
|
|
2020-11-10 21:20:03 +00:00
|
|
|
Format( &partialFootprint, 0 );
|
2023-01-18 00:40:14 +00:00
|
|
|
|
|
|
|
partialFootprint.SetParent( nullptr );
|
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
|
2018-11-22 16:12:41 +00:00
|
|
|
LOCALE_IO io;
|
|
|
|
|
2023-11-18 22:34:09 +00:00
|
|
|
m_formatter.Print( 0, "(kicad_pcb (version %d) (generator \"pcbnew\") (generator_version \"%s\")\n",
|
|
|
|
SEXPR_BOARD_FILE_VERSION, GetMajorMinorVersion().c_str().AsChar() );
|
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" );
|
|
|
|
|
2023-07-13 10:24:33 +00:00
|
|
|
for( EDA_ITEM* item : aSelected )
|
2017-05-18 17:21:30 +00:00
|
|
|
{
|
2023-07-13 10:24:33 +00:00
|
|
|
BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( item );
|
2020-07-27 17:56:15 +00:00
|
|
|
BOARD_ITEM* copy = nullptr;
|
|
|
|
|
2023-07-13 10:24:33 +00:00
|
|
|
wxCHECK2( boardItem, continue );
|
|
|
|
|
|
|
|
if( boardItem->Type() == PCB_FIELD_T || boardItem->Type() == PCB_TEXT_T )
|
2017-05-18 17:21:30 +00:00
|
|
|
{
|
2023-07-13 10:24:33 +00:00
|
|
|
copy = static_cast<BOARD_ITEM*>( boardItem->Clone() );
|
2022-01-30 10:52:52 +00:00
|
|
|
|
2023-03-30 11:49:23 +00:00
|
|
|
PCB_TEXT* textItem = static_cast<PCB_TEXT*>( copy );
|
2022-01-30 10:52:52 +00:00
|
|
|
|
2023-03-30 11:49:23 +00:00
|
|
|
if( textItem->GetText() == wxT( "${VALUE}" ) )
|
2023-07-13 10:24:33 +00:00
|
|
|
textItem->SetText( boardItem->GetParentFootprint()->GetValue() );
|
2023-03-30 11:49:23 +00:00
|
|
|
else if( textItem->GetText() == wxT( "${REFERENCE}" ) )
|
2023-07-13 10:24:33 +00:00
|
|
|
textItem->SetText( boardItem->GetParentFootprint()->GetReference() );
|
2022-01-30 10:52:52 +00:00
|
|
|
}
|
2023-07-13 10:24:33 +00:00
|
|
|
else if( boardItem->Type() == PCB_GROUP_T )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2023-07-13 10:24:33 +00:00
|
|
|
copy = static_cast<PCB_GROUP*>( boardItem )->DeepClone();
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
2024-01-16 23:37:12 +00:00
|
|
|
else if( boardItem->Type() == PCB_GENERATOR_T )
|
|
|
|
{
|
|
|
|
copy = static_cast<PCB_GENERATOR*>( boardItem )->DeepClone();
|
|
|
|
}
|
2024-03-28 13:33:44 +00:00
|
|
|
else if( item->Type() == PCB_TABLECELL_T )
|
|
|
|
{
|
|
|
|
if( parentIsPromoted( static_cast<PCB_TABLECELL*>( item ) ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
copy = static_cast<BOARD_ITEM*>( item->GetParent()->Clone() );
|
|
|
|
promotedTables.insert( static_cast<PCB_TABLE*>( copy ) );
|
|
|
|
}
|
2020-07-27 17:56:15 +00:00
|
|
|
else
|
|
|
|
{
|
2023-07-13 10:24:33 +00:00
|
|
|
copy = static_cast<BOARD_ITEM*>( boardItem->Clone() );
|
2020-07-27 17:56:15 +00:00
|
|
|
}
|
2019-11-03 16:09:23 +00:00
|
|
|
|
2020-07-27 17:56:15 +00:00
|
|
|
if( copy )
|
|
|
|
{
|
2024-02-16 10:21:44 +00:00
|
|
|
if( copy->Type() == PCB_FIELD_T || copy->Type() == PCB_PAD_T )
|
|
|
|
{
|
|
|
|
// Create a parent footprint to own the copied item
|
|
|
|
FOOTPRINT* footprint = new FOOTPRINT( m_board );
|
|
|
|
|
|
|
|
footprint->SetPosition( copy->GetPosition() );
|
|
|
|
footprint->Add( copy );
|
|
|
|
|
2024-02-16 13:27:11 +00:00
|
|
|
// Convert any mandatory fields to user fields. The destination footprint
|
|
|
|
// will already have its own mandatory fields.
|
2024-02-16 10:21:44 +00:00
|
|
|
if( PCB_FIELD* field = dynamic_cast<PCB_FIELD*>( copy ) )
|
|
|
|
{
|
|
|
|
if( field->IsMandatoryField() )
|
|
|
|
field->SetId( footprint->GetFieldCount() );
|
|
|
|
}
|
|
|
|
|
|
|
|
copy = footprint;
|
|
|
|
}
|
|
|
|
|
2023-11-09 13:55:00 +00:00
|
|
|
copy->SetLocked( false );
|
2020-08-11 19:37:07 +00:00
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
// locate the reference point at (0, 0) in the copied items
|
2022-01-02 02:06:40 +00:00
|
|
|
copy->Move( -refPoint );
|
2020-07-27 17:56:15 +00:00
|
|
|
|
2024-03-28 13:33:44 +00:00
|
|
|
if( copy->Type() == PCB_TABLE_T )
|
|
|
|
{
|
|
|
|
PCB_TABLE* table = static_cast<PCB_TABLE*>( copy );
|
|
|
|
|
|
|
|
if( promotedTables.count( table ) )
|
|
|
|
deleteUnselectedCells( table );
|
|
|
|
}
|
|
|
|
|
2020-07-27 17:56:15 +00:00
|
|
|
Format( copy, 1 );
|
2017-09-22 15:17:38 +00:00
|
|
|
|
2024-01-16 23:37:12 +00:00
|
|
|
if( copy->Type() == PCB_GROUP_T || copy->Type() == PCB_GENERATOR_T )
|
2020-08-11 19:37:07 +00:00
|
|
|
{
|
2023-11-09 13:55:00 +00:00
|
|
|
copy->RunOnDescendants(
|
2024-03-28 13:33:44 +00:00
|
|
|
[&]( BOARD_ITEM* descendant )
|
2023-11-09 13:55:00 +00:00
|
|
|
{
|
2024-03-28 13:33:44 +00:00
|
|
|
descendant->SetLocked( false );
|
|
|
|
Format( descendant, 1 );
|
2023-11-09 13:55:00 +00:00
|
|
|
} );
|
2020-08-11 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 22:33:41 +00:00
|
|
|
copy->SetParentGroup( nullptr );
|
2020-07-27 17:56:15 +00:00
|
|
|
delete copy;
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-28 13:33:44 +00:00
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
m_formatter.Print( 0, "\n)" );
|
|
|
|
}
|
2018-11-02 04:12:34 +00:00
|
|
|
|
2018-11-22 16:12:41 +00:00
|
|
|
// These are placed at the end to minimize the open time of the clipboard
|
2021-01-29 19:13:12 +00:00
|
|
|
wxLogNull doNotLog; // disable logging of failed clipboard actions
|
2018-11-22 16:12:41 +00:00
|
|
|
auto clipboard = wxTheClipboard;
|
|
|
|
wxClipboardLocker clipboardLock( clipboard );
|
|
|
|
|
|
|
|
if( !clipboardLock || !clipboard->IsOpened() )
|
|
|
|
return;
|
|
|
|
|
2020-10-31 17:25:08 +00:00
|
|
|
clipboard->SetData( new wxTextDataObject( wxString( m_formatter.GetString().c_str(),
|
|
|
|
wxConvUTF8 ) ) );
|
2018-11-02 04:12:34 +00:00
|
|
|
|
|
|
|
clipboard->Flush();
|
2018-11-22 16:12:41 +00:00
|
|
|
|
2021-01-30 16:17:52 +00:00
|
|
|
#ifndef __WXOSX__
|
2018-11-22 16:12:41 +00:00
|
|
|
// This section exists to return the clipboard data, ensuring it has fully
|
|
|
|
// been processed by the system clipboard. This appears to be needed for
|
|
|
|
// extremely large clipboard copies on asynchronous linux clipboard managers
|
2020-04-03 14:54:09 +00:00
|
|
|
// such as KDE's Klipper. However, a read back of the data on OSX before the
|
|
|
|
// clipboard is closed seems to cause an ASAN error (heap-buffer-overflow)
|
|
|
|
// since it uses the cached version of the clipboard data and not the system
|
|
|
|
// clipboard data.
|
2022-01-12 09:24:04 +00:00
|
|
|
if( clipboard->IsSupported( wxDF_TEXT ) || clipboard->IsSupported( wxDF_UNICODETEXT ) )
|
2018-11-22 16:12:41 +00:00
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
clipboard->GetData( data );
|
2021-10-05 16:57:25 +00:00
|
|
|
ignore_unused( data.GetText() );
|
2018-11-22 16:12:41 +00:00
|
|
|
}
|
2021-01-30 16:17:52 +00:00
|
|
|
#endif
|
2017-04-15 16:08:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
BOARD_ITEM* CLIPBOARD_IO::Parse()
|
|
|
|
{
|
2018-03-13 08:26:34 +00:00
|
|
|
BOARD_ITEM* item;
|
|
|
|
wxString result;
|
2017-05-18 17:21:30 +00:00
|
|
|
|
2021-01-29 19:13:12 +00:00
|
|
|
wxLogNull doNotLog; // disable logging of failed clipboard actions
|
|
|
|
|
2018-11-02 04:12:34 +00:00
|
|
|
auto clipboard = wxTheClipboard;
|
|
|
|
wxClipboardLocker clipboardLock( clipboard );
|
|
|
|
|
|
|
|
if( !clipboardLock )
|
|
|
|
return nullptr;
|
2017-05-18 17:21:30 +00:00
|
|
|
|
2022-01-12 09:24:04 +00:00
|
|
|
if( clipboard->IsSupported( wxDF_TEXT ) || clipboard->IsSupported( wxDF_UNICODETEXT ) )
|
2018-11-02 04:12:34 +00:00
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
clipboard->GetData( data );
|
|
|
|
result = data.GetText();
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
try
|
|
|
|
{
|
2023-12-24 01:21:58 +00:00
|
|
|
item = PCB_IO_KICAD_SEXPR::Parse( result );
|
2018-03-13 08:26:34 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2017-09-22 15:17:38 +00:00
|
|
|
item = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
2017-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2023-08-13 02:11:58 +00:00
|
|
|
void CLIPBOARD_IO::SaveBoard( const wxString& aFileName, BOARD* aBoard,
|
2023-12-27 17:06:23 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2023-11-18 22:34:09 +00:00
|
|
|
m_out->Print( 0, "(kicad_pcb (version %d) (generator \"pcbnew\") (generator_version \"%s\")\n", SEXPR_BOARD_FILE_VERSION, GetMajorMinorVersion().c_str().AsChar() );
|
2017-04-15 16:08:23 +00:00
|
|
|
|
|
|
|
Format( aBoard, 1 );
|
|
|
|
|
|
|
|
m_out->Print( 0, ")\n" );
|
|
|
|
|
2021-01-29 19:13:12 +00:00
|
|
|
wxLogNull doNotLog; // disable logging of failed clipboard actions
|
|
|
|
|
2018-11-22 16:12:41 +00:00
|
|
|
auto clipboard = wxTheClipboard;
|
|
|
|
wxClipboardLocker clipboardLock( clipboard );
|
|
|
|
|
|
|
|
if( !clipboardLock )
|
|
|
|
return;
|
|
|
|
|
2018-11-02 04:12:34 +00:00
|
|
|
clipboard->SetData( new wxTextDataObject(
|
|
|
|
wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) );
|
|
|
|
clipboard->Flush();
|
2018-11-22 16:12:41 +00:00
|
|
|
|
|
|
|
// This section exists to return the clipboard data, ensuring it has fully
|
|
|
|
// been processed by the system clipboard. This appears to be needed for
|
|
|
|
// extremely large clipboard copies on asynchronous linux clipboard managers
|
|
|
|
// such as KDE's Klipper
|
2022-01-12 09:24:04 +00:00
|
|
|
if( clipboard->IsSupported( wxDF_TEXT ) || clipboard->IsSupported( wxDF_UNICODETEXT ) )
|
2018-11-22 16:12:41 +00:00
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
clipboard->GetData( data );
|
2021-10-05 16:57:25 +00:00
|
|
|
ignore_unused( data.GetText() );
|
2018-11-22 16:12:41 +00:00
|
|
|
}
|
2017-04-15 16:08:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 09:44:05 +00:00
|
|
|
|
2023-08-13 02:11:58 +00:00
|
|
|
BOARD* CLIPBOARD_IO::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
|
2023-12-27 17:06:23 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
|
2017-04-15 16:08:23 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
2021-01-29 19:13:12 +00:00
|
|
|
wxLogNull doNotLog; // disable logging of failed clipboard actions
|
|
|
|
|
2018-11-02 04:12:34 +00:00
|
|
|
auto clipboard = wxTheClipboard;
|
|
|
|
wxClipboardLocker clipboardLock( clipboard );
|
2017-04-15 16:08:23 +00:00
|
|
|
|
2018-11-02 04:12:34 +00:00
|
|
|
if( !clipboardLock )
|
|
|
|
return nullptr;
|
|
|
|
|
2022-01-12 09:24:04 +00:00
|
|
|
if( clipboard->IsSupported( wxDF_TEXT ) || clipboard->IsSupported( wxDF_UNICODETEXT ) )
|
2018-11-02 04:12:34 +00:00
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
clipboard->GetData( data );
|
2017-04-15 16:08:23 +00:00
|
|
|
|
2018-11-02 04:12:34 +00:00
|
|
|
result = data.GetText().mb_str();
|
2017-04-15 16:08:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 21:38:31 +00:00
|
|
|
std::function<bool( wxString, int, wxString, wxString )> queryUser =
|
|
|
|
[&]( wxString aTitle, int aIcon, wxString aMessage, wxString aAction ) -> bool
|
|
|
|
{
|
|
|
|
KIDIALOG dlg( nullptr, aMessage, aTitle, wxOK | wxCANCEL | aIcon );
|
|
|
|
|
|
|
|
if( !aAction.IsEmpty() )
|
|
|
|
dlg.SetOKLabel( aAction );
|
|
|
|
|
|
|
|
dlg.DoNotShowCheckbox( aMessage, 0 );
|
|
|
|
|
|
|
|
return dlg.ShowModal() == wxID_OK;
|
|
|
|
};
|
|
|
|
|
2021-10-31 19:57:52 +00:00
|
|
|
STRING_LINE_READER reader( result, wxT( "clipboard" ) );
|
2023-12-24 01:21:58 +00:00
|
|
|
PCB_IO_KICAD_SEXPR_PARSER parser( &reader, aAppendToMe, queryUser );
|
2017-04-15 16:08:23 +00:00
|
|
|
|
|
|
|
init( aProperties );
|
|
|
|
|
2017-05-18 17:21:30 +00:00
|
|
|
BOARD_ITEM* item;
|
2017-04-15 16:08:23 +00:00
|
|
|
BOARD* board;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-10-31 19:57:52 +00:00
|
|
|
item = 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 )
|
|
|
|
{
|
2021-10-31 19:57:52 +00:00
|
|
|
if( parser.IsTooRecent() )
|
|
|
|
throw FUTURE_FORMAT_ERROR( parse_error, parser.GetRequiredVersion() );
|
2017-04-15 16:08:23 +00:00
|
|
|
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.
|
2021-10-31 19:57:52 +00:00
|
|
|
THROW_PARSE_ERROR( _( "Clipboard content is not KiCad compatible" ), parser.CurSource(),
|
|
|
|
parser.CurLine(), parser.CurLineNumber(), parser.CurOffset() );
|
2017-04-15 16:08:23 +00:00
|
|
|
}
|
2017-05-18 17:21:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
board = dynamic_cast<BOARD*>( item );
|
|
|
|
}
|
2017-11-01 09:20:13 +00:00
|
|
|
|
2017-04-15 16:08:23 +00:00
|
|
|
// Give the filename to the board if it's new
|
2017-12-04 09:20:05 +00:00
|
|
|
if( board && !aAppendToMe )
|
2017-04-15 16:08:23 +00:00
|
|
|
board->SetFileName( aFileName );
|
|
|
|
|
|
|
|
return board;
|
|
|
|
}
|