2016-11-25 21 views
0

我需要从下面的文本行中提取以下数据(粗体),并将其放入数据网格中;从一行文本中获取特定数据

PERS tooldata t_rrt_ja03579:= [TRUE,[[-39.643,-0.001,1025.49],[0.382684,-0.000130001,-0.923889,0.000120001]],[200.9,[ - 88.1 - 12.6,359.7],[1,0,0,0],29.347,50.927,18.261]];

这行是从文件中读取的。我设法修剪了这条线,所以它摆脱了“PERS tooldata”和空格,并且留下了工具名称。我将它绑定到代码中其他地方的datagrid中的数据,这些代码完成了第1步。

我的问题是我怎样才能提取单独的粗体的值,并把它们放在双倍的数据声明?第一块数值(-39.643,-0.001,1025.49)是X,Y,Z坐标值,第二块(0.382684,-0.000130001,-0.923889,0.000120001)是Q1,Q2,Q3,Q4。

下面是我如何做名

private void AutoFillToolData(object sender, RoutedEventArgs e) 
    { 
     // Gives user option to auto populate datagrid 
     var AutoFillToolResult = MessageBox.Show("Do you want to auto populate fields?", "Tool Data", MessageBoxButton.YesNo); 
     if (AutoFillToolResult == MessageBoxResult.Yes) 
     { 
      // User directs application to the specified file 
      System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog(); 
      if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       // Application looks for specific file and removes unwanted data 
       string robotBackupPath = folderBrowser.SelectedPath; 
       string allDataPath = robotBackupPath + @"\RAPID\TASK1\SYSMOD\ALL_DATA.sys"; 
       string[] tLines = File.ReadAllLines(allDataPath); 
       List<string> toolDataLines = new List<string>(); 
       foreach (string tLine in tLines) 
       { 
        if (tLine.Contains("PERS tooldata") && !tLine.StartsWith("!")) 
        { 
         if (tLine.Contains("tToolChanger")) continue; 
         if (tLine.Contains("tPointer")) continue; 
         if (tLine.Contains("tHome")) continue; 
         toolDataLines.Add(tLine); 
        } 
       } 

       foreach (string line in toolDataLines) 
       { 
        // Gets the name of the tool 
        ToolData toolData = GetToolNameFromLine(line); 

        // Puts the tool name in the DataGrid 
        TCPData.Add(toolData); 
       } 
      } 
     } 
    } 

    private ToolData GetToolNameFromLine(string line) 
    { 
     // Removes white space at the beggining of line in txt file 
     ToolData tooldata = new ToolData(); 
     string[] spaceSplit = line.Trim().Split(' '); 
     string values = spaceSplit[2]; 

     // Gets Tool Name 
     int colonLocation = values.IndexOf(":"); 
     tooldata.ToolName = values.Substring(0, colonLocation); 
     return tooldata; 
    } 
+0

特别是对文本的过滤器它不是坏的,以提供多个例子来看看静态部分。 – C4u

回答

0

如果所有你必须遵循相同的模式样本,似乎并提取这些值并不难:

//First we get all the string after the := 
string tooldata = line.Substring(data.IndexOf(":=") + 2) ; 

//Split the string by [   
string[] tooldataArray = tooldata.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries); 
//the second and the third strings are what we are interested in 
string xyzValue = tooldataArray[1].Replace(']' ,' '); 
string Q1234value = tooldataArray[2].Replace(']', ' '); 

如果之后你想要获得个别参数,只需要分开,就行。

编辑

这将提取所有你想要的值的double阵列:

string tooldata = data.Substring(data.IndexOf(":=") + 2) ; 

string[] tooldataArray = tooldata.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries); 
double[] xyzValue = tooldataArray[1].Replace(']' ,' ') 
        .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) 
        .Select(s => double.Parse(s, CultureInfo.InvariantCulture)) 
        .ToArray(); 
double[] Q1234value = tooldataArray[2].Replace(']', ' ') 
        .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) 
        .Select(s => double.Parse(s, CultureInfo.InvariantCulture)) 
        .ToArray(); 
+0

得到了现在的工作,感谢您的指导。 –

相关问题