forked from mtl/svg2mod
/
svg2mod.py
executable file
·1437 lines (1003 loc) · 38 KB
/
svg2mod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
from __future__ import absolute_import
import argparse
import datetime
import os
from pprint import pformat, pprint
import re
import svg
import sys
#----------------------------------------------------------------------------
def main():
args, parser = get_arguments()
pretty = args.format == 'pretty'
use_mm = args.units == 'mm'
if pretty:
if not use_mm:
print( "Error: decimil units only allowed with legacy output type" )
sys.exit( -1 )
#if args.include_reverse:
#print(
#"Warning: reverse footprint not supported or required for" +
#" pretty output format"
#)
# Import the SVG:
imported = Svg2ModImport(
args.input_file_name,
args.module_name,
args.module_value
)
# Pick an output file name if none was provided:
if args.output_file_name is None:
args.output_file_name = os.path.splitext(
os.path.basename( args.input_file_name )
)[ 0 ]
# Append the correct file name extension if needed:
if pretty:
extension = ".kicad_mod"
else:
extension = ".mod"
if args.output_file_name[ - len( extension ) : ] != extension:
args.output_file_name += extension
# Create an exporter:
if pretty:
exported = Svg2ModExportPretty(
imported,
args.output_file_name,
args.scale_factor,
args.precision,
)
else:
# If the module file exists, try to read it:
exported = None
if os.path.isfile( args.output_file_name ):
try:
exported = Svg2ModExportLegacyUpdater(
imported,
args.output_file_name,
args.scale_factor,
args.precision,
include_reverse = not args.front_only,
)
except Exception as e:
raise e
#print( e.message )
#exported = None
# Write the module file:
if exported is None:
exported = Svg2ModExportLegacy(
imported,
args.output_file_name,
args.scale_factor,
args.precision,
use_mm = use_mm,
include_reverse = not args.front_only,
)
# Export the footprint:
exported.write()
#----------------------------------------------------------------------------
class LineSegment( object ):
#------------------------------------------------------------------------
@staticmethod
def _on_segment( p, q, r ):
""" Given three colinear points p, q, and r, check if
point q lies on line segment pr. """
if (
q.x <= max( p.x, r.x ) and
q.x >= min( p.x, r.x ) and
q.y <= max( p.y, r.y ) and
q.y >= min( p.y, r.y )
):
return True
return False
#------------------------------------------------------------------------
@staticmethod
def _orientation( p, q, r ):
""" Find orientation of ordered triplet (p, q, r).
Returns following values
0 --> p, q and r are colinear
1 --> Clockwise
2 --> Counterclockwise
"""
val = (
( q.y - p.y ) * ( r.x - q.x ) -
( q.x - p.x ) * ( r.y - q.y )
)
if val == 0: return 0
if val > 0: return 1
return 2
#------------------------------------------------------------------------
def __init__( self, p = None, q = None ):
self.p = p
self.q = q
#------------------------------------------------------------------------
def connects( self, segment ):
if self.q.x == segment.p.x and self.q.y == segment.p.y: return True
if self.q.x == segment.q.x and self.q.y == segment.q.y: return True
if self.p.x == segment.p.x and self.p.y == segment.p.y: return True
if self.p.x == segment.q.x and self.p.y == segment.q.y: return True
return False
#------------------------------------------------------------------------
def intersects( self, segment ):
""" Return true if line segments 'p1q1' and 'p2q2' intersect.
Adapted from:
http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
"""
# Find the four orientations needed for general and special cases:
o1 = self._orientation( self.p, self.q, segment.p )
o2 = self._orientation( self.p, self.q, segment.q )
o3 = self._orientation( segment.p, segment.q, self.p )
o4 = self._orientation( segment.p, segment.q, self.q )
return (
# General case:
( o1 != o2 and o3 != o4 )
or
# p1, q1 and p2 are colinear and p2 lies on segment p1q1:
( o1 == 0 and self._on_segment( self.p, segment.p, self.q ) )
or
# p1, q1 and p2 are colinear and q2 lies on segment p1q1:
( o2 == 0 and self._on_segment( self.p, segment.q, self.q ) )
or
# p2, q2 and p1 are colinear and p1 lies on segment p2q2:
( o3 == 0 and self._on_segment( segment.p, self.p, segment.q ) )
or
# p2, q2 and q1 are colinear and q1 lies on segment p2q2:
( o4 == 0 and self._on_segment( segment.p, self.q, segment.q ) )
)
#------------------------------------------------------------------------
def q_next( self, q ):
self.p = self.q
self.q = q
#------------------------------------------------------------------------
#----------------------------------------------------------------------------
class PolygonSegment( object ):
#------------------------------------------------------------------------
def __init__( self, points ):
self.points = points
if len( points ) < 3:
print(
"Warning:"
" Path segment has only {} points (not a polygon?)".format(
len( points )
)
)
#------------------------------------------------------------------------
# KiCad will not "pick up the pen" when moving between a polygon outline
# and holes within it, so we search for a pair of points connecting the
# outline (self) to the hole such that the connecting segment will not
# cross the visible inner space within any hole.
def _find_insertion_point( self, hole, holes ):
#print( " Finding insertion point. {} holes".format( len( holes ) ) )
# Try the next point on the container:
for cp in range( len( self.points ) ):
container_point = self.points[ cp ]
#print( " Trying container point {}".format( cp ) )
# Try the next point on the hole:
for hp in range( len( hole.points ) - 1 ):
hole_point = hole.points[ hp ]
#print( " Trying hole point {}".format( cp ) )
bridge = LineSegment( container_point, hole_point )
# Check for intersection with each other hole:
for other_hole in holes:
#print( " Trying other hole. Check = {}".format( hole == other_hole ) )
# If the other hole intersects, don't bother checking
# remaining holes:
if other_hole.intersects(
bridge,
check_connects = (
other_hole == hole or other_hole == self
)
): break
#print( " Hole does not intersect." )
else:
print( " Found insertion point: {}, {}".format( cp, hp ) )
# No other holes intersected, so this insertion point
# is acceptable:
return ( cp, hole.points_starting_on_index( hp ) )
print(
"Could not insert segment without overlapping other segments"
)
#------------------------------------------------------------------------
# Return the list of ordered points starting on the given index, ensuring
# that the first and last points are the same.
def points_starting_on_index( self, index ):
points = self.points
if index > 0:
# Strip off end point, which is a duplicate of the start point:
points = points[ : -1 ]
points = points[ index : ] + points[ : index ]
points.append(
svg.Point( points[ 0 ].x, points[ 0 ].y )
)
return points
#------------------------------------------------------------------------
# Return a list of points with the given polygon segments (paths) inlined.
def inline( self, segments ):
if len( segments ) < 1:
return self.points
print( " Inlining {} segments...".format( len( segments ) ) )
all_segments = segments[ : ] + [ self ]
insertions = []
# Find the insertion point for each hole:
for hole in segments:
insertion = self._find_insertion_point(
hole, all_segments
)
if insertion is not None:
insertions.append( insertion )
insertions.sort( key = lambda i: i[ 0 ] )
inlined = [ self.points[ 0 ] ]
ip = 1
points = self.points
for insertion in insertions:
while ip <= insertion[ 0 ]:
inlined.append( points[ ip ] )
ip += 1
if (
inlined[ -1 ].x == insertion[ 1 ][ 0 ].x and
inlined[ -1 ].y == insertion[ 1 ][ 0 ].y
):
inlined += insertion[ 1 ][ 1 : -1 ]
else:
inlined += insertion[ 1 ]
inlined.append( svg.Point(
points[ ip - 1 ].x,
points[ ip - 1 ].y,
) )
while ip < len( points ):
inlined.append( points[ ip ] )
ip += 1
return inlined
#------------------------------------------------------------------------
def intersects( self, line_segment, check_connects ):
hole_segment = LineSegment()
# Check each segment of other hole for intersection:
for point in self.points:
hole_segment.q_next( point )
if hole_segment.p is not None:
if (
check_connects and
line_segment.connects( hole_segment )
): continue
if line_segment.intersects( hole_segment ):
#print( "Intersection detected." )
return True
return False
#------------------------------------------------------------------------
# Apply all transformations and rounding, then remove duplicate
# consecutive points along the path.
def process( self, transformer, flip ):
points = []
for point in self.points:
point = transformer.transform_point( point, flip )
if (
len( points ) < 1 or
point.x != points[ -1 ].x or
point.y != points[ -1 ].y
):
points.append( point )
if (
points[ 0 ].x != points[ -1 ].x or
points[ 0 ].y != points[ -1 ].y
):
#print( "Warning: Closing polygon. start=({}, {}) end=({}, {})".format(
#points[ 0 ].x, points[ 0 ].y,
#points[ -1 ].x, points[ -1 ].y,
#) )
points.append( svg.Point(
points[ 0 ].x,
points[ 0 ].y,
) )
#else:
#print( "Polygon closed: start=({}, {}) end=({}, {})".format(
#points[ 0 ].x, points[ 0 ].y,
#points[ -1 ].x, points[ -1 ].y,
#) )
self.points = points
#------------------------------------------------------------------------
#----------------------------------------------------------------------------
class Svg2ModImport( object ):
#------------------------------------------------------------------------
def __init__( self, file_name, module_name, module_value ):
self.file_name = file_name
self.module_name = module_name
self.module_value = module_value
print( "Parsing SVG..." )
self.svg = svg.parse( file_name )
#------------------------------------------------------------------------
#----------------------------------------------------------------------------
class Svg2ModExport( object ):
#------------------------------------------------------------------------
@staticmethod
def _convert_decimil_to_mm( decimil ):
return float( decimil ) * 0.00254
#------------------------------------------------------------------------
@staticmethod
def _convert_mm_to_decimil( mm ):
return int( round( mm * 393.700787 ) )
#------------------------------------------------------------------------
@classmethod
def _get_fill_stroke( cls, item ):
fill = True
stroke = True
stroke_width = 0.0
if item.style is not None and item.style != "":
for property in item.style.split( ";" ):
nv = property.split( ":" );
name = nv[ 0 ].strip()
value = nv[ 1 ].strip()
if name == "fill" and value == "none":
fill = False
elif name == "stroke" and value == "none":
stroke = False
elif name == "stroke-width":
value = value.replace( "px", "" )
stroke_width = float( value ) * 25.4 / 90.0
if not stroke:
stroke_width = 0.0
elif stroke_width is None:
# Give a default stroke width?
stroke_width = cls._convert_decimil_to_mm( 1 )
return fill, stroke, stroke_width
#------------------------------------------------------------------------
def __init__(
self,
svg2mod_import,
file_name,
scale_factor = 1.0,
precision = 20.0,
use_mm = True,
):
if use_mm:
# 25.4 mm/in; Inkscape uses 90 DPI:
scale_factor *= 25.4 / 90.0
use_mm = True
else:
# PCBNew uses "decimil" (10K DPI); Inkscape uses 90 DPI:
scale_factor *= 10000.0 / 90.0
self.imported = svg2mod_import
self.file_name = file_name
self.scale_factor = scale_factor
self.precision = precision
self.use_mm = use_mm
#------------------------------------------------------------------------
def _calculate_translation( self ):
min_point, max_point = self.imported.svg.bbox()
# Center the drawing:
adjust_x = min_point.x + ( max_point.x - min_point.x ) / 2.0
adjust_y = min_point.y + ( max_point.y - min_point.y ) / 2.0
self.translation = svg.Point(
0.0 - adjust_x,
0.0 - adjust_y,
)
#------------------------------------------------------------------------
# Find and keep only the layers of interest.
def _prune( self, items = None ):
if items is None:
self.layers = {}
for name in self.layer_map.iterkeys():
self.layers[ name ] = None
items = self.imported.svg.items
self.imported.svg.items = []
for item in items:
if not isinstance( item, svg.Group ):
continue
for name in self.layers.iterkeys():
#if re.search( name, item.name, re.I ):
if name == item.name:
print( "Found SVG layer: {}".format( item.name ) )
self.imported.svg.items.append( item )
self.layers[ name ] = item
break
else:
self._prune( item.items )
#------------------------------------------------------------------------
def _write_items( self, items, layer, flip = False ):
for item in items:
if isinstance( item, svg.Group ):
self._write_items( item.items, layer, flip )
continue
elif isinstance( item, svg.Path ):
segments = [
PolygonSegment( segment )
for segment in item.segments(
precision = self.precision
)
]
for segment in segments:
segment.process( self, flip )
if len( segments ) > 1:
points = segments[ 0 ].inline( segments[ 1 : ] )
elif len( segments ) > 0:
points = segments[ 0 ].points
fill, stroke, stroke_width = self._get_fill_stroke( item )
if not self.use_mm:
stroke_width = self._convert_mm_to_decimil(
stroke_width
)
print( " Writing polygon with {} points".format(
len( points ) )
)
self._write_polygon(
points, layer, fill, stroke, stroke_width
)
else:
print( "Unsupported SVG element: {}".format(
item.__class__.__name__
) )
#------------------------------------------------------------------------
def _write_module( self, front ):
module_name = self._get_module_name( front )
min_point, max_point = self.imported.svg.bbox()
min_point = self.transform_point( min_point, flip = False )
max_point = self.transform_point( max_point, flip = False )
label_offset = 1200
label_size = 600
label_pen = 120
if self.use_mm:
label_size = self._convert_decimil_to_mm( label_size )
label_pen = self._convert_decimil_to_mm( label_pen )
reference_y = min_point.y - self._convert_decimil_to_mm( label_offset )
value_y = max_point.y + self._convert_decimil_to_mm( label_offset )
else:
reference_y = min_point.y - label_offset
value_y = max_point.y + label_offset
self._write_module_header(
label_size, label_pen,
reference_y, value_y,
front,
)
for name, group in self.layers.iteritems():
if group is None: continue
layer = self._get_layer_name( name, front )
#print( " Writing layer: {}".format( name ) )
self._write_items( group.items, layer, not front )
self._write_module_footer( front )
#------------------------------------------------------------------------
def _write_polygon_filled( self, points, layer, stroke_width = 0.0 ):
self._write_polygon_header( points, layer )
for point in points:
self._write_polygon_point( point )
self._write_polygon_footer( layer, stroke_width )
#------------------------------------------------------------------------
def _write_polygon_outline( self, points, layer, stroke_width ):
prior_point = None
for point in points:
if prior_point is not None:
self._write_polygon_segment(
prior_point, point, layer, stroke_width
)
prior_point = point
#------------------------------------------------------------------------
def transform_point( self, point, flip = False ):
transformed_point = svg.Point(
( point.x + self.translation.x ) * self.scale_factor,
( point.y + self.translation.y ) * self.scale_factor,
)
if flip:
transformed_point.x *= -1
if self.use_mm:
transformed_point.x = round( transformed_point.x, 12 )
transformed_point.y = round( transformed_point.y, 12 )
else:
transformed_point.x = int( round( transformed_point.x ) )
transformed_point.y = int( round( transformed_point.y ) )
return transformed_point
#------------------------------------------------------------------------
def write( self ):
self._prune()
# Must come after pruning:
translation = self._calculate_translation()
print( "Writing module file: {}".format( self.file_name ) )
self.output_file = open( self.file_name, 'w' )
self._write_library_intro()
self._write_modules()
self.output_file.close()
self.output_file = None
#------------------------------------------------------------------------
#----------------------------------------------------------------------------
class Svg2ModExportLegacy( Svg2ModExport ):
layer_map = {
#'inkscape-name' : [ kicad-front, kicad-back ],
'Cu' : [ 15, 0 ],
'Adhes' : [ 17, 16 ],
'Paste' : [ 19, 18 ],
'SilkS' : [ 21, 20 ],
'Mask' : [ 23, 22 ],
'Dwgs.User' : [ 24, 24 ],
'Cmts.User' : [ 25, 25 ],
'Eco1.User' : [ 26, 26 ],
'Eco2.User' : [ 27, 27 ],
'Edge.Cuts' : [ 28, 28 ],
}
#------------------------------------------------------------------------
def __init__(
self,
svg2mod_import,
file_name,
scale_factor = 1.0,
precision = 20.0,
use_mm = True,
include_reverse = True,
):
super( Svg2ModExportLegacy, self ).__init__(
svg2mod_import,
file_name,
scale_factor,
precision,
use_mm,
)
self.include_reverse = include_reverse
#------------------------------------------------------------------------
def _get_layer_name( self, name, front ):
layer_info = self.layer_map[ name ]
layer = layer_info[ 0 ]
if not front and layer_info[ 1 ] is not None:
layer = layer_info[ 1 ]
return layer
#------------------------------------------------------------------------
def _get_module_name( self, front = None ):
if self.include_reverse and not front:
return self.imported.module_name + "-rev"
return self.imported.module_name
#------------------------------------------------------------------------
def _write_library_intro( self ):
modules_list = self._get_module_name( front = True )
if self.include_reverse:
modules_list += (
"\n" +
self._get_module_name( front = False )
)
units = ""
if self.use_mm:
units = "\nUnits mm"
self.output_file.write( """PCBNEW-LibModule-V1 {0}{1}
$INDEX
{2}
$EndINDEX
#
# {3}
#
""".format(
datetime.datetime.now().strftime( "%a %d %b %Y %I:%M:%S %p %Z" ),
units,
modules_list,
self.imported.file_name,
)
)
#------------------------------------------------------------------------
def _write_module_header(
self,
label_size,
label_pen,
reference_y,
value_y,
front,
):
self.output_file.write( """$MODULE {0}
Po 0 0 0 {6} 00000000 00000000 ~~
Li {0}
T0 0 {1} {2} {2} 0 {3} N I 21 "{0}"
T1 0 {5} {2} {2} 0 {3} N I 21 "{4}"
""".format(
self._get_module_name( front ),
reference_y,
label_size,
label_pen,
self.imported.module_value,
value_y,
15, # Seems necessary
)
)
#------------------------------------------------------------------------
def _write_module_footer( self, front ):
self.output_file.write(
"$EndMODULE {0}\n".format( self._get_module_name( front ) )
)
#------------------------------------------------------------------------
def _write_modules( self ):
self._write_module( front = True )
if self.include_reverse:
self._write_module( front = False )
self.output_file.write( "$EndLIBRARY" )
#------------------------------------------------------------------------
def _write_polygon( self, points, layer, fill, stroke, stroke_width ):
if fill:
self._write_polygon_filled(
points, layer
)
if stroke:
self._write_polygon_outline(
points, layer, stroke_width
)
#------------------------------------------------------------------------
def _write_polygon_footer( self, layer, stroke_width ):
pass
#------------------------------------------------------------------------
def _write_polygon_header( self, points, layer ):
pen = 1
if self.use_mm:
pen = self._convert_decimil_to_mm( pen )
self.output_file.write( "DP 0 0 0 0 {} {} {}\n".format(
len( points ),
pen,
layer
) )
#------------------------------------------------------------------------
def _write_polygon_point( self, point ):
self.output_file.write(
"Dl {} {}\n".format( point.x, point.y )
)
#------------------------------------------------------------------------
def _write_polygon_segment( self, p, q, layer, stroke_width ):
self.output_file.write( "DS {} {} {} {} {} {}\n".format(
p.x, p.y,
q.x, q.y,
stroke_width,
layer
) )
#------------------------------------------------------------------------
#----------------------------------------------------------------------------
class Svg2ModExportLegacyUpdater( Svg2ModExportLegacy ):
#------------------------------------------------------------------------
def __init__(
self,
svg2mod_import,
file_name,
scale_factor = 1.0,
precision = 20.0,
include_reverse = True,
):
self.file_name = file_name
use_mm = self._parse_output_file()
super( Svg2ModExportLegacyUpdater, self ).__init__(
svg2mod_import,
file_name,
scale_factor,
precision,
use_mm,
include_reverse,
)
#------------------------------------------------------------------------
def _parse_output_file( self ):
print( "Parsing module file: {}".format( self.file_name ) )
module_file = open( self.file_name, 'r' )
lines = module_file.readlines()
module_file.close()
self.loaded_modules = {}
self.post_index = []
self.pre_index = []
use_mm = False
index = 0
# Find the start of the index:
while index < len( lines ):
line = lines[ index ]
index += 1
self.pre_index.append( line )
if line[ : 6 ] == "$INDEX":
break
m = re.match( "Units[\s]+mm[\s]*", line )
if m is not None:
print( " Use mm detected" )
use_mm = True
# Read the index:
while index < len( lines ):
line = lines[ index ]