Exemple #1
0
        private S7Block FindBlockInProgram(KeyValuePair<String, Blocks> BlockToFind, S7Project ProjectS7, ref SimaticCache SimaticCache)
        {
            try
            {
                List<String> PathParts = (BlockToFind.Key.Split('\\')).ToList<String>();

                // Trying fast method of findig the block
                // Verify that project name is matches
                if (ProjectS7.Name == PathParts[0])
                {
                    // So far so good project seems to be a right one
                    // Get a station
                    S7Station station = (S7Station)ProjectS7.Stations[PathParts[1]];
                    if (station != null)
                    {
                        // Got a correct station
                        // There is an issue finding a rack by name, so for now assuming it's rack 0
                        S7Rack rack = (S7Rack)station.Racks[0];
                        if (rack != null)
                        {
                            // Got a rack
                            // For unknown reasons we can't search module by name, so for now assuming that CPU is module #2
                            S7Module module = (S7Module)rack.Modules[2];
                            if (module != null && module.Name == PathParts[2])
                            {
                                // Got a CPU module. Now we can find a corresponding program folder
                                S7Programs programs = (S7Programs)ProjectS7.Programs;
                                S7Program program = (S7Program)programs[module];
                                if (program != null && program.Name == PathParts[3])
                                {
                                    // It should be a block subfolder in the program folder
                                    S7SWItem blocksFolder = (S7SWItem)program.Next[PathParts[4]];
                                    if (blocksFolder != null)
                                    {
                                        // Block folder is fine, return a block
                                        return (S7Block)blocksFolder.Next[PathParts.Last()];
                                    }
                                }
                            }
                        }
                    }
                }

                // Fall back to the old method
                String BlocksFolderPath = BlockToFind.Key.Remove(BlockToFind.Key.LastIndexOf('\\'));
                String ProgramFolderPath = BlocksFolderPath.Remove(BlocksFolderPath.LastIndexOf('\\'));
                SimaticCacheUpdate(ProjectS7, ref SimaticCache);

                var prgs = from ds in SimaticCache.dicProgramsLogPath where ds.Value.strProgramLogPath == ProgramFolderPath select ds;
                if (prgs.Count() > 0)
                {
                    var prg = prgs.First();
                    var blks = from ds in prg.Value.dicBlocksLogPath where ds.Value == BlocksFolderPath select ds;
                    if (blks.Count() > 0)
                    {
                        var blk = blks.First();
                        try
                        {
                            return (S7Block)ProjectS7.Programs[prg.Key].Next[blk.Key].Next[PathParts.Last()];
                        }
                        catch (Exception ex)
                        {
                            EventFire.Error("S7.FindBlockInProgram. Exception: " + ex.ToString());
                            return null;
                        }
                    }
                }
            }
            catch (Exception err)
            {
                EventFire.Error(err.ToString());
            }

            // Can't find it
            return null;
        }
Exemple #2
0
        private IS7Container FindBlockFolderInProgram(KeyValuePair<String, Blocks> FolderToFind, S7Project ProjectS7, ref SimaticCache SimaticCache)
        {
            try
            {
                List<String> PathParts = (FolderToFind.Key.Split('\\')).ToList<String>();

                // Trying fast method of findig the block
                S7Program program = FindProgramFolderFast(FolderToFind, ProjectS7);
                if (program != null && program.Name == PathParts[3])
                {
                    // It should be a block subfolder in the program folder
                    return (IS7Container)program.Next[PathParts[4]];
                }

                // Fall back to the old method
                String BlocksFolderPath = FolderToFind.Key.Remove(FolderToFind.Key.LastIndexOf('\\'));
                String ProgramFolderPath = BlocksFolderPath.Remove(BlocksFolderPath.LastIndexOf('\\'));
                SimaticCacheUpdate(ProjectS7, ref SimaticCache);

                var prgs = from ds in SimaticCache.dicProgramsLogPath where ds.Value.strProgramLogPath == ProgramFolderPath select ds;
                if (prgs.Count() > 0)
                {
                    var prg = prgs.First();
                    var blks = from ds in prg.Value.dicBlocksLogPath where ds.Value == BlocksFolderPath select ds;
                    if (blks.Count() > 0)
                    {
                        var blk = blks.First();
                        try
                        {
                            return (IS7Container)ProjectS7.Programs[prg.Key].Next[blk.Key];
                        }
                        catch (Exception ex)
                        {
                            EventFire.Error("S7.FindBlockFolderInProgram. Exception: " + ex.ToString());
                            return null;
                        }
                    }
                }
            }
            catch (Exception err)
            {
                EventFire.Error(err.ToString());
            }

            // Can't find it
            return null;
        }
Exemple #3
0
        private void SimaticCacheUpdate(S7Project ProjectS7, ref SimaticCache SimaticCache)
        {
            try
            {
                // if dictionary is not populated
                if (SimaticCache.dicProgramsLogPath.Count == 0)
                {
                    for (int i = 1; i <= ProjectS7.Programs.Count; i++)
                    {
                        if (ProjectS7.Programs[i].Type == S7ProgramType.S7)
                        {
                            SimaticCache.dicProgramsLogPath.Add(i, new SimaticCache.SimaticBlocksCache());

                            //Debug.Print("SimaticCacheUpdate. Program: "+ i.ToString() + "; Before LogPath: " + DateTime.Now.ToLocalTime().ToString());
                            SimaticCache.dicProgramsLogPath[i].strProgramLogPath = ProjectS7.Programs[i].LogPath;
                            //Debug.Print("SimaticCacheUpdate. Program: " + i.ToString() + "; After LogPath: " + DateTime.Now.ToLocalTime().ToString());

                            for (int j = 1; j <= ProjectS7.Programs[i].Next.Count; j++)
                            {
                                SimaticCache.dicProgramsLogPath[i].dicBlocksLogPath.Add(j, ProjectS7.Programs[i].Next[j].LogPath);
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                EventFire.Error(err.ToString());
            }
        }
Exemple #4
0
        private void LoadingBothSimaticProjects(ref Simatic _Simatic)
        {
            try
            {
                // Trying to find loaded projects
                EventFire.Info("Loading left project to Simatic.");
                if (ExtractLeftProjectPath != "")
                {
                    _LeftProject = getSimaticProject(ref _Simatic, GetProjectFileNameFromPath(ExtractLeftProjectPath));
                }
                else
                {
                    if (IsZippedProject(LeftProjectPath))
                    {
                        // Zipped project - Unzip it first
                        ExtractLeftProjectPath = Path.GetTempPath() + "DMC_Merge" + "\\pLeft\\";
                        _LeftProject = getSimaticProject(ref _Simatic, UnzipProject(LeftProjectPath, ExtractLeftProjectPath));
                    }
                    else
                    {
                        // Unzipped project - attach
                        ExtractLeftProjectPath = LeftProjectPath;
                        _LeftProject = getSimaticProject(ref _Simatic, GetProjectFileNameFromPath(LeftProjectPath));
                    }
                }
                ProgressBarCurrent++;

                EventFire.Info("Loading right project to Simatic.");
                if (ExtractRightProjectPath != "")
                {
                    _RightProject = getSimaticProject(ref _Simatic, GetProjectFileNameFromPath(ExtractRightProjectPath));
                }
                else
                {
                    if (IsZippedProject(RightProjectPath))
                    {
                        // Zipped project - Unzip it first
                        ExtractRightProjectPath = Path.GetTempPath() + "DMC_Merge" + "\\pRight\\";
                        _RightProject = getSimaticProject(ref _Simatic, UnzipProject(RightProjectPath, ExtractRightProjectPath));
                    }
                    else
                    {
                        // Unzipped project - attach
                        ExtractRightProjectPath = RightProjectPath;
                        _RightProject = getSimaticProject(ref _Simatic, GetProjectFileNameFromPath(RightProjectPath));
                    }
                }
                ProgressBarCurrent++;
            }
            catch (Exception err)
            {
                EventFire.Error(err.ToString());
            }
        }
Exemple #5
0
        private S7Program FindProgramFolderFast(KeyValuePair<String, Blocks> ProgramFolderToFind, S7Project ProjectS7)
        {
            try
            {
                List<String> PathParts = (ProgramFolderToFind.Key.Split('\\')).ToList<String>();

                // Trying fast method of findig the block
                // Verify that project name is matches
                if (ProjectS7.Name == PathParts[0])
                {
                    // So far so good project seems to be a right one
                    // Get a station
                    S7Station station = (S7Station)ProjectS7.Stations[PathParts[1]];
                    if (station != null)
                    {
                        // Got a correct station
                        // There is an issue finding a rack by name, so for now assuming it's rack 0
                        S7Rack rack = (S7Rack)station.Racks[0];
                        if (rack != null)
                        {
                            // Got a rack
                            // For unknown reasons we can't search module by name, so for now assuming that CPU is module #2
                            S7Module module = (S7Module)rack.Modules[2];
                            if (module != null && module.Name == PathParts[2])
                            {
                                // Got a CPU module. Now we can find a corresponding program folder
                                S7Programs programs = (S7Programs)ProjectS7.Programs;
                                return (S7Program)programs[module];
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                EventFire.Error(err.ToString());
            }

            // Can't find it
            return null;
        }
Exemple #6
0
 private S7Source FindCurrentSourceInProgram(S7Project ProjectS7, ref SimaticCache SimaticCache)
 {
     try
     {
         return FindSourceInProgram(CurrentBlock, ProjectS7, ref SimaticCache);
     }
     catch (Exception err)
     {
         EventFire.Error(err.ToString());
     }
     return null;
 }