graphics/LD_Objects.py
author etisserant
Tue, 05 Jun 2007 14:32:01 +0200
changeset 14 cd0133ed377b
parent 8 7ceec5c40d77
child 27 dae55dd9ee14
permissions -rw-r--r--
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     5
#based on the plcopen standard. 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     6
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     7
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     8
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    10
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
diff changeset
    12
#modify it under the terms of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    15
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    19
#Lesser General Public License for more details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
diff changeset
    21
#You should have received a copy of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    24
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    25
from wxPython.wx import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    26
import wx
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    27
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    28
from GraphicCommons import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    29
from plcopen.structures import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    30
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    31
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    32
#                         Ladder Diagram PowerRail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    33
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    34
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    35
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    36
Class that implements the graphic representation of a power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    37
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    38
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    39
class LD_PowerRail(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    40
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    41
    # Create a new power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    42
    def __init__(self, parent, type, id = None, connectors = [True]):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    43
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    44
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    45
        self.Id = id
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    46
        # Create a connector or a blank according to 'connectors' and add it in
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    47
        # the connectors list
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    48
        self.Connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    49
        for connector in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    50
            self.AddConnector(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    51
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    52
        
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    53
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    54
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    55
        self.Connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    56
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    57
    # Forbids to change the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    58
    def SetSize(self, width, height):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    59
        pass
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    60
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    61
    # Forbids to select a power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    62
    def HitTest(self, pt):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    63
        return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    64
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    65
    # Deletes this power rail by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    66
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    67
        self.Parent.DeletePowerRail(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    68
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    69
    # Unconnect all connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    70
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    71
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    72
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    73
                connector.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    74
                
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    75
    # Refresh the power rail bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    76
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    77
        dc = wxClientDC(self.Parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    78
        if self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    79
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    80
        elif self.Type == RIGHTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    81
            bbx_x = self.Pos.x - CONNECTOR_SIZE
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    82
        self.BoundingBox = wxRect(bbx_x, self.Pos.y, self.Size[0] + CONNECTOR_SIZE + 1, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    83
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    84
    # Refresh the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    85
    def RefreshSize(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    86
        self.Size = wxSize(2, LD_LINE_SIZE * len(self.Connectors))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    87
        self.RefreshBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    88
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    89
    # Add a connector or a blank to this power rail at the last place
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    90
    def AddConnector(self, connector = True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    91
        self.InsertConnector(len(self.Connectors), connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    92
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    93
    # Add a connector or a blank to this power rail at the place given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    94
    def InsertConnector(self, idx, connector = True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    95
        if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    96
            if self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    97
                connector = Connector(self, "", "BOOL", wxPoint(2, 0), EAST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    98
            elif self.Type == RIGHTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    99
                connector = Connector(self, "", "BOOL", wxPoint(0, 0), WEST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   100
            self.Connectors.insert(idx, connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   101
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   102
            self.Connectors.insert(idx, None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   103
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   104
        self.RefreshConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   105
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   106
    # Returns the index in connectors list for the connector given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   107
    def GetConnectorIndex(self, connector):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   108
        if connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   109
            return self.Connectors.index(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   110
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   111
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   112
    # Returns if there is a connector in connectors list at the index given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   113
    def IsNullConnector(self, idx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   114
        if idx < len(self.Connectors):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   115
            return self.Connectors[idx] == None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   116
        return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   117
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   118
    # Delete the connector or blank from connectors list at the index given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   119
    def DeleteConnector(self, idx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   120
        self.Connectors.pop(idx)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   121
        self.RefreshConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   122
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   123
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   124
    # Refresh the positions of the power rail connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   125
    def RefreshConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   126
        position = LD_LINE_SIZE / 2
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   127
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   128
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   129
                if self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   130
                    connector.SetPosition(wxPoint(self.Size[0], position))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   131
                elif self.Type == RIGHTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   132
                    connector.SetPosition(wxPoint(0, position))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   133
            position += LD_LINE_SIZE
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   134
        self.RefreshConnected()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   135
    
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 5
diff changeset
   136
    # Refresh the position of wires connected to power rail
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   137
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   138
        for connector in self.Connectors:
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   139
            if connector:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   140
                connector.MoveConnected(exclude)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   141
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   142
    # Returns the power rail connector that starts with the point given if it exists 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   143
    def GetConnector(self, position):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   144
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   145
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   146
                connector_pos = connector.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   147
                if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   148
                    return connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   149
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   150
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   151
    # Returns all the power rail connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   152
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   153
        return [connector for connector in self.Connectors if connector]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   154
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   155
    # Test if point given is on one of the power rail connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   156
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   157
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   158
            if connector and connector.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   159
                return connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   160
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   161
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   162
    # Returns the power rail type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   163
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   164
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   165
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   166
    # Refreshes the power rail model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   167
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   168
        self.Parent.RefreshPowerRailModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   169
        # If power rail has moved and power rail is of type LEFT, refresh the model 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   170
        # of wires connected to connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   171
        if move and self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   172
            for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   173
                if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   174
                    connector.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   175
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   176
    # Draws power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   177
    def Draw(self, dc):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   178
        dc.SetPen(wxBLACK_PEN)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   179
        dc.SetBrush(wxBLACK_BRUSH)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   180
        # Draw a rectangle with the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   181
        dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   182
        # Draw connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   183
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   184
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   185
                connector.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   186
        Graphic_Element.Draw(self, dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   188
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   189
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   190
#                         Ladder Diagram Contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   191
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   192
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   193
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   194
Class that implements the graphic representation of a contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   195
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   196
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   197
class LD_Contact(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   198
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   199
    # Create a new contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   200
    def __init__(self, parent, type, name, id = None):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   201
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   202
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   203
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   204
        self.Id = id
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   205
        self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   206
        # Create an input and output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   207
        self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   208
        self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   209
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   210
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   211
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   212
        self.Input = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   213
        self.Output = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   214
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   215
    # Forbids to change the contact size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   216
    def SetSize(self, width, height):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   217
        pass
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   218
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   219
    # Delete this contact by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   220
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   221
        self.Parent.DeleteContact(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   222
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   223
    # Unconnect input and output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   224
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   225
        self.Input.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   226
        self.Output.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   227
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   228
    # Refresh the contact bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   229
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   230
        dc = wxClientDC(self.Parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   231
        # Calculate the size of the name outside the contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   232
        text_width, text_height = dc.GetTextExtent(self.Name)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   233
        # Calculate the bounding box size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   234
        if self.Name != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   235
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   236
            bbx_width = max(self.Size[0], text_width)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   237
            bbx_y = self.Pos.y - (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   238
            bbx_height = self.Size[1] + (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   239
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   240
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   241
            bbx_width = self.Size[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   242
            bbx_y = self.Pos.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   243
            bbx_height = self.Size[1]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   244
        self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   245
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   246
    # Refresh the position of wire connected to contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   247
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   248
        self.Input.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   249
        self.Output.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   250
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   251
    # Returns the contact connector that starts with the point given if it exists 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   252
    def GetConnector(self, position):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   253
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   254
        input_pos = self.Input.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   255
        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   256
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   257
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   258
        output_pos = self.Output.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   259
        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   260
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   261
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   262
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   263
    # Returns input and output contact connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   264
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   265
        return {"input":self.Input,"output":self.Output}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   266
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   267
    # Test if point given is on contact input or output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   268
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   269
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   270
        if self.Input.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   271
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   272
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   273
        if self.Output.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   274
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   275
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   276
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   277
    # Changes the contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   278
    def SetName(self, name):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   279
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   280
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   281
    # Returns the contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   282
    def GetName(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   283
        return self.Name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   284
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   285
    # Changes the contact type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   286
    def SetType(self, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   287
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   288
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   289
    # Returns the contact type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   290
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   291
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   292
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   293
    # Method called when a LeftDClick event have been generated
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   294
    def OnLeftDClick(self, event, scaling):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   295
        # Edit the contact properties
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   296
        self.Parent.EditContactContent(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   297
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   298
    # Refreshes the contact model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   299
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   300
        self.Parent.RefreshContactModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   301
        # If contact has moved, refresh the model of wires connected to output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   302
        if move:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   303
            self.Output.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   304
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   305
    # Draws contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   306
    def Draw(self, dc):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   307
        dc.SetPen(wxBLACK_PEN)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   308
        dc.SetBrush(wxBLACK_BRUSH)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   309
        # Draw two rectangles for representing the contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   310
        dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   311
        dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   312
        # Draw contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   313
        namewidth, nameheight = dc.GetTextExtent(self.Name)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   314
        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   315
                self.Pos.y - (nameheight + 2))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   316
        # Draw the modifier symbol in the middle of contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   317
        typetext = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   318
        if self.Type == CONTACT_REVERSE:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   319
            typetext = "/"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   320
        elif self.Type == CONTACT_RISING:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   321
            typetext = "P"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   322
        elif self.Type == CONTACT_FALLING:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   323
            typetext = "N"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   324
        if typetext != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   325
            typewidth, typeheight = dc.GetTextExtent(typetext)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   326
            dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   327
                    self.Pos.y + (self.Size[1] - typeheight) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   328
        # Draw input and output connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   329
        self.Input.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   330
        self.Output.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   331
        Graphic_Element.Draw(self, dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   332
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   333
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   334
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   335
#                         Ladder Diagram Coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   336
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   337
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   338
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   339
Class that implements the graphic representation of a coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   340
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   341
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   342
class LD_Coil(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   343
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   344
    # Create a new coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   345
    def __init__(self, parent, type, name, id = None):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   346
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   347
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   348
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   349
        self.Id = id
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   350
        self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   351
        # Create an input and output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   352
        self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   353
        self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   354
        
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   355
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   356
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   357
        self.Input = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   358
        self.Output = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   359
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   360
    # Forbids to change the contact size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   361
    def SetSize(self, width, height):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   362
        pass
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   363
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   364
    # Delete this coil by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   365
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   366
        self.Parent.DeleteCoil(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   367
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   368
    # Unconnect input and output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   369
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   370
        self.Input.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   371
        self.Output.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   372
                
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   373
    # Refresh the coil bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   374
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   375
        dc = wxClientDC(self.Parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   376
        # Calculate the size of the name outside the coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   377
        text_width, text_height = dc.GetTextExtent(self.Name)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   378
        # Calculate the bounding box size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   379
        if self.Name != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   380
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   381
            bbx_width = max(self.Size[0], text_width)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   382
            bbx_y = self.Pos.y - (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   383
            bbx_height = self.Size[1] + (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   384
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   385
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   386
            bbx_width = self.Size[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   387
            bbx_y = self.Pos.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   388
            bbx_height = self.Size[1]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   389
        self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   390
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   391
    # Refresh the position of wire connected to coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   392
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   393
        self.Input.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   394
        self.Output.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   395
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   396
    # Returns the coil connector that starts with the point given if it exists 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   397
    def GetConnector(self, position):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   398
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   399
        input_pos = self.Input.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   400
        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   401
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   402
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   403
        output_pos = self.Output.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   404
        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   405
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   406
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   407
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   408
    # Returns input and output coil connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   409
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   410
        return {"input":self.Input,"output":self.Output}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   411
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   412
    # Test if point given is on coil input or output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   413
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   414
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   415
        if self.Input.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   416
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   417
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   418
        if self.Output.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   419
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   420
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   421
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   422
    # Changes the coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   423
    def SetName(self, name):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   424
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   425
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   426
    # Returns the coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   427
    def GetName(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   428
        return self.Name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   429
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   430
    # Changes the coil type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   431
    def SetType(self, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   432
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   433
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   434
    # Returns the coil type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   435
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   436
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   437
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   438
    # Method called when a LeftDClick event have been generated
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   439
    def OnLeftDClick(self, event, scaling):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   440
        # Edit the coil properties
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   441
        self.Parent.EditCoilContent(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   442
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   443
    # Refreshes the coil model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   444
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   445
        self.Parent.RefreshCoilModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   446
        # If coil has moved, refresh the model of wires connected to output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   447
        if move:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   448
            self.Output.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   449
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   450
    # Draws coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   451
    def Draw(self, dc):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   452
        dc.SetPen(wxPen(wxBLACK, 2, wxSOLID))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   453
        dc.SetBrush(wxTRANSPARENT_BRUSH)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   454
        # Draw a two circle arcs for representing the coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   455
        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)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   456
        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)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   457
        dc.SetPen(wxBLACK_PEN)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   458
        dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   459
        # Draw coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   460
        namewidth, nameheight = dc.GetTextExtent(self.Name)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   461
        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   462
                self.Pos.y - (nameheight + 2))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   463
        # Draw the modifier symbol in the middle of coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   464
        typetext = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   465
        if self.Type == COIL_REVERSE:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   466
            typetext = "/"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   467
        elif self.Type == COIL_SET:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   468
            typetext = "S"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   469
        elif self.Type == COIL_RESET:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   470
            typetext = "R"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   471
        if typetext != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   472
            typewidth, typeheight = dc.GetTextExtent(typetext)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   473
            dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   474
                    self.Pos.y + (self.Size[1] - typeheight) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   475
        # Draw input and output connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   476
        self.Input.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   477
        self.Output.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   478
        Graphic_Element.Draw(self, dc)