Author Topic: line or polyline distance  (Read 12675 times)

0 Members and 1 Guest are viewing this topic.

pedroantonio

  • Guest
line or polyline distance
« on: April 07, 2014, 04:50:06 AM »
Hi, I try to write a lisp when i selected a line or polyline then  write the length on top of the line or polyline.

Can anyone help me to finish it. Thanks

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ e p ht scl)
  2.     "_layer" "_m" "dist" "_c" "3" "" "_lw" "0.30" "" ""
  3.    )
  4.   (SETQ SCL (GETVAR "useri1"))
  5.   (SETQ HT (* 0.00175 SCL))
  6.   (COMMAND "style" "diast" "wgsimpl.shx" "" "" "" "" "")
  7.   (if (and (setq e (entsel "\n select line or polyline :"))
  8.            (member (cdr (assoc 0 (entget (car e))))
  9.                    '("LINE" "LWPOLYLINE" "POLYLINE")
  10.            )
  11.       )
  12.     (progn
  13.       (entmakex (list '(0 . "TEXT")
  14.                       '(100 . "AcDbEntity")
  15.                       '(100 . "AcDbText")
  16.                       (cons 40 HT)
  17.                       (cons 10 (trans p 1 0))
  18.                       (cons 1
  19.                             (strcat (rtos (car p) 2)
  20.                                     (rtos (cadr p) 2)
  21.                                     (rtos (caddr p) 2)
  22.                             )
  23.                       )
  24.                       (cons 50 1.5708)
  25.                 )
  26.       )
  27.     )
  28.     (princ "\nSelect a LINE or POLYLINE !! Error selection .... Try again....")
  29.   )
  30.  
  31.   (princ)
  32. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: line or polyline distance
« Reply #1 on: April 07, 2014, 05:38:58 AM »
First the vlax-curve-getPointAtDist is probably not what you're after. You might rather want the vlax-curve-getDistAtParam and vlax-curve-getEndParam.

Next all those vlax-curve-* functions require at least one argument: the object's ename or vla reference (can be used interchangeably). I.e. the car of e. Some might require an extra argument, like the getPointAtDist would require a distance along the curve so it can return the point, or the getDistAtParam requires the param (vector index) along the curve so it can return the distance. In the last scenario you get the end of the curve from getEndParam which is then passed as the 2nd argument to getDistAtParam so you get the curve's total length.

Finally, you can then place the text using the vlax-curve-getPointAtDist (e.g. middle of curve by dividing the distance by 2). You might also want to rotate the text to follow the curve using vlax-curve-getFirstDeriv (this gives a 3d point in the direction following the curve, so you can use angle from a (0 0 0) point to convert it to radians.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pedroantonio

  • Guest
Re: line or polyline distance
« Reply #2 on: April 07, 2014, 09:01:13 AM »
Hi irneb, you confused me . I can not understand you .Can you fix the code?

Thanks

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: line or polyline distance
« Reply #3 on: April 07, 2014, 09:15:27 AM »
I'd rather show you how to do it yourself:
  • Where do you get the line / pline length? You use vlax-curve-getDistAtParam. This has other requirements because it needs you to specify where along the line / pline you want the length. So you tell it you want the length at the end, this you do by getting the EndParam so you can send that to getDistAtParam.
  • The vlax-curve functions need something you send them so they can extract info from that. In your code you simply call vlax-curve-getPointAtDist without anything sent to the function, this will error. Please read some of the help pages on these functions, they have examples of what to do when you use them.
  • Where do you want the text? Your code seems to obtain a point p somehow, but is it meant to be at the start of the line/pline, the end, the middle, or somewhere else? This is similar to point 2 ... if I can't tell what you wanted from the code, then the Lisp interpreter can't do so either.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pedroantonio

  • Guest
Re: line or polyline distance
« Reply #4 on: April 07, 2014, 01:21:30 PM »
 I try to write a lisp
1) select a line or polyline
2) write the length on top of the line or polyline.
I am not good in lisp. I know that you can find a lot of mistakes in the code. I am trying to learn some things. Can you helm me ?

But i want the code to have

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ e p ht scl)
  2.     "_layer" "_m" "dist" "_c" "3" "" "_lw" "0.30" "" ""
  3.    )
  4.   (SETQ SCL (GETVAR "useri1"))
  5.   (SETQ HT (* 0.00175 SCL))
  6.   (COMMAND "style" "diast" "wgsimpl.shx" "" "" "" "" "")
  7.   (if (and (setq e (entsel "\n select line or polyline :"))
  8.            (member (cdr (assoc 0 (entget (car e))))
  9.                    '("LINE" "LWPOLYLINE" "POLYLINE")
  10.            )
  11.      )
  12.     ------------------------------------------
  13. ------------------------------------------
  14. ----------------------------------------------
  15. ---------------------------------------------------
  16.   (princ)
  17. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: line or polyline distance
« Reply #5 on: April 08, 2014, 04:30:17 AM »
Fine
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test  (/ e p ht scl len pt ang)
  2.   (COMMAND "_layer" "_m" "dist" "_c" "3" "" "_lw" "0.30" "" "")
  3.   (SETQ scl (GETVAR "useri1"))
  4.   (SETQ ht (* 0.00175 SCL))
  5.   (COMMAND "style" "diast" "romans.shx")
  6.   (while (> (getvar "CmdActive") 0) (command ""))
  7.   (if (and (setq e (entsel "\n select line or polyline :"))
  8.            (setq e (car e))
  9.            (member (cdr (assoc 0 (entget e))) '("LINE" "LWPOLYLINE" "POLYLINE")))
  10.     (progn (setq len (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)) ;Get the length of the line/pline to its end
  11.                  pt (vlax-curve-getPointAtDist e (/ len 2.0)) ;Get the point at mid of line/pline
  12.                  ang (angle '(0.0 0.0 0.0) (vlax-curve-getFirstDeriv e (vlax-curve-getParamAtPoint e pt)))) ;Get angle at midpoint
  13.            (entmakex (list '(0 . "TEXT")
  14.                            '(100 . "AcDbEntity")
  15.                            '(100 . "AcDbText")
  16.                            (cons 40 ht)
  17.                            '(7 . "diast") ;Text style
  18.                            (cons 10 pt) ;The insert point of the text
  19.                            (cons 1 (rtos len 2)) ;The length converted to text
  20.                            (cons 50 ang)))) ;Rotate the text to follow the line/pline
  21.     (princ "\nSelect a LINE or POLYLINE !! Error selection .... Try again...."))
  22.   (princ))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pedroantonio

  • Guest
Re: line or polyline distance
« Reply #6 on: April 09, 2014, 10:40:08 AM »
Thank you irneb for the code.

I want the dimension text be 0.08m over the line .How can i do it ?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test  (/ e p ht scl len pt ang)
  2.   (COMMAND "_layer" "_m" "dist" "_c" "3" "" "_lw" "0.30" "" "")
  3.   (SETQ scl (GETVAR "useri1"))
  4.   (SETQ ht (* 0.00175 SCL))
  5.   (COMMAND "style" "diast" "romans.shx")
  6.   (while (> (getvar "CmdActive") 0) (command ""))
  7.   (if (and (setq e (entsel "\n select line or polyline :"))
  8.            (setq e (car e))
  9.            (member (cdr (assoc 0 (entget e))) '("LINE" "LWPOLYLINE" "POLYLINE")))
  10.     (progn (setq len (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)) ;Get the length of the line/pline to its end
  11.                  pt (vlax-curve-getPointAtDist e (/ len 2.0)) ;Get the point at mid of line/pline
  12.                  ang (angle '(0.0 0.0 0.0) (vlax-curve-getFirstDeriv e (vlax-curve-getParamAtPoint e pt)))) ;Get angle at midpoint
  13.            (entmakex (list '(0 . "TEXT")
  14.                            '(100 . "AcDbEntity")
  15.                            '(100 . "AcDbText")
  16.                            (cons 40 ht)
  17.                            '(7 . "diast") ;Text style
  18.                            (cons 71 1)
  19.                            (cons 10 pt) ;The insert point of the text
  20.                            (cons 1 (rtos len 2 2)) ;The length converted to text
  21.                            (cons 50 ang)))) ;Rotate the text to follow the line/pline
  22.     (princ "\nSelect a LINE or POLYLINE !! Error selection .... Try again...."))
  23.   (princ))

ronjonp

  • Needs a day job
  • Posts: 7527
Re: line or polyline distance
« Reply #7 on: April 09, 2014, 10:58:58 AM »
Look into the POLAR function. See if you can figure out where to plug this in.


Code: [Select]
(polar pt (+ (/ pi 2) ang) 0.08)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

pedroantonio

  • Guest
Re: line or polyline distance
« Reply #8 on: April 09, 2014, 11:09:56 AM »
Hi  ronjonp, I don't know how to use it this

Code: [Select]
(polar pt (+ (/ pi 2) ang) 0.08)
Can you be more specific?

Thanks

ronjonp

  • Needs a day job
  • Posts: 7527
Re: line or polyline distance
« Reply #9 on: April 09, 2014, 11:16:25 AM »
Hi  ronjonp, I don't know how to use it this

Code: [Select]
(polar pt (+ (/ pi 2) ang) 0.08)
Can you be more specific?

Thanks


Really?  :?  .. Irneb commented the code he wrote for you .. I'm sure you can figure it out.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

pedroantonio

  • Guest
Re: line or polyline distance
« Reply #10 on: April 09, 2014, 01:52:57 PM »
I try to align the textin the midle center and I want the dimension text be 0.08m over the line . Can anyone help?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test  (/ e p ht scl len pt ang)
  2. (setvar "cmdecho" 0)
  3. (setq flg 1)
  4. (setvar "cmdecho" 0)
  5.   (COMMAND "_layer" "_m" "dist" "_c" "3" "" "_lw" "0.30" "" "")
  6.   (SETQ scl (GETVAR "useri1"))
  7.   (SETQ ht (* 0.00175 SCL))
  8.   (COMMAND "style" "diast" "romans.shx")
  9.   (while (> (getvar "CmdActive") 0) (command ""))
  10.   (if (and (setq e (entsel "\n select line or polyline :"))
  11.            (setq e (car e))
  12.            (member (cdr (assoc 0 (entget e))) '("LINE" "LWPOLYLINE" "POLYLINE")))
  13.     (progn (setq len (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)) ;Get the length of the line/pline to its end
  14.                  pt (vlax-curve-getPointAtDist e (/ len 2.0)) ;Get the point at mid of line/pline
  15.                  ang (angle '(0.0 0.0 0.0) (vlax-curve-getFirstDeriv e (vlax-curve-getParamAtPoint e pt)))) ;Get angle at midpoint
  16.            (entmakex (list '(0 . "TEXT")
  17.                            '(100 . "AcDbEntity")
  18.                            '(100 . "AcDbText")
  19.                            (cons 40 ht)
  20.                            '(7 . "diast") ;Text style
  21.                            '(72 . 1) ;; Center
  22.                            '(73 . 2) ;; Middle
  23.                            (cons 71 1) ; Text generation flags
  24.                            (cons 10 pt) ;The insert point of the text
  25.                            (cons 11 pt) ;The insert point of the text
  26.                            (cons 1 (rtos len 2 2)) ;The length converted to text
  27.                            (cons 50 ang)))) ;Rotate the text to follow the line/pline
  28.     (princ "\nSelect a LINE or POLYLINE !! Error selection .... Try again...."))
  29.   (princ))
  30.  

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: line or polyline distance
« Reply #11 on: April 09, 2014, 02:57:22 PM »
I try to align the textin the midle center and I want the dimension text be 0.08m over the line . Can anyone help?
Ronjon's code will return a "moved" point (i.e. the XYZ value) from the one saved in the variable pt by moving it 0.08 units perpendicular to the angle saved in the variable ang.

Then as noted:
Irneb commented the code he wrote for you ... I'm sure you can figure it out.
Look for where I commented "The insertion point of the text" and replace the pt with either that code or another variable you've set to the return value of that code.

Please, I'd beg you to at least try and modify the code yourself instead of simply asking for someone to do it for you. Believe me, you'll be a lot better off if you can modify these things for yourself. Don't worry about making mistakes ... be glad of mistakes because they are the true teachers.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pedroantonio

  • Guest
Re: line or polyline distance
« Reply #12 on: April 09, 2014, 03:08:42 PM »
Ok irneb ,thank you for the reply. I understand the part the 0.08 m ,but what about the midle center of the text ???
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test  (/ e p ht scl len pt ang)
  2. (setvar "cmdecho" 0)
  3. (setq flg 1)
  4. (setvar "cmdecho" 0)
  5.   (COMMAND "_layer" "_m" "dist" "_c" "3" "" "_lw" "0.30" "" "")
  6.   (SETQ scl (GETVAR "useri1"))
  7.   (SETQ ht (* 0.00175 SCL))
  8.   (COMMAND "style" "diast" "romans.shx")
  9.   (while (> (getvar "CmdActive") 0) (command ""))
  10.   (if (and (setq e (entsel "\n select line or polyline :"))
  11.            (setq e (car e))
  12.            (member (cdr (assoc 0 (entget e))) '("LINE" "LWPOLYLINE" "POLYLINE")))
  13.     (progn (setq len (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)) ;Get the length of the line/pline to its end
  14.                  pt (vlax-curve-getPointAtDist e (/ len 2.0)) ;Get the point at mid of line/pline
  15.                  ang (angle '(0.0 0.0 0.0) (vlax-curve-getFirstDeriv e (vlax-curve-getParamAtPoint e pt)))) ;Get angle at
  16.  
  17. midpoint
  18.            (entmakex (list '(0 . "TEXT")
  19.                            '(100 . "AcDbEntity")
  20.                            '(100 . "AcDbText")
  21.                            (cons 40 ht)
  22.                           '(7 . "diast") ;Text style
  23.                             (cons 71 1) ; Text generation flags
  24.                            (cons 10 (polar pt (+ (/ pi 2) ang) 0.08)) ;The insert point of the text
  25.                            (cons 1 (rtos len 2 2)) ;The length converted to text
  26.                            (cons 50 ang)))) ;Rotate the text to follow the line/pline
  27.     (princ "\nSelect a LINE or POLYLINE !! Error selection .... Try again...."))
  28.   (princ))
  29.  

Thanks

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: line or polyline distance
« Reply #13 on: April 09, 2014, 03:24:45 PM »
... but what about the midle center of the text
If you compare an entity list from a left aligned text with that of a middle center aligned text you will find the group code items that need to be added to, or changed in, the entmakex list.

pedroantonio

  • Guest
Re: line or polyline distance
« Reply #14 on: April 09, 2014, 05:50:36 PM »
I can't find the error .please help

I add this two lines for Middle Center Justification
               
Code: [Select]
(cons 72  1) ;Horizontal text justification type
(cons 73  2) ;Vertical text justification type

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test  (/ e p ht scl len pt ang)
  2. (setvar "cmdecho" 0)
  3. (setq flg 1)
  4. (setvar "cmdecho" 0)
  5.   (COMMAND "_layer" "_m" "dist" "_c" "3" "" "_lw" "0.30" "" "")
  6.   (SETQ scl (GETVAR "useri1"))
  7.   (SETQ ht (* 0.00175 SCL))
  8.   (SETQ kl (* 0.0004 SCL))
  9.   (COMMAND "style" "diast" "romans.shx")
  10.   (while (> (getvar "CmdActive") 0) (command ""))
  11.   (if (and (setq e (entsel "\n \n Επιλέξτε μια line ή polyline :"))
  12.            (setq e (car e))
  13.            (member (cdr (assoc 0 (entget e))) '("LINE" "LWPOLYLINE" "POLYLINE")))
  14.     (progn (setq len (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)) ;Get the length of the line/pline to its end
  15.                  pt (vlax-curve-getPointAtDist e (/ len 2.0)) ;Get the point at mid of line/pline
  16.                  ang (angle '(0.0 0.0 0.0) (vlax-curve-getFirstDeriv e (vlax-curve-getParamAtPoint e pt)))) ;Get angle at
  17.  
  18. midpoint
  19.            (entmakex (list '(0 . "TEXT")
  20.                            '(100 . "AcDbEntity")
  21.                            '(100 . "AcDbText")
  22.                            (cons 40 ht)
  23.                            '(7 . "diast") ;Text style
  24.                            (cons 71 1) ; Text generation flags
  25.                            (cons 72  1) ;Horizontal text justification type
  26.                            (cons 73  2) ;Vertical text justification type
  27.                            (cons 10 (polar pt (+ (/ pi 2) ang) kl)) ;The insert point of the text
  28.                            (cons 1 (rtos len 2 2)) ;The length converted to text
  29.                            (cons 50 ang)))) ;Rotate the text to follow the line/pline
  30.     (princ "\nSelect a LINE or POLYLINE !! Error selection .... Try again...."))
  31.   (princ))
  32.