How to get the total length while using "PLINE" command?

How to get the total length while using "PLINE" command?

Anonymous
Not applicable
1,369 Views
7 Replies
Message 1 of 8

How to get the total length while using "PLINE" command?

Anonymous
Not applicable

Hello,

 

Can any help me how to get the total length while using "PLINE" command?

The routine will draw pline and show the total length everytime I choose the next point of the plines. The result can be exported to command window or shown in the drawing session in text format. 

Any suggestions would be appreciated.

 

Many thanks.

Khanh

 

 

 

 

 

 

 

 

 

 

0 Likes
Accepted solutions (1)
1,370 Views
7 Replies
Replies (7)
Message 2 of 8

DannyNL
Advisor
Advisor
Accepted solution

Something like this?

While drawing the polyline the total length will be shown in the status bar besides the coordinates. After finishing the polyline the total length will be shown in the command line.

 

Note: this will only work while drawing straight polyline segments and not using arc segments.

 

(defun c:Test (/ T_Base T_SelectedPoint T_Length T_NextPoint)
   (setq T_Length 0)
   (setq T_Base (getvar "LASTPOINT"))
   (command "_.PLINE")
   (while
      (= (getvar "CMDACTIVE") 1)           
      (command PAUSE)
      (cond
         (
            (and
               (not T_SelectedPoint)
               (not (equal T_Base (setq T_NextPoint (getvar "LASTPOINT")) 1e-8))
            )         
            (setq T_SelectedPoint T_NextPoint)            
         )
         (
            T_SelectedPoint
            (not (equal T_SelectedPoint (setq T_NextPoint (getvar "LASTPOINT")) 1e-8))
            (setq T_Length (+ T_Length (distance T_SelectedPoint T_NextPoint)))
            (setq T_SelectedPoint T_NextPoint)
            (grtext -1 (strcat "Total length: " (rtos T_Length)))
         )
         (
            T
            nil
         )
      )                     
   )
   (grtext)
   (if
      (> T_Length 0)
      (princ (strcat "Total length: " (rtos T_Length)))
   )
   (princ)
)
Message 3 of 8

marko_ribar
Advisor
Advisor

FWIW. This one works with arcs too... It operates the same way DannyNL explained, but it's more concise and works fine for all possible options provided with built-in PLINE command...

 

(defun c:pllen ( / *error* cmde len el )

  (vl-load-com)

  (defun *error* ( m )
    (if cmde
      (setvar 'cmdecho cmde)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (setq cmde (getvar 'cmdecho))
  (setvar 'cmdecho 1)
  (setq len 0.0 el (entlast))
  (vl-cmdf "_.PLINE")
  (while (< 0 (getvar 'cmdactive))
    (vl-cmdf "\\")
    (if
      (and
        (not (eq el (entlast)))
        (not (equal len (setq len (vla-get-length (vlax-ename->vla-object (entlast)))) 1e-3))
      )
      (grtext -1 (rtos len 2 50))
    )
  )
  (grtext)
  (prompt "\nTotal length : ") (princ (rtos len 2 50))
  (*error* nil)
)

Regards, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 4 of 8

DannyNL
Advisor
Advisor

That is actually really smart of Marko to use the polyline itself while it is being drawn.

I've modified my code as well to use the polyline's length instead of measuring the distance between the selected points. Works with line and arc segments as well now.

 

(defun c:Test (/ T_Length T_Last T_New T_NewLength)
   (setq T_Length 0)
   (setq T_Last (entlast))
   (command "_.PLINE")
   (while
      (= (getvar "CMDACTIVE") 1)           
      (command PAUSE)
      (if
         (and
            (not (eq (setq T_New (entlast)) T_Last))
            (not (equal T_Length (setq T_NewLength (vla-get-Length (vlax-ename->vla-object T_New))) 1e-8))
         )
         (grtext -1 (strcat "Total length: " (rtos (setq T_Length T_NewLength))))
      )                          
   )
   (grtext)
   (if
      (> T_Length 0)
      (princ (strcat "Total length: " (rtos T_Length)))
   )
   (princ)
)

 

0 Likes
Message 5 of 8

Anonymous
Not applicable

@DannyNL wrote:

Something like this?

While drawing the polyline the total length will be shown in the status bar besides the coordinates. After finishing the polyline the total length will be shown in the command line.

 

Note: this will only work while drawing straight polyline segments and not using arc segments.

 

(defun c:Test (/ T_Base T_SelectedPoint T_Length T_NextPoint)
   (setq T_Length 0)
   (setq T_Base (getvar "LASTPOINT"))
   (command "_.PLINE")
   (while
      (= (getvar "CMDACTIVE") 1)           
      (command PAUSE)
      (cond
         (
            (and
               (not T_SelectedPoint)
               (not (equal T_Base (setq T_NextPoint (getvar "LASTPOINT")) 1e-8))
            )         
            (setq T_SelectedPoint T_NextPoint)            
         )
         (
            T_SelectedPoint
            (not (equal T_SelectedPoint (setq T_NextPoint (getvar "LASTPOINT")) 1e-8))
            (setq T_Length (+ T_Length (distance T_SelectedPoint T_NextPoint)))
            (setq T_SelectedPoint T_NextPoint)
            (grtext -1 (strcat "Total length: " (rtos T_Length)))
         )
         (
            T
            nil
         )
      )                     
   )
   (grtext)
   (if
      (> T_Length 0)
      (princ (strcat "Total length: " (rtos T_Length)))
   )
   (princ)
)

Hello Danny,

Many thanks. It works really great as I wanted for my basic needs. 

Khanh

0 Likes
Message 6 of 8

DannyNL
Advisor
Advisor

You're welcome & glad I could help Smiley Happy

0 Likes
Message 7 of 8

Anonymous
Not applicable

Thanks Danny. Great improvements. 

Message 8 of 8

Anonymous
Not applicable

Thanks Marko. It is also great ways to fit my basic needs 🙂

0 Likes