You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
14 KiB

  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. #include <build_version.h>
  27. #include <string_utils.h>
  28. #include <locale_io.h>
  29. #include <math/vector2d.h>
  30. #include <drawing_sheet/ds_painter.h>
  31. #include <drawing_sheet/ds_data_item.h>
  32. #include <drawing_sheet/ds_data_model.h>
  33. #include <drawing_sheet/drawing_sheet_lexer.h>
  34. #include <drawing_sheet/ds_file_versions.h>
  35. #include <font/font.h>
  36. #include <io/kicad/kicad_io_utils.h>
  37. #include <string_utils.h>
  38. #include <wx/msgdlg.h>
  39. #include <wx/mstream.h>
  40. using namespace DRAWINGSHEET_T;
  41. // A helper function to write tokens:
  42. static const char* getTokenName( T aTok )
  43. {
  44. return DRAWING_SHEET_LEXER::TokenName( aTok );
  45. }
  46. /**
  47. * A basic helper class to write a drawing sheet file.
  48. *
  49. * Not used alone, a file writer or a string writer should be derived to use it.
  50. * Therefore the constructor is protected.
  51. */
  52. class DS_DATA_MODEL_IO
  53. {
  54. public:
  55. void Format( DS_DATA_MODEL* aSheet ) const;
  56. void Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const;
  57. void Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
  58. protected:
  59. DS_DATA_MODEL_IO() { m_out = NULL; }
  60. virtual ~DS_DATA_MODEL_IO() {}
  61. private:
  62. void format( DS_DATA_ITEM_TEXT* aItem ) const;
  63. void format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const;
  64. void format( DS_DATA_ITEM_POLYGONS* aItem ) const;
  65. void format( DS_DATA_ITEM_BITMAP* aItem ) const;
  66. void formatCoordinate( const char* aToken, POINT_COORD& aCoord ) const;
  67. void formatRepeatParameters( DS_DATA_ITEM* aItem ) const;
  68. void formatOptions( DS_DATA_ITEM* aItem ) const;
  69. protected:
  70. OUTPUTFORMATTER* m_out;
  71. };
  72. /**
  73. * A helper class to write a drawing sheet to a file.
  74. */
  75. class DS_DATA_MODEL_FILEIO : public DS_DATA_MODEL_IO
  76. {
  77. public:
  78. DS_DATA_MODEL_FILEIO( const wxString& aFilename ) :
  79. DS_DATA_MODEL_IO(),
  80. m_fileout( nullptr )
  81. {
  82. try
  83. {
  84. m_fileout = new PRETTIFIED_FILE_OUTPUTFORMATTER( aFilename );
  85. m_out = m_fileout;
  86. }
  87. catch( const IO_ERROR& ioe )
  88. {
  89. wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
  90. }
  91. }
  92. ~DS_DATA_MODEL_FILEIO()
  93. {
  94. delete m_fileout;
  95. }
  96. private:
  97. PRETTIFIED_FILE_OUTPUTFORMATTER* m_fileout;
  98. };
  99. /**
  100. * A helper class to write a drawing sheet to a string.
  101. */
  102. class DS_DATA_MODEL_STRINGIO : public DS_DATA_MODEL_IO
  103. {
  104. public:
  105. DS_DATA_MODEL_STRINGIO( wxString* aOutputString ) :
  106. DS_DATA_MODEL_IO(),
  107. m_output( aOutputString )
  108. {
  109. try
  110. {
  111. m_writer = new STRING_FORMATTER();
  112. m_out = m_writer;
  113. }
  114. catch( const IO_ERROR& ioe )
  115. {
  116. wxMessageBox( ioe.What(), _( "Error writing drawing sheet file" ) );
  117. }
  118. }
  119. ~DS_DATA_MODEL_STRINGIO()
  120. {
  121. *m_output = From_UTF8( m_writer->GetString().c_str() );
  122. delete m_writer;
  123. }
  124. private:
  125. STRING_FORMATTER* m_writer;
  126. wxString* m_output;
  127. };
  128. void DS_DATA_MODEL::Save( const wxString& aFullFileName )
  129. {
  130. DS_DATA_MODEL_FILEIO writer( aFullFileName );
  131. writer.Format( this );
  132. }
  133. void DS_DATA_MODEL::SaveInString( wxString* aOutputString )
  134. {
  135. DS_DATA_MODEL_STRINGIO writer( aOutputString );
  136. writer.Format( this );
  137. }
  138. void DS_DATA_MODEL::SaveInString( std::vector<DS_DATA_ITEM*>& aItemsList, wxString* aOutputString )
  139. {
  140. DS_DATA_MODEL_STRINGIO writer( aOutputString );
  141. writer.Format( this, aItemsList );
  142. }
  143. void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, std::vector<DS_DATA_ITEM*>& aItemsList ) const
  144. {
  145. LOCALE_IO toggle; // switch on/off the locale "C" notation
  146. m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
  147. SEXPR_WORKSHEET_FILE_VERSION,
  148. m_out->Quotew( GetMajorMinorVersion() ).c_str() );
  149. for( DS_DATA_ITEM* item : aItemsList )
  150. Format( aModel, item );
  151. m_out->Print( ")" );
  152. }
  153. void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const
  154. {
  155. switch( aItem->GetType() )
  156. {
  157. case DS_DATA_ITEM::DS_TEXT:
  158. format( (DS_DATA_ITEM_TEXT*) aItem );
  159. break;
  160. case DS_DATA_ITEM::DS_SEGMENT:
  161. case DS_DATA_ITEM::DS_RECT:
  162. format( aModel, aItem );
  163. break;
  164. case DS_DATA_ITEM::DS_POLYPOLYGON:
  165. format( (DS_DATA_ITEM_POLYGONS*) aItem );
  166. break;
  167. case DS_DATA_ITEM::DS_BITMAP:
  168. format( (DS_DATA_ITEM_BITMAP*) aItem );
  169. break;
  170. default:
  171. wxFAIL_MSG( wxT( "Cannot format item" ) );
  172. }
  173. }
  174. void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aSheet ) const
  175. {
  176. LOCALE_IO toggle; // switch on/off the locale "C" notation
  177. m_out->Print( "(kicad_wks (version %d) (generator \"pl_editor\") (generator_version %s)",
  178. SEXPR_WORKSHEET_FILE_VERSION,
  179. m_out->Quotew( GetMajorMinorVersion() ).c_str() );
  180. // Setup
  181. // Write default values:
  182. m_out->Print( "(setup" );
  183. m_out->Print( "(textsize %s %s)",
  184. FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(),
  185. FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() );
  186. m_out->Print( "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() );
  187. m_out->Print( "(textlinewidth %s)",
  188. FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() );
  189. // Write margin values
  190. m_out->Print( "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() );
  191. m_out->Print( "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() );
  192. m_out->Print( "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() );
  193. m_out->Print( "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() );
  194. m_out->Print( ")" );
  195. // Save the graphical items on the drawing sheet
  196. for( unsigned ii = 0; ii < aSheet->GetCount(); ii++ )
  197. {
  198. DS_DATA_ITEM* item = aSheet->GetItem( ii );
  199. Format( aSheet, item );
  200. }
  201. m_out->Print( ")" );
  202. }
  203. void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_TEXT* aItem ) const
  204. {
  205. m_out->Print( "(tbtext %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
  206. m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
  207. formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
  208. formatOptions( aItem );
  209. if( aItem->m_Orient )
  210. m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() );
  211. // Write font info, only if it is not the default setup
  212. bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
  213. bool write_thickness = aItem->m_LineWidth != 0.0;
  214. bool write_face = aItem->m_Font && !aItem->m_Font->GetName().IsEmpty();
  215. if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic
  216. || write_face || aItem->m_TextColor != COLOR4D::UNSPECIFIED )
  217. {
  218. m_out->Print( "(font" );
  219. if( write_face )
  220. m_out->Print( "(face %s)", m_out->Quotew( aItem->m_Font->NameAsToken() ).c_str() );
  221. if( write_thickness )
  222. m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
  223. if( write_size )
  224. {
  225. m_out->Print( "(size %s %s)",
  226. FormatDouble2Str( aItem->m_TextSize.x ).c_str(),
  227. FormatDouble2Str( aItem->m_TextSize.y ).c_str() );
  228. }
  229. if( aItem->m_Bold )
  230. m_out->Print( " bold" );
  231. if( aItem->m_Italic )
  232. m_out->Print( " italic" );
  233. if( aItem->m_TextColor != COLOR4D::UNSPECIFIED )
  234. {
  235. m_out->Print( "(color %d %d %d %s)",
  236. KiROUND( aItem->m_TextColor.r * 255.0 ),
  237. KiROUND( aItem->m_TextColor.g * 255.0 ),
  238. KiROUND( aItem->m_TextColor.b * 255.0 ),
  239. FormatDouble2Str( aItem->m_TextColor.a ).c_str() );
  240. }
  241. m_out->Print( ")" );
  242. }
  243. // Write text justification
  244. if( aItem->m_Hjustify != GR_TEXT_H_ALIGN_LEFT || aItem->m_Vjustify != GR_TEXT_V_ALIGN_CENTER )
  245. {
  246. m_out->Print( "(justify" );
  247. // Write T_center opt first, because it is
  248. // also a center for both m_Hjustify and m_Vjustify
  249. if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_CENTER )
  250. m_out->Print( " center" );
  251. else if( aItem->m_Hjustify == GR_TEXT_H_ALIGN_RIGHT )
  252. m_out->Print( " right" );
  253. if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_TOP )
  254. m_out->Print( " top" );
  255. else if( aItem->m_Vjustify == GR_TEXT_V_ALIGN_BOTTOM )
  256. m_out->Print( " bottom" );
  257. m_out->Print( ")" );
  258. }
  259. // write constraints
  260. if( aItem->m_BoundingBoxSize.x )
  261. m_out->Print( "(maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() );
  262. if( aItem->m_BoundingBoxSize.y )
  263. m_out->Print( "(maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() );
  264. formatRepeatParameters( aItem );
  265. if( !aItem->m_Info.IsEmpty() )
  266. m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
  267. m_out->Print( ")" );
  268. }
  269. void DS_DATA_MODEL_IO::format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem ) const
  270. {
  271. if( aItem->GetType() == DS_DATA_ITEM::DS_RECT )
  272. m_out->Print( "(rect" );
  273. else
  274. m_out->Print( "(line" );
  275. m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
  276. formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
  277. formatCoordinate( getTokenName( T_end ), aItem->m_End );
  278. formatOptions( aItem );
  279. if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
  280. m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
  281. formatRepeatParameters( aItem );
  282. if( !aItem->m_Info.IsEmpty() )
  283. m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
  284. m_out->Print( ")" );
  285. }
  286. void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_POLYGONS* aItem ) const
  287. {
  288. m_out->Print( "(polygon" );
  289. m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
  290. formatCoordinate( "pos", aItem->m_Pos );
  291. formatOptions( aItem );
  292. formatRepeatParameters( aItem );
  293. if( !aItem->m_Orient.IsZero() )
  294. m_out->Print( "(rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() );
  295. if( aItem->m_LineWidth )
  296. m_out->Print( "(linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() );
  297. if( !aItem->m_Info.IsEmpty() )
  298. m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
  299. // Write polygon corners list
  300. for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
  301. {
  302. m_out->Print( "(pts" );
  303. // Create current polygon corners list
  304. unsigned ist = aItem->GetPolyIndexStart( kk );
  305. unsigned iend = aItem->GetPolyIndexEnd( kk );
  306. while( ist <= iend )
  307. {
  308. VECTOR2D pos = aItem->m_Corners[ist++];
  309. m_out->Print( "(xy %s %s)",
  310. FormatDouble2Str( pos.x ).c_str(),
  311. FormatDouble2Str( pos.y ).c_str() );
  312. }
  313. m_out->Print( ")" );
  314. }
  315. m_out->Print( ")" );
  316. }
  317. void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_BITMAP* aItem ) const
  318. {
  319. // Don't save empty images
  320. if( !aItem->m_ImageBitmap->GetOriginalImageData() )
  321. return;
  322. m_out->Print( "(bitmap" );
  323. m_out->Print( "(name %s)", m_out->Quotew( aItem->m_Name ).c_str() );
  324. formatCoordinate( "pos", aItem->m_Pos );
  325. formatOptions( aItem );
  326. m_out->Print( "(scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
  327. formatRepeatParameters( aItem );
  328. if( !aItem->m_Info.IsEmpty() )
  329. m_out->Print( "(comment %s)", m_out->Quotew( aItem->m_Info ).c_str() );
  330. // Write image in png readable format
  331. wxMemoryOutputStream stream;
  332. aItem->m_ImageBitmap->SaveImageData( stream );
  333. KICAD_FORMAT::FormatStreamData( *m_out, *stream.GetOutputStreamBuffer() );
  334. m_out->Print( ")" ); // Closes bitmap token.
  335. }
  336. void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const
  337. {
  338. m_out->Print( "(%s %s %s", aToken,
  339. FormatDouble2Str( aCoord.m_Pos.x ).c_str(),
  340. FormatDouble2Str( aCoord.m_Pos.y ).c_str() );
  341. switch( aCoord.m_Anchor )
  342. {
  343. case RB_CORNER: break;
  344. case LT_CORNER: m_out->Print( " ltcorner" ); break;
  345. case LB_CORNER: m_out->Print( " lbcorner" ); break;
  346. case RT_CORNER: m_out->Print( " rtcorner" ); break;
  347. }
  348. m_out->Print( ")" );
  349. }
  350. void DS_DATA_MODEL_IO::formatRepeatParameters( DS_DATA_ITEM* aItem ) const
  351. {
  352. if( aItem->m_RepeatCount <= 1 )
  353. return;
  354. m_out->Print( "(repeat %d)", aItem->m_RepeatCount );
  355. if( aItem->m_IncrementVector.x )
  356. m_out->Print( "(incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() );
  357. if( aItem->m_IncrementVector.y )
  358. m_out->Print( "(incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() );
  359. if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT )
  360. m_out->Print( "(incrlabel %d)", aItem->m_IncrementLabel );
  361. }
  362. void DS_DATA_MODEL_IO::formatOptions( DS_DATA_ITEM* aItem ) const
  363. {
  364. if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
  365. m_out->Print( "(option page1only)" );
  366. else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
  367. m_out->Print( "(option notonpage1)" );
  368. }