×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Nearest point in AutoCAD VBA

Nearest point in AutoCAD VBA

Nearest point in AutoCAD VBA

(OP)
Hi people...

Does anyone knows how could I get the nearest point of an entity of the point I inform using VBA?

Let's say I have a spline and in a top view I know one point (X and Y) which is really near it, and I need to know the Z coordinate of the spline there. I have a large experience in VBA with SolidWorks, Excel, etc... But completely new to AutoCAD VBA, I tried to found any method to simulate the Nearest Osnap, but could find. Any idea?

Best Regards,
Rodrigo Basniak

RE: Nearest point in AutoCAD VBA

Do you want point to point 3D closest distance? This is a geometry question BTW.

"Everybody is ignorant, only on different subjects." — Will Rogers

RE: Nearest point in AutoCAD VBA

(OP)
Hi,

No... the closest distance is zero, but for some reasons autocad is not recognizing this as a intersection. So I'm trying to find out the Z coordinate of a point that lies on a 3d spline given the X and Y coordinates.

Thanks,
Rodrigo Basniak

RE: Nearest point in AutoCAD VBA

Have you looked at the IntersectWith method yet?

"Everybody is ignorant, only on different subjects." — Will Rogers

RE: Nearest point in AutoCAD VBA

(OP)
Yep... This was my first idea, but I don't know why some cases aren't considered as intersection. I used 8 decimal places and took the distance from the spline to the xline which should intersect the spline and the result was 0.00000000 although AutoCAD says they don't intersect :( That's when I come to the nearest point lying in the spline that I mentioned before.

RE: Nearest point in AutoCAD VBA

Splines can be tricky since they are defined by control points and not vertices per se. Is converting the spline to a polyline an option?

"Everybody is ignorant, only on different subjects." — Will Rogers

RE: Nearest point in AutoCAD VBA

(OP)
Nope... because what I need is exactly the interpoled curves of the spline, which I'll loose if I convert to pline. I did what I want, but not in the best way. I use the SendCommand method to get the points with the NEAREST osnap, but since my application have around 10000-20000 iterations it's really annoying to do this way and it tooks alot more time than needed since this way the commands are send to the command line and it has to bw updated at every command :(

RE: Nearest point in AutoCAD VBA

In "visual lisp active x" the function 'vlax-curve-getClosestPointTo' may do what you need.
In manual AutoCAD, trimming the spline, then id-ing the endpoint works, FWIW.

RE: Nearest point in AutoCAD VBA

(OP)
Migrate to Visual Lisp would be complicated. The whole application is already written in VBA, and I never programmed in lisp, so converting the code wouldn't be that easy since my application also use some API calls to interface with other software. I guess I'll have to live with the command line problem... But thanks anyway :)

RE: Nearest point in AutoCAD VBA

I will throw this out there. Would drawing a line from a point to a perpendicular point on the spline work and then read the endpoint of the line (and then delete it) work?

"Everybody is ignorant, only on different subjects." — Will Rogers

RE: Nearest point in AutoCAD VBA

(OP)
Hello again... I tried manually the point thing you suggest but AutoCAD doesn't recognize any perpendicular on the spline...

RE: Nearest point in AutoCAD VBA

This is a kludge of some ACAD samples, it may give some ideas? ....

Sub Example_IntersectWith()
       
    Dim splineObj As AcadSpline
    Dim startTan(0 To 2) As Double
    Dim endTan(0 To 2) As Double
    Dim fitPoints(0 To 8) As Double
    
    startTan(0) = 0.5: startTan(1) = 0.5: startTan(2) = 0
    endTan(0) = 0.5: endTan(1) = 0.5: endTan(2) = 0
    fitPoints(0) = 1: fitPoints(1) = 1: fitPoints(2) = 0
    fitPoints(3) = 5: fitPoints(4) = 5: fitPoints(5) = 0
    fitPoints(6) = 10: fitPoints(7) = 0: fitPoints(8) = 0
    Set splineObj = ThisDrawing.ModelSpace.AddSpline(fitPoints, startTan, endTan)

    
    Dim lineObj As AcadLine
    Dim startPt(0 To 2) As Double
    Dim endPt(0 To 2) As Double
    startPt(0) = 1: startPt(1) = 1: startPt(2) = 0
    endPt(0) = 5: endPt(1) = 5: endPt(2) = 0
    Set lineObj = ThisDrawing.ModelSpace.AddLine(startPt, endPt)

    ZoomAll
      
    ' Find the intersection points between the line and the circle
    Dim intPoints As Variant
    intPoints = lineObj.IntersectWith(splineObj, acExtendThisEntity)
    
    ' Print all the intersection points
    Dim I As Integer, j As Integer, k As Integer
    Dim str As String
    If VarType(intPoints) <> vbEmpty Then
        For I = LBound(intPoints) To UBound(intPoints)
            str = "Intersection Point[" & k & "] is: " & intPoints(j) & "," & intPoints(j + 1) & "," & intPoints(j + 2)
            MsgBox str, , "IntersectWith Example"
            str = ""
            I = I + 2
            j = j + 3
            k = k + 1
        Next
    End If
End Sub

"Everybody is ignorant, only on different subjects." — Will Rogers

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources