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