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 The 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
45// A helper function to write tokens:
46static const char* getTokenName( T aTok )
47{
48 return DRAWING_SHEET_LEXER::TokenName( aTok );
49}
50
51
59{
60public:
61 void Format( DS_DATA_MODEL* aSheet ) const;
62
63 void Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const;
64 void Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
65
66protected:
67 DS_DATA_MODEL_IO() { m_out = NULL; }
68 virtual ~DS_DATA_MODEL_IO() {}
69
70private:
71 void format( DS_DATA_ITEM_TEXT* aItem ) const;
72 void format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
73 void format( DS_DATA_ITEM_POLYGONS* aItem ) const;
74 void format( DS_DATA_ITEM_BITMAP* aItem ) const;
75 void formatCoordinate( const char* aToken, POINT_COORD& aCoord ) const;
76 void formatRepeatParameters( DS_DATA_ITEM* aItem ) const;
77 void formatOptions( DS_DATA_ITEM* aItem ) const;
78
79protected:
81};
82
83
88{
89public:
90 DS_DATA_MODEL_FILEIO( const wxString& aFilename ) :
92 m_fileout( nullptr )
93 {
94 try
95 {
98 }
99 catch( const IO_ERROR& ioe )
100 {
101 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
102 }
103 }
104
106 {
107 delete m_fileout;
108 }
109
110private:
112};
113
114
119{
120public:
121 DS_DATA_MODEL_STRINGIO( wxString* aOutputString ) :
123 m_output( aOutputString )
124 {
125 try
126 {
128 m_out = m_writer;
129 }
130 catch( const IO_ERROR& ioe )
131 {
132 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
133 }
134 }
135
137 {
138 *m_output = From_UTF8( m_writer->GetString().c_str() );
139 delete m_writer;
140 }
141
142private:
144 wxString* m_output;
145};
146
147
148void DS_DATA_MODEL::Save( const wxString& aFullFileName )
149{
150 DS_DATA_MODEL_FILEIO writer( aFullFileName );
151 writer.Format( this );
152}
153
154
155void DS_DATA_MODEL::SaveInString( wxString* aOutputString )
156{
157 DS_DATA_MODEL_STRINGIO writer( aOutputString );
158 writer.Format( this );
159}
160
161
162void DS_DATA_MODEL::SaveInString( std::vector<DS_DATA_ITEM*>& aItemsList, wxString* aOutputString )
163{
164 DS_DATA_MODEL_STRINGIO writer( aOutputString );
165 writer.Format( this, aItemsList );
166}
167
168
169void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const
170{
171 LOCALE_IO toggle; // switch on/off the locale "C" notation
172
173 m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
175 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
176
177 for( DS_DATA_ITEM* item : aItemsList )
178 Format( aModel, item );
179
180 m_out->Print( ")" );
181}
182
183
185{
186 switch( aItem->GetType() )
187 {
189 format( (DS_DATA_ITEM_TEXT*) aItem );
190 break;
191
194 format( aModel, aItem );
195 break;
196
198 format( (DS_DATA_ITEM_POLYGONS*) aItem );
199 break;
200
202 format( (DS_DATA_ITEM_BITMAP*) aItem );
203 break;
204
205 default:
206 wxFAIL_MSG( wxT( "Cannot format item" ) );
207 }
208}
209
210
212{
213 LOCALE_IO toggle; // switch on/off the locale "C" notation
214
215 m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
217 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
218
219 // Setup
220
221 // Write default values:
222 m_out->Print( "(setup" );
223 m_out->Print( "(textsize %s %s)",
224 FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(),
225 FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() );
226 m_out->Print( "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() );
227 m_out->Print( "(textlinewidth %s)",
228 FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() );
229
230 // Write margin values
231 m_out->Print( "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() );
232 m_out->Print( "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() );
233 m_out->Print( "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() );
234 m_out->Print( "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() );
235
236 m_out->Print( ")" );
237
238 // Save the graphical items on the drawing sheet
239 for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
240 {
241 DS_DATA_ITEM* item = aSheet->GetItem( ii );
242 Format( aSheet, item );
243 }
244
245 m_out->Print( ")" );
246}
247
248
250{
251 m_out->Print( "(tbtext %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
252 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
253
254 formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
255 formatOptions( aItem );
256
257 if( aItem->m_Orient )
258 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() );
259
260 // Write font info, only if it is not the default setup
261 bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
262 bool write_thickness = aItem->m_LineWidth != 0.0;
263 bool write_face = aItem->m_Font && !aItem->m_Font->GetName().IsEmpty();
264
265 if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic
266 || write_face || aItem->m_TextColor != COLOR4D::UNSPECIFIED )
267 {
268 m_out->Print( "(font" );
269
270 if( write_face )
271 m_out->Print( "(face %s)", m_out->Quotew( aItem->m_Font->NameAsToken() ).c_str() );
272
273 if( write_thickness )
274 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
275
276 if( write_size )
277 {
278 m_out->Print( "(size %s %s)",
279 FormatDouble2Str( aItem->m_TextSize.x ).c_str(),
280 FormatDouble2Str( aItem->m_TextSize.y ).c_str() );
281 }
282
283 if( aItem->m_Bold )
284 m_out->Print( " bold" );
285
286 if( aItem->m_Italic )
287 m_out->Print( " italic" );
288
289 if( aItem->m_TextColor != COLOR4D::UNSPECIFIED )
290 {
291 m_out->Print( "(color %d %d %d %s)",
292 KiROUND( aItem->m_TextColor.r * 255.0 ),
293 KiROUND( aItem->m_TextColor.g * 255.0 ),
294 KiROUND( aItem->m_TextColor.b * 255.0 ),
295 FormatDouble2Str( aItem->m_TextColor.a ).c_str() );
296 }
297
298 m_out->Print( ")" );
299 }
300
301 // Write text justification
303 {
304 m_out->Print( "(justify" );
305
306 // Write T_center opt first, because it is
307 // also a center for both m_Hjustify and m_Vjustify
308 if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_CENTER )
309 m_out->Print( " center" );
310 else if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_RIGHT )
311 m_out->Print( " right" );
312
313 if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_TOP )
314 m_out->Print( " top" );
315 else if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_BOTTOM )
316 m_out->Print( " bottom" );
317
318 m_out->Print( ")" );
319 }
320
321 // write constraints
322 if( aItem->m_BoundingBoxSize.x )
323 m_out->Print( "(maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() );
324
325 if( aItem->m_BoundingBoxSize.y )
326 m_out->Print( "(maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() );
327
328 formatRepeatParameters( aItem );
329
330 if( !aItem->m_Info.IsEmpty() )
331 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
332
333 m_out->Print( ")" );
334}
335
336
338{
339 if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
340 m_out->Print( "(rect" );
341 else
342 m_out->Print( "(line" );
343
344 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
345
346 formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
347 formatCoordinate( getTokenName( T_end ), aItem->m_End );
348 formatOptions( aItem );
349
350 if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
351 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
352
353 formatRepeatParameters( aItem );
354
355 if( !aItem->m_Info.IsEmpty() )
356 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
357
358 m_out->Print( ")" );
359}
360
361
363{
364 m_out->Print( "(polygon" );
365 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
366 formatCoordinate( "pos", aItem->m_Pos );
367 formatOptions( aItem );
368
369 formatRepeatParameters( aItem );
370
371 if( !aItem->m_Orient.IsZero() )
372 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() );
373
374 if( aItem->m_LineWidth )
375 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
376
377 if( !aItem->m_Info.IsEmpty() )
378 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
379
380 // Write polygon corners list
381 for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
382 {
383 m_out->Print( "(pts" );
384
385 // Create current polygon corners list
386 unsigned ist = aItem->GetPolyIndexStart( kk );
387 unsigned iend = aItem->GetPolyIndexEnd( kk );
388
389 while( ist <= iend )
390 {
391 VECTOR2D pos = aItem->m_Corners[ist++];
392 m_out->Print( "(xy %s %s)",
393 FormatDouble2Str( pos.x ).c_str(),
394 FormatDouble2Str( pos.y ).c_str() );
395 }
396
397 m_out->Print( ")" );
398 }
399
400 m_out->Print( ")" );
401}
402
403
405{
406 // Don't save empty images
407 if( !aItem->m_ImageBitmap->GetOriginalImageData() )
408 return;
409
410 m_out->Print( "(bitmap" );
411 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
412 formatCoordinate( "pos", aItem->m_Pos );
413 formatOptions( aItem );
414
415 m_out->Print( "(scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
416
417 formatRepeatParameters( aItem );
418
419 if( !aItem->m_Info.IsEmpty() )
420 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
421
422 // Write image in png readable format
423 m_out->Print( "(data" );
424
425 wxString out = wxBase64Encode( aItem->m_ImageBitmap->GetImageDataBuffer() );
426
427 // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
428 // so use it in a vain attempt to be standard like.
429#define MIME_BASE64_LENGTH 76
430
431 size_t first = 0;
432
433 while( first < out.Length() )
434 {
435 m_out->Print( "\n\"%s\"", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
436 first += MIME_BASE64_LENGTH;
437 }
438
439 m_out->Print( ")" ); // Closes data token.
440 m_out->Print( ")" ); // Closes bitmap token.
441}
442
443
444void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
445{
446 m_out->Print( "(%s %s %s", aToken,
447 FormatDouble2Str( aCoord.m_Pos.x ).c_str(),
448 FormatDouble2Str( aCoord.m_Pos.y ).c_str() );
449
450 switch( aCoord.m_Anchor )
451 {
452 case RB_CORNER: break;
453 case LT_CORNER: m_out->Print( " ltcorner" ); break;
454 case LB_CORNER: m_out->Print( " lbcorner" ); break;
455 case RT_CORNER: m_out->Print( " rtcorner" ); break;
456 }
457
458 m_out->Print( ")" );
459}
460
461
463{
464 if( aItem->m_RepeatCount <= 1 )
465 return;
466
467 m_out->Print( "(repeat %d)", aItem->m_RepeatCount );
468
469 if( aItem->m_IncrementVector.x )
470 m_out->Print( "(incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() );
471
472 if( aItem->m_IncrementVector.y )
473 m_out->Print( "(incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() );
474
475 if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
476 m_out->Print( "(incrlabel %d)", aItem->m_IncrementLabel );
477}
478
479
481{
482 if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
483 m_out->Print( "(option page1only)" );
484 else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
485 m_out->Print( "(option notonpage1)" );
486}
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
A helper class to write a drawing sheet to a file.
DS_DATA_MODEL_FILEIO(const wxString &aFilename)
PRETTIFIED_FILE_OUTPUTFORMATTER * m_fileout
A basic helper class to write a drawing sheet file.
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
A helper class to write a drawing sheet to a string.
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:545
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:460
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