Advertisement
Robomatics

Custom File Save and Read

Jan 3rd, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.79 KB | None | 0 0
  1. 'Saving my custom file type, a collection of points with colors
  2.  
  3.  
  4.         Dim index As Integer = NumericUpDown1.Value
  5.         If vectordata.Count = 0 Then 'Vectordata is an array (list) of a structure that contains X,Y, and Z
  6.             MsgBox("Nothing to save")
  7.             Return
  8.         End If
  9.  
  10.         Dim bytes As List(Of Byte) = New List(Of Byte)
  11.  
  12.         For i = 0 To vectordata(index).Count - 1
  13.             Dim pointX() As Byte = BitConverter.GetBytes(vectordata(index)(i).X)
  14.             Dim pointY() As Byte = BitConverter.GetBytes(vectordata(index)(i).Y)
  15.             Dim pointZ() As Byte = BitConverter.GetBytes(vectordata(index)(i).Z)
  16.             bytes.Add(pointX(0))
  17.             bytes.Add(pointX(1))
  18.             bytes.Add(pointX(2))
  19.             bytes.Add(pointX(3))
  20.             bytes.Add(pointY(0))
  21.             bytes.Add(pointY(1))
  22.             bytes.Add(pointY(2))
  23.             bytes.Add(pointY(3))
  24.             bytes.Add(pointZ(0))
  25.             bytes.Add(pointZ(1))
  26.             bytes.Add(pointZ(2))
  27.             bytes.Add(pointZ(3))
  28.             bytes.Add(colordata(index)(i).R)
  29.             bytes.Add(colordata(index)(i).G)
  30.             bytes.Add(colordata(index)(i).B)
  31.         Next
  32.  
  33.         Dim bytez(bytes.Count - 1) As Byte
  34.         For i = 0 To bytes.Count - 1
  35.             bytez(i) = bytes(i)
  36.         Next
  37.  
  38.         GC.Collect()
  39.         IO.File.WriteAllBytes(SFD1.FileName, bytez)
  40.         GC.Collect()
  41.  
  42. 'Reading my custom file type
  43.  
  44.  
  45.         Dim filelocation() As String = OFD1.FileNames 'OFD1 is the openfiledialog that is enabled for multiple files
  46.         Dim bytez() As Byte
  47.  
  48.         For i2 = 0 To filelocation.Length - 1
  49.             If System.IO.File.Exists(filelocation(i2)) = True Then
  50.  
  51.                 GC.Collect()
  52.                 bytez = System.IO.File.ReadAllBytes(filelocation(i2))
  53.                 GC.Collect()
  54.  
  55.                 vectordata.Add(New List(Of Vector3))
  56.                 colordata.Add(New List(Of Color))
  57.                 vectordata(currentframe) = New List(Of Vector3)
  58.                 colordata(currentframe) = New List(Of Color)
  59.  
  60.                 rotatedata.Add(New Vector3(0, 0, 0))
  61.                 translatedata.Add(New Vector3(0, 0, 0))
  62.  
  63.                 For i = 0 To bytez.Length - 1 Step 15
  64.                     Dim Px As Single = BitConverter.ToSingle(bytez, i + 0)
  65.                     Dim Py As Single = BitConverter.ToSingle(bytez, i + 4)
  66.                     Dim Pz As Single = BitConverter.ToSingle(bytez, i + 8)
  67.                     Dim Pc As Color = Color.FromArgb(bytez(i + 12), bytez(i + 13), bytez(i + 14))
  68.  
  69.                     vectordata(currentframe).Add(New Vector3(Px, Py, Pz))
  70.                     colordata(currentframe).Add(Pc)
  71.                 Next
  72.  
  73.                 currentframe = currentframe + 1
  74.             End If
  75.  
  76.         Next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement