Author Topic: get correct angle  (Read 4068 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
get correct angle
« on: July 20, 2020, 06:30:26 AM »
Hello guys,

I am trying to locate one similar text ( 125 ) on line objects on the top side regardless of the ( start, end, and angle ) of lines.
I tried to solve this by myself but failed and the result is not the same all the time.

Hope you can solve this problem for me.

Thanks in advance.

badjo_5

  • Mosquito
  • Posts: 18
Re: get correct angle
« Reply #1 on: July 20, 2020, 09:13:44 AM »
I have something simmilar, this is in C#, raw version.
PI is const.

 
Code: [Select]
         public static double checkAngle(double angle)
        {
              if (angle >= 0 && angle < PI / 2)
              {
                  //angle = angle;
              }
              else if (angle >= PI / 2 && angle < PI)
              {
                  angle = angle - PI;
              }
              else if (angle >= PI && angle < PI + PI / 2)
              {
                  angle = angle - PI;
              }
              else if (angle >= PI + PI / 2 && angle < PI + PI / 2)
              {
                  angle = angle - 2 * PI;
              }
             
              return angle;
        }

This gives slightly different result from yours (lower left corner must like upper right corner)..
« Last Edit: July 20, 2020, 09:22:59 AM by badjo_5 »

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: get correct angle
« Reply #2 on: July 20, 2020, 09:20:03 AM »
Code - Auto/Visual Lisp: [Select]
  1. (setq a (angle StartPoint EndPoint))
  2.  
  3. (setq a (atan (/ (sin a) (cos a))))

Don't worry about cos, it is never zero.
Code - Auto/Visual Lisp: [Select]
  1. (cos (/ pi 2.0)) -> 6.12323e-17

Coder

  • Swamp Rat
  • Posts: 827
Re: get correct angle
« Reply #3 on: July 20, 2020, 09:44:58 AM »
Thanks badjo, that's what I was trying to write but I not sure if it will work correctly all the time .

Thanks Stefan,

Can you please explain that function and the way it works ? I can't get it to work but when I added (/ pi 2.0) worked but not as expected and it fails in most cases to give the right rotation as explained in photo above.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: get correct angle
« Reply #4 on: July 20, 2020, 10:32:33 AM »
Thanks badjo, that's what I was trying to write but I not sure if it will work correctly all the time .

Thanks Stefan,

Can you please explain that function and the way it works ? I can't get it to work but when I added (/ pi 2.0) worked but not as expected and it fails in most cases to give the right rotation as explained in photo above.

atan can take 1 or 2 arguments. For 2 arguments, returns an angle between 0 and 2pi, depending on the sign of the 2 arg. For 1 argument, returns an angle between -pi/2 and pi/2, which is exactly the range of the readable angles.

Use WCS points and entmakex and it should work.

If you use it in a 2D rotated UCS, you need to add UCSXDIR angle. For 3D rotated UCS, you need some more transformation.
« Last Edit: July 20, 2020, 10:42:04 AM by Stefan »

Coder

  • Swamp Rat
  • Posts: 827
Re: get correct angle
« Reply #5 on: July 20, 2020, 11:23:12 AM »
Sorry Stefan, but that was not the same as my example.

Number 16 located above the line but it has to be on the opposite side.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: get correct angle
« Reply #6 on: July 20, 2020, 11:34:04 AM »
Sorry Stefan, but that was not the same as my example.

Number 16 located above the line but it has to be on the opposite side.
If it's on the top side already but you want it on the opposite side wouldn't that be the bottom ?  :?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: get correct angle
« Reply #7 on: July 20, 2020, 11:45:07 AM »
Sorry Stefan, but that was not the same as my example.

Number 16 located above the line but it has to be on the opposite side.

I'm really sorry, I didn't read carefully your request, I thought it's all about angles.

I am trying to locate one similar text ( 125 ) on line objects on the top...
Now is more confusing... Do you want some text above and some below? They are all "125", which one are you trying to fix?

« Last Edit: July 20, 2020, 11:52:38 AM by Stefan »

Coder

  • Swamp Rat
  • Posts: 827
Re: get correct angle
« Reply #8 on: July 20, 2020, 12:01:17 PM »
Sorry Stefan, but that was not the same as my example.

Number 16 located above the line but it has to be on the opposite side.
If it's on the top side already but you want it on the opposite side wouldn't that be the bottom ?  :?

The side which is anticlockwise is the top side.  :wink:

Sorry Stefan, but that was not the same as my example.

Number 16 located above the line but it has to be on the opposite side.

I'm really sorry, I didn't read carefully your request, I thought it's all about angles.

I am trying to locate one similar text ( 125 ) on line objects on the top...
Now is more confusing... Do you want some text above and some below? They are all "125", which one are you trying to fix?

Thank you.

Yes I may add one more text on the opposite side but not now and the location of text is on the side of anticlockwise.

Hopefully its not bothering and won't take much codes.

Coder

  • Swamp Rat
  • Posts: 827
Re: get correct angle
« Reply #9 on: July 20, 2020, 12:04:02 PM »
The 125 represents the length of each line so the lengths are not the same all the time but in my above example they were the same by coincidence.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: get correct angle
« Reply #10 on: July 20, 2020, 12:26:31 PM »
The side which is anticlockwise is the top side. 
:?  :wideeyed2:

Code - Auto/Visual Lisp: [Select]
  1. ;u  - line angle
  2. ;a  - text angle
  3. ;pm - mid point
  4. ;pt - text position
  5. ;(setq d 2.5);text displacement
  6.  
  7. (setq u (angle p1 p2))
  8.  
  9. (setq a (atan (/ (sin u) (cos u))))
  10.  
  11. (setq pm (mapcar '(lambda (a b) (/ (+ a b) 2.0)) p1 p2)
  12.       pt (polar pm (+ u (/ pi 2)) d)
  13.      ;it might be (- u (/ pi 2.0)) for the... other anticlockwise direction, but then again, who knows exactly what to expect
  14. )
  15.  

Coder

  • Swamp Rat
  • Posts: 827
Re: get correct angle
« Reply #11 on: July 20, 2020, 01:23:28 PM »
Sorry, it does not work as expected.

Can you please check if the codes are correct with your codes?
Code: [Select]
(defun c:test (/ ss n obj start end lst ang pt center a)
  (setq ss (ssget '((0 . "LINE"))))
  (repeat (setq n (sslength ss))
    (setq obj (ssname ss (setq n (1- n))))
    (setq lst (entget obj))
    (setq start  (cdr (assoc 10 lst))
          end    (cdr (assoc 11 lst))
          ang    (angle start end)
          center (polar start ang (/ (distance start end) 2.0))
    )
    (setq a (atan (/ (sin ang) (cos ang))))
    (setq pt (polar center (- ang (/ pi 2)) 0.5))
    (entmakex (list '(0 . "TEXT")
                    (cons 1 (rtos (distance start end) 2 0))
                    (cons 10 pt)
                    (cons 11 pt)
                    (cons 40 0.35)
                    (cons 50 a)
              )
    )
  )
  (princ)
)

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: get correct angle
« Reply #12 on: July 20, 2020, 02:05:17 PM »
I give up on this. I'm sure you are nearly there, just a little push and you are done.


ronjonp

  • Needs a day job
  • Posts: 7529
Re: get correct angle
« Reply #13 on: July 20, 2020, 02:08:31 PM »
Sorry, it does not work as expected.

Can you please check if the codes are correct with your codes?
Code: [Select]
(defun c:test (/ ss n obj start end lst ang pt center a)
  (setq ss (ssget '((0 . "LINE"))))
  (repeat (setq n (sslength ss))
    (setq obj (ssname ss (setq n (1- n))))
    (setq lst (entget obj))
    (setq start  (cdr (assoc 10 lst))
          end    (cdr (assoc 11 lst))
          ang    (angle start end)
          center (polar start ang (/ (distance start end) 2.0))
    )
    (setq a (atan (/ (sin ang) (cos ang))))
    (setq pt (polar center (- ang (/ pi 2)) 0.5))
    (entmakex (list '(0 . "TEXT")
                    (cons 1 (rtos (distance start end) 2 0))
                    (cons 10 pt)
                    (cons 11 pt)
                    (cons 40 0.35)
                    (cons 50 a)
              )
    )
  )
  (princ)
)
This request is confusing. It would help to post your sample drawing too so we don't have to try to recreate something we know little about. Are you sure you don't just need a 'make text readable' function?
« Last Edit: July 20, 2020, 02:12:09 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: get correct angle
« Reply #14 on: July 20, 2020, 05:46:45 PM »
atan can take 1 or 2 arguments. For 2 arguments, returns an angle between 0 and 2pi, depending on the sign of the 2 arg. For 1 argument, returns an angle between -pi/2 and pi/2, which is exactly the range of the readable angles.

Neat trick.  :-)

Coder

  • Swamp Rat
  • Posts: 827
Re: get correct angle
« Reply #15 on: July 21, 2020, 06:04:45 AM »
I give up on this. I'm sure you are nearly there, just a little push and you are done.

Thank you Stefan for your kind replies and your help, I am very grateful.

This request is confusing. It would help to post your sample drawing too so we don't have to try to recreate something we know little about. Are you sure you don't just need a 'make text readable' function?

Many thanks ronjonp for your contribution, yes I am sure but to make the text readable is easy to work around but my concern was about the location of the text on certain side of the selected lines.

Anyway, that seems to be over my head and I already finished what I wanted to automate by codes.

Have a great days guys.