Ejemplo n.º 1
0
        public Building Load(string filename)
        {
            var formatter = new BinaryFormatter();
            var files     = new List <SavedFile>();

            using (var fs = new FileStream(filename, FileMode.OpenOrCreate))
            {
                files = (List <SavedFile>)formatter.Deserialize(fs);
            }

            Type t = typeof(Building);

            var sBuilding = (BuildingSavedFile)files.FirstOrDefault(x => x.FileType == typeof(Building));

            if (sBuilding == null)
            {
                return(null);
            }

            Building building   = new Building(sBuilding.Title);
            var      dictionary = new Dictionary <int, dynamic>();

            foreach (var file in files.Where(x => x.FileType == typeof(ZoomTool)))
            {
                var sScale = file as ZoomToolSavedFile;
                dictionary.Add(sScale.HashCode, new ZoomTool(sScale.ActualLength, sScale.GraphicLength));
            }


            foreach (var file in files.Where(x => x.FileType == typeof(Floor)))
            {
                var sFloor = file as FloorSavedFile;
                var floor  = new Floor(sFloor.Title, building)
                {
                    Scale          = dictionary[sFloor.Scale],
                    FloorPlanImage = SettingsManager.GetBitmapImage(sFloor.FloorPlanImage)
                };
                dictionary.Add(sFloor.HashCode, floor);
                building.AddFloor(floor);
            }



            foreach (var file in files)
            {
                if (file is StartNodeSavedFile)
                {
                    var   sNode = file as StartNodeSavedFile;
                    Floor floor = dictionary[sNode.ParentHashCode];

                    var node = new StartNode(floor, sNode.Position, sNode.Title)
                    {
                        PeopleCount    = sNode.PeopleCount,
                        ProjectionArea = sNode.ProjectionArea,
                        AutoSize       = sNode.AutoSize
                    };
                    dictionary.Add(sNode.HashCode, node);
                    floor.AddObject(node);
                    continue;
                }
                if (file is EntryNodeSavedFile)
                {
                    var   sNode = file as EntryNodeSavedFile;
                    Floor floor = dictionary[sNode.ParentHashCode];

                    var node = new EntryNode(floor, sNode.Position, sNode.Title)
                    {
                        AutoSize = sNode.AutoSize,
                        Width    = sNode.Width
                    };
                    dictionary.Add(sNode.HashCode, node);
                    floor.AddObject(node);
                    continue;
                }

                if (file is StairsNodeSavedFile)
                {
                    var   sNode = file as StairsNodeSavedFile;
                    Floor floor = dictionary[sNode.ParentHashCode];

                    var node = new StairsNode(floor, sNode.Position, sNode.Title)
                    {
                        AutoSize          = sNode.AutoSize,
                        IsFloorsConnected = sNode.IsFloorsConnected
                    };
                    dictionary.Add(sNode.HashCode, node);
                    floor.AddObject(node);
                    continue;
                }

                if (file is NodeSavedFile)
                {
                    var   sNode = file as NodeSavedFile;
                    Floor floor = dictionary[sNode.ParentHashCode];
                    Node  node  = null;
                    if (sNode.FileType == typeof(ExitNode))
                    {
                        node = new ExitNode(floor, sNode.Position, sNode.Title)
                        {
                            AutoSize = sNode.AutoSize
                        }
                    }
                    ;
                    if (sNode.FileType == typeof(RoadNode))
                    {
                        node = new RoadNode(floor, sNode.Position, sNode.Title)
                        {
                            AutoSize = sNode.AutoSize
                        }
                    }
                    ;

                    dictionary.Add(sNode.HashCode, node);
                    floor.AddObject(node);
                    continue;
                }

                if (file is SectionSavedFile)
                {
                    var     sSection = file as SectionSavedFile;
                    Floor   floor    = dictionary[sSection.ParentHashCode];
                    Section section  = null;
                    if (sSection.FileType == typeof(RoadSection))
                    {
                        section = new RoadSection((Node)dictionary[sSection.FirstNodeHashCode], (Node)dictionary[sSection.LastNodeHashCode], floor, sSection.Title)
                        {
                            AutoSize = sSection.AutoSize,
                            Length   = sSection.Length,
                            Width    = sSection.Width
                        }
                    }
                    ;
                    else if (sSection.FileType == typeof(StairsSection))
                    {
                        section = new StairsSection((Node)dictionary[sSection.FirstNodeHashCode], (Node)dictionary[sSection.LastNodeHashCode], floor, sSection.Title)
                        {
                            AutoSize = sSection.AutoSize,
                            Length   = sSection.Length,
                            Width    = sSection.Width
                        }
                    }
                    ;
                    else if (sSection.FileType == typeof(FloorsConnectionSection))
                    {
                        continue;                                                            // следует произвести добавление в самом конце
                    }
                    dictionary.Add(sSection.HashCode, section);
                    floor.AddObject(section);
                    continue;
                }
            }

            foreach (var file in files.Where(x => x.FileType == typeof(FloorsConnectionSection)))
            {
                var   sSection = file as SectionSavedFile;
                Floor floor    = dictionary[sSection.ParentHashCode];
                var   section  = new FloorsConnectionSection((Node)dictionary[sSection.FirstNodeHashCode], (Node)dictionary[sSection.LastNodeHashCode], floor);
                dictionary.Add(sSection.HashCode, section);
                floor.AddObject(section);
            }

            return(building);
        }
    }
}