vba problem for newbie

Discussion in 'AutoCAD' started by johnbortoli, Jul 30, 2003.

  1. johnbortoli

    johnbortoli Guest

    want to get a length from two points selected on screen and save the result to avalue in an attribute i have the code but am not sure how to pass the length value to the attribute . can anyone help

    Sub GetLength()
        Dim tenstart As Variant
        Dim tenend As Variant

    ' Get the points from the user
        tenstart = ThisDrawing.Utility.GetPoint _
                    (, vbCrLf & "select start of tendon: ")
        tenend = ThisDrawing.Utility.GetPoint _
                    (tenstart, vbCrLf & "select end of tendon: ")

    ' Calculate the distance between point1 and point2
        Dim x As Double, y As Double, z As Double
        Dim length As Double
        x = tenstart(0) - tenend(0)
        y = tenstart(1) - tenend(1)
        z = tenstart(2) - tenend(2)
        length = Sqr((Sqr((x ^ 2) + (y ^ 2)) ^ 2) + (z ^ 2))

    End Sub
       Dim attributeObj As AcadAttribute
        Dim height As Double
        Dim mode As Long
        Dim prompt As String
        Dim insertionPoint(0 To 2) As Double
        Dim tag As String
        Dim value As String

    height = 250
        mode = acAttributeModeInvisible
        prompt = "tendon Length"
        insertionPoint(0) = 5
        insertionPoint(1) = 5
        insertionPoint(2) = 0
        tag = "length"
        value = "length" ' i want to place length here
        Set attributeObj = blockObj.AddAttribute(height, mode, _
                              prompt, insertionPoint, tag, value)
     
    johnbortoli, Jul 30, 2003
    #1
  2. johnbortoli

    wivory Guest

    Change your GetLength procedure from a Sub to a Function. This will let you return the length value to the calling routine.
      
    Regards
      
    Wayne Ivory
    IT Analyst Programmer
    Wespine Industries Pty Ltd
     
    wivory, Jul 30, 2003
    #2
  3. johnbortoli

    joesu Guest

    John,

    Here is a much clearer way to calculate the distance between two points.

    Joe
    --

    Dim StartPoint As Variant Dim Distance As Double

    With ThisDrawing.Utility     StartPoint = .GetPoint(, "Select start point ")     Distance = .GetDistance(StartPoint, "Select end point ")     MsgBox "Distance is " & Distance   End With
     
    joesu, Jul 30, 2003
    #3
  4. johnbortoli

    johnbortoli Guest

    thanks for that but as i am very new to vba how should my code to pass the length to the calling return look. please help.
     
    johnbortoli, Aug 5, 2003
    #4
  5. johnbortoli

    wivory Guest

    Add the line


        GetLength = length

    to the end of your GetLength routine. Alternatively you can just replace length with GetLength in your assignment statement on the last line.
      
    Regards
      
    Wayne
     
    wivory, Aug 6, 2003
    #5
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.