etisserant@0: #!/usr/bin/env python etisserant@0: # -*- coding: utf-8 -*- etisserant@0: etisserant@0: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor etisserant@0: #based on the plcopen standard. etisserant@0: # etisserant@0: #Copyright (C): Edouard TISSERANT and Laurent BESSARD etisserant@0: # etisserant@0: #See COPYING file for copyrights details. etisserant@0: # etisserant@0: #This library is free software; you can redistribute it and/or etisserant@0: #modify it under the terms of the GNU Lesser General Public etisserant@0: #License as published by the Free Software Foundation; either etisserant@0: #version 2.1 of the License, or (at your option) any later version. etisserant@0: # etisserant@0: #This library is distributed in the hope that it will be useful, etisserant@0: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@0: #Lesser General Public License for more details. etisserant@0: # etisserant@0: #You should have received a copy of the GNU Lesser General Public etisserant@0: #License along with this library; if not, write to the Free Software etisserant@0: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: etisserant@0: from wxPython.wx import * etisserant@0: import wx etisserant@0: etisserant@0: from GraphicCommons import * etisserant@0: from plcopen.structures import * etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Ladder Diagram PowerRail etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements the graphic representation of a power rail etisserant@0: """ etisserant@0: etisserant@0: class LD_PowerRail(Graphic_Element): etisserant@0: etisserant@0: # Create a new power rail etisserant@0: def __init__(self, parent, type, id = None, connectors = [True]): etisserant@0: Graphic_Element.__init__(self, parent) etisserant@0: self.Type = type etisserant@0: self.Id = id etisserant@0: # Create a connector or a blank according to 'connectors' and add it in etisserant@0: # the connectors list etisserant@0: self.Connectors = [] etisserant@0: for connector in connectors: etisserant@0: self.AddConnector(connector) etisserant@0: self.RefreshSize() etisserant@0: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Connectors = [] etisserant@0: etisserant@0: # Forbids to change the power rail size etisserant@0: def SetSize(self, width, height): etisserant@0: pass etisserant@0: etisserant@0: # Forbids to select a power rail etisserant@0: def HitTest(self, pt): etisserant@0: return False etisserant@0: etisserant@0: # Deletes this power rail by calling the appropriate method etisserant@0: def Delete(self): etisserant@0: self.Parent.DeletePowerRail(self) etisserant@0: etisserant@0: # Unconnect all connectors etisserant@0: def Clean(self): etisserant@0: for connector in self.Connectors: etisserant@0: if connector: etisserant@0: connector.UnConnect() etisserant@0: etisserant@0: # Refresh the power rail bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: dc = wxClientDC(self.Parent) etisserant@0: if self.Type == LEFTRAIL: etisserant@0: bbx_x = self.Pos.x etisserant@0: elif self.Type == RIGHTRAIL: etisserant@0: bbx_x = self.Pos.x - CONNECTOR_SIZE etisserant@0: self.BoundingBox = wxRect(bbx_x, self.Pos.y, self.Size[0] + CONNECTOR_SIZE + 1, self.Size[1] + 1) etisserant@0: etisserant@0: # Refresh the power rail size etisserant@0: def RefreshSize(self): etisserant@0: self.Size = wxSize(2, LD_LINE_SIZE * len(self.Connectors)) etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Add a connector or a blank to this power rail at the last place etisserant@0: def AddConnector(self, connector = True): etisserant@0: self.InsertConnector(len(self.Connectors), connector) etisserant@0: etisserant@0: # Add a connector or a blank to this power rail at the place given etisserant@0: def InsertConnector(self, idx, connector = True): etisserant@0: if connector: etisserant@0: if self.Type == LEFTRAIL: etisserant@0: connector = Connector(self, "", "BOOL", wxPoint(2, 0), EAST) etisserant@0: elif self.Type == RIGHTRAIL: etisserant@0: connector = Connector(self, "", "BOOL", wxPoint(0, 0), WEST) etisserant@0: self.Connectors.insert(idx, connector) etisserant@0: else: etisserant@0: self.Connectors.insert(idx, None) etisserant@0: self.RefreshSize() etisserant@0: self.RefreshConnectors() etisserant@0: etisserant@0: # Returns the index in connectors list for the connector given etisserant@0: def GetConnectorIndex(self, connector): etisserant@0: if connector in self.Connectors: etisserant@0: return self.Connectors.index(connector) etisserant@0: return None etisserant@0: etisserant@0: # Returns if there is a connector in connectors list at the index given etisserant@0: def IsNullConnector(self, idx): etisserant@0: if idx < len(self.Connectors): etisserant@0: return self.Connectors[idx] == None etisserant@0: return False etisserant@0: etisserant@0: # Delete the connector or blank from connectors list at the index given etisserant@0: def DeleteConnector(self, idx): etisserant@0: self.Connectors.pop(idx) etisserant@0: self.RefreshConnectors() etisserant@0: self.RefreshSize() etisserant@0: etisserant@0: # Refresh the positions of the power rail connectors etisserant@0: def RefreshConnectors(self): etisserant@0: position = LD_LINE_SIZE / 2 etisserant@0: for connector in self.Connectors: etisserant@0: if connector: etisserant@0: if self.Type == LEFTRAIL: etisserant@0: connector.SetPosition(wxPoint(self.Size[0], position)) etisserant@0: elif self.Type == RIGHTRAIL: etisserant@0: connector.SetPosition(wxPoint(0, position)) etisserant@0: position += LD_LINE_SIZE etisserant@0: self.RefreshConnected() etisserant@0: etisserant@0: # Refresh the position of wires connefcted to power rail etisserant@0: def RefreshConnected(self, exclude = []): etisserant@0: for connector in self.Connectors: etisserant@0: connector.MoveConnected(exclude) etisserant@0: etisserant@0: # Returns the power rail connector that starts with the point given if it exists etisserant@0: def GetConnector(self, position): etisserant@0: for connector in self.Connectors: etisserant@0: if connector: etisserant@0: connector_pos = connector.GetRelPosition() etisserant@0: if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y: etisserant@0: return connector etisserant@0: return None etisserant@0: etisserant@0: # Returns all the power rail connectors etisserant@0: def GetConnectors(self): etisserant@0: return [connector for connector in self.Connectors if connector] etisserant@0: etisserant@0: # Test if point given is on one of the power rail connectors etisserant@0: def TestConnector(self, pt, exclude=True): etisserant@0: for connector in self.Connectors: etisserant@0: if connector and connector.TestPoint(pt, exclude): etisserant@0: return connector etisserant@0: return None etisserant@0: etisserant@0: # Returns the power rail type etisserant@0: def GetType(self): etisserant@0: return self.Type etisserant@0: etisserant@0: # Refreshes the power rail model etisserant@0: def RefreshModel(self, move=True): etisserant@0: self.Parent.RefreshPowerRailModel(self) etisserant@0: # If power rail has moved and power rail is of type LEFT, refresh the model etisserant@0: # of wires connected to connectors etisserant@0: if move and self.Type == LEFTRAIL: etisserant@0: for connector in self.Connectors: etisserant@0: if connector: etisserant@0: connector.RefreshWires() etisserant@0: etisserant@0: # Draws power rail etisserant@0: def Draw(self, dc): etisserant@0: dc.SetPen(wxBLACK_PEN) etisserant@0: dc.SetBrush(wxBLACK_BRUSH) etisserant@0: # Draw a rectangle with the power rail size etisserant@0: dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) etisserant@0: # Draw connectors etisserant@0: for connector in self.Connectors: etisserant@0: if connector: etisserant@0: connector.Draw(dc) etisserant@0: Graphic_Element.Draw(self, dc) etisserant@0: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Ladder Diagram Contact etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements the graphic representation of a contact etisserant@0: """ etisserant@0: etisserant@0: class LD_Contact(Graphic_Element): etisserant@0: etisserant@0: # Create a new contact etisserant@0: def __init__(self, parent, type, name, id = None): etisserant@0: Graphic_Element.__init__(self, parent) etisserant@0: self.Type = type etisserant@0: self.Name = name etisserant@0: self.Id = id etisserant@0: self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) etisserant@0: # Create an input and output connector etisserant@0: self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST) etisserant@0: self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST) etisserant@0: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Input = None etisserant@0: self.Output = None etisserant@0: etisserant@0: # Forbids to change the contact size etisserant@0: def SetSize(self, width, height): etisserant@0: pass etisserant@0: etisserant@0: # Delete this contact by calling the appropriate method etisserant@0: def Delete(self): etisserant@0: self.Parent.DeleteContact(self) etisserant@0: etisserant@0: # Unconnect input and output etisserant@0: def Clean(self): etisserant@0: self.Input.UnConnect() etisserant@0: self.Output.UnConnect() etisserant@0: etisserant@0: # Refresh the contact bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: dc = wxClientDC(self.Parent) etisserant@0: # Calculate the size of the name outside the contact etisserant@0: text_width, text_height = dc.GetTextExtent(self.Name) etisserant@0: # Calculate the bounding box size etisserant@0: if self.Name != "": etisserant@0: bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) etisserant@0: bbx_width = max(self.Size[0], text_width) etisserant@0: bbx_y = self.Pos.y - (text_height + 2) etisserant@0: bbx_height = self.Size[1] + (text_height + 2) etisserant@0: else: etisserant@0: bbx_x = self.Pos.x etisserant@0: bbx_width = self.Size[0] etisserant@0: bbx_y = self.Pos.y etisserant@0: bbx_height = self.Size[1] etisserant@0: self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) etisserant@0: etisserant@0: # Refresh the position of wire connected to contact etisserant@0: def RefreshConnected(self, exclude = []): etisserant@0: self.Input.MoveConnected(exclude) etisserant@0: self.Output.MoveConnected(exclude) etisserant@0: etisserant@0: # Returns the contact connector that starts with the point given if it exists etisserant@0: def GetConnector(self, position): etisserant@0: # Test input connector etisserant@0: input_pos = self.Input.GetRelPosition() etisserant@0: if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: etisserant@0: return self.Input etisserant@0: # Test output connector etisserant@0: output_pos = self.Output.GetRelPosition() etisserant@0: if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: etisserant@0: return self.Output etisserant@0: return None etisserant@0: etisserant@0: # Returns input and output contact connectors etisserant@0: def GetConnectors(self): etisserant@0: return {"input":self.Input,"output":self.Output} etisserant@0: etisserant@0: # Test if point given is on contact input or output connector etisserant@0: def TestConnector(self, pt, exclude=True): etisserant@0: # Test input connector etisserant@0: if self.Input.TestPoint(pt, exclude): etisserant@0: return self.Input etisserant@0: # Test output connector etisserant@0: if self.Output.TestPoint(pt, exclude): etisserant@0: return self.Output etisserant@0: return None etisserant@0: etisserant@0: # Changes the contact name etisserant@0: def SetName(self, name): etisserant@0: self.Name = name etisserant@0: etisserant@0: # Returns the contact name etisserant@0: def GetName(self): etisserant@0: return self.Name etisserant@0: etisserant@0: # Changes the contact type etisserant@0: def SetType(self, type): etisserant@0: self.Type = type etisserant@0: etisserant@0: # Returns the contact type etisserant@0: def GetType(self): etisserant@0: return self.Type etisserant@0: etisserant@0: # Method called when a LeftDClick event have been generated etisserant@0: def OnLeftDClick(self, event, scaling): etisserant@0: # Edit the contact properties etisserant@0: self.Parent.EditContactContent(self) etisserant@0: etisserant@0: # Refreshes the contact model etisserant@0: def RefreshModel(self, move=True): etisserant@0: self.Parent.RefreshContactModel(self) etisserant@0: # If contact has moved, refresh the model of wires connected to output etisserant@0: if move: etisserant@0: self.Output.RefreshWires() etisserant@0: etisserant@0: # Draws contact etisserant@0: def Draw(self, dc): etisserant@0: dc.SetPen(wxBLACK_PEN) etisserant@0: dc.SetBrush(wxBLACK_BRUSH) etisserant@0: # Draw two rectangles for representing the contact etisserant@0: dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1) etisserant@0: dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1) etisserant@0: # Draw contact name etisserant@0: namewidth, nameheight = dc.GetTextExtent(self.Name) etisserant@0: dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, etisserant@0: self.Pos.y - (nameheight + 2)) etisserant@0: # Draw the modifier symbol in the middle of contact etisserant@0: typetext = "" etisserant@0: if self.Type == CONTACT_REVERSE: etisserant@0: typetext = "/" etisserant@0: elif self.Type == CONTACT_RISING: etisserant@0: typetext = "P" etisserant@0: elif self.Type == CONTACT_FALLING: etisserant@0: typetext = "N" etisserant@0: if typetext != "": etisserant@0: typewidth, typeheight = dc.GetTextExtent(typetext) etisserant@0: dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1, etisserant@0: self.Pos.y + (self.Size[1] - typeheight) / 2) etisserant@0: # Draw input and output connectors etisserant@0: self.Input.Draw(dc) etisserant@0: self.Output.Draw(dc) etisserant@0: Graphic_Element.Draw(self, dc) etisserant@0: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Ladder Diagram Coil etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements the graphic representation of a coil etisserant@0: """ etisserant@0: etisserant@0: class LD_Coil(Graphic_Element): etisserant@0: etisserant@0: # Create a new coil etisserant@0: def __init__(self, parent, type, name, id = None): etisserant@0: Graphic_Element.__init__(self, parent) etisserant@0: self.Type = type etisserant@0: self.Name = name etisserant@0: self.Id = id etisserant@0: self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) etisserant@0: # Create an input and output connector etisserant@0: self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST) etisserant@0: self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST) etisserant@0: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Input = None etisserant@0: self.Output = None etisserant@0: etisserant@0: # Forbids to change the contact size etisserant@0: def SetSize(self, width, height): etisserant@0: pass etisserant@0: etisserant@0: # Delete this coil by calling the appropriate method etisserant@0: def Delete(self): etisserant@0: self.Parent.DeleteCoil(self) etisserant@0: etisserant@0: # Unconnect input and output etisserant@0: def Clean(self): etisserant@0: self.Input.UnConnect() etisserant@0: self.Output.UnConnect() etisserant@0: etisserant@0: # Refresh the coil bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: dc = wxClientDC(self.Parent) etisserant@0: # Calculate the size of the name outside the coil etisserant@0: text_width, text_height = dc.GetTextExtent(self.Name) etisserant@0: # Calculate the bounding box size etisserant@0: if self.Name != "": etisserant@0: bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) etisserant@0: bbx_width = max(self.Size[0], text_width) etisserant@0: bbx_y = self.Pos.y - (text_height + 2) etisserant@0: bbx_height = self.Size[1] + (text_height + 2) etisserant@0: else: etisserant@0: bbx_x = self.Pos.x etisserant@0: bbx_width = self.Size[0] etisserant@0: bbx_y = self.Pos.y etisserant@0: bbx_height = self.Size[1] etisserant@0: self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) etisserant@0: etisserant@0: # Refresh the position of wire connected to coil etisserant@0: def RefreshConnected(self, exclude = []): etisserant@0: self.Input.MoveConnected(exclude) etisserant@0: self.Output.MoveConnected(exclude) etisserant@0: etisserant@0: # Returns the coil connector that starts with the point given if it exists etisserant@0: def GetConnector(self, position): etisserant@0: # Test input connector etisserant@0: input_pos = self.Input.GetRelPosition() etisserant@0: if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: etisserant@0: return self.Input etisserant@0: # Test output connector etisserant@0: output_pos = self.Output.GetRelPosition() etisserant@0: if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: etisserant@0: return self.Output etisserant@0: return None etisserant@0: etisserant@0: # Returns input and output coil connectors etisserant@0: def GetConnectors(self): etisserant@0: return {"input":self.Input,"output":self.Output} etisserant@0: etisserant@0: # Test if point given is on coil input or output connector etisserant@0: def TestConnector(self, pt, exclude=True): etisserant@0: # Test input connector etisserant@0: if self.Input.TestPoint(pt, exclude): etisserant@0: return self.Input etisserant@0: # Test output connector etisserant@0: if self.Output.TestPoint(pt, exclude): etisserant@0: return self.Output etisserant@0: return None etisserant@0: etisserant@0: # Changes the coil name etisserant@0: def SetName(self, name): etisserant@0: self.Name = name etisserant@0: etisserant@0: # Returns the coil name etisserant@0: def GetName(self): etisserant@0: return self.Name etisserant@0: etisserant@0: # Changes the coil type etisserant@0: def SetType(self, type): etisserant@0: self.Type = type etisserant@0: etisserant@0: # Returns the coil type etisserant@0: def GetType(self): etisserant@0: return self.Type etisserant@0: etisserant@0: # Method called when a LeftDClick event have been generated etisserant@0: def OnLeftDClick(self, event, scaling): etisserant@0: # Edit the coil properties etisserant@0: self.Parent.EditCoilContent(self) etisserant@0: etisserant@0: # Refreshes the coil model etisserant@0: def RefreshModel(self, move=True): etisserant@0: self.Parent.RefreshCoilModel(self) etisserant@0: # If coil has moved, refresh the model of wires connected to output etisserant@0: if move: etisserant@0: self.Output.RefreshWires() etisserant@0: etisserant@0: # Draws coil etisserant@0: def Draw(self, dc): etisserant@0: dc.SetPen(wxPen(wxBLACK, 2, wxSOLID)) etisserant@0: dc.SetBrush(wxTRANSPARENT_BRUSH) etisserant@0: # Draw a two circle arcs for representing the coil etisserant@0: dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225) etisserant@0: dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45) etisserant@0: dc.SetPen(wxBLACK_PEN) etisserant@0: dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1) etisserant@0: # Draw coil name etisserant@0: namewidth, nameheight = dc.GetTextExtent(self.Name) etisserant@0: dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, etisserant@0: self.Pos.y - (nameheight + 2)) etisserant@0: # Draw the modifier symbol in the middle of coil etisserant@0: typetext = "" etisserant@0: if self.Type == COIL_REVERSE: etisserant@0: typetext = "/" etisserant@0: elif self.Type == COIL_SET: etisserant@0: typetext = "S" etisserant@0: elif self.Type == COIL_RESET: etisserant@0: typetext = "R" etisserant@0: if typetext != "": etisserant@0: typewidth, typeheight = dc.GetTextExtent(typetext) etisserant@0: dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1, etisserant@0: self.Pos.y + (self.Size[1] - typeheight) / 2) etisserant@0: # Draw input and output connectors etisserant@0: self.Input.Draw(dc) etisserant@0: self.Output.Draw(dc) etisserant@0: Graphic_Element.Draw(self, dc)