KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_clipboard.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 KiCad Developers, see AUTHORS.TXT for contributors.
5 * Copyright (C) 2017-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Kristoffer Ödmark
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <wx/clipbrd.h>
27#include <wx/log.h>
28
29#include <board.h>
30#include <core/ignore.h>
31#include <pad.h>
32#include <pcb_group.h>
33#include <pcb_shape.h>
34#include <pcb_text.h>
35#include <pcb_textbox.h>
36#include <zone.h>
37#include <locale_io.h>
38#include <netinfo.h>
40
42#include <kicad_clipboard.h>
43#include "confirm.h"
44
47 m_formatter()
48{
50}
51
52
54{
55}
56
57
59{
60 m_board = aBoard;
61}
62
63
64void CLIPBOARD_IO::SaveSelection( const PCB_SELECTION& aSelected, bool isFootprintEditor )
65{
66 VECTOR2I refPoint( 0, 0 );
67
68 // dont even start if the selection is empty
69 if( aSelected.Empty() )
70 return;
71
72 if( aSelected.HasReferencePoint() )
73 refPoint = aSelected.GetReferencePoint();
74
75 // Prepare net mapping that assures that net codes saved in a file are consecutive integers
77
78 if( aSelected.Size() == 1 && aSelected.Front()->Type() == PCB_FOOTPRINT_T )
79 {
80 // make the footprint safe to transfer to other pcbs
81 const FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aSelected.Front() );
82 // Do not modify existing board
83 FOOTPRINT newFootprint( *footprint );
84
85 for( PAD* pad : newFootprint.Pads() )
86 pad->SetNetCode( 0 );
87
88 // locked means "locked in place"; copied items therefore can't be locked
89 newFootprint.SetLocked( false );
90
91 // locate the reference point at (0, 0) in the copied items
92 newFootprint.Move( VECTOR2I( -refPoint.x, -refPoint.y ) );
93
94 Format( static_cast<BOARD_ITEM*>( &newFootprint ) );
95
96 newFootprint.SetParent( nullptr );
97 }
98 else if( isFootprintEditor )
99 {
100 FOOTPRINT partialFootprint( m_board );
101
102 // Useful to copy the selection to the board editor (if any), and provides
103 // a dummy lib id.
104 // Perhaps not a good Id, but better than a empty id
105 KIID dummy;
106 LIB_ID id( "clipboard", dummy.AsString() );
107 partialFootprint.SetFPID( id );
108
109 for( const EDA_ITEM* item : aSelected )
110 {
111 const PCB_GROUP* group = dynamic_cast<const PCB_GROUP*>( item );
112 BOARD_ITEM* clone;
113
114 if( const PCB_TEXT* text = dyn_cast<const PCB_TEXT*>( item ) )
115 {
116 if( text->GetType() != PCB_TEXT::TEXT_is_DIVERS )
117 continue;
118 }
119
120 if( group )
121 clone = static_cast<BOARD_ITEM*>( group->DeepClone() );
122 else
123 clone = static_cast<BOARD_ITEM*>( item->Clone() );
124
125 // If it is only a footprint, clear the nets from the pads
126 if( PAD* pad = dyn_cast<PAD*>( clone ) )
127 pad->SetNetCode( 0 );
128
129 // Add the pad to the new footprint before moving to ensure the local coords are
130 // correct
131 partialFootprint.Add( clone );
132
133 // A list of not added items, when adding items to the footprint
134 // some PCB_TEXT (reference and value) cannot be added to the footprint
135 std::vector<BOARD_ITEM*> skipped_items;
136
137 if( group )
138 {
139 static_cast<PCB_GROUP*>( clone )->RunOnDescendants(
140 [&]( BOARD_ITEM* descendant )
141 {
142 // One cannot add a text reference or value to a given footprint:
143 // only one is allowed. So add only PCB_TEXT::TEXT_is_DIVERS
144 bool can_add = true;
145
146 if( const PCB_TEXT* text = dyn_cast<const PCB_TEXT*>( descendant ) )
147 {
148 if( text->GetType() != PCB_TEXT::TEXT_is_DIVERS )
149 can_add = false;
150 }
151
152 if( can_add )
153 partialFootprint.Add( descendant );
154 else
155 skipped_items.push_back( descendant );
156 } );
157 }
158
159 // locate the reference point at (0, 0) in the copied items
160 clone->Move( -refPoint );
161
162 // Now delete items, duplicated but not added:
163 for( BOARD_ITEM* skp_item : skipped_items )
164 delete skp_item;
165 }
166
167 // Set the new relative internal local coordinates of copied items
168 FOOTPRINT* editedFootprint = m_board->Footprints().front();
169 VECTOR2I moveVector = partialFootprint.GetPosition() + editedFootprint->GetPosition();
170
171 partialFootprint.MoveAnchorPosition( moveVector );
172
173 Format( &partialFootprint, 0 );
174
175 partialFootprint.SetParent( nullptr );
176 }
177 else
178 {
179 // we will fake being a .kicad_pcb to get the full parser kicking
180 // This means we also need layers and nets
181 LOCALE_IO io;
182
183 m_formatter.Print( 0, "(kicad_pcb (version %d) (generator pcbnew)\n",
185
186 m_formatter.Print( 0, "\n" );
187
190
191 m_formatter.Print( 0, "\n" );
192
193 for( EDA_ITEM* i : aSelected )
194 {
195 BOARD_ITEM* item = static_cast<BOARD_ITEM*>( i );
196 BOARD_ITEM* copy = nullptr;
197
198 if( item->Type() == PCB_TEXT_T )
199 {
200 copy = static_cast<BOARD_ITEM*>( item->Clone() );
201
202 PCB_TEXT* textItem = static_cast<PCB_TEXT*>( copy );
203
204 if( textItem->GetText() == wxT( "${VALUE}" ) )
205 textItem->SetText( item->GetParentFootprint()->GetValue() );
206 else if( textItem->GetText() == wxT( "${REFERENCE}" ) )
207 textItem->SetText( item->GetParentFootprint()->GetReference() );
208 }
209 else if( item->Type() == PCB_PAD_T )
210 {
211 // Create a parent to own the copied pad
212 FOOTPRINT* footprint = new FOOTPRINT( m_board );
213 PAD* pad = (PAD*) item->Clone();
214
215 footprint->SetPosition( pad->GetPosition() );
216 footprint->Add( pad );
217 copy = footprint;
218 }
219 else if( item->Type() == PCB_GROUP_T )
220 {
221 copy = static_cast<PCB_GROUP*>( item )->DeepClone();
222 }
223 else
224 {
225 copy = static_cast<BOARD_ITEM*>( item->Clone() );
226 }
227
228 auto prepItem = [&]( BOARD_ITEM* aItem )
229 {
230 aItem->SetLocked( false );
231 };
232
233 if( copy )
234 {
235 prepItem( copy );
236
237 // locate the reference point at (0, 0) in the copied items
238 copy->Move( -refPoint );
239
240 Format( copy, 1 );
241
242 if( copy->Type() == PCB_GROUP_T )
243 {
244 static_cast<PCB_GROUP*>( copy )->RunOnDescendants( prepItem );
245 static_cast<PCB_GROUP*>( copy )->RunOnDescendants( [&]( BOARD_ITEM* titem )
246 {
247 Format( titem, 1 );
248 } );
249 }
250
251 delete copy;
252 }
253 }
254 m_formatter.Print( 0, "\n)" );
255 }
256
257 // These are placed at the end to minimize the open time of the clipboard
258 wxLogNull doNotLog; // disable logging of failed clipboard actions
259 auto clipboard = wxTheClipboard;
260 wxClipboardLocker clipboardLock( clipboard );
261
262 if( !clipboardLock || !clipboard->IsOpened() )
263 return;
264
265 clipboard->SetData( new wxTextDataObject( wxString( m_formatter.GetString().c_str(),
266 wxConvUTF8 ) ) );
267
268 clipboard->Flush();
269
270 #ifndef __WXOSX__
271 // This section exists to return the clipboard data, ensuring it has fully
272 // been processed by the system clipboard. This appears to be needed for
273 // extremely large clipboard copies on asynchronous linux clipboard managers
274 // such as KDE's Klipper. However, a read back of the data on OSX before the
275 // clipboard is closed seems to cause an ASAN error (heap-buffer-overflow)
276 // since it uses the cached version of the clipboard data and not the system
277 // clipboard data.
278 if( clipboard->IsSupported( wxDF_TEXT ) || clipboard->IsSupported( wxDF_UNICODETEXT ) )
279 {
280 wxTextDataObject data;
281 clipboard->GetData( data );
282 ignore_unused( data.GetText() );
283 }
284 #endif
285}
286
287
289{
290 BOARD_ITEM* item;
291 wxString result;
292
293 wxLogNull doNotLog; // disable logging of failed clipboard actions
294
295 auto clipboard = wxTheClipboard;
296 wxClipboardLocker clipboardLock( clipboard );
297
298 if( !clipboardLock )
299 return nullptr;
300
301 if( clipboard->IsSupported( wxDF_TEXT ) || clipboard->IsSupported( wxDF_UNICODETEXT ) )
302 {
303 wxTextDataObject data;
304 clipboard->GetData( data );
305 result = data.GetText();
306 }
307
308 try
309 {
310 item = PCB_PLUGIN::Parse( result );
311 }
312 catch (...)
313 {
314 item = nullptr;
315 }
316
317 return item;
318}
319
320
321void CLIPBOARD_IO::Save( const wxString& aFileName, BOARD* aBoard,
322 const STRING_UTF8_MAP* aProperties )
323{
324 init( aProperties );
325
326 m_board = aBoard; // after init()
327
328 // Prepare net mapping that assures that net codes saved in a file are consecutive integers
329 m_mapping->SetBoard( aBoard );
330
331 STRING_FORMATTER formatter;
332
333 m_out = &formatter;
334
335 m_out->Print( 0, "(kicad_pcb (version %d) (generator pcbnew)\n", SEXPR_BOARD_FILE_VERSION );
336
337 Format( aBoard, 1 );
338
339 m_out->Print( 0, ")\n" );
340
341 wxLogNull doNotLog; // disable logging of failed clipboard actions
342
343 auto clipboard = wxTheClipboard;
344 wxClipboardLocker clipboardLock( clipboard );
345
346 if( !clipboardLock )
347 return;
348
349 clipboard->SetData( new wxTextDataObject(
350 wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) );
351 clipboard->Flush();
352
353 // This section exists to return the clipboard data, ensuring it has fully
354 // been processed by the system clipboard. This appears to be needed for
355 // extremely large clipboard copies on asynchronous linux clipboard managers
356 // such as KDE's Klipper
357 if( clipboard->IsSupported( wxDF_TEXT ) || clipboard->IsSupported( wxDF_UNICODETEXT ) )
358 {
359 wxTextDataObject data;
360 clipboard->GetData( data );
361 ignore_unused( data.GetText() );
362 }
363}
364
365
366BOARD* CLIPBOARD_IO::Load( const wxString& aFileName, BOARD* aAppendToMe,
367 const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
368 PROGRESS_REPORTER* aProgressReporter )
369{
370 std::string result;
371
372 wxLogNull doNotLog; // disable logging of failed clipboard actions
373
374 auto clipboard = wxTheClipboard;
375 wxClipboardLocker clipboardLock( clipboard );
376
377 if( !clipboardLock )
378 return nullptr;
379
380 if( clipboard->IsSupported( wxDF_TEXT ) || clipboard->IsSupported( wxDF_UNICODETEXT ) )
381 {
382 wxTextDataObject data;
383 clipboard->GetData( data );
384
385 result = data.GetText().mb_str();
386 }
387
388 std::function<bool( wxString, int, wxString, wxString )> queryUser =
389 [&]( wxString aTitle, int aIcon, wxString aMessage, wxString aAction ) -> bool
390 {
391 KIDIALOG dlg( nullptr, aMessage, aTitle, wxOK | wxCANCEL | aIcon );
392
393 if( !aAction.IsEmpty() )
394 dlg.SetOKLabel( aAction );
395
396 dlg.DoNotShowCheckbox( aMessage, 0 );
397
398 return dlg.ShowModal() == wxID_OK;
399 };
400
401 STRING_LINE_READER reader( result, wxT( "clipboard" ) );
402 PCB_PARSER parser( &reader, aAppendToMe, queryUser );
403
404 init( aProperties );
405
406 BOARD_ITEM* item;
407 BOARD* board;
408
409 try
410 {
411 item = parser.Parse();
412 }
413 catch( const FUTURE_FORMAT_ERROR& )
414 {
415 // Don't wrap a FUTURE_FORMAT_ERROR in another
416 throw;
417 }
418 catch( const PARSE_ERROR& parse_error )
419 {
420 if( parser.IsTooRecent() )
421 throw FUTURE_FORMAT_ERROR( parse_error, parser.GetRequiredVersion() );
422 else
423 throw;
424 }
425
426 if( item->Type() != PCB_T )
427 {
428 // The parser loaded something that was valid, but wasn't a board.
429 THROW_PARSE_ERROR( _( "Clipboard content is not KiCad compatible" ), parser.CurSource(),
430 parser.CurLine(), parser.CurLineNumber(), parser.CurOffset() );
431 }
432 else
433 {
434 board = dynamic_cast<BOARD*>( item );
435 }
436
437 // Give the filename to the board if it's new
438 if( board && !aAppendToMe )
439 board->SetFileName( aFileName );
440
441 return board;
442}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:71
virtual void SetLocked(bool aLocked)
Definition: board_item.h:270
virtual void Move(const VECTOR2I &aMoveVector)
Move this object.
Definition: board_item.h:282
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:240
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:270
void SetFileName(const wxString &aFileName)
Definition: board.h:305
FOOTPRINTS & Footprints()
Definition: board.h:312
void SaveSelection(const PCB_SELECTION &selected, bool isFootprintEditor)
STRING_FORMATTER m_formatter
BOARD_ITEM * Parse()
BOARD * Load(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
Load information from some input file format that this PLUGIN implementation knows about into either ...
void SetBoard(BOARD *aBoard)
void Save(const wxString &aFileName, BOARD *aBoard, const STRING_UTF8_MAP *aProperties=nullptr) override
Write aBoard to a storage file in a format that this PLUGIN implementation knows about or it can be u...
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:100
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:81
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:87
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:175
void SetPosition(const VECTOR2I &aPos) override
Definition: footprint.cpp:1656
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:215
void SetLocked(bool isLocked) override
Set the #MODULE_is_LOCKED bit in the m_ModuleStatus.
Definition: footprint.h:352
void MoveAnchorPosition(const VECTOR2I &aMoveVector)
Move the reference point of the footprint.
Definition: footprint.cpp:1683
PADS & Pads()
Definition: footprint.h:172
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: footprint.cpp:1554
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: footprint.cpp:568
const wxString & GetValue() const
Definition: footprint.h:549
const wxString & GetReference() const
Definition: footprint.h:521
VECTOR2I GetPosition() const override
Definition: footprint.h:190
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:46
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: confirm.cpp:59
int ShowModal() override
Definition: confirm.cpp:103
Definition: kiid.h:48
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
void SetBoard(const BOARD *aBoard)
Set a BOARD object that is used to prepare the net code map.
Definition: netinfo.h:215
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:475
Definition: pad.h:59
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:51
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
Definition: pcb_parser.h:74
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
Definition: pcb_parser.h:105
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:743
wxString GetRequiredVersion()
Return a string representing the version of KiCad required to open this file.
Definition: pcb_parser.cpp:227
A PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
Definition: pcb_plugin.h:261
BOARD * m_board
which BOARD, no ownership here
Definition: pcb_plugin.h:415
void init(const STRING_UTF8_MAP *aProperties)
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
Definition: pcb_plugin.cpp:309
OUTPUTFORMATTER * m_out
output any Format()s to this, no ownership
Definition: pcb_plugin.h:424
NETINFO_MAPPING * m_mapping
mapping for net codes, so only not empty net codes are stored with consecutive integers as net codes
Definition: pcb_plugin.h:426
void formatNetInformation(const BOARD *aBoard, int aNestLevel=0) const
formats the Nets and Netclasses
Definition: pcb_plugin.cpp:663
void formatBoardLayers(const BOARD *aBoard, int aNestLevel=0) const
formats the board layer information
Definition: pcb_plugin.cpp:593
void Format(const BOARD_ITEM *aItem, int aNestLevel=0) const
Output aItem to aFormatter in s-expression format.
Definition: pcb_plugin.cpp:330
@ TEXT_is_DIVERS
Definition: pcb_text.h:52
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition: project.h:64
VECTOR2I GetReferencePoint() const
Definition: selection.h:252
EDA_ITEM * Front() const
Definition: selection.h:208
int Size() const
Returns the number of selected parts.
Definition: selection.h:115
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:109
bool HasReferencePoint() const
Definition: selection.h:247
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:427
const std::string & GetString()
Definition: richio.h:450
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:253
A name/value tuple with unique names and optional values.
This file is part of the common library.
#define _(s)
void ignore_unused(const T &)
Definition: ignore.h:24
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
Class to handle a set of BOARD_ITEMs.
Pcbnew s-expression file format parser definition.
#define CTL_FOR_CLIPBOARD
Format output for the clipboard instead of footprint library or BOARD.
Definition: pcb_plugin.h:155
#define SEXPR_BOARD_FILE_VERSION
Current s-expression file format version. 2 was the last legacy format version.
Definition: pcb_plugin.h:133
std::vector< FAB_LAYER_COLOR > dummy
Variant of PARSE_ERROR indicating that a syntax or related error was likely caused by a file generate...
Definition: ki_exception.h:175
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:119
@ PCB_T
Definition: typeinfo.h:82
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:106
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:90
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588