Print Page | Close Window

Extract polyline total length

Printed From: CAD Forum
Category: EN
Forum Name: AutoCAD
Forum Description: Discussion about AutoCAD and AutoCAD LT, viewers, DWG and DWF formats, Design Review, AutoCAD 360, add-ons
URL: https://www.cadforum.cz/forum_en/forum_posts.asp?TID=1854
Printed Date: 21.Apr.2026 at 08:58


Topic: Extract polyline total length
Posted By: sadhu
Subject: Extract polyline total length
Date Posted: 29.Mar.2009 at 20:14
I have several polylines at the end of which there is a block. I need to extract the total length of each polyline togather with an attribute of the attached block.

All help is appreciated.






Replies:
Posted By: msplcdykee69
Date Posted: 29.Mar.2009 at 21:47
There are 2 way to do this: One is to use this lisp command "LNL" Copy this into a notepad and save as a lisp so you can install in CAD.  The second list works like the distance command except it is cumulative distance "CD" it gives you the length from point to point plus toe total length of what you have measured.  The lisp command for this is below as well.
One:
;;; LENLYR.LSP a program to find the length of all
;;; entities on any selected layer
;;;
(defun err (s)
  (if (= s "Function cancelled")
    (princ "\nLENLYR - cancelled: ")
    (progn (princ "\nLENLYR - Error: ") (princ s)
    (terpri))
  ); if
  (resetting)
  (princ "SYSTEM VARIABLES have been reset\n")
  (princ)
); err
(defun setv (systvar newval)
   (setq x (read (strcat systvar "1")))  
   (set x (getvar systvar))
   (setvar systvar newval)
); setv
(defun setting ()
   (setq oerr *error*)
   (setq *error* err)
   (setv "CMDECHO" 0)
   (setv "BLIPMODE" 0)
); end of setting
(defun rsetv (systvar)
   (setq x (read (strcat systvar "1")))
   (setvar systvar (eval x))
); restv
(defun resetting ()
   (rsetv "CMDECHO")
   (rsetv "BLIPMODE")
   (setq *error* oerr)
); end of resetting
(defun dxf (code ename)
  (cdr (assoc code (entget ename)))
); dxf
(defun lenlyr ( / LayerList EntityList EntityLengths)
  (setq LayerList (get-lyrs)
        EntityList (get-ents LayerList)
        EntityLengths (do-len EntityList)
  ) ; setq
  (prin1 (read (rtos EntityLengths 2 2)))
); lenlyr
(defun get-lyrs ( / isent lyr-list ss i lname)
  (setq isent nil lyr-list nil)
  (prompt
       "\nSelect object(s) on required layer(s): ")
  (setq ss nil)
  (while (not ss)
    (setq ss (ssget))
    (if (not ss)
        (prompt "\nNo entities were selected.")
    ); if
  ); while
  (setq i (- 1))
  (repeat (sslength ss)
    (setq lname (dxf 8 (ssname ss (setq i (1+ i)))))
    (if (not (member lname lyr-list)) 
      (setq lyr-list (append lyr-list (list lname)))
    ); if
  ); repeat
  lyr-list
); get-lyrs
(defun get-ents (LayerList / lstr lst lyr str
      filtlist i ss ename elist)
  (setq lstr (strcat (car LayerList) ","))
  (setq lst (cdr LayerList))
  (foreach lyr lst
    (setq str (strcat lyr ",")
          lstr (strcat lstr str)
    ); setq
  ); foreach
  (setq lstr (substr lstr 1 (1- (strlen lstr))))
  (setq filtlist (list
       (cons 8 lstr)
        '(-4 . "<or")
          '(0 . "LINE")
          '(0 . "ARC")
          '(0 . "CIRCLE")
          '(0 . "POLYLINE")
          '(0 . "LWPOLYLINE")
        '(-4 . "or>")
    ); list
  ); setq
  (setq i (- 1) elist nil ss nil)
  (setq ss (ssget "X" filtlist))
  (if ss
    (progn
      (repeat (sslength ss)
        (setq ename (ssname ss (setq i (1+ i))))
        (setq elist (append elist (list ename)))
      ); repeat
      (princ (strcat "\nTotal length of all "
            "entities on layer(s) " lstr ": ")
      ); print
     ); progn
     (progn
       (alert (strcat "No eligible entities on "
                "layer(s) \n" lstr "\naborting."))
       (exit)
     ); progn
   ); if
  elist
); get-ents
(defun do-len (EntityList / TotalLength ent len)
  (setq TotalLength 0 len 0)
  (foreach ent EntityList
    (setq len (ent-len ent))
    (if len
      (setq TotalLength (+ len TotalLength))
    ); if
  ); foreach
); do-len
(defun ent-len (en / elength)
  (setq elength
    (cond
      ((= (dxf 0 en ) "LINE") (do-line en))
      ((= (dxf 0 en ) "ARC") (do-arc en))
      ((= (dxf 0 en ) "CIRCLE") (do-circle en))
      ((= (dxf 0 en ) "POLYLINE") (do-pline en))
      ((= (dxf 0 en ) "LWPOLYLINE") (do-lwpline en))
    ); cond
  ); setq
  elength
); ent-len
(defun do-line (en / p10 p11 len)
  (setq p10 (dxf 10 en)
        p11 (dxf 11 en)
        len (distance p10 p11)
  ); setq
  len
); do-line
(defun do-arc (en / rad sang eang theta len)
  (setq rad (dxf 40 en)
        sang (dxf 50 en)
        eang (dxf 51 en)
        theta (- eang sang)
  ); setq
  (if (minusp theta)
    (setq theta (+ theta pi pi))
  ); if
  (setq len (* rad theta))
); do-arc
(defun do-circle (en / rad len)
  (setq rad (dxf 40 en)
        len (* rad pi 2)
  ); setq
  len
); do-circle
(defun do-pline (en / is-closed vlist vvlist len)
  (setq is-closed (dxf 70 en))
  (if (/= is-closed 100)
    (progn
      (setq en (entnext en))
      (setq vlist nil
            vvlist nil
      ); setq
      (while (/= "SEQEND" (dxf 0 en))
        (setq vlist (dxf 10 en)
              vlist (reverse vlist)
              vlist (cdr vlist)
              vlist (list (reverse vlist))
        ); setq
        (setq vlist
          (append vlist (list (dxf 42 en))))
        (setq vvlist (append vvlist (list vlist)))
        (setq en (entnext en))
      ); while
    ); progn
  ); if
  (setq len (do-polylen vvlist is-closed))
); do-pline
(defun do-lwpline (en / num-vert is-closed elist
     vlist vvlist len)
  (setq num-vert (dxf 90 en)
        is-closed (dxf 70 en)
        elist (entget en)
        elist (member (assoc 10 elist) elist)
        vlist nil
        vvlist nil
  ); setq
  (repeat num-vert
    (setq vlist (list (cdr (assoc 10 elist))))
    (setq vlist
       (append vlist (list (cdr (assoc 42 elist)))))
    (setq vvlist (append vvlist (list vlist)))
    (setq elist (cdr elist)
          elist (member (assoc 10 elist) elist)
    ); setq
  ); repeat
  (setq len (do-polylen vvlist is-closed))
); do-lwpline
(defun do-polylen (vvlist is-closed / closed seglen
     plen first p10 p11 bulge)
  (setq closed (logand is-closed 1)
        seglen 0
        plen 0
  ); setq
  (if (= closed 1)
    (progn
      (setq first (car vvlist)
            vvlist (append vvlist (list first))
      ); setq
    ); progn
  ); if
  (repeat (1- (length vvlist))
    (setq p10 (caar vvlist)
          p11 (caadr vvlist)
          bulge (cadar vvlist)
    ); setq
    (setq seglen (do-seg p10 p11 bulge))
    (setq plen (+ plen seglen))
    (setq vvlist (cdr vvlist))
  ); repeat
  plen
); do-polylen
(defun do-seg (p1 p2 bulg / seglen ang4 ang dis rad)
  (if (= bulg 0.0)
    (progn
       (setq seglen (distance p1 p2))
    ); progn
    (progn
      (setq ang4 (atan bulg)
            ang (* 4.0 ang4)
            dis (distance p1 p2)
            rad (/ (/ dis 2.0) (sin (/ ang 2.0)))
            seglen (* rad ang)
       ); setq
    ); progn
  ); if
  seglen
); do-seg
(defun c:lnl ()
   (setting)
   (lenlyr)
   (resetting)
   (princ)
); c:lnl
(prompt "\nEnter LNL to start")
 
two:
;cumulative distance
;this routine is just like the Autocad Distance command with the
;exception that it allows you to pick more than 2 consecutive points.
;the routine will display the cumulative distance and the distance
;between the last two points picked on the command line.
(defun c:cd ()
 (setvar "cmdecho" 0)
 (graphscr)
 (setq
  p1 (getpoint "\nPick start point ")
  p2 (getpoint p1 "\nPick next point ")
  d1 (distance p1 p2)
  prdist (strcat "\nDistance: " (rtos d1))
 )
 (princ prdist)
 (setq p3 (getpoint p2 "\nPick next point or RETURN if done "))
 (while p3
  (setq
   d0 (distance p2 p3)
   d1 (+ (distance p2 p3) d1)
   p2 p3
   prdist (strcat "\nDistance: " (rtos d0) ", Cumulative distance: " (rtos d1))
  )
  (princ prdist)
  (setq p3 (getpoint p2 "\nPick Next Point "))
 )
 (setq cumd (strcat "Cumulative distance --> " (rtos d1)))
 (prompt cumd)
 (princ)
)
(princ "\nType CD to run Cumulative Distance")
(princ)


Posted By: Arben.Allaraj
Date Posted: 29.Mar.2009 at 22:27
 
 msplcdykee69

  Nice AutoLISP program .

-------------
Ing Arben.Allaraj
http://cad-drafting-corner.blogspot.com


Posted By: sadhu
Date Posted: 31.Mar.2009 at 18:44
How does the lisp command "LNL" work ? After loading the lisp file I called LNL and selected various polylines - where do I get the results ?

CD works perfect.


Posted By: msplcdykee69
Date Posted: 31.Mar.2009 at 22:14
if you look at your command line after you call the command LNL and then select the layer they are on it will list the total length on the command line

-------------
Engineering Manufacturing Medical Cables/Adjunct Instructor Engineering Graphics/Engineering Computations and Careers

msplcdykee69@yahoo.com

Shawn


Posted By: sadhu
Date Posted: 31.Mar.2009 at 23:35
After I type LNL I'm asked to select objects. I then select all objects on the current layer and then hit "ENTER". I don't see any result on the command line.

What am I doing wrong ? What do you mean by "select the layer" ?



 


Posted By: msplcdykee69
Date Posted: 01.Apr.2009 at 04:00

Well First you did seperate the two lisps into two seperate applications right?  I am sure you did. 

here is what should come up when you imput LNL in the command prompt:

Command: lnl

Select object(s) on required layer(s):
Select objects: 1 found

Select objects:

Total length of all entities on layer(s) 0: 402.48

If you have any other problems just email me and I will send you my lisp program That does run correctly.  I have been using it for years.
 
mailto:msplcdykee69@yahoo.com - msplcdykee69@yahoo.com


-------------
Engineering Manufacturing Medical Cables/Adjunct Instructor Engineering Graphics/Engineering Computations and Careers

msplcdykee69@yahoo.com

Shawn


Posted By: sadhu
Date Posted: 06.Apr.2009 at 19:48
yes it works.  I just had to enlarge the command line window.

Thanks a lot. 


Posted By: msplcdykee69
Date Posted: 07.Apr.2009 at 04:20
Glad it was an easy fix.. that is always good.

-------------
Engineering Manufacturing Medical Cables/Adjunct Instructor Engineering Graphics/Engineering Computations and Careers

msplcdykee69@yahoo.com

Shawn


Posted By: sadhu
Date Posted: 18.Apr.2009 at 12:26
Hello,

I'am using the CD.lisp and need to add a few features.

1.  enter a value from keyboard that will add to the cumulative distance to misure ;

2.  enter a second value from keyboard that will add to the cumulative distance  measured at the end of the consecutive clicks;

3. add three constant values (offset1, offset2 add tolerance) in background to the total value. These constant values need to be defined at the beginning of every new project.

thanks


Posted By: sadhu
Date Posted: 19.Apr.2009 at 15:26
What I need to do is measure the distance from an electrical distribution box to an outlet box in an apartement or residence. The process is repetitive.
 
-The distribution box is always 30cm from the floor (A) ;
-the distance on the floor is almost always about two to four segments (polyline - in fact I use CD.lisp here) (B) ;
-the outlet box is at a variable distance from the floor. (30, 100, 110,  120 cm) (D).;
- To these three misurments (A+B+C) I need to add three constant values (k1=15cm, k2=20cm, k3=15cm). These constant values differ from project to project (building to building).  
 
So the result is (A+k1)+(B+k2)+(C+k3)
 
1. A can be entered from keyboard or autolisp
2. B is measured by CD.lisp (consecutive clicks)
3. C should be entered by keyboard
4. k1, k2 and k3 can be entered by lisp (prefered) or keyboard.
 
The total measurement that I get in the end [(A+k1)+(B+k2)+(C+k3)] is used in manufacturing to cut tubes of appropriate length.
 
Actually there are objects on the drawing that have height attributes - I wonder if they can be used.

I hope the explanation is clear enough.
 
I work in the electrical section but this is the installation part.



Print Page | Close Window