First of all read up on how you post your code or you will get a moderator onto you.
Your logic is a bit off and will not deliver correct answers. You need to consider which quadrant you are in and the special cases when the angle is due E, N , W or S. The resulting code can get a little complicated so I would recommend another approach. You have the starting point and end point coordinates, so use them to draw a temporary line, interrogate the line to get its angle and delete the temporary line.
This code should work for you if you adapt it to your situation:
- Dim basePnt As Variant Dim returnPnt As Variant Dim DistLine As AcadLine Dim Bearing As Double Dim AngleXY As Double Dim PI As Double ' Set the value for PI PI = Math.Atn(1#) * 4 ' select points on screen basePnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Select first point...") returnPnt = ThisDrawing.Utility.GetPoint(basePnt, "Select second point...") ' Create a line from the base point and the last point entered Dim lineObj As AcadLine Set DistLine = ThisDrawing.ModelSpace.AddLine(basePnt, returnPnt) ' Get angle of line (rads) and convert it to degrees AngleXY = DistLine.Angle Bearing = AngleXY * 180# / PI ' Format the result Bearing = Format(Bearing, "##0.00000") ' Display result in a message box MsgBox "Angle from system zero = " & Bearing & "°" _ , vbOKOnly, "3D-Angles" ' Delete temporary line DistLine.Delete
I hope that helps.
@ BlackBox:
Nice neat and concise code. Unfortunately it doesn't work in VBA, as far as I am aware. |