KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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
25
26#include <base_units.h>
27#include <math/util.h> // for KiROUND
28
29#include <gerbview.h>
30#include <gerber_file_image.h>
31#include <core/ignore.h>
32#include <macros.h>
33#include <string_utils.h>
35#include <gbr_metadata.h>
36#include <wx/log.h>
37
38extern int ReadInt( char*& text, bool aSkipSeparator = true );
39extern 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 coordinates
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 extension 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 extension 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 extension 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 specific
103 // These 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
115{
116 /* reads two bytes of data and assembles them into an int with the first
117 * byte in the sequence put into the most significant part of a 16 bit value
118 */
119 int result;
120 int currbyte;
121
122 if( text && *text )
123 {
124 currbyte = *text++;
125 result = ( currbyte & 0xFF ) << 8;
126 }
127 else
128 return -1;
129
130 if( text && *text )
131 {
132 currbyte = *text++;
133 result += currbyte & 0xFF;
134 }
135 else
136 return -1;
137
138 return result;
139}
140
141
142bool GERBER_FILE_IMAGE::ReadRS274XCommand( char *aBuff, unsigned int aBuffSize, char*& aText )
143{
144 bool ok = true;
145 int code_command;
146
147 aText++;
148
149 for( ; ; )
150 {
151 while( *aText )
152 {
153 switch( *aText )
154 {
155 case '%': // end of command
156 aText++;
158 goto exit; // success completion
159
160 case ' ':
161 case '\r':
162 case '\n':
163 aText++;
164 break;
165
166 case '*':
167 aText++;
168 break;
169
170 default:
171 code_command = ReadXCommandID( aText );
172 ok = ExecuteRS274XCommand( code_command, aBuff, aBuffSize, aText );
173
174 if( !ok )
175 goto exit;
176
177 break;
178 }
179 }
180
181 // end of current line, read another one.
182 if( fgets( aBuff, aBuffSize, m_Current_File ) == nullptr )
183 {
184 // end of file
185 ok = false;
186 break;
187 }
188
189 m_LineNum++;
190 aText = aBuff;
191 }
192
193exit:
194 return ok;
195}
196
197
198bool GERBER_FILE_IMAGE::ExecuteRS274XCommand( int aCommand, char* aBuff,
199 unsigned int aBuffSize, char*& aText )
200{
201 int code;
202 int seq_len; // not used, just provided
203 int seq_char;
204 bool ok = true;
205 wxString msg;
206 double fcoord;
207 bool x_fmt_known = false;
208 bool y_fmt_known = false;
209
210 // conv_scale = scaling factor from inch to Internal Unit
211 double conv_scale = gerbIUScale.IU_PER_MILS * 1000;
212
214
215 if( m_GerbMetric )
216 conv_scale /= 25.4;
217
218 switch( aCommand )
219 {
220 case FORMAT_STATEMENT:
221 seq_len = 2;
222
223 while( *aText != '*' )
224 {
225 switch( *aText )
226 {
227 case ' ':
228 aText++;
229 break;
230
231 case 'D': // Non-standard option for all zeros (leading + tailing)
232 msg.Printf( _( "RS274X: Invalid GERBER format command '%c' at line %d: '%s'" ),
233 'D', m_LineNum, aBuff );
234 AddMessageToList( msg );
235 msg.Printf( _("GERBER file '%s' may not display as intended." ),
236 m_FileName.ToAscii() );
237 AddMessageToList( msg );
239
240 case 'L': // No Leading 0
241 m_NoTrailingZeros = false;
242 aText++;
243 break;
244
245 case 'T': // No trailing 0
246 m_NoTrailingZeros = true;
247 aText++;
248 break;
249
250 case 'A': // Absolute coord
251 m_Relative = false;
252 aText++;
253 break;
254
255 case 'I': // Relative coord
256 m_Relative = true;
257 aText++;
258 break;
259
260 case 'G':
261 case 'N': // Sequence code (followed by one digit: the sequence len)
262 // (sometimes found before the X,Y sequence)
263 // Obscure option
264 aText++;
265 seq_char = *aText++;
266
267 if( (seq_char >= '0') && (seq_char <= '9') )
268 seq_len = seq_char - '0';
269
270 break;
271
272 case 'M': // Sequence code (followed by one digit: the sequence len)
273 // (sometimes found after the X,Y sequence)
274 // Obscure option
275 aText++;
276 code = *aText;
277
278 if( ( code >= '0' ) && ( code <= '9' ) )
279 aText++; // skip the digit
280
281 break;
282
283 case 'X':
284 case 'Y':
285 {
286 code = *(aText++);
287 char ctmp = *(aText++) - '0';
288
289 if( code == 'X' )
290 {
291 x_fmt_known = true;
292 // number of digits after the decimal point (0 to 7 allowed)
293 m_FmtScale.x = *aText - '0';
294 m_FmtLen.x = ctmp + m_FmtScale.x;
295
296 // m_FmtScale is 0 to 7
297 // (Old Gerber specification was 0 to 6)
298 if( m_FmtScale.x < 0 )
299 m_FmtScale.x = 0;
300
301 if( m_FmtScale.x > 7 )
302 m_FmtScale.x = 7;
303 }
304 else
305 {
306 y_fmt_known = true;
307 m_FmtScale.y = *aText - '0';
308 m_FmtLen.y = ctmp + m_FmtScale.y;
309
310 if( m_FmtScale.y < 0 )
311 m_FmtScale.y = 0;
312
313 if( m_FmtScale.y > 7 )
314 m_FmtScale.y = 7;
315 }
316
317 aText++;
318 break;
319 }
320
321 case '*':
322 break;
323
324 default:
325 msg.Printf( wxT( "Unknown id (%c) in FS command" ), *aText );
326 AddMessageToList( msg );
327 GetEndOfBlock( aBuff, aBuffSize, aText, m_Current_File );
328 ok = false;
329 break;
330 }
331 }
332
333 if( !x_fmt_known || !y_fmt_known )
334 AddMessageToList( wxT( "RS274X: Format Statement (FS) without X or Y format" ) );
335
336 break;
337
338 case AXIS_SELECT: // command ASAXBY*% or %ASAYBX*%
339 m_SwapAxis = false;
340
341 if( strncasecmp( aText, "AYBX", 4 ) == 0 )
342 m_SwapAxis = true;
343
344 break;
345
346 case MIRROR_IMAGE: // command %MIA0B0*%, %MIA0B1*%, %MIA1B0*%, %MIA1B1*%
347 m_MirrorA = m_MirrorB = false;
348
349 while( *aText && *aText != '*' )
350 {
351 switch( *aText )
352 {
353 case 'A': // Mirror A axis ?
354 aText++;
355
356 if( *aText == '1' )
357 m_MirrorA = true;
358
359 break;
360
361 case 'B': // Mirror B axis ?
362 aText++;
363
364 if( *aText == '1' )
365 m_MirrorB = true;
366
367 break;
368
369 default:
370 aText++;
371 break;
372 }
373 }
374 break;
375
376 case MODE_OF_UNITS:
377 code = ReadXCommandID( aText );
378
379 if( code == INCH )
380 m_GerbMetric = false;
381 else if( code == MILLIMETER )
382 m_GerbMetric = true;
383
384 conv_scale = m_GerbMetric ? gerbIUScale.IU_PER_MILS / 25.4 : gerbIUScale.IU_PER_MILS;
385 break;
386
387 case FILE_ATTRIBUTE: // Command %TF ...
388 dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
389
390 if( dummy.IsFileFunction() )
391 {
392 delete m_FileFunction;
394
395 // Don't set this until we get a file function; other code expects m_IsX2_file == true
396 // to mean that we have a valid m_FileFunction
397 m_IsX2_file = true;
398 }
399 else if( dummy.IsFileMD5() )
400 {
401 m_MD5_value = dummy.GetPrm( 1 );
402 }
403 else if( dummy.IsFilePart() )
404 {
405 m_PartString = dummy.GetPrm( 1 );
406 }
407
408 break;
409
410 case APERTURE_ATTRIBUTE: // Command %TA
411 dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
412
413 if( dummy.GetAttribute() == wxT( ".AperFunction" ) )
414 {
415 m_AperFunction = dummy.GetPrm( 1 );
416
417 // A few function values can have other parameters. Add them
418 for( int ii = 2; ii < dummy.GetPrmCount(); ii++ )
419 m_AperFunction << wxT( "," ) << dummy.GetPrm( ii );
420 }
421
422 break;
423
424 case NET_ATTRIBUTE: // Command %TO currently %TO.P %TO.N and %TO.C
425 dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
426
427 if( dummy.GetAttribute() == wxT( ".N" ) )
428 {
430 m_NetAttributeDict.m_Netname = FormatStringFromGerber( dummy.GetPrm( 1 ) );
431 }
432 else if( dummy.GetAttribute() == wxT( ".C" ) )
433 {
435 m_NetAttributeDict.m_Cmpref = FormatStringFromGerber( dummy.GetPrm( 1 ) );
436 }
437 else if( dummy.GetAttribute() == wxT( ".P" ) )
438 {
440 m_NetAttributeDict.m_Cmpref = FormatStringFromGerber( dummy.GetPrm( 1 ) );
441 m_NetAttributeDict.m_Padname.SetField( FormatStringFromGerber( dummy.GetPrm( 2 ) ), true, true );
442
443 if( dummy.GetPrmCount() > 3 )
444 {
445 m_NetAttributeDict.m_PadPinFunction.SetField( FormatStringFromGerber( dummy.GetPrm( 3 ) ),
446 true, true );
447 }
448 else
449 {
450 m_NetAttributeDict.m_PadPinFunction.Clear();
451 }
452 }
453
454 break;
455
456 case REMOVE_APERTURE_ATTRIBUTE: // Command %TD ...
457 dummy.ParseAttribCmd( m_Current_File, aBuff, aBuffSize, aText, m_LineNum );
459
460 break;
461
462 case OFFSET: // command: OFAnnBnn (nn = float number) = layer Offset
463 m_Offset.x = m_Offset.y = 0;
464
465 while( *aText != '*' )
466 {
467 switch( *aText )
468 {
469 case 'A': // A axis offset in current unit (inch or mm)
470 aText++;
471 fcoord = ReadDouble( aText );
472 m_Offset.x = KiROUND( fcoord * conv_scale );
473 break;
474
475 case 'B': // B axis offset in current unit (inch or mm)
476 aText++;
477 fcoord = ReadDouble( aText );
478 m_Offset.y = KiROUND( fcoord * conv_scale );
479 break;
480 }
481 }
482
483 break;
484
485 case SCALE_FACTOR:
486 m_Scale.x = m_Scale.y = 1.0;
487
488 while( *aText != '*' )
489 {
490 switch( *aText )
491 {
492 case 'A': // A axis scale
493 aText++;
494 m_Scale.x = ReadDouble( aText );
495 break;
496
497 case 'B': // B axis scale
498 aText++;
499 m_Scale.y = ReadDouble( aText );
500 break;
501 }
502 }
503
504 break;
505
506 case IMAGE_OFFSET: // command: IOAnnBnn (nn = float number) = Image Offset
508
509 while( *aText != '*' )
510 {
511 switch( *aText )
512 {
513 case 'A': // A axis offset in current unit (inch or mm)
514 aText++;
515 fcoord = ReadDouble( aText );
516 m_ImageOffset.x = KiROUND( fcoord * conv_scale );
517 break;
518
519 case 'B': // B axis offset in current unit (inch or mm)
520 aText++;
521 fcoord = ReadDouble( aText );
522 m_ImageOffset.y = KiROUND( fcoord * conv_scale );
523 break;
524 }
525 }
526
527 break;
528
529 case IMAGE_ROTATION: // command IR0* or IR90* or IR180* or IR270*
530 if( strncasecmp( aText, "0*", 2 ) == 0 )
531 m_ImageRotation = 0;
532 else if( strncasecmp( aText, "90*", 3 ) == 0 )
533 m_ImageRotation = 90;
534 else if( strncasecmp( aText, "180*", 4 ) == 0 )
535 m_ImageRotation = 180;
536 else if( strncasecmp( aText, "270*", 4 ) == 0 )
537 m_ImageRotation = 270;
538 else
539 AddMessageToList( _( "RS274X: Command \"IR\" rotation value not allowed" ) );
540
541 break;
542
543 case STEP_AND_REPEAT: // command SR, like %SRX3Y2I5.0J2*%
544 m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
546 GetLayerParams().m_StepForRepeat.x = 0.0; // offset for Step and Repeat command
548 GetLayerParams().m_YRepeatCount = 1; // The repeat count
550
551 while( *aText && *aText != '*' )
552 {
553 switch( *aText )
554 {
555 case 'I': // X axis offset
556 aText++;
558 break;
559
560 case 'J': // Y axis offset
561 aText++;
563 break;
564
565 case 'X': // X axis repeat count
566 aText++;
568 break;
569
570 case 'Y': // Y axis offset
571 aText++;
573 break;
574
575 default:
576 aText++;
577 break;
578 }
579 }
580
581 break;
582
583 case IMAGE_JUSTIFY: // Command IJAnBn*
584 m_ImageJustifyXCenter = false; // Image Justify Center on X axis (default = false)
585 m_ImageJustifyYCenter = false; // Image Justify Center on Y axis (default = false)
586 m_ImageJustifyOffset = VECTOR2I( 0, 0 ); // Image Justify Offset on XY axis (default = 0,0)
587
588 while( *aText && *aText != '*' )
589 {
590 // IJ command is (for A or B axis) AC or AL or A<coordinate>
591 switch( *aText )
592 {
593 case 'A': // A axis justify
594 aText++;
595
596 if( *aText == 'C' )
597 {
599 aText++;
600 }
601 else if( *aText == 'L' )
602 {
604 aText++;
605 }
606 else
607 {
608 m_ImageJustifyOffset.x = KiROUND( ReadDouble( aText ) * conv_scale);
609 }
610
611 break;
612
613 case 'B': // B axis justify
614 aText++;
615
616 if( *aText == 'C' )
617 {
619 aText++;
620 }
621 else if( *aText == 'L' )
622 {
624 aText++;
625 }
626 else
627 {
628 m_ImageJustifyOffset.y = KiROUND( ReadDouble( aText ) * conv_scale);
629 }
630
631 break;
632
633 default:
634 aText++;
635 break;
636 }
637 }
638
641
644
645 break;
646
647 case KNOCKOUT:
648 m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
649 msg = _( "RS274X: Command KNOCKOUT ignored by GerbView" ) ;
650 AddMessageToList( msg );
651 break;
652
653 case ROTATE: // Layer rotation: command like %RO45*%
654 m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
655 m_LocalRotation = ReadDouble( aText ); // Store layer rotation in degrees
656 break;
657
658 case IMAGE_NAME:
659 m_ImageName.Empty();
660
661 while( *aText != '*' )
662 m_ImageName.Append( *aText++ );
663
664 break;
665
666 case LOAD_NAME:
667 // %LN is a (deprecated) equivalentto G04: a comment
668 while( *aText && *aText != '*' )
669 aText++; // Skip text
670
671 break;
672
673 case IMAGE_POLARITY:
674 // Note: these commands IPPOS and IPNEG are deprecated since 2012.
675 if( strncasecmp( aText, "NEG", 3 ) == 0 )
676 {
677 m_ImageNegative = true;
678 // IPPOS Gerber command is deprecated since 2012.
679 // in Gerber doc 2024, the advice is: warn user and skip it.
680 AddMessageToList( _( "IPNEG Gerber command is deprecated since 2012. Skip it" ) );
681 }
682 else
683 {
684 m_ImageNegative = false;
685 // IPPOS Gerber command is deprecated since 2012.
686 // However this is the default for a Gerber file, and does not have
687 // actual effect. Just skip it.
688 }
689
690 break;
691
692 case LOAD_POLARITY:
693 if( *aText == 'C' )
695 else
697
698 break;
699
700 case AP_MACRO: // lines like %AMMYMACRO*
701 // 5,1,8,0,0,1.08239X$1,22.5*
702 // %
703 /*ok = */ReadApertureMacro( aBuff, aBuffSize, aText, m_Current_File );
704 break;
705
706 case AP_DEFINITION:
707 /* input example: %ADD30R,0.081800X0.101500*%
708 * Aperture definition has 4 options: C, R, O, P
709 * (Circle, Rect, Oval, regular Polygon)
710 * and shapes can have a hole (round or rectangular).
711 * All optional parameters values start by X
712 * at this point, text points to 2nd 'D'
713 */
714 if( *aText++ != 'D' )
715 {
716 ok = false;
717 break;
718 }
719
720 m_Has_DCode = true;
721
722 code = ReadInt( aText );
723
724 D_CODE* dcode;
725 dcode = GetDCODEOrCreate( code );
726
727 if( dcode == nullptr )
728 break;
729
731
732 // at this point, text points to character after the ADD<num>,
733 // i.e. R in example above. If aText[0] is one of the usual
734 // apertures: (C,R,O,P), there is a comma after it.
735 if( aText[1] == ',' )
736 {
737 char stdAperture = *aText;
738
739 aText += 2; // skip "C," for example
740
741 // First parameter is the size X:
742 dcode->m_Size.x = KiROUND( ReadDouble( aText ) * conv_scale );
743 dcode->m_Size.y = dcode->m_Size.x;
744
745 switch( stdAperture ) // Aperture desceiption has optional parameters. Read them
746 {
747 case 'C': // Circle
748 dcode->m_ApertType = APT_CIRCLE;
749 while( *aText == ' ' )
750 aText++;
751
752 if( *aText == 'X' )
753 {
754 aText++;
755 dcode->m_Drill.x = dcode->m_Drill.y =
756 KiROUND( ReadDouble( aText ) * conv_scale );
758 }
759
760 while( *aText == ' ' )
761 aText++;
762
763 if( *aText == 'X' )
764 {
765 aText++;
766 dcode->m_Drill.y =
767 KiROUND( ReadDouble( aText ) * conv_scale );
768
770 }
771
772 dcode->m_Defined = true;
773 break;
774
775 case 'O': // oval
776 case 'R': // rect
777 dcode->m_ApertType = (stdAperture == 'O') ? APT_OVAL : APT_RECT;
778
779 while( *aText == ' ' )
780 aText++;
781
782 if( *aText == 'X' ) // Second parameter: size Y
783 {
784 aText++;
785 dcode->m_Size.y = KiROUND( ReadDouble( aText ) * conv_scale );
786 }
787
788 while( *aText == ' ' )
789 aText++;
790
791 if( *aText == 'X' ) // third parameter: drill size (or drill size X)
792 {
793 aText++;
794 dcode->m_Drill.x = KiROUND( ReadDouble( aText ) * conv_scale );
795 dcode->m_Drill.y = dcode->m_Drill.x;
797 }
798
799 while( *aText == ' ' )
800 aText++;
801
802 if( *aText == 'X' ) // fourth parameter: drill size Y
803 {
804 aText++;
805 dcode->m_Drill.y = KiROUND( ReadDouble( aText ) * conv_scale );
807 }
808
809 dcode->m_Defined = true;
810 break;
811
812 case 'P':
813
814 /* Regular polygon: a command line like %ADD12P,0.040X10X25X0.025X0.025X0.0150*%
815 * params are: <diameter>, X<edge count>, X<Rotation>, X<X hole dim>, X<Y hole dim>
816 */
817 dcode->m_ApertType = APT_POLYGON;
818
819 while( *aText == ' ' )
820 aText++;
821
822 if( *aText == 'X' )
823 {
824 aText++;
825 dcode->m_EdgesCount = ReadInt( aText );
826 }
827
828 while( *aText == ' ' )
829 aText++;
830
831 if( *aText == 'X' )
832 {
833 aText++;
834 dcode->m_Rotation = EDA_ANGLE( ReadDouble( aText ), DEGREES_T );
835 }
836
837 while( *aText == ' ' )
838 aText++;
839
840 if( *aText == 'X' )
841 {
842 aText++;
843 dcode->m_Drill.x = KiROUND( ReadDouble( aText ) * conv_scale );
844 dcode->m_Drill.y = dcode->m_Drill.x;
846 }
847
848 while( *aText == ' ' )
849 aText++;
850
851 if( *aText == 'X' )
852 {
853 aText++;
854 dcode->m_Drill.y = KiROUND( ReadDouble( aText ) * conv_scale );
856 }
857
858 dcode->m_Defined = true;
859 break;
860 }
861 }
862 else // aText[0] starts an aperture macro name
863 {
864 APERTURE_MACRO am_lookup;
865
866 while( *aText && *aText != '*' && *aText != ',' )
867 am_lookup.m_AmName.Append( *aText++ );
868
869 // When an aperture definition is like %AMLINE2* 22,1,$1,$2,0,0,-45*
870 // the ADDxx<MACRO_NAME> command has parameters, like %ADD14LINE2,0.8X0.5*%
871 if( *aText == ',' )
872 { // Read aperture macro parameters and store them
873 aText++; // aText points the first parameter
874
875 while( *aText && *aText != '*' )
876 {
877 double param = ReadDouble( aText );
878 dcode->AppendParam( param );
879
880 if( !( isspace( *aText ) || *aText == 'X' || *aText == 'x' || *aText == '*' ) )
881 {
882 msg.Printf( wxT( "RS274X: aperture macro %s has invalid template "
883 "parameters\n" ),
884 TO_UTF8( am_lookup.m_AmName ) );
885 AddMessageToList( msg );
886 ok = false;
887 break;
888 }
889
890 while( isspace( *aText ) )
891 aText++;
892
893 // Skip 'X' separator:
894 if( *aText == 'X' || *aText == 'x' )
895 aText++;
896 }
897 }
898
899 // lookup the aperture macro here.
900 APERTURE_MACRO* pam = FindApertureMacro( am_lookup );
901
902 if( !pam )
903 {
904 msg.Printf( wxT( "RS274X: aperture macro %s not found\n" ),
905 TO_UTF8( am_lookup.m_AmName ) );
906 AddMessageToList( msg );
907 ok = false;
908 break;
909 }
910
911 dcode->m_ApertType = APT_MACRO;
912 dcode->SetMacro( pam );
913 dcode->m_Defined = true;
914 }
915
916 break;
917
918 default:
919 ok = false;
920 break;
921 }
922
923 ignore_unused( seq_len );
924
925 if( !GetEndOfBlock( aBuff, aBuffSize, aText, m_Current_File ) )
926 ok = false;
927
928 return ok;
929}
930
931
932bool GERBER_FILE_IMAGE::GetEndOfBlock( char* aBuff, unsigned int aBuffSize, char*& aText, FILE* gerber_file )
933{
934 for( ; ; )
935 {
936 while( (aText < aBuff + aBuffSize) && *aText )
937 {
938 if( *aText == '*' )
939 return true;
940
941 if( *aText == '%' )
942 return true;
943
944 aText++;
945 }
946
947 if( fgets( aBuff, aBuffSize, gerber_file ) == nullptr )
948 break;
949
950 m_LineNum++;
951 aText = aBuff;
952 }
953
954 return false;
955}
956
957
958char* GERBER_FILE_IMAGE::GetNextLine( char *aBuff, unsigned int aBuffSize, char* aText, FILE* aFile )
959{
960 for( ; ; )
961 {
962 switch (*aText )
963 {
964 case ' ': // skip blanks
965 case '\n':
966 case '\r': // Skip line terminators
967 ++aText;
968 break;
969
970 case 0: // End of text found in aBuff: Read a new string
971 if( fgets( aBuff, aBuffSize, aFile ) == nullptr )
972 return nullptr;
973
974 m_LineNum++;
975 aText = aBuff;
976 return aText;
977
978 default:
979 return aText;
980 }
981 }
982}
983
984
985bool GERBER_FILE_IMAGE::ReadApertureMacro( char *aBuff, unsigned int aBuffSize, char*& aText,
986 FILE* gerber_file )
987{
988 wxString msg;
990
991 // read macro name
992 while( *aText )
993 {
994 if( *aText == '*' )
995 {
996 ++aText;
997 break;
998 }
999
1000 am.m_AmName.Append( *aText++ );
1001 }
1002
1003 // Read aperture macro parameters
1004 for( ; ; )
1005 {
1006 if( *aText == '*' )
1007 ++aText;
1008
1009 aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
1010
1011 if( aText == nullptr ) // End of File
1012 return false;
1013
1014 // aText points the beginning of a new line.
1015
1016 // Test for the last line in aperture macro lis:
1017 // last line is % or *% sometime found.
1018 if( *aText == '*' )
1019 ++aText;
1020
1021 if( *aText == '%' )
1022 break; // exit with aText still pointing at %
1023
1024 int paramCount = 0; // will be set to the minimal parameters count,
1025 // depending on the actual primitive
1026 int primitive_type = AMP_UNKNOWN;
1027 // Test for a valid symbol at the beginning of a description:
1028 // it can be: a parameter declaration like $1=$2/4
1029 // or a digit (macro primitive selection)
1030 // all other symbols are illegal.
1031 if( *aText == '$' ) // local parameter declaration, inside the aperture macro
1032 {
1035 aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
1036
1037 if( aText == nullptr) // End of File
1038 return false;
1039
1040 param.ReadParamFromAmDef( aText );
1041 continue;
1042 }
1043 else if( !isdigit(*aText) ) // Ill. symbol
1044 {
1045 msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": ill. symbol, line: \"%s\"" ),
1046 am.m_AmName, From_UTF8( aBuff ) );
1047 AddMessageToList( msg );
1048 primitive_type = AMP_COMMENT;
1049 }
1050 else
1051 {
1052 primitive_type = ReadInt( aText );
1053 }
1054
1055 bool is_comment = false;
1056
1057 switch( primitive_type )
1058 {
1059 case AMP_COMMENT: // lines starting by 0 are a comment
1060 paramCount = 0;
1061 is_comment = true;
1062
1063 // Skip comment
1065
1066 break;
1067
1068 case AMP_CIRCLE:
1069 paramCount = 4; // minimal count. can have a optional parameter (rotation)
1070 break;
1071
1072 case AMP_LINE2:
1073 case AMP_LINE20:
1074 paramCount = 7;
1075 break;
1076
1077 case AMP_LINE_CENTER:
1079 paramCount = 6;
1080 break;
1081
1082 case AMP_OUTLINE:
1083 paramCount = 4; // partial count. other parameters are vertices and rotation
1084 // Second parameter is vertice (coordinate pairs) count.
1085 break;
1086
1087 case AMP_POLYGON:
1088 paramCount = 6;
1089 break;
1090
1091 case AMP_MOIRE:
1092 paramCount = 9;
1093 break;
1094
1095 case AMP_THERMAL:
1096 paramCount = 6;
1097 break;
1098
1099 default:
1100 msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": Invalid primitive id code %d, line %d: \"%s\"" ),
1101 am.m_AmName, primitive_type, m_LineNum, From_UTF8( aBuff ) );
1102 AddMessageToList( msg );
1103 return false;
1104 }
1105
1106 if( is_comment )
1107 continue;
1108
1109 AM_PRIMITIVE prim( m_GerbMetric );
1110 prim.m_Primitive_id = (AM_PRIMITIVE_ID) primitive_type;
1111 int ii;
1112
1113 for( ii = 0; ii < paramCount && *aText && *aText != '*'; ++ii )
1114 {
1115 prim.m_Params.push_back( AM_PARAM() );
1116
1117 AM_PARAM& param = prim.m_Params.back();
1118
1119 aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
1120
1121 if( aText == nullptr) // End of File
1122 return false;
1123
1124 param.ReadParamFromAmDef( aText );
1125 }
1126
1127 if( ii < paramCount )
1128 {
1129 // maybe some day we can throw an exception and track a line number
1130 msg.Printf( wxT( "RS274X: read macro descr type %d: read %d parameters, insufficient "
1131 "parameters\n" ),
1132 prim.m_Primitive_id, ii );
1133 AddMessageToList( msg );
1134 }
1135
1136 // there are more parameters to read if this is an AMP_OUTLINE
1137 if( prim.m_Primitive_id == AMP_OUTLINE )
1138 {
1139 // so far we have read [0]:exposure, [1]:#points, [2]:X start, [3]: Y start
1140 // Now read all the points, plus trailing rotation in degrees.
1141
1142 // m_Params[1] is a count of polygon points, so it must be given
1143 // in advance, i.e. be immediate.
1144 wxASSERT( prim.m_Params[1].IsImmediate() );
1145
1146 paramCount = (int) prim.m_Params[1].GetValueFromMacro( nullptr ) * 2 + 1;
1147
1148 for( int jj = 0; jj < paramCount && *aText != '*'; ++jj )
1149 {
1150 prim.m_Params.push_back( AM_PARAM() );
1151
1152 AM_PARAM& param = prim.m_Params.back();
1153
1154 aText = GetNextLine( aBuff, aBuffSize, aText, gerber_file );
1155
1156 if( aText == nullptr ) // End of File
1157 return false;
1158
1159 param.ReadParamFromAmDef( aText );
1160 }
1161 }
1162
1163 // AMP_CIRCLE can have a optional parameter (rotation)
1164 if( prim.m_Primitive_id == AMP_CIRCLE && aText && *aText != '*' )
1165 {
1166 prim.m_Params.push_back( AM_PARAM() );
1167 AM_PARAM& param = prim.m_Params.back();
1168 param.ReadParamFromAmDef( aText );
1169 }
1170
1171 // The primitive description is now parsed: push it to the current aperture macro
1172 am.AddPrimitiveToList( prim );
1173 }
1174
1175 m_aperture_macros.insert( std::move( am ) );
1176
1177 return true;
1178}
1179
int ReadInt(char *&text, bool aSkipSeparator=true)
Read an integer from an ASCII character buffer.
double ReadDouble(char *&text, bool aSkipSeparator=true)
Read a double precision floating point number from an ASCII character buffer.
AM_PRIMITIVE_ID
The set of all "aperture macro primitives" (primitive numbers).
@ AMP_POLYGON
@ AMP_LINE_LOWER_LEFT
@ AMP_LINE2
@ AMP_CIRCLE
@ AMP_THERMAL
@ AMP_UNKNOWN
@ AMP_COMMENT
@ AMP_LINE_CENTER
@ AMP_MOIRE
@ AMP_OUTLINE
@ AMP_LINE20
constexpr EDA_IU_SCALE gerbIUScale
Definition base_units.h:111
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
Hold a parameter value for an "aperture macro" as defined within standard RS274X.
Definition am_param.h:285
bool ReadParamFromAmDef(char *&aText)
Read one aperture macro parameter.
Definition am_param.cpp:168
An aperture macro primitive as given in gerber layer format doc.
AM_PARAMS m_Params
A sequence of parameters used by the primitive.
AM_PRIMITIVE_ID m_Primitive_id
The primitive type.
Support the "aperture macro" defined within standard RS274X.
wxString m_AmName
The name of the aperture macro as defined like AMVB_RECTANGLE* (name is VB_RECTANGLE)
void AddLocalParamDefToStack()
A deferred parameter can be defined in aperture macro, but outside aperture primitives.
AM_PARAM & GetLastLocalParamDefFromStack()
void AddPrimitiveToList(AM_PRIMITIVE &aPrimitive)
Add a new ptimitive ( AMP_CIRCLE, AMP_LINE2 ...) to the list of primitives to define the full shape o...
A gerber DCODE (also called Aperture) definition.
Definition dcode.h:80
wxString m_AperFunction
the aperture attribute (created by a TA.AperFunction command).
Definition dcode.h:214
EDA_ANGLE m_Rotation
shape rotation
Definition dcode.h:208
VECTOR2I m_Drill
dimension of the hole (if any) (drill file)
Definition dcode.h:205
void SetMacro(APERTURE_MACRO *aMacro)
Definition dcode.h:131
int m_EdgesCount
in aperture definition Polygon only: number of edges for the polygon
Definition dcode.h:209
VECTOR2I m_Size
Horizontal and vertical dimensions.
Definition dcode.h:201
APERTURE_T m_ApertType
Aperture type ( Line, rectangle, circle, oval poly, macro )
Definition dcode.h:202
bool m_Defined
false if the aperture is not defined in the header
Definition dcode.h:213
APERTURE_DEF_HOLETYPE m_DrillShape
shape of the hole (0 = no hole, round = 1, rect = 2).
Definition dcode.h:206
void AppendParam(double aValue)
Add a parameter to the D_CODE parameter list.
Definition dcode.h:102
@ GBR_NETINFO_NET
print info associated to a net (TO.N attribute)
@ GBR_NETINFO_CMP
print info associated to a component (TO.C attribute)
@ GBR_NETINFO_PAD
print info associated to a flashed pad (TO.P attribute)
APERTURE_MACRO * FindApertureMacro(const APERTURE_MACRO &aLookup)
Look up a previously read in aperture macro.
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
file function parameters, found in a TF command or a G04
void RemoveAttribute(X2_ATTRIBUTE &aAttribute)
Called when a TD command is found the Gerber file.
static char m_LineBuffer[GERBER_BUFZ+1]
bool m_SwapAxis
false if A = X and B = Y (default); true if A = Y, B = X
double m_LocalRotation
Local rotation added to m_ImageRotation.
VECTOR2I m_Offset
Coord Offset, from OF command.
wxSize m_FmtScale
Fmt 2.3: m_FmtScale = 3, fmt 3.4: m_FmtScale = 4.
wxString m_FileName
Full File Name for this layer.
bool m_ImageJustifyXCenter
Image Justify Center on X axis (default = false)
bool m_ImageJustifyYCenter
Image Justify Center on Y axis (default = false)
bool m_ImageNegative
true = Negative image
void AddMessageToList(const wxString &aMessage)
Add a message to the message list.
int m_LineNum
Line number of the gerber file while reading.
wxString m_ImageName
Image name, from IN <name>* command.
bool m_Relative
false = absolute Coord, true = relative Coord.
wxString m_MD5_value
MD5 value found in a TF.MD5 command.
char * GetNextLine(char *aBuff, unsigned int aBuffSize, char *aText, FILE *aFile)
Test for an end of line.
Definition rs274x.cpp:958
bool m_MirrorB
true: mirror / axis B (Y)
wxString m_PartString
string found in a TF.Part command
bool ReadRS274XCommand(char *aBuff, unsigned int aBuffSize, char *&aText)
Read a single RS274X command terminated with a %.
Definition rs274x.cpp:142
bool m_IsX2_file
True if a X2 gerber attribute was found in file.
int m_ImageRotation
Image rotation (0, 90, 180, 270 only) in degrees.
VECTOR2I m_ImageOffset
Coord Offset, from IO command.
bool m_Has_DCode
< True if has DCodes in file or false if no DCodes found. Perhaps deprecated RS274D file.
bool GetEndOfBlock(char *aBuff, unsigned int aBuffSize, char *&aText, FILE *aGerberFile)
Definition rs274x.cpp:932
bool ReadApertureMacro(char *aBuff, unsigned int aBuffSize, char *&text, FILE *gerber_file)
Read in an aperture macro and saves it in m_aperture_macros.
Definition rs274x.cpp:985
D_CODE * GetDCODEOrCreate(int aDCODE, bool aCreateIfNoExist=true)
Return a pointer to the D_CODE within this GERBER for the given aDCODE.
bool ExecuteRS274XCommand(int aCommand, char *aBuff, unsigned int aBuffSize, char *&aText)
Execute a RS274X command.
Definition rs274x.cpp:198
wxSize m_FmtLen
Nb chars per coord. ex fmt 2.3, m_FmtLen = 5.
APERTURE_MACRO_SET m_aperture_macros
int ReadXCommandID(char *&text)
Read two bytes of data and assembles them into an int with the first byte in the sequence put into th...
Definition rs274x.cpp:114
bool m_GerbMetric
false = Inches, true = metric
GBR_NETLIST_METADATA m_NetAttributeDict
int m_CommandState
state of gerber analysis command
bool m_NoTrailingZeros
true: remove tailing zeros.
int m_Iterpolation
Linear, 90 arc, Circ.
bool m_MirrorA
true: mirror / axis A (X)
VECTOR2I m_Scale
scale (X and Y) of layer.
VECTOR2I m_ImageJustifyOffset
Image Justify Offset on XY axis (default = 0,0)
GERBER_LAYER & GetLayerParams()
wxRealPoint m_StepForRepeat
X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF....
The attribute value consists of a number of substrings separated by a comma.
@ APT_DEF_ROUND_HOLE
Definition dcode.h:62
@ APT_DEF_RECT_HOLE
Definition dcode.h:63
@ APT_RECT
Definition dcode.h:50
@ APT_OVAL
Definition dcode.h:51
@ APT_POLYGON
Definition dcode.h:52
@ APT_CIRCLE
Definition dcode.h:49
@ APT_MACRO
Definition dcode.h:54
#define SCALE_FACTOR(x)
#define _(s)
@ DEGREES_T
Definition eda_angle.h:31
wxString FormatStringFromGerber(const wxString &aString)
Convert a gerber string into a 16 bit Unicode string.
Handle special data (items attributes) during plot.
#define GERBER_BUFZ
@ GERB_INTERPOL_LINEAR_1X
Definition gerbview.h:35
@ CMD_IDLE
Definition gerbview.h:64
void ignore_unused(const T &)
Definition ignore.h:24
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition macros.h:83
RS274X_PARAMETERS
Definition rs274x.cpp:55
@ FILE_ATTRIBUTE
Definition rs274x.cpp:85
@ IMAGE_JUSTIFY
Definition rs274x.cpp:69
@ APERTURE_ATTRIBUTE
Definition rs274x.cpp:93
@ FORMAT_STATEMENT
Definition rs274x.cpp:59
@ NET_ATTRIBUTE
Definition rs274x.cpp:90
@ IMAGE_OFFSET
Definition rs274x.cpp:71
@ ROTATE
Definition rs274x.cpp:107
@ STEP_AND_REPEAT
Definition rs274x.cpp:106
@ LOAD_NAME
Definition rs274x.cpp:110
@ REMOVE_APERTURE_ATTRIBUTE
Definition rs274x.cpp:99
@ KNOCKOUT
Definition rs274x.cpp:105
@ AP_MACRO
Definition rs274x.cpp:78
@ MIRROR_IMAGE
Definition rs274x.cpp:60
@ MODE_OF_UNITS
Definition rs274x.cpp:61
@ INCH
Definition rs274x.cpp:62
@ IMAGE_NAME
Definition rs274x.cpp:70
@ OFFSET
Definition rs274x.cpp:64
@ MILLIMETER
Definition rs274x.cpp:63
@ AXIS_SELECT
Definition rs274x.cpp:58
@ IMAGE_ROTATION
Definition rs274x.cpp:73
@ LOAD_POLARITY
Definition rs274x.cpp:109
@ IMAGE_POLARITY
Definition rs274x.cpp:72
@ AP_DEFINITION
Definition rs274x.cpp:77
#define CODE(x, y)
Definition rs274x.cpp:42
int ReadInt(char *&text, bool aSkipSeparator=true)
Read an integer from an ASCII character buffer.
double ReadDouble(char *&text, bool aSkipSeparator=true)
Read a double precision floating point number from an ASCII character buffer.
std::vector< FAB_LAYER_COLOR > dummy
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695