Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Error Handling in VB Data Trouble 1

Status
Not open for further replies.

Twullf

Mechanical
Jan 24, 2012
196
0
0
US
I have an interesting conundrum. I am parsing some data reading it as strings and turning it to integers and using it in the program. My data looked good, it was all numbers but it kept creating an error saying the numbers were in the wrong format. When I formatted the data in excel to number format with enough decimal places it works fine.

I want to create a handle, preferably to fix the data and use it anyway, but I'm not entirely sure what the problem or why it is a problem. So I'm not sure how to fix it. Here is the sub as currently written.

Code:
   Public Sub CreateBotSurfaceSpline( byRef strDir as String, transform As Transform, byRef lineChord as Line, _
      byRef lineTE as Line, byRef counter as Integer ) 

      '------------------------------------------------------------------------------------------ 
      '   Import spline2 using points from file
      '------------------------------------------------------------------------------------------

      Dim strTFileName        As String = "S" & counter & "BOT.dat"
      Dim fileExist           As Boolean         
      Dim splinePoints( -1 )  As Point
      Dim botSpline           As Spline
      Dim readLine            As String
      Dim count               As Integer
      Dim strings             As String ()
      fileExist = true
         
      Try
         sr = File.OpenText( strDir & "\" & strTFileName )            
      Catch    'E As Exception
         'MessageBox.Show(E.Message)
         fileExist = false
      End Try

      '------------------------------------------------------------------------------------------
      '   If then statement, if spline file exists continue, if it does not, exit SubRoutine
      '------------------------------------------------------------------------------------------

      If fileExist = True

         '------------------------------------------------------------------------------------------
         '   Read Line, parse line, translate points, and place points in an array
         '------------------------------------------------------------------------------------------

         Try
            readLine = sr.ReadLine()
            count = 0
            While Not readLine is Nothing
               Dim pt1 As Point3d
               Dim pt2 as Point3d

               Try
                  strings = readLine.Split( vbTab )
                  pt1.x = Double.Parse(strings(0))
                  pt1.y = Double.Parse(strings(1))
                  pt1.z = Double.Parse(strings(2))

               Catch
                  MessageBox.Show( "Spline data file " & strTFileName & " must be tab deliminted. and located in " _
                     & strDir, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error )
                  Exit Sub
               End Try



               Try           
                  pt2 = Transform.Apply( New Point3d( pt1.x, pt1.y, pt1.z ))
               Catch
                  MessageBox.Show( "File data needs to be in full numerical format, no special characters.", _
                     "Error", MessageBoxButtons.OK, MessageBoxIcon.Error )
                  Exit Sub
               End Try

               '----------------------------------------------------------------------------------------
               '   Create an array of points, each array value containing a single point
               '----------------------------------------------------------------------------------------

               ReDim Preserve SplinePoints(count)
               SplinePoints(count) = workPart.Points.createPoint(pt2)

               '----------------------------------------------------------------------------------------
               '   Increase count value by 1, Read the next line of the file and continue While statement
               '----------------------------------------------------------------------------------------

               count = count + 1
                             
               readLine = sr.ReadLine()
            End While
         Finally
            sr.Close()
         End Try

         '------------------------------------------------------------------------------------------
         '   Place spline
         '------------------------------------------------------------------------------------------

         CreateSketchSpline( splinePoints, botSpline )

         '------------------------------------------------------------------------------------------
         '   Constrain botSpline to Uniform Scale
         '------------------------------------------------------------------------------------------

         Dim conGeom1 As Sketch.ConstraintGeometry

         conGeom1.Geometry = botSpline
         conGeom1.PointType = Sketch.ConstraintPointType.None
         conGeom1.SplineDefiningPointIndex = 0
         Dim sketchGeometricConstraint1 As SketchGeometricConstraint
         sketchGeometricConstraint1 = theSession.ActiveSketch.CreateUniformScaledConstraint(conGeom1)

         '------------------------------------------------------------------------------------------
         '   Constrain Botspline startpoint to be coincident with lineChord startpoint
         '------------------------------------------------------------------------------------------

         Dim geom1_1 As Sketch.ConstraintGeometry

         geom1_1.Geometry = BotSpline
         geom1_1.PointType = Sketch.ConstraintPointType.EndVertex
         geom1_1.SplineDefiningPointIndex = 0

         Dim geom2_1 As Sketch.ConstraintGeometry

         geom2_1.Geometry = lineChord
         geom2_1.PointType = Sketch.ConstraintPointType.StartVertex
         geom2_1.SplineDefiningPointIndex = 0
         Dim sketchGeometricConstraint2 As SketchGeometricConstraint
         sketchGeometricConstraint2 = theSession.ActiveSketch.CreateCoincidentConstraint(geom1_1, geom2_1)

         '------------------------------------------------------------------------------------------
         '   Constrain botSpline endpoint to be coincident with lineTE StartPoing
         '------------------------------------------------------------------------------------------
            
         Dim geom1_2 As Sketch.ConstraintGeometry

         geom1_2.Geometry = botSpline
         geom1_2.PointType = Sketch.ConstraintPointType.StartVertex
         geom1_2.SplineDefiningPointIndex = 0
         Dim geom2_2 As Sketch.ConstraintGeometry

         geom2_2.Geometry = lineTE
         geom2_2.PointType = Sketch.ConstraintPointType.StartVertex
         geom2_2.SplineDefiningPointIndex = 0
         Dim sketchGeometricConstraint3 As SketchGeometricConstraint
         sketchGeometricConstraint3 = theSession.ActiveSketch.CreateCoincidentConstraint(geom1_2, geom2_2)

      End If
   End Sub

Thanks for the help.
 
Replies continue below

Recommended for you

What error message does your code return?

I notice you have commented out the messages returned by the exception objects, try turning them back on (or adding them as necessary); the information they return will help track down the problem(s).

 
The problem was occurring at the Parse functions. It was saying the data was in the wrong format. But the files looked fine, in notepad they were in numerical format.

When I manually told UG to put the splines in no problem, they worked, when I used excel to go into the files and make the column format numerical, no problem. I figure there has to be something more that is being done to the data to format the data, that I'm not calling in my code.

I know it is the Parse functions cause I pulled them out of the Try Catch block and got the error about wrong formatting.
 
The error message states:
System.FormatException:Input string was not in a correct format
at system.Number.StringtoNumber(String str, NumberStyles options, NumberBuffer_number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.Parse Double(string value, NumberStyles options, NumberFormatInfo numfmt)
at System.DoubleParse(String s, NumberStyles style, NumberFormatInfo info)
at NXJournal.CreateTopSurfaceSpline(String strDir, Transform transform, LIne lineChord, Line lineTE, Int32_counter) in [dir location]
 
 http://files.engineering.com/getfile.aspx?folder=5ad6c7a5-02a7-4f6d-ad42-05723f189b75&file=S10BOT.dat
You get an error because there is a blank line at the end of the file. Here is a small code snippet I used to loop through your file and check for blank lines:
Code:
Dim readLine            As String
Dim strings             As String ()
Dim pt1 As Point3d

Dim sr As IO.StreamReader = IO.File.OpenText("C:\Temp\S10BOT.dat")
Try
	Do While sr.Peek >= 0
	readLine = sr.ReadLine
		[highlight]if Len(Trim(readLine)) > 0 then[/highlight]
			strings = readLine.Split( vbTab )
			msgbox(strings(0) & ", " & strings(1) & ", " & strings(2))
			pt1.x = Double.Parse(strings(0))
			pt1.y = Double.Parse(strings(1))
			pt1.z = Double.Parse(strings(2))
		end if
	Loop
Catch ex As Exception
	MsgBox("Wrong data format")
End Try
Blank lines will be skipped, and you'll get the "Wrong data format" message if the line cannot be parsed into doubles.

 
Status
Not open for further replies.
Back
Top