chiark / gitweb /
Syncing drawing loaders from NinjaKittens.
authordaid <daid303@gmail.com>
Thu, 2 Jan 2014 15:11:33 +0000 (16:11 +0100)
committerdaid <daid303@gmail.com>
Thu, 2 Jan 2014 15:11:33 +0000 (16:11 +0100)
Cura/util/drawingLoader/drawing.py
Cura/util/drawingLoader/dxf.py

index c8871a285119f30d837dbff1e22c31a074140c00..636559a72566c94995c63f5b7581dcc5af1a2af7 100644 (file)
@@ -191,6 +191,21 @@ class Drawing(object):
                return p
 
        def _postProcessPaths(self):
+               for path1 in self.paths:
+                       if not path1.isClosed() and len(path1._nodes) > 0:
+                               for path2 in self.paths:
+                                       if path1 != path2 and not path2.isClosed():
+                                               if abs(path1._nodes[-1].position - path2._startPoint) < 0.001:
+                                                       path1._nodes += path2._nodes
+                                                       path2._nodes = []
+
+               cleanList = []
+               for path in self.paths:
+                       if len(path._nodes) < 1:
+                               cleanList.append(path)
+               for path in cleanList:
+                       self.paths.remove(path)
+
                for path in self.paths:
                        if not path.isClosed():
                                if abs(path._nodes[-1].position - path._startPoint) < 0.001:
index 5f9fe0b7a59505f864b075dc935d9e5ec5f965af..eaa6e87b43d8268605957f86f867765e85d04c33 100644 (file)
@@ -15,8 +15,8 @@ class DXF(drawing.Drawing):
                self._lastLine = None
                self._polyLine = None
 
-               entityType = 'UNKNOWN'
-               sectionName = 'UNKNOWN'
+               entityType = 'NONE'
+               sectionName = 'NONE'
                activeObject = None
                f = open(filename, "r")
                while True:
@@ -26,15 +26,22 @@ class DXF(drawing.Drawing):
                        groupCode = int(groupCode)
                        value = f.readline().strip()
                        if groupCode == 0:
-                               if sectionName == 'ENTITIES':
+                               if entityType == 'SECTION':
+                                       sectionName = activeObject[2][0]
+                               elif entityType == 'ENDSEC':
+                                       sectionName = 'NONE'
+                               elif sectionName == 'ENTITIES':
                                        self._checkForNewPath(entityType, activeObject)
+                               elif activeObject is not None:
+                                       pass
+                                       #print sectionName, entityType, activeObject
                                entityType = value
                                activeObject = {}
-                       elif entityType == 'SECTION':
-                               if groupCode == 2:
-                                       sectionName = value
                        else:
-                               activeObject[groupCode] = value
+                               if groupCode in activeObject:
+                                       activeObject[groupCode].append(value)
+                               else:
+                                       activeObject[groupCode] = [value]
                if sectionName == 'ENTITIES':
                        self._checkForNewPath(entityType, activeObject)
                f.close()
@@ -43,22 +50,34 @@ class DXF(drawing.Drawing):
 
        def _checkForNewPath(self, type, obj):
                if type == 'LINE':
-                       if self._lastLine is not None and self._lastLinePoint == complex(float(obj[10]), float(obj[20])):
-                               self._lastLine.addLineTo(float(obj[11]), float(obj[21]))
+                       if self._lastLine is not None and self._lastLinePoint == complex(float(obj[10][0]), float(obj[20][0])):
+                               self._lastLine.addLineTo(float(obj[11][0]), float(obj[21][0]))
                        else:
-                               p = self.addPath(float(obj[10]), float(obj[20]))
-                               p.addLineTo(float(obj[11]), float(obj[21]))
+                               p = self.addPath(float(obj[10][0]), float(obj[20][0]))
+                               p.addLineTo(float(obj[11][0]), float(obj[21][0]))
                                self._lastLine = p
-                       self._lastLinePoint = complex(float(obj[11]), float(obj[21]))
+                       self._lastLinePoint = complex(float(obj[11][0]), float(obj[21][0]))
                elif type == 'POLYLINE':
                        self._polyLine = None
                elif type == 'VERTEX':
                        if self._polyLine is None:
-                               self._polyLine = self.addPath(float(obj[10]), float(obj[20]))
+                               self._polyLine = self.addPath(float(obj[10][0]), float(obj[20][0]))
                        else:
-                               self._polyLine.addLineTo(float(obj[10]), float(obj[20]))
+                               self._polyLine.addLineTo(float(obj[10][0]), float(obj[20][0]))
+               elif type == 'LWPOLYLINE':
+                       p = self.addPath(float(obj[10][0]), float(obj[20][0]))
+                       for n in xrange(1, len(obj[10])):
+                               p.addLineTo(float(obj[10][n]), float(obj[20][n]))
+                       self._lastLine = p
+               elif type == 'SPLINE':
+                       p = self.addPath(float(obj[10][0]), float(obj[20][0]))
+                       for n in xrange(1, len(obj[10])):
+                               p.addLineTo(float(obj[10][n]), float(obj[20][n]))
+                       self._lastLine = p
                else:
-                       print type
+                       print 'type=%s' % type
+                       for k in obj.keys():
+                               print k, obj[k]
 
 if __name__ == '__main__':
        for n in xrange(1, len(sys.argv)):