// boutons loco
        private void BTN_modifierLoco_Click(object sender, RoutedEventArgs e)
        {
            CLoco loco = new CLoco();
            if (this.TB_ID.Text == "")
            {
                MessageBox.Show("Veuillez saisir un ID");
                return; 
            }

            loco.ID = this.TB_ID.Text;                             
            loco.longueur = (this.TB_longueur.Text == "") ? 0 : Convert.ToInt32(this.TB_longueur.Text);
            loco.masse = (this.TB_masse.Text == "") ? 0 : Convert.ToInt32(this.TB_masse.Text);
            loco.nrcars = (this.TB_nrCars.Text == "") ? 0 : Convert.ToInt32(this.TB_nrCars.Text);
            loco.vitesse = (this.TB_vitesse.Text == "") ? 0 : Convert.ToInt32(this.TB_vitesse.Text);
            loco.vitesseMoy = (this.TB_vitesseMoyenne.Text == "") ? 0 : Convert.ToInt32(this.TB_vitesseMoyenne.Text);
            loco.direction = (bool)this.ChB_direction.IsChecked;
            
            if (this._mode == 0) // creation d'une nouvelle loomotive sur le plan
            {
                this._gestionTrain.creerLoco(loco);                
            }
            else if (this._mode == 1) // modification d'une locomotive déjà existante sur le plan
            {
                this._gestionTrain.modifierParamLoco(loco);
            }
            this.Close();
        }
 // ouverture fenetre modifier loco
 public fenetreLoco(CGestionTrain gestionTrain, CLoco loco)
 {
     InitializeComponent();
     this._mode = 1;
     this._gestionTrain = gestionTrain;
     TB_ID.Text = loco.ID;
     TB_longueur.Text = loco.longueur.ToString();
     TB_blockid.Text = loco.blockid;
     TB_vitesse.Text = loco.vitesse.ToString();
     TB_vitesseMoyenne.Text = loco.vitesseMoy.ToString();
     TB_masse.Text = loco.masse.ToString();
     TB_nrCars.Text = loco.nrcars.ToString();
     ChB_direction.IsChecked = loco.direction;
     Show();
 }
        /// <summary>
        /// Determine et modifie la zone aval d'un train.
        /// </summary>
        /// <param name="train">Train à modifer</param>
        public static void determinerZA(CLoco loco)
        {
            if (loco.vitesse == 0) return;

            CVMRegulateurComposeur res = (CVMRegulateurComposeur)Application.Current.FindResource("RessourcesMV");
            int nbrItineraire = 0;

            foreach (CItineraire itineraire in res.listeItineraire)
            {
                // Si la loco principale se situe sur le point de départ d'un itinéraire
                // qu'elle va en direction du prochain aiguillage,
                // et que ce soit en marche avant ou marche arrière
                if (loco.blockid == itineraire.bka && 
                    itineraire.bkb != loco.ZoneAmont &&
                     (((itineraire.bkaside == itineraire.bkbside) ^
                     (itineraire.bkbside == loco.blockEnterSide)) ^
                     !loco.direction))
                {
                    nbrItineraire++;
                    // Si les aiguillages sur l'itinéraire sont dans la bonne position
                    if (CLocalisation._itineraireCorrect(itineraire.listeAiguillage))
                    {
                        loco.ZA = CLocalisation.trouverZone(itineraire.bkb);
                        loco.ZoneAval = loco.ZA.blockID;
                        loco.ProchainBES = itineraire.bkbside;
                        return;
                    }
                }
            }
            loco.ZA = null;
            loco.ZoneAval = (nbrItineraire == 0) ? "Pas d'itinéraire" : "Erreur d'aiguillage(s)";
        }
        private static void OnTrameReceived(object sender, TrameEventArgs e)
        {
            if (!planRecu) return;

            string r = e.Datas;
            XmlDocument d = new XmlDocument();

            if (r.Contains("<model cmd="))
            {
                // Ajout d'un élément 
                if (r.Contains("\"add\""))
                {
                    string aRetirer1 = "<model cmd=\"add\">\r\n  ";
                    string aRetirer2 = "\r </model>";
                    r = r.Remove(0, aRetirer1.Length);
                    r = r.Remove(r.Length - aRetirer2.Length, aRetirer2.Length);
                    d.LoadXml(r);

                    if (d.DocumentElement.Name == "car")
                    {
                        CWagon wagon = new CWagon();
                        wagon.ID = d.DocumentElement.Attributes["id"].Value;
                        if (d.DocumentElement.Attributes["weight_empty"] != null)
                            wagon.poidsAvide = Convert.ToInt32(d.DocumentElement.Attributes["weight_empty"].Value);
                        if (d.DocumentElement.Attributes["weight_loaded"] != null)
                            wagon.poidsRempli = Convert.ToInt32(d.DocumentElement.Attributes["weight_loaded"].Value);
                        if (d.DocumentElement.Attributes["location"] != null)
                            wagon.location = d.DocumentElement.Attributes["location"].Value;
                        if (d.DocumentElement.Attributes["len"] != null)
                            wagon.longueur = Convert.ToInt32(d.DocumentElement.Attributes["len"].Value);
                        res.listeWagon.Add(wagon);
                    }
                    else if (d.DocumentElement.Name == "lc")
                    {
                        CLoco loco = new CLoco();
                        loco.ID = d.DocumentElement.Attributes["id"].Value;
                        if (d.DocumentElement.Attributes["blockid"] != null)
                            loco.blockid = d.DocumentElement.Attributes["blockid"].Value;
                        if (d.DocumentElement.Attributes["V"] != null)
                            loco.vitesse = Convert.ToInt32(d.DocumentElement.Attributes["V"].Value);
                        if (d.DocumentElement.Attributes["dir"] != null)
                            loco.direction = Convert.ToBoolean(d.DocumentElement.Attributes["dir"].Value);
                        if (d.DocumentElement.Attributes["V_mid"] != null)
                            loco.vitesseMoy = Convert.ToInt32(d.DocumentElement.Attributes["V_mid"].Value);
                        if (d.DocumentElement.Attributes["blockenterside"] != null)
                            loco.blockEnterSide = Convert.ToBoolean(d.DocumentElement.Attributes["blockenterside"].Value);
                        if (d.DocumentElement.Attributes["mass"] != null)
                            loco.masse = Convert.ToInt32(d.DocumentElement.Attributes["mass"].Value);
                        if (d.DocumentElement.Attributes["nrcars"] != null)
                            loco.nrcars = Convert.ToInt32(d.DocumentElement.Attributes["nrcars"].Value);
                        if (d.DocumentElement.Attributes["number"] != null)
                            loco.number = d.DocumentElement.Attributes["number"].Value;
                        if (d.DocumentElement.Attributes["len"] != null)
                            loco.longueur = Convert.ToInt32(d.DocumentElement.Attributes["len"].Value);
                        res.listeLoco.Add(loco);
                    }
                }
                // Suppression d'un élément
                else if (r.Contains("\"remove\""))
                {
                    string aRetirer1 = "<model cmd=\"remove\" controlcode=\"\" slavecode=\"\" server=\"infw03A5B7AC\">\r\n  ";
                    string aRetirer2 = "\r </model>";
                    r = r.Remove(0, aRetirer1.Length);
                    r = r.Remove(r.Length - aRetirer2.Length, aRetirer2.Length);
                    d.LoadXml(r);

                    if (d.DocumentElement.Name == "car")
                    {
                        foreach (CWagon wagon in res.listeWagon)
                        {
                            if (wagon.ID == d.DocumentElement.Attributes["id"].Value)
                            {
                                res.listeWagon.Remove(wagon);
                                break;
                            }
                        }
                    }
                    else if (d.DocumentElement.Name == "lc")
                    {
                        foreach (CLoco loco in res.listeLoco)
                        {
                            if (loco.ID == d.DocumentElement.Attributes["id"].Value)
                            {
                                res.listeLoco.Remove(loco);
                                break;
                            }
                        }
                    }
                }
            }
                        
        }