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, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <memory>
24
25#include <build_version.h>
26#include <string_utils.h>
27#include <locale_io.h>
28#include <math/vector2d.h>
32#include <drawing_sheet/drawing_sheet_lexer.h>
34#include <font/font.h>
36#include <string_utils.h>
37
38#include <wx/msgdlg.h>
39#include <wx/mstream.h>
40
41using namespace DRAWINGSHEET_T;
42
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
58{
59public:
60 void Format( DS_DATA_MODEL* aSheet ) const;
61
62 void Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const;
63 void Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
64
65protected:
66 DS_DATA_MODEL_IO() { m_out = NULL; }
67 virtual ~DS_DATA_MODEL_IO() {}
68
69private:
70 void format( DS_DATA_ITEM_TEXT* aItem ) const;
71 void format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
72 void format( DS_DATA_ITEM_POLYGONS* aItem ) const;
73 void format( DS_DATA_ITEM_BITMAP* aItem ) const;
74 void formatCoordinate( const char* aToken, POINT_COORD& aCoord ) const;
75 void formatRepeatParameters( DS_DATA_ITEM* aItem ) const;
76 void formatOptions( DS_DATA_ITEM* aItem ) const;
77
78protected:
80};
81
82
87{
88public:
89 DS_DATA_MODEL_FILEIO( const wxString& aFilename ) :
91 m_fileout( std::make_unique<PRETTIFIED_FILE_OUTPUTFORMATTER>( aFilename ) )
92 {
93 m_out = m_fileout.get();
94 }
95
97
98 void Finish() { m_fileout->Finish(); }
99
100private:
101 std::unique_ptr<PRETTIFIED_FILE_OUTPUTFORMATTER> m_fileout;
102};
103
104
109{
110public:
111 DS_DATA_MODEL_STRINGIO( wxString* aOutputString ) :
113 m_output( aOutputString )
114 {
115 try
116 {
118 m_out = m_writer;
119 }
120 catch( const IO_ERROR& ioe )
121 {
122 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
123 }
124 }
125
127 {
128 *m_output = From_UTF8( m_writer->GetString().c_str() );
129 delete m_writer;
130 }
131
132private:
134 wxString* m_output;
135};
136
137
138void DS_DATA_MODEL::Save( const wxString& aFullFileName )
139{
140 DS_DATA_MODEL_FILEIO writer( aFullFileName );
141 writer.Format( this );
142 writer.Finish();
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 %s) (generator_version %s)",
166 m_out->Quotew( aModel->GetGenerator() ).c_str(),
167 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
168
169 for( DS_DATA_ITEM* item : aItemsList )
170 Format( aModel, item );
171
172 m_out->Print( ")" );
173}
174
175
177{
178 switch( aItem->GetType() )
179 {
181 format( (DS_DATA_ITEM_TEXT*) aItem );
182 break;
183
186 format( aModel, aItem );
187 break;
188
190 format( (DS_DATA_ITEM_POLYGONS*) aItem );
191 break;
192
194 format( (DS_DATA_ITEM_BITMAP*) aItem );
195 break;
196
197 default:
198 wxFAIL_MSG( wxT( "Cannot format item" ) );
199 }
200}
201
202
204{
205 LOCALE_IO toggle; // switch on/off the locale "C" notation
206
207 m_out->Print( "(kicad_wks (version %d) (generator %s) (generator_version %s)",
209 m_out->Quotew( aSheet->GetGenerator() ).c_str(),
210 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
211
212 // Setup
213
214 // Write default values:
215 m_out->Print( "(setup" );
216 m_out->Print( "(textsize %s %s)",
217 FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(),
218 FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() );
219 m_out->Print( "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() );
220 m_out->Print( "(textlinewidth %s)",
221 FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() );
222
223 // Write margin values
224 m_out->Print( "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() );
225 m_out->Print( "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() );
226 m_out->Print( "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() );
227 m_out->Print( "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() );
228
229 m_out->Print( ")" );
230
231 // Save the graphical items on the drawing sheet
232 for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
233 {
234 DS_DATA_ITEM* item = aSheet->GetItem( ii );
235 Format( aSheet, item );
236 }
237
238 m_out->Print( ")" );
239}
240
241
243{
244 m_out->Print( "(tbtext %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
245 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
246
247 formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
248 formatOptions( aItem );
249
250 if( aItem->m_Orient )
251 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() );
252
253 // Write font info, only if it is not the default setup
254 bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
255 bool write_thickness = aItem->m_LineWidth != 0.0;
256 bool write_face = aItem->m_Font && !aItem->m_Font->GetName().IsEmpty();
257
258 if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic
259 || write_face || aItem->m_TextColor != COLOR4D::UNSPECIFIED )
260 {
261 m_out->Print( "(font" );
262
263 if( write_face )
264 m_out->Print( "(face %s)", m_out->Quotew( aItem->m_Font->NameAsToken() ).c_str() );
265
266 if( write_thickness )
267 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
268
269 if( write_size )
270 {
271 m_out->Print( "(size %s %s)",
272 FormatDouble2Str( aItem->m_TextSize.x ).c_str(),
273 FormatDouble2Str( aItem->m_TextSize.y ).c_str() );
274 }
275
276 if( aItem->m_Bold )
277 m_out->Print( " bold" );
278
279 if( aItem->m_Italic )
280 m_out->Print( " italic" );
281
282 if( aItem->m_TextColor != COLOR4D::UNSPECIFIED )
283 {
284 m_out->Print( "(color %d %d %d %s)",
285 KiROUND( aItem->m_TextColor.r * 255.0 ),
286 KiROUND( aItem->m_TextColor.g * 255.0 ),
287 KiROUND( aItem->m_TextColor.b * 255.0 ),
288 FormatDouble2Str( aItem->m_TextColor.a ).c_str() );
289 }
290
291 m_out->Print( ")" );
292 }
293
294 // Write text justification
296 {
297 m_out->Print( "(justify" );
298
299 // Write T_center opt first, because it is
300 // also a center for both m_Hjustify and m_Vjustify
301 if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_CENTER )
302 m_out->Print( " center" );
303 else if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_RIGHT )
304 m_out->Print( " right" );
305
306 if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_TOP )
307 m_out->Print( " top" );
308 else if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_BOTTOM )
309 m_out->Print( " bottom" );
310
311 m_out->Print( ")" );
312 }
313
314 // write constraints
315 if( aItem->m_BoundingBoxSize.x )
316 m_out->Print( "(maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() );
317
318 if( aItem->m_BoundingBoxSize.y )
319 m_out->Print( "(maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() );
320
321 formatRepeatParameters( aItem );
322
323 if( !aItem->m_Info.IsEmpty() )
324 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
325
326 m_out->Print( ")" );
327}
328
329
331{
332 if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
333 m_out->Print( "(rect" );
334 else
335 m_out->Print( "(line" );
336
337 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
338
339 formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
340 formatCoordinate( getTokenName( T_end ), aItem->m_End );
341 formatOptions( aItem );
342
343 if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
344 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
345
346 formatRepeatParameters( aItem );
347
348 if( !aItem->m_Info.IsEmpty() )
349 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
350
351 m_out->Print( ")" );
352}
353
354
356{
357 m_out->Print( "(polygon" );
358 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
359 formatCoordinate( "pos", aItem->m_Pos );
360 formatOptions( aItem );
361
362 formatRepeatParameters( aItem );
363
364 if( !aItem->m_Orient.IsZero() )
365 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() );
366
367 if( aItem->m_LineWidth )
368 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
369
370 if( !aItem->m_Info.IsEmpty() )
371 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
372
373 // Write polygon corners list
374 for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
375 {
376 m_out->Print( "(pts" );
377
378 // Create current polygon corners list
379 unsigned ist = aItem->GetPolyIndexStart( kk );
380 unsigned iend = aItem->GetPolyIndexEnd( kk );
381
382 while( ist <= iend )
383 {
384 VECTOR2D pos = aItem->m_Corners[ist++];
385 m_out->Print( "(xy %s %s)",
386 FormatDouble2Str( pos.x ).c_str(),
387 FormatDouble2Str( pos.y ).c_str() );
388 }
389
390 m_out->Print( ")" );
391 }
392
393 m_out->Print( ")" );
394}
395
396
398{
399 // Don't save empty images
400 if( !aItem->m_ImageBitmap->GetOriginalImageData() )
401 return;
402
403 m_out->Print( "(bitmap" );
404 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
405 formatCoordinate( "pos", aItem->m_Pos );
406 formatOptions( aItem );
407
408 m_out->Print( "(scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
409
410 formatRepeatParameters( aItem );
411
412 if( !aItem->m_Info.IsEmpty() )
413 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
414
415 // Write image in png readable format
416 wxMemoryOutputStream stream;
417 aItem->m_ImageBitmap->SaveImageData( stream );
418
419 KICAD_FORMAT::FormatStreamData( *m_out, *stream.GetOutputStreamBuffer() );
420
421 m_out->Print( ")" ); // Closes bitmap token.
422}
423
424
425void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
426{
427 m_out->Print( "(%s %s %s", aToken,
428 FormatDouble2Str( aCoord.m_Pos.x ).c_str(),
429 FormatDouble2Str( aCoord.m_Pos.y ).c_str() );
430
431 switch( aCoord.m_Anchor )
432 {
433 case RB_CORNER: break;
434 case LT_CORNER: m_out->Print( " ltcorner" ); break;
435 case LB_CORNER: m_out->Print( " lbcorner" ); break;
436 case RT_CORNER: m_out->Print( " rtcorner" ); break;
437 }
438
439 m_out->Print( ")" );
440}
441
442
444{
445 if( aItem->m_RepeatCount <= 1 )
446 return;
447
448 m_out->Print( "(repeat %d)", aItem->m_RepeatCount );
449
450 if( aItem->m_IncrementVector.x )
451 m_out->Print( "(incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() );
452
453 if( aItem->m_IncrementVector.y )
454 m_out->Print( "(incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() );
455
456 if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
457 m_out->Print( "(incrlabel %d)", aItem->m_IncrementLabel );
458}
459
460
462{
463 if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
464 m_out->Print( "(option page1only)" );
465 else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
466 m_out->Print( "(option notonpage1)" );
467}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
const wxImage * GetOriginalImageData() const
Definition bitmap_base.h:67
double GetScale() const
Definition bitmap_base.h:69
bool SaveImageData(wxOutputStream &aOutStream) const
Write the bitmap data to aOutStream.
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
BITMAP_BASE * m_ImageBitmap
unsigned GetPolyIndexStart(unsigned aContour) const
unsigned GetPolyIndexEnd(unsigned aContour) const
std::vector< VECTOR2D > m_Corners
KIFONT::FONT * m_Font
GR_TEXT_H_ALIGN_T m_Hjustify
KIGFX::COLOR4D m_TextColor
VECTOR2D m_BoundingBoxSize
GR_TEXT_V_ALIGN_T m_Vjustify
Drawing sheet structure type definitions.
PAGE_OPTION GetPage1Option() const
DS_ITEM_TYPE GetType() const
wxString m_Name
VECTOR2D m_IncrementVector
POINT_COORD m_Pos
wxString m_Info
double m_LineWidth
POINT_COORD m_End
A helper class to write a drawing sheet to a file.
~DS_DATA_MODEL_FILEIO()=default
DS_DATA_MODEL_FILEIO(const wxString &aFilename)
std::unique_ptr< PRETTIFIED_FILE_OUTPUTFORMATTER > m_fileout
void Format(DS_DATA_MODEL *aSheet) const
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.
double GetRightMargin()
VECTOR2D m_DefaultTextSize
double GetTopMargin()
unsigned GetCount() const
double m_DefaultLineWidth
DS_DATA_ITEM * GetItem(unsigned aIdx) const
double GetBottomMargin()
double GetLeftMargin()
double m_DefaultTextThickness
void SaveInString(wxString *aOutputString)
Save the description in a buffer.
const wxString & GetGenerator() const
void Save(const wxString &aFullFileName)
Save the description in a file.
double AsDegrees() const
Definition eda_angle.h:116
bool IsZero() const
Definition eda_angle.h:136
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
const char * NameAsToken() const
Definition font.h:113
const wxString & GetName() const
Definition font.h:112
double r
Red component.
Definition color4d.h:389
double g
Green component.
Definition color4d.h:390
double a
Alpha component.
Definition color4d.h:392
double b
Blue component.
Definition color4d.h:391
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
An interface used to output 8 bit text in a convenient way.
Definition richio.h:291
A coordinate point.
VECTOR2D m_Pos
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:418
@ FIRST_PAGE_ONLY
@ SUBSEQUENT_PAGES
@ RB_CORNER
@ RT_CORNER
@ LT_CORNER
@ LB_CORNER
static const char * getTokenName(T aTok)
#define SEXPR_WORKSHEET_FILE_VERSION
This file contains the file format version information for the s-expression drawing sheet file format...
#define _(s)
void FormatStreamData(OUTPUTFORMATTER &aOut, const wxStreamBuffer &aStream)
Write binary data to the formatter as base 64 encoded string.
STL namespace.
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...
@ 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
VECTOR2< double > VECTOR2D
Definition vector2d.h:682