Fix wire disappearing when wire tip and connector are not exactly at the same position fixed.
authorlaurent
Thu, 21 Apr 2011 16:06:33 +0200
changeset 537 a31bf722aa82
parent 536 288324dddfb8
child 538 8c7605bbf673
Fix wire disappearing when wire tip and connector are not exactly at the same position fixed.
PLCOpenEditor.py
graphics/FBD_Objects.py
graphics/GraphicCommons.py
graphics/LD_Objects.py
graphics/SFC_Objects.py
--- a/PLCOpenEditor.py	Thu Apr 21 15:15:57 2011 +0200
+++ b/PLCOpenEditor.py	Thu Apr 21 16:06:33 2011 +0200
@@ -83,7 +83,7 @@
 # Get folder containing translation files
 localedir = os.path.join(CWD,"locale")
 # Get the default language
-langid = wx.LANGUAGE_SPANISH#wx.LANGUAGE_DEFAULT
+langid = wx.LANGUAGE_DEFAULT
 # Define translation domain (name of translation files)
 domain = "PLCOpenEditor"
 
--- a/graphics/FBD_Objects.py	Thu Apr 21 15:15:57 2011 +0200
+++ b/graphics/FBD_Objects.py	Thu Apr 21 16:06:33 2011 +0200
@@ -167,18 +167,8 @@
             for output in self.Outputs:
                 if name == output.GetName():
                     return output
-        # Test each input connector
-        for input in self.Inputs:
-            input_pos = input.GetRelPosition()
-            if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
-                return input
-        # Test each output connector
-        for output in self.Outputs:
-            output_pos = output.GetRelPosition()
-            if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
-                return output
-        return None
-    
+        return self.FindNearestConnector(position, self.Inputs + self.Outputs)
+        
     def GetInputTypes(self):
         return tuple([input.GetType(True) for input in self.Inputs if input.GetName() != "EN"])
     
@@ -565,17 +555,14 @@
                 return self.Input
             if self.Output and name == self.Output.GetName():
                 return self.Output
+        connectors = []
         # Test input connector if it exists
         if self.Input:
-            input_pos = self.Input.GetRelPosition()
-            if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
-                return self.Input
+            connectors.append(self.Input)
         # Test output connector if it exists
         if self.Output:
-            output_pos = self.Output.GetRelPosition()
-            if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
-                return self.Output
-        return None
+            connectors.append(self.Output)
+        return self.FindNearestConnector(position, connectors)
     
     # Returns all the block connectors 
     def GetConnectors(self):
--- a/graphics/GraphicCommons.py	Thu Apr 21 15:15:57 2011 +0200
+++ b/graphics/GraphicCommons.py	Thu Apr 21 16:06:33 2011 +0200
@@ -570,6 +570,18 @@
     def GetConnectorTranslation(self, element):
         return {}
     
+    def FindNearestConnector(self, position, connectors):
+        distances = []
+        for connector in connectors:
+            connector_pos = connector.GetRelPosition()
+            distances.append((sqrt((self.Pos.x + connector_pos.x - position.x) ** 2 +
+                                   (self.Pos.y + connector_pos.y - position.y) ** 2),
+                              connector))
+        distances.sort()
+        if len(distances) > 0:
+            return distances[0][1]
+        return None
+        
     def IsOfType(self, type, reference):
         return self.Parent.IsOfType(type, reference)
     
--- a/graphics/LD_Objects.py	Thu Apr 21 15:15:57 2011 +0200
+++ b/graphics/LD_Objects.py	Thu Apr 21 16:06:33 2011 +0200
@@ -237,12 +237,7 @@
             for connector in self.Connectors:
                 if connector and name == connector.GetName():
                     return connector
-        for connector in self.Connectors:
-            if connector:
-                connector_pos = connector.GetRelPosition()
-                if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y:
-                    return connector
-        return None
+        return self.FindNearestConnector(position, [connector for connector in self.Connectors if connector is not None])
     
     # Returns all the power rail connectors 
     def GetConnectors(self):
@@ -552,15 +547,7 @@
                 return self.Input
             if name == self.Output.GetName():
                 return self.Output
-        # Test input connector
-        input_pos = self.Input.GetRelPosition()
-        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
-            return self.Input
-        # Test output connector
-        output_pos = self.Output.GetRelPosition()
-        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
-            return self.Output
-        return None
+        return self.FindNearestConnector(position, [self.Input, self.Output])
     
     # Returns input and output contact connectors 
     def GetConnectors(self):
@@ -843,15 +830,7 @@
                 return self.Input
             if self.Output and name == self.Output.GetName():
                 return self.Output
-        # Test input connector
-        input_pos = self.Input.GetRelPosition()
-        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
-            return self.Input
-        # Test output connector
-        output_pos = self.Output.GetRelPosition()
-        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
-            return self.Output
-        return None
+        return self.FindNearestConnector(position, [self.Input, self.Output])
     
     # Returns input and output coil connectors 
     def GetConnectors(self):
--- a/graphics/SFC_Objects.py	Thu Apr 21 15:15:57 2011 +0200
+++ b/graphics/SFC_Objects.py	Thu Apr 21 16:06:33 2011 +0200
@@ -261,22 +261,17 @@
                 return self.Output
             if self.Action and name == self.Action.GetName():
                 return self.Action
+        connectors = []
         # Test input connector if it exists
         if self.Input:
-            input_pos = self.Input.GetRelPosition()
-            if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
-                return self.Input
+            connectors.append(self.Input)
         # Test output connector if it exists
         if self.Output:
-            output_pos = self.Output.GetRelPosition()
-            if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
-                return self.Output
+            connectors.append(self.Output)
         # Test action connector if it exists
         if self.Action:
-            action_pos = self.Action.GetRelPosition()
-            if position.x == self.Pos.x + action_pos.x and position.y == self.Pos.y + action_pos.y:
-                return self.Action
-        return None
+            connectors.append(self.Action)
+        return self.FindNearestConnector(position, connectors)
     
     # Returns action step connector 
     def GetActionConnector(self):
@@ -759,20 +754,10 @@
                 return self.Output
             if self.Type == "connection" and name == self.Condition.GetName():
                 return self.Condition
-        # Test input connector
-        input_pos = self.Input.GetRelPosition()
-        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
-            return self.Input
-        # Test output connector
-        output_pos = self.Output.GetRelPosition()
-        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
-            return self.Output
+        connectors = [self.Input, self.Output]
         if self.Type == "connection":
-            # Test condition connector
-            condition_pos = self.Condition.GetRelPosition()
-            if position.x == self.Pos.x + condition_pos.x and position.y == self.Pos.y + condition_pos.y:
-                return self.Condition
-        return None
+            connectors.append(self.Connection)
+        return self.FindNearestConnector(position, connectors)
     
     # Returns the transition condition connector
     def GetConditionConnector(self):
@@ -1228,17 +1213,7 @@
             for output in self.Outputs:
                 if name == output.GetName():
                     return output
-        # Test input connector
-        for input in self.Inputs:
-            input_pos = input.GetPosition(False)
-            if position.x == input_pos.x and position.y == input_pos.y:
-                return input
-        # Test output connector
-        for output in self.Outputs:
-            output_pos = output.GetPosition(False)
-            if position.x == output_pos.x and position.y == output_pos.y:
-                return output
-        return None
+        return self.FindNearestConnector(position, self.Inputs + self.Outputs)
     
     # Returns input and output divergence connectors 
     def GetConnectors(self):