Example 11 - vrml1to2.swb
Yes this is a big one. This program is unique in that it does not use SW API at all. It uses Visual Basic code to perform simple file I/O (Input/Output), check for the existance of a file, string manipulation etc. It was fun to write and offers some great functionality if you want to work with VRML files.
' ******************************************************************************
' VRML1TO2.SWB
'
' macro written on 01/25/00 by
' Joe Jones joe@nhcad.com
' NEW HAMPSHIRE CAD www.nhcad.com
'
' This macro will translate a VRML 1.0 file (generated by SolidWorks) to a
' VRML 2.0 style file. It puts all material definitions at the top of the file.
' By editing the material definitions at the top of the file, you can easily
' fine tune the properties of those faces.
'
' 10/10/00
' * comment out DIRECTIONAL LIGHT
' * automatically replaces "emissiveColor" with "diffuseColor"
' * create simple avatar and comment it out
' * choose units for conversion
' ******************************************************************************
Dim infile, outfile As String
Dim dataStr As String
Dim I, materialCNT As Integer
Dim materialRA ( 1 to 50 ) As String
Dim Exists As Boolean
Dim Conversion As Double
Dim Units As String
Private Sub readLine()
Dim dataChr As String
dataStr = ""
While dataStr = "" And Not EOF( 2 )
dataChr = Input( 1, #2)
While Not (dataChr = Chr(13) or dataChr = Chr(10))
dataStr = dataStr + dataChr
dataChr = Input( 1, #2 )
Wend
Wend
dataStr = Trim( dataStr )
End Sub
Private Function convertUnits( dataStr, conversion )
Dim num1, num2, num3 as Double
Dim tempStr As String
Dim I As Integer
tempStr = Trim( dataStr )
I = InStr( 1, tempStr, " " )
num1 = Val( Mid( tempStr, 1, I - 1 ))
tempStr = Trim ( Right( tempStr, Len( tempStr) - I + 1 ))
I = InStr( 1, tempStr, " " )
num2 = Val( Mid( tempStr, 1, I - 1 ))
tempStr = Trim ( Mid( tempStr, I, Len( tempStr ) - I))
num3 = Val( tempStr )
num1 = num1 * conversion
num2 = num2 * conversion
num3 = num3 * conversion
tempStr = format(num1, "0.000000000") + " " + _
format(num2, "0.000000000") + " " + _
format(num3, "0.000000000") + ","
'msgbox(datastr + Chr(13) + Chr(13) + tempStr)
convertUnits = tempStr
End Function
Sub main()
materialCNT = 0
lastMaterial = ""
infile = ""
While Dir( infile ) = "" Or infile = "" ' see if file exists
infile = InputBox ( "enter filename to convert", + _
"Source Filename",infile )
If infile = "" Then End ' user canceled
If Dir( infile ) = "" Then Beep
Wend
outfile = infile
While outfile = infile ' keep asking until target file is different
outfile = InputBox ( "Enter name of new file." + Chr(13) + _
"You can NOT use the same name shown here.", "Target Filename", infile )
If outfile = "" Then End ' user canceled
If outfile = infile Then Beep
Wend
Units = InputBox ("1 - mm" + _
" 2 - cm" + Chr(13) + "3 - m " + " 4 - in" + Chr(13) + _
"5 - ft", "Output Units", "3")
Conversion = 1
Select Case Trim(Units)
Case "1"
Conversion = 1000
Case "2"
Conversion = 100
Case "4"
Conversion = 39.36996
Case "5"
Conversion = 3.28083
End Select
Open outfile For Output As #1
Print #1, "#VRML V2.0 utf8"
Print #1, ""
Print #1, "WorldInfo {"
Print #1, " title " + Chr( 34 ) + outfile + " " + Str( Date ) + Chr( 34 )
Print #1, ""
Print #1, " info [" + Chr( 34 ) + infile + " converted by VRML1TO2.SWB macro" + Chr( 34 )
Print #1, " " + Chr( 34 ) + "author: Joe Jones joe@nhcad.com " + Chr( 34 )
Print #1, " " + Chr( 34 ) + "New Hampshire CAD www.nhcad.com " + Chr( 34 ) + " ]"
Print #1, "}"
Print #1, ""
Print #1, ""
Print #1, ""
Print #1, "# you may edit the material properties as follows"
Print #1, ""
Print #1, "# The folowing use (r, g, b) colors 0-1 each"
Print #1, "# ------------------------------------------"
Print #1, "# diffuseColor = The normal color of the object"
Print #1, "# specularColor = The color of highlights on shiny objects"
Print #1, "# emissiveColor = The object *glows* with a light of its own of this color."
Print #1, "# It doesn't case light on any other objects though."
Print #1, ""
Print #1, "# The following use 0-1 each"
Print #1, "# --------------------------"
Print #1, "# ambientIntensity= The amount of ambient light that the object reflects."
Print #1, "# shininess = How reflective the object is."
Print #1, "# transparency = How transparent the object is. Not all browsers support this."
Print #1, ""
Print #1, "# emissiveColor was changed to diffuseColor"
Print #1, "# comment out DIRECTIONAL LIGHT"
Print #1, ""
Print #1, "# The following is an example of an avitar (person walking around)"
Print #1, "# Uncomment the following lines to get it working"
Print #1, ""
Print #1, "# NavigationInfo {"
Print #1, "# # avatarSize [min dist to collision, height of user, max step height]"
Print #1, "# avatarSize [.2, 2, 1]"
Print #1, "# speed 4"
Print #1, "# headlight TRUE"
Print #1, "# }"
Print #1, "# Viewpoint {"
Print #1, "# position 2.44 1.22 -2.44"
Print #1, "# orientation 0 1 0 0"
Print #1, "# description " + Chr(34) + "Start" + Chr(34)
Print #1, "# }"
Print #1, ""
Print #1, ""
' parse file and strip out materials to put at top of VRML file
Open infile For Input As #2
While NOT EOF( 2 )
readLine
If InStr( 1, dataStr, "Material") Then
Exists = False
' replace emissiveColor with diffuseColor
I = InStr( 1, dataStr, "emissiveColor")
Mid( dataStr, I, 13 ) = "diffuseColor "
' check to see if material already exists
For I = 1 To materialCNT
If dataStr = materialRA(I) Then Exists = True
Next I
If Not Exists Then
Print #1, "Shape { appearance DEF App" + Trim( Str( I ) ) + _
" Appearance { material " + dataStr + " } }"
materialCNT = materialCNT + 1
materialRA( materialCNT ) = dataStr
End If
End If
Wend
Close #2
J = 0
' parse file again - create geometry and lights this time
Open infile For Input As #2
While NOT EOF( 2 )
readLine
J = J + 1
' last material to be found
If InStr( 1, dataStr, "Material" ) Then
' replace emissiveColor with diffuseColor
I = InStr( 1, dataStr, "emissiveColor")
Mid( dataStr, I, 13 ) = "diffuseColor "
For I = 1 To materialCNT
If dataStr = materialRA( I ) Then useMaterial = I
Next I
End If
' see if this is a DirectionalLight
If InStr( 1, dataStr, "DirectionalLight" ) Then
Print #1, "#" + dataStr
Print #1, ""
End If
' see if this is the begginning of a point set
If InStr( 1, dataStr, "point [" ) Then
Print #1, "Shape {"
Print #1, "appearance USE App" + Trim( Str( useMaterial ) )
Print #1, "geometry IndexedFaceSet"
Print #1, Chr(9) + "{"
Print #1, Chr(9) + "coord Coordinate"
Print #1, Chr(9) + Chr(9) + "{"
While InStr(1, dataStr, "]") = 0
If InStr( 1, dataStr, "[") = 0 Then
dataStr = convertUnits(dataStr, conversion)
End If
Print #1, Chr(9) + Chr(9)+ dataStr
readLine
Wend
Print #1, Chr(9) + Chr(9) + dataStr
Print #1, Chr(9) + Chr(9) + "}"
End If
' see if this is the begginning of a coordIndex
If InStr( 1, dataStr, "coordIndex" ) Then
While InStr(1, dataStr, "]") = 0
Print #1, Chr(9) + dataStr
readLine
Wend
Print #1, Chr(9) + dataStr
Print #1, "} }"
Print #1, ""
Print #1, ""
End If
Wend
Close #1
Close #2
MsgBox( "finished converting " + str( J ) + " lines" )
End Sub