KiCad PCB EDA Suite
common_plotDXF_functions.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 2016 KiCad Developers, see CHANGELOG.TXT for contributors.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 
29 #include <fctsys.h>
30 #include <gr_basic.h>
31 #include <trigo.h>
32 #include <wxstruct.h>
33 #include <base_struct.h>
34 #include <plot_common.h>
35 #include <macros.h>
36 #include <kicad_string.h>
38 
43 static const double DXF_OBLIQUE_ANGLE = 15;
44 
45 /* The layer/colors palette. The acad/DXF palette is divided in 3 zones:
46 
47  - The primary colors (1 - 9)
48  - An HSV zone (10-250, 5 values x 2 saturations x 10 hues
49  - Greys (251 - 255)
50 
51  There is *no* black... the white does it on paper, usually, and
52  anyway it depends on the plotter configuration, since DXF colors
53  are meant to be logical only (they represent *both* line color and
54  width); later version with plot styles only complicate the matter!
55 
56  As usual, brown and magenta/purple are difficult to place since
57  they are actually variations of other colors.
58  */
59 static const struct
60 {
61  const char *name;
62  int color;
64 {
65  { "BLACK", 7 }, // In DXF, color 7 is *both* white and black!
66  { "GRAY1", 251 },
67  { "GRAY2", 8 },
68  { "GRAY3", 9 },
69  { "WHITE", 7 },
70  { "LYELLOW", 51 },
71  { "BLUE1", 178 },
72  { "GREEN1", 98 },
73  { "CYAN1", 138 },
74  { "RED1", 18 },
75  { "MAGENTA1", 228 },
76  { "BROWN1", 58 },
77  { "BLUE2", 5 },
78  { "GREEN2", 3 },
79  { "CYAN2", 4 },
80  { "RED2", 1 },
81  { "MAGENTA2", 6 },
82  { "BROWN2", 54 },
83  { "BLUE3", 171 },
84  { "GREEN3", 91 },
85  { "CYAN3", 131 },
86  { "RED3", 11 },
87  { "MAGENTA3", 221 },
88  { "YELLOW3", 2 },
89  { "BLUE4", 5 },
90  { "GREEN4", 3 },
91  { "CYAN4", 4 },
92  { "RED4", 1 },
93  { "MAGENTA4", 6 },
94  { "YELLOW4", 2 }
95 };
96 
97 
98 // A helper function to create a color name acceptable in DXF files
99 // DXF files do not use a RGB definition
100 static wxString getDXFColorName( COLOR4D aColor )
101 {
102  EDA_COLOR_T color = ColorFindNearest( int( aColor.r*255 ),
103  int( aColor.g*255 ),
104  int( aColor.b*255 ) );
105  wxString cname( dxf_layer[color].name );
106  return cname;
107 }
108 
114 void DXF_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
115  double aScale, bool aMirror )
116 {
117  plotOffset = aOffset;
118  plotScale = aScale;
119 
120  /* DXF paper is 'virtual' so there is no need of a paper size.
121  Also this way we can handle the aux origin which can be useful
122  (for example when aligning to a mechanical drawing) */
123  paperSize.x = 0;
124  paperSize.y = 0;
125 
126  /* Like paper size DXF units are abstract too. Anyway there is a
127  * system variable (MEASUREMENT) which will be set to 1 to indicate
128  * metric units */
129  m_IUsPerDecimil = aIusPerDecimil;
130  iuPerDeviceUnit = 1.0 / aIusPerDecimil; // Gives a DXF in decimils
131  iuPerDeviceUnit *= 0.00254; // ... now in mm
132 
133  SetDefaultLineWidth( 0 ); // No line width on DXF
134  m_plotMirror = false; // No mirroring on DXF
136 }
137 
142 {
143  wxASSERT( outputFile );
144 
145  // DXF HEADER - Boilerplate
146  // Defines the minimum for drawing i.e. the angle system and the
147  // continuous linetype
148  fputs( " 0\n"
149  "SECTION\n"
150  " 2\n"
151  "HEADER\n"
152  " 9\n"
153  "$ANGBASE\n"
154  " 50\n"
155  "0.0\n"
156  " 9\n"
157  "$ANGDIR\n"
158  " 70\n"
159  " 1\n"
160  " 9\n"
161  "$MEASUREMENT\n"
162  " 70\n"
163  "0\n"
164  " 0\n" // This means 'metric units'
165  "ENDSEC\n"
166  " 0\n"
167  "SECTION\n"
168  " 2\n"
169  "TABLES\n"
170  " 0\n"
171  "TABLE\n"
172  " 2\n"
173  "LTYPE\n"
174  " 70\n"
175  "1\n"
176  " 0\n"
177  "LTYPE\n"
178  " 2\n"
179  "CONTINUOUS\n"
180  " 70\n"
181  "0\n"
182  " 3\n"
183  "Solid line\n"
184  " 72\n"
185  "65\n"
186  " 73\n"
187  "0\n"
188  " 40\n"
189  "0.0\n"
190  " 0\n"
191  "ENDTAB\n",
192  outputFile );
193 
194  // Text styles table
195  // Defines 4 text styles, one for each bold/italic combination
196  fputs( " 0\n"
197  "TABLE\n"
198  " 2\n"
199  "STYLE\n"
200  " 70\n"
201  "4\n", outputFile );
202 
203  static const char *style_name[4] = {"KICAD", "KICADB", "KICADI", "KICADBI"};
204  for(int i = 0; i < 4; i++ )
205  {
206  fprintf( outputFile,
207  " 0\n"
208  "STYLE\n"
209  " 2\n"
210  "%s\n" // Style name
211  " 70\n"
212  "0\n" // Standard flags
213  " 40\n"
214  "0\n" // Non-fixed height text
215  " 41\n"
216  "1\n" // Width factor (base)
217  " 42\n"
218  "1\n" // Last height (mandatory)
219  " 50\n"
220  "%g\n" // Oblique angle
221  " 71\n"
222  "0\n" // Generation flags (default)
223  " 3\n"
224  // The standard ISO font (when kicad is build with it
225  // the dxf text in acad matches *perfectly*)
226  "isocp.shx\n", // Font name (when not bigfont)
227  // Apply a 15 degree angle to italic text
228  style_name[i], i < 2 ? 0 : DXF_OBLIQUE_ANGLE );
229  }
230 
231 
232  // Layer table - one layer per color
233  fprintf( outputFile,
234  " 0\n"
235  "ENDTAB\n"
236  " 0\n"
237  "TABLE\n"
238  " 2\n"
239  "LAYER\n"
240  " 70\n"
241  "%d\n", NBCOLORS );
242 
243  /* The layer/colors palette. The acad/DXF palette is divided in 3 zones:
244 
245  - The primary colors (1 - 9)
246  - An HSV zone (10-250, 5 values x 2 saturations x 10 hues
247  - Greys (251 - 255)
248  */
249 
250  for( EDA_COLOR_T i = BLACK; i < NBCOLORS; i = NextColor(i) )
251  {
252  fprintf( outputFile,
253  " 0\n"
254  "LAYER\n"
255  " 2\n"
256  "%s\n" // Layer name
257  " 70\n"
258  "0\n" // Standard flags
259  " 62\n"
260  "%d\n" // Color number
261  " 6\n"
262  "CONTINUOUS\n",// Linetype name
263  dxf_layer[i].name, dxf_layer[i].color );
264  }
265 
266  // End of layer table, begin entities
267  fputs( " 0\n"
268  "ENDTAB\n"
269  " 0\n"
270  "ENDSEC\n"
271  " 0\n"
272  "SECTION\n"
273  " 2\n"
274  "ENTITIES\n", outputFile );
275 
276  return true;
277 }
278 
279 
281 {
282  wxASSERT( outputFile );
283 
284  // DXF FOOTER
285  fputs( " 0\n"
286  "ENDSEC\n"
287  " 0\n"
288  "EOF\n", outputFile );
289  fclose( outputFile );
290  outputFile = NULL;
291 
292  return true;
293 }
294 
295 
300 {
301  if( ( colorMode )
302  || ( color == COLOR4D::BLACK )
303  || ( color == COLOR4D::WHITE ) )
304  {
306  }
307  else
309 }
310 
314 void DXF_PLOTTER::Rect( const wxPoint& p1, const wxPoint& p2, FILL_T fill, int width )
315 {
316  wxASSERT( outputFile );
317  MoveTo( p1 );
318  LineTo( wxPoint( p1.x, p2.y ) );
319  LineTo( wxPoint( p2.x, p2.y ) );
320  LineTo( wxPoint( p2.x, p1.y ) );
321  FinishTo( wxPoint( p1.x, p1.y ) );
322 }
323 
324 
331 void DXF_PLOTTER::Circle( const wxPoint& centre, int diameter, FILL_T fill, int width )
332 {
333  wxASSERT( outputFile );
334  double radius = userToDeviceSize( diameter / 2 );
335  DPOINT centre_dev = userToDeviceCoordinates( centre );
336  if( radius > 0 )
337  {
338  wxString cname = getDXFColorName( m_currentColor );
339 
340  if( !fill )
341  {
342  fprintf( outputFile, "0\nCIRCLE\n8\n%s\n10\n%g\n20\n%g\n40\n%g\n",
343  TO_UTF8( cname ),
344  centre_dev.x, centre_dev.y, radius );
345  }
346 
347  if( fill == FILLED_SHAPE )
348  {
349  double r = radius*0.5;
350  fprintf( outputFile, "0\nPOLYLINE\n");
351  fprintf( outputFile, "8\n%s\n66\n1\n70\n1\n", TO_UTF8( cname ));
352  fprintf( outputFile, "40\n%g\n41\n%g\n", radius, radius);
353  fprintf( outputFile, "0\nVERTEX\n8\n%s\n", TO_UTF8( cname ));
354  fprintf( outputFile, "10\n%g\n 20\n%g\n42\n1.0\n",
355  centre_dev.x-r, centre_dev.y );
356  fprintf( outputFile, "0\nVERTEX\n8\n%s\n", TO_UTF8( cname ));
357  fprintf( outputFile, "10\n%g\n 20\n%g\n42\n1.0\n",
358  centre_dev.x+r, centre_dev.y );
359  fprintf( outputFile, "0\nSEQEND\n");
360  }
361  }
362 }
363 
364 
371 void DXF_PLOTTER::PlotPoly( const std::vector<wxPoint>& aCornerList,
372  FILL_T aFill, int aWidth, void * aData )
373 {
374  if( aCornerList.size() <= 1 )
375  return;
376 
377  unsigned last = aCornerList.size() - 1;
378 
379  // Plot outlines with lines (thickness = 0) to define the polygon
380  if( aWidth <= 0 )
381  {
382  MoveTo( aCornerList[0] );
383 
384  for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
385  LineTo( aCornerList[ii] );
386 
387  // Close polygon if 'fill' requested
388  if( aFill )
389  {
390  if( aCornerList[last] != aCornerList[0] )
391  LineTo( aCornerList[0] );
392  }
393 
394  PenFinish();
395 
396  return;
397  }
398 
399 
400  // if the polygon outline has thickness, and is not filled
401  // (i.e. is a polyline) plot outlines with thick segments
402  if( aWidth > 0 && !aFill )
403  {
404  MoveTo( aCornerList[0] );
405 
406  for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
407  ThickSegment( aCornerList[ii-1], aCornerList[ii],
408  aWidth, FILLED, NULL );
409 
410  return;
411  }
412 
413  // The polygon outline has thickness, and is filled
414  // Build and plot the polygon which contains the initial
415  // polygon and its thick outline
416  SHAPE_POLY_SET bufferOutline;
417  SHAPE_POLY_SET bufferPolybase;
418  const int circleToSegmentsCount = 16;
419 
420  bufferPolybase.NewOutline();
421 
422  // enter outline as polygon:
423  for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
424  {
426  aCornerList[ii-1], aCornerList[ii], circleToSegmentsCount, aWidth );
427  }
428 
429  // enter the initial polygon:
430  for( unsigned ii = 0; ii < aCornerList.size(); ii++ )
431  {
432  bufferPolybase.Append( aCornerList[ii] );
433  }
434 
435  // Merge polygons to build the polygon which contains the initial
436  // polygon and its thick outline
437 
438  // create the outline which contains thick outline:
439  bufferPolybase.BooleanAdd( bufferOutline, SHAPE_POLY_SET::PM_FAST );
440  bufferPolybase.Fracture( SHAPE_POLY_SET::PM_FAST );
441 
442  if( bufferPolybase.OutlineCount() < 1 ) // should not happen
443  return;
444 
445  const SHAPE_LINE_CHAIN& path = bufferPolybase.COutline( 0 );
446 
447  if( path.PointCount() < 2 ) // should not happen
448  return;
449 
450  // Now, output the final polygon to DXF file:
451  last = path.PointCount() - 1;
452  VECTOR2I point = path.CPoint( 0 );
453 
454  wxPoint startPoint( point.x, point.y );
455  MoveTo( startPoint );
456 
457  for( int ii = 1; ii < path.PointCount(); ii++ )
458  {
459  point = path.CPoint( ii );
460  LineTo( wxPoint( point.x, point.y ) );
461  }
462 
463  // Close polygon, if needed
464  point = path.CPoint( last );
465  wxPoint endPoint( point.x, point.y );
466 
467  if( endPoint != startPoint )
468  LineTo( startPoint );
469 
470  PenFinish();
471 }
472 
473 
474 void DXF_PLOTTER::PenTo( const wxPoint& pos, char plume )
475 {
476  wxASSERT( outputFile );
477  if( plume == 'Z' )
478  {
479  return;
480  }
481  DPOINT pos_dev = userToDeviceCoordinates( pos );
482  DPOINT pen_lastpos_dev = userToDeviceCoordinates( penLastpos );
483 
484  if( penLastpos != pos && plume == 'D' )
485  {
486  // DXF LINE
487  wxString cname = getDXFColorName( m_currentColor );
488  fprintf( outputFile, "0\nLINE\n8\n%s\n10\n%g\n20\n%g\n11\n%g\n21\n%g\n",
489  TO_UTF8( cname ),
490  pen_lastpos_dev.x, pen_lastpos_dev.y, pos_dev.x, pos_dev.y );
491  }
492  penLastpos = pos;
493 }
494 
495 
499 void DXF_PLOTTER::SetDash( bool dashed )
500 {
501  // NOP for now
502 }
503 
504 
505 void DXF_PLOTTER::ThickSegment( const wxPoint& aStart, const wxPoint& aEnd, int aWidth,
506  EDA_DRAW_MODE_T aPlotMode, void* aData )
507 {
508  segmentAsOval( aStart, aEnd, aWidth, aPlotMode );
509 }
510 
511 /* Plot an arc in DXF format
512  * Filling is not supported
513  */
514 void DXF_PLOTTER::Arc( const wxPoint& centre, double StAngle, double EndAngle, int radius,
515  FILL_T fill, int width )
516 {
517  wxASSERT( outputFile );
518 
519  if( radius <= 0 )
520  return;
521 
522  // In DXF, arcs are drawn CCW.
523  // In Kicad, arcs are CW or CCW
524  // If StAngle > EndAngle, it is CW. So transform it to CCW
525  if( StAngle > EndAngle )
526  {
527  std::swap( StAngle, EndAngle );
528  }
529 
530  DPOINT centre_dev = userToDeviceCoordinates( centre );
531  double radius_dev = userToDeviceSize( radius );
532 
533  // Emit a DXF ARC entity
534  wxString cname = getDXFColorName( m_currentColor );
535  fprintf( outputFile,
536  "0\nARC\n8\n%s\n10\n%g\n20\n%g\n40\n%g\n50\n%g\n51\n%g\n",
537  TO_UTF8( cname ),
538  centre_dev.x, centre_dev.y, radius_dev,
539  StAngle / 10.0, EndAngle / 10.0 );
540 }
541 
545 void DXF_PLOTTER::FlashPadOval( const wxPoint& pos, const wxSize& aSize, double orient,
546  EDA_DRAW_MODE_T trace_mode, void* aData )
547 {
548  wxASSERT( outputFile );
549  wxSize size( aSize );
550 
551  /* The chip is reduced to an oval tablet with size.y > size.x
552  * (Oval vertical orientation 0) */
553  if( size.x > size.y )
554  {
555  std::swap( size.x, size.y );
556  orient = AddAngles( orient, 900 );
557  }
558 
559  sketchOval( pos, size, orient, -1 );
560 }
561 
562 
567 void DXF_PLOTTER::FlashPadCircle( const wxPoint& pos, int diametre,
568  EDA_DRAW_MODE_T trace_mode, void* aData )
569 {
570  wxASSERT( outputFile );
571  Circle( pos, diametre, NO_FILL );
572 }
573 
574 
578 void DXF_PLOTTER::FlashPadRect( const wxPoint& pos, const wxSize& padsize,
579  double orient, EDA_DRAW_MODE_T trace_mode, void* aData )
580 {
581  wxASSERT( outputFile );
582  wxSize size;
583  int ox, oy, fx, fy;
584 
585  size.x = padsize.x / 2;
586  size.y = padsize.y / 2;
587 
588  if( size.x < 0 )
589  size.x = 0;
590  if( size.y < 0 )
591  size.y = 0;
592 
593  // If a dimension is zero, the trace is reduced to 1 line
594  if( size.x == 0 )
595  {
596  ox = pos.x;
597  oy = pos.y - size.y;
598  RotatePoint( &ox, &oy, pos.x, pos.y, orient );
599  fx = pos.x;
600  fy = pos.y + size.y;
601  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
602  MoveTo( wxPoint( ox, oy ) );
603  FinishTo( wxPoint( fx, fy ) );
604  return;
605  }
606  if( size.y == 0 )
607  {
608  ox = pos.x - size.x;
609  oy = pos.y;
610  RotatePoint( &ox, &oy, pos.x, pos.y, orient );
611  fx = pos.x + size.x;
612  fy = pos.y;
613  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
614  MoveTo( wxPoint( ox, oy ) );
615  FinishTo( wxPoint( fx, fy ) );
616  return;
617  }
618 
619  ox = pos.x - size.x;
620  oy = pos.y - size.y;
621  RotatePoint( &ox, &oy, pos.x, pos.y, orient );
622  MoveTo( wxPoint( ox, oy ) );
623 
624  fx = pos.x - size.x;
625  fy = pos.y + size.y;
626  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
627  LineTo( wxPoint( fx, fy ) );
628 
629  fx = pos.x + size.x;
630  fy = pos.y + size.y;
631  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
632  LineTo( wxPoint( fx, fy ) );
633 
634  fx = pos.x + size.x;
635  fy = pos.y - size.y;
636  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
637  LineTo( wxPoint( fx, fy ) );
638 
639  FinishTo( wxPoint( ox, oy ) );
640 }
641 
642 void DXF_PLOTTER::FlashPadRoundRect( const wxPoint& aPadPos, const wxSize& aSize,
643  int aCornerRadius, double aOrient,
644  EDA_DRAW_MODE_T aTraceMode, void* aData )
645 {
646  SHAPE_POLY_SET outline;
647  const int segmentToCircleCount = 64;
648  TransformRoundRectToPolygon( outline, aPadPos, aSize, aOrient,
649  aCornerRadius, segmentToCircleCount );
650 
651  // TransformRoundRectToPolygon creates only one convex polygon
652  SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
653 
654  MoveTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
655 
656  for( int ii = 1; ii < poly.PointCount(); ++ii )
657  LineTo( wxPoint( poly.Point( ii ).x, poly.Point( ii ).y ) );
658 
659  FinishTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
660 }
661 
662 void DXF_PLOTTER::FlashPadCustom( const wxPoint& aPadPos, const wxSize& aSize,
663  SHAPE_POLY_SET* aPolygons,
664  EDA_DRAW_MODE_T aTraceMode, void* aData )
665 {
666  for( int cnt = 0; cnt < aPolygons->OutlineCount(); ++cnt )
667  {
668  SHAPE_LINE_CHAIN& poly = aPolygons->Outline( cnt );
669 
670  MoveTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
671 
672  for( int ii = 1; ii < poly.PointCount(); ++ii )
673  LineTo( wxPoint( poly.Point( ii ).x, poly.Point( ii ).y ) );
674 
675  FinishTo(wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
676  }
677 }
678 
679 
683 void DXF_PLOTTER::FlashPadTrapez( const wxPoint& aPadPos, const wxPoint *aCorners,
684  double aPadOrient, EDA_DRAW_MODE_T aTrace_Mode, void* aData )
685 {
686  wxASSERT( outputFile );
687  wxPoint coord[4]; /* coord actual corners of a trapezoidal trace */
688 
689  for( int ii = 0; ii < 4; ii++ )
690  {
691  coord[ii] = aCorners[ii];
692  RotatePoint( &coord[ii], aPadOrient );
693  coord[ii] += aPadPos;
694  }
695 
696  // Plot edge:
697  MoveTo( coord[0] );
698  LineTo( coord[1] );
699  LineTo( coord[2] );
700  LineTo( coord[3] );
701  FinishTo( coord[0] );
702 }
703 
712 bool containsNonAsciiChars( const wxString& string )
713 {
714  for( unsigned i = 0; i < string.length(); i++ )
715  {
716  wchar_t ch = string[i];
717  if( ch > 255 )
718  return true;
719  }
720  return false;
721 }
722 
723 void DXF_PLOTTER::Text( const wxPoint& aPos,
724  COLOR4D aColor,
725  const wxString& aText,
726  double aOrient,
727  const wxSize& aSize,
728  enum EDA_TEXT_HJUSTIFY_T aH_justify,
729  enum EDA_TEXT_VJUSTIFY_T aV_justify,
730  int aWidth,
731  bool aItalic,
732  bool aBold,
733  bool aMultilineAllowed,
734  void* aData )
735 {
736  // Fix me: see how to use DXF text mode for multiline texts
737  if( aMultilineAllowed && !aText.Contains( wxT( "\n" ) ) )
738  aMultilineAllowed = false; // the text has only one line.
739 
740  if( textAsLines || containsNonAsciiChars( aText ) || aMultilineAllowed )
741  {
742  // output text as graphics.
743  // Perhaps miltiline texts could be handled as DXF text entity
744  // but I do not want spend time about this (JPC)
745  PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify,
746  aWidth, aItalic, aBold, aMultilineAllowed );
747  }
748  else
749  {
750  /* Emit text as a text entity. This loses formatting and shape but it's
751  more useful as a CAD object */
752  DPOINT origin_dev = userToDeviceCoordinates( aPos );
753  SetColor( aColor );
754  wxString cname = getDXFColorName( m_currentColor );
755  DPOINT size_dev = userToDeviceSize( aSize );
756  int h_code = 0, v_code = 0;
757  switch( aH_justify )
758  {
760  h_code = 0;
761  break;
763  h_code = 1;
764  break;
766  h_code = 2;
767  break;
768  }
769  switch( aV_justify )
770  {
772  v_code = 3;
773  break;
775  v_code = 2;
776  break;
778  v_code = 1;
779  break;
780  }
781 
782  // Position, size, rotation and alignment
783  // The two alignment point usages is somewhat idiot (see the DXF ref)
784  // Anyway since we don't use the fit/aligned options, they're the same
785  fprintf( outputFile,
786  " 0\n"
787  "TEXT\n"
788  " 7\n"
789  "%s\n" // Text style
790  " 8\n"
791  "%s\n" // Layer name
792  " 10\n"
793  "%g\n" // First point X
794  " 11\n"
795  "%g\n" // Second point X
796  " 20\n"
797  "%g\n" // First point Y
798  " 21\n"
799  "%g\n" // Second point Y
800  " 40\n"
801  "%g\n" // Text height
802  " 41\n"
803  "%g\n" // Width factor
804  " 50\n"
805  "%g\n" // Rotation
806  " 51\n"
807  "%g\n" // Oblique angle
808  " 71\n"
809  "%d\n" // Mirror flags
810  " 72\n"
811  "%d\n" // H alignment
812  " 73\n"
813  "%d\n", // V alignment
814  aBold ? (aItalic ? "KICADBI" : "KICADB")
815  : (aItalic ? "KICADI" : "KICAD"),
816  TO_UTF8( cname ),
817  origin_dev.x, origin_dev.x,
818  origin_dev.y, origin_dev.y,
819  size_dev.y, fabs( size_dev.x / size_dev.y ),
820  aOrient / 10.0,
821  aItalic ? DXF_OBLIQUE_ANGLE : 0,
822  size_dev.x < 0 ? 2 : 0, // X mirror flag
823  h_code, v_code );
824 
825  /* There are two issue in emitting the text:
826  - Our overline character (~) must be converted to the appropriate
827  control sequence %%O or %%o
828  - Text encoding in DXF is more or less unspecified since depends on
829  the DXF declared version, the acad version reading it *and* some
830  system variables to be put in the header handled only by newer acads
831  Also before R15 unicode simply is not supported (you need to use
832  bigfonts which are a massive PITA). Common denominator solution:
833  use Latin1 (and however someone could choke on it, anyway). Sorry
834  for the extended latin people. If somewant want to try fixing this
835  recent version seems to use UTF-8 (and not UCS2 like the rest of
836  Windows)
837 
838  XXX Actually there is a *third* issue: older DXF formats are limited
839  to 255 bytes records (it was later raised to 2048); since I'm lazy
840  and text so long is not probable I just don't implement this rule.
841  If someone is interested in fixing this, you have to emit the first
842  partial lines with group code 3 (max 250 bytes each) and then finish
843  with a group code 1 (less than 250 bytes). The DXF refs explains it
844  in no more details...
845  */
846 
847  bool overlining = false;
848  fputs( " 1\n", outputFile );
849  for( unsigned i = 0; i < aText.length(); i++ )
850  {
851  /* Here I do a bad thing: writing the output one byte at a time!
852  but today I'm lazy and I have no idea on how to coerce a Unicode
853  wxString to spit out latin1 encoded text ...
854 
855  Atleast stdio is *supposed* to do output buffering, so there is
856  hope is not too slow */
857  wchar_t ch = aText[i];
858  if( ch > 255 )
859  {
860  // I can't encode this...
861  putc( '?', outputFile );
862  }
863  else
864  {
865  if( ch == '~' )
866  {
867  // Handle the overline toggle
868  fputs( overlining ? "%%o" : "%%O", outputFile );
869  overlining = !overlining;
870  }
871  else
872  {
873  putc( ch, outputFile );
874  }
875  }
876  }
877  putc( '\n', outputFile );
878  }
879 }
880 
void TransformRoundedEndsSegmentToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aStart, wxPoint aEnd, int aCircleToSegmentsCount, int aWidth)
Function TransformRoundedEndsSegmentToPolygon convert a segment with rounded ends to a polygon Conver...
void FinishTo(const wxPoint &pos)
Definition: plot_common.h:241
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=NULL) override
DXF polygon: doesn't fill it but at least it close the filled ones DXF does not know thick outline...
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
EDA_COLOR_T ColorFindNearest(const wxColour &aColor)
Find the nearest color match.
Definition: colors.cpp:96
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:47
void PenFinish()
Definition: plot_common.h:247
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL) override
Draws text with the plotter.
void BooleanAdd(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset union For aFastMode meaning, see function booleanOp
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the scale/position for the DXF plot The DXF engine doesn't support line widths and mirroring...
int PointCount() const
Function PointCount()
bool textAsLines
Definition: plot_common.h:1296
void TransformRoundRectToPolygon(SHAPE_POLY_SET &aCornerBuffer, const wxPoint &aPosition, const wxSize &aSize, double aRotation, int aCornerRadius, int aCircleToSegmentsCount)
Function TransformRoundRectToPolygon convert a rectangle with rounded corners to a polygon Convert ar...
virtual void SetDash(bool dashed) override
Dashed lines are not (yet) supported by DXF_PLOTTER.
virtual void FlashPadCustom(const wxPoint &aPadPos, const wxSize &aSize, SHAPE_POLY_SET *aPolygons, EDA_DRAW_MODE_T aTraceMode, void *aData) override
virtual function FlashPadCustom
virtual void PenTo(const wxPoint &pos, char plume) override
moveto/lineto primitive, moves the 'pen' to the specified direction
double g
Green component.
Definition: color4d.h:282
static const double DXF_OBLIQUE_ANGLE
Oblique angle for DXF native text (I don't remember if 15 degrees is the ISO value...
bool colorMode
Definition: plot_common.h:537
static wxString getDXFColorName(COLOR4D aColor)
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
COLOR4D m_currentColor
Definition: plot_common.h:1297
int OutlineCount() const
Returns the number of outlines in the set
double m_IUsPerDecimil
Definition: plot_common.h:519
static const struct @44 dxf_layer[NBCOLORS]
wxPoint plotOffset
Plot offset (in IUs)
Definition: plot_common.h:525
wxPoint penLastpos
Last pen positions; set to -1,-1 when the pen is at rest.
Definition: plot_common.h:544
double b
Blue component.
Definition: color4d.h:283
This file contains miscellaneous commonly used macros and functions.
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL)
Draws text with the plotter.
Definition: drawtxt.cpp:227
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
virtual void ThickSegment(const wxPoint &start, const wxPoint &end, int width, EDA_DRAW_MODE_T tracemode, void *aData) override
void LineTo(const wxPoint &pos)
Definition: plot_common.h:236
bool m_plotMirror
X axis orientation (SVG) and plot mirrored (only for PS, PDF HPGL and SVG)
Definition: plot_common.h:529
virtual bool StartPlot() override
Opens the DXF plot with a skeleton header.
bool containsNonAsciiChars(const wxString &string)
Checks if a given string contains non-ASCII characters.
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:246
Class SHAPE_POLY_SET.
Base window classes and related definitions.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
virtual void FlashPadTrapez(const wxPoint &aPadPos, const wxPoint *aCorners, double aPadOrient, EDA_DRAW_MODE_T aTrace_Mode, void *aData) override
DXF trapezoidal pad: only sketch mode is supported.
EDA_DRAW_MODE_T
Definition: eda_text.h:62
double plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plot_common.h:513
virtual bool EndPlot() override
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
DXF rectangle: fill not supported.
virtual void FlashPadCircle(const wxPoint &pos, int diametre, EDA_DRAW_MODE_T trace_mode, void *aData) override
DXF round pad: always done in sketch mode; it could be filled but it isn't pretty if other kinds of p...
virtual DPOINT userToDeviceSize(const wxSize &size)
Modifies size according to the plotter scale factors (wxSize version, returns a DPOINT) ...
int NewOutline()
Creates a new empty polygon in the set and returns its index
void Fracture(POLYGON_MODE aFastMode)
Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the oute...
virtual void Circle(const wxPoint &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
DXF circle: full functionality; it even does 'fills' drawing a circle with a dual-arc polyline wide a...
virtual void FlashPadRect(const wxPoint &pos, const wxSize &size, double orient, EDA_DRAW_MODE_T trace_mode, void *aData) override
DXF rectangular pad: alwayd done in sketch mode.
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:54
void MoveTo(const wxPoint &pos)
Definition: plot_common.h:231
virtual DPOINT userToDeviceCoordinates(const wxPoint &aCoordinate)
Modifies coordinates according to the orientation, scale factor, and offsets trace.
Class SHAPE_LINE_CHAIN.
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
Definition: plot_common.h:1243
FILE * outputFile
true if the Y axis is top to bottom (SVG)
Definition: plot_common.h:534
virtual void SetColor(COLOR4D color) override
The DXF exporter handles 'colors' as layers...
Definition: colors.h:49
virtual void FlashPadOval(const wxPoint &pos, const wxSize &size, double orient, EDA_DRAW_MODE_T trace_mode, void *aData) override
DXF oval pad: always done in sketch mode.
const char * name
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:56
VECTOR2I & Point(int aIndex)
Function Point()
Definition: colors.h:45
double r
Red component.
Definition: color4d.h:281
Basic classes for most KiCad items.
virtual void Arc(const wxPoint &centre, double StAngle, double EndAngle, int rayon, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
Generic fallback: arc rendered as a polyline.
double iuPerDeviceUnit
Device scale (from IUs to plotter device units - usually decimils)
Definition: plot_common.h:522
void segmentAsOval(const wxPoint &start, const wxPoint &end, int width, EDA_DRAW_MODE_T tracemode)
Cdonvert a thick segment and plot it as an oval.
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
Number of colors.
Definition: colors.h:75
void sketchOval(const wxPoint &pos, const wxSize &size, double orient, int width)
virtual void FlashPadRoundRect(const wxPoint &aPadPos, const wxSize &aSize, int aCornerRadius, double aOrient, EDA_DRAW_MODE_T aTraceMode, void *aData) override
virtual function FlashPadRoundRect
wxSize paperSize
Paper size in IU - not in mils.
Definition: plot_common.h:550
EDA_COLOR_T NextColor(EDA_COLOR_T &aColor)
Definition: colors.h:87
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline) ...
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39