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 
176  if( !ok )
177  goto exit;
178 
179  break;
180  }
181  }
182 
183  // end of current line, read another one.
184  if( fgets( aBuff, aBuffSize, m_Current_File ) == NULL )
185  {
186  // end of file
187  ok = false;
188  break;
189  }
190  m_LineNum++;
191  aText = aBuff;
192  }
193 
194 exit:
195  return ok;
196 }
197 
198 
199 bool GERBER_FILE_IMAGE::ExecuteRS274XCommand( int aCommand, char* aBuff,
200  unsigned int aBuffSize, char*& aText )
201 {
202  int code;
203  int seq_len; // not used, just provided
204  int seq_char;
205  bool ok = true;
206  wxString msg;
207  double fcoord;
208  bool x_fmt_known = false;
209  bool y_fmt_known = false;
210 
211  // conv_scale = scaling factor from inch to Internal Unit
212  double conv_scale = IU_PER_MILS * 1000;
213 
214  if( m_GerbMetric )
215  conv_scale /= 25.4;
216 
217  switch( aCommand )
218  {
219  case FORMAT_STATEMENT:
220  seq_len = 2;
221 
222  while( *aText != '*' )
223  {
224  switch( *aText )
225  {
226  case ' ':
227  aText++;
228  break;
229 
230  case 'D': // Non-standard option for all zeros (leading + tailing)
231  msg.Printf( _( "RS274X: Invalid GERBER format command '%c' at line %d: \"%s\"" ),
232  'D', m_LineNum, aBuff );
233  AddMessageToList( msg );
234  msg.Printf( _("GERBER file \"%s\" may not display as intended." ),
235  m_FileName.ToAscii() );
236  AddMessageToList( msg );
238 
239  case 'L': // No Leading 0
240  m_NoTrailingZeros = false;
241  aText++;
242  break;
243 
244  case 'T': // No trailing 0
245  m_NoTrailingZeros = true;
246  aText++;
247  break;
248 
249  case 'A': // Absolute coord
250  m_Relative = false;
251  aText++;
252  break;
253 
254  case 'I': // Relative coord
255  m_Relative = true;
256  aText++;
257  break;
258 
259  case 'G':
260  case 'N': // Sequence code (followed by one digit: the sequence len)
261  // (sometimes found before the X,Y sequence)
262  // Obscure option
263  aText++;
264  seq_char = *aText++;
265  if( (seq_char >= '0') && (seq_char <= '9') )
266  seq_len = seq_char - '0';
267  break;
268 
269  case 'M': // Sequence code (followed by one digit: the sequence len)
270  // (sometimes found after the X,Y sequence)
271  // Obscure option
272  code = *aText++;
273  if( ( *aText >= '0' ) && ( *aText<= '9' ) )
274  aText++; // skip the digit
275  break;
276 
277  case 'X':
278  case 'Y':
279  {
280  code = *(aText++);
281  char ctmp = *(aText++) - '0';
282  if( code == 'X' )
283  {
284  x_fmt_known = true;
285  // number of digits after the decimal point (0 to 7 allowed)
286  m_FmtScale.x = *aText - '0';
287  m_FmtLen.x = ctmp + m_FmtScale.x;
288 
289  // m_FmtScale is 0 to 7
290  // (Old Gerber specification was 0 to 6)
291  if( m_FmtScale.x < 0 )
292  m_FmtScale.x = 0;
293  if( m_FmtScale.x > 7 )
294  m_FmtScale.x = 7;
295  }
296  else
297  {
298  y_fmt_known = true;
299  m_FmtScale.y = *aText - '0';
300  m_FmtLen.y = ctmp + m_FmtScale.y;
301  if( m_FmtScale.y < 0 )
302  m_FmtScale.y = 0;
303  if( m_FmtScale.y > 7 )
304  m_FmtScale.y = 7;
305  }
306  aText++;
307  }
308  break;
309 
310  case '*':
311  break;
312 
313  default:
314  msg.Printf( wxT( "Unknown id (%c) in FS command" ),
315  *aText );
316  AddMessageToList( msg );
317  GetEndOfBlock( aBuff, aBuffSize, aText, m_Current_File );
318  ok = false;
319  break;
320  }
321  }
322  if( !x_fmt_known || !y_fmt_known )
323  AddMessageToList( wxT( "RS274X: Format Statement (FS) without X or Y format" ) );
324 
325  break;
326 
327  case AXIS_SELECT: // command ASAXBY*% or %ASAYBX*%
328  m_SwapAxis = false;
329  if( strncasecmp( aText, "AYBX", 4 ) == 0 )
330  m_SwapAxis = true;
331  break;
332 
333  case MIRROR_IMAGE: // command %MIA0B0*%, %MIA0B1*%, %MIA1B0*%, %MIA1B1*%
334  m_MirrorA = m_MirrorB = 0;
335  while( *aText && *aText != '*' )
336  {
337  switch( *aText )
338  {
339  case 'A': // Mirror A axis ?
340  aText++;
341  if( *aText == '1' )
342  m_MirrorA = true;
343  break;
344 
345  case 'B': // Mirror B axis ?
346  aText++;
347  if( *aText == '1' )
348  m_MirrorB = true;
349  break;
350 
351  default:
352  aText++;
353  break;
354  }
355  }
356  break;
357 
358  case MODE_OF_UNITS:
359  code = ReadXCommandID( aText );
360  if( code == INCH )
361  m_GerbMetric = false;
362  else if( code == MILLIMETER )
363  m_GerbMetric = true;
364  conv_scale = m_GerbMetric ? IU_PER_MILS / 25.4 : IU_PER_MILS;
365  break;
366 
367  case FILE_ATTRIBUTE: // Command %TF ...
368  m_IsX2_file = true;
369  {
371  dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
372 
373  if( dummy.IsFileFunction() )
374  {
375  delete m_FileFunction;
377  }
378  else if( dummy.IsFileMD5() )
379  {
380  m_MD5_value = dummy.GetPrm( 1 );
381  }
382  else if( dummy.IsFilePart() )
383  {
384  m_PartString = dummy.GetPrm( 1 );
385  }
386  }
387  break;
388 
389  case APERTURE_ATTRIBUTE: // Command %TA
390  {
392  dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
393 
394  if( dummy.GetAttribute() == ".AperFunction" )
395  {
396  m_AperFunction = dummy.GetPrm( 1 );
397 
398  // A few function values can have other parameters. Add them
399  for( int ii = 2; ii < dummy.GetPrmCount(); ii++ )
400  m_AperFunction << "," << dummy.GetPrm( ii );
401  }
402  }
403  break;
404 
405  case NET_ATTRIBUTE: // Command %TO currently %TO.P %TO.N and %TO.C
406  {
408 
409  dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
410 
411  if( dummy.GetAttribute() == ".N" )
412  {
415  }
416  else if( dummy.GetAttribute() == ".C" )
417  {
420  }
421  else if( dummy.GetAttribute() == ".P" )
422  {
425  m_NetAttributeDict.m_Padname.SetField( FormatStringFromGerber( dummy.GetPrm( 2 ) ), true, true );
426 
427  if( dummy.GetPrmCount() > 3 )
429  FormatStringFromGerber( dummy.GetPrm( 3 ) ), true, true );
430  else
432  }
433  }
434  break;
435 
436  case REMOVE_APERTURE_ATTRIBUTE: // Command %TD ...
437  {
439  dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
441  }
442  break;
443 
444  case OFFSET: // command: OFAnnBnn (nn = float number) = layer Offset
445  m_Offset.x = m_Offset.y = 0;
446  while( *aText != '*' )
447  {
448  switch( *aText )
449  {
450  case 'A': // A axis offset in current unit (inch or mm)
451  aText++;
452  fcoord = ReadDouble( aText );
453  m_Offset.x = KiROUND( fcoord * conv_scale );
454  break;
455 
456  case 'B': // B axis offset in current unit (inch or mm)
457  aText++;
458  fcoord = ReadDouble( aText );
459  m_Offset.y = KiROUND( fcoord * conv_scale );
460  break;
461  }
462  }
463  break;
464 
465  case SCALE_FACTOR:
466  m_Scale.x = m_Scale.y = 1.0;
467  while( *aText != '*' )
468  {
469  switch( *aText )
470  {
471  case 'A': // A axis scale
472  aText++;
473  m_Scale.x = ReadDouble( aText );
474  break;
475 
476  case 'B': // B axis scale
477  aText++;
478  m_Scale.y = ReadDouble( aText );
479  break;
480  }
481  }
482  break;
483 
484  case IMAGE_OFFSET: // command: IOAnnBnn (nn = float number) = Image Offset
486  while( *aText != '*' )
487  {
488  switch( *aText )
489  {
490  case 'A': // A axis offset in current unit (inch or mm)
491  aText++;
492  fcoord = ReadDouble( aText );
493  m_ImageOffset.x = KiROUND( fcoord * conv_scale );
494  break;
495 
496  case 'B': // B axis offset in current unit (inch or mm)
497  aText++;
498  fcoord = ReadDouble( aText );
499  m_ImageOffset.y = KiROUND( fcoord * conv_scale );
500  break;
501  }
502  }
503  break;
504 
505  case IMAGE_ROTATION: // command IR0* or IR90* or IR180* or IR270*
506  if( strncasecmp( aText, "0*", 2 ) == 0 )
507  m_ImageRotation = 0;
508  else if( strncasecmp( aText, "90*", 3 ) == 0 )
509  m_ImageRotation = 90;
510  else if( strncasecmp( aText, "180*", 4 ) == 0 )
511  m_ImageRotation = 180;
512  else if( strncasecmp( aText, "270*", 4 ) == 0 )
513  m_ImageRotation = 270;
514  else
515  AddMessageToList( _( "RS274X: Command \"IR\" rotation value not allowed" ) );
516  break;
517 
518  case STEP_AND_REPEAT: // command SR, like %SRX3Y2I5.0J2*%
519  m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
521  GetLayerParams().m_StepForRepeat.x = 0.0; // offset for Step and Repeat command
523  GetLayerParams().m_YRepeatCount = 1; // The repeat count
524  GetLayerParams().m_StepForRepeatMetric = m_GerbMetric; // the step units
525  while( *aText && *aText != '*' )
526  {
527  switch( *aText )
528  {
529  case 'I': // X axis offset
530  aText++;
532  break;
533 
534  case 'J': // Y axis offset
535  aText++;
537  break;
538 
539  case 'X': // X axis repeat count
540  aText++;
542  break;
543 
544  case 'Y': // Y axis offset
545  aText++;
547  break;
548  default:
549  aText++;
550  break;
551  }
552  }
553  break;
554 
555  case IMAGE_JUSTIFY: // Command IJAnBn*
556  m_ImageJustifyXCenter = false; // Image Justify Center on X axis (default = false)
557  m_ImageJustifyYCenter = false; // Image Justify Center on Y axis (default = false)
558  m_ImageJustifyOffset = wxPoint(0,0); // Image Justify Offset on XY axis (default = 0,0)
559  while( *aText && *aText != '*' )
560  {
561  // IJ command is (for A or B axis) AC or AL or A<coordinate>
562  switch( *aText )
563  {
564  case 'A': // A axis justify
565  aText++;
566  if( *aText == 'C' )
567  {
568  m_ImageJustifyXCenter = true;
569  aText++;
570  }
571  else if( *aText == 'L' )
572  {
573  m_ImageJustifyXCenter = true;
574  aText++;
575  }
576  else m_ImageJustifyOffset.x = KiROUND( ReadDouble( aText ) * conv_scale);
577  break;
578 
579  case 'B': // B axis justify
580  aText++;
581  if( *aText == 'C' )
582  {
583  m_ImageJustifyYCenter = true;
584  aText++;
585  }
586  else if( *aText == 'L' )
587  {
588  m_ImageJustifyYCenter = true;
589  aText++;
590  }
591  else m_ImageJustifyOffset.y = KiROUND( ReadDouble( aText ) * conv_scale);
592  break;
593  default:
594  aText++;
595  break;
596  }
597  }
602  break;
603 
604  case KNOCKOUT:
605  m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
606  msg = _( "RS274X: Command KNOCKOUT ignored by GerbView" ) ;
607  AddMessageToList( msg );
608  break;
609 
610  case ROTATE: // Layer rotation: command like %RO45*%
611  m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
612  m_LocalRotation =ReadDouble( aText ); // Store layer rotation in degrees
613  break;
614 
615  case IMAGE_NAME:
616  m_ImageName.Empty();
617  while( *aText != '*' )
618  {
619  m_ImageName.Append( *aText++ );
620  }
621 
622  break;
623 
624  case LOAD_NAME:
625  // %LN is a (deprecated) equivalentto G04: a comment
626  while( *aText && *aText != '*' )
627  {
628  aText++; // Skip text
629  }
630 
631  break;
632 
633  case IMAGE_POLARITY:
634  if( strncasecmp( aText, "NEG", 3 ) == 0 )
635  m_ImageNegative = true;
636  else
637  m_ImageNegative = false;
638 
639  break;
640 
641  case LOAD_POLARITY:
642  if( *aText == 'C' )
644  else
646 
647  break;
648 
649  case AP_MACRO: // lines like %AMMYMACRO*
650  // 5,1,8,0,0,1.08239X$1,22.5*
651  // %
652  /*ok = */ReadApertureMacro( aBuff, aBuffSize, aText, m_Current_File );
653  break;
654 
655  case AP_DEFINITION:
656  /* input example: %ADD30R,0.081800X0.101500*%
657  * Aperture definition has 4 options: C, R, O, P
658  * (Circle, Rect, Oval, regular Polygon)
659  * and shapes can have a hole (round or rectangular).
660  * All optional parameters values start by X
661  * at this point, text points to 2nd 'D'
662  */
663  if( *aText++ != 'D' )
664  {
665  ok = false;
666  break;
667  }
668 
669  m_Has_DCode = true;
670 
671  code = ReadInt( aText );
672 
673  D_CODE* dcode;
674  dcode = GetDCODEOrCreate( code );
675 
676  if( dcode == NULL )
677  break;
678 
680 
681  // at this point, text points to character after the ADD<num>,
682  // i.e. R in example above. If aText[0] is one of the usual
683  // apertures: (C,R,O,P), there is a comma after it.
684  if( aText[1] == ',' )
685  {
686  char stdAperture = *aText;
687 
688  aText += 2; // skip "C," for example
689 
690  // First parameter is the size X:
691  dcode->m_Size.x = KiROUND( ReadDouble( aText ) * conv_scale );
692  dcode->m_Size.y = dcode->m_Size.x;
693 
694  switch( stdAperture ) // Aperture desceiption has optional parameters. Read them
695  {
696  case 'C': // Circle
697  dcode->m_Shape = APT_CIRCLE;
698  while( *aText == ' ' )
699  aText++;
700 
701  if( *aText == 'X' )
702  {
703  aText++;
704  dcode->m_Drill.x = dcode->m_Drill.y =
705  KiROUND( ReadDouble( aText ) * conv_scale );
707  }
708 
709  while( *aText == ' ' )
710  aText++;
711 
712  if( *aText == 'X' )
713  {
714  aText++;
715  dcode->m_Drill.y =
716  KiROUND( ReadDouble( aText ) * conv_scale );
717 
719  }
720  dcode->m_Defined = true;
721  break;
722 
723  case 'O': // oval
724  case 'R': // rect
725  dcode->m_Shape = (stdAperture == 'O') ? APT_OVAL : APT_RECT;
726 
727  while( *aText == ' ' )
728  aText++;
729 
730  if( *aText == 'X' ) // Second parameter: size Y
731  {
732  aText++;
733  dcode->m_Size.y =
734  KiROUND( ReadDouble( aText ) * conv_scale );
735  }
736 
737  while( *aText == ' ' )
738  aText++;
739 
740  if( *aText == 'X' ) // third parameter: drill size (or drill size X)
741  {
742  aText++;
743  dcode->m_Drill.x = KiROUND( ReadDouble( aText ) * conv_scale );
744  dcode->m_Drill.y = dcode->m_Drill.x;
746  }
747 
748  while( *aText == ' ' )
749  aText++;
750 
751  if( *aText == 'X' ) // fourth parameter: drill size Y
752  {
753  aText++;
754  dcode->m_Drill.y =
755  KiROUND( ReadDouble( aText ) * conv_scale );
757  }
758 
759  dcode->m_Defined = true;
760  break;
761 
762  case 'P':
763 
764  /* Regular polygon: a command line like %ADD12P,0.040X10X25X0.025X0.025X0.0150*%
765  * params are: <diameter>, X<edge count>, X<Rotation>, X<X hole dim>, X<Y hole dim>
766  */
767  dcode->m_Shape = APT_POLYGON;
768  while( *aText == ' ' )
769  aText++;
770 
771  if( *aText == 'X' )
772  {
773  aText++;
774  dcode->m_EdgesCount = ReadInt( aText );
775  }
776 
777  while( *aText == ' ' )
778  aText++;
779 
780  if( *aText == 'X' )
781  {
782  aText++;
783  dcode->m_Rotation = ReadDouble( aText );
784  }
785 
786  while( *aText == ' ' )
787  aText++;
788 
789  if( *aText == 'X' )
790  {
791  aText++;
792  dcode->m_Drill.x = KiROUND( ReadDouble( aText ) * conv_scale );
793  dcode->m_Drill.y = dcode->m_Drill.x;
795  }
796 
797  while( *aText == ' ' )
798  aText++;
799 
800  if( *aText == 'X' )
801  {
802  aText++;
803  dcode->m_Drill.y = KiROUND( ReadDouble( aText ) * conv_scale );
805  }
806  dcode->m_Defined = true;
807  break;
808  }
809  }
810  else // aText[0] starts an aperture macro name
811  {
812  APERTURE_MACRO am_lookup;
813 
814  while( *aText && *aText != '*' && *aText != ',' )
815  am_lookup.name.Append( *aText++ );
816 
817  // When an aperture definition is like %AMLINE2* 22,1,$1,$2,0,0,-45*
818  // the ADDxx<MACRO_NAME> command has parameters, like %ADD14LINE2,0.8X0.5*%
819  if( *aText == ',' )
820  { // Read aperture macro parameters and store them
821  aText++; // aText points the first parameter
822 
823  while( *aText && *aText != '*' )
824  {
825  double param = ReadDouble( aText );
826  dcode->AppendParam( param );
827 
828  while( isspace( *aText ) )
829  aText++;
830 
831  // Skip 'X' separator:
832  if( *aText == 'X' || *aText == 'x' )
833  aText++;
834  }
835  }
836 
837  // lookup the aperture macro here.
838  APERTURE_MACRO* pam = FindApertureMacro( am_lookup );
839 
840  if( !pam )
841  {
842  msg.Printf( wxT( "RS274X: aperture macro %s not found\n" ),
843  TO_UTF8( am_lookup.name ) );
844  AddMessageToList( msg );
845  ok = false;
846  break;
847  }
848 
849  dcode->m_Shape = APT_MACRO;
850  dcode->SetMacro( pam );
851  }
852 
853  break;
854 
855  default:
856  ok = false;
857  break;
858  }
859 
860  (void) seq_len; // quiet g++, or delete the unused variable.
861 
862  ok = GetEndOfBlock( aBuff, aBuffSize, aText, m_Current_File );
863 
864  return ok;
865 }
866 
867 
868 bool GERBER_FILE_IMAGE::GetEndOfBlock( char* aBuff, unsigned int aBuffSize, char*& aText, FILE* gerber_file )
869 {
870  for( ; ; )
871  {
872  while( (aText < aBuff + aBuffSize) && *aText )
873  {
874  if( *aText == '*' )
875  return true;
876 
877  if( *aText == '%' )
878  return true;
879 
880  aText++;
881  }
882 
883  if( fgets( aBuff, aBuffSize, gerber_file ) == NULL )
884  break;
885 
886  m_LineNum++;
887  aText = aBuff;
888  }
889 
890  return false;
891 }
892 
893 
894 char* GERBER_FILE_IMAGE::GetNextLine( char *aBuff, unsigned int aBuffSize, char* aText, FILE* aFile )
895 {
896  for( ; ; )
897  {
898  switch (*aText )
899  {
900  case ' ': // skip blanks
901  case '\n':
902  case '\r': // Skip line terminators
903  ++aText;
904  break;
905 
906  case 0: // End of text found in aBuff: Read a new string
907  if( fgets( aBuff, aBuffSize, aFile ) == NULL )
908  return NULL;
909 
910  m_LineNum++;
911  aText = aBuff;
912  return aText;
913 
914  default:
915  return aText;
916  }
917  }
918  return aText;
919 }
920 
921 
922 bool GERBER_FILE_IMAGE::ReadApertureMacro( char *aBuff, unsigned int aBuffSize,
923  char*& aText,
924  FILE* gerber_file )
925 {
926  wxString msg;
927  APERTURE_MACRO am;
928 
929  // read macro name
930  while( *aText )
931  {
932  if( *aText == '*' )
933  {
934  ++aText;
935  break;
936  }
937 
938  am.name.Append( *aText++ );
939  }
940 
941  // Read aperture macro parameters
942  for( ; ; )
943  {
944  if( *aText == '*' )
945  ++aText;
946 
947  aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
948 
949  if( aText == NULL ) // End of File
950  return false;
951 
952  // aText points the beginning of a new line.
953 
954  // Test for the last line in aperture macro lis:
955  // last line is % or *% sometime found.
956  if( *aText == '*' )
957  ++aText;
958 
959  if( *aText == '%' )
960  break; // exit with aText still pointing at %
961 
962  int paramCount = 0; // will be set to the minimal parameters count,
963  // depending on the actual primitive
964  int primitive_type = AMP_UNKNOWN;
965  // Test for a valid symbol at the beginning of a description:
966  // it can be: a parameter declaration like $1=$2/4
967  // or a digit (macro primitive selection)
968  // all other symbols are illegal.
969  if( *aText == '$' ) // local parameter declaration, inside the aperture macro
970  {
971  am.m_localparamStack.push_back( AM_PARAM() );
972  AM_PARAM& param = am.m_localparamStack.back();
973  aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
974  if( aText == NULL) // End of File
975  return false;
976  param.ReadParam( aText );
977  continue;
978  }
979  else if( !isdigit(*aText) ) // Ill. symbol
980  {
981  msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": ill. symbol, line: \"%s\"" ),
982  GetChars( am.name ), GetChars( FROM_UTF8( aBuff ) ) );
983  AddMessageToList( msg );
984  primitive_type = AMP_COMMENT;
985  }
986  else
987  primitive_type = ReadInt( aText );
988 
989  bool is_comment = false;
990 
991  switch( primitive_type )
992  {
993  case AMP_COMMENT: // lines starting by 0 are a comment
994  paramCount = 0;
995  is_comment = true;
996  // Skip comment
997  while( *aText && ( *aText != '*' ) )
998  aText++;
999  break;
1000 
1001  case AMP_CIRCLE:
1002  paramCount = 4; // minimal count. can have a optional parameter (rotation)
1003  break;
1004 
1005  case AMP_LINE2:
1006  case AMP_LINE20:
1007  paramCount = 7;
1008  break;
1009 
1010  case AMP_LINE_CENTER:
1011  case AMP_LINE_LOWER_LEFT:
1012  paramCount = 6;
1013  break;
1014 
1015  case AMP_EOF:
1016  paramCount = 0;
1017  break;
1018 
1019  case AMP_OUTLINE:
1020  paramCount = 4; // partial count. other parameters are vertices and rotation
1021  // Second parameter is vertice (coordinate pairs) count.
1022  break;
1023 
1024  case AMP_POLYGON:
1025  paramCount = 6;
1026  break;
1027 
1028  case AMP_MOIRE:
1029  paramCount = 9;
1030  break;
1031 
1032  case AMP_THERMAL:
1033  paramCount = 6;
1034  break;
1035 
1036  default:
1037  msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": Invalid primitive id code %d, line %d: \"%s\"" ),
1038  GetChars( am.name ), primitive_type, m_LineNum, GetChars( FROM_UTF8( aBuff ) ) );
1039  AddMessageToList( msg );
1040  return false;
1041  }
1042 
1043  if( is_comment )
1044  continue;
1045 
1046  AM_PRIMITIVE prim( m_GerbMetric );
1047  prim.primitive_id = (AM_PRIMITIVE_ID) primitive_type;
1048  int ii;
1049 
1050  for( ii = 0; ii < paramCount && *aText && *aText != '*'; ++ii )
1051  {
1052  prim.params.push_back( AM_PARAM() );
1053 
1054  AM_PARAM& param = prim.params.back();
1055 
1056  aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
1057 
1058  if( aText == NULL) // End of File
1059  return false;
1060 
1061  param.ReadParam( aText );
1062  }
1063 
1064  if( ii < paramCount )
1065  {
1066  // maybe some day we can throw an exception and track a line number
1067  msg.Printf( "RS274X: read macro descr type %d: read %d parameters, insufficient parameters\n",
1068  prim.primitive_id, ii );
1069  AddMessageToList( msg );
1070  }
1071 
1072  // there are more parameters to read if this is an AMP_OUTLINE
1073  if( prim.primitive_id == AMP_OUTLINE )
1074  {
1075  // so far we have read [0]:exposure, [1]:#points, [2]:X start, [3]: Y start
1076  // Now read all the points, plus trailing rotation in degrees.
1077 
1078  // params[1] is a count of polygon points, so it must be given
1079  // in advance, i.e. be immediate.
1080  wxASSERT( prim.params[1].IsImmediate() );
1081 
1082  paramCount = (int) prim.params[1].GetValue( 0 ) * 2 + 1;
1083 
1084  for( int jj = 0; jj < paramCount && *aText != '*'; ++jj )
1085  {
1086  prim.params.push_back( AM_PARAM() );
1087 
1088  AM_PARAM& param = prim.params.back();
1089 
1090  aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
1091 
1092  if( aText == NULL ) // End of File
1093  return false;
1094 
1095  param.ReadParam( aText );
1096  }
1097  }
1098 
1099  // AMP_CIRCLE can have a optional parameter (rotation)
1100  if( prim.primitive_id == AMP_CIRCLE && aText && *aText != '*' )
1101  {
1102  prim.params.push_back( AM_PARAM() );
1103  AM_PARAM& param = prim.params.back();
1104  param.ReadParam( aText );
1105  }
1106 
1107  am.primitives.push_back( prim );
1108  }
1109 
1110  m_aperture_macros.insert( am );
1111 
1112  return true;
1113 }
1114 
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.
#define KI_FALLTHROUGH
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:114
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:172
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:199
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:894
void SetField(const wxString &aField, bool aUseUTF8, bool aEscapeString)
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:922
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:153
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:33
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:868
#define IU_PER_MILS
Definition: plotter.cpp:138
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
#define TO_UTF8(wxstring)
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)