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, int aNestLevel ) 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, int aNestLevel ) const;
67 void format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const;
68 void format( DS_DATA_ITEM_POLYGONS* aItem, int aNestLevel ) const;
69 void format( DS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) 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( 0, "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version \"%s\")\n",
166
167 for( DS_DATA_ITEM* item : aItemsList )
168 Format( aModel, item, 1 );
169
170 m_out->Print( 0, ")\n" );
171}
172
173
174void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const
175{
176 switch( aItem->GetType() )
177 {
179 format( (DS_DATA_ITEM_TEXT*) aItem, aNestLevel );
180 break;
181
184 format( aModel, aItem, aNestLevel );
185 break;
186
188 format( (DS_DATA_ITEM_POLYGONS*) aItem, aNestLevel );
189 break;
190
192 format( (DS_DATA_ITEM_BITMAP*) aItem, aNestLevel );
193 break;
194
195 default:
196 wxFAIL_MSG( wxT( "Cannot format item" ) );
197 }
198}
199
200
202{
203 LOCALE_IO toggle; // switch on/off the locale "C" notation
204
205 m_out->Print( 0, "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version \"%s\")\n",
207
208 // Setup
209 int nestLevel = 1;
210 // Write default values:
211 m_out->Print( nestLevel, "(setup " );
212 m_out->Print( 0, "(textsize %s %s)",
213 FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(),
214 FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() );
215 m_out->Print( 0, "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() );
216 m_out->Print( 0, "(textlinewidth %s)", FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() );
217 m_out->Print( 0, "\n" );
218
219 // Write margin values
220 m_out->Print( nestLevel, "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() );
221 m_out->Print( 0, "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() );
222 m_out->Print( 0, "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() );
223 m_out->Print( 0, "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() );
224 m_out->Print( 0, ")\n" );
225
226 // Save the graphical items on the drawing sheet
227 for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
228 {
229 DS_DATA_ITEM* item = aSheet->GetItem( ii );
230 Format( aSheet, item, nestLevel );
231 }
232
233 m_out->Print( 0, ")\n" );
234}
235
236
237void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const
238{
239 m_out->Print( aNestLevel, "(tbtext" );
240 m_out->Print( 0, " %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
241 m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
242
243 formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
244 formatOptions( aItem );
245
246 if( aItem->m_Orient )
247 m_out->Print( 0, " (rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() );
248
249 // Write font info, only if it is not the default setup
250 bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
251 bool write_thickness = aItem->m_LineWidth != 0.0;
252 bool write_face = aItem->m_Font && !aItem->m_Font->GetName().IsEmpty();
253
254 if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic
255 || write_face || aItem->m_TextColor != COLOR4D::UNSPECIFIED )
256 {
257 m_out->Print( 0, " (font" );
258
259 if( write_face )
260 m_out->Print( 0, " (face \"%s\")", aItem->m_Font->NameAsToken() );
261
262 if( write_thickness )
263 m_out->Print( 0, " (linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
264
265 if( write_size )
266 {
267 m_out->Print( 0, " (size %s %s)",
268 FormatDouble2Str( aItem->m_TextSize.x ).c_str(),
269 FormatDouble2Str( aItem->m_TextSize.y ).c_str() );
270 }
271
272 if( aItem->m_Bold )
273 m_out->Print( 0, " bold" );
274
275 if( aItem->m_Italic )
276 m_out->Print( 0, " italic" );
277
278 if( aItem->m_TextColor != COLOR4D::UNSPECIFIED )
279 {
280 m_out->Print( 0, " (color %d %d %d %s)",
281 KiROUND( aItem->m_TextColor.r * 255.0 ),
282 KiROUND( aItem->m_TextColor.g * 255.0 ),
283 KiROUND( aItem->m_TextColor.b * 255.0 ),
284 FormatDouble2Str( aItem->m_TextColor.a ).c_str() );
285 }
286
287 m_out->Print( 0, ")" );
288 }
289
290 // Write text justification
292 {
293 m_out->Print( 0, " (justify" );
294
295 // Write T_center opt first, because it is
296 // also a center for both m_Hjustify and m_Vjustify
297 if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_CENTER )
298 m_out->Print( 0, " center" );
299 else if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_RIGHT )
300 m_out->Print( 0, " right" );
301
302 if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_TOP )
303 m_out->Print( 0, " top" );
304 else if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_BOTTOM )
305 m_out->Print( 0, " bottom" );
306
307 m_out->Print( 0, ")" );
308 }
309
310 // write constraints
311 if( aItem->m_BoundingBoxSize.x )
312 m_out->Print( 0, " (maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() );
313
314 if( aItem->m_BoundingBoxSize.y )
315 m_out->Print( 0, " (maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() );
316
317 formatRepeatParameters( aItem );
318
319 if( !aItem->m_Info.IsEmpty() )
320 m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
321
322 m_out->Print( 0, ")\n" );
323}
324
325
326void DS_DATA_MODEL_IO::format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int aNestLevel ) const
327{
328 if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
329 m_out->Print( aNestLevel, "(rect" );
330 else
331 m_out->Print( aNestLevel, "(line" );
332
333 m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
334
335 formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
336 formatCoordinate( getTokenName( T_end ), aItem->m_End );
337 formatOptions( aItem );
338
339 if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
340 m_out->Print( 0, " (linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
341
342 formatRepeatParameters( aItem );
343
344 if( !aItem->m_Info.IsEmpty() )
345 m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
346
347 m_out->Print( 0, ")\n" );
348}
349
350
351void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_POLYGONS* aItem, int aNestLevel ) const
352{
353 m_out->Print( aNestLevel, "(polygon" );
354 m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
355 formatCoordinate( "pos", aItem->m_Pos );
356 formatOptions( aItem );
357
358 formatRepeatParameters( aItem );
359
360 if( !aItem->m_Orient.IsZero() )
361 m_out->Print( 0, " (rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() );
362
363 if( aItem->m_LineWidth )
364 m_out->Print( 0, " (linewidth %s)\n", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
365
366 if( !aItem->m_Info.IsEmpty() )
367 m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
368
369 // Write polygon corners list
370 for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
371 {
372 m_out->Print( aNestLevel+1, "(pts" );
373 // Create current polygon corners list
374 unsigned ist = aItem->GetPolyIndexStart( kk );
375 unsigned iend = aItem->GetPolyIndexEnd( kk );
376 int ii = 0;
377
378 while( ist <= iend )
379 {
380 VECTOR2D pos = aItem->m_Corners[ist++];
381 int nestLevel = 0;
382
383 if( ii++ > 4)
384 {
385 m_out->Print( 0, "\n" );
386 nestLevel = aNestLevel+2;
387 ii = 0;
388 }
389
390 m_out->Print( nestLevel, " (xy %s %s)",
391 FormatDouble2Str( pos.x ).c_str(),
392 FormatDouble2Str( pos.y ).c_str() );
393 }
394
395 m_out->Print( 0, ")\n" );
396 }
397
398 m_out->Print( aNestLevel, ")\n" );
399}
400
401
402void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) const
403{
404 // Don't save empty images
405 if( !aItem->m_ImageBitmap->GetOriginalImageData() )
406 return;
407
408 m_out->Print( aNestLevel, "(bitmap" );
409 m_out->Print( 0, " (name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
410 formatCoordinate( "pos", aItem->m_Pos );
411 formatOptions( aItem );
412
413 m_out->Print( 0, " (scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
414
415 formatRepeatParameters( aItem );
416 m_out->Print( 0,"\n");
417
418 if( !aItem->m_Info.IsEmpty() )
419 m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() );
420
421 // Write image in png readable format
422 m_out->Print( aNestLevel, "(data" );
423
424 wxString out = wxBase64Encode( aItem->m_ImageBitmap->GetImageDataBuffer() );
425
426 // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
427 // so use it in a vain attempt to be standard like.
428#define MIME_BASE64_LENGTH 76
429
430 size_t first = 0;
431
432 while( first < out.Length() )
433 {
434 m_out->Print( 0, "\n" );
435 m_out->Print( aNestLevel + 1, "\"%s\"", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
436 first += MIME_BASE64_LENGTH;
437 }
438
439 m_out->Print( 0, "\n" );
440 m_out->Print( aNestLevel, ")\n" ); // Closes data token.
441 m_out->Print( aNestLevel, ")\n" ); // Closes bitmap token.
442}
443
444
445void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
446{
447 m_out->Print( 0, " (%s %s %s", aToken,
448 FormatDouble2Str( aCoord.m_Pos.x ).c_str(),
449 FormatDouble2Str( aCoord.m_Pos.y ).c_str() );
450
451 switch( aCoord.m_Anchor )
452 {
453 case RB_CORNER: break;
454 case LT_CORNER: m_out->Print( 0, " ltcorner" ); break;
455 case LB_CORNER: m_out->Print( 0, " lbcorner" ); break;
456 case RT_CORNER: m_out->Print( 0, " rtcorner" ); break;
457 }
458
459 m_out->Print( 0, ")" );
460}
461
462
464{
465 if( aItem->m_RepeatCount <= 1 )
466 return;
467
468 m_out->Print( 0, " (repeat %d)", aItem->m_RepeatCount );
469
470 if( aItem->m_IncrementVector.x )
471 m_out->Print( 0, " (incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() );
472
473 if( aItem->m_IncrementVector.y )
474 m_out->Print( 0, " (incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() );
475
476 if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
477 m_out->Print( 0, " (incrlabel %d)", aItem->m_IncrementLabel );
478}
479
480
482{
483 if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
484 m_out->Print( 0, " (option page1only)" );
485 else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
486 m_out->Print( 0, " (option notonpage1)" );
487}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
const wxImage * GetOriginalImageData() const
Definition: bitmap_base.h:70
double GetScale() const
Definition: bitmap_base.h:72
const wxMemoryBuffer & GetImageDataBuffer() const
Definition: bitmap_base.h:240
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 format(DS_DATA_ITEM_TEXT *aItem, int aNestLevel) const
void formatOptions(DS_DATA_ITEM *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:155
bool IsZero() const
Definition: eda_angle.h:175
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:148
const wxString & GetName() const
Definition: font.h:147
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:526
int PRINTF_FUNC 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:433
const std::string & GetString()
Definition: richio.h:456
@ 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:391
@ 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
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:118