thiagoralves / OpenPLC_Editor

OpenPLC Editor - IDE capable of creating programs for the OpenPLC Runtime
GNU General Public License v2.0
387 stars 191 forks source link

Optimize the algorithm of finding nearest connector and fix a bug that occurs when add a connector #29

Closed zarekvane closed 2 years ago

zarekvane commented 2 years ago

Optimize the algorithm of finding nearest connector

I find there is an efficiency problem in function at file GraphicCommons.py. When there are too many connectors, sorting them is a waste of time.

    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

fix a bug

when add a new connector there is an error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/wx-3.0-gtk2/wx/_core.py", line 16765, in <lambda>
    lambda event: event.callable(*event.args, **event.kw) )
  File "/data/OpenPLC_Editor/editor/editors/Viewer.py", line 2672, in AddNewConnection
    if dialog.ShowModal() == wx.ID_OK
  File "/data/OpenPLC_Editor/editor/dialogs/ConnectionDialog.py", line 151, in GetValues
    values["width"], values["height"] = self.Element.GetSize()
AttributeError: 'NoneType' object has no attribute 'GetSize'