private void btnLoadBui_Click(object sender, RoutedEventArgs e) { BuiFile buiFile = new BuiFile(); OpenFileDialog ofd = new OpenFileDialog(); ofd.FileName = ""; ofd.DefaultExt = "*.b*"; ofd.Filter = "TRNBuld File (*.bui;*.b17;*.b18)|*.bui;*.b17;*.b18"; if (ofd.ShowDialog() == true) { buiFile.Load(ofd.FileName); convertBuiToDot(buiFile); } }
public MainWindow() { InitializeComponent(); bool exitOption = false; FileInfo buiFileInfo = null; if (App.CommandLineArgs != null) { if (GetBuiFileAndOption(ref buiFileInfo, ref exitOption)) { BuiFile buiFile = new BuiFile(); buiFile.Load(buiFileInfo.FullName); convertBuiToDot(buiFile); if (exitOption) { Application.Current.Shutdown(); //終了 } } } }
/// <summary> /// Drag&Drop /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Drop(object sender, DragEventArgs e) { DroppedFiles list = this.DataContext as DroppedFiles; string[] files = e.Data.GetData(DataFormats.FileDrop) as string[]; if (files != null) { if (checkFileNmae(files[0])) { BuiFile buiFile = new BuiFile(); buiFile.Load(files[0]); convertBuiToDot(buiFile); } else { this.Activate(); // bring the window to the foreground. var msg = "Non-bui file was dropped. Please drop the Bui file here."; MessageBox.Show(this, msg, "Non-bui file", MessageBoxButton.OK); this.txtBox.Text = "Error !!\n" + msg; } } }
/// <summary> /// convert Bui to Dot format /// </summary> /// <param name="buiFile"></param> /// <returns></returns> private static List <string> buiToDot(BuiFile buiFile) { List <string> strList = new List <string>(); strList.Add("digraph {"); strList.Add(" rankdir=LR;"); List <string> zones = new List <string>(); List <string> extNodes = new List <string>(); List <string> auxNodes = new List <string>(); List <string> constPressureNodes = new List <string>(); // 数字で始まるZone名への対策 // Zone名の1文字目が数字なら先頭に"_"を追加する foreach (Link link in buiFile.LinkList.Links) { int num = 0; if (int.TryParse(link.FromNode.Substring(0, 1), out num)) { link.FromNode = "_" + link.FromNode; } if (int.TryParse(link.ToNode.Substring(0, 1), out num)) { link.ToNode = "_" + link.ToNode; } } // ノードをZone, Ext, Auxへ分類する foreach (Link link in buiFile.LinkList.Links) { string node; node = link.FromNode; parseNode(zones, extNodes, auxNodes, constPressureNodes, node); node = link.ToNode; parseNode(zones, extNodes, auxNodes, constPressureNodes, node); } string zoneList = ""; foreach (var str in zones) { zoneList += str + " "; } string extNodeList = ""; foreach (var str in extNodes) { extNodeList += str + " "; } string auxNodeList = ""; foreach (var str in auxNodes) { auxNodeList += str + " "; } string constPressureNodeList = ""; foreach (var str in constPressureNodes) { constPressureNodeList += str + " "; } strList.Add(" node [shape = doublecircle]; " + zoneList + (string.IsNullOrEmpty(zoneList) ? "" : ";")); strList.Add(" node [shape = circle]; " + extNodeList + (string.IsNullOrEmpty(extNodeList) ? "" : ";")); strList.Add(" node [shape = rectangle]; " + auxNodeList + (string.IsNullOrEmpty(auxNodeList) ? "" : ";")); strList.Add(" node [style=rounded, shape = diamond]; " + constPressureNodeList + (string.IsNullOrEmpty(constPressureNodeList) ? "" : ";")); foreach (Link link in buiFile.LinkList.Links) { string str = $" {link.FromNode} -> {link.ToNode}[ label = \"{link.LinkType} [{link.ID}]\" ];"; strList.Add(str); } strList.Add("} "); return(strList); }
/// <summary> /// Bui to Dot /// </summary> /// <param name="buiFile">BuiFile class</param> private void convertBuiToDot(BuiFile buiFile) { var dir = System.IO.Path.GetDirectoryName(buiFile.FileName); var fileName = System.IO.Path.GetFileNameWithoutExtension(buiFile.FileName); var baseFileName = $@"{dir}\{fileName}_airnetwork.bui"; var gvFileName = System.IO.Path.ChangeExtension(baseFileName, ".gv"); bool append = false; // overwrite // convert bui to gv var strList = buiToDot(buiFile); // display string gv = ""; foreach (string str in strList) { gv += str + "\n"; } this.txtBox.Text = gv; // save the .gv file MessageBoxResult ret; if (File.Exists(gvFileName)) { this.Activate(); // Put this window forward var msg = "The default GV file already exists. Overwrite it?"; ret = MessageBox.Show(msg, "Confirm saving default file", MessageBoxButton.YesNo); if (ret == MessageBoxResult.Yes) { // Yes, Overwrite the existing file. SaveGVFile(gvFileName, append, strList); } else { // No, new filename is needed. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.InitialDirectory = System.IO.Path.GetDirectoryName(gvFileName); dlg.FileName = System.IO.Path.GetFileName(gvFileName); // Default file name dlg.DefaultExt = ".gv"; // Default file extension dlg.Filter = "Graphviz DOT File (.gv)|*.gv"; // Filter files by extension // Show save file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document gvFileName = dlg.FileName; SaveGVFile(gvFileName, append, strList); } } } else { SaveGVFile(gvFileName, append, strList); } bool pngRet = false; var pngFileName = System.IO.Path.ChangeExtension(gvFileName, ".png"); if (File.Exists(pngFileName)) { this.Activate(); // Put this window forward var msg = "The default PNG file already exists. Overwrite it?"; ret = MessageBox.Show(msg, "Confirm saving default file", MessageBoxButton.YesNo); if (ret == MessageBoxResult.Yes) { // Yes, Overwrite the existing file. pngRet = SavePngFile(gv, pngFileName); } else { // No, new filename is needed. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.InitialDirectory = System.IO.Path.GetDirectoryName(pngFileName); dlg.FileName = System.IO.Path.GetFileName(pngFileName); // Default file name dlg.DefaultExt = ".png"; // Default file extension dlg.Filter = "PNG (.png)|*.png"; // Filter files by extension // Show save file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document pngRet = SavePngFile(gv, dlg.FileName); } } } else { // generate a image using Graphviz/Dot pngRet = SavePngFile(gv, pngFileName); } // Launch application associated with png file if (pngRet) { Process.Start(pngFileName); } }