Advertisement
Robomatics

Obj File Read and Write

Jan 3rd, 2013
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.20 KB | None | 0 0
  1. 'Writing an Obj File
  2. 'Vectordata is an arry structure of decimals, with X,Y and Z.
  3. 'Colordata is an array of drawing.color types
  4.  
  5. Dim pointstring(vectordata.Count - 1) As String
  6.         For i = 0 To vectordata.Count - 1
  7.  
  8.             pointstring(i) = "v " & vectordata(i).X.ToString & " " & vectordata(i).Y.ToString & " " & vectordata(i).Z.ToString & " " & colordata(i).R / 255 & " " & colordata(i).G / 255 & " " & colordata(i).B / 255
  9.  
  10.  
  11.         Next
  12.         GC.Collect()
  13.         IO.File.WriteAllLines(SaveFileDialog1.FileName, pointstring)
  14.         GC.Collect()
  15.  
  16.  
  17. 'Reading a Obj File
  18.  
  19. Dim pointsstring() As String 'The array of lines to read from file
  20.  
  21.         GC.Collect()
  22.         pointsstring = System.IO.File.ReadAllLines(filelocation)
  23.         GC.Collect()
  24.  
  25.         points(objectcount) = New List(Of Vector3) 'Vector3 is a structure containing an X,Y and Z
  26.  
  27.         Dim X1, Y1, Z1 As Single
  28.  
  29.         For i = 0 To pointsstring.Length - 1
  30.             If pointsstring(i).Length > 2 Then
  31.                 If pointsstring(i)(0) = "v" And pointsstring(i)(1) = " " Then
  32.  
  33.                     Dim tempstring = pointsstring(i)
  34.  
  35.                     pointsstring(i) = pointsstring(i).Substring(pointsstring(i).IndexOf(" ") + 1)
  36.                     Dim XST As String = pointsstring(i).Substring(0, pointsstring(i).IndexOf(" "))
  37.                     pointsstring(i) = pointsstring(i).Substring(pointsstring(i).IndexOf(" ") + 1)
  38.                     Dim YST As String = pointsstring(i).Substring(0, pointsstring(i).IndexOf(" "))
  39.                     pointsstring(i) = pointsstring(i).Substring(pointsstring(i).IndexOf(" ") + 1)
  40.  
  41.                     Dim tempint As Integer = pointsstring(i).IndexOf(" ")
  42.                     Dim ZST As String
  43.                     If tempint <> -1 Then
  44.                         ZST = pointsstring(i).Substring(0, pointsstring(i).IndexOf(" "))
  45.                     Else
  46.                         ZST = pointsstring(i)
  47.                     End If
  48.  
  49.                     X1 = XST
  50.                     Y1 = YST
  51.                     Z1 = ZST
  52.  
  53.                 End If
  54.             End If
  55.  
  56.             points(objectcount).Add(New Vector3(X1, Y1, Z1))
  57.         Next
  58.  
  59.         objectcount = objectcount + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement