graphics/FBD_Objects.py
changeset 2437 105c20fdeb19
parent 2432 dbc065a2f7a5
child 3303 0ffb41625592
equal deleted inserted replaced
2436:82bfc75bcd9d 2437:105c20fdeb19
    22 # along with this program; if not, write to the Free Software
    22 # along with this program; if not, write to the Free Software
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    24 
    24 
    25 
    25 
    26 from __future__ import absolute_import
    26 from __future__ import absolute_import
       
    27 from __future__ import division
    27 import wx
    28 import wx
    28 from six.moves import xrange
    29 from six.moves import xrange
    29 
    30 
    30 from graphics.GraphicCommons import *
    31 from graphics.GraphicCommons import *
    31 from plcopen.structures import *
    32 from plcopen.structures import *
   129 
   130 
   130     # Returns the bounding box of the name outside the block
   131     # Returns the bounding box of the name outside the block
   131     def GetTextBoundingBox(self):
   132     def GetTextBoundingBox(self):
   132         # Calculate the size of the name outside the block
   133         # Calculate the size of the name outside the block
   133         text_width, text_height = self.NameSize
   134         text_width, text_height = self.NameSize
   134         return wx.Rect(self.Pos.x + (self.Size[0] - text_width) / 2,
   135         return wx.Rect(self.Pos.x + (self.Size[0] - text_width) // 2,
   135                        self.Pos.y - (text_height + 2),
   136                        self.Pos.y - (text_height + 2),
   136                        text_width,
   137                        text_width,
   137                        text_height)
   138                        text_height)
   138 
   139 
   139     # Returns the bounding box of function block without name outside
   140     # Returns the bounding box of function block without name outside
   159     def RefreshConnectors(self):
   160     def RefreshConnectors(self):
   160         scaling = self.Parent.GetScaling()
   161         scaling = self.Parent.GetScaling()
   161         # Calculate the size for the connector lines
   162         # Calculate the size for the connector lines
   162         lines = max(len(self.Inputs), len(self.Outputs))
   163         lines = max(len(self.Inputs), len(self.Outputs))
   163         if lines > 0:
   164         if lines > 0:
   164             linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE)
   165             linesize = max((self.Size[1] - BLOCK_LINE_SIZE) // lines, BLOCK_LINE_SIZE)
   165             # Update inputs and outputs positions
   166             # Update inputs and outputs positions
   166             position = BLOCK_LINE_SIZE + linesize / 2
   167             position = BLOCK_LINE_SIZE + linesize // 2
   167             for i in xrange(lines):
   168             for i in xrange(lines):
   168                 if scaling is not None:
   169                 if scaling is not None:
   169                     ypos = round_scaling(self.Pos.y + position, scaling[1]) - self.Pos.y
   170                     ypos = round_scaling(self.Pos.y + position, scaling[1]) - self.Pos.y
   170                 else:
   171                 else:
   171                     ypos = position
   172                     ypos = position
   477             executionorder_size = self.ExecutionOrderSize
   478             executionorder_size = self.ExecutionOrderSize
   478 
   479 
   479         # Draw a rectangle with the block size
   480         # Draw a rectangle with the block size
   480         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   481         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   481         # Draw block name and block type
   482         # Draw block name and block type
   482         name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
   483         name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
   483                     self.Pos.y - (name_size[1] + 2))
   484                     self.Pos.y - (name_size[1] + 2))
   484         type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2,
   485         type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) // 2,
   485                     self.Pos.y + 5)
   486                     self.Pos.y + 5)
   486         dc.DrawText(self.Name, name_pos[0], name_pos[1])
   487         dc.DrawText(self.Name, name_pos[0], name_pos[1])
   487         dc.DrawText(self.Type, type_pos[0], type_pos[1])
   488         dc.DrawText(self.Type, type_pos[0], type_pos[1])
   488         # Draw inputs and outputs connectors
   489         # Draw inputs and outputs connectors
   489         for input in self.Inputs:
   490         for input in self.Inputs:
   590             bbx_x = self.Pos.x
   591             bbx_x = self.Pos.x
   591         if self.Type == INOUT:
   592         if self.Type == INOUT:
   592             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
   593             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
   593         else:
   594         else:
   594             bbx_width = self.Size[0] + CONNECTOR_SIZE
   595             bbx_width = self.Size[0] + CONNECTOR_SIZE
   595         bbx_x = min(bbx_x, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2)
   596         bbx_x = min(bbx_x, self.Pos.x + (self.Size[0] - self.NameSize[0]) // 2)
   596         bbx_width = max(bbx_width, self.NameSize[0])
   597         bbx_width = max(bbx_width, self.NameSize[0])
   597         bbx_height = self.Size[1]
   598         bbx_height = self.Size[1]
   598         if self.ExecutionOrder != 0:
   599         if self.ExecutionOrder != 0:
   599             bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
   600             bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
   600             bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
   601             bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
   603 
   604 
   604     # Refresh the position of the variable connector
   605     # Refresh the position of the variable connector
   605     def RefreshConnectors(self):
   606     def RefreshConnectors(self):
   606         scaling = self.Parent.GetScaling()
   607         scaling = self.Parent.GetScaling()
   607         if scaling is not None:
   608         if scaling is not None:
   608             position = round_scaling(self.Pos.y + self.Size[1] / 2, scaling[1]) - self.Pos.y
   609             position = round_scaling(self.Pos.y + self.Size[1] // 2, scaling[1]) - self.Pos.y
   609         else:
   610         else:
   610             position = self.Size[1] / 2
   611             position = self.Size[1] // 2
   611         if self.Input:
   612         if self.Input:
   612             self.Input.SetPosition(wx.Point(0, position))
   613             self.Input.SetPosition(wx.Point(0, position))
   613         if self.Output:
   614         if self.Output:
   614             self.Output.SetPosition(wx.Point(self.Size[0], position))
   615             self.Output.SetPosition(wx.Point(self.Size[0], position))
   615         self.RefreshConnected()
   616         self.RefreshConnected()
   776             executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder))
   777             executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder))
   777         else:
   778         else:
   778             name_size = self.NameSize
   779             name_size = self.NameSize
   779             executionorder_size = self.ExecutionOrderSize
   780             executionorder_size = self.ExecutionOrderSize
   780 
   781 
   781         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
   782         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
   782                     self.Pos.y + (self.Size[1] - name_size[1]) / 2)
   783                     self.Pos.y + (self.Size[1] - name_size[1]) // 2)
   783         # Draw a rectangle with the variable size
   784         # Draw a rectangle with the variable size
   784         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   785         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   785         # Draw variable name
   786         # Draw variable name
   786         dc.DrawText(self.Name, text_pos[0], text_pos[1])
   787         dc.DrawText(self.Name, text_pos[0], text_pos[1])
   787         # Draw connectors
   788         # Draw connectors
   875 
   876 
   876     # Refresh the position of the connection connector
   877     # Refresh the position of the connection connector
   877     def RefreshConnectors(self):
   878     def RefreshConnectors(self):
   878         scaling = self.Parent.GetScaling()
   879         scaling = self.Parent.GetScaling()
   879         if scaling is not None:
   880         if scaling is not None:
   880             position = round_scaling(self.Pos.y + self.Size[1] / 2, scaling[1]) - self.Pos.y
   881             position = round_scaling(self.Pos.y + self.Size[1] // 2, scaling[1]) - self.Pos.y
   881         else:
   882         else:
   882             position = self.Size[1] / 2
   883             position = self.Size[1] // 2
   883         if self.Type == CONNECTOR:
   884         if self.Type == CONNECTOR:
   884             self.Connector.SetPosition(wx.Point(0, position))
   885             self.Connector.SetPosition(wx.Point(0, position))
   885         else:
   886         else:
   886             self.Connector.SetPosition(wx.Point(self.Size[0], position))
   887             self.Connector.SetPosition(wx.Point(self.Size[0], position))
   887         self.RefreshConnected()
   888         self.RefreshConnected()
  1016         else:
  1017         else:
  1017             name_size = self.NameSize
  1018             name_size = self.NameSize
  1018 
  1019 
  1019         # Draw a rectangle with the connection size with arrows inside
  1020         # Draw a rectangle with the connection size with arrows inside
  1020         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
  1021         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
  1021         arrowsize = min(self.Size[1] / 2, (self.Size[0] - name_size[0] - 10) / 2)
  1022         arrowsize = min(self.Size[1] // 2, (self.Size[0] - name_size[0] - 10) // 2)
  1022         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize,
  1023         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize,
  1023                     self.Pos.y + self.Size[1] / 2)
  1024                     self.Pos.y + self.Size[1] // 2)
  1024         dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2,
  1025         dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] // 2,
  1025                     self.Pos.x, self.Pos.y + self.Size[1])
  1026                     self.Pos.x, self.Pos.y + self.Size[1])
  1026         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y,
  1027         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y,
  1027                     self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
  1028                     self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] // 2)
  1028         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2,
  1029         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] // 2,
  1029                     self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
  1030                     self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
  1030         # Draw connection name
  1031         # Draw connection name
  1031         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
  1032         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
  1032                     self.Pos.y + (self.Size[1] - name_size[1]) / 2)
  1033                     self.Pos.y + (self.Size[1] - name_size[1]) // 2)
  1033         dc.DrawText(self.Name, text_pos[0], text_pos[1])
  1034         dc.DrawText(self.Name, text_pos[0], text_pos[1])
  1034         # Draw connector
  1035         # Draw connector
  1035         if self.Connector:
  1036         if self.Connector:
  1036             self.Connector.Draw(dc)
  1037             self.Connector.Draw(dc)
  1037 
  1038