SHARE
TWEET

xyz (csv) writer class

a guest Dec 22nd, 2011 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Class xyzFile
  2.     Public xValues() As Double
  3.     Public yValues() As Double
  4.     Public zValues() As Double
  5.     Public xCount As Integer
  6.     Public yCount As Integer
  7.     Public zCount As Integer
  8.     Public decimalSep As String = "."
  9.     Public xyzSeperator As String = ""
  10.     Public prevSave As Boolean = False
  11.     Public prevSavePath As String = ""
  12.     Public serialNumColumn As Boolean = False
  13.  
  14.     Sub isSerialNum(ByVal issetValue As Boolean)
  15.         If issetValue = True Then
  16.             Me.serialNumColumn = True
  17.         Else
  18.             Me.serialNumColumn = False
  19.         End If
  20.  
  21.     End Sub
  22.  
  23.  
  24.     Sub setXYZSeperator(ByVal strings As String)
  25.         If strings = "Value seperator" Or strings = "" Then
  26.             Me.xyzSeperator = " "
  27.         Else
  28.             Me.xyzSeperator = strings
  29.         End If
  30.     End Sub
  31.  
  32.     Sub setCommaAsDecimal(ByVal isComma As Boolean)
  33.         If isComma = True Then
  34.             decimalSep = ","
  35.         Else
  36.             decimalSep = "."
  37.         End If
  38.     End Sub
  39.  
  40.  
  41.  
  42.     Sub setX(ByVal xvalueString As String)
  43.         Dim xValArray() As Double
  44.         Dim tmpValue As String
  45.         Dim valueDbl As Double
  46.         Dim i As Integer
  47.  
  48.         xValArray = splitStringByLine(xvalueString)
  49.  
  50.         ReDim xValues(UBound(xValArray))
  51.  
  52.         For i = 0 To UBound(xValArray)
  53.  
  54.             tmpValue = xValArray(i)
  55.             tmpValue = Replace(tmpValue, ",", DecimalSeparator)
  56.             tmpValue = Replace(tmpValue, ".", DecimalSeparator)
  57.             valueDbl = CDbl(tmpValue)
  58.  
  59.             xValues(i) = valueDbl
  60.         Next i
  61.  
  62.         Me.xCount = i
  63.  
  64.  
  65.  
  66.     End Sub
  67.  
  68.  
  69.     Sub setY(ByVal yvalueString As String)
  70.         Dim yValArray() As Double
  71.         Dim tmpValue As String
  72.         Dim valueDbl As Double
  73.         Dim i As Integer
  74.  
  75.  
  76.         yValArray = splitStringByLine(yvalueString)
  77.  
  78.         ReDim yValues(UBound(yValArray))
  79.  
  80.         For i = 0 To UBound(yValArray)
  81.  
  82.             tmpValue = yValArray(i)
  83.             tmpValue = Replace(tmpValue, ",", DecimalSeparator)
  84.             tmpValue = Replace(tmpValue, ".", DecimalSeparator)
  85.             valueDbl = CDbl(tmpValue)
  86.  
  87.             yValues(i) = valueDbl
  88.  
  89.         Next i
  90.  
  91.         Me.yCount = i
  92.  
  93.     End Sub
  94.  
  95.     Sub setZ(ByVal zvalueStirng As String)
  96.         Dim zValArray() As Double
  97.         Dim tmpValue As String
  98.         Dim valueDbl As Double
  99.         Dim i As Integer
  100.  
  101.         zValArray = splitStringByLine(zvalueStirng)
  102.  
  103.         ReDim zValues(UBound(zValArray))
  104.  
  105.         For i = 0 To UBound(zValArray)
  106.  
  107.             tmpValue = zValArray(i)
  108.             tmpValue = Replace(tmpValue, ",", DecimalSeparator)
  109.             tmpValue = Replace(tmpValue, ".", DecimalSeparator)
  110.             valueDbl = CDbl(tmpValue)
  111.  
  112.             zValues(i) = valueDbl
  113.         Next i
  114.  
  115.         Me.zCount = i
  116.  
  117.     End Sub
  118.  
  119.     Sub checkLenght()
  120.         If Not (Me.xCount = Me.zCount And Me.xCount = Me.yCount And Me.zCount = Me.yCount) Then
  121.             Error (1)
  122.         End If
  123.     End Sub
  124.  
  125.  
  126.     Sub writeValues(ByVal path As String)
  127.  
  128.         Dim valueArray As String = ""
  129.         Dim tempX As String
  130.         Dim tempY As String
  131.         Dim tempZ As String
  132.         Dim serialNumColumnString As String = ""
  133.  
  134.         For i = 0 To Me.yCount - 1
  135.  
  136.             tempX = Me.xValues(i)
  137.             tempX = Replace(tempX, ".", Me.decimalSep)
  138.             tempX = Replace(tempX, ",", Me.decimalSep)
  139.  
  140.             tempY = Me.yValues(i)
  141.             tempY = Replace(tempY, ".", Me.decimalSep)
  142.             tempY = Replace(tempY, ",", Me.decimalSep)
  143.  
  144.             tempZ = Me.zValues(i)
  145.             tempZ = Replace(tempZ, ".", Me.decimalSep)
  146.             tempZ = Replace(tempZ, ",", Me.decimalSep)
  147.  
  148.             If Me.serialNumColumn = True Then
  149.                 serialNumColumnString = i & Me.xyzSeperator
  150.             End If
  151.  
  152.             valueArray = valueArray & serialNumColumnString & tempX & Me.xyzSeperator & tempY & Me.xyzSeperator & tempZ & vbCrLf
  153.  
  154.  
  155.         Next
  156.  
  157.         System.IO.File.WriteAllText(path, valueArray)
  158.  
  159.     End Sub
  160.  
  161.  
  162.     Function splitStringByLine(ByVal strings As String, Optional ByVal warnings As String = "Empty data values has been found.")
  163.         Dim masivs() As String
  164.         Dim dblMasivs() As Double
  165.         Replace(strings, vbCr, vbCrLf)
  166.         Replace(strings, vbLf, vbCrLf)
  167.         masivs = Split(strings, vbCrLf)
  168.         dblMasivs = checkElement(masivs, warnings)
  169.         Return dblMasivs
  170.     End Function
  171.  
  172.     Private Function checkElement(ByVal masivs() As String, Optional ByVal warning As String = "Empty values has been found.")
  173.         Dim j As Integer
  174.         Dim i As Integer
  175.         Dim lastEmpty As Boolean
  176.         Dim dblMasivs() As Double
  177.         Dim ubounds As Integer
  178.         Dim tmpMasivs As String
  179.  
  180.         'janočeko, vai pēdējais elements ir tukÅ¡s.
  181.         If (masivs(UBound(masivs))) = String.Empty Then
  182.             lastEmpty = True
  183.             ubounds = UBound(masivs) - 1
  184.         Else
  185.             ubounds = UBound(masivs)
  186.         End If
  187.         'janodefinē dbl masÄ«va augÅ¡a
  188.         ReDim dblMasivs(ubounds)
  189.  
  190.  
  191.  
  192.         'katrs masÄ«va elements jāpārvērÅ¡ par double
  193.         'ja ir tukÅ¡s elements, tad jāatstāj tukÅ¡s pa vidu
  194.         For i = 0 To ubounds
  195.             If Not (masivs(i) = String.Empty) Then
  196.  
  197.                 tmpMasivs = Replace(masivs(i), ".", DecimalSeparator)
  198.                 tmpMasivs = Replace(tmpMasivs, ",", DecimalSeparator)
  199.  
  200.                 dblMasivs(i) = CDbl(tmpMasivs)
  201.             Else
  202.                 dblMasivs(i) = Double.NaN
  203.             End If
  204.  
  205.         Next i
  206.  
  207.         'jāsaskaita tukÅ¡ie elementi pa vidu un ja ir, tad
  208.         'jādod brÄ«dinājums
  209.         j = 0
  210.         For i = 0 To UBound(dblMasivs)
  211.             If Double.IsNaN(dblMasivs(i)) Then
  212.                 j = j + 1
  213.             End If
  214.  
  215.         Next i
  216.  
  217.  
  218.         If j > 0 Then
  219.             MsgBox(warning & vbCrLf & "Count: " & j)
  220.         End If
  221.  
  222.         Return dblMasivs
  223.     End Function
  224. End Class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top