Attribute VB_Name = "SauvegardeModule"
Public Function CheminFichier(fonction As Boolean) As String
Dim dlg As CFileDialog

Set dlg = New CFileDialog
If fonction = False Then dlg.DialogTitle = "Sauvegarder le schema en cours"
If fonction = True Then dlg.DialogTitle = "Ouvrir un schema"

dlg.Filter = "Fichier SimLogic|*.sim"
dlg.InitialDir = "C:\"
If dlg.Show(fonction) Then
CheminFichier = dlg.FileName
End If
End Function



Public Function Sauvegarde()
' sauvegarde le schema

Dim Fichier As String

Fichier = CheminFichier(False)

If Fichier <> "" Then
    Open Fichier For Output As #1
    
    Dim PosX As Integer, PosY As Integer
    Dim SortiePosX As Integer, SortiePosY As Integer
    Dim PinPos() As Integer
    Dim NbEntrees As Long
    
    ligne = "NbPad:" & NB_PAD
    Print #1, ligne
    
    For i = 0 To NB_Gates - 1
    If GatesStatus(i) = 1 Then
    
        ligne = "[Porte " & i & "]"
        Print #1, ligne
        ligne = "ID:" & Gates(i).GetID()
        Print #1, ligne
        ligne = "Type:" & Gates(i).GetType()
        Print #1, ligne
        ligne = "Etat:" & Gates(i).GetEtat()
        Print #1, ligne
        NbEntrees = Gates(i).GetNbEntree()
        ligne = "NBEntrees:" & NbEntrees
        Print #1, ligne
        tmp = Gates(i).GetPosition(PosX, PosY, PinPos, SortiePosX, SortiePosY)
        ligne = "PositionX:" & PosX
        Print #1, ligne
        ligne = "PositionY:" & PosY
        Print #1, ligne
        ligne = "PositionSortieX:" & SortiePosX
        Print #1, ligne
        ligne = "PositionSortieY:" & SortiePosY
        Print #1, ligne
        
        For j = 0 To NbEntrees - 1
            ligne = "Entree:" & j
            Print #1, ligne
            ligne = "Parent:" & Gates(i).GetParent(j)
            Print #1, ligne
            ligne = "Etat:" & Gates(i).GetPinEtat(j)
            Print #1, ligne
            ligne = "PinPositionX:" & PinPos(0, j)
            Print #1, ligne
            ligne = "PinPositionY:" & PinPos(1, j)
            Print #1, ligne
        Next j
            
    End If
    Next i
    
    Close #1
    
End If

End Function


Public Function Charge()

Dim Fichier As String
Dim PorteID As Long
Dim PorteType As String
Dim NbEntrees As Long
Dim PinParents() As Long
Dim PinEtat() As Long
Dim PortePosX As Integer
Dim PortePosY As Integer
Dim PinPos() As Integer
Dim SortiePosX As Integer
Dim SortiePosY As Integer
Dim PosX As Integer
Dim PosY As Integer

If NBGates > 0 Then ' propose de sauvegarder si un schema est affiché
    reponse = MsgBox("Charger un schema entrainera la perte du schema en cours. Voulez-vous sauvegarder avant de continuer ?", vbYesNo)
    If reponse = vbYes Then Sauvegarde
End If

Fichier = CheminFichier(True)

If Fichier <> "" Then

Open Fichier For Input As #1

For i = 0 To NBGates - 1 ' decharge les portes en cours
    Set Gates(i) = Nothing
Next i


Input #1, ligne
NbPortes = Right(ligne, Len(ligne) - 9)
NBGates = NbPortes
ReDim GatesStatus(NbPortes - 1)
ReDim Gates(NbPortes - 1)

For i = 0 To NbPortes - 1
    GatesStatus(i) = -1
    Set Gates(i) = Nothing
Next i


While Not EOF(1)

'Caracteristiques de la porte
Input #1, ligne ' passe le tag [porte x]
Input #1, ligne
PorteID = Right(ligne, Len(ligne) - 3)
Input #1, ligne
PorteType = Right(ligne, Len(ligne) - 5)
Input #1, ligne ' on passe l'etat, on le recalculera
Input #1, ligne
NbEntrees = Right(ligne, Len(ligne) - 10)
Input #1, ligne
PortePosX = Right(ligne, Len(ligne) - 10)
Input #1, ligne
PortePosY = Right(ligne, Len(ligne) - 10)
Input #1, ligne
SortiePosX = Right(ligne, Len(ligne) - 16)
Input #1, ligne
SortiePosY = Right(ligne, Len(ligne) - 16)

ReDim PinParents(NbEntrees - 1)
ReDim PinEtat(NbEntrees - 1)
ReDim PinPos(1, NbEntrees - 1)

For i = 0 To NbEntrees - 1
    Input #1, ligne ' on passe le num d;entree, ils sont dans l'ordre
    Input #1, ligne
    PinParents(i) = Right(ligne, Len(ligne) - 7)
    Input #1, ligne
    PinEtat(i) = Right(ligne, Len(ligne) - 5)
    Input #1, ligne
    PinPos(0, i) = Right(ligne, Len(ligne) - 13)
    Input #1, ligne
    PinPos(1, i) = Right(ligne, Len(ligne) - 13)
Next i

GatesStatus(PorteID) = 1
Set Gates(PorteID) = New Gate ' instancie une nouvelle porte
tmp = Gates(PorteID).ChargePorte(PorteID, PorteType, NbEntrees, PinParents, PinEtat, PortePosX, PortePosY, PinPos, SortiePosX, SortiePosY)




Wend

Close #1
End If

ReDraw SchemaFrm.Schema

End Function

