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 <memory>
28
29#include <build_version.h>
30#include <string_utils.h>
31#include <locale_io.h>
32#include <math/vector2d.h>
36#include <drawing_sheet/drawing_sheet_lexer.h>
38#include <font/font.h>
40#include <string_utils.h>
41
42#include <wx/msgdlg.h>
43#include <wx/mstream.h>
44
45using namespace DRAWINGSHEET_T;
46
47
48// A helper function to write tokens:
49static const char* getTokenName( T aTok )
50{
51 return DRAWING_SHEET_LEXER::TokenName( aTok );
52}
53
54
62{
63public:
64 void Format( DS_DATA_MODEL* aSheet ) const;
65
66 void Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const;
67 void Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
68
69protected:
70 DS_DATA_MODEL_IO() { m_out = NULL; }
71 virtual ~DS_DATA_MODEL_IO() {}
72
73private:
74 void format( DS_DATA_ITEM_TEXT* aItem ) const;
75 void format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
76 void format( DS_DATA_ITEM_POLYGONS* aItem ) const;
77 void format( DS_DATA_ITEM_BITMAP* aItem ) const;
78 void formatCoordinate( const char* aToken, POINT_COORD& aCoord ) const;
79 void formatRepeatParameters( DS_DATA_ITEM* aItem ) const;
80 void formatOptions( DS_DATA_ITEM* aItem ) const;
81
82protected:
84};
85
86
91{
92public:
93 DS_DATA_MODEL_FILEIO( const wxString& aFilename ) :
95 m_fileout( std::make_unique<PRETTIFIED_FILE_OUTPUTFORMATTER>( aFilename ) )
96 {
97 m_out = m_fileout.get();
98 }
99
101
102 void Finish() { m_fileout->Finish(); }
103
104private:
105 std::unique_ptr<PRETTIFIED_FILE_OUTPUTFORMATTER> m_fileout;
106};
107
108
113{
114public:
115 DS_DATA_MODEL_STRINGIO( wxString* aOutputString ) :
117 m_output( aOutputString )
118 {
119 try
120 {
122 m_out = m_writer;
123 }
124 catch( const IO_ERROR& ioe )
125 {
126 wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
127 }
128 }
129
131 {
132 *m_output = From_UTF8( m_writer->GetString().c_str() );
133 delete m_writer;
134 }
135
136private:
138 wxString* m_output;
139};
140
141
142void DS_DATA_MODEL::Save( const wxString& aFullFileName )
143{
144 DS_DATA_MODEL_FILEIO writer( aFullFileName );
145 writer.Format( this );
146 writer.Finish();
147}
148
149
150void DS_DATA_MODEL::SaveInString( wxString* aOutputString )
151{
152 DS_DATA_MODEL_STRINGIO writer( aOutputString );
153 writer.Format( this );
154}
155
156
157void DS_DATA_MODEL::SaveInString( std::vector<DS_DATA_ITEM*>& aItemsList, wxString* aOutputString )
158{
159 DS_DATA_MODEL_STRINGIO writer( aOutputString );
160 writer.Format( this, aItemsList );
161}
162
163
164void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const
165{
166 LOCALE_IO toggle; // switch on/off the locale "C" notation
167
168 m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
170 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
171
172 for( DS_DATA_ITEM* item : aItemsList )
173 Format( aModel, item );
174
175 m_out->Print( ")" );
176}
177
178
180{
181 switch( aItem->GetType() )
182 {
184 format( (DS_DATA_ITEM_TEXT*) aItem );
185 break;
186
189 format( aModel, aItem );
190 break;
191
193 format( (DS_DATA_ITEM_POLYGONS*) aItem );
194 break;
195
197 format( (DS_DATA_ITEM_BITMAP*) aItem );
198 break;
199
200 default:
201 wxFAIL_MSG( wxT( "Cannot format item" ) );
202 }
203}
204
205
207{
208 LOCALE_IO toggle; // switch on/off the locale "C" notation
209
210 m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
212 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
213
214 // Setup
215
216 // Write default values:
217 m_out->Print( "(setup" );
218 m_out->Print( "(textsize %s %s)",
219 FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(),
220 FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() );
221 m_out->Print( "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() );
222 m_out->Print( "(textlinewidth %s)",
223 FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() );
224
225 // Write margin values
226 m_out->Print( "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() );
227 m_out->Print( "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() );
228 m_out->Print( "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() );
229 m_out->Print( "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() );
230
231 m_out->Print( ")" );
232
233 // Save the graphical items on the drawing sheet
234 for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
235 {
236 DS_DATA_ITEM* item = aSheet->GetItem( ii );
237 Format( aSheet, item );
238 }
239
240 m_out->Print( ")" );
241}
242
243
245{
246 m_out->Print( "(tbtext %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
247 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
248
249 formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
250 formatOptions( aItem );
251
252 if( aItem->m_Orient )
253 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() );
254
255 // Write font info, only if it is not the default setup
256 bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
257 bool write_thickness = aItem->m_LineWidth != 0.0;
258 bool write_face = aItem->m_Font && !aItem->m_Font->GetName().IsEmpty();
259
260 if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic
261 || write_face || aItem->m_TextColor != COLOR4D::UNSPECIFIED )
262 {
263 m_out->Print( "(font" );
264
265 if( write_face )
266 m_out->Print( "(face %s)", m_out->Quotew( aItem->m_Font->NameAsToken() ).c_str() );
267
268 if( write_thickness )
269 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
270
271 if( write_size )
272 {
273 m_out->Print( "(size %s %s)",
274 FormatDouble2Str( aItem->m_TextSize.x ).c_str(),
275 FormatDouble2Str( aItem->m_TextSize.y ).c_str() );
276 }
277
278 if( aItem->m_Bold )
279 m_out->Print( " bold" );
280
281 if( aItem->m_Italic )
282 m_out->Print( " italic" );
283
284 if( aItem->m_TextColor != COLOR4D::UNSPECIFIED )
285 {
286 m_out->Print( "(color %d %d %d %s)",
287 KiROUND( aItem->m_TextColor.r * 255.0 ),
288 KiROUND( aItem->m_TextColor.g * 255.0 ),
289 KiROUND( aItem->m_TextColor.b * 255.0 ),
290 FormatDouble2Str( aItem->m_TextColor.a ).c_str() );
291 }
292
293 m_out->Print( ")" );
294 }
295
296 // Write text justification
298 {
299 m_out->Print( "(justify" );
300
301 // Write T_center opt first, because it is
302 // also a center for both m_Hjustify and m_Vjustify
303 if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_CENTER )
304 m_out->Print( " center" );
305 else if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_RIGHT )
306 m_out->Print( " right" );
307
308 if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_TOP )
309 m_out->Print( " top" );
310 else if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_BOTTOM )
311 m_out->Print( " bottom" );
312
313 m_out->Print( ")" );
314 }
315
316 // write constraints
317 if( aItem->m_BoundingBoxSize.x )
318 m_out->Print( "(maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() );
319
320 if( aItem->m_BoundingBoxSize.y )
321 m_out->Print( "(maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() );
322
323 formatRepeatParameters( aItem );
324
325 if( !aItem->m_Info.IsEmpty() )
326 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
327
328 m_out->Print( ")" );
329}
330
331
333{
334 if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
335 m_out->Print( "(rect" );
336 else
337 m_out->Print( "(line" );
338
339 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
340
341 formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
342 formatCoordinate( getTokenName( T_end ), aItem->m_End );
343 formatOptions( aItem );
344
345 if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
346 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
347
348 formatRepeatParameters( aItem );
349
350 if( !aItem->m_Info.IsEmpty() )
351 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
352
353 m_out->Print( ")" );
354}
355
356
358{
359 m_out->Print( "(polygon" );
360 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
361 formatCoordinate( "pos", aItem->m_Pos );
362 formatOptions( aItem );
363
364 formatRepeatParameters( aItem );
365
366 if( !aItem->m_Orient.IsZero() )
367 m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() );
368
369 if( aItem->m_LineWidth )
370 m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
371
372 if( !aItem->m_Info.IsEmpty() )
373 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
374
375 // Write polygon corners list
376 for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
377 {
378 m_out->Print( "(pts" );
379
380 // Create current polygon corners list
381 unsigned ist = aItem->GetPolyIndexStart( kk );
382 unsigned iend = aItem->GetPolyIndexEnd( kk );
383
384 while( ist <= iend )
385 {
386 VECTOR2D pos = aItem->m_Corners[ist++];
387 m_out->Print( "(xy %s %s)",
388 FormatDouble2Str( pos.x ).c_str(),
389 FormatDouble2Str( pos.y ).c_str() );
390 }
391
392 m_out->Print( ")" );
393 }
394
395 m_out->Print( ")" );
396}
397
398
400{
401 // Don't save empty images
402 if( !aItem->m_ImageBitmap->GetOriginalImageData() )
403 return;
404
405 m_out->Print( "(bitmap" );
406 m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
407 formatCoordinate( "pos", aItem->m_Pos );
408 formatOptions( aItem );
409
410 m_out->Print( "(scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
411
412 formatRepeatParameters( aItem );
413
414 if( !aItem->m_Info.IsEmpty() )
415 m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
416
417 // Write image in png readable format
418 wxMemoryOutputStream stream;
419 aItem->m_ImageBitmap->SaveImageData( stream );
420
421 KICAD_FORMAT::FormatStreamData( *m_out, *stream.GetOutputStreamBuffer() );
422
423 m_out->Print( ")" ); // Closes bitmap token.
424}
425
426
427void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
428{
429 m_out->Print( "(%s %s %s", aToken,
430 FormatDouble2Str( aCoord.m_Pos.x ).c_str(),
431 FormatDouble2Str( aCoord.m_Pos.y ).c_str() );
432
433 switch( aCoord.m_Anchor )
434 {
435 case RB_CORNER: break;
436 case LT_CORNER: m_out->Print( " ltcorner" ); break;
437 case LB_CORNER: m_out->Print( " lbcorner" ); break;
438 case RT_CORNER: m_out->Print( " rtcorner" ); break;
439 }
440
441 m_out->Print( ")" );
442}
443
444
446{
447 if( aItem->m_RepeatCount <= 1 )
448 return;
449
450 m_out->Print( "(repeat %d)", aItem->m_RepeatCount );
451
452 if( aItem->m_IncrementVector.x )
453 m_out->Print( "(incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() );
454
455 if( aItem->m_IncrementVector.y )
456 m_out->Print( "(incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() );
457
458 if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
459 m_out->Print( "(incrlabel %d)", aItem->m_IncrementLabel );
460}
461
462
464{
465 if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
466 m_out->Print( "(option page1only)" );
467 else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
468 m_out->Print( "(option notonpage1)" );
469}
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
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:402
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.
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:117
const wxString & GetName() const
Definition font.h:116
double r
Red component.
Definition color4d.h:393
double g
Green component.
Definition color4d.h:394
double a
Alpha component.
Definition color4d.h:396
double b
Blue component.
Definition color4d.h:395
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
An interface used to output 8 bit text in a convenient way.
Definition richio.h:295
A coordinate point.
VECTOR2D m_Pos
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:422
@ 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:686