AngleFromXAxis

AngleFromXAxis

ksd
Participant Participant
1,950 Views
12 Replies
Message 1 of 13

AngleFromXAxis

ksd
Participant
Participant

Can someone please explain to me why this works:

Sub Example_AngleFromXAxis()
    ' This example finds the angle, in radians, between the X axis
    ' and a line defined by two points.
    
    Dim pt1(0 To 2) As Double
    Dim pt2(0 To 2) As Double
    Dim retAngle As Double
    
    pt1(0) = 256833.405419725: pt1(1) = 5928142.14654379: pt1(2) = 0
    pt2(0) = 256890.630751811: pt2(1) = 5928014.31962269: pt2(2) = 0
    
    ' Return the angle
    retAngle = ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)
    
    ' Create the line for a visual reference
    Dim lineObj As AcadLine
    Set lineObj = ThisDrawing.ModelSpace.AddLine(pt1, pt2)
    ZoomAll
    
    ' Display the angle found
    MsgBox "The angle in radians between the X axis and the line is " & retAngle, , "AngleFromXAxis Example"
    
End Sub

But, when those same coordinates are passed from another array is doesn't work??

 

..........
pt1(0) = CDbl(blockLocation(0)): pt1(1) = CDbl(blockLocation(1)): pt1(2) = 0
pt2(0) = CDbl(blockLocation(2)): pt2(1) = CDbl(blockLocation(3)): pt2(2) = 0
retAngle = acadDoc.Utility.AngleFromXAxis(pt1, pt2)
..........

??

0 Likes
Accepted solutions (2)
1,951 Views
12 Replies
Replies (12)
Message 2 of 13

Ed__Jobe
Mentor
Mentor

As I explained at augi, you need to show how blockLocation is getting assigned. Its possible that all its values are empty if its not getting set properly.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 13

ksd
Participant
Participant

Hi thanks for the replies,

 

I'm trying to avoid posting the whole code (for IP and glut reasons) but basically there is code that looks for a text in a block, when it find the right text it loads the coordinates into the blockLocation array

 

 coords = ss.InsertionPoint
 blockLocation(0) = coords(0)
 blockLocation(1) = coords(1)

It then looks for a second set of text and loads the coordinates of that block the same way:

 

 coords = ss.InsertionPoint
 blockLocation(2) = coords(0)
 blockLocation(3) = coords(1)

I have got the code to show me (in a msgbox) what the blockLocation() array has in it right before it throws it at the AngleFromXAxis, and the coordinates look fine. I used the coordinates from the message box to manually set the pt1() and pt2() in the working example, but they will not work when they're passed from the blockLocation array...

 

It's no doubt something really simple I'm missing, but I just cant see it.

 

 

0 Likes
Message 4 of 13

Ed__Jobe
Mentor
Mentor

That's still not enough info to try and reproduce  your situation with. Can you post a dvb and sample dwg?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 13

ambrosl
Autodesk
Autodesk

What data type is assigned to the blockLocation variable?  If assigned as a Double, there is no need to use CDbl which converts the value to a double.  Logically that alone shouldn't be a problem though.

 

Can you use the Debug.Print statement in your code to output the current values of the blockLocation variable?

 

  Debug.Print blockLocation(0)
  Debug.Print blockLocation(1)
  Debug.Print blockLocation(2)
  Debug.Print blockLocation(3)

 

The output will go to the Immediate window in the VBA IDE upon execution during your program.

 

Are you using a custom UCS by chance as well?



Lee Ambrosius
Senior Principal Content Experience Designer
For additional help, check out the AutoCAD Developer Documentation
0 Likes
Message 6 of 13

ksd
Participant
Participant

Yeah, the CDbl was an attempt to fix the problem.

 

I put the debug code in:

Debug.Print "blockLocation(0 to 3)"
Debug.Print blockLocation(0)
Debug.Print blockLocation(1)
Debug.Print blockLocation(2)
Debug.Print blockLocation(3)
Debug.Print "pt1(0 to 2)"
Debug.Print pt1(0)
Debug.Print pt1(1)
Debug.Print pt1(2)
Debug.Print "pt2(0 to 2)"
Debug.Print pt2(0)
Debug.Print pt2(1)
Debug.Print pt2(2)

 

Results:

blockLocation(0 to 3)
256833.405419725
5928142.14654379
256860.94223028
5928154.50562402
pt1(0 to 2)
256833.405419725
5928142.14654379
0
pt2(0 to 2)
256860.94223028
5928154.50562402
0

0 Likes
Message 7 of 13

ambrosl
Autodesk
Autodesk

Based on those numbers... I am getting an angle in Radians of 0.42187226 which when converted is 24.1715 Degrees.  What value are you getting and expecting?



Lee Ambrosius
Senior Principal Content Experience Designer
For additional help, check out the AutoCAD Developer Documentation
0 Likes
Message 8 of 13

ksd
Participant
Participant

Hi Lee,

 

I'm getting an error:

2017-06-27 11_59_46-Microsoft Visual Basic.jpg

 

But if I take the same coordinates (from the debug output) and manually set them in pt1() & pt2(), it works fine..

 

 

 

0 Likes
Message 9 of 13

ksd
Participant
Participant
Accepted solution

Woooo! Figured it out...

 

It was to do with the way I was declaring pt1 and pt2.

 

If I declare it like this:

Dim pt1(0 To 2), pt2(0 To 2) As Double

It fails..

 

But like this:

Dim pt1(0 To 2) As Double
Dim pt2(0 To 2) As Double

It works fine.

 

No idea why...

0 Likes
Message 10 of 13

ambrosl
Autodesk
Autodesk
Accepted solution

Glad to hear you got it figured out.

 

Dim pt1(0 To 2), pt2(0 To 2) As Double defines the first array as a Variant array and the second as a Double array.

 

The AngleFromAxis function as you have seen from the change made and the error message that you encountered expects a Double array.

 

The variable types of the arrays can be tested with the following statements:

 

Dim pt1(0 To 2), pt2(0 To 2) As Double

 

Debug.Print VarType(pt1)
Debug.Print VarType(pt2)

 

8204
8197

 

8204 represents an Array (8192) of the Variant data type (12)

8197 represents an Array (8192) of the Double data type (5)

 



Lee Ambrosius
Senior Principal Content Experience Designer
For additional help, check out the AutoCAD Developer Documentation
0 Likes
Message 11 of 13

Ed__Jobe
Mentor
Mentor

Glad you figured it out.

 

What @ambrosl said, plus we may have pointed it out sooner if you posted your code.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 12 of 13

ksd
Participant
Participant

Ed, that may very well be the case, but as advised earlier, I am subject to an NDA for this work, so was not comfortable posting the full code.

0 Likes
Message 13 of 13

Ed__Jobe
Mentor
Mentor

@Anonymous wrote:

Ed, that may very well be the case, but as advised earlier, I am subject to an NDA for this work, so was not comfortable posting the full code.


I don't see where  you mentioned that part, but OK.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes