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: 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@64: dc = wx.ClientDC(self.Parent) lbessard@64: self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1] + 1) etisserant@0: etisserant@0: # Refresh the power rail size etisserant@0: def RefreshSize(self): lbessard@64: self.Size = wx.Size(2, 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@96: return 2, self.Extensions[0] + self.Extensions[1] lbessard@96: else: lbessard@96: return 2, 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@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@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@71: position = connector.GetRelPosition() lbessard@80: if self.Type == LEFTRAIL: lbessard@80: if self.RealConnectors: lbessard@71: connector.SetPosition(wx.Point(self.Size[0], self.Extensions[0] + int(round(self.RealConnectors[i] * height)))) lbessard@80: else: lbessard@96: connector.SetPosition(wx.Point(self.Size[0], self.Extensions[0] + int(round(i * interval)))) lbessard@80: elif self.Type == RIGHTRAIL: lbessard@80: if self.RealConnectors: lbessard@71: connector.SetPosition(wx.Point(0, self.Extensions[0] + int(round(self.RealConnectors[i] * height)))) lbessard@80: else: lbessard@96: connector.SetPosition(wx.Point(0, self.Extensions[0] + int(round(i * interval)))) lbessard@71: else: lbessard@71: position = self.Extensions[0] lbessard@71: for connector in self.Connectors: lbessard@71: if connector: lbessard@71: if self.Type == LEFTRAIL: lbessard@71: connector.SetPosition(wx.Point(self.Size[0], position)) lbessard@71: elif self.Type == RIGHTRAIL: lbessard@71: connector.SetPosition(wx.Point(0, position)) 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@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@71: self.RealConnectors = [] lbessard@71: height = self.Size[1] - self.Extensions[0] - self.Extensions[1] lbessard@71: for connector in self.Connectors: lbessard@71: position = connector.GetRelPosition() lbessard@71: self.RealConnectors.append(float(position.y - self.Extensions[0])/float(max(1, height))) lbessard@27: Graphic_Element.OnLeftDown(self, event, dc, scaling) lbessard@27: lbessard@27: # Method called when a LeftUp event have been generated lbessard@27: def OnLeftUp(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@27: Graphic_Element.OnLeftUp(self, event, dc, scaling) lbessard@96: self.RealConnectors = None lbessard@27: lbessard@28: # Method called when a LeftDClick event have been generated lbessard@28: def OnLeftDClick(self, event, dc, scaling): lbessard@28: # Edit the powerrail properties lbessard@28: self.Parent.EditPowerRailContent(self) lbessard@28: lbessard@27: # Method called when a RightUp event have been generated lbessard@27: def OnRightUp(self, event, dc, scaling): lbessard@27: pos = GetScaledEventPosition(event, dc, scaling) lbessard@27: # Popup the menu with special items for a block and a connector if one is handled lbessard@27: connector = self.TestConnector(pos, False) lbessard@27: if connector: lbessard@27: self.Handle = (HANDLE_CONNECTOR, connector) lbessard@27: # self.Parent.PopupDivergenceMenu(True) lbessard@27: #else: lbessard@27: # Popup the divergence menu without delete branch lbessard@27: # self.Parent.PopupDivergenceMenu(False) lbessard@27: lbessard@27: # Refreshes the divergence state according to move defined and handle selected lbessard@27: def ProcessDragging(self, movex, movey): lbessard@27: handle_type, handle = self.Handle lbessard@27: # A connector has been handled lbessard@27: if handle_type == HANDLE_CONNECTOR: lbessard@27: self.MoveConnector(handle, movey) lbessard@27: else: lbessard@27: Graphic_Element.ProcessDragging(self, movex, movey) 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@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.BLACK_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 lbessard@64: self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) 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: 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@64: dc = wx.ClientDC(self.Parent) lbessard@42: if self.Name != "": lbessard@42: self.NameSize = dc.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@64: dc = wx.ClientDC(self.Parent) 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@42: self.TypeSize = dc.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): lbessard@64: dc = wx.ClientDC(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] 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@64: self.Input.SetPosition(wx.Point(0, self.Size[1] / 2 + 1)) lbessard@64: self.Output.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2 + 1)) 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: 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): lbessard@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.BLACK_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 lbessard@42: dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, lbessard@42: self.Pos.y - (self.NameSize[1] + 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 != "": lbessard@42: dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1, lbessard@42: self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 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 lbessard@64: self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) 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: 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 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@64: dc = wx.ClientDC(self.Parent) lbessard@42: if self.Name != "": lbessard@42: self.NameSize = dc.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@64: dc = wx.ClientDC(self.Parent) 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@42: self.TypeSize = dc.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): lbessard@64: dc = wx.ClientDC(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] 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@64: self.Input.SetPosition(wx.Point(0, self.Size[1] / 2 + 1)) lbessard@64: self.Output.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2 + 1)) 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: 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): lbessard@64: dc.SetPen(wx.Pen(wx.BLACK, 2, wx.SOLID)) lbessard@64: dc.SetBrush(wx.TRANSPARENT_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) lbessard@64: dc.SetPen(wx.BLACK_PEN) etisserant@0: dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1) etisserant@0: # Draw coil name lbessard@42: dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, lbessard@42: self.Pos.y - (self.NameSize[1] + 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 != "": lbessard@42: dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1, lbessard@42: self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 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)