public void MergeBlocks(S7Block leftBlock, S7Block rightBlock) { // Standard FB/FC/DB // Should have both blocks if (leftBlock == null || rightBlock == null) { EventFire.Error("Merge S7 Blocks. Can't find block in the Simatic structure."); return; } EventFire.Info("Merge S7 Blocks. Generating right source."); if (!Directory.Exists(_RightSourceFilePath)) { Directory.CreateDirectory(_RightSourceFilePath); } String rightSourceFileName = _RightSourceFilePath + rightBlock.Name + ".awl"; rightBlock.GenerateSource(rightSourceFileName, S7GenerateSourceFlags.S7GSFDoOverwrite); EventFire.Info("Merge S7 Blocks. Generating left source."); if (!Directory.Exists(_LeftSourceFilePath)) { Directory.CreateDirectory(_LeftSourceFilePath); } String leftSourceFileName = _LeftSourceFilePath + leftBlock.Name + ".awl"; leftBlock.GenerateSource(leftSourceFileName, S7GenerateSourceFlags.S7GSFDoOverwrite); String mergedSourceFileName = _MergedSourceFilePath + rightBlock.Name + ".awl"; Process DiffProcess = new Process { StartInfo = new ProcessStartInfo { FileName = "TortoiseMerge.exe", Arguments = "/base:\"" + leftSourceFileName + "\" /mine:\"" + rightSourceFileName + "\" /merged:\"" + mergedSourceFileName + "\"" } }; DiffProcess.Start(); DiffProcess.WaitForExit(); if (File.Exists(mergedSourceFileName)) { S7SWItems s7swItems = rightBlock.Program.Next; for (int i = 1; i <= s7swItems.Count; i++) { if (((S7Container)s7swItems[i]).ConcreteType == S7ContainerType.S7SourceContainer) { IS7Source3 s7Source = (IS7Source3)s7swItems[i].Next.Add("Tmprc", S7SWObjType.S7Source, mergedSourceFileName); EventFire.Info("Merge S7 Blocks. Compiling block."); S7SWItems s7swReturn = s7Source.Compile(); IntPtr hwnd = (IntPtr)s7Source.AppWindowHandle; ShowWindow(hwnd, 6); // 0 = hide the window, 6 = minimize if (s7swReturn.Count <= 0) { // Something is not right - show edit window EventFire.Error("Merge S7 Blocks. Can't compile block, please resolve compilation errors."); ShowWindow(hwnd, 3); // 3 - maximize s7Source.Edit(); // Wait till they fixed it and close the window while (IsWindow(hwnd)) { ; } } else { // Close editor window EventFire.Info("Merge S7 Blocks. Compiled successfully."); //SendMessage(hwnd, 0x0112, 0xF060, 0); // 0xF060 = SC_CLOSE; 0x0112 = WM_SYSCOMMAND } // Remove the source s7Source.Remove(); // S7 editor doesn't like close message. Minimize it ShowWindow(hwnd, 6); break; } } } }
public void MergeBlocks(S7Source leftBlock, S7Source rightBlock, string ExtractRightProjectPath, KeyValuePair <String, Blocks> CurrentBlock) { // Should have both blocks if (leftBlock == null || rightBlock == null) { EventFire.Error("Merge Source Blocks. Can't find block in the Simatic structure."); return; } EventFire.Info("Merge Source Blocks. Exporting right source."); if (!Directory.Exists(_RightSourceFilePath)) { Directory.CreateDirectory(_RightSourceFilePath); } String rightSourceFileName = _RightSourceFilePath + rightBlock.Name + ".scl"; rightBlock.Export(rightSourceFileName); EventFire.Info("Merge Source Blocks. Exporting left source."); if (!Directory.Exists(_LeftSourceFilePath)) { Directory.CreateDirectory(_LeftSourceFilePath); } String leftSourceFileName = _LeftSourceFilePath + leftBlock.Name + ".scl"; leftBlock.Export(leftSourceFileName); String mergedSourceFileName = _MergedSourceFilePath + rightBlock.Name + ".scl"; Process DiffProcess = new Process { StartInfo = new ProcessStartInfo { FileName = "TortoiseMerge.exe", Arguments = "/base:\"" + leftSourceFileName + "\" /mine:\"" + rightSourceFileName + "\" /merged:\"" + mergedSourceFileName + "\"" } }; DiffProcess.Start(); DiffProcess.WaitForExit(); if (File.Exists(mergedSourceFileName)) { // Can't find any gracefull way of importing source block - just override the file String strSrcFile = ExtractRightProjectPath + ((BlockSource)CurrentBlock.Value.RightBlock).Filename.Replace(@"/", @"\"); File.Copy(mergedSourceFileName, strSrcFile, true); EventFire.Info("Merge Source Blocks. Compiling block"); S7SWItems s7swReturn = rightBlock.Compile(); // AppWindowHandle crashes. Find window hwnd and wait till they done editing/compiling //IntPtr hwnd = (IntPtr)rightBlock.AppWindowHandle; IntPtr hwnd = IntPtr.Zero; Process[] processRunning = Process.GetProcesses(); Process proc = null; foreach (Process pr in processRunning) { if (pr.ProcessName == "s7sclapx") { proc = pr; hwnd = pr.Handle; break; } } //ShowWindow(hwnd, 6); // 0 = hide the window, 6 = minimize // Seems to be a bug, compile always returns 0 if (s7swReturn.Count <= 0) { // Something is not right - show edit window //EventFire.Error("Can't compile block, please resolve compilation errors"); EventFire.Info("Merge Source Blocks. Please review/compile block and close SCL editor."); ShowWindow(hwnd, 3); // 3 - maximize rightBlock.Edit(); // Wait till they fixed it and close the window //while (IsWindow(hwnd)) ; while (!proc.HasExited) { ; } } else { // Close editor window EventFire.Info("Merge Source Blocks. Compiled successfully"); SendMessage(hwnd, 0x0112, 0xF060, 0); // 0xF060 = SC_CLOSE; 0x0112 = WM_SYSCOMMAND } } }