graphics/GraphicCommons.py
changeset 283 c4199b88cf60
parent 269 34eff05909b0
child 287 fab9a51d5b57
--- a/graphics/GraphicCommons.py	Mon Oct 13 16:07:52 2008 +0200
+++ b/graphics/GraphicCommons.py	Fri Oct 17 16:22:15 2008 +0200
@@ -323,6 +323,9 @@
     def SpreadCurrent(self):
         pass
     
+    def GetConnectorTranslation(self, element):
+        return {}
+    
     def IsOfType(self, type, reference):
         return self.Parent.IsOfType(type, reference)
     
@@ -674,6 +677,51 @@
     def __del__(self):
         self.Elements = []
     
+    # Make a clone of this element
+    def Clone(self, parent, pos = None):
+        group = Graphic_Group(parent)
+        connectors = {}
+        wires = []
+        if pos is not None:
+            dx, dy = pos.x - self.BoundingBox.x, pos.y - self.BoundingBox.y
+        for element in self.Elements:
+            if isinstance(element, Wire):
+                wires.append(element)
+            else:
+                if pos is not None:
+                    x, y = element.GetPosition()
+                    new_pos = wx.Point(x + dx, y + dy)
+                    newid = parent.GetNewId()
+                    if parent.IsNamedElement(element):
+                        name = parent.GenerateNewName(element)
+                        new_element = element.Clone(parent, newid, name, pos = new_pos)
+                    else:
+                        new_element = element.Clone(parent, newid, pos = new_pos)
+                else:
+                    new_element = element.Clone(parent)
+                connectors.update(element.GetConnectorTranslation(new_element))
+                group.SelectElement(new_element)
+        for element in wires:
+            if pos is not None:
+                new_wire = element.Clone(parent, connectors, dx, dy)
+            else:
+                new_wire = element.Clone(parent, connectors)
+            if new_wire is not None:
+                parent.AddWire(new_wire)
+                group.SelectElement(new_wire)
+        if pos is not None:
+            for element in group.Elements:
+                if not isinstance(element, Wire):
+                    parent.AddBlockInModel(element)
+        return group
+    
+    def CanAddBlocks(self, parent):
+        valid = True
+        for element in self.Elements:
+            if not isinstance(element, Wire):
+                valid &= parent.CanAddElement(element)
+        return valid
+    
     def IsVisible(self):
         for element in self.Elements:
             if element.IsVisible():
@@ -690,16 +738,6 @@
                 if startblock in self.Elements and endblock in self.Elements:
                     self.WireExcluded.append(element)
     
-    # Make a clone of this group
-    def Clone(self, parent):
-        clone = Graphic_Group(parent)
-        elements = []
-        # Makes a clone of all the elements in this group
-        for element in self.Elements:
-            elements.append(element.Clone(parent))
-        clone.SetElements(elements)
-        return clone
-    
     # Returns the RedrawRect
     def GetRedrawRect(self, movex = 0, movey = 0):
         rect = None
@@ -1309,6 +1347,19 @@
                 rect = rect.Union(wx.Rect(x, y, width, height))
         return rect
     
+    def Clone(self, parent, connectors = {}, dx = 0, dy = 0):
+        start_connector = connectors.get(self.StartConnected, None)
+        end_connector = connectors.get(self.EndConnected, None)
+        if start_connector is not None and end_connector is not None:
+            wire = Wire(parent)
+            wire.SetPoints([(point.x + dx, point.y + dy) for point in self.Points])
+            start_connector.Connect((wire, 0), False)
+            end_connector.Connect((wire, -1), False)
+            wire.ConnectStartPoint(start_connector.GetPosition(), start_connector)
+            wire.ConnectEndPoint(end_connector.GetPosition(), end_connector)
+            return wire
+        return None
+    
     # Forbids to change the wire position
     def SetPosition(x, y):
         pass