KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ds_data_model_io.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) 2013-2016 CERN
5 * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <build_version.h>
28#include <string_utils.h>
29#include <locale_io.h>
30#include <math/vector2d.h>
34#include <drawing_sheet/drawing_sheet_lexer.h>
36#include <font/font.h>
37#include <string_utils.h>
38
39#include <wx/base64.h>
40#include <wx/msgdlg.h>
41
42using namespace DRAWINGSHEET_T;
43
44// A helper function to write tokens:
45static const char* getTokenName( T aTok )
46{
47 return DRAWING_SHEET_LEXER::TokenName( aTok );
48}
49
50// A basic helper class to write a drawing sheet file
51// Not used alone, a file writer or a string writer should be derived to use it.
52// Therefore the constructor is protected.
54{
55public:
56 void Format( DS_DATA_MODEL* aSheet ) const;
57
58 void Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const;
59 void Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
60
61protected:
62 DS_DATA_MODEL_IO() { m_out = NULL; }
63 virtual ~DS_DATA_MODEL_IO() {}
64
65private:
66 void format( DS_DATA_ITEM_TEXT* aItem ) const;
67 void format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
68 void format( DS_DATA_ITEM_POLYGONS* aItem ) const;
69 void format( DS_DATA_ITEM_BITMAP* aItem ) const;
70 void formatCoordinate( const char* aToken, POINT_COORD& aCoord ) const;
71 void formatRepeatParameters( DS_DATA_ITEM* aItem ) const;
72 void formatOptions( DS_DATA_ITEM* aItem ) const;
73
74protected:
76};
77
78
79// A helper class to write a drawing sheet to a file
81{
82public:
83 DS_DATA_MODEL_FILEIO( const wxString& aFilename ) :
85 m_fileout( nullptr )
86 {
87 try
88 {
91 }
92 catch( const IO_ERROR& ioe )
93 {
94 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
95 }
96 }
97
99 {
100 delete m_fileout;
101 }
102
103private:
105};
106
107
108// A helper class to write a drawing sheet to a string
110{
111public:
112 DS_DATA_MODEL_STRINGIO( wxString* aOutputString ) :
114 m_output( aOutputString )
115 {
116 try
117 {
119 m_out = m_writer;
120 }
121 catch( const IO_ERROR& ioe )
122 {
123 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
124 }
125 }
126
128 {
129 *m_output = From_UTF8( m_writer->GetString().c_str() );
130 delete m_writer;
131 }
132
133private:
135 wxString* m_output;
136};
137
138
139void DS_DATA_MODEL::Save( const wxString& aFullFileName )
140{
141 DS_DATA_MODEL_FILEIO writer( aFullFileName );
142 writer.Format( this );
143}
144
145
146void DS_DATA_MODEL::SaveInString( wxString* aOutputString )
147{
148 DS_DATA_MODEL_STRINGIO writer( aOutputString );
149 writer.Format( this );
150}
151
152
153void DS_DATA_MODEL::SaveInString( std::vector<DS_DATA_ITEM*>& aItemsList, wxString* aOutputString )
154{
155 DS_DATA_MODEL_STRINGIO writer( aOutputString );
156 writer.Format( this, aItemsList );
157}
158
159
160void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const
161{
162 LOCALE_IO toggle; // switch on/off the locale "C" notation
163
164 m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
166 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
167
168 for( DS_DATA_ITEM* item : aItemsList )
169 Format( aModel, item );
170
171 m_out->Print( ")" );
172}
173
174
176{
177 switch( aItem->GetType() )
178 {
180 format( (DS_DATA_ITEM_TEXT*) aItem );
181 break;
182
185 format( aModel, aItem );
186 break;
187
189 format( (DS_DATA_ITEM_POLYGONS*) aItem );
190 break;
191
193 format( (DS_DATA_ITEM_BITMAP*) aItem );
194 break;
195
196 default:
197 wxFAIL_MSG( wxT( "Cannot format item" ) );
198 }
199}
200
201
203{
204 LOCALE_IO toggle; // switch on/off the locale "C" notation
205
206 m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
208 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
209
210 // Setup
211
212 // Write default values:
213 m_out->Print( "(setup" );
214 m_out->Print( "(textsize %s %s)",
215 FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(),
216 FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() );
217 m_out->Print( "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() );
218 m_out->Print( "(textlinewidth %s)", FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() );
219
220 // Write margin values
221 m_out->Print( "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() );
222 m_out->Print( "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() );
223 m_out->Print( "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() );
224 m_out->Print( "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() );
225
226 m_out->Print( ")" );
227
228 // Save the graphical items on the drawing sheet
229 for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
230 {
231 DS_DATA_ITEM* item = aSheet->GetItem( ii );
232 Format( aSheet, item );
233 }
234
235 m_out->Print( ")" );
236}
237
238
240{
241 m_out->Print( "(tbtext %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
242 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
243
244 formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
245 formatOptions( aItem );
246
247 if( aItem->m_Orient )
248 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() );
249
250 // Write font info, only if it is not the default setup
251 bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
252 bool write_thickness = aItem->m_LineWidth != 0.0;
253 bool write_face = aItem->m_Font && !aItem->m_Font->GetName().IsEmpty();
254
255 if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic
256 || write_face || aItem->m_TextColor != COLOR4D::UNSPECIFIED )
257 {
258 m_out->Print( "(font" );
259
260 if( write_face )
261 m_out->Print( "(face %s)", m_out->Quotew( aItem->m_Font->NameAsToken() ).c_str() );
262
263 if( write_thickness )
264 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
265
266 if( write_size )
267 {
268 m_out->Print( "(size %s %s)",
269 FormatDouble2Str( aItem->m_TextSize.x ).c_str(),
270 FormatDouble2Str( aItem->m_TextSize.y ).c_str() );
271 }
272
273 if( aItem->m_Bold )
274 m_out->Print( " bold" );
275
276 if( aItem->m_Italic )
277 m_out->Print( " italic" );
278
279 if( aItem->m_TextColor != COLOR4D::UNSPECIFIED )
280 {
281 m_out->Print( "(color %d %d %d %s)",
282 KiROUND( aItem->m_TextColor.r * 255.0 ),
283 KiROUND( aItem->m_TextColor.g * 255.0 ),
284 KiROUND( aItem->m_TextColor.b * 255.0 ),
285 FormatDouble2Str( aItem->m_TextColor.a ).c_str() );
286 }
287
288 m_out->Print( ")" );
289 }
290
291 // Write text justification
293 {
294 m_out->Print( "(justify" );
295
296 // Write T_center opt first, because it is
297 // also a center for both m_Hjustify and m_Vjustify
298 if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_CENTER )
299 m_out->Print( " center" );
300 else if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_RIGHT )
301 m_out->Print( " right" );
302
303 if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_TOP )
304 m_out->Print( " top" );
305 else if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_BOTTOM )
306 m_out->Print( " bottom" );
307
308 m_out->Print( ")" );
309 }
310
311 // write constraints
312 if( aItem->m_BoundingBoxSize.x )
313 m_out->Print( "(maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() );
314
315 if( aItem->m_BoundingBoxSize.y )
316 m_out->Print( "(maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() );
317
318 formatRepeatParameters( aItem );
319
320 if( !aItem->m_Info.IsEmpty() )
321 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
322
323 m_out->Print( ")" );
324}
325
326
328{
329 if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
330 m_out->Print( "(rect" );
331 else
332 m_out->Print( "(line" );
333
334 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
335
336 formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
337 formatCoordinate( getTokenName( T_end ), aItem->m_End );
338 formatOptions( aItem );
339
340 if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
341 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
342
343 formatRepeatParameters( aItem );
344
345 if( !aItem->m_Info.IsEmpty() )
346 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
347
348 m_out->Print( ")" );
349}
350
351
353{
354 m_out->Print( "(polygon" );
355 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
356 formatCoordinate( "pos", aItem->m_Pos );
357 formatOptions( aItem );
358
359 formatRepeatParameters( aItem );
360
361 if( !aItem->m_Orient.IsZero() )
362 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() );
363
364 if( aItem->m_LineWidth )
365 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
366
367 if( !aItem->m_Info.IsEmpty() )
368 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
369
370 // Write polygon corners list
371 for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
372 {
373 m_out->Print( "(pts" );
374
375 // Create current polygon corners list
376 unsigned ist = aItem->GetPolyIndexStart( kk );
377 unsigned iend = aItem->GetPolyIndexEnd( kk );
378
379 while( ist <= iend )
380 {
381 VECTOR2D pos = aItem->m_Corners[ist++];
382 m_out->Print( "(xy %s %s)",
383 FormatDouble2Str( pos.x ).c_str(),
384 FormatDouble2Str( pos.y ).c_str() );
385 }
386
387 m_out->Print( ")" );
388 }
389
390 m_out->Print( ")" );
391}
392
393
395{
396 // Don't save empty images
397 if( !aItem->m_ImageBitmap->GetOriginalImageData() )
398 return;
399
400 m_out->Print( "(bitmap" );
401 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
402 formatCoordinate( "pos", aItem->m_Pos );
403 formatOptions( aItem );
404
405 m_out->Print( "(scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
406
407 formatRepeatParameters( aItem );
408
409 if( !aItem->m_Info.IsEmpty() )
410 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
411
412 // Write image in png readable format
413 m_out->Print( "(data" );
414
415 wxString out = wxBase64Encode( aItem->m_ImageBitmap->GetImageDataBuffer() );
416
417 // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
418 // so use it in a vain attempt to be standard like.
419#define MIME_BASE64_LENGTH 76
420
421 size_t first = 0;
422
423 while( first < out.Length() )
424 {
425 m_out->Print( "\n\"%s\"", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
426 first += MIME_BASE64_LENGTH;
427 }
428
429 m_out->Print( ")" ); // Closes data token.
430 m_out->Print( ")" ); // Closes bitmap token.
431}
432
433
434void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
435{
436 m_out->Print( "(%s %s %s", aToken,
437 FormatDouble2Str( aCoord.m_Pos.x ).c_str(),
438 FormatDouble2Str( aCoord.m_Pos.y ).c_str() );
439
440 switch( aCoord.m_Anchor )
441 {
442 case RB_CORNER: break;
443 case LT_CORNER: m_out->Print( " ltcorner" ); break;
444 case LB_CORNER: m_out->Print( " lbcorner" ); break;
445 case RT_CORNER: m_out->Print( " rtcorner" ); break;
446 }
447
448 m_out->Print( ")" );
449}
450
451
453{
454 if( aItem->m_RepeatCount <= 1 )
455 return;
456
457 m_out->Print( "(repeat %d)", aItem->m_RepeatCount );
458
459 if( aItem->m_IncrementVector.x )
460 m_out->Print( "(incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() );
461
462 if( aItem->m_IncrementVector.y )
463 m_out->Print( "(incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() );
464
465 if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
466 m_out->Print( "(incrlabel %d)", aItem->m_IncrementLabel );
467}
468
469
471{
472 if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
473 m_out->Print( "(option page1only)" );
474 else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
475 m_out->Print( "(option notonpage1)" );
476}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
const wxImage * GetOriginalImageData() const
Definition: bitmap_base.h:71
double GetScale() const
Definition: bitmap_base.h:73
const wxMemoryBuffer & GetImageDataBuffer() const
Definition: bitmap_base.h:246
BITMAP_BASE * m_ImageBitmap
Definition: ds_data_item.h:370
unsigned GetPolyIndexStart(unsigned aContour) const
Definition: ds_data_item.h:252
unsigned GetPolyIndexEnd(unsigned aContour) const
Definition: ds_data_item.h:264
int GetPolyCount() const
Definition: ds_data_item.h:246
std::vector< VECTOR2D > m_Corners
Definition: ds_data_item.h:288
KIFONT::FONT * m_Font
Definition: ds_data_item.h:342
GR_TEXT_H_ALIGN_T m_Hjustify
Definition: ds_data_item.h:338
KIGFX::COLOR4D m_TextColor
Definition: ds_data_item.h:344
VECTOR2D m_BoundingBoxSize
Definition: ds_data_item.h:345
GR_TEXT_V_ALIGN_T m_Vjustify
Definition: ds_data_item.h:339
Drawing sheet structure type definitions.
Definition: ds_data_item.h:96
PAGE_OPTION GetPage1Option() const
Definition: ds_data_item.h:133
DS_ITEM_TYPE GetType() const
Definition: ds_data_item.h:128
wxString m_Name
Definition: ds_data_item.h:198
VECTOR2D m_IncrementVector
Definition: ds_data_item.h:204
POINT_COORD m_Pos
Definition: ds_data_item.h:200
wxString m_Info
Definition: ds_data_item.h:199
double m_LineWidth
Definition: ds_data_item.h:202
POINT_COORD m_End
Definition: ds_data_item.h:201
int m_IncrementLabel
Definition: ds_data_item.h:205
DS_DATA_MODEL_FILEIO(const wxString &aFilename)
PRETTIFIED_FILE_OUTPUTFORMATTER * m_fileout
void Format(DS_DATA_MODEL *aSheet) const
virtual ~DS_DATA_MODEL_IO()
void formatRepeatParameters(DS_DATA_ITEM *aItem) const
OUTPUTFORMATTER * m_out
void formatOptions(DS_DATA_ITEM *aItem) const
void format(DS_DATA_ITEM_TEXT *aItem) const
void formatCoordinate(const char *aToken, POINT_COORD &aCoord) const
STRING_FORMATTER * m_writer
DS_DATA_MODEL_STRINGIO(wxString *aOutputString)
Handle the graphic items list to draw/plot the frame and title block.
Definition: ds_data_model.h:39
double GetRightMargin()
Definition: ds_data_model.h:66
VECTOR2D m_DefaultTextSize
double GetTopMargin()
Definition: ds_data_model.h:69
unsigned GetCount() const
double m_DefaultLineWidth
DS_DATA_ITEM * GetItem(unsigned aIdx) const
double GetBottomMargin()
Definition: ds_data_model.h:72
double GetLeftMargin()
Definition: ds_data_model.h:63
double m_DefaultTextThickness
void SaveInString(wxString *aOutputString)
Save the description in a buffer.
void Save(const wxString &aFullFileName)
Save the description in a file.
double AsDegrees() const
Definition: eda_angle.h:113
bool IsZero() const
Definition: eda_angle.h:133
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
const char * NameAsToken() const
Definition: font.h:150
const wxString & GetName() const
Definition: font.h:149
double r
Red component.
Definition: color4d.h:392
double g
Green component.
Definition: color4d.h:393
double a
Alpha component.
Definition: color4d.h:395
double b
Blue component.
Definition: color4d.h:394
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:543
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:458
A coordinate point.
Definition: ds_data_item.h:70
VECTOR2D m_Pos
Definition: ds_data_item.h:80
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:449
const std::string & GetString()
Definition: richio.h:472
@ FIRST_PAGE_ONLY
Definition: ds_data_item.h:58
@ SUBSEQUENT_PAGES
Definition: ds_data_item.h:59
@ RB_CORNER
Definition: ds_data_item.h:49
@ RT_CORNER
Definition: ds_data_item.h:50
@ LT_CORNER
Definition: ds_data_item.h:52
@ LB_CORNER
Definition: ds_data_item.h:51
static const char * getTokenName(T aTok)
#define MIME_BASE64_LENGTH
#define SEXPR_WORKSHEET_FILE_VERSION
This file contains the file format version information for the s-expression drawing sheet file format...
#define _(s)
wxString From_UTF8(const char *cstring)
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP