LISP SUM OF LINES

LISP SUM OF LINES

immobiliareA2T2L
Explorer Explorer
620 Views
8 Replies
Message 1 of 9

LISP SUM OF LINES

immobiliareA2T2L
Explorer
Explorer

How can you add up all the lines in the same layer?

0 Likes
621 Views
8 Replies
Replies (8)
Message 2 of 9

pbejse
Mentor
Mentor

@immobiliareA2T2L wrote:

How can you add up all the lines in the same layer?



Exclusively LINE Entities? or you meant any objects with Length property?  and of a specific layer? or it varies?

For LINES only

(defun c:LengthOfLines (/ LayerOfLines objectData objectSelection sumOf entName i)
  (if
    (and
      (princ "\Select Line entity")
      (Setq LayerOfLines (ssget "_+.:S:E" '((0 . "LINE"))))
      (Setq objectData (entget (ssname LayerOfLines 0)))
      (setq objectSelection
	     (ssget "_X"
		    (setq objectProperty
			   (mapcar
			     '(lambda (d)
				(assoc d objectData)
			      )
			     '(0 8 410)
			   )
		    )
	     )
      )
    )
     (repeat (setq sumOf 0
		   i	 (sslength objectSelection)
	     )
       (setq entName (ssname objectSelection (Setq i (1- i))))
       (setq sumOf (+ sumOf
		      (vlax-curve-getDistAtParam
			entName
			(vlax-curve-getEndParam entName)
		      )
		   )
       )
     )
  )
  (and (> sumOf 0)
       (princ (strcat "\nTotal Length of selected LINES at Layer "
		      (cdadr objectProperty)
		      ": "
		      (rtos sumOf 2)
	      )
       )
  )
  (princ)
)

 

0 Likes
Message 3 of 9

pbejse
Mentor
Mentor
(defun c:LengthOf (/ objectType objectData objectSelection sumOf entName i)
  (if
    (and
      (setq objectType
	     (car (entsel "\nSelect Object type to porcess: "))
      )
      (setq objectData (entget objectType))
      (member (cdr (assoc 0 objectData))
	      '("ARC" "CIRCLE" "ELLIPSE" "LINE" "LWPOLYLINE" "SPLINE")
      )
      (setq objectSelection
	     (ssget "_X"
		    (setq objectProperty
			   (mapcar
			     '(lambda (d)
				(assoc d objectData)
			      )
			     '(0 8 410)
			   )
		    )
	     )
      )
    )
     (repeat (setq sumOf 0
		   i	 (sslength objectSelection)
	     )
       (setq entName (ssname objectSelection (Setq i (1- i))))
       (setq sumOf (+ sumOf
		      (vlax-curve-getDistAtParam
			entName
			(vlax-curve-getEndParam entName)
		      )
		   )
       )
     )
  )
  (and (> sumOf 0)
       (princ (strcat "\nTotal Length of selected "
		      (cdar objectProperty)
		      "S at Layer "
		      (cdadr objectProperty)
		      ": "
		      (rtos sumOf 2)
	      )
       )
  )
  (princ)
)

HTH

0 Likes
Message 4 of 9

immobiliareA2T2L
Explorer
Explorer

esclusivamente linee

0 Likes
Message 5 of 9

paullimapa
Mentor
Mentor

Perhaps try the many solutions found in this thread

https://www.cadtutor.net/forum/topic/24806-need-lisp-to-calculate-lengths-of-layers/


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 9

komondormrex
Mentor
Mentor

check the following

(defun c:sum_lay_lines (/ lines_assoc_list line_counted)
  (vlax-map-collection (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
    '(lambda (block)
      (if (minusp (vlax-get block 'islayout))
        (vlax-map-collection block
          '(lambda (object)
            (if  (= "AcDbLine" (vla-get-objectname object))
              (progn
                (if (setq line_counted (assoc (vla-get-layer object) lines_assoc_list))
                  (setq lines_assoc_list (subst (cons (car line_counted) (+ (cdr line_counted) (vla-get-length object))) line_counted lines_assoc_list))
                  (setq lines_assoc_list (append lines_assoc_list (list (cons (vla-get-layer object) (vla-get-length object)))))
                )
              )
            )
           )
        )
      )
     )
  )
  (foreach layer lines_assoc_list
    (princ "\n\"") (princ (car layer)) (princ "\" - all lines total length: ") (princ (cdr layer))
  )
  (princ)
)
0 Likes
Message 7 of 9

immobiliareA2T2L
Explorer
Explorer

Come si installa LISP?

0 Likes
Message 8 of 9

paullimapa
Mentor
Mentor

Follow these instructions to learn how to create and load Autolisp files

https://help.autodesk.com/view/ACDLT/2025/ENU/?guid=GUID-3B8EDFF1-A130-434F-B615-7F2EC04322EE


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 9 of 9

Kent1Cooper
Consultant
Consultant

@immobiliareA2T2L wrote:

esclusivamente linee


In that case, the extraction of lengths can be simplified from the way it is done in Messages 2 & 3.  For Line objects specifically, the Parameter anywhere on it is the length to that point.  So it's not necessary to get the distance along it at a Parameter value, and give it the Line's End Parameter.  You can just use the End Parameter directly:

  (setq sumOf
    (+
      sumOf
      (vlax-curve-getEndParam entName)
    ); +
  ); setq

But that doesn't give the right answer for other object types whose Parameter values represent different things, for which the approach in those routines is appropriate.  That also makes it the appropriate way for something to get lengths of all (vlax-curve-...)-type objects including Lines.

Kent Cooper, AIA
0 Likes