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: # lbessard@58: #Copyright (C) 2007: 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@5: #modify it under the terms of the GNU 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 lbessard@58: #General Public License for more details. etisserant@0: # etisserant@5: #You should have received a copy of the GNU 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: 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) lbessard@61: self.Type = None lbessard@61: self.Connectors = [] lbessard@71: self.RealConnectors = None etisserant@0: self.Id = id lbessard@27: self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2] lbessard@27: if len(connectors) < 1: lbessard@27: connectors = [True] lbessard@61: self.SetType(type, connectors) etisserant@0: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Connectors = [] etisserant@0: lbessard@112: # Make a clone of this LD_PowerRail lbessard@162: def Clone(self, parent, id = None, pos = None): lbessard@162: powerrail = LD_PowerRail(parent, self.Type, id) lbessard@112: powerrail.SetSize(self.Size[0], self.Size[1]) lbessard@112: if pos is not None: lbessard@112: powerrail.SetPosition(pos.x, pos.y) lbessard@112: powerrail.Connectors = [] lbessard@112: for connector in self.Connectors: lbessard@112: if connector is not None: lbessard@112: powerrail.Connectors.append(connector.Clone(powerrail)) lbessard@112: else: lbessard@112: powerrail.Connectors.append(None) lbessard@112: return powerrail lbessard@112: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: rect = Graphic_Element.GetRedrawRect(self, movex, movey) lbessard@144: for connector in self.Connectors: lbessard@144: if connector is not None: lbessard@144: rect = rect.Union(connector.GetRedrawRect(movex, movey)) lbessard@144: if movex != 0 or movey != 0: lbessard@144: for connector in self.Connectors: lbessard@144: if connector is not None and connector.IsConnected(): lbessard@144: rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey)) lbessard@144: return rect lbessard@144: etisserant@0: # Forbids to change the power rail size etisserant@0: def SetSize(self, width, height): lbessard@42: if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: lbessard@27: Graphic_Element.SetSize(self, width, height) lbessard@27: self.RefreshConnectors() etisserant@0: etisserant@0: # Forbids to select a power rail etisserant@0: def HitTest(self, pt): lbessard@42: if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: lbessard@27: return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None etisserant@0: return False etisserant@0: lbessard@42: # Forbids to select a power rail lbessard@42: def IsInSelection(self, rect): lbessard@42: if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: lbessard@61: return Graphic_Element.IsInSelection(self, rect) lbessard@42: return False lbessard@42: 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: lbessard@61: connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) etisserant@0: etisserant@0: # Refresh the power rail bounding box etisserant@0: def RefreshBoundingBox(self): lbessard@144: self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) etisserant@0: etisserant@0: # Refresh the power rail size etisserant@0: def RefreshSize(self): lbessard@144: self.Size = wx.Size(LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors)) etisserant@0: self.RefreshBoundingBox() etisserant@0: lbessard@27: # Returns the block minimum size lbessard@27: def GetMinSize(self): lbessard@96: if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: lbessard@144: return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1] lbessard@144: else: lbessard@144: return LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors) lbessard@27: 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: lbessard@80: connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST) etisserant@0: elif self.Type == RIGHTRAIL: lbessard@64: connector = Connector(self, "", "BOOL", wx.Point(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: lbessard@27: # Moves the divergence connector given lbessard@27: def MoveConnector(self, connector, movey): lbessard@27: position = connector.GetRelPosition() lbessard@64: connector.SetPosition(wx.Point(position.x, position.y + movey)) lbessard@27: miny = self.Size[1] lbessard@27: maxy = 0 lbessard@27: for connect in self.Connectors: lbessard@27: connect_pos = connect.GetRelPosition() lbessard@80: miny = min(miny, connect_pos.y - self.Extensions[0]) lbessard@80: maxy = max(maxy, connect_pos.y - self.Extensions[0]) lbessard@80: min_pos = self.Pos.y + miny lbessard@27: self.Pos.y = min(min_pos, self.Pos.y) lbessard@27: if min_pos == self.Pos.y: lbessard@27: for connect in self.Connectors: lbessard@27: connect_pos = connect.GetRelPosition() lbessard@80: connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y - miny)) lbessard@110: self.Connectors.sort(lambda x, y: x.Pos.y.__cmp__(y.Pos.y)) lbessard@80: maxy = 0 lbessard@80: for connect in self.Connectors: lbessard@80: connect_pos = connect.GetRelPosition() lbessard@80: maxy = max(maxy, connect_pos.y) lbessard@80: self.Size[1] = max(maxy + self.Extensions[1], self.Size[1]) lbessard@27: connector.MoveConnected() lbessard@27: self.RefreshBoundingBox() lbessard@27: 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): lbessard@145: scaling = self.Parent.GetScaling() lbessard@71: if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: lbessard@71: height = self.Size[1] - self.Extensions[0] - self.Extensions[1] lbessard@80: interval = float(height) / float(max(len(self.Connectors) - 1, 1)) lbessard@71: for i, connector in enumerate(self.Connectors): lbessard@145: if self.RealConnectors: lbessard@145: position = self.Extensions[0] + int(round(self.RealConnectors[i] * height)) lbessard@145: else: lbessard@145: position = self.Extensions[0] + int(round(i * interval)) lbessard@145: if scaling is not None: lbessard@145: position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y lbessard@80: if self.Type == LEFTRAIL: lbessard@145: connector.SetPosition(wx.Point(self.Size[0], position)) lbessard@80: elif self.Type == RIGHTRAIL: lbessard@145: connector.SetPosition(wx.Point(0, position)) lbessard@71: else: lbessard@71: position = self.Extensions[0] lbessard@71: for connector in self.Connectors: lbessard@71: if connector: lbessard@145: if scaling is not None: lbessard@145: ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y lbessard@145: else: lbessard@145: ypos = position lbessard@71: if self.Type == LEFTRAIL: lbessard@145: connector.SetPosition(wx.Point(self.Size[0], ypos)) lbessard@71: elif self.Type == RIGHTRAIL: lbessard@145: connector.SetPosition(wx.Point(0, ypos)) lbessard@71: position += LD_LINE_SIZE etisserant@0: self.RefreshConnected() etisserant@0: lbessard@7: # Refresh the position of wires connected to power rail etisserant@0: def RefreshConnected(self, exclude = []): etisserant@0: for connector in self.Connectors: lbessard@8: if connector: lbessard@8: connector.MoveConnected(exclude) etisserant@0: etisserant@0: # Returns the power rail connector that starts with the point given if it exists lbessard@27: def GetConnector(self, position, name = None): lbessard@27: # if a name is given lbessard@27: if name: lbessard@27: # Test each connector if it exists lbessard@27: for connector in self.Connectors: lbessard@27: if connector and name == connector.GetName(): lbessard@27: return connector 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 lbessard@61: def SetType(self, type, connectors): lbessard@61: if type != self.Type or len(self.Connectors) != len(connectors): lbessard@61: # Create a connector or a blank according to 'connectors' and add it in lbessard@61: # the connectors list lbessard@61: self.Type = type lbessard@61: self.Clean() lbessard@61: self.Connectors = [] lbessard@61: for connector in connectors: lbessard@61: self.AddConnector(connector) lbessard@61: self.RefreshSize() lbessard@61: lbessard@61: # Returns the power rail type etisserant@0: def GetType(self): etisserant@0: return self.Type etisserant@0: lbessard@27: # Method called when a LeftDown event have been generated lbessard@27: def OnLeftDown(self, event, dc, scaling): lbessard@145: self.RealConnectors = [] lbessard@145: height = self.Size[1] - self.Extensions[0] - self.Extensions[1] lbessard@145: for connector in self.Connectors: lbessard@145: position = connector.GetRelPosition() lbessard@145: self.RealConnectors.append(float(position.y - self.Extensions[0])/float(max(1, height))) lbessard@145: Graphic_Element.OnLeftDown(self, event, dc, scaling) lbessard@145: lbessard@145: # Method called when a LeftUp event have been generated lbessard@145: def OnLeftUp(self, event, dc, scaling): lbessard@145: Graphic_Element.OnLeftUp(self, event, dc, scaling) lbessard@145: self.RealConnectors = None lbessard@145: lbessard@145: # Method called when a LeftDown event have been generated lbessard@145: def OnRightDown(self, event, dc, scaling): lbessard@27: pos = GetScaledEventPosition(event, dc, scaling) lbessard@27: # Test if a connector have been handled lbessard@27: connector = self.TestConnector(pos, False) lbessard@27: if connector: lbessard@27: self.Handle = (HANDLE_CONNECTOR, connector) lbessard@64: self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND)) lbessard@27: self.Selected = False lbessard@27: # Initializes the last position lbessard@27: self.oldPos = GetScaledEventPosition(event, dc, scaling) lbessard@27: else: lbessard@145: Graphic_Element.OnRightDown(self, event, dc, scaling) lbessard@145: lbessard@145: # Method called when a LeftDClick event have been generated lbessard@145: def OnLeftDClick(self, event, dc, scaling): lbessard@145: # Edit the powerrail properties lbessard@145: self.Parent.EditPowerRailContent(self) lbessard@145: lbessard@145: # Method called when a RightUp event have been generated lbessard@145: def OnRightUp(self, event, dc, scaling): lbessard@27: handle_type, handle = self.Handle lbessard@27: if handle_type == HANDLE_CONNECTOR: lbessard@27: wires = handle.GetWires() lbessard@80: if len(wires) == 1: lbessard@80: if handle == wires[0][0].StartConnected: lbessard@80: block = wires[0][0].EndConnected.GetParentBlock() lbessard@80: else: lbessard@80: block = wires[0][0].StartConnected.GetParentBlock() lbessard@80: block.RefreshModel(False) lbessard@145: Graphic_Element.OnRightUp(self, event, dc, scaling) lbessard@145: else: lbessard@145: self.Parent.PopupDefaultMenu() lbessard@27: etisserant@175: def Resize(self, x, y, width, height): etisserant@175: self.Move(x, y) etisserant@175: self.SetSize(LD_POWERRAIL_WIDTH, height) etisserant@175: lbessard@110: # Refreshes the powerrail state according to move defined and handle selected lbessard@165: def ProcessDragging(self, movex, movey, centered, scaling): lbessard@27: handle_type, handle = self.Handle lbessard@27: # A connector has been handled lbessard@27: if handle_type == HANDLE_CONNECTOR: lbessard@138: movey = max(-self.BoundingBox.y, movey) lbessard@145: if scaling is not None: lbessard@145: position = handle.GetRelPosition() lbessard@145: movey = round(float(self.Pos.y + position.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y - position.y lbessard@27: self.MoveConnector(handle, movey) lbessard@138: return 0, movey lbessard@110: else: lbessard@165: return Graphic_Element.ProcessDragging(self, movex, movey, centered, scaling) lbessard@27: 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): lbessard@144: Graphic_Element.Draw(self, dc) lbessard@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.BLACK_BRUSH) etisserant@0: # Draw a rectangle with the power rail size etisserant@175: if self.Type == LEFTRAIL: etisserant@175: dc.DrawRectangle(self.Pos.x + self.Size[0] - LD_POWERRAIL_WIDTH, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1) etisserant@175: else: etisserant@175: dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 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) lbessard@140: 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 lbessard@64: self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) lbessard@231: self.Errors = {} etisserant@0: # Create an input and output connector lbessard@64: self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) lbessard@64: self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) lbessard@42: self.RefreshNameSize() lbessard@42: self.RefreshTypeSize() etisserant@0: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Input = None etisserant@0: self.Output = None etisserant@0: lbessard@112: # Make a clone of this LD_Contact lbessard@162: def Clone(self, parent, id = None, pos = None): lbessard@162: contact = LD_Contact(parent, self.Type, self.Name, id) lbessard@112: contact.SetSize(self.Size[0], self.Size[1]) lbessard@112: if pos is not None: lbessard@112: contact.SetPosition(pos.x, pos.y) lbessard@112: contact.Input = self.Input.Clone(contact) lbessard@112: contact.Output = self.Output.Clone(contact) lbessard@112: return contact lbessard@112: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: rect = Graphic_Element.GetRedrawRect(self, movex, movey) lbessard@144: rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) lbessard@144: rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) lbessard@144: if movex != 0 or movey != 0: lbessard@144: if self.Input.IsConnected(): lbessard@144: rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) lbessard@144: if self.Output.IsConnected(): lbessard@144: rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) lbessard@144: return rect etisserant@180: etisserant@180: def ProcessDragging(self, movex, movey, centered, scaling): etisserant@180: return Graphic_Element.ProcessDragging(self, movex, movey, centered, scaling, height_fac = 2) lbessard@144: etisserant@0: # Forbids to change the contact size etisserant@0: def SetSize(self, width, height): lbessard@42: if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: lbessard@27: Graphic_Element.SetSize(self, width, height) lbessard@27: self.RefreshConnectors() 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): lbessard@61: self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) lbessard@61: self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) etisserant@0: lbessard@42: # Refresh the size of text for name lbessard@42: def RefreshNameSize(self): lbessard@42: if self.Name != "": lbessard@165: self.NameSize = self.Parent.GetTextExtent(self.Name) lbessard@42: else: lbessard@42: self.NameSize = 0, 0 lbessard@42: lbessard@42: # Refresh the size of text for type lbessard@42: def RefreshTypeSize(self): lbessard@42: typetext = "" lbessard@42: if self.Type == CONTACT_REVERSE: lbessard@42: typetext = "/" lbessard@42: elif self.Type == CONTACT_RISING: lbessard@42: typetext = "P" lbessard@42: elif self.Type == CONTACT_FALLING: lbessard@42: typetext = "N" lbessard@42: if typetext != "": lbessard@165: self.TypeSize = self.Parent.GetTextExtent(typetext) lbessard@42: else: lbessard@42: self.TypeSize = 0, 0 lbessard@42: etisserant@0: # Refresh the contact bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: # Calculate the size of the name outside the contact lbessard@165: text_width, text_height = self.Parent.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] lbessard@64: self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) etisserant@0: lbessard@27: # Returns the block minimum size lbessard@27: def GetMinSize(self): lbessard@27: return LD_ELEMENT_SIZE lbessard@27: 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 lbessard@27: def GetConnector(self, position, name = None): lbessard@27: # if a name is given lbessard@27: if name: lbessard@27: # Test input and output connector lbessard@27: if name == self.Input.GetName(): lbessard@27: return self.Input lbessard@27: if name == self.Output.GetName(): lbessard@27: return self.Output 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: lbessard@27: # Refresh the positions of the block connectors lbessard@27: def RefreshConnectors(self): lbessard@145: scaling = self.Parent.GetScaling() lbessard@145: position = self.Size[1] / 2 + 1 lbessard@145: if scaling is not None: lbessard@145: position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y lbessard@145: self.Input.SetPosition(wx.Point(0, position)) lbessard@145: self.Output.SetPosition(wx.Point(self.Size[0], position)) lbessard@27: self.RefreshConnected() lbessard@27: etisserant@0: # Changes the contact name etisserant@0: def SetName(self, name): etisserant@0: self.Name = name lbessard@42: self.RefreshNameSize() 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 lbessard@50: self.RefreshTypeSize() 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 lbessard@27: def OnLeftDClick(self, event, dc, scaling): etisserant@0: # Edit the contact properties etisserant@0: self.Parent.EditContactContent(self) etisserant@0: lbessard@127: # Method called when a RightUp event have been generated lbessard@127: def OnRightUp(self, event, dc, scaling): lbessard@127: # Popup the default menu lbessard@127: self.Parent.PopupDefaultMenu() lbessard@127: 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: lbessard@140: # Draws the highlightment of this element if it is highlighted lbessard@140: def DrawHighlightment(self, dc): lbessard@144: dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) lbessard@144: dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) lbessard@144: dc.SetLogicalFunction(wx.AND) lbessard@144: # Draw two rectangles for representing the contact lbessard@144: dc.DrawRectangle(self.Pos.x - 2, self.Pos.y - 2, 6, self.Size[1] + 5) lbessard@144: dc.DrawRectangle(self.Pos.x + self.Size[0] - 3, self.Pos.y - 2, 6, self.Size[1] + 5) lbessard@144: dc.SetLogicalFunction(wx.COPY) lbessard@140: lbessard@231: def AddError(self, infos, start, end): lbessard@231: self.Errors[infos[0]] = (start[1], end[1]) lbessard@231: etisserant@0: # Draws contact etisserant@0: def Draw(self, dc): lbessard@144: Graphic_Element.Draw(self, dc) lbessard@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.BLACK_BRUSH) lbessard@213: lbessard@213: # Compiling contact type modifier symbol 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" lbessard@213: lbessard@213: if getattr(dc, "printing", False): lbessard@213: name_size = dc.GetTextExtent(self.Name) lbessard@213: if typetext != "": lbessard@213: type_size = dc.GetTextExtent(typetext) lbessard@213: else: lbessard@213: name_size = self.NameSize lbessard@213: if typetext != "": lbessard@213: type_size = self.TypeSize lbessard@213: lbessard@213: # Draw two rectangles for representing the contact lbessard@213: dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1) lbessard@213: dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1) lbessard@213: # Draw contact name lbessard@231: name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, lbessard@231: self.Pos.y - (name_size[1] + 2)) lbessard@231: dc.DrawText(self.Name, name_pos[0], name_pos[1]) lbessard@213: # Draw the modifier symbol in the middle of contact etisserant@0: if typetext != "": lbessard@231: type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1, lbessard@231: self.Pos.y + (self.Size[1] - type_size[1]) / 2) lbessard@231: dc.DrawText(typetext, type_pos[0], type_pos[1]) etisserant@0: # Draw input and output connectors etisserant@0: self.Input.Draw(dc) etisserant@0: self.Output.Draw(dc) lbessard@231: if "reference" in self.Errors: lbessard@231: HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1]) lbessard@231: if typetext != "" and ("negated" in self.Errors or "rising" in self.Errors or "falling" in self.Errors): lbessard@231: HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1]) lbessard@140: 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 lbessard@64: self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) lbessard@231: self.Errors = {} etisserant@0: # Create an input and output connector lbessard@64: self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) lbessard@64: self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) lbessard@42: self.RefreshNameSize() lbessard@42: self.RefreshTypeSize() etisserant@0: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Input = None etisserant@0: self.Output = None etisserant@0: lbessard@112: # Make a clone of this LD_Coil lbessard@162: def Clone(self, parent, id = None, pos = None): lbessard@162: coil = LD_Coil(parent, self.Type, self.Name, id) lbessard@112: coil.SetSize(self.Size[0], self.Size[1]) lbessard@112: if pos is not None: lbessard@112: coil.SetPosition(pos.x, pos.y) lbessard@112: coil.Input = self.Input.Clone(coil) lbessard@112: coil.Output = self.Output.Clone(coil) lbessard@112: return coil lbessard@112: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: rect = Graphic_Element.GetRedrawRect(self, movex, movey) lbessard@144: rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) lbessard@144: rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) lbessard@144: if movex != 0 or movey != 0: lbessard@144: if self.Input.IsConnected(): lbessard@144: rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) lbessard@144: if self.Output.IsConnected(): lbessard@144: rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) lbessard@144: return rect lbessard@144: etisserant@180: def ProcessDragging(self, movex, movey, centered, scaling): etisserant@180: return Graphic_Element.ProcessDragging(self, movex, movey, centered, scaling, height_fac = 2) etisserant@180: etisserant@180: # Forbids to change the Coil size etisserant@0: def SetSize(self, width, height): lbessard@42: if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: lbessard@27: Graphic_Element.SetSize(self, width, height) lbessard@27: self.RefreshConnectors() 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: lbessard@42: # Refresh the size of text for name lbessard@42: def RefreshNameSize(self): lbessard@42: if self.Name != "": lbessard@165: self.NameSize = self.Parent.GetTextExtent(self.Name) lbessard@42: else: lbessard@42: self.NameSize = 0, 0 lbessard@42: lbessard@42: # Refresh the size of text for type lbessard@42: def RefreshTypeSize(self): lbessard@42: typetext = "" lbessard@42: if self.Type == COIL_REVERSE: lbessard@42: typetext = "/" lbessard@42: elif self.Type == COIL_SET: lbessard@42: typetext = "S" lbessard@42: elif self.Type == COIL_RESET: lbessard@42: typetext = "R" lbessard@42: if typetext != "": lbessard@165: self.TypeSize = self.Parent.GetTextExtent(typetext) lbessard@42: else: lbessard@42: self.TypeSize = 0, 0 lbessard@42: etisserant@0: # Refresh the coil bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: # Calculate the size of the name outside the coil lbessard@165: text_width, text_height = self.Parent.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] lbessard@64: self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) lbessard@27: lbessard@27: # Returns the block minimum size lbessard@27: def GetMinSize(self): lbessard@27: return LD_ELEMENT_SIZE 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 lbessard@27: def GetConnector(self, position, name = None): lbessard@27: # if a name is given lbessard@27: if name: lbessard@27: # Test input and output connector lbessard@27: if self.Input and name == self.Input.GetName(): lbessard@27: return self.Input lbessard@27: if self.Output and name == self.Output.GetName(): lbessard@27: return self.Output 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: lbessard@27: # Refresh the positions of the block connectors lbessard@27: def RefreshConnectors(self): lbessard@145: scaling = self.Parent.GetScaling() lbessard@145: position = self.Size[1] / 2 + 1 lbessard@145: if scaling is not None: lbessard@145: position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y lbessard@145: self.Input.SetPosition(wx.Point(0, position)) lbessard@145: self.Output.SetPosition(wx.Point(self.Size[0], position)) lbessard@27: self.RefreshConnected() lbessard@27: etisserant@0: # Changes the coil name etisserant@0: def SetName(self, name): etisserant@0: self.Name = name lbessard@42: self.RefreshNameSize() 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 lbessard@42: self.RefreshTypeSize() 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 lbessard@27: def OnLeftDClick(self, event, dc, scaling): etisserant@0: # Edit the coil properties etisserant@0: self.Parent.EditCoilContent(self) etisserant@0: lbessard@127: # Method called when a RightUp event have been generated lbessard@127: def OnRightUp(self, event, dc, scaling): lbessard@127: # Popup the default menu lbessard@127: self.Parent.PopupDefaultMenu() lbessard@127: 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: lbessard@140: # Draws the highlightment of this element if it is highlighted lbessard@140: def DrawHighlightment(self, dc): lbessard@144: dc.SetPen(wx.Pen(HIGHLIGHTCOLOR, 6, wx.SOLID)) lbessard@144: dc.SetBrush(wx.TRANSPARENT_BRUSH) lbessard@144: dc.SetLogicalFunction(wx.AND) lbessard@144: # Draw a two circle arcs for representing the coil lbessard@144: 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) lbessard@144: 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) lbessard@144: dc.SetLogicalFunction(wx.COPY) lbessard@140: lbessard@231: def AddError(self, infos, start, end): lbessard@231: self.Errors[infos[0]] = (start[1], end[1]) lbessard@231: etisserant@0: # Draws coil etisserant@0: def Draw(self, dc): lbessard@144: Graphic_Element.Draw(self, dc) lbessard@64: dc.SetPen(wx.Pen(wx.BLACK, 2, wx.SOLID)) lbessard@64: dc.SetBrush(wx.TRANSPARENT_BRUSH) lbessard@213: lbessard@213: # Compiling coil type modifier symbol 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" lbessard@213: lbessard@213: if getattr(dc, "printing", False) and not isinstance(dc, wx.PostScriptDC): lbessard@213: # Draw an clipped ellipse for representing the coil lbessard@213: clipping_box = dc.GetClippingBox() lbessard@213: dc.SetClippingRegion(self.Pos.x - 1, self.Pos.y, self.Size[0] + 2, self.Size[1] + 1) lbessard@213: dc.DrawEllipse(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) lbessard@213: dc.DestroyClippingRegion() lbessard@213: if clipping_box != (0, 0, 0, 0): lbessard@213: dc.SetClippingRegion(*clipping_box) lbessard@213: name_size = dc.GetTextExtent(self.Name) lbessard@213: if typetext != "": lbessard@213: type_size = dc.GetTextExtent(typetext) lbessard@213: else: lbessard@213: # Draw a two ellipse arcs for representing the coil lbessard@213: 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) lbessard@213: 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) lbessard@213: # Draw a point to avoid hole in left arc lbessard@213: if not getattr(dc, "printing", False): lbessard@213: dc.SetPen(wx.BLACK_PEN) lbessard@213: dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1) lbessard@213: name_size = self.NameSize lbessard@213: if typetext != "": lbessard@213: type_size = self.TypeSize lbessard@213: lbessard@213: # Draw coil name lbessard@231: name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, lbessard@231: self.Pos.y - (name_size[1] + 2)) lbessard@231: dc.DrawText(self.Name, name_pos[0], name_pos[1]) lbessard@213: # Draw the modifier symbol in the middle of coil etisserant@0: if typetext != "": lbessard@231: type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1, lbessard@231: self.Pos.y + (self.Size[1] - type_size[1]) / 2) lbessard@231: dc.DrawText(typetext, type_pos[0], type_pos[1]) etisserant@0: # Draw input and output connectors etisserant@0: self.Input.Draw(dc) etisserant@0: self.Output.Draw(dc) lbessard@231: if "reference" in self.Errors: lbessard@231: HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1]) lbessard@231: if typetext != "" and ("negated" in self.Errors or "rising" in self.Errors or "falling" in self.Errors): lbessard@231: HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1]) lbessard@231: lbessard@231: