Reverted PLCOpenEditor to DRIVEN mode.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard.
#
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from wxPython.wx import *
import wx
from GraphicCommons import *
from plcopen.structures import *
def GetWireSize(block):
if isinstance(block, SFC_Step):
return SFC_WIRE_MIN_SIZE + block.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1]
else:
return SFC_WIRE_MIN_SIZE
#-------------------------------------------------------------------------------
# Sequencial Function Chart Step
#-------------------------------------------------------------------------------
"""
Class that implements the graphic representation of a step
"""
class SFC_Step(Graphic_Element):
# Create a new step
def __init__(self, parent, name, initial = False, id = None):
Graphic_Element.__init__(self, parent)
self.Name = name
self.Initial = initial
self.Id = id
self.Size = wxSize(SFC_STEP_DEFAULT_SIZE[0], SFC_STEP_DEFAULT_SIZE[1])
# Create an input and output connector
if not self.Initial:
self.Input = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, 0), NORTH)
else:
self.Input = None
self.Output = None
self.Action = None
# Destructor
def __del__(self):
self.Input = None
self.Output = None
self.Action = None
# Delete this step by calling the appropriate method
def Delete(self):
self.Parent.DeleteStep(self)
# Unconnect input and output
def Clean(self):
if self.Input:
self.Input.UnConnect()
if self.Output:
self.Output.UnConnect()
if self.Action:
self.Action.UnConnect()
# Add output connector to step
def AddOutput(self):
if not self.Output:
self.Output = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, self.Size[1]), SOUTH)
self.RefreshBoundingBox()
# Remove output connector from step
def RemoveOutput(self):
if self.Output:
self.Output.UnConnect()
self.Output = None
self.RefreshBoundingBox()
# Add action connector to step
def AddAction(self):
if not self.Action:
self.Action = Connector(self, "", "ANY", wxPoint(self.Size[0], self.Size[1] / 2), EAST)
self.RefreshBoundingBox()
# Remove action connector from step
def RemoveAction(self):
if self.Action:
self.Action.UnConnect()
self.Action = None
self.RefreshBoundingBox()
# Refresh the step bounding box
def RefreshBoundingBox(self):
dc = wxClientDC(self.Parent)
# Calculate the bounding box size
if self.Action:
bbx_width = self.Size[0] + CONNECTOR_SIZE
else:
bbx_width = self.Size[0]
if self.Initial:
bbx_y = self.Pos.y
bbx_height = self.Size[1]
if self.Output:
bbx_height += CONNECTOR_SIZE
else:
bbx_y = self.Pos.y - CONNECTOR_SIZE
bbx_height = self.Size[1] + CONNECTOR_SIZE
if self.Output:
bbx_height += CONNECTOR_SIZE
#self.BoundingBox = wxRect(self.Pos.x, bbx_y, bbx_width + 1, bbx_height + 1)
self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
# Refresh the positions of the step connectors
def RefreshConnectors(self):
# Update input position if it exists
if self.Input:
self.Input.SetPosition(wxPoint(self.Size[0] / 2, 0))
# Update output position
if self.Output:
self.Output.SetPosition(wxPoint(self.Size[0] / 2, self.Size[1]))
# Update action position if it exists
if self.Action:
self.Action.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2))
self.RefreshConnected()
# Refresh the position of wires connected to step
def RefreshConnected(self, exclude = []):
if self.Input:
self.Input.MoveConnected(exclude)
if self.Output:
self.Output.MoveConnected(exclude)
if self.Action:
self.Action.MoveConnected(exclude)
# Returns the step connector that starts with the point given if it exists
def GetConnector(self, position, name = None):
# if a name is given
if name:
# Test input, output and action connector if they exists
if self.Input and name == self.Input.GetName():
return self.Input
if self.Output and name == self.Output.GetName():
return self.Output
if self.Action and name == self.Action.GetName():
return self.Action
# 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
# 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
# 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
# Returns input and output step connectors
def GetConnectors(self):
return {"input":self.Input,"output":self.Output,"action":self.Action}
# Test if point given is on step input or output connector
def TestConnector(self, pt, exclude=True):
# Test input connector if it exists
if self.Input and self.Input.TestPoint(pt, exclude):
return self.Input
# Test output connector
if self.Output and self.Output.TestPoint(pt, exclude):
return self.Output
return None
# Changes the step name
def SetName(self, name):
self.Name = name
# Returns the step name
def GetName(self):
return self.Name
# Returns the step initial property
def GetInitial(self):
return self.Initial
# Returns the connector connected to input
def GetPreviousConnector(self):
if self.Input:
wires = self.Input.GetWires()
if len(wires) == 1:
return wires[0][0].EndConnected
return None
# Returns the connector connected to output
def GetNextConnector(self):
if self.Output:
wires = self.Output.GetWires()
if len(wires) == 1:
return wires[0][0].StartConnected
return None
# Returns the connector connected to action
def GetActionConnector(self):
if self.Action:
wires = self.Action.GetWires()
if len(wires) == 1:
return wires[0][0].StartConnected
return None
# Returns the number of action line
def GetActionExtraLineNumber(self):
if self.Action:
wires = self.Action.GetWires()
if len(wires) != 1:
return 0
action_block = wires[0][0].StartConnected.GetParentBlock()
return max(0, action_block.GetLineNumber() - 1)
return 0
# Returns the step minimum size
def GetMinSize(self):
dc = wxClientDC(self.Parent)
text_width, text_height = dc.GetTextExtent(self.Name)
if self.Initial:
return text_width + 14, text_height + 14
else:
return text_width + 10, text_height + 10
# Updates the step size
def UpdateSize(self, width, height):
diffx = self.Size.GetWidth() / 2 - width / 2
diffy = height - self.Size.GetHeight()
self.Move(diffx, 0)
Graphic_Element.SetSize(self, width, height)
self.RefreshOutputPosition((0, diffy))
# Align input element with this step
def RefreshInputPosition(self):
if self.Input:
current_pos = self.Input.GetPosition(False)
input = self.GetPreviousConnector()
if input:
input_pos = input.GetPosition(False)
diffx = current_pos.x - input_pos.x
input_block = input.GetParentBlock()
if isinstance(input_block, SFC_Divergence):
input_block.MoveConnector(input, diffx)
else:
if isinstance(input_block, SFC_Step):
input_block.MoveActionBlock((diffx, 0))
input_block.Move(diffx, 0)
input_block.RefreshInputPosition()
# Align output element with this step
def RefreshOutputPosition(self, move = None):
if self.Output:
wires = self.Output.GetWires()
if len(wires) != 1:
return
current_pos = self.Output.GetPosition(False)
output = wires[0][0].StartConnected
output_pos = output.GetPosition(False)
diffx = current_pos.x - output_pos.x
output_block = output.GetParentBlock()
wire_size = SFC_WIRE_MIN_SIZE + self.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1]
diffy = wire_size - output_pos.y + current_pos.y
if diffy != 0:
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock((diffx, diffy))
wires[0][0].SetPoints([wxPoint(current_pos.x, current_pos.y + wire_size),
wxPoint(current_pos.x, current_pos.y)])
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
output_block.Move(diffx, diffy, self.Parent.Wires)
output_block.RefreshOutputPosition((diffx, diffy))
else:
output_block.RefreshPosition()
elif move:
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock(move)
wires[0][0].Move(move[0], move[1], True)
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
output_block.Move(move[0], move[1], self.Parent.Wires)
output_block.RefreshOutputPosition(move)
else:
output_block.RefreshPosition()
elif isinstance(output_block, SFC_Divergence):
output_block.MoveConnector(output, diffx)
else:
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock((diffx, 0))
output_block.Move(diffx, 0)
output_block.RefreshOutputPosition()
# Refresh action element with this step
def MoveActionBlock(self, move):
if self.Action:
wires = self.Action.GetWires()
if len(wires) != 1:
return
action_block = wires[0][0].StartConnected.GetParentBlock()
action_block.Move(move[0], move[1], self.Parent.Wires)
wires[0][0].Move(move[0], move[1], True)
# Resize the divergence from position and size given
def Resize(self, x, y, width, height):
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
self.UpdateSize(width, height)
else:
Graphic_Element.Resize(self, x, y, width, height)
# Method called when a LeftDClick event have been generated
def OnLeftDClick(self, event, scaling):
# Edit the step properties
self.Parent.EditStepContent(self)
# Method called when a RightUp event have been generated
def OnRightUp(self, event, dc, scaling):
# Popup the menu with special items for a step
self.Parent.PopupDefaultMenu()
# Refreshes the step state according to move defined and handle selected
def ProcessDragging(self, movex, movey):
handle_type, handle = self.Handle
if handle_type == HANDLE_MOVE:
action_block = None
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
self.Move(movex, movey)
self.RefreshConnected()
elif self.Initial:
self.MoveActionBlock((movex, movey))
self.Move(movex, movey, self.Parent.Wires)
self.RefreshOutputPosition((movex, movey))
else:
self.MoveActionBlock((movex, 0))
self.Move(movex, 0)
self.RefreshInputPosition()
self.RefreshOutputPosition()
else:
Graphic_Element.ProcessDragging(self, movex, movey)
# Refresh input element model
def RefreshInputModel(self):
if self.Input:
input = self.GetPreviousConnector()
if input:
input_block = input.GetParentBlock()
input_block.RefreshModel(False)
if not isinstance(input_block, SFC_Divergence):
input_block.RefreshInputModel()
# Refresh output element model
def RefreshOutputModel(self, move=False):
if self.Output:
output = self.GetNextConnector()
if output:
output_block = output.GetParentBlock()
output_block.RefreshModel(False)
if not isinstance(output_block, SFC_Divergence) or move:
output_block.RefreshOutputModel(move)
# Refreshes the step model
def RefreshModel(self, move=True):
self.Parent.RefreshStepModel(self)
if self.Action:
action = self.GetActionConnector()
if action:
action_block = action.GetParentBlock()
action_block.RefreshModel(False)
# If step has moved, refresh the model of wires connected to output
if move:
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
self.RefreshInputModel()
self.RefreshOutputModel(self.Initial)
elif self.Output:
self.Output.RefreshWires()
# Draws step
def Draw(self, dc):
dc.SetPen(wxBLACK_PEN)
dc.SetBrush(wxWHITE_BRUSH)
# Draw two rectangles for representing the step
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
if self.Initial:
dc.DrawRectangle(self.Pos.x + 2, self.Pos.y + 2, self.Size[0] - 3, self.Size[1] - 3)
# Draw step name
namewidth, nameheight = dc.GetTextExtent(self.Name)
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
self.Pos.y + (self.Size[1] - nameheight) / 2)
# Draw input and output connectors
if self.Input:
self.Input.Draw(dc)
if self.Output:
self.Output.Draw(dc)
if self.Action:
self.Action.Draw(dc)
Graphic_Element.Draw(self, dc)
#-------------------------------------------------------------------------------
# Sequencial Function Chart Transition
#-------------------------------------------------------------------------------
"""
Class that implements the graphic representation of a transition
"""
class SFC_Transition(Graphic_Element):
# Create a new transition
def __init__(self, parent, type = "reference", condition = "", id = None):
Graphic_Element.__init__(self, parent)
self.Type = type
self.Condition = condition
self.Id = id
self.Size = wxSize(SFC_TRANSITION_SIZE[0], SFC_TRANSITION_SIZE[1])
# Create an input and output connector
self.Input = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, 0), NORTH)
self.Output = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, self.Size[1]), SOUTH)
# Destructor
def __del__(self):
self.Input = None
self.Output = None
# Forbids to change the transition size
def SetSize(self, width, height):
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
Graphic_Element.SetSize(self, width, height)
# Forbids to resize the transition
def Resize(self, x, y, width, height):
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
Graphic_Element.Resize(self, x, y, width, height)
# Delete this transition by calling the appropriate method
def Delete(self):
self.Parent.DeleteTransition(self)
# Unconnect input and output
def Clean(self):
self.Input.UnConnect()
self.Output.UnConnect()
# Refresh the transition bounding box
def RefreshBoundingBox(self):
dc = wxClientDC(self.Parent)
if self.Condition != "":
text_width, text_height = dc.GetTextExtent(self.Condition)
else:
text_width, text_height = dc.GetTextExtent("Transition")
# Calculate the bounding box size
bbx_width = self.Size[0] + 5 + text_width
bbx_y = self.Pos.y - max(0, (text_height - 5 - self.Size[1]) / 2)
bbx_height = max(self.Size[1], text_height)
self.BoundingBox = wxRect(self.Pos.x, bbx_y, bbx_width + 1, bbx_height - 4)
# Returns the connector connected to input
def GetPreviousConnector(self):
wires = self.Input.GetWires()
if len(wires) == 1:
return wires[0][0].EndConnected
return None
# Returns the connector connected to output
def GetNextConnector(self):
wires = self.Output.GetWires()
if len(wires) == 1:
return wires[0][0].StartConnected
return None
# Refresh the positions of the transition connectors
def RefreshConnectors(self):
# Update input position
self.Input.SetPosition(wxPoint(self.Size[0] / 2, 0))
# Update output position
self.Output.SetPosition(wxPoint(self.Size[0] / 2, self.Size[1]))
self.RefreshConnected()
# Refresh the position of the wires connected to transition
def RefreshConnected(self, exclude = []):
self.Input.MoveConnected(exclude)
self.Output.MoveConnected(exclude)
# Returns the transition connector that starts with the point given if it exists
def GetConnector(self, position, name = None):
# if a name is given
if name:
# Test input and output connector
if name == self.Input.GetName():
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
# Returns input and output transition connectors
def GetConnectors(self):
return {"input":self.Input,"output":self.Output}
# Test if point given is on transition input or output connector
def TestConnector(self, pt, exclude=True):
# Test input connector
if self.Input.TestPoint(pt, exclude):
return self.Input
# Test output connector
if self.Output.TestPoint(pt, exclude):
return self.Output
return None
# Changes the transition type
def SetType(self, type):
self.Type = type
# Returns the transition type
def GetType(self):
return self.Type
# Changes the transition condition
def SetCondition(self, condition):
self.Condition = condition
self.RefreshBoundingBox()
# Returns the transition condition
def GetCondition(self):
return self.Condition
# Returns the transition minimum size
def GetMinSize(self):
return SFC_TRANSITION_SIZE
# Align input element with this step
def RefreshInputPosition(self):
wires = self.Input.GetWires()
current_pos = self.Input.GetPosition(False)
input = self.GetPreviousConnector()
if input:
input_pos = input.GetPosition(False)
diffx = current_pos.x - input_pos.x
input_block = input.GetParentBlock()
if isinstance(input_block, SFC_Divergence):
input_block.MoveConnector(input, diffx)
else:
if isinstance(input_block, SFC_Step):
input_block.MoveActionBlock((diffx, 0))
input_block.Move(diffx, 0)
input_block.RefreshInputPosition()
# Align output element with this step
def RefreshOutputPosition(self, move = None):
wires = self.Output.GetWires()
if len(wires) != 1:
return
current_pos = self.Output.GetPosition(False)
output = wires[0][0].StartConnected
output_pos = output.GetPosition(False)
diffx = current_pos.x - output_pos.x
output_block = output.GetParentBlock()
if move:
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock(move)
wires[0][0].Move(move[0], move[1], True)
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
output_block.Move(move[0], move[1], self.Parent.Wires)
output_block.RefreshOutputPosition(move)
else:
output_block.RefreshPosition()
elif isinstance(output_block, SFC_Divergence):
output_block.MoveConnector(output, diffx)
else:
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock((diffx, 0))
output_block.Move(diffx, 0)
output_block.RefreshOutputPosition()
# Method called when a LeftDClick event have been generated
def OnLeftDClick(self, event, dc, scaling):
# Edit the transition properties
self.Parent.EditTransitionContent(self)
# Method called when a RightUp event have been generated
def OnRightUp(self, event, dc, scaling):
# Popup the menu with special items for a step
self.Parent.PopupDefaultMenu()
# Refreshes the transition state according to move defined and handle selected
def ProcessDragging(self, movex, movey):
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
self.Move(movex, 0)
self.RefreshInputPosition()
self.RefreshOutputPosition()
else:
Graphic_Element.ProcessDragging(self, movex, movey)
# Refresh input element model
def RefreshInputModel(self):
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
input = self.GetPreviousConnector()
if input:
input_block = input.GetParentBlock()
input_block.RefreshModel(False)
if not isinstance(input_block, SFC_Divergence):
input_block.RefreshInputModel()
# Refresh output element model
def RefreshOutputModel(self, move=False):
output = self.GetNextConnector()
if output:
output_block = output.GetParentBlock()
output_block.RefreshModel(False)
if not isinstance(output_block, SFC_Divergence) or move:
output_block.RefreshOutputModel(move)
# Refreshes the transition model
def RefreshModel(self, move=True):
self.Parent.RefreshTransitionModel(self)
# If transition has moved, refresh the model of wires connected to output
if move:
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
self.RefreshInputModel()
self.RefreshOutputModel()
else:
self.Output.RefreshWires()
# Draws transition
def Draw(self, dc):
dc.SetPen(wxBLACK_PEN)
dc.SetBrush(wxBLACK_BRUSH)
# Draw plain rectangle for representing the transition
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
# Draw transition condition
if self.Condition != "":
text_width, text_height = dc.GetTextExtent(self.Condition)
condition = self.Condition
else:
text_width, text_height = dc.GetTextExtent("Transition")
condition = "Transition"
dc.DrawText(condition, self.Pos.x + self.Size[0] + 5,
self.Pos.y + (self.Size[1] - text_height) / 2)
# Draw input and output connectors
self.Input.Draw(dc)
self.Output.Draw(dc)
Graphic_Element.Draw(self, dc)
#-------------------------------------------------------------------------------
# Sequencial Function Chart Divergence and Convergence
#-------------------------------------------------------------------------------
"""
Class that implements the graphic representation of a divergence or convergence,
selection or simultaneous
"""
class SFC_Divergence(Graphic_Element):
# Create a new divergence
def __init__(self, parent, type, number = 2, id = None):
Graphic_Element.__init__(self, parent)
self.Type = type
self.Id = id
self.RealConnectors = None
number = max(2, number)
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
self.Size = wxSize((number - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL, 1)
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
self.Size = wxSize((number - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL, 3)
# Create an input and output connector
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
self.Inputs = [Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, 0), NORTH)]
self.Outputs = []
for i in xrange(number):
self.Outputs.append(Connector(self, "", "ANY", wxPoint(i * SFC_DEFAULT_SEQUENCE_INTERVAL, self.Size[1]), SOUTH))
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
self.Inputs = []
for i in xrange(number):
self.Inputs.append(Connector(self, "", "ANY", wxPoint(i * SFC_DEFAULT_SEQUENCE_INTERVAL, 0), NORTH))
self.Outputs = [Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, self.Size[1]), SOUTH)]
# Destructor
def __del__(self):
self.Inputs = []
self.Outputs = []
# Forbids to resize the divergence
def Resize(self, x, y, width, height):
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
Graphic_Element.Resize(self, x, y, width, height)
# Delete this divergence by calling the appropriate method
def Delete(self):
self.Parent.DeleteDivergence(self)
# Returns the divergence type
def GetType(self):
return self.Type
# Unconnect input and output
def Clean(self):
for input in self.Inputs:
input.UnConnect()
for output in self.Outputs:
output.UnConnect()
# Add a branch to the divergence
def AddBranch(self):
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
maxx = 0
for output in self.Outputs:
pos = output.GetRelPosition()
maxx = max(maxx, pos.x)
connector = Connector(self, "", "ANY", wxPoint(maxx + SFC_DEFAULT_SEQUENCE_INTERVAL, self.Size[1]), SOUTH)
self.Outputs.append(connector)
self.MoveConnector(connector, 0)
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
maxx = 0
for input in self.Inputs:
pos = input.GetRelPosition()
maxx = max(maxx, pos.x)
connector = Connector(self, "", "ANY", wxPoint(maxx + SFC_DEFAULT_SEQUENCE_INTERVAL, 0), NORTH)
self.Inputs.append(connector)
self.MoveConnector(connector, SFC_DEFAULT_SEQUENCE_INTERVAL)
# Remove a branch from the divergence
def RemoveBranch(self, connector):
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
if connector in self.Outputs:
self.Outputs.remove(connector)
self.MoveConnector(self.Outputs[0], 0)
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
if connector in self.Inputs:
self.Inputs.remove(connector)
self.MoveConnector(self.Inputs[0], 0)
# Return the number of branches for the divergence
def GetBranchNumber(self):
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
return len(self.Outputs)
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
return len(self.Inputs)
# Returns if the point given is in the bounding box
def HitTest(self, pt):
rect = self.BoundingBox
return rect.InsideXY(pt.x, pt.y) or self.TestConnector(pt, False) != None
# Refresh the divergence bounding box
def RefreshBoundingBox(self):
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
self.BoundingBox = wxRect(self.Pos.x, self.Pos.y - 2,
self.Size[0] + 1, self.Size[1] + 5)
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
self.BoundingBox = wxRect(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y - 2,
self.Size[0] + 2 * SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Size[1] + 5)
# Refresh the position of wires connected to divergence
def RefreshConnected(self, exclude = []):
for input in self.Inputs:
input.MoveConnected(exclude)
for output in self.Outputs:
output.MoveConnected(exclude)
# Moves the divergence connector given
def MoveConnector(self, connector, movex):
position = connector.GetRelPosition()
connector.SetPosition(wxPoint(position.x + movex, position.y))
minx = self.Size[0]
maxx = 0
for input in self.Inputs:
input_pos = input.GetRelPosition()
minx = min(minx, input_pos.x)
maxx = max(maxx, input_pos.x)
for output in self.Outputs:
output_pos = output.GetRelPosition()
minx = min(minx, output_pos.x)
maxx = max(maxx, output_pos.x)
if minx != 0:
for input in self.Inputs:
input_pos = input.GetRelPosition()
input.SetPosition(wxPoint(input_pos.x - minx, input_pos.y))
for output in self.Outputs:
output_pos = output.GetRelPosition()
output.SetPosition(wxPoint(output_pos.x - minx, output_pos.y))
self.Pos.x += minx
self.Size[0] = maxx - minx
connector.MoveConnected()
self.RefreshBoundingBox()
# Returns the divergence connector that starts with the point given if it exists
def GetConnector(self, position, name = None):
# if a name is given
if name:
# Test each input and output connector
for input in self.Inputs:
if name == input.GetName():
return input
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
# Returns input and output divergence connectors
def GetConnectors(self):
return {"inputs":self.Inputs,"outputs":self.Outputs}
# Test if point given is on divergence input or output connector
def TestConnector(self, pt, exclude=True):
# Test input connector
for input in self.Inputs:
if input.TestPoint(pt, exclude):
return input
# Test output connector
for output in self.Outputs:
if output.TestPoint(pt, exclude):
return output
return None
# Changes the divergence size
def SetSize(self, width, height):
for i, input in enumerate(self.Inputs):
position = input.GetRelPosition()
if self.RealConnectors:
input.SetPosition(wxPoint(int(round(self.RealConnectors["Inputs"][i] * width)), 0))
else:
input.SetPosition(wxPoint(int(round(float(position.x)*float(width)/float(self.Size[0]))), 0))
input.MoveConnected()
for i, output in enumerate(self.Outputs):
position = output.GetRelPosition()
if self.RealConnectors:
output.SetPosition(wxPoint(int(round(self.RealConnectors["Outputs"][i] * width)), self.Size[1]))
else:
output.SetPosition(wxPoint(int(round(float(position.x)*float(width)/float(self.Size[0]))), self.Size[1]))
output.MoveConnected()
self.Size = wxSize(width, height)
self.RefreshBoundingBox()
# Returns the divergence minimum size
def GetMinSize(self):
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
return 0, 1
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
return 0, 3
return 0, 0
# Refresh the position of the block connected to connector
def RefreshConnectedPosition(self, connector):
wires = connector.GetWires()
if len(wires) != 1:
return
current_pos = connector.GetPosition(False)
if connector in self.Inputs:
next = wires[0][0].EndConnected
else:
next = wires[0][0].StartConnected
next_pos = next.GetPosition(False)
diffx = current_pos.x - next_pos.x
next_block = next.GetParentBlock()
if isinstance(next_block, SFC_Divergence):
next_block.MoveConnector(next, diffx)
else:
next_block.Move(diffx, 0)
if connector in self.Inputs:
next_block.RefreshInputPosition()
else:
next_block.RefreshOutputPosition()
# Refresh the position of this divergence
def RefreshPosition(self):
y = 0
for input in self.Inputs:
wires = input.GetWires()
if len(wires) != 1:
return
previous = wires[0][0].EndConnected
previous_pos = previous.GetPosition(False)
y = max(y, previous_pos.y + GetWireSize(previous.GetParentBlock()))
diffy = y - self.Pos.y
if diffy != 0:
self.Move(0, diffy, self.Parent.Wires)
self.RefreshOutputPosition((0, diffy))
for input in self.Inputs:
input.MoveConnected()
# Align output element with this divergence
def RefreshOutputPosition(self, move = None):
if move:
for output_connector in self.Outputs:
wires = output_connector.GetWires()
if len(wires) != 1:
return
current_pos = output_connector.GetPosition(False)
output = wires[0][0].StartConnected
output_pos = output.GetPosition(False)
diffx = current_pos.x - output_pos.x
output_block = output.GetParentBlock()
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock(move)
wires[0][0].Move(move[0], move[1], True)
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
output_block.Move(move[0], move[1], self.Parent.Wires)
output_block.RefreshOutputPosition(move)
# Method called when a LeftDown event have been generated
def OnLeftDown(self, event, dc, scaling):
pos = GetScaledEventPosition(event, dc, scaling)
# Test if a connector have been handled
connector = self.TestConnector(pos, False)
if connector:
self.Handle = (HANDLE_CONNECTOR, connector)
self.Parent.SetCursor(wxStockCursor(wxCURSOR_HAND))
self.Selected = False
# Initializes the last position
self.oldPos = GetScaledEventPosition(event, dc, scaling)
else:
self.RealConnectors = {"Inputs":[],"Outputs":[]}
for input in self.Inputs:
position = input.GetRelPosition()
self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0]))
for output in self.Outputs:
position = output.GetRelPosition()
self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0]))
Graphic_Element.OnLeftDown(self, event, dc, scaling)
# Method called when a LeftUp event have been generated
def OnLeftUp(self, event, dc, scaling):
self.RealConnectors = None
handle_type, handle = self.Handle
if handle_type == HANDLE_CONNECTOR:
wires = handle.GetWires()
if len(wires) != 1:
return
if handle in self.Inputs:
block = wires[0][0].EndConnected.GetParentBlock()
else:
block = wires[0][0].StartConnected.GetParentBlock()
block.RefreshModel(False)
if not isinstance(block, SFC_Divergence):
if handle in self.Inputs:
block.RefreshInputModel()
else:
block.RefreshOutputModel()
Graphic_Element.OnLeftUp(self, event, dc, scaling)
# Method called when a RightUp event have been generated
def OnRightUp(self, event, dc, scaling):
pos = GetScaledEventPosition(event, dc, scaling)
# Popup the menu with special items for a block and a connector if one is handled
connector = self.TestConnector(pos, False)
if connector:
self.Handle = (HANDLE_CONNECTOR, connector)
self.Parent.PopupDivergenceMenu(True)
else:
# Popup the divergence menu without delete branch
self.Parent.PopupDivergenceMenu(False)
# Refreshes the divergence state according to move defined and handle selected
def ProcessDragging(self, movex, movey):
handle_type, handle = self.Handle
# A connector has been handled
if handle_type == HANDLE_CONNECTOR:
self.MoveConnector(handle, movex)
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
self.RefreshConnectedPosition(handle)
elif self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
Graphic_Element.ProcessDragging(self, movex, movey)
# Refresh output element model
def RefreshOutputModel(self, move=False):
if move and self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
for output in self.Outputs:
wires = output.GetWires()
if len(wires) != 1:
return
output_block = wires[0][0].StartConnected.GetParentBlock()
output_block.RefreshModel(False)
if not isinstance(output_block, SFC_Divergence) or move:
output_block.RefreshOutputModel(move)
# Refreshes the divergence model
def RefreshModel(self, move=True):
self.Parent.RefreshDivergenceModel(self)
# If divergence has moved, refresh the model of wires connected to outputs
if move:
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
self.RefreshOutputModel()
else:
for output in self.Outputs:
output.RefreshWires()
# Draws divergence
def Draw(self, dc):
dc.SetPen(wxBLACK_PEN)
dc.SetBrush(wxBLACK_BRUSH)
# Draw plain rectangle for representing the divergence
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y,
self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y)
dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y + self.Size[1],
self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y + self.Size[1])
# Draw inputs and outputs connectors
for input in self.Inputs:
input.Draw(dc)
for output in self.Outputs:
output.Draw(dc)
Graphic_Element.Draw(self, dc)
#-------------------------------------------------------------------------------
# Sequencial Function Chart Jump to Step
#-------------------------------------------------------------------------------
"""
Class that implements the graphic representation of a jump to step
"""
class SFC_Jump(Graphic_Element):
# Create a new jump
def __init__(self, parent, target, id = None):
Graphic_Element.__init__(self, parent)
self.Target = target
self.Id = id
self.Size = wxSize(SFC_JUMP_SIZE[0], SFC_JUMP_SIZE[1])
# Create an input and output connector
self.Input = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, 0), NORTH)
# Destructor
def __del__(self):
self.Input = None
# Forbids to change the jump size
def SetSize(self, width, height):
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
Graphic_Element.SetSize(self, width, height)
# Forbids to resize jump
def Resize(self, x, y, width, height):
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
Graphic_Element.Resize(self, x, y, width, height)
# Delete this jump by calling the appropriate method
def Delete(self):
self.Parent.DeleteJump(self)
# Unconnect input
def Clean(self):
self.Input.UnConnect()
# Refresh the jump bounding box
def RefreshBoundingBox(self):
dc = wxClientDC(self.Parent)
text_width, text_height = dc.GetTextExtent(self.Target)
# Calculate the bounding box size
bbx_width = self.Size[0] + 2 + text_width
self.BoundingBox = wxRect(self.Pos.x, self.Pos.y - CONNECTOR_SIZE,
bbx_width + 1, self.Size[1] + CONNECTOR_SIZE + 1)
# Returns the connector connected to input
def GetPreviousConnector(self):
wires = self.Input.GetWires()
if len(wires) == 1:
return wires[0][0].EndConnected
return None
# Refresh the element connectors position
def RefreshConnectors(self):
self.Input.SetPosition(wxPoint(self.Size[0] / 2, 0))
self.RefreshConnected()
# Refresh the position of wires connected to jump
def RefreshConnected(self, exclude = []):
if self.Input:
self.Input.MoveConnected(exclude)
# Returns input jump connector
def GetConnector(self, position = None, name = None):
return self.Input
# Test if point given is on jump input connector
def TestConnector(self, pt, exclude = True):
# Test input connector
if self.Input and self.Input.TestPoint(pt, exclude):
return self.Input
return None
# Changes the jump target
def SetTarget(self, target):
self.Target = target
self.RefreshBoundingBox()
# Returns the jump target
def GetTarget(self):
return self.Target
# Returns the jump minimum size
def GetMinSize(self):
return SFC_JUMP_SIZE
# Align input element with this jump
def RefreshInputPosition(self):
if self.Input:
current_pos = self.Input.GetPosition(False)
input = self.GetPreviousConnector()
if input:
input_pos = input.GetPosition(False)
diffx = current_pos.x - input_pos.x
input_block = input.GetParentBlock()
if isinstance(input_block, SFC_Divergence):
input_block.MoveConnector(input, diffx)
else:
if isinstance(input_block, SFC_Step):
input_block.MoveActionBlock((diffx, 0))
input_block.Move(diffx, 0)
input_block.RefreshInputPosition()
# Can't align output element, because there is no output
def RefreshOutputPosition(self, move = None):
pass
# Method called when a LeftDClick event have been generated
def OnLeftDClick(self, event, dc, scaling):
# Edit the jump properties
self.Parent.EditJumpContent(self)
# Method called when a RightUp event have been generated
def OnRightUp(self, event, dc, scaling):
# Popup the default menu
self.Parent.PopupDefaultMenu()
# Refreshes the jump state according to move defined and handle selected
def ProcessDragging(self, movex, movey):
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
self.Move(movex, 0)
self.RefreshInputPosition()
else:
Graphic_Element.ProcessDragging(self, movex, movey)
# Refresh input element model
def RefreshInputModel(self):
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
input = self.GetPreviousConnector()
if input:
input_block = input.GetParentBlock()
input_block.RefreshModel(False)
if not isinstance(input_block, SFC_Divergence):
input_block.RefreshInputModel()
# Refresh output element model
def RefreshOutputModel(self, move=False):
pass
# Refreshes the jump model
def RefreshModel(self, move=True):
self.Parent.RefreshJumpModel(self)
if move:
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
self.RefreshInputModel()
# Draws divergence
def Draw(self, dc):
dc.SetPen(wxBLACK_PEN)
dc.SetBrush(wxBLACK_BRUSH)
# Draw plain rectangle for representing the divergence
dc.DrawLine(self.Pos.x + self.Size[0] / 2, self.Pos.y, self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])
points = [wxPoint(self.Pos.x, self.Pos.y),
wxPoint(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1] / 3),
wxPoint(self.Pos.x + self.Size[0], self.Pos.y),
wxPoint(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])]
dc.DrawPolygon(points)
text_width, text_height = dc.GetTextExtent(self.Target)
dc.DrawText(self.Target, self.Pos.x + self.Size[0] + 2,
self.Pos.y + (self.Size[1] - text_height) / 2)
# Draw input connector
if self.Input:
self.Input.Draw(dc)
Graphic_Element.Draw(self, dc)
#-------------------------------------------------------------------------------
# Sequencial Function Chart Action Block
#-------------------------------------------------------------------------------
"""
Class that implements the graphic representation of an action block
"""
class SFC_ActionBlock(Graphic_Element):
# Create a new action block
def __init__(self, parent, actions = [], id = None):
Graphic_Element.__init__(self, parent)
self.Id = id
self.Size = wxSize(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1])
# Create an input and output connector
self.Input = Connector(self, "", "ANY", wxPoint(0, SFC_ACTION_MIN_SIZE[1] / 2), WEST)
self.SetActions(actions)
# Destructor
def __del__(self):
self.Input = None
# Returns the number of action lines
def GetLineNumber(self):
return len(self.Actions)
def GetLineSize(self):
if len(self.Actions) > 1:
return self.Size[1] / len(self.Actions)
else:
return SFC_ACTION_MIN_SIZE[1]
# Forbids to resize the action block
def Resize(self, x, y, width, height):
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
if x == 0:
self.SetSize(width, self.Size[1])
else:
Graphic_Element.Resize(self, x, y, width, height)
# Delete this action block by calling the appropriate method
def Delete(self):
self.Parent.DeleteActionBlock(self)
# Unconnect input and output
def Clean(self):
self.Input.UnConnect()
# Refresh the action block bounding box
def RefreshBoundingBox(self):
self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1])
# Refresh the position of wires connected to action block
def RefreshConnected(self, exclude = []):
self.Input.MoveConnected(exclude)
# Returns input action block connector
def GetConnector(self, position = None, name = None):
return self.Input
# Test if point given is on action block input connector
def TestConnector(self, pt, exclude = True):
# Test input connector
if self.Input.TestPoint(pt, exclude):
return self.Input
return None
# Changes the action block actions
def SetActions(self, actions):
dc = wxClientDC(self.Parent)
self.Actions = actions
self.ColSize = [0, 0, 0]
for action in self.Actions:
width, height = dc.GetTextExtent(action["qualifier"])
self.ColSize[0] = max(self.ColSize[0], width + 10)
if "duration" in action:
width, height = dc.GetTextExtent(action["duration"])
self.ColSize[0] = max(self.ColSize[0], width + 10)
width, height = dc.GetTextExtent(action["value"])
self.ColSize[1] = max(self.ColSize[1], width + 10)
if "indicator" in action and action["indicator"] != "":
width, height = dc.GetTextExtent(action["indicator"])
self.ColSize[2] = max(self.ColSize[2], width + 10)
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
line_size = self.GetLineSize()
self.Size = wxSize(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], len(self.Actions) * line_size)
else:
self.Size = wxSize(max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2],
SFC_ACTION_MIN_SIZE[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1])
self.RefreshBoundingBox()
if self.Input:
wires = self.Input.GetWires()
if len(wires) == 1:
input_block = wires[0][0].EndConnected.GetParentBlock()
input_block.RefreshOutputPosition()
input_block.RefreshOutputModel(True)
# Returns the action block actions
def GetActions(self):
return self.Actions
# Returns the action block minimum size
def GetMinSize(self):
return max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2],
SFC_ACTION_MIN_SIZE[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1]
# Method called when a LeftDClick event have been generated
def OnLeftDClick(self, event, dc, scaling):
# Edit the action block properties
self.Parent.EditActionBlockContent(self)
# Refreshes the action block state according to move defined and handle selected
def ProcessDragging(self, movex, movey):
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
handle_type, handle = self.Handle
if handle_type == HANDLE_MOVE:
wires = self.Input.GetWires()
if len(wires) == 1:
input_pos = wires[0][0].EndConnected.GetPosition(False)
if self.Pos.x - input_pos.x + movex >= SFC_WIRE_MIN_SIZE:
self.Move(movex, 0)
else:
Graphic_Element.ProcessDragging(self, movex, movey)
else:
Graphic_Element.ProcessDragging(self, movex, movey)
# Refreshes the action block model
def RefreshModel(self, move=True):
self.Parent.RefreshActionBlockModel(self)
# Draws divergence
def Draw(self, dc):
dc.SetPen(wxBLACK_PEN)
dc.SetBrush(wxWHITE_BRUSH)
colsize = [self.ColSize[0], self.Size[0] - self.ColSize[0] - self.ColSize[2], self.ColSize[2]]
# Draw plain rectangle for representing the action block
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
dc.DrawLine(self.Pos.x + colsize[0], self.Pos.y,
self.Pos.x + colsize[0], self.Pos.y + self.Size[1])
dc.DrawLine(self.Pos.x + colsize[0] + colsize[1], self.Pos.y,
self.Pos.x + colsize[0] + colsize[1], self.Pos.y + self.Size[1])
line_size = self.GetLineSize()
for i, action in enumerate(self.Actions):
if i != 0:
dc.DrawLine(self.Pos.x, self.Pos.y + i * line_size,
self.Pos.x + self.Size[0], self.Pos.y + i * line_size)
text_width, text_height = dc.GetTextExtent(action["qualifier"])
if "duration" in action:
dc.DrawText(action["qualifier"], self.Pos.x + (colsize[0] - text_width) / 2,
self.Pos.y + i * line_size + line_size / 2 - text_height)
text_width, text_height = dc.GetTextExtent(action["duration"])
dc.DrawText(action["duration"], self.Pos.x + (colsize[0] - text_width) / 2,
self.Pos.y + i * line_size + line_size / 2)
else:
dc.DrawText(action["qualifier"], self.Pos.x + (colsize[0] - text_width) / 2,
self.Pos.y + i * line_size + (line_size - text_height) / 2)
text_width, text_height = dc.GetTextExtent(action["value"])
dc.DrawText(action["value"], self.Pos.x + colsize[0] + (colsize[1] - text_width) / 2,
self.Pos.y + i * line_size + (line_size - text_height) / 2)
if "indicator" in action:
text_width, text_height = dc.GetTextExtent(action["indicator"])
dc.DrawText(action["indicator"], self.Pos.x + colsize[0] + colsize[1] + (colsize[2] - text_width) / 2,
self.Pos.y + i * line_size + (line_size - text_height) / 2)
# Draw input connector
self.Input.Draw(dc)
Graphic_Element.Draw(self, dc)