Draw Perpendicular Lines - Code help requested

Discussion in 'AutoCAD' started by Oberer, Jan 5, 2005.

  1. Oberer

    Oberer Guest

    I'd like to draw a line that's perpendicular to a selected line with a constant length.

    The code I have now does all of the above, except for the constant length.

    "A picture is worth a thousand words" certainly applies here. Please look at the attachment to see what I'd like the code to do (in yellow). I'm currently getting the shorter, different length cyan lines.

    i've search thru vba, the NG, and google for ways to modify a line's length, and can't find it. TIA!

    Code:
    Public Sub PerpLine()
    
    Dim line As AcadLine
    Dim pt As Variant
    Dim xline As AcadXline
    Dim oLateralTop As AcadLine
    Dim vPointSelected As Variant
    Dim pt2, ang As Double, pi As Double, vMidPt As Variant
    Dim iOsnapMode As Integer
    Dim oUtil As AcadUtility
    
    pi = Atn(1) * 4
    
    iOsnapMode = ThisDrawing.GetVariable("OSMODE")
    ThisDrawing.SetVariable "OSMODE", 1
    
    Set oUtil = ThisDrawing.Utility
    
    
    oUtilGetEntity line, pt, "Select a lateral: "
    
    ' SELECT ENDPOINT TO ANNOTATE
    vPointSelected = oUtil.GetPoint(, "Select a point: ")
    pt = vPointSelected
    
    '-this is where i'm screwing up. if i don't adjust this point
    'somehow, i end up with a zero length line...
    pt(0) = pt(0) + 3
    pt(1) = pt(1) + 3
    
    
    ang = oUtil.AngleFromXAxis(line.StartPoint, line.EndPoint)
    pt2 = oUtil.PolarPoint(pt, ang - pi / 2, 1)
    Set xline = ThisDrawing.ModelSpace.AddXline(pt, pt2)
    pt2 = xline.IntersectWith(line, acExtendBoth)
    Set oLateralTop = ThisDrawing.ModelSpace.AddLine(pt, pt2)
    xline.Delete
    
    ' get midpoint of newly created line
    vMidPt = MidPoint(oLateralTop.StartPoint, oLateralTop.EndPoint)
    ' move new line from it's midpoint to the selected endpoint
    oLateralTop.Move vMidPt, vPointSelected
    
    ThisDrawing.SetVariable "OSMODE", iOsnapMode
    
    Set oUtil = Nothing
    Set line = Nothing
    Set xline = Nothing
    Set oLateralTop = Nothing
    
    
    End Sub
    
    

    Message was edited by: Oberer
     
    Oberer, Jan 5, 2005
    #1
  2. Oberer

    Jeff Mishler Guest

    You want something like this:
    Sub testme()
    Dim pi As Double
    Dim line As AcadLine
    Dim pt1, pt2, pt3
    Dim oUtil As AcadUtility
    Dim osmode As Integer
    Dim ang As Double

    pi = Atn(1) * 4
    Set oUtil = ThisDrawing.Utility

    oUtil.GetEntity line, pt1, "Select line: "
    osmode = ThisDrawing.GetVariable("osmode")
    ThisDrawing.SetVariable "Osmode", 1
    pt1 = oUtil.GetPoint(, "Select end for lateral: ")
    ThisDrawing.SetVariable "Osmode", osmode
    ang = line.Angle
    pt2 = oUtil.PolarPoint(pt1, ang + (pi / 2), 3)
    pt3 = oUtil.PolarPoint(pt1, ang - (pi / 2), 3)
    ThisDrawing.ModelSpace.AddLine pt2, pt3

    End Sub
     
    Jeff Mishler, Jan 5, 2005
    #2
  3. Oberer

    Oberer Guest

    Jeff,
    Once again, your help is greatly appreciated. This is exactly what I was looking for!
     
    Oberer, Jan 5, 2005
    #3
  4. Oberer

    Jeff Mishler Guest

    Glad I can be of help!
     
    Jeff Mishler, Jan 5, 2005
    #4
  5. Oberer

    Oberer Guest

    If anyone else is interested, I updated the line selection to calculate the endpoint to annotate:

    Thanks again!

    Code:
    ' SELECT LATERAL
    oUtil.GetEntity oLateral, vPoint1, "Select lateral near setback: "
    
    ' calculate which endpoint to use
    If getDistance(vPoint1, oLateral.StartPoint) > getDistance(vPoint1, oLateral.EndPoint) Then
    vPoint1 = oLateral.EndPoint
    Else
    vPoint1 = oLateral.StartPoint
    End If
    
     
    Oberer, Jan 5, 2005
    #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.