KiCad PCB EDA Suite
rs274x.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) 2007-2018 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 #include <fctsys.h>
30 #include <common.h>
31 #include <macros.h>
32 #include <base_units.h>
33 #include <math/util.h> // for KiROUND
34 
35 #include <gerbview.h>
36 #include <gerber_file_image.h>
37 #include <X2_gerber_attributes.h>
38 #include <gbr_metadata.h>
39 
40 extern int ReadInt( char*& text, bool aSkipSeparator = true );
41 extern double ReadDouble( char*& text, bool aSkipSeparator = true );
42 
43 
44 #define CODE( x, y ) ( ( (x) << 8 ) + (y) )
45 
46 // See rs274xrevd_e.pdf, table 1: RS-274X parameters order of entry
47 // in gerber files, when a coordinate is given (like X78Y600 or I0J80):
48 // Y and Y are logical coordinates
49 // A and B are plotter coordiantes
50 // Usually A = X, B = Y
51 // But we can have A = Y, B = X and/or offset, mirror, scale;
52 // Also:
53 // Image is what you must plot (the entire data of the file).
54 // Layer is just a set of data blocks with their parameters. An image can have more than one
55 // layer so a gerber layer is not like a board layer or the graphic layers used in GerbView
56 // to show a file.
58  // Directive parameters: single usage recommended
59  // Must be at the beginning of the file
60  AXIS_SELECT = CODE( 'A', 'S' ), // Default: A=X, B=Y
61  FORMAT_STATEMENT = CODE( 'F', 'S' ), // no default: this command must exists
62  MIRROR_IMAGE = CODE( 'M', 'I' ), // Default: mo mirror
63  MODE_OF_UNITS = CODE( 'M', 'O' ), // Default: inch
64  INCH = CODE( 'I', 'N' ),
65  MILLIMETER = CODE( 'M', 'M' ),
66  OFFSET = CODE( 'O', 'F' ), // Default: A = 0, B = 0
67  SCALE_FACTOR = CODE( 'S', 'F' ), // Default: A = 1.0, B = 1.0
68 
69  // Image parameters:
70  // commands used only once at the beginning of the file, and are deprecated
71  IMAGE_JUSTIFY = CODE( 'I', 'J' ), // Default: no justification
72  IMAGE_NAME = CODE( 'I', 'N' ), // Default: void
73  IMAGE_OFFSET = CODE( 'I', 'O' ), // Default: A = 0, B = 0
74  IMAGE_POLARITY = CODE( 'I', 'P' ), // Default: Positive
75  IMAGE_ROTATION = CODE( 'I', 'R' ), // Default: 0
76 
77  // Aperture parameters:
78  // Usually for the whole file
79  AP_DEFINITION = CODE( 'A', 'D' ),
80  AP_MACRO = CODE( 'A', 'M' ),
81 
82  // X2 extension attribute commands
83  // Mainly are found standard attributes and user attributes
84  // standard attributes commands are:
85  // TF (file attribute) TO (net attribute)
86  // TA (aperture attribute) and TD (delete aperture attribute)
87  FILE_ATTRIBUTE = CODE( 'T', 'F' ),
88 
89  // X2 extension Net attribute info
90  // Net attribute options are:
91  // TO (net attribute data): TO.CN or TO.P TO.N or TO.C
92  NET_ATTRIBUTE = CODE( 'T', 'O' ),
93 
94  // X2 extension Aperture attribute TA
95  APERTURE_ATTRIBUTE = CODE( 'T', 'A' ),
96 
97  // TD (delete aperture/object attribute):
98  // Delete aperture attribute added by %TA or Oblect attribute added b %TO
99  // TD (delete all) or %TD<attr name> to delete <attr name>.
100  // eg: TD.P or TD.N or TD.C ...
102 
103  // Layer specific parameters
104  // May be used singly or may be layer specfic
105  // These parameters are at the beginning of the file or layer
106  // and reset some layer parameters (like interpolation)
107  KNOCKOUT = CODE( 'K', 'O' ), // Default: off
108  STEP_AND_REPEAT = CODE( 'S', 'R' ), // Default: A = 1, B = 1
109  ROTATE = CODE( 'R', 'O' ), // Default: 0
110 
111  LOAD_POLARITY = CODE( 'L', 'P' ), //LPC or LPD. Default: Dark (LPD)
112  LOAD_NAME = CODE( 'L', 'N' ), // Deprecated: equivalent to G04
113 };
114 
115 
117 {
118  /* reads two bytes of data and assembles them into an int with the first
119  * byte in the sequence put into the most significant part of a 16 bit value
120  */
121  int result;
122  int currbyte;
123 
124  if( text && *text )
125  {
126  currbyte = *text++;
127  result = ( currbyte & 0xFF ) << 8;
128  }
129  else
130  return -1;
131 
132  if( text && *text )
133  {
134  currbyte = *text++;
135  result += currbyte & 0xFF;
136  }
137  else
138  return -1;
139 
140  return result;
141 }
142 
143 
144 bool GERBER_FILE_IMAGE::ReadRS274XCommand( char *aBuff, unsigned int aBuffSize, char*& aText )
145 {
146  bool ok = true;
147  int code_command;
148 
149  aText++;
150 
151  for( ; ; )
152  {
153  while( *aText )
154  {
155  switch( *aText )
156  {
157  case '%': // end of command
158  aText++;
160  goto exit; // success completion
161 
162  case ' ':
163  case '\r':
164  case '\n':
165  aText++;
166  break;
167 
168  case '*':
169  aText++;
170  break;
171 
172  default:
173  code_command = ReadXCommandID( aText );
174  ok = ExecuteRS274XCommand( code_command, aBuff, aBuffSize, aText );
175  if( !ok )
176  goto exit;
177  break;
178  }
179  }
180 
181  // end of current line, read another one.
182  if( fgets( aBuff, aBuffSize, m_Current_File ) == NULL )
183  {
184  // end of file
185  ok = false;
186  break;
187  }
188  m_LineNum++;
189  aText = aBuff;
190  }
191 
192 exit:
193  return ok;
194 }
195 
196 
197 bool GERBER_FILE_IMAGE::ExecuteRS274XCommand( int aCommand, char* aBuff,
198  unsigned int aBuffSize, char*& aText )
199 {
200  int code;
201  int seq_len; // not used, just provided
202  int seq_char;
203  bool ok = true;
204  wxString msg;
205  double fcoord;
206  bool x_fmt_known = false;
207  bool y_fmt_known = false;
208 
209  // conv_scale = scaling factor from inch to Internal Unit
210  double conv_scale = IU_PER_MILS * 1000;
211 
212  if( m_GerbMetric )
213  conv_scale /= 25.4;
214 
215  switch( aCommand )
216  {
217  case FORMAT_STATEMENT:
218  seq_len = 2;
219 
220  while( *aText != '*' )
221  {
222  switch( *aText )
223  {
224  case ' ':
225  aText++;
226  break;
227 
228  case 'D': // Non-standard option for all zeros (leading + tailing)
229  msg.Printf( _( "RS274X: Invalid GERBER format command '%c' at line %d: \"%s\"" ),
230  'D', m_LineNum, aBuff );
231  AddMessageToList( msg );
232  msg.Printf( _("GERBER file \"%s\" may not display as intended." ),
233  m_FileName.ToAscii() );
234  AddMessageToList( msg );
235  // Fallthrough
236 
237  case 'L': // No Leading 0
238  m_NoTrailingZeros = false;
239  aText++;
240  break;
241 
242  case 'T': // No trailing 0
243  m_NoTrailingZeros = true;
244  aText++;
245  break;
246 
247  case 'A': // Absolute coord
248  m_Relative = false;
249  aText++;
250  break;
251 
252  case 'I': // Relative coord
253  m_Relative = true;
254  aText++;
255  break;
256 
257  case 'G':
258  case 'N': // Sequence code (followed by one digit: the sequence len)
259  // (sometimes found before the X,Y sequence)
260  // Obscure option
261  aText++;
262  seq_char = *aText++;
263  if( (seq_char >= '0') && (seq_char <= '9') )
264  seq_len = seq_char - '0';
265  break;
266 
267  case 'M': // Sequence code (followed by one digit: the sequence len)
268  // (sometimes found after the X,Y sequence)
269  // Obscure option
270  code = *aText++;
271  if( ( *aText >= '0' ) && ( *aText<= '9' ) )
272  aText++; // skip the digit
273  break;
274 
275  case 'X':
276  case 'Y':
277  {
278  code = *(aText++);
279  char ctmp = *(aText++) - '0';
280  if( code == 'X' )
281  {
282  x_fmt_known = true;
283  // number of digits after the decimal point (0 to 7 allowed)
284  m_FmtScale.x = *aText - '0';
285  m_FmtLen.x = ctmp + m_FmtScale.x;
286 
287  // m_FmtScale is 0 to 7
288  // (Old Gerber specification was 0 to 6)
289  if( m_FmtScale.x < 0 )
290  m_FmtScale.x = 0;
291  if( m_FmtScale.x > 7 )
292  m_FmtScale.x = 7;
293  }
294  else
295  {
296  y_fmt_known = true;
297  m_FmtScale.y = *aText - '0';
298  m_FmtLen.y = ctmp + m_FmtScale.y;
299  if( m_FmtScale.y < 0 )
300  m_FmtScale.y = 0;
301  if( m_FmtScale.y > 7 )
302  m_FmtScale.y = 7;
303  }
304  aText++;
305  }
306  break;
307 
308  case '*':
309  break;
310 
311  default:
312  msg.Printf( wxT( "Unknown id (%c) in FS command" ),
313  *aText );
314  AddMessageToList( msg );
315  GetEndOfBlock( aBuff, aBuffSize, aText, m_Current_File );
316  ok = false;
317  break;
318  }
319  }
320  if( !x_fmt_known || !y_fmt_known )
321  AddMessageToList( wxT( "RS274X: Format Statement (FS) without X or Y format" ) );
322 
323  break;
324 
325  case AXIS_SELECT: // command ASAXBY*% or %ASAYBX*%
326  m_SwapAxis = false;
327  if( strncasecmp( aText, "AYBX", 4 ) == 0 )
328  m_SwapAxis = true;
329  break;
330 
331  case MIRROR_IMAGE: // command %MIA0B0*%, %MIA0B1*%, %MIA1B0*%, %MIA1B1*%
332  m_MirrorA = m_MirrorB = 0;
333  while( *aText && *aText != '*' )
334  {
335  switch( *aText )
336  {
337  case 'A': // Mirror A axis ?
338  aText++;
339  if( *aText == '1' )
340  m_MirrorA = true;
341  break;
342 
343  case 'B': // Mirror B axis ?
344  aText++;
345  if( *aText == '1' )
346  m_MirrorB = true;
347  break;
348 
349  default:
350  aText++;
351  break;
352  }
353  }
354  break;
355 
356  case MODE_OF_UNITS:
357  code = ReadXCommandID( aText );
358  if( code == INCH )
359  m_GerbMetric = false;
360  else if( code == MILLIMETER )
361  m_GerbMetric = true;
362  conv_scale = m_GerbMetric ? IU_PER_MILS / 25.4 : IU_PER_MILS;
363  break;
364 
365  case FILE_ATTRIBUTE: // Command %TF ...
366  m_IsX2_file = true;
367  {
369  dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
370 
371  if( dummy.IsFileFunction() )
372  {
373  delete m_FileFunction;
375  }
376  else if( dummy.IsFileMD5() )
377  {
378  m_MD5_value = dummy.GetPrm( 1 );
379  }
380  else if( dummy.IsFilePart() )
381  {
382  m_PartString = dummy.GetPrm( 1 );
383  }
384  }
385  break;
386 
387  case APERTURE_ATTRIBUTE: // Command %TA
388  {
390  dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
391 
392  if( dummy.GetAttribute() == ".AperFunction" )
393  {
394  m_AperFunction = dummy.GetPrm( 1 );
395 
396  // A few function values can have other parameters. Add them
397  for( int ii = 2; ii < dummy.GetPrmCount(); ii++ )
398  m_AperFunction << "," << dummy.GetPrm( ii );
399  }
400  }
401  break;
402 
403  case NET_ATTRIBUTE: // Command %TO currently %TO.P %TO.N and %TO.C
404  {
406 
407  dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
408 
409  if( dummy.GetAttribute() == ".N" )
410  {
413  }
414  else if( dummy.GetAttribute() == ".C" )
415  {
418  }
419  else if( dummy.GetAttribute() == ".P" )
420  {
423  m_NetAttributeDict.m_Padname.SetField( FormatStringFromGerber( dummy.GetPrm( 2 ) ), true, true );
424 
425  if( dummy.GetPrmCount() > 3 )
427  FormatStringFromGerber( dummy.GetPrm( 3 ) ), true, true );
428  else
430  }
431  }
432  break;
433 
434  case REMOVE_APERTURE_ATTRIBUTE: // Command %TD ...
435  {
437  dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
439  }
440  break;
441 
442  case OFFSET: // command: OFAnnBnn (nn = float number) = layer Offset
443  m_Offset.x = m_Offset.y = 0;
444  while( *aText != '*' )
445  {
446  switch( *aText )
447  {
448  case 'A': // A axis offset in current unit (inch or mm)
449  aText++;
450  fcoord = ReadDouble( aText );
451  m_Offset.x = KiROUND( fcoord * conv_scale );
452  break;
453 
454  case 'B': // B axis offset in current unit (inch or mm)
455  aText++;
456  fcoord = ReadDouble( aText );
457  m_Offset.y = KiROUND( fcoord * conv_scale );
458  break;
459  }
460  }
461  break;
462 
463  case SCALE_FACTOR:
464  m_Scale.x = m_Scale.y = 1.0;
465  while( *aText != '*' )
466  {
467  switch( *aText )
468  {
469  case 'A': // A axis scale
470  aText++;
471  m_Scale.x = ReadDouble( aText );
472  break;
473 
474  case 'B': // B axis scale
475  aText++;
476  m_Scale.y = ReadDouble( aText );
477  break;
478  }
479  }
480  break;
481 
482  case IMAGE_OFFSET: // command: IOAnnBnn (nn = float number) = Image Offset
484  while( *aText != '*' )
485  {
486  switch( *aText )
487  {
488  case 'A': // A axis offset in current unit (inch or mm)
489  aText++;
490  fcoord = ReadDouble( aText );
491  m_ImageOffset.x = KiROUND( fcoord * conv_scale );
492  break;
493 
494  case 'B': // B axis offset in current unit (inch or mm)
495  aText++;
496  fcoord = ReadDouble( aText );
497  m_ImageOffset.y = KiROUND( fcoord * conv_scale );
498  break;
499  }
500  }
501  break;
502 
503  case IMAGE_ROTATION: // command IR0* or IR90* or IR180* or IR270*
504  if( strncasecmp( aText, "0*", 2 ) == 0 )
505  m_ImageRotation = 0;
506  else if( strncasecmp( aText, "90*", 3 ) == 0 )
507  m_ImageRotation = 90;
508  else if( strncasecmp( aText, "180*", 4 ) == 0 )
509  m_ImageRotation = 180;
510  else if( strncasecmp( aText, "270*", 4 ) == 0 )
511  m_ImageRotation = 270;
512  else
513  AddMessageToList( _( "RS274X: Command \"IR\" rotation value not allowed" ) );
514  break;
515 
516  case STEP_AND_REPEAT: // command SR, like %SRX3Y2I5.0J2*%
517  m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
519  GetLayerParams().m_StepForRepeat.x = 0.0; // offset for Step and Repeat command
521  GetLayerParams().m_YRepeatCount = 1; // The repeat count
522  GetLayerParams().m_StepForRepeatMetric = m_GerbMetric; // the step units
523  while( *aText && *aText != '*' )
524  {
525  switch( *aText )
526  {
527  case 'I': // X axis offset
528  aText++;
530  break;
531 
532  case 'J': // Y axis offset
533  aText++;
535  break;
536 
537  case 'X': // X axis repeat count
538  aText++;
540  break;
541 
542  case 'Y': // Y axis offset
543  aText++;
545  break;
546  default:
547  aText++;
548  break;
549  }
550  }
551  break;
552 
553  case IMAGE_JUSTIFY: // Command IJAnBn*
554  m_ImageJustifyXCenter = false; // Image Justify Center on X axis (default = false)
555  m_ImageJustifyYCenter = false; // Image Justify Center on Y axis (default = false)
556  m_ImageJustifyOffset = wxPoint(0,0); // Image Justify Offset on XY axis (default = 0,0)
557  while( *aText && *aText != '*' )
558  {
559  // IJ command is (for A or B axis) AC or AL or A<coordinate>
560  switch( *aText )
561  {
562  case 'A': // A axis justify
563  aText++;
564  if( *aText == 'C' )
565  {
566  m_ImageJustifyXCenter = true;
567  aText++;
568  }
569  else if( *aText == 'L' )
570  {
571  m_ImageJustifyXCenter = true;
572  aText++;
573  }
574  else m_ImageJustifyOffset.x = KiROUND( ReadDouble( aText ) * conv_scale);
575  break;
576 
577  case 'B': // B axis justify
578  aText++;
579  if( *aText == 'C' )
580  {
581  m_ImageJustifyYCenter = true;
582  aText++;
583  }
584  else if( *aText == 'L' )
585  {
586  m_ImageJustifyYCenter = true;
587  aText++;
588  }
589  else m_ImageJustifyOffset.y = KiROUND( ReadDouble( aText ) * conv_scale);
590  break;
591  default:
592  aText++;
593  break;
594  }
595  }
600  break;
601 
602  case KNOCKOUT:
603  m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
604  msg = _( "RS274X: Command KNOCKOUT ignored by GerbView" ) ;
605  AddMessageToList( msg );
606  break;
607 
608  case ROTATE: // Layer rotation: command like %RO45*%
609  m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
610  m_LocalRotation =ReadDouble( aText ); // Store layer rotation in degrees
611  break;
612 
613  case IMAGE_NAME:
614  m_ImageName.Empty();
615  while( *aText != '*' )
616  {
617  m_ImageName.Append( *aText++ );
618  }
619 
620  break;
621 
622  case LOAD_NAME:
623  // %LN is a (deprecated) equivalentto G04: a comment
624  while( *aText && *aText != '*' )
625  {
626  aText++; // Skip text
627  }
628 
629  break;
630 
631  case IMAGE_POLARITY:
632  if( strncasecmp( aText, "NEG", 3 ) == 0 )
633  m_ImageNegative = true;
634  else
635  m_ImageNegative = false;
636 
637  break;
638 
639  case LOAD_POLARITY:
640  if( *aText == 'C' )
642  else
644 
645  break;
646 
647  case AP_MACRO: // lines like %AMMYMACRO*
648  // 5,1,8,0,0,1.08239X$1,22.5*
649  // %
650  /*ok = */ReadApertureMacro( aBuff, aBuffSize, aText, m_Current_File );
651  break;
652 
653  case AP_DEFINITION:
654  /* input example: %ADD30R,0.081800X0.101500*%
655  * Aperture definition has 4 options: C, R, O, P
656  * (Circle, Rect, Oval, regular Polygon)
657  * and shapes can have a hole (round or rectangular).
658  * All optional parameters values start by X
659  * at this point, text points to 2nd 'D'
660  */
661  if( *aText++ != 'D' )
662  {
663  ok = false;
664  break;
665  }
666 
667  m_Has_DCode = true;
668 
669  code = ReadInt( aText );
670 
671  D_CODE* dcode;
672  dcode = GetDCODEOrCreate( code );
673 
674  if( dcode == NULL )
675  break;
676 
678 
679  // at this point, text points to character after the ADD<num>,
680  // i.e. R in example above. If aText[0] is one of the usual
681  // apertures: (C,R,O,P), there is a comma after it.
682  if( aText[1] == ',' )
683  {
684  char stdAperture = *aText;
685 
686  aText += 2; // skip "C," for example
687 
688  dcode->m_Size.x = KiROUND( ReadDouble( aText ) * conv_scale );
689  dcode->m_Size.y = dcode->m_Size.x;
690 
691  switch( stdAperture ) // Aperture desceiption has optional parameters. Read them
692  {
693  case 'C': // Circle
694  dcode->m_Shape = APT_CIRCLE;
695  while( *aText == ' ' )
696  aText++;
697 
698  if( *aText == 'X' )
699  {
700  aText++;
701  dcode->m_Drill.x = dcode->m_Drill.y =
702  KiROUND( ReadDouble( aText ) * conv_scale );
704  }
705 
706  while( *aText == ' ' )
707  aText++;
708 
709  if( *aText == 'X' )
710  {
711  aText++;
712  dcode->m_Drill.y =
713  KiROUND( ReadDouble( aText ) * conv_scale );
714 
716  }
717  dcode->m_Defined = true;
718  break;
719 
720  case 'O': // oval
721  case 'R': // rect
722  dcode->m_Shape = (stdAperture == 'O') ? APT_OVAL : APT_RECT;
723 
724  while( *aText == ' ' )
725  aText++;
726 
727  if( *aText == 'X' )
728  {
729  aText++;
730  dcode->m_Size.y =
731  KiROUND( ReadDouble( aText ) * conv_scale );
732  }
733 
734  while( *aText == ' ' )
735  aText++;
736 
737  if( *aText == 'X' )
738  {
739  aText++;
740  dcode->m_Drill.x = KiROUND( ReadDouble( aText ) * conv_scale );
741  dcode->m_Drill.y = dcode->m_Drill.x;
743  }
744 
745  while( *aText == ' ' )
746  aText++;
747 
748  if( *aText == 'X' )
749  {
750  aText++;
751  dcode->m_Drill.y =
752  KiROUND( ReadDouble( aText ) * conv_scale );
754  }
755  dcode->m_Defined = true;
756  break;
757 
758  case 'P':
759 
760  /* Regular polygon: a command line like %ADD12P,0.040X10X25X0.025X0.025X0.0150*%
761  * params are: <diameter>, X<edge count>, X<Rotation>, X<X hole dim>, X<Y hole dim>
762  */
763  dcode->m_Shape = APT_POLYGON;
764  while( *aText == ' ' )
765  aText++;
766 
767  if( *aText == 'X' )
768  {
769  aText++;
770  dcode->m_EdgesCount = ReadInt( aText );
771  }
772 
773  while( *aText == ' ' )
774  aText++;
775 
776  if( *aText == 'X' )
777  {
778  aText++;
779  dcode->m_Rotation = ReadDouble( aText );
780  }
781 
782  while( *aText == ' ' )
783  aText++;
784 
785  if( *aText == 'X' )
786  {
787  aText++;
788  dcode->m_Drill.x = KiROUND( ReadDouble( aText ) * conv_scale );
789  dcode->m_Drill.y = dcode->m_Drill.x;
791  }
792 
793  while( *aText == ' ' )
794  aText++;
795 
796  if( *aText == 'X' )
797  {
798  aText++;
799  dcode->m_Drill.y = KiROUND( ReadDouble( aText ) * conv_scale );
801  }
802  dcode->m_Defined = true;
803  break;
804  }
805  }
806  else // aText[0] starts an aperture macro name
807  {
808  APERTURE_MACRO am_lookup;
809 
810  while( *aText && *aText != '*' && *aText != ',' )
811  am_lookup.name.Append( *aText++ );
812 
813  // When an aperture definition is like %AMLINE2* 22,1,$1,$2,0,0,-45*
814  // the ADDxx<MACRO_NAME> command has parameters, like %ADD14LINE2,0.8X0.5*%
815  if( *aText == ',' )
816  { // Read aperture macro parameters and store them
817  aText++; // aText points the first parameter
818 
819  while( *aText && *aText != '*' )
820  {
821  double param = ReadDouble( aText );
822  dcode->AppendParam( param );
823 
824  while( isspace( *aText ) )
825  aText++;
826 
827  // Skip 'X' separator:
828  if( *aText == 'X' || *aText == 'x' )
829  aText++;
830  }
831  }
832 
833  // lookup the aperture macro here.
834  APERTURE_MACRO* pam = FindApertureMacro( am_lookup );
835 
836  if( !pam )
837  {
838  msg.Printf( wxT( "RS274X: aperture macro %s not found\n" ),
839  TO_UTF8( am_lookup.name ) );
840  AddMessageToList( msg );
841  ok = false;
842  break;
843  }
844 
845  dcode->m_Shape = APT_MACRO;
846  dcode->SetMacro( pam );
847  }
848 
849  break;
850 
851  default:
852  ok = false;
853  break;
854  }
855 
856  (void) seq_len; // quiet g++, or delete the unused variable.
857 
858  ok = GetEndOfBlock( aBuff, aBuffSize, aText, m_Current_File );
859 
860  return ok;
861 }
862 
863 
864 bool GERBER_FILE_IMAGE::GetEndOfBlock( char* aBuff, unsigned int aBuffSize, char*& aText, FILE* gerber_file )
865 {
866  for( ; ; )
867  {
868  while( (aText < aBuff + aBuffSize) && *aText )
869  {
870  if( *aText == '*' )
871  return true;
872 
873  if( *aText == '%' )
874  return true;
875 
876  aText++;
877  }
878 
879  if( fgets( aBuff, aBuffSize, gerber_file ) == NULL )
880  break;
881 
882  m_LineNum++;
883  aText = aBuff;
884  }
885 
886  return false;
887 }
888 
889 
890 char* GERBER_FILE_IMAGE::GetNextLine( char *aBuff, unsigned int aBuffSize, char* aText, FILE* aFile )
891 {
892  for( ; ; )
893  {
894  switch (*aText )
895  {
896  case ' ': // skip blanks
897  case '\n':
898  case '\r': // Skip line terminators
899  ++aText;
900  break;
901 
902  case 0: // End of text found in aBuff: Read a new string
903  if( fgets( aBuff, aBuffSize, aFile ) == NULL )
904  return NULL;
905 
906  m_LineNum++;
907  aText = aBuff;
908  return aText;
909 
910  default:
911  return aText;
912  }
913  }
914  return aText;
915 }
916 
917 
918 bool GERBER_FILE_IMAGE::ReadApertureMacro( char *aBuff, unsigned int aBuffSize,
919  char*& aText,
920  FILE* gerber_file )
921 {
922  wxString msg;
923  APERTURE_MACRO am;
924 
925  // read macro name
926  while( *aText )
927  {
928  if( *aText == '*' )
929  {
930  ++aText;
931  break;
932  }
933 
934  am.name.Append( *aText++ );
935  }
936 
937  // Read aperture macro parameters
938  for( ; ; )
939  {
940  if( *aText == '*' )
941  ++aText;
942 
943  aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
944 
945  if( aText == NULL ) // End of File
946  return false;
947 
948  // aText points the beginning of a new line.
949 
950  // Test for the last line in aperture macro lis:
951  // last line is % or *% sometime found.
952  if( *aText == '*' )
953  ++aText;
954 
955  if( *aText == '%' )
956  break; // exit with aText still pointing at %
957 
958  int paramCount = 0; // will be set to the minimal parameters count,
959  // depending on the actual primitive
960  int primitive_type = AMP_UNKNOWN;
961  // Test for a valid symbol at the beginning of a description:
962  // it can be: a parameter declaration like $1=$2/4
963  // or a digit (macro primitive selection)
964  // all other symbols are illegal.
965  if( *aText == '$' ) // local parameter declaration, inside the aperture macro
966  {
967  am.m_localparamStack.push_back( AM_PARAM() );
968  AM_PARAM& param = am.m_localparamStack.back();
969  aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
970  if( aText == NULL) // End of File
971  return false;
972  param.ReadParam( aText );
973  continue;
974  }
975  else if( !isdigit(*aText) ) // Ill. symbol
976  {
977  msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": ill. symbol, line: \"%s\"" ),
978  GetChars( am.name ), GetChars( FROM_UTF8( aBuff ) ) );
979  AddMessageToList( msg );
980  primitive_type = AMP_COMMENT;
981  }
982  else
983  primitive_type = ReadInt( aText );
984 
985  bool is_comment = false;
986 
987  switch( primitive_type )
988  {
989  case AMP_COMMENT: // lines starting by 0 are a comment
990  paramCount = 0;
991  is_comment = true;
992  // Skip comment
993  while( *aText && ( *aText != '*' ) )
994  aText++;
995  break;
996 
997  case AMP_CIRCLE:
998  paramCount = 4; // minimal count. can have a optional parameter (rotation)
999  break;
1000 
1001  case AMP_LINE2:
1002  case AMP_LINE20:
1003  paramCount = 7;
1004  break;
1005 
1006  case AMP_LINE_CENTER:
1007  case AMP_LINE_LOWER_LEFT:
1008  paramCount = 6;
1009  break;
1010 
1011  case AMP_EOF:
1012  paramCount = 0;
1013  break;
1014 
1015  case AMP_OUTLINE:
1016  paramCount = 4; // partial count. other parameters are vertices and rotation
1017  // Second parameter is vertice (coordinate pairs) count.
1018  break;
1019 
1020  case AMP_POLYGON:
1021  paramCount = 6;
1022  break;
1023 
1024  case AMP_MOIRE:
1025  paramCount = 9;
1026  break;
1027 
1028  case AMP_THERMAL:
1029  paramCount = 6;
1030  break;
1031 
1032  default:
1033  msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": Invalid primitive id code %d, line %d: \"%s\"" ),
1034  GetChars( am.name ), primitive_type, m_LineNum, GetChars( FROM_UTF8( aBuff ) ) );
1035  AddMessageToList( msg );
1036  return false;
1037  }
1038 
1039  if( is_comment )
1040  continue;
1041 
1042  AM_PRIMITIVE prim( m_GerbMetric );
1043  prim.primitive_id = (AM_PRIMITIVE_ID) primitive_type;
1044  int ii;
1045 
1046  for( ii = 0; ii < paramCount && *aText && *aText != '*'; ++ii )
1047  {
1048  prim.params.push_back( AM_PARAM() );
1049 
1050  AM_PARAM& param = prim.params.back();
1051 
1052  aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
1053 
1054  if( aText == NULL) // End of File
1055  return false;
1056 
1057  param.ReadParam( aText );
1058  }
1059 
1060  if( ii < paramCount )
1061  {
1062  // maybe some day we can throw an exception and track a line number
1063  msg.Printf( "RS274X: read macro descr type %d: read %d parameters, insufficient parameters\n",
1064  prim.primitive_id, ii );
1065  AddMessageToList( msg );
1066  }
1067 
1068  // there are more parameters to read if this is an AMP_OUTLINE
1069  if( prim.primitive_id == AMP_OUTLINE )
1070  {
1071  // so far we have read [0]:exposure, [1]:#points, [2]:X start, [3]: Y start
1072  // Now read all the points, plus trailing rotation in degrees.
1073 
1074  // params[1] is a count of polygon points, so it must be given
1075  // in advance, i.e. be immediate.
1076  wxASSERT( prim.params[1].IsImmediate() );
1077 
1078  paramCount = (int) prim.params[1].GetValue( 0 ) * 2 + 1;
1079 
1080  for( int jj = 0; jj < paramCount && *aText != '*'; ++jj )
1081  {
1082  prim.params.push_back( AM_PARAM() );
1083 
1084  AM_PARAM& param = prim.params.back();
1085 
1086  aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
1087 
1088  if( aText == NULL ) // End of File
1089  return false;
1090 
1091  param.ReadParam( aText );
1092  }
1093  }
1094 
1095  // AMP_CIRCLE can have a optional parameter (rotation)
1096  if( prim.primitive_id == AMP_CIRCLE && aText && *aText != '*' )
1097  {
1098  prim.params.push_back( AM_PARAM() );
1099  AM_PARAM& param = prim.params.back();
1100  param.ReadParam( aText );
1101  }
1102 
1103  am.primitives.push_back( prim );
1104  }
1105 
1106  m_aperture_macros.insert( am );
1107 
1108  return true;
1109 }
1110 
APERTURE_MACRO_SET m_aperture_macros
a collection of APERTURE_MACROS, sorted by name
a class to handle special data (items attributes) during plot.
X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF....
void AddMessageToList(const wxString &aMessage)
Function AddMessageToList Add a message to the message list.
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
X2_ATTRIBUTE The attribute value consists of a number of substrings separated by a comma.
wxString name
The name of the aperture macro.
Definition: am_primitive.h:165
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:62
Implementation of conversion functions that require both schematic and board internal units.
print info associated to a component (TO.C attribute)
wxSize m_Size
Horizontal and vertical dimensions.
Definition: dcode.h:94
int ReadInt(char *&text, bool aSkipSeparator=true)
Function ReadInt reads an int from an ASCII character buffer.
Definition: dcode.h:53
APERTURE_T m_Shape
shape ( Line, rectangle, circle , oval .. )
Definition: dcode.h:95
double m_Rotation
shape rotation in degrees
Definition: dcode.h:99
wxSize m_Drill
dimension of the hole (if any) (draill file)
Definition: dcode.h:97
wxRealPoint m_StepForRepeat
wxString m_Cmpref
the component reference parent of the data
int ReadXCommandID(char *&text)
reads two bytes of data and assembles them into an int with the first byte in the sequence put into t...
Definition: rs274x.cpp:116
Definition: dcode.h:52
bool ReadParam(char *&aText)
Function ReadParam Read one aperture macro parameter a parameter can be: a number a reference to an a...
Definition: am_param.cpp:171
AM_PARAMS m_localparamStack
Definition: am_primitive.h:174
This file contains miscellaneous commonly used macros and functions.
GBR_DATA_FIELD m_PadPinFunction
for a pad: the pin function (defined in schematic)
bool ExecuteRS274XCommand(int aCommand, char *aBuff, unsigned int aBuffSize, char *&aText)
executes a RS274X command
Definition: rs274x.cpp:197
char * GetNextLine(char *aBuff, unsigned int aBuffSize, char *aText, FILE *aFile)
test for an end of line if a end of line is found: read a new line
Definition: rs274x.cpp:890
void SetField(const wxString &aField, bool aUseUTF8, bool aEscapeString)
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
double ReadDouble(char *&text, bool aSkipSeparator=true)
Function ReadDouble reads a double from an ASCII character buffer.
bool m_Defined
false if the aperture is not defined in the header
Definition: dcode.h:104
void AppendParam(double aValue)
AppendParam() Add a parameter to the D_CODE parameter list.
Definition: dcode.h:122
#define NULL
AM_PARAM holds a parameter value for an "aperture macro" as defined within standard RS274X.
Definition: am_param.h:286
GBR_DATA_FIELD m_Padname
for a flashed pad: the pad name ((TO.P attribute)
wxString m_Netname
for items associated to a net: the netname
APERTURE_MACRO * FindApertureMacro(const APERTURE_MACRO &aLookup)
Function FindApertureMacro looks up a previously read in aperture macro.
bool m_StepForRepeatMetric
bool ReadApertureMacro(char *aBuff, unsigned int aBuffSize, char *&text, FILE *gerber_file)
reads in an aperture macro and saves it in m_aperture_macros.
Definition: rs274x.cpp:918
wxString m_AperFunction
the aperture attribute (created by a TA.AperFunction command) attached to the D_CODE
Definition: dcode.h:105
#define CODE(x, y)
Definition: rs274x.cpp:44
void RemoveAttribute(X2_ATTRIBUTE &aAttribute)
Function RemoveAttribute.
APERTURE_DEF_HOLETYPE m_DrillShape
shape of the hole (0 = no hole, round = 1, rect = 2) */
Definition: dcode.h:98
RS274X_PARAMETERS
Definition: rs274x.cpp:57
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:101
AM_PRIMITIVES primitives
A sequence of AM_PRIMITIVEs.
Definition: am_primitive.h:166
D_CODE holds a gerber DCODE (also called Aperture) definition.
Definition: dcode.h:82
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: rs274x.cpp:64
#define _(s)
Definition: 3d_actions.cpp:31
GBR_NETLIST_METADATA m_NetAttributeDict
bool ReadRS274XCommand(char *aBuff, unsigned int aBuffSize, char *&aText)
reads a single RS274X command terminated with a %
Definition: rs274x.cpp:144
Struct AM_PRIMITIVE holds an aperture macro primitive as given in Table 3 of http://gerbv....
Definition: am_primitive.h:92
void SetMacro(APERTURE_MACRO *aMacro)
Definition: dcode.h:151
bool GetEndOfBlock(char *aBuff, unsigned int aBuffSize, char *&aText, FILE *aGerberFile)
Definition: rs274x.cpp:864
#define IU_PER_MILS
Definition: plotter.cpp:137
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61
int m_EdgesCount
in aperture definition Polygon only: number of edges for the polygon
Definition: dcode.h:100
The common library.
print info associated to a flashed pad (TO.P attribute)
wxString FormatStringFromGerber(const wxString &aString)
This helper function make the inverse conversion of FormatStringToGerber() It converts a "normalized"...
AM_PRIMITIVE_ID
Enum AM_PRIMITIVE_ID is the set of all "aperture macro primitives" (primitive numbers).
Definition: am_primitive.h:71
Struct APERTURE_MACRO helps support the "aperture macro" defined within standard RS274X.
Definition: am_primitive.h:163
print info associated to a net (TO.N attribute)
D_CODE * GetDCODEOrCreate(int aDCODE, bool aCreateIfNoExist=true)
Function GetDCODEOrCreate returns a pointer to the D_CODE within this GERBER for the given aDCODE.
AM_PARAMS params
A sequence of parameters used by.
Definition: am_primitive.h:96
GERBER_LAYER & GetLayerParams()
Function GetLayerParams.
AM_PRIMITIVE_ID primitive_id
The primitive type.
Definition: am_primitive.h:95
int m_NetAttribType
the type of net info (used to define the gerber string to create)