Advertisement
kijato

c#_Leica_frt_to_csv

Feb 22nd, 2020 (edited)
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.91 KB | None | 0 0
  1. /*
  2. #!/usr/bin/mcs -r:System.Windows.Forms.dll -r:System.Drawing.dll
  3.  
  4. set CSC=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\
  5. set CSC=c:\Windows\Microsoft.NET\Framework64\v4.0.30319\
  6. set path=%csc%;%path%
  7.  
  8. csc.exe -debug+ -target:winexe -r:System.Windows.Forms.dll -r:System.Drawing.dll C#_Leica_frt_to_csv.cs
  9. csc.exe -debug+ -target:exe -r:System.Windows.Forms.dll,System.Drawing.dll -platform:x64 C#_Leica_frt_to_csv.cs && c#_leica_frt_to_csv.exe
  10.  
  11. */
  12.  
  13. using System;
  14. using System.Windows.Forms;
  15. using System.Drawing;
  16.  
  17. using System.IO;
  18. using System.Text.RegularExpressions;
  19. using System.Collections; // for Hashtable()
  20. using System.Collections.Generic; // for List()
  21. using System.Text; // Encoding
  22. using System.Data; // DataTable
  23.  
  24. using System.Reflection; // https://codecharm.com/blog/archive/2017-11-19-c-assembly-automatic-versioning/
  25. [assembly: AssemblyVersion("1.0.*")]
  26. [assembly: AssemblyFileVersion("1.0.1.123")]
  27.  
  28.  
  29. public class Program
  30. {
  31.  
  32.     //static Button myButton;
  33.     static MenuStrip strip = new MenuStrip();
  34.     static StatusStrip statusStrip = new StatusStrip();
  35.     static ToolStripStatusLabel statusLabel = new ToolStripStatusLabel();  
  36.     static ToolStripStatusLabel statusLabel2 = new ToolStripStatusLabel(); 
  37.  
  38.     static List<Dictionary<String,String>> pontok;
  39.  
  40.     static DataGridView dataGridView = new DataGridView();
  41.    
  42.     [STAThread]
  43.     public static void Main() {
  44.         try {
  45.  
  46.         var f = new Form();
  47.             f.Text = Assembly.GetExecutingAssembly().GetName().Name + " [v" +
  48.                      Assembly.GetExecutingAssembly().GetName().Version +"]";
  49.             f.Size = new Size(600, 400);
  50.             /*
  51.             myButton = new Button { Location = new Point(f.Width-100, f.Height-70), Text = "Megnyit..." , Anchor = ( AnchorStyles.Bottom | AnchorStyles.Right ) };
  52.             f.Controls.Add(myButton);
  53.             myButton.Click += new EventHandler (ButtonClick);
  54.             */
  55.             ToolStripMenuItem fileItem = new ToolStripMenuItem("&Fájl");
  56.             var openFileMenuItem = fileItem.DropDownItems.Add("&Megnyitás");
  57.             openFileMenuItem.Click += new EventHandler (ButtonClick);
  58.             var saveFileMenuItem = fileItem.DropDownItems.Add("M&entés");
  59.                 saveFileMenuItem.Enabled = false;
  60.                 // username.Click += (s, e) => SomeTextBox.Text = "test";
  61.             var quitMenuItem = fileItem.DropDownItems.Add("&Kilépés");
  62.                 quitMenuItem.Click += new EventHandler (Quit);
  63.             strip.Items.Add(fileItem);
  64.             f.Controls.Add(strip);
  65.  
  66.             statusStrip.Items.AddRange(new ToolStripItem[] {statusLabel});
  67.             statusStrip.Items.AddRange(new ToolStripItem[] {statusLabel2});
  68.             statusLabel2.Alignment = ToolStripItemAlignment.Right;
  69.             statusStrip.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
  70.             f.Controls.Add(statusStrip);
  71.  
  72.             dataGridView.Location = new Point(5, 30);
  73.             dataGridView.Size = new Size(f.Width-25, f.Height-95);
  74.             dataGridView.Anchor = ( AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right );
  75.             dataGridView.ReadOnly = true;
  76.             dataGridView.MultiSelect = true;
  77.             dataGridView.AllowUserToAddRows = false;
  78.             dataGridView.AutoResizeColumns();
  79.             dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  80.             f.Controls.Add(dataGridView);
  81.  
  82.             Application.Run(f);
  83.  
  84.         } catch (Exception e) {
  85.             statusLabel.Text = e.Message;
  86.         }
  87.     }
  88.  
  89.     static private void Quit (object sender, EventArgs e) {
  90.         Application.Exit();
  91.     }
  92.  
  93.     static private void ButtonClick (object sender, EventArgs e) {
  94.  
  95.         OpenFileDialog openDialog = new OpenFileDialog();
  96.         openDialog.Title = "Select A File";
  97.         openDialog.Filter = "MJK Files (*.mjk)|*.mjk" + "|" +
  98.                             "Text Files (*.txt)|*.txt" + "|" +
  99.                             "All Files (*.*)|*.*";
  100.         openDialog.InitialDirectory = Environment.CurrentDirectory;
  101.         if ( openDialog.ShowDialog() == DialogResult.OK ) {
  102.  
  103.             statusLabel.Text = openDialog.FileName;
  104.  
  105.             int i=0;
  106.             string row = String.Empty;
  107.             try {
  108.                 //String file = File.ReadAllText(myLabel.Text);
  109.                 string[] lines = File.ReadAllLines(statusLabel.Text, Encoding.Default);
  110.  
  111.                 pontok = new List<Dictionary<String,String>>();
  112.                 foreach (string line in lines) {
  113.                    
  114.                     i++;
  115.  
  116.                     if ( line.Contains("Meghat") || line.Contains("Y=") || line.Contains("X=") || line.Contains("h=") || line.Contains("hib") || line.Contains("Dátum") ) {
  117.  
  118.                         row+=line+";";
  119.  
  120.                     } else if ( (Regex.Match(line,@"-{20,}")).Success ) {
  121.  
  122.                         row = Regex.Replace(row, @"(\s{2,}|\t+)", ";");
  123.                         row = Regex.Replace(row, @"=", ":");
  124.  
  125.                         //Hashtable pont = new Hashtable();
  126.                          Dictionary<String,String> pont = new  Dictionary<String,String>();
  127.                         foreach ( string data in row.Split(';') ) {
  128.                             string[] kv = data.Split(':');
  129.                             if(kv.Length==2) pont.Add(kv[0],kv[1]);
  130.                         }
  131.                          if(!pont.Count.Equals(0)) {
  132.                             pontok.Add(pont);
  133.                          }
  134.                         row=String.Empty;
  135.                     }
  136.     System.Console.Write("pontok feltöltése: "+i+"/"+lines.Length + "\r");
  137.                     statusLabel2.Text = i+"/"+lines.Length;
  138.                     statusStrip.Refresh();
  139.                 }
  140.     System.Console.WriteLine("\nPontok száma: " + pontok.Count);
  141.                
  142.                 // Adatok
  143.                 //dataGridView.AutoGenerateColumns = false;
  144.                 dataGridView.DataSource = null;
  145.                 //dataTable.Reset();
  146.                 //dataTable.Clear();
  147.                 DataTable dataTable = new DataTable();
  148.  
  149.                 // Fejléc
  150.                 foreach ( var key in pontok[0].Keys ) {
  151.                     dataTable.Columns.Add(key.ToString(),typeof(string));
  152.                 }
  153.  
  154.                 //foreach ( Hashtable p in pontok ) {
  155.                 i=0;
  156.                 foreach ( Dictionary<String,String>  p in pontok ) {
  157.                     i++;
  158.                     //foreach (DictionaryEntry entry in p) {
  159.                     List<string> r = new List<string>();
  160.                     foreach (KeyValuePair<String,String> entry in p) {
  161.                         r.Add(entry.Value);
  162.                     }
  163.                     dataTable.Rows.Add(r.ToArray());
  164.     System.Console.Write("dataTable feltöltés: "+i+"/"+pontok.Count + "\r");
  165.                 }
  166.     System.Console.WriteLine("\ndataTable száma: " + dataTable.Rows.Count);
  167.                
  168.     System.Console.WriteLine("table to grid..."+DateTime.Now.ToString("h:mm:ss tt"));
  169.                 dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; // Enélkül folyamatosan újraszámol, igazítja a cellákat, ami sokszorosára növeli az időigényt.
  170.                 dataGridView.DataSource = dataTable;
  171.     System.Console.WriteLine("done."+DateTime.Now.ToString("h:mm:ss tt"));
  172.                 foreach (DataGridViewRow dgrow in dataGridView.Rows) {
  173.                     dgrow.HeaderCell.Value = String.Format("{0}", dgrow.Index + 1);
  174.                     dgrow.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  175.                 }
  176.                 //dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
  177.                 dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  178.                 //dataGridView.Update();
  179.                 //dataGridView.Refresh();
  180.     System.Console.WriteLine("->"+DateTime.Now.ToString("h:mm:ss tt"));
  181.  
  182.             } catch (Exception e2) {
  183.                 statusLabel.Text=e2.Message + " (sorszám: "+i+")";
  184.                 System.Console.WriteLine("["+row+"]");
  185.             }
  186.         }
  187.        
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement