Author Topic: Line with aligned text (Calculation of angle)  (Read 1608 times)

0 Members and 1 Guest are viewing this topic.

Logan

  • Newt
  • Posts: 41
Line with aligned text (Calculation of angle)
« on: February 06, 2014, 07:56:12 PM »
Hello guys.

I found a program here in the forum, written by Lee Mac. (The reviews were inserted in the header)
This program draws a line and puts the length at mid point. I decided to make some changes for my use, I'm coming to the conclusion that the best way to learn is to try. If I'm wrong about this please correct me.

Please, before you criticize my bad programming practices, remember that I'm just starting, but any advice would be most welcome, I'm here to learn. As I do not yet have the language domain, so i just did a MacGyver on it.  :ugly:

The adjustments I made in the program are working, but I'm struggling to find a solution to a small problem.

When I'm doing a line from left to right, the text is above on the line as expected, however, as the line is created from right left, the text is rotated below the line. I know it is related to the calculation of the angle, but my limited knowledge does not allow me to see the solution.
Could you help me?

Thank you very much.

Logan

Code - Auto/Visual Lisp: [Select]
  1.  
  2.  
  3. ;;----------------------------------------------------------------------;;
  4. ;;  http://www.theswamp.org/index.php?topic=42402.0                     ;;
  5. ;;----------------------------------------------------------------------;;
  6. ;;  Author:  Lee Mac, Copyright © 2012  -  www.lee-mac.com              ;;
  7. ;;----------------------------------------------------------------------;;
  8. ;;  Version 0.0    -     August 03, 2012, 11:59:29 am                   ;;
  9. ;;                                                                      ;;
  10. ;;  First release - previously 'Line with aligned text'.                ;;
  11. ;;----------------------------------------------------------------------;;
  12. ;;  Modified by Luís Augusto                                            ;;
  13. ;;  Changes for personal use                                            ;;
  14. ;;  Entering getInt                                                     ;;
  15. ;;  Setting the value of the text in 3.0                                ;;
  16. ;;  Setting Justify Botton Center                                       ;;
  17. ;;  Setting offset of text in relation to the line 0.0                  ;;
  18. ;;  Entering Looping function                                           ;;
  19. ;;----------------------------------------------------------------------;;
  20.  
  21. (defun c:dmid ( / an di p1 p2 tp ) ;; Define function, localise variables
  22.     (if ;; if the following test expression returns a non-nil value
  23.         (and ;; all of the following expressions must return a non-nil value
  24.              ;; Collect the first point from the user, assign to p1
  25.              (setq p1 (getpoint "\nSpecify First Point: "))
  26.              ;; Collect the second point from the user, rubber-band to p1, assign to p2
  27.              (setq p2 (getpoint "\nSpecify Second Point: " p1))
  28.         ) ;; end and
  29.         (progn ;; Evaluate the following expressions as the 'then' expression for the if function
  30.             ;; Calculate the distance between the points
  31.             (setq di (distance p1 p2)
  32.             ;; Calculate the angle between the points
  33.                   an (angle p1 p2)
  34.             ;; Calculate the point for the Text
  35.                   tp (polar
  36.                          (polar p1 an (/ di 2.0)) ;; Line midpoint
  37.                          (+ an (/ pi 2.0))  ;; Perpendicular to Line
  38.                                                   0 ;Offset 0
  39.                          ;(getvar 'textsize) ;; Offset by text height
  40.                      )
  41.             )
  42.                        
  43.                         ;; Text above
  44.                         (cond
  45.                   ((< an (* pi 1.5) (setq an (+ an pi))))
  46.                   ((> an (* pi 1.5) (setq an (+ an pi))))
  47.                 )
  48.            
  49.                         ;; Create the line between the points
  50.             (entmake
  51.                 (list
  52.                    '(0 . "LINE") ;; Entity type
  53.                     (cons 10 p1) ;; Start point
  54.                     (cons 11 p2) ;; End point
  55.                 ) ;; end list
  56.             ) ;; end entmake
  57.             ;; Create the text to display the distance
  58.                         (setq cont (getint "\nEnter the value :")) ;;Get value text content
  59.             (entmake
  60.                 (list
  61.                    '(0 . "TEXT") ;; Entity type
  62.                     (cons 10 tp) ;; Insertion Point
  63.                     (cons 11 tp) ;; Alignment POint
  64.                     ;(cons  1 (rtos di)) ;; Content
  65.                                         (cons  1 (rtos cont)) ;; Content
  66.                     ;(cons 40 (getvar 'textsize)) ;; Height
  67.                                         (cons 40 3.0) ;; Height
  68.                     (cons 50 an) ;; Angle
  69.                    '(72 . 1) ;; Center
  70.                    '(73 . 1) ;; 0 Center / 1 Botton Center / 2 Middle Center ....
  71.                 ) ;; end list
  72.             ) ;; end entmake
  73.         ) ;; end progn
  74.     ) ;; end if
  75.     (princ) ;; Suppress the return of the last evaluated expression
  76.         (Loop)
  77. ) ;; end defun
  78.  
  79. (defun Loop (/ ang dist tpp)
  80.   (setq pnext p2)
  81.     (if ;; if the following test expression returns a non-nil value
  82.         (and ;; all of the following expressions must return a non-nil value
  83.              ;; Define pnext
  84.              (setq pnext pnext)
  85.              ;; Collect the second point from the user, rubber-band to pnext, assign to p2
  86.              (setq p2 (getpoint "\nSpecify Second Point: " pnext))
  87.         ) ;; end and
  88.         (progn ;; Evaluate the following expressions as the 'then' expression for the if function
  89.             ;; Calculate the distance between the points
  90.             (setq dist (distance pnext p2)
  91.             ;; Calculate the angle between the points
  92.                   ang (angle pnext p2)
  93.             ;; Calculate the point for the Text
  94.                   tpp (polar
  95.                          (polar pnext ang (/ dist 2.0)) ;; Line midpoint
  96.                          (+ ang (/ pi 2.0))  ;; Perpendicular to Line
  97.                                                   0 ;Offset 0
  98.                          ;(getvar 'textsize) ;; Offset by text height
  99.                      )
  100.             )
  101.                        
  102.                         ;; Text above
  103.                         (cond
  104.                   ((< ang (* pi 1.5) (setq ang (+ ang pi))))
  105.                   ((> ang (* pi 1.5) (setq ang (+ ang pi))))
  106.                 )
  107.                        
  108.             ;; Create the line between the points
  109.             (entmake
  110.                 (list
  111.                    '(0 . "LINE") ;; Entity type
  112.                     (cons 10 pnext) ;; Start point
  113.                     (cons 11 p2) ;; End point
  114.                 ) ;; end list
  115.             ) ;; end entmake
  116.             ;; Create the text to display the distance
  117.                         (setq cont (getint "\nEnter the value :")) ;;Get value text content
  118.             (entmake
  119.                 (list
  120.                    '(0 . "TEXT") ;; Entity type
  121.                     (cons 10 tpp) ;; Insertion Point
  122.                     (cons 11 tpp) ;; Alignment POint
  123.                     ;(cons  1 (rtos dist)) ;; Content
  124.                                         (cons  1 (rtos cont)) ;; Content
  125.                     ;(cons 40 (getvar 'textsize)) ;; Height
  126.                                         (cons 40 3.0) ;; Height
  127.                     (cons 50 ang) ;; Angle
  128.                    '(72 . 1) ;; Center
  129.                    '(73 . 1) ;; 0 Center / 1 Botton Center / 2 Middle Center ....
  130.                 ) ;; end list
  131.             ) ;; end entmake
  132.         ) ;; end progn
  133.     ) ;; end if
  134.     (princ) ;; Suppress the return of the last evaluated expression
  135.         (Loop1)
  136. ) ;; end defun
  137.  
  138. (defun Loop1 ()(loop))
  139.  
  140. (princ) ;; Suppress return of expression evaluated on load
  141.  
  142.  
« Last Edit: February 09, 2014, 12:17:22 AM by Logan »

ymg

  • Guest
Re: Line with aligned text (Calculation of angle)
« Reply #1 on: February 06, 2014, 11:07:17 PM »
Logan,

Look in the various forum for make readable.

Basically, when the angle of your line is between 90 (pi/2)  and 270 (1.5pi), your need to add 180 to the
the angle you got from reading the dxfcode of the line.

Code - Auto/Visual Lisp: [Select]
  1. (rem (if (< (* pi 0.5) (setq a (rem (+ a pi pi) (+ pi pi))) (* pi 1.5))(+ a pi) a) (+ pi pi))

ymg
« Last Edit: February 06, 2014, 11:29:31 PM by ymg »

Logan

  • Newt
  • Posts: 41
Re: Line with aligned text (Calculation of angle)
« Reply #2 on: February 07, 2014, 06:19:45 PM »
Thanks for your response ymg.
I will try to implement the solution, when finish, I'll post the result. 

Best regards,
Logan

Logan

  • Newt
  • Posts: 41
Re: Line with aligned text (Calculation of angle)
« Reply #3 on: February 09, 2014, 12:27:38 AM »
I will try to implement the solution, when finish, I'll post the result. 

As promised I have updated the code in post 01.
Maybe not the best way, but it is working.  :-D
ymg, thank you for help.

Best regards, Logan

ymg

  • Guest
Re: Line with aligned text (Calculation of angle)
« Reply #4 on: February 09, 2014, 02:53:50 AM »
Logan,

Glad! to hear it works.

Sometimes "Best" is the ennemy of "Just Enough"

ymg