using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace vcsPSHD_gnuplot { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //-------------------------------------------------------------------------------------------- private void Form1_Load(object sender, EventArgs e) { pictureBox1.Visible = true; toolStripStatusLabel1.Text = "size and Png file name"; } //-------------------------------------------------------------------------------------------- private void toolStripButton1_Click(object sender, EventArgs e) { System.IO.StreamReader sr; System.IO.StreamWriter sw; string fnameR = ""; string fnameW = ""; string dat; string[] sbuf; char delim = ','; string fnameF; string fnameZ; string[] ps_name; string[] ps_type; string[] st_class; double[] co_YEAR; double[] ps_HD; string[] tbrl; int[] mark; int[] bboxv = new int[4]; //BoundingBox定義:変更無し bboxv[0] = 50; bboxv[1] = 50; bboxv[2] = 410; bboxv[3] = 302; string sxjiku, syjiku; double xmin, xmax, dx; double ymin, ymax, dy; int nd; double xx, yy,dlx,dly; string str; int i, k; //********************************************** //入出力ファイル指定 //********************************************** openFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { fnameR = openFileDialog1.FileName; } if (saveFileDialog1.ShowDialog() == DialogResult.OK) { fnameW = saveFileDialog1.FileName; } //********************************************** //データ入力 //********************************************** sr = new System.IO.StreamReader(fnameR, System.Text.Encoding.Default); dat = sr.ReadLine(); sbuf = dat.Split(delim); fnameF = sbuf[0]; dat = sr.ReadLine(); sbuf = dat.Split(delim); sxjiku = sbuf[0]; syjiku = sbuf[1]; dat = sr.ReadLine(); sbuf = dat.Split(delim); xmin = double.Parse(sbuf[0]); xmax = double.Parse(sbuf[1]); dx = double.Parse(sbuf[2]); dat = sr.ReadLine(); sbuf = dat.Split(delim); ymin = double.Parse(sbuf[0]); ymax = double.Parse(sbuf[1]); dy = double.Parse(sbuf[2]); dat = sr.ReadLine(); sbuf = dat.Split(delim); nd = int.Parse(sbuf[0]); ps_name = new string[nd]; ps_type = new string[nd]; st_class = new string[nd]; co_YEAR = new double[nd]; ps_HD = new double[nd]; tbrl = new string[nd]; mark = new int[nd]; dat = sr.ReadLine(); k = 0; for (i = 0; i <= nd - 1; i++) { dat = sr.ReadLine(); sbuf = dat.Split(delim); if (sbuf[5].Trim() != "0") { ps_name[k] = sbuf[0].Trim(); ps_type[k] = sbuf[1].Trim(); co_YEAR[k] = double.Parse(sbuf[2].Trim()); ps_HD[k] = double.Parse(sbuf[3].Trim()); st_class[k] = sbuf[4].Trim(); tbrl[k] = sbuf[5].Trim(); if (st_class[k] == "400MPa" && ps_type[k] == "C") mark[k] = 0; if (st_class[k] == "490MPa" && ps_type[k] == "C") mark[k] = 1; if (st_class[k] == "490MPa" && ps_type[k] == "P") mark[k] = 2; if (st_class[k] == "570MPa" && ps_type[k] == "C") mark[k] = 3; if (st_class[k] == "570MPa" && ps_type[k] == "P") mark[k] = 4; if (st_class[k] == "690MPa" && ps_type[k] == "C") mark[k] = 5; if (st_class[k] == "690MPa" && ps_type[k] == "P") mark[k] = 6; if (st_class[k] == "780MPa" && ps_type[k] == "P") mark[k] = 7; if (st_class[k] == "950MPa" && ps_type[k] == "P") mark[k] = 8; //Console.WriteLine(k.ToString() + " " + st_class[k]+" " +ps_type[k]+" " +mark[k].ToString()); k = k + 1; } } sr.Close(); nd = k; //********************************************** //gnuplot制御用データ書き込み //********************************************** sw = new System.IO.StreamWriter(fnameW, false, System.Text.Encoding.Default); //dat = "set terminal postscript eps font \"Helvetica\" 12"; sw.WriteLine(dat); dat = "set terminal postscript eps font \"Arial Narrow\" 14"; sw.WriteLine(dat); dat = string.Format("set output \"{0:s}\"",fnameF); sw.WriteLine(dat); dat = "set key left top"; sw.WriteLine(dat); dat = "set key width -10"; sw.WriteLine(dat); dat = "set key box"; sw.WriteLine(dat); dat = "set key title \"Tensile strength and Type of P/S\""; sw.WriteLine(dat); dat = "set key Left top"; sw.WriteLine(dat); dat = "set key spacing 1.2"; sw.WriteLine(dat); dat = string.Format("set xlabel \"{0:s}\"", sxjiku); sw.WriteLine(dat); dat = string.Format("set ylabel \"{0:s}\"", syjiku); sw.WriteLine(dat); dat = string.Format("set xrange [{0:F0}:{1:F0}]",xmin,xmax); sw.WriteLine(dat); dat = string.Format("set xtics {0:F0}", dx); sw.WriteLine(dat); dat = string.Format("set format x \"{0:s}\"","%.0f"); sw.WriteLine(dat); dat = string.Format("set yrange [{0:F0}:{1:F0}]", ymin, ymax); sw.WriteLine(dat); dat = string.Format("set ytics {0:F0}", dy); sw.WriteLine(dat); dat = string.Format("set format y \"{0:s}\"", "%.0f"); sw.WriteLine(dat); dlx = 0.0; dly = 0.0; str = ""; for(i=0;i<=nd-1;i++){ if (tbrl[i] == "t") { dlx = 0.0; dly = 150.0; str = "center"; } if (tbrl[i] == "b") { dlx = 0.0; dly = -120.0; str = "center"; } if (tbrl[i] == "r") { dlx = -0.7; dly = 0.0; str = "right"; } if (tbrl[i] == "l") { dlx = 0.7; dly = 0.0; str = "left"; } xx = co_YEAR[i]+dlx; yy = ps_HD[i] + dly; dat = string.Format("set label \"{0:s}\" at {1:F3},{2:F3} {3:s} font \"Arial Narrow,12\"", ps_name[i], xx, yy, str); sw.WriteLine(dat); } /*軸作成*/ dat = "set grid xtics ytics mxtics mytics"; sw.WriteLine(dat); dat = "plot \\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 12 pointsize 1.0 title \" 400MPa: Conventional \",\\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 6 pointsize 1.0 title \" 490MPa: Conventional \",\\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 7 pointsize 1.0 title \" 490MPa: Pumped-storage\",\\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 4 pointsize 1.0 title \" 570MPa: Conventional \",\\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 5 pointsize 1.0 title \" 570MPa: Pumped-storage\",\\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 8 pointsize 1.0 title \" 690MPa: Conventional \",\\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 9 pointsize 1.0 title \" 690MPa: Pumped-storage\",\\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 21 pointsize 1.3 title \" 780MPa: Pumped-storage\",\\"; sw.WriteLine(dat); dat = "\"-\" with points pointtype 37 pointsize 1.3 title \" 950MPa: Pumped-storage"; sw.WriteLine(dat); for (k = 0; k <= 8; k++) { for (i = 0; i <= nd - 1; i++) { if (mark[i] == k) { dat = string.Format("{0:F0} {1:F0} #{2:s}", co_YEAR[i], ps_HD[i], ps_name[i]); sw.WriteLine(dat); } } dat = "e"; sw.WriteLine(dat); } sw.Close(); //fnameF のフルパス指定 fnameF = System.IO.Path.GetDirectoryName(fnameR) + "\\" + fnameF; //************************************************************** //gnuplot 起動+epsファイル作成 (fnameW: コマンド記述ファイル名) //************************************************************** GPL(fnameW); System.Threading.Thread.Sleep(1000); //1秒待つ //************************************************************** //eps の BoundingBox 修正 (fnameF: eps画像ファイル名) //************************************************************** fnameZ = fnameF.Replace("_eps_", "_eps_z_"); BBOX(fnameF, fnameZ, bboxv); //************************************************************** //ImageMagick による png 画像作成 (fnameZ: eps画像ファイル名) //************************************************************** IMCVT(fnameZ); //************************************************************** //ImageMagick による 白背景合成 (fnameZ: png画像ファイル名) //************************************************************** fnameZ = fnameZ.Replace("eps", "png"); IMCMP(fnameZ); System.Threading.Thread.Sleep(1000); //1秒待つ //************************************************************** //png 画像画面表示 (fnameZ: png画像ファイル名) //************************************************************** fnameZ = fnameZ.Replace("_png_z_", "_png_zw_"); VIEWPNG(fnameZ); } //-------------------------------------------------------------------------------------------- private void GPL(string fnameR) { //***************************************************** //gnuplot 起動+epsファイル作成 //***************************************************** System.Diagnostics.Process pr; System.IO.StreamWriter sw; string dir = ""; string fname = ""; string dat = ""; dir = System.IO.Path.GetDirectoryName(fnameR); System.IO.Directory.SetCurrentDirectory(dir); fname = System.IO.Path.GetFileName(fnameR); pr = new System.Diagnostics.Process(); pr.StartInfo.FileName = "gnuplot.exe"; pr.StartInfo.RedirectStandardInput = true; pr.StartInfo.RedirectStandardOutput = true; pr.StartInfo.UseShellExecute = false; pr.StartInfo.CreateNoWindow = true; pr.Start(); sw = pr.StandardInput; dat = string.Format("load \"{0:s}\"", fname); sw.WriteLine(dat); sw.Close(); pr.WaitForExit(); pr.Close(); } //-------------------------------------------------------------------------------------------- private void BBOX(string fnameR, string fnameW, int[] bboxv) { System.IO.StreamReader sr; System.IO.StreamWriter sw; string dat; int i; Console.WriteLine("BBOX:fnameR=" + fnameR); Console.WriteLine("BBOX:fnameW=" + fnameW); //データ入力 sr = new System.IO.StreamReader(fnameR, System.Text.Encoding.Default); sw = new System.IO.StreamWriter(fnameW, false, System.Text.Encoding.Default); i = 0; while (!sr.EndOfStream) { i = i + 1; dat = sr.ReadLine(); if (i == 6) dat = string.Format("%%BoundingBox: {0:D} {1:D} {2:D} {3:D}", bboxv[0], bboxv[1], bboxv[2], bboxv[3]); sw.WriteLine(dat); } sr.Close(); sw.Close(); } //-------------------------------------------------------------------------------------------- private void IMCVT(string fnameF) { //***************************************************** //ImageMagick convert で png 画像作成 //***************************************************** string fnameP; System.Diagnostics.Process pr; System.IO.StreamWriter sw; string dat = ""; fnameP = fnameF.Replace("eps", "png"); pr = new System.Diagnostics.Process(); pr.StartInfo.FileName = "cmd.exe"; pr.StartInfo.RedirectStandardInput = true; pr.StartInfo.RedirectStandardOutput = true; pr.StartInfo.UseShellExecute = false; pr.StartInfo.CreateNoWindow = true; pr.Start(); sw = pr.StandardInput; dat = string.Format("convert -density 300 {0:s} {1:s}", fnameF, fnameP); sw.WriteLine(dat); sw.Close(); pr.WaitForExit(); pr.Close(); } //-------------------------------------------------------------------------------------------- private void IMCMP(string fnameZ) { //***************************************************** //ImageMagick convert で 白背景合成画像作成 //***************************************************** string fnameP; string fnameB; System.Diagnostics.Process pr; System.IO.StreamWriter sw; string dat = ""; Image img; int ww; int hh; img = Image.FromFile(fnameZ); ww = img.Width; hh = img.Height; img.Dispose(); fnameP = fnameZ.Replace("_z_", "_zw_"); fnameB = System.IO.Path.GetDirectoryName(fnameP) + "\\base.png"; pr = new System.Diagnostics.Process(); pr.StartInfo.FileName = "cmd.exe"; pr.StartInfo.RedirectStandardInput = true; pr.StartInfo.RedirectStandardOutput = true; pr.StartInfo.UseShellExecute = false; pr.StartInfo.CreateNoWindow = true; pr.Start(); sw = pr.StandardInput; dat = string.Format("convert -size {0:D}x{1:D} xc:\"#FFFFFF\" {2:s}", ww, hh, fnameB); sw.WriteLine(dat); dat = string.Format("convert {0:s} {1:s} -composite {2:s}", fnameB, fnameZ, fnameP); sw.WriteLine(dat); sw.Close(); pr.WaitForExit(); pr.Close(); Console.WriteLine(fnameB + " " + fnameZ + " " + fnameP); } //-------------------------------------------------------------------------------------------- private void VIEWPNG(string fnameZ) { //***************************************************** //png 画像画面表示 (50%縮尺) //***************************************************** Image img; Bitmap bmp; Graphics g; float ratio = 0.5f; int ww; int hh; int w_org; int h_org; img = Image.FromFile(fnameZ); w_org = img.Width; h_org = img.Height; ww = (int)(ratio * w_org); hh = (int)(ratio * h_org); pictureBox1.Size = new Size(ww, hh); bmp = new Bitmap(ww, hh); pictureBox1.Image = bmp; g = Graphics.FromImage(pictureBox1.Image); g.DrawImage(img, 0, 0, ww, hh); g.Dispose(); img.Dispose(); pictureBox1.Left = 0; pictureBox1.Top = toolStrip1.Height; this.ClientSize = new Size(pictureBox1.Width, pictureBox1.Height + toolStrip1.Height + statusStrip1.Height); toolStripStatusLabel1.Text = string.Format("{0:D} x {1:D} : {2:s}", w_org, h_org, fnameZ); } //-------------------------------------------------------------------------------------------- } }