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  MoveTo( aStart );
509  FinishTo( aEnd );
510 }
511 
512 /* Plot an arc in DXF format
513  * Filling is not supported
514  */
515 void DXF_PLOTTER::Arc( const wxPoint& centre, double StAngle, double EndAngle, int radius,
516  FILL_T fill, int width )
517 {
518  wxASSERT( outputFile );
519 
520  if( radius <= 0 )
521  return;
522 
523  // In DXF, arcs are drawn CCW.
524  // In Kicad, arcs are CW or CCW
525  // If StAngle > EndAngle, it is CW. So transform it to CCW
526  if( StAngle > EndAngle )
527  {
528  std::swap( StAngle, EndAngle );
529  }
530 
531  DPOINT centre_dev = userToDeviceCoordinates( centre );
532  double radius_dev = userToDeviceSize( radius );
533 
534  // Emit a DXF ARC entity
535  wxString cname = getDXFColorName( m_currentColor );
536  fprintf( outputFile,
537  "0\nARC\n8\n%s\n10\n%g\n20\n%g\n40\n%g\n50\n%g\n51\n%g\n",
538  TO_UTF8( cname ),
539  centre_dev.x, centre_dev.y, radius_dev,
540  StAngle / 10.0, EndAngle / 10.0 );
541 }
542 
546 void DXF_PLOTTER::FlashPadOval( const wxPoint& pos, const wxSize& aSize, double orient,
547  EDA_DRAW_MODE_T trace_mode, void* aData )
548 {
549  wxASSERT( outputFile );
550  wxSize size( aSize );
551 
552  /* The chip is reduced to an oval tablet with size.y > size.x
553  * (Oval vertical orientation 0) */
554  if( size.x > size.y )
555  {
556  std::swap( size.x, size.y );
557  orient = AddAngles( orient, 900 );
558  }
559 
560  sketchOval( pos, size, orient, -1 );
561 }
562 
563 
568 void DXF_PLOTTER::FlashPadCircle( const wxPoint& pos, int diametre,
569  EDA_DRAW_MODE_T trace_mode, void* aData )
570 {
571  wxASSERT( outputFile );
572  Circle( pos, diametre, NO_FILL );
573 }
574 
575 
579 void DXF_PLOTTER::FlashPadRect( const wxPoint& pos, const wxSize& padsize,
580  double orient, EDA_DRAW_MODE_T trace_mode, void* aData )
581 {
582  wxASSERT( outputFile );
583  wxSize size;
584  int ox, oy, fx, fy;
585 
586  size.x = padsize.x / 2;
587  size.y = padsize.y / 2;
588 
589  if( size.x < 0 )
590  size.x = 0;
591  if( size.y < 0 )
592  size.y = 0;
593 
594  // If a dimension is zero, the trace is reduced to 1 line
595  if( size.x == 0 )
596  {
597  ox = pos.x;
598  oy = pos.y - size.y;
599  RotatePoint( &ox, &oy, pos.x, pos.y, orient );
600  fx = pos.x;
601  fy = pos.y + size.y;
602  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
603  MoveTo( wxPoint( ox, oy ) );
604  FinishTo( wxPoint( fx, fy ) );
605  return;
606  }
607  if( size.y == 0 )
608  {
609  ox = pos.x - size.x;
610  oy = pos.y;
611  RotatePoint( &ox, &oy, pos.x, pos.y, orient );
612  fx = pos.x + size.x;
613  fy = pos.y;
614  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
615  MoveTo( wxPoint( ox, oy ) );
616  FinishTo( wxPoint( fx, fy ) );
617  return;
618  }
619 
620  ox = pos.x - size.x;
621  oy = pos.y - size.y;
622  RotatePoint( &ox, &oy, pos.x, pos.y, orient );
623  MoveTo( wxPoint( ox, oy ) );
624 
625  fx = pos.x - size.x;
626  fy = pos.y + size.y;
627  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
628  LineTo( wxPoint( fx, fy ) );
629 
630  fx = pos.x + size.x;
631  fy = pos.y + size.y;
632  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
633  LineTo( wxPoint( fx, fy ) );
634 
635  fx = pos.x + size.x;
636  fy = pos.y - size.y;
637  RotatePoint( &fx, &fy, pos.x, pos.y, orient );
638  LineTo( wxPoint( fx, fy ) );
639 
640  FinishTo( wxPoint( ox, oy ) );
641 }
642 
643 void DXF_PLOTTER::FlashPadRoundRect( const wxPoint& aPadPos, const wxSize& aSize,
644  int aCornerRadius, double aOrient,
645  EDA_DRAW_MODE_T aTraceMode, void* aData )
646 {
647  SHAPE_POLY_SET outline;
648  const int segmentToCircleCount = 64;
649  TransformRoundRectToPolygon( outline, aPadPos, aSize, aOrient,
650  aCornerRadius, segmentToCircleCount );
651 
652  // TransformRoundRectToPolygon creates only one convex polygon
653  SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
654 
655  MoveTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
656 
657  for( int ii = 1; ii < poly.PointCount(); ++ii )
658  LineTo( wxPoint( poly.Point( ii ).x, poly.Point( ii ).y ) );
659 
660  FinishTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
661 }
662 
663 void DXF_PLOTTER::FlashPadCustom( const wxPoint& aPadPos, const wxSize& aSize,
664  SHAPE_POLY_SET* aPolygons,
665  EDA_DRAW_MODE_T aTraceMode, void* aData )
666 {
667  for( int cnt = 0; cnt < aPolygons->OutlineCount(); ++cnt )
668  {
669  SHAPE_LINE_CHAIN& poly = aPolygons->Outline( cnt );
670 
671  MoveTo( wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
672 
673  for( int ii = 1; ii < poly.PointCount(); ++ii )
674  LineTo( wxPoint( poly.Point( ii ).x, poly.Point( ii ).y ) );
675 
676  FinishTo(wxPoint( poly.Point( 0 ).x, poly.Point( 0 ).y ) );
677  }
678 }
679 
680 
684 void DXF_PLOTTER::FlashPadTrapez( const wxPoint& aPadPos, const wxPoint *aCorners,
685  double aPadOrient, EDA_DRAW_MODE_T aTrace_Mode, void* aData )
686 {
687  wxASSERT( outputFile );
688  wxPoint coord[4]; /* coord actual corners of a trapezoidal trace */
689 
690  for( int ii = 0; ii < 4; ii++ )
691  {
692  coord[ii] = aCorners[ii];
693  RotatePoint( &coord[ii], aPadOrient );
694  coord[ii] += aPadPos;
695  }
696 
697  // Plot edge:
698  MoveTo( coord[0] );
699  LineTo( coord[1] );
700  LineTo( coord[2] );
701  LineTo( coord[3] );
702  FinishTo( coord[0] );
703 }
704 
713 bool containsNonAsciiChars( const wxString& string )
714 {
715  for( unsigned i = 0; i < string.length(); i++ )
716  {
717  wchar_t ch = string[i];
718  if( ch > 255 )
719  return true;
720  }
721  return false;
722 }
723 
724 void DXF_PLOTTER::Text( const wxPoint& aPos,
725  COLOR4D aColor,
726  const wxString& aText,
727  double aOrient,
728  const wxSize& aSize,
729  enum EDA_TEXT_HJUSTIFY_T aH_justify,
730  enum EDA_TEXT_VJUSTIFY_T aV_justify,
731  int aWidth,
732  bool aItalic,
733  bool aBold,
734  bool aMultilineAllowed,
735  void* aData )
736 {
737  // Fix me: see how to use DXF text mode for multiline texts
738  if( aMultilineAllowed && !aText.Contains( wxT( "\n" ) ) )
739  aMultilineAllowed = false; // the text has only one line.
740 
741  if( textAsLines || containsNonAsciiChars( aText ) || aMultilineAllowed )
742  {
743  // output text as graphics.
744  // Perhaps multiline texts could be handled as DXF text entity
745  // but I do not want spend time about this (JPC)
746  PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify,
747  aWidth, aItalic, aBold, aMultilineAllowed );
748  }
749  else
750  {
751  /* Emit text as a text entity. This loses formatting and shape but it's
752  more useful as a CAD object */
753  DPOINT origin_dev = userToDeviceCoordinates( aPos );
754  SetColor( aColor );
755  wxString cname = getDXFColorName( m_currentColor );
756  DPOINT size_dev = userToDeviceSize( aSize );
757  int h_code = 0, v_code = 0;
758  switch( aH_justify )
759  {
761  h_code = 0;
762  break;
764  h_code = 1;
765  break;
767  h_code = 2;
768  break;
769  }
770  switch( aV_justify )
771  {
773  v_code = 3;
774  break;
776  v_code = 2;
777  break;
779  v_code = 1;
780  break;
781  }
782 
783  // Position, size, rotation and alignment
784  // The two alignment point usages is somewhat idiot (see the DXF ref)
785  // Anyway since we don't use the fit/aligned options, they're the same
786  fprintf( outputFile,
787  " 0\n"
788  "TEXT\n"
789  " 7\n"
790  "%s\n" // Text style
791  " 8\n"
792  "%s\n" // Layer name
793  " 10\n"
794  "%g\n" // First point X
795  " 11\n"
796  "%g\n" // Second point X
797  " 20\n"
798  "%g\n" // First point Y
799  " 21\n"
800  "%g\n" // Second point Y
801  " 40\n"
802  "%g\n" // Text height
803  " 41\n"
804  "%g\n" // Width factor
805  " 50\n"
806  "%g\n" // Rotation
807  " 51\n"
808  "%g\n" // Oblique angle
809  " 71\n"
810  "%d\n" // Mirror flags
811  " 72\n"
812  "%d\n" // H alignment
813  " 73\n"
814  "%d\n", // V alignment
815  aBold ? (aItalic ? "KICADBI" : "KICADB")
816  : (aItalic ? "KICADI" : "KICAD"),
817  TO_UTF8( cname ),
818  origin_dev.x, origin_dev.x,
819  origin_dev.y, origin_dev.y,
820  size_dev.y, fabs( size_dev.x / size_dev.y ),
821  aOrient / 10.0,
822  aItalic ? DXF_OBLIQUE_ANGLE : 0,
823  size_dev.x < 0 ? 2 : 0, // X mirror flag
824  h_code, v_code );
825 
826  /* There are two issue in emitting the text:
827  - Our overline character (~) must be converted to the appropriate
828  control sequence %%O or %%o
829  - Text encoding in DXF is more or less unspecified since depends on
830  the DXF declared version, the acad version reading it *and* some
831  system variables to be put in the header handled only by newer acads
832  Also before R15 unicode simply is not supported (you need to use
833  bigfonts which are a massive PITA). Common denominator solution:
834  use Latin1 (and however someone could choke on it, anyway). Sorry
835  for the extended latin people. If somewant want to try fixing this
836  recent version seems to use UTF-8 (and not UCS2 like the rest of
837  Windows)
838 
839  XXX Actually there is a *third* issue: older DXF formats are limited
840  to 255 bytes records (it was later raised to 2048); since I'm lazy
841  and text so long is not probable I just don't implement this rule.
842  If someone is interested in fixing this, you have to emit the first
843  partial lines with group code 3 (max 250 bytes each) and then finish
844  with a group code 1 (less than 250 bytes). The DXF refs explains it
845  in no more details...
846  */
847 
848  bool overlining = false;
849  fputs( " 1\n", outputFile );
850  for( unsigned i = 0; i < aText.length(); i++ )
851  {
852  /* Here I do a bad thing: writing the output one byte at a time!
853  but today I'm lazy and I have no idea on how to coerce a Unicode
854  wxString to spit out latin1 encoded text ...
855 
856  Atleast stdio is *supposed* to do output buffering, so there is
857  hope is not too slow */
858  wchar_t ch = aText[i];
859  if( ch > 255 )
860  {
861  // I can't encode this...
862  putc( '?', outputFile );
863  }
864  else
865  {
866  if( ch == '~' )
867  {
868  // Handle the overline toggle
869  fputs( overlining ? "%%o" : "%%O", outputFile );
870  overlining = !overlining;
871  }
872  else
873  {
874  putc( ch, outputFile );
875  }
876  }
877  }
878  putc( '\n', outputFile );
879  }
880 }
881 
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
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
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
Number of colors.
Definition: colors.h:75
static const struct @38 dxf_layer[NBCOLORS]
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