在明经通道函数下载栏中,发现了一个求直线上离已知点距离最近的点的函数,感到特别复杂,特用以下方法实现,请朋友们提意见
Public Function nearestPoint2Line(point As Variant, lineobj As AcadLine) As Variant
Dim pt1 As Variant, pt2 As Variant, pt3 As Variant, intpt As Variant Dim pointobj1 As AcadPoint, pointobj2 As AcadPoint Dim lineobjtemp As AcadLine Dim mosapce As AcadModelSpace
Set mospace = ThisDrawing.ModelSpace
pt1 = lineobj.StartPoint: pt2 = lineobj.EndPoint Set pointobj1 = mospace.AddPoint(point) Set pointobj2 = pointobj1.Mirror(pt1, pt2) Set lineobjtemp = mospace.AddLine(pointobj1.Coordinates, pointobj2.Coordinates) intpt = lineobjtemp.IntersectWith(lineobj, acextendno)
lineobjtemp.Delete pointobj1.Delete pointobj2.Delete
nearestPoint2Line = intpt
End Function |