Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[inkscape.git] / share / extensions / dxf_input.py
index 7c5e4d0f91b390a80df04e02499879a34758622d..b46477de16bf581366e880e4caa08cb948d7f7de 100644 (file)
@@ -35,7 +35,7 @@ def export_MTEXT():
         size = 12                       # default fontsize in px
         if vals[groups['40']]:
             size = scale*vals[groups['40']][0]
-        attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s' % (size, color)}
+        attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s; font-family: %s' % (size, color, options.font)}
         angle = 0                       # default angle in degrees
         if vals[groups['50']]:
             angle = vals[groups['50']][0]
@@ -65,7 +65,10 @@ def export_MTEXT():
 def export_POINT():
     # mandatory group codes : (10, 20) (x, y)
     if vals[groups['10']] and vals[groups['20']]:
-        generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], w/2, 0.0, 1.0, 0.0, 0.0)
+        if options.gcodetoolspoints:
+            generate_gcodetools_point(vals[groups['10']][0], vals[groups['20']][0])
+        else:
+            generate_ellipse(vals[groups['10']][0], vals[groups['20']][0], w/2, 0.0, 1.0, 0.0, 0.0)
 
 def export_LINE():
     # mandatory group codes : (10, 11, 20, 21) (x1, x2, y1, y2)
@@ -118,6 +121,10 @@ def export_LWPOLYLINE():
             # optional group codes : (42) (bulge)
             iseqs = 0
             ibulge = 0
+            if vals[groups['70']][0]:           # closed path
+                seqs.append('20')
+                vals[groups['10']].append(vals[groups['10']][0])
+                vals[groups['20']].append(vals[groups['20']][0])
             while seqs[iseqs] != '20':
                 iseqs += 1
             path = 'M %f,%f' % (vals[groups['10']][0], vals[groups['20']][0])
@@ -146,7 +153,7 @@ def export_LWPOLYLINE():
                     path += ' L %f,%f' % (vals[groups['10']][i], vals[groups['20']][i])
                 xold = vals[groups['10']][i]
                 yold = vals[groups['20']][i]
-            if vals[groups['70']][0] == 1:      # closed path
+            if vals[groups['70']][0]:           # closed path
                 path += ' z'
             attribs = {'d': path, 'style': style}
             inkex.etree.SubElement(layer, 'path', attribs)
@@ -154,7 +161,7 @@ def export_LWPOLYLINE():
 def export_HATCH():
     # mandatory group codes : (10, 20, 70, 72, 92, 93) (x, y, fill, Edge Type, Path Type, Number of edges)
     if vals[groups['10']] and vals[groups['20']] and vals[groups['70']] and vals[groups['72']] and vals[groups['92']] and vals[groups['93']]:
-        if vals[groups['70']][0] and len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
+        if len(vals[groups['10']]) > 1 and len(vals[groups['20']]) == len(vals[groups['10']]):
             # optional group codes : (11, 21, 40, 50, 51, 73) (x, y, r, angle1, angle2, CCW)
             i10 = 1    # count start points
             i11 = 0    # count line end points
@@ -198,7 +205,10 @@ def export_HATCH():
                         i72 += 1
                     i10 += 1
                 path += "z "
-            style = simplestyle.formatStyle({'fill': '%s' % color})
+            if vals[groups['70']][0]:
+                style = simplestyle.formatStyle({'fill': '%s' % color})
+            else:
+                style = simplestyle.formatStyle({'fill': 'url(#Hatch)', 'fill-opacity': '1.0'})
             attribs = {'d': path, 'style': style}
             inkex.etree.SubElement(layer, 'path', attribs)
 
@@ -217,7 +227,7 @@ def export_DIMENSION():
             path = 'M %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['10']][0], vals[groups['23']][0])
         else:
             return
-        attribs = {'d': path, 'style': style + '; marker-start: url(#DistanceX); marker-end: url(#DistanceX)'}
+        attribs = {'d': path, 'style': style + '; marker-start: url(#DistanceX); marker-end: url(#DistanceX); stroke-width: 0.25px'}
         inkex.etree.SubElement(layer, 'path', attribs)
         x = scale*(vals[groups['11']][0] - xmin)
         y = - scale*(vals[groups['21']][0] - ymax)
@@ -227,7 +237,7 @@ def export_DIMENSION():
                 size = scale*DIMTXT[vals[groups['3']][0]]
                 if size < 2:
                     size = 2
-        attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s' % (size, color)}
+        attribs = {'x': '%f' % x, 'y': '%f' % y, 'style': 'font-size: %.1fpx; fill: %s; font-family: %s; text-anchor: middle; text-align: center' % (size, color, options.font)}
         if dx == 0:
             attribs.update({'transform': 'rotate (%f %f %f)' % (-90, x, y)})
         node = inkex.etree.SubElement(layer, 'text', attribs)
@@ -280,6 +290,11 @@ def generate_ellipse(xc, yc, xm, ym, w, a1, a2):
     attribs = {'d': path, 'style': style}
     inkex.etree.SubElement(layer, 'path', attribs)
 
+def generate_gcodetools_point(xc, yc):
+    path= 'm %s,%s 2.9375,-6.34375 0.8125,1.90625 6.84375,-6.84375 0,0 0.6875,0.6875 -6.84375,6.84375 1.90625,0.8125 z' % (xc,yc)
+    attribs = {'d': path, inkex.addNS('dxfpoint','inkscape'):'1', 'style': 'stroke:#ff0000;fill:#ff0000'}
+    inkex.etree.SubElement(layer, 'path', attribs)
+
 def get_line():
     return (stream.readline().strip(), stream.readline().strip())
 
@@ -292,7 +307,7 @@ def get_group(group):
 
 #   define DXF Entities and specify which Group Codes to monitor
 
-entities = {'MTEXT': export_MTEXT, 'TEXT': export_MTEXT, 'POINT': export_POINT, 'LINE': export_LINE, 'SPLINE': export_SPLINE, 'CIRCLE': export_CIRCLE, 'ARC': export_ARC, 'ELLIPSE': export_ELLIPSE, 'LEADER': export_LEADER, 'LWPOLYLINE': export_LWPOLYLINE, 'HATCH': export_HATCH, 'DIMENSION': export_DIMENSION, 'INSERT': export_INSERT, 'BLOCK': export_BLOCK, 'ENDBLK': export_ENDBLK, 'ATTDEF': export_ATTDEF, 'DICTIONARY': False}
+entities = {'MTEXT': export_MTEXT, 'TEXT': export_MTEXT, 'POINT': export_POINT, 'LINE': export_LINE, 'SPLINE': export_SPLINE, 'CIRCLE': export_CIRCLE, 'ARC': export_ARC, 'ELLIPSE': export_ELLIPSE, 'LEADER': export_LEADER, 'LWPOLYLINE': export_LWPOLYLINE, 'HATCH': export_HATCH, 'DIMENSION': export_DIMENSION, 'INSERT': export_INSERT, 'BLOCK': export_BLOCK, 'ENDBLK': export_ENDBLK, 'ATTDEF': export_ATTDEF, 'VIEWPORT': False, 'DICTIONARY': False}
 groups = {'1': 0, '2': 1, '3': 2, '6': 3, '8': 4, '10': 5, '11': 6, '13': 7, '14': 8, '20': 9, '21': 10, '23': 11, '24': 12, '40': 13, '41': 14, '42': 15, '50': 16, '51': 17, '62': 18, '70': 19, '72': 20, '73': 21, '92': 22, '93': 23, '370': 24}
 colors = {  1: '#FF0000',   2: '#FFFF00',   3: '#00FF00',   4: '#00FFFF',   5: '#0000FF',
             6: '#FF00FF',   8: '#414141',   9: '#808080',  12: '#BD0000',  30: '#FF7F00',
@@ -301,7 +316,9 @@ colors = {  1: '#FF0000',   2: '#FFFF00',   3: '#00FF00',   4: '#00FFFF',   5: '
 parser = inkex.optparse.OptionParser(usage="usage: %prog [options] SVGfile", option_class=inkex.InkOption)
 parser.add_option("--auto", action="store", type="inkbool", dest="auto", default=True)
 parser.add_option("--scale", action="store", type="string", dest="scale", default="1.0")
+parser.add_option("--gcodetoolspoints", action="store", type="inkbool", dest="gcodetoolspoints", default=True)
 parser.add_option("--encoding", action="store", type="string", dest="input_encode", default="latin_1")
+parser.add_option("--font", action="store", type="string", dest="font", default="Arial")
 parser.add_option("--tab", action="store", type="string", dest="tab", default="Options")
 parser.add_option("--inputhelp", action="store", type="string", dest="inputhelp", default="")
 (options, args) = parser.parse_args(inkex.sys.argv[1:])
@@ -310,6 +327,10 @@ desc = inkex.etree.SubElement(doc.getroot(), 'desc', {})
 defs = inkex.etree.SubElement(doc.getroot(), 'defs', {})
 marker = inkex.etree.SubElement(defs, 'marker', {'id': 'DistanceX', 'orient': 'auto', 'refX': '0.0', 'refY': '0.0', 'style': 'overflow:visible'})
 inkex.etree.SubElement(marker, 'path', {'d': 'M 3,-3 L -3,3 M 0,-5 L  0,5', 'style': 'stroke:#000000; stroke-width:0.5'})
+pattern = inkex.etree.SubElement(defs, 'pattern', {'id': 'Hatch', 'patternUnits': 'userSpaceOnUse', 'width': '8', 'height': '8', 'x': '0', 'y': '0'})
+inkex.etree.SubElement(pattern, 'path', {'d': 'M8 4 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
+inkex.etree.SubElement(pattern, 'path', {'d': 'M6 2 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
+inkex.etree.SubElement(pattern, 'path', {'d': 'M4 0 l-4,4', 'stroke': '#000000', 'stroke-width': '0.25', 'linecap': 'square'})
 stream = open(args[0], 'r')
 xmax = xmin = 0.0
 ymax = 297.0                                        # default A4 height in mm
@@ -369,8 +390,14 @@ if not layer_nodes:
 for linename in linetypes.keys():                   # scale the dashed lines
     linetype = ''
     for length in linetypes[linename]:
-        linetype += '%.4f,' % math.fabs(length*scale)
-    linetypes[linename] = 'stroke-dasharray:' + linetype
+        if length == 0:                             # test for dot
+            linetype += ' 0.5,'
+        else:
+            linetype += '%.4f,' % math.fabs(length*scale)
+    if linetype == '':
+        linetypes[linename] = 'stroke-linecap: round'
+    else:
+        linetypes[linename] = 'stroke-dasharray:' + linetype
 
 entity = ''
 block = defs                                        # initiallize with dummy
@@ -425,11 +452,12 @@ while line[0] and line[1] != 'DICTIONARY':
             if vals[groups['6']]:                   # Common Linetype
                 if linetypes.has_key(vals[groups['6']][0]):
                     style += ';' + linetypes[vals[groups['6']][0]]
-            entities[entity]()
+            if entities[entity]:
+                entities[entity]()
         entity = line[1]
         vals = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
         seqs = []
 
 doc.write(inkex.sys.stdout)
 
-# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99