+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
  1. #1
    Senior Member
    Using
    Civil 3D 2012
    Join Date
    Apr 2009
    Posts
    116

    Default VB.net: Perpendicular lines to a polyline

    Registered forum members do not see this ad.

    I use vb.net. I work in 2D.
    I have a certain point (Point3D). I have a certain polyline. I need to draw a perpendicular line from the point to the polyline. Any ideas on how to do that??
    Last edited by Jozi68; 2nd Oct 2009 at 04:48 pm. Reason: resolved

  2. #2
    Forum Deity SEANT's Avatar
    Using
    AutoCAD 2014
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    2,169

    Default

    This is typically done with Curve.GetClosestPointTo(). The point returned from that method can be combined with the input point to create a line.

  3. #3
    Senior Member
    Using
    Civil 3D 2012
    Join Date
    Apr 2009
    Posts
    116

    Default

    Thanx Seant,
    Can you please show me how to implement this statement? I have no idea how.

  4. #4
    Forum Deity SEANT's Avatar
    Using
    AutoCAD 2014
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    2,169

    Default

    There may be a detail or two overlooked in this sample, but it shows the general idea.

    Code:
       Public Sub Per2Poly()
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            db.Pdmode = 66 'just to make the points more visible
            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Dim peo As PromptEntityOptions = New PromptEntityOptions(vbCr & "Select a lwpline: ")
            peo.SetRejectMessage(vbCr & "Please select lightweight polyline only! ")
            peo.AddAllowedClass(GetType(Polyline), True)
            Try
                Dim per As PromptEntityResult = ed.GetEntity(peo)
                If per.Status <> PromptStatus.OK Then Exit Sub
                Dim mat As Matrix3d = ed.CurrentUserCoordinateSystem
                Dim pl As Polyline = tr.GetObject(per.ObjectId, OpenMode.ForRead)
    
                Dim ppo As PromptPointOptions = New PromptPointOptions(vbCr & "Select point from which to strike a perpendicular: " & vbLf)
                ppo.AllowNone = True
    
                Dim ppr As PromptPointResult = ed.GetPoint(ppo)
                If ppr.Status <> PromptStatus.OK Then Exit Sub
                Dim basePt As Point3d = ppr.Value.TransformBy(mat)
                Dim pt As Point3d = pl.GetClosestPointTo(basePt, False) 'crux of the process
    
                Dim v3d As Vector3d = basePt.GetVectorTo(pt)       'Test if closest point is indeed perpendicular.
                Dim derV3d As Vector3d = pl.GetFirstDerivative(pt) '
                If v3d.IsPerpendicularTo(derV3d) Then              '
                    Dim ln As Line = New Line(basePt, pt)
                    Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
                    ln.SetDatabaseDefaults()
                    btr.AppendEntity(ln)
                    tr.AddNewlyCreatedDBObject(ln, True)
                    tr.Commit()
                Else
                    ed.WriteMessage(vbCr & "Perpendicular not found!")
                End If
            Catch ex As Exception
                tr.Abort()
                ed.WriteMessage("Error during execution! " & ex.Message)
            Finally
                tr.Dispose()
            End Try
    
        End Sub

  5. #5
    Senior Member
    Using
    Civil 3D 2012
    Join Date
    Apr 2009
    Posts
    116

    Default

    Thanx SEANT,
    I got it to work

  6. #6
    Forum Newbie
    Using
    Civil 3D 2012
    Join Date
    Jun 2013
    Posts
    2

    Default

    Hi Seant,
    I tried to to used part of your code...but i am planning to replace the PPR value input with loop of point3d in the point3dcollection but it gives me an error. I would like to ask how to do it correctly.



    For Each vx As Point3d In VertexPts
    Dim basept As Point3d = vx.TransformBy(mat)
    Dim pt As Point3d = poly2.GetClosestPointTo(basept, False)
    Dim v3d As Vector3d = basept.GetVectorTo(pt)
    Dim derv3d As Vector3d = poly2.GetFirstDerivative(pt)

    If v3d.IsPerpendicularTo(derv3d) Then
    Dim ln As Line = New Line(basept, pt)
    Dim btr As BlockTableRecord = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
    ln.SetDatabaseDefaults()
    btr.AppendEntity(ln)
    trans.AddNewlyCreatedDBObject(ln, True)
    trans.Commit()
    Else
    ed.WriteMessage(vbCr & "Perpendicular not found!")
    End If
    Next

  7. #7
    Forum Deity SEANT's Avatar
    Using
    AutoCAD 2014
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    2,169

    Default


    At which line of code does the error occur?

  8. #8
    Forum Newbie
    Using
    Civil 3D 2012
    Join Date
    Jun 2013
    Posts
    2

    Default

    Quote Originally Posted by SEANT View Post
    At which line of code does the error occur?
    the error message point the error to this line

    Dim btr As BlockTableRecord = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)

    during the program execution it only creates one line to the first point point3d which is perpendicular to the polyline the rest does not...

  9. #9
    Forum Deity SEANT's Avatar
    Using
    AutoCAD 2014
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    2,169

    Default

    Oops! I guess I should have seen that as a problem.

    These line really need to be on the outside of the For Loop.

    Code:
    Dim btr As BlockTableRecord = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
    For Each vx As Point3d In VertexPts
        Dim basept As Point3d = vx.TransformBy(mat)
        Dim pt As Point3d = poly2.GetClosestPointTo(basept, False)
        Dim v3d As Vector3d = basept.GetVectorTo(pt)
        Dim derv3d As Vector3d = poly2.GetFirstDerivative(pt)
        If v3d.IsPerpendicularTo(derv3d) Then
            Dim ln As Line = New Line(basept, pt)
            ln.SetDatabaseDefaults()
            btr.AppendEntity(ln)
            trans.AddNewlyCreatedDBObject(ln, True)
        End If
    Next
    trans.Commit()
    It might also make sense to "Dim" all the variable prior to the for loop - just assign them within the loop.

  10. #10
    Forum Deity BlackBox's Avatar
    Computer Details
    BlackBox's Computer Details
    Operating System:
    Windows 8.1 Professional, Server 2012 R2 Datacenter
    Computer:
    Dell Precision T3600
    CPU:
    Hex-Core Intel Xeon 3.2Ghz
    RAM:
    32GB
    Graphics:
    3GB NVIDIA Quadro K4000
    Primary Storage:
    256GB SSD
    Secondary Storage:
    1TB Western Digital VelociRaptor 10K RPM
    Monitor:
    Dual 24" Dell Ultrasharp Widescreen Monitors
    Using
    Civil 3D 2015
    Join Date
    Nov 2009
    Posts
    4,604

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by SEANT View Post
    It might also make sense to "Dim" all the variable prior to the for loop - just assign them within the loop.
    That's only necessary if the variables are going to be used outside of the FOR loop's scope (before, or after)... i.e., if one needs to further manipulate a variable's Object, or use as Returned value.
    "Potential has a shelf life." - Margaret Atwood

    Autodesk Exchange Apps
    ~ Autoloader ~ AutoCAD Security

Similar Threads

  1. VB.NET: Lines crossing Polyline
    By Jozi68 in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 20th Aug 2009, 08:34 am
  2. Lisp routine for the widths of perpendicular lines
    By bsimpson in forum AutoLISP, Visual LISP & DCL
    Replies: 9
    Last Post: 28th Jan 2009, 02:56 pm
  3. Lisp routine for the perpendicular distance between two lines
    By bsimpson in forum AutoLISP, Visual LISP & DCL
    Replies: 0
    Last Post: 15th Nov 2008, 05:08 pm
  4. Drawing perpendicular lines
    By FCspecial in forum AutoCAD Beginners' Area
    Replies: 6
    Last Post: 20th Jun 2008, 06:12 am
  5. lines perpendicular to spline
    By Dario in forum AutoCAD General
    Replies: 9
    Last Post: 3rd Mar 2006, 09:36 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts