graphics/LD_Objects.py
author lbessard
Wed, 30 Jan 2008 09:52:57 +0100
changeset 164 0fb64076d3f5
parent 162 e746ff4aa8be
child 165 e464a4e4e06d
permissions -rw-r--r--
Adding support for the new version of xmlclass
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard. 
#
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#General Public License for more details.
#
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import wx

from GraphicCommons import *
from plcopen.structures import *

#-------------------------------------------------------------------------------
#                         Ladder Diagram PowerRail
#-------------------------------------------------------------------------------

"""
Class that implements the graphic representation of a power rail
"""

class LD_PowerRail(Graphic_Element):
    
    # Create a new power rail
    def __init__(self, parent, type, id = None, connectors = [True]):
        Graphic_Element.__init__(self, parent)
        self.Type = None
        self.Connectors = []
        self.RealConnectors = None
        self.Id = id
        self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
        if len(connectors) < 1:
            connectors = [True]
        self.SetType(type, connectors)
        
    # Destructor
    def __del__(self):
        self.Connectors = []
    
    # Make a clone of this LD_PowerRail
    def Clone(self, parent, id = None, pos = None):
        powerrail = LD_PowerRail(parent, self.Type, id)
        powerrail.SetSize(self.Size[0], self.Size[1])
        if pos is not None:
            powerrail.SetPosition(pos.x, pos.y)
        powerrail.Connectors = []
        for connector in self.Connectors:
            if connector is not None:
                powerrail.Connectors.append(connector.Clone(powerrail))
            else:
                powerrail.Connectors.append(None)
        return powerrail
    
    # Returns the RedrawRect
    def GetRedrawRect(self, movex = 0, movey = 0):
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
        for connector in self.Connectors:
            if connector is not None:
                rect = rect.Union(connector.GetRedrawRect(movex, movey))
        if movex != 0 or movey != 0:
            for connector in self.Connectors:
                if connector is not None and connector.IsConnected():
                    rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey))
        return rect
    
    # Forbids to change the power rail size
    def SetSize(self, width, height):
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
            Graphic_Element.SetSize(self, width, height)
            self.RefreshConnectors()
    
    # Forbids to select a power rail
    def HitTest(self, pt):
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
            return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None
        return False
    
    # Forbids to select a power rail
    def IsInSelection(self, rect):
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
            return Graphic_Element.IsInSelection(self, rect)
        return False
    
    # Deletes this power rail by calling the appropriate method
    def Delete(self):
        self.Parent.DeletePowerRail(self)
    
    # Unconnect all connectors
    def Clean(self):
        for connector in self.Connectors:
            if connector:
                connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
                
    # Refresh the power rail bounding box
    def RefreshBoundingBox(self):
        dc = wx.ClientDC(self.Parent)
        self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
    
    # Refresh the power rail size
    def RefreshSize(self):
        self.Size = wx.Size(LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors))
        self.RefreshBoundingBox()
    
    # Returns the block minimum size
    def GetMinSize(self):
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
            return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1]
        else:
            return LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors)
    
    # Add a connector or a blank to this power rail at the last place
    def AddConnector(self, connector = True):
        self.InsertConnector(len(self.Connectors), connector)
    
    # Add a connector or a blank to this power rail at the place given
    def InsertConnector(self, idx, connector = True):
        if connector:
            if self.Type == LEFTRAIL:
                connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST)
            elif self.Type == RIGHTRAIL:
                connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST)
            self.Connectors.insert(idx, connector)
        else:
            self.Connectors.insert(idx, None)
        self.RefreshSize()
        self.RefreshConnectors()
    
    # Moves the divergence connector given
    def MoveConnector(self, connector, movey):
        position = connector.GetRelPosition()
        connector.SetPosition(wx.Point(position.x, position.y + movey))
        miny = self.Size[1]
        maxy = 0
        for connect in self.Connectors:
            connect_pos = connect.GetRelPosition()
            miny = min(miny, connect_pos.y - self.Extensions[0])
            maxy = max(maxy, connect_pos.y - self.Extensions[0])
        min_pos = self.Pos.y + miny
        self.Pos.y = min(min_pos, self.Pos.y)
        if min_pos == self.Pos.y:
            for connect in self.Connectors:
                connect_pos = connect.GetRelPosition()
                connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y - miny))
        self.Connectors.sort(lambda x, y: x.Pos.y.__cmp__(y.Pos.y))
        maxy = 0
        for connect in self.Connectors:
            connect_pos = connect.GetRelPosition()
            maxy = max(maxy, connect_pos.y)
        self.Size[1] = max(maxy + self.Extensions[1], self.Size[1])
        connector.MoveConnected()
        self.RefreshBoundingBox()
    
    # Returns the index in connectors list for the connector given
    def GetConnectorIndex(self, connector):
        if connector in self.Connectors:
            return self.Connectors.index(connector)
        return None
    
    # Returns if there is a connector in connectors list at the index given
    def IsNullConnector(self, idx):
        if idx < len(self.Connectors):
            return self.Connectors[idx] == None
        return False
    
    # Delete the connector or blank from connectors list at the index given
    def DeleteConnector(self, idx):
        self.Connectors.pop(idx)
        self.RefreshConnectors()
        self.RefreshSize()
    
    # Refresh the positions of the power rail connectors
    def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
            height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
            interval = float(height) / float(max(len(self.Connectors) - 1, 1))
            for i, connector in enumerate(self.Connectors):
                if self.RealConnectors:
                    position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
                else:
                    position = self.Extensions[0] + int(round(i * interval))
                if scaling is not None:
                    position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
                if self.Type == LEFTRAIL:
                    connector.SetPosition(wx.Point(self.Size[0], position))
                elif self.Type == RIGHTRAIL:
                    connector.SetPosition(wx.Point(0, position))
        else:
            position = self.Extensions[0]
            for connector in self.Connectors:
                if connector:
                    if scaling is not None:
                        ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
                    else:
                        ypos = position
                    if self.Type == LEFTRAIL:
                        connector.SetPosition(wx.Point(self.Size[0], ypos))
                    elif self.Type == RIGHTRAIL:
                        connector.SetPosition(wx.Point(0, ypos))
                position += LD_LINE_SIZE
        self.RefreshConnected()
    
    # Refresh the position of wires connected to power rail
    def RefreshConnected(self, exclude = []):
        for connector in self.Connectors:
            if connector:
                connector.MoveConnected(exclude)
    
    # Returns the power rail connector that starts with the point given if it exists 
    def GetConnector(self, position, name = None):
        # if a name is given
        if name:
            # Test each connector if it exists
            for connector in self.Connectors:
                if connector and name == connector.GetName():
                    return connector
        for connector in self.Connectors:
            if connector:
                connector_pos = connector.GetRelPosition()
                if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y:
                    return connector
        return None
    
    # Returns all the power rail connectors 
    def GetConnectors(self):
        return [connector for connector in self.Connectors if connector]
    
    # Test if point given is on one of the power rail connectors
    def TestConnector(self, pt, exclude=True):
        for connector in self.Connectors:
            if connector and connector.TestPoint(pt, exclude):
                return connector
        return None
    
    # Returns the power rail type
    def SetType(self, type, connectors):
        if type != self.Type or len(self.Connectors) != len(connectors):
            # Create a connector or a blank according to 'connectors' and add it in
            # the connectors list
            self.Type = type
            self.Clean()
            self.Connectors = []
            for connector in connectors:
                self.AddConnector(connector)
            self.RefreshSize()
    
    # Returns the power rail type
    def GetType(self):
        return self.Type
    
    # Method called when a LeftDown event have been generated
    def OnLeftDown(self, event, dc, scaling):
        self.RealConnectors = []
        height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
        for connector in self.Connectors:
            position = connector.GetRelPosition()
            self.RealConnectors.append(float(position.y - self.Extensions[0])/float(max(1, height)))
        Graphic_Element.OnLeftDown(self, event, dc, scaling)
    
    # Method called when a LeftUp event have been generated
    def OnLeftUp(self, event, dc, scaling):
        Graphic_Element.OnLeftUp(self, event, dc, scaling)
        self.RealConnectors = None
    
    # Method called when a LeftDown event have been generated
    def OnRightDown(self, event, dc, scaling):
        pos = GetScaledEventPosition(event, dc, scaling)
        # Test if a connector have been handled
        connector = self.TestConnector(pos, False)
        if connector:
            self.Handle = (HANDLE_CONNECTOR, connector)
            self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND))
            self.Selected = False
            # Initializes the last position
            self.oldPos = GetScaledEventPosition(event, dc, scaling)
        else:
            Graphic_Element.OnRightDown(self, event, dc, scaling)
    
    # Method called when a LeftDClick event have been generated
    def OnLeftDClick(self, event, dc, scaling):
        # Edit the powerrail properties
        self.Parent.EditPowerRailContent(self)
    
    # Method called when a RightUp event have been generated
    def OnRightUp(self, event, dc, scaling):
        handle_type, handle = self.Handle
        if handle_type == HANDLE_CONNECTOR:
            wires = handle.GetWires()
            if len(wires) == 1:
                if handle == wires[0][0].StartConnected:
                    block = wires[0][0].EndConnected.GetParentBlock()
                else:
                    block = wires[0][0].StartConnected.GetParentBlock()
                block.RefreshModel(False)
            Graphic_Element.OnRightUp(self, event, dc, scaling)
        else:
            self.Parent.PopupDefaultMenu()
    
    # Refreshes the powerrail state according to move defined and handle selected
    def ProcessDragging(self, movex, movey, scaling):
        handle_type, handle = self.Handle
        # A connector has been handled
        if handle_type == HANDLE_CONNECTOR:
            movey = max(-self.BoundingBox.y, movey)
            if scaling is not None:
                position = handle.GetRelPosition()
                movey = round(float(self.Pos.y + position.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y - position.y
            self.MoveConnector(handle, movey)
            return 0, movey
        else:
            return Graphic_Element.ProcessDragging(self, movex, movey, scaling)
    
    # Refreshes the power rail model
    def RefreshModel(self, move=True):
        self.Parent.RefreshPowerRailModel(self)
        # If power rail has moved and power rail is of type LEFT, refresh the model 
        # of wires connected to connectors
        if move and self.Type == LEFTRAIL:
            for connector in self.Connectors:
                if connector:
                    connector.RefreshWires()
    
    # Draws power rail
    def Draw(self, dc):
        Graphic_Element.Draw(self, dc)
        dc.SetPen(wx.BLACK_PEN)
        dc.SetBrush(wx.BLACK_BRUSH)
        # Draw a rectangle with the power rail size
        dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
        # Draw connectors
        for connector in self.Connectors:
            if connector:
                connector.Draw(dc)
        

#-------------------------------------------------------------------------------
#                         Ladder Diagram Contact
#-------------------------------------------------------------------------------

"""
Class that implements the graphic representation of a contact
"""

class LD_Contact(Graphic_Element):
    
    # Create a new contact
    def __init__(self, parent, type, name, id = None):
        Graphic_Element.__init__(self, parent)
        self.Type = type
        self.Name = name
        self.Id = id
        self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
        # Create an input and output connector
        self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST)
        self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST)
        self.RefreshNameSize()
        self.RefreshTypeSize()
    
    # Destructor
    def __del__(self):
        self.Input = None
        self.Output = None
    
    # Make a clone of this LD_Contact
    def Clone(self, parent, id = None, pos = None):
        contact = LD_Contact(parent, self.Type, self.Name, id)
        contact.SetSize(self.Size[0], self.Size[1])
        if pos is not None:
            contact.SetPosition(pos.x, pos.y)
        contact.Input = self.Input.Clone(contact)
        contact.Output = self.Output.Clone(contact)
        return contact
    
    # Returns the RedrawRect
    def GetRedrawRect(self, movex = 0, movey = 0):
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
        rect = rect.Union(self.Input.GetRedrawRect(movex, movey))
        rect = rect.Union(self.Output.GetRedrawRect(movex, movey))
        if movex != 0 or movey != 0:
            if self.Input.IsConnected():
                rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
            if self.Output.IsConnected():
                rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
        return rect
    
    # Forbids to change the contact size
    def SetSize(self, width, height):
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
            Graphic_Element.SetSize(self, width, height)
            self.RefreshConnectors()
    
    # Delete this contact by calling the appropriate method
    def Delete(self):
        self.Parent.DeleteContact(self)
    
    # Unconnect input and output
    def Clean(self):
        self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
        self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
    
    # Refresh the size of text for name
    def RefreshNameSize(self):
        dc = wx.ClientDC(self.Parent)
        if self.Name != "":
            self.NameSize = dc.GetTextExtent(self.Name)
        else:
            self.NameSize = 0, 0
    
    # Refresh the size of text for type
    def RefreshTypeSize(self):
        dc = wx.ClientDC(self.Parent)
        typetext = ""
        if self.Type == CONTACT_REVERSE:
            typetext = "/"
        elif self.Type == CONTACT_RISING:
            typetext = "P"
        elif self.Type == CONTACT_FALLING:
            typetext = "N"
        if typetext != "":
            self.TypeSize = dc.GetTextExtent(typetext)
        else:
            self.TypeSize = 0, 0
    
    # Refresh the contact bounding box
    def RefreshBoundingBox(self):
        dc = wx.ClientDC(self.Parent)
        # Calculate the size of the name outside the contact
        text_width, text_height = dc.GetTextExtent(self.Name)
        # Calculate the bounding box size
        if self.Name != "":
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
            bbx_width = max(self.Size[0], text_width)
            bbx_y = self.Pos.y - (text_height + 2)
            bbx_height = self.Size[1] + (text_height + 2)
        else:
            bbx_x = self.Pos.x
            bbx_width = self.Size[0]
            bbx_y = self.Pos.y
            bbx_height = self.Size[1]
        self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
    
    # Returns the block minimum size
    def GetMinSize(self):
        return LD_ELEMENT_SIZE
    
    # Refresh the position of wire connected to contact
    def RefreshConnected(self, exclude = []):
        self.Input.MoveConnected(exclude)
        self.Output.MoveConnected(exclude)
    
    # Returns the contact connector that starts with the point given if it exists 
    def GetConnector(self, position, name = None):
        # if a name is given
        if name:
            # Test input and output connector
            if name == self.Input.GetName():
                return self.Input
            if name == self.Output.GetName():
                return self.Output
        # Test input connector
        input_pos = self.Input.GetRelPosition()
        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
            return self.Input
        # Test output connector
        output_pos = self.Output.GetRelPosition()
        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
            return self.Output
        return None
    
    # Returns input and output contact connectors 
    def GetConnectors(self):
        return {"input":self.Input,"output":self.Output}
    
    # Test if point given is on contact input or output connector
    def TestConnector(self, pt, exclude=True):
        # Test input connector
        if self.Input.TestPoint(pt, exclude):
            return self.Input
        # Test output connector
        if self.Output.TestPoint(pt, exclude):
            return self.Output
        return None

    # Refresh the positions of the block connectors
    def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        position = self.Size[1] / 2 + 1
        if scaling is not None:
            position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
        self.Input.SetPosition(wx.Point(0, position))
        self.Output.SetPosition(wx.Point(self.Size[0], position))
        self.RefreshConnected()

    # Changes the contact name
    def SetName(self, name):
        self.Name = name
        self.RefreshNameSize()

    # Returns the contact name
    def GetName(self):
        return self.Name

    # Changes the contact type
    def SetType(self, type):
        self.Type = type
        self.RefreshTypeSize()

    # Returns the contact type
    def GetType(self):
        return self.Type
    
    # Method called when a LeftDClick event have been generated
    def OnLeftDClick(self, event, dc, scaling):
        # Edit the contact properties
        self.Parent.EditContactContent(self)
    
    # Method called when a RightUp event have been generated
    def OnRightUp(self, event, dc, scaling):
        # Popup the default menu
        self.Parent.PopupDefaultMenu()
    
    # Refreshes the contact model
    def RefreshModel(self, move=True):
        self.Parent.RefreshContactModel(self)
        # If contact has moved, refresh the model of wires connected to output
        if move:
            self.Output.RefreshWires()
    
    # Draws the highlightment of this element if it is highlighted
    def DrawHighlightment(self, dc):
        dc.SetPen(wx.Pen(HIGHLIGHTCOLOR))
        dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
        dc.SetLogicalFunction(wx.AND)
        # Draw two rectangles for representing the contact
        dc.DrawRectangle(self.Pos.x - 2, self.Pos.y - 2, 6, self.Size[1] + 5)
        dc.DrawRectangle(self.Pos.x + self.Size[0] - 3, self.Pos.y - 2, 6, self.Size[1] + 5)
        dc.SetLogicalFunction(wx.COPY)
    
    # Draws contact
    def Draw(self, dc):
        Graphic_Element.Draw(self, dc)
        dc.SetPen(wx.BLACK_PEN)
        dc.SetBrush(wx.BLACK_BRUSH)
        # Draw two rectangles for representing the contact
        dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
        dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
        # Draw contact name
        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
                self.Pos.y - (self.NameSize[1] + 2))
        # Draw the modifier symbol in the middle of contact
        typetext = ""
        if self.Type == CONTACT_REVERSE:
            typetext = "/"
        elif self.Type == CONTACT_RISING:
            typetext = "P"
        elif self.Type == CONTACT_FALLING:
            typetext = "N"
        if typetext != "":
            dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1,
                    self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2)
        # Draw input and output connectors
        self.Input.Draw(dc)
        self.Output.Draw(dc)
        

#-------------------------------------------------------------------------------
#                         Ladder Diagram Coil
#-------------------------------------------------------------------------------

"""
Class that implements the graphic representation of a coil
"""

class LD_Coil(Graphic_Element):
    
    # Create a new coil
    def __init__(self, parent, type, name, id = None):
        Graphic_Element.__init__(self, parent)
        self.Type = type
        self.Name = name
        self.Id = id
        self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
        # Create an input and output connector
        self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST)
        self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST)
        self.RefreshNameSize()
        self.RefreshTypeSize()
        
    # Destructor
    def __del__(self):
        self.Input = None
        self.Output = None
    
    # Make a clone of this LD_Coil
    def Clone(self, parent, id = None, pos = None):
        coil = LD_Coil(parent, self.Type, self.Name, id)
        coil.SetSize(self.Size[0], self.Size[1])
        if pos is not None:
            coil.SetPosition(pos.x, pos.y)
        coil.Input = self.Input.Clone(coil)
        coil.Output = self.Output.Clone(coil)
        return coil
    
    # Returns the RedrawRect
    def GetRedrawRect(self, movex = 0, movey = 0):
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
        rect = rect.Union(self.Input.GetRedrawRect(movex, movey))
        rect = rect.Union(self.Output.GetRedrawRect(movex, movey))
        if movex != 0 or movey != 0:
            if self.Input.IsConnected():
                rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
            if self.Output.IsConnected():
                rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
        return rect
    
    # Forbids to change the contact size
    def SetSize(self, width, height):
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
            Graphic_Element.SetSize(self, width, height)
            self.RefreshConnectors()
    
    # Delete this coil by calling the appropriate method
    def Delete(self):
        self.Parent.DeleteCoil(self)
    
    # Unconnect input and output
    def Clean(self):
        self.Input.UnConnect()
        self.Output.UnConnect()
                
    # Refresh the size of text for name
    def RefreshNameSize(self):
        dc = wx.ClientDC(self.Parent)
        if self.Name != "":
            self.NameSize = dc.GetTextExtent(self.Name)
        else:
            self.NameSize = 0, 0
    
    # Refresh the size of text for type
    def RefreshTypeSize(self):
        dc = wx.ClientDC(self.Parent)
        typetext = ""
        if self.Type == COIL_REVERSE:
            typetext = "/"
        elif self.Type == COIL_SET:
            typetext = "S"
        elif self.Type == COIL_RESET:
            typetext = "R"
        if typetext != "":
            self.TypeSize = dc.GetTextExtent(typetext)
        else:
            self.TypeSize = 0, 0
    
    # Refresh the coil bounding box
    def RefreshBoundingBox(self):
        dc = wx.ClientDC(self.Parent)
        # Calculate the size of the name outside the coil
        text_width, text_height = dc.GetTextExtent(self.Name)
        # Calculate the bounding box size
        if self.Name != "":
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
            bbx_width = max(self.Size[0], text_width)
            bbx_y = self.Pos.y - (text_height + 2)
            bbx_height = self.Size[1] + (text_height + 2)
        else:
            bbx_x = self.Pos.x
            bbx_width = self.Size[0]
            bbx_y = self.Pos.y
            bbx_height = self.Size[1]
        self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
        
    # Returns the block minimum size
    def GetMinSize(self):
        return LD_ELEMENT_SIZE
    
    # Refresh the position of wire connected to coil
    def RefreshConnected(self, exclude = []):
        self.Input.MoveConnected(exclude)
        self.Output.MoveConnected(exclude)
    
    # Returns the coil connector that starts with the point given if it exists 
    def GetConnector(self, position, name = None):
        # if a name is given
        if name:
            # Test input and output connector
            if self.Input and name == self.Input.GetName():
                return self.Input
            if self.Output and name == self.Output.GetName():
                return self.Output
        # Test input connector
        input_pos = self.Input.GetRelPosition()
        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
            return self.Input
        # Test output connector
        output_pos = self.Output.GetRelPosition()
        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
            return self.Output
        return None
    
    # Returns input and output coil connectors 
    def GetConnectors(self):
        return {"input":self.Input,"output":self.Output}
    
    # Test if point given is on coil input or output connector
    def TestConnector(self, pt, exclude=True):
        # Test input connector
        if self.Input.TestPoint(pt, exclude):
            return self.Input
        # Test output connector
        if self.Output.TestPoint(pt, exclude):
            return self.Output
        return None
    
    # Refresh the positions of the block connectors
    def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        position = self.Size[1] / 2 + 1
        if scaling is not None:
            position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
        self.Input.SetPosition(wx.Point(0, position))
        self.Output.SetPosition(wx.Point(self.Size[0], position))
        self.RefreshConnected()
    
    # Changes the coil name
    def SetName(self, name):
        self.Name = name
        self.RefreshNameSize()

    # Returns the coil name
    def GetName(self):
        return self.Name
    
    # Changes the coil type
    def SetType(self, type):
        self.Type = type
        self.RefreshTypeSize()
    
    # Returns the coil type
    def GetType(self):
        return self.Type
    
    # Method called when a LeftDClick event have been generated
    def OnLeftDClick(self, event, dc, scaling):
        # Edit the coil properties
        self.Parent.EditCoilContent(self)
    
    # Method called when a RightUp event have been generated
    def OnRightUp(self, event, dc, scaling):
        # Popup the default menu
        self.Parent.PopupDefaultMenu()
    
    # Refreshes the coil model
    def RefreshModel(self, move=True):
        self.Parent.RefreshCoilModel(self)
        # If coil has moved, refresh the model of wires connected to output
        if move:
            self.Output.RefreshWires()
    
    # Draws the highlightment of this element if it is highlighted
    def DrawHighlightment(self, dc):
        dc.SetPen(wx.Pen(HIGHLIGHTCOLOR, 6, wx.SOLID))
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        dc.SetLogicalFunction(wx.AND)
        # Draw a two circle arcs for representing the coil
        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)
        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)
        dc.SetLogicalFunction(wx.COPY)
    
    # Draws coil
    def Draw(self, dc):
        Graphic_Element.Draw(self, dc)
        dc.SetPen(wx.Pen(wx.BLACK, 2, wx.SOLID))
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        # Draw a two circle arcs for representing the coil
        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)
        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)
        dc.SetPen(wx.BLACK_PEN)
        dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
        # Draw coil name
        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
                self.Pos.y - (self.NameSize[1] + 2))
        # Draw the modifier symbol in the middle of coil
        typetext = ""
        if self.Type == COIL_REVERSE:
            typetext = "/"
        elif self.Type == COIL_SET:
            typetext = "S"
        elif self.Type == COIL_RESET:
            typetext = "R"
        if typetext != "":
            dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1,
                    self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2)
        # Draw input and output connectors
        self.Input.Draw(dc)
        self.Output.Draw(dc)