Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nearest point in AutoCAD VBA

Status
Not open for further replies.

RBasniak

Mechanical
Sep 19, 2001
59
0
0
BR
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
 
Replies continue below

Recommended for you

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

"Everybody is ignorant, only on different subjects." — Will Rogers
 
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
 
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.
 
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
 
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 :(
 
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.
 
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 :)
 
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
 
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
 
Status
Not open for further replies.
Back
Top