Jump to content

VBA - Getting angle from 2 points


Recommended Posts

Posted

Hi all. (aCad2007)

 

I am grabbing 2 points in VBA using this code.

 

vPoint1 = ThisDrawing.Utility.GetPoint(, vbCr & "Select Point 1: ")
vPoint2 = ThisDrawing.Utility.GetPoint(, vbCr & "Select Point 2: ")

How do I get the angle between them? as in 90 degrees or 45

 

Thanks!

Posted

Also tried...

 

Dim d As Double
d = ThisDrawing.Utility.AngleFromXAxis(vPoint1,vPoint2)
Debug.Print ThisDrawing.Utility.AngleToString(d, acDegrees, 2)

But this just returns 0.00 no matter what it is vPoint1 and 2.

Posted

When you say get the angle between two points are you implying there is a common vertex? The World Origin, perhaps?

 

The way the AngleFromXAxis works is by using the two supplied points to create a line and returning the angle it makes with the World X axis.

 

If there is indeed common vertexes then you should pair it with each of the other two points, use AngleFromXAxis twice, then subtract one from the other.

Posted
When you say get the angle between two points are you implying there is a common vertex? The World Origin, perhaps?

 

To my dismay, I get all of about 3 weeks a year to play Autocad master and as you can imagine, I suck at it. (now if you have some SQL or .NET then I am OK) So when you say 'common vertex' or 'World Orgin' I have no idea what that is. (I inherited this project, what your a programmer? here fix this)

 

But basically I get 2 points with GetPoint and insert a existing drawing evenly spaced out across this line. What they asked for was was to rotate the just to be inserted drawing on what ever angle the to points are. So if the 2 points are 180 degrees (up and down) then rotate the drawing 180. If the 2 points are left to right then 0 degrees. So all I have is the 2 arrays from the GetPoint function.

 

Thanks again

Posted

Easy old fashioned sin cos

 

when you getpoint it is made up of 3 parts x,y,z you just need to apply a simple sine rule sin(ang)=o/h cos=a/h tan=o/a (others have a habit of answering ohahoa)

 

ie pt1(0) = x1 pt1(1) =y1 pt1(2) =z

pt2(0) = x2 pt2(1) =y2 pt1(2) =z

 

a=pt2(0)-pt1(0)

o=pt2(1)-pt1(1)

h=sqrt(a^2+o^2)

 

trying to remember the correct dim for pt1 &pt2

Dim dblPOINT(0 To 2) As Double

 

I would write two functions distance and angle so you could use them any time in other programs. It may be a standard vba function in lisp (distance pt1 pt2) (angle pt1 pt2) ?

Posted

The standard VBA function BIGAL alluded to is indeed ThisDrawing.Utility.AngleFromXAxis (and ThisDrawing.Utility.GetDistance for distances). No doubt AngleFromXAxis uses the same math BIGAL illustrated.

 

The one caveat, however, would be when using those two functions in UCSs other than the World Coordinate System. If an alternate UCS is a possibility then a coordinate translation is required. See sample:

 

 

Sub AngleSample()
Dim vPoint1 As Variant
Dim vPoint2 As Variant
Dim vTrans1 As Variant
Dim vTrans2 As Variant
Dim d As Double
Dim strMsg As String
  With ThisDrawing.Utility
     On Error Resume Next
     vPoint1 = .GetPoint(, "Select Point 1: " & vbCr)
     If Err <> 0 Then Exit Sub
     vTrans1 = .TranslateCoordinates(vPoint1, acWorld, acUCS, 0)
     vPoint2 = .GetPoint(vTrans1, "Select Point 2: ")
     If Err <> 0 Then Exit Sub
     On Error GoTo 0
     vTrans2 = .TranslateCoordinates(vPoint2, acWorld, acUCS, 0)
     d = .AngleFromXAxis(vTrans1, vTrans2)
     strMsg = .AngleToString(d, acDegrees, 2)
     .Prompt "Angle in Degrees is " & strMsg & vbLf
  End With
End Sub

Posted

Seant, Bigall, thanks

 

Maybe I am looking at this the wrong way... If you look at the picture I have uploaded. You can see that my inserted drawings (they are called chairs) are inserted across the line that was selected by using GetPoint. So if the line is from 12 o-clock to 6 o-clock then the chairs are inserted 90 degrees evenly spaced out. If the line was left to right then the chairs are inserted 0 degrees.

 

At least that's how I understood it until this week. Now they want it to follow the figure on the far right. That's why I was trying to figure out the degrees.

 

I think I am looking at this the wrong way. Any comments would be very helpful.

 

Thanks

degrees.jpg

Posted
Seant, Bigall, thanks

 

Maybe I am looking at this the wrong way... If you look at the picture I have uploaded. You can see that my inserted drawings (they are called chairs) are inserted across the line that was selected by using GetPoint. So if the line is from 12 o-clock to 6 o-clock then the chairs are inserted 90 degrees evenly spaced out. If the line was left to right then the chairs are inserted 0 degrees.

 

At least that's how I understood it until this week. Now they want it to follow the figure on the far right. That's why I was trying to figure out the degrees.

 

I think I am looking at this the wrong way. Any comments would be very helpful.

 

Thanks

 

you can use ROTATE..type Ro then select the object...enter..select reference which is new angle from the object..then..enter the spicified angle want to rotate..

 

sorry for my bad english..:(

 

oliver

Posted

I think what your doing wrong is calculating the angle sort of backwards Autocad measures angles counter clockwise in Radians and takes no notice of what your units settings are when you write code so sometimes you even need to know which quadrant your in to either add 90deg or subtract so the block does not appear upside down. Also make sure your in world UCS.

 

For your Block you would add 1.5714 to the angle of the line should work then adjust upside down.

Posted

here is a movie...

 

http://www.mediafire.com/download.php?qjwva4mw1yj

 

:)

Posted
here is a movie...

 

http://www.mediafire.com/download.php?qjwva4mw1yj

:)

 

Nice video Oliver

That's basically what I want to do but I need to do it in VBA code using the two points selected with getpoint.

 

 

BigAl

yep, your right, I need to flip 90

Now time to read about quadrants!!!

Posted

1.5714 is the magic number!!!! thanks to all.

 

Recap...

 

Used the AngleFromXAxis function

and then added 1.5714

and converted my Generic InsertBlock function to use rads

 

 

Again, thanks to all!!!!!!!!!!!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...