Imperial Text input to REAL?

Discussion in 'AutoCAD' started by David Kurtz, Aug 24, 2004.

  1. David Kurtz

    David Kurtz Guest

    Hello,
    I've searched the Webgroups of this forum, but I've not seen anything
    that addresses this. I've obviously overlooked it or used the wrong
    search terms. But:

    I'm trying to find the proper way in acad VBA to convert string values
    taken from textboxes (e.g. 2'-4") into their respective REAL values (28
    units).
    Are there built in functions for this that I'm too blind to find or
    should I build one with vbaRegex's? :)

    Thanks
     
    David Kurtz, Aug 24, 2004
    #1
  2. Sub test()
    Dim sValue As String
    Dim sTmp() As String
    Dim dReturn As Double

    sValue = "2'-4" & Chr(34)

    sTmp = Split(sValue, "'")
    dReturn = CDbl(sTmp(0)) * 12
    sTmp = Split(sValue, "-")
    sTmp = Split(sTmp(1), Chr(34))
    dReturn = dReturn + CDbl(sTmp(0))

    MsgBox dReturn

    End Sub

    Unless there is a better answer, this could be improved on but it fits the
    bill. You'd just need to factor in fractions if they use "/" instead of
    decimal equivalents.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Aug 24, 2004
    #2
  3. David Kurtz

    Norman Yuan Guest

    AutoCAD VBA has built-in object just doing that:
    AcadUtility.DistanceToReal().

    On Error Resume Next

    Dim dblNum as Double
    dblNum=ThisDrawing.Utility.DistanceToReal(TextBox1.Text,acArchitectural)

    If Err.Number<>0 Then
    MsgBox "Invalid Value was entered!"
    End If

    Doing your own conversion like this looks simple, but very easily get you
    into trouble, if you try do deal all possible user inputs.
     
    Norman Yuan, Aug 24, 2004
    #3
  4. David Kurtz

    David Kurtz Guest

    Much thanks for both of your replys guys.


    hoHO! I looked at the "Utility" object as I expected it to live in
    there with the other conversion utils; but I skimmed right over this
    one. Thanks for the slap on the forehead. *:)
    This is exactly what I was thinking, and have learned the hard way in
    the past with other languages; so I though I should post here...

    Thanks again for the response Norman.
     
    David Kurtz, Aug 25, 2004
    #4
  5. Ah, thanks for picking me up Norman =) One of the downsides to not doing
    much vba - I overlook some of the pieces.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Aug 25, 2004
    #5
  6. David Kurtz

    David Kurtz Guest

    Actually, due to your post, I am going to employ both methods (sort of)
    as I need to strip out "bad" characters (i.e. anything not "numeric" in
    all of acads myriad of unit input formats.)
    But for now the utility function will suffice to check it and substitute
    a default value if it can't convert.

    Thanks again
     
    David Kurtz, Aug 26, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.