Editing LISP to Measure group of CENTERLINE

Editing LISP to Measure group of CENTERLINE

joe.heslop
Participant Participant
1,608 Views
15 Replies
Message 1 of 16

Editing LISP to Measure group of CENTERLINE

joe.heslop
Participant
Participant

Hi, I currently have a LISP below that will measure a group of selected lines and return a value of all the line's lengths added together.

 

I have tried to edit the LISP to add the values of a group of CENTERLINEs instead of Lines but I can't get it to work.

 

Does anyone have any suggestions?

 

Thanks.

 

;|

TLEN.LSP - Total LENgth of selected objects
(c) 1998 Tee Square Graphics

|;

(defun C:TLEN (/ ss tl n ent itm obj l)
  (setq ss (ssget)
        tl 0
        n (1- (sslength ss)))
  (while (>= n 0)
    (setq ent (entget (setq itm (ssname ss n)))
          obj (cdr (assoc 0 ent))
          l (cond
              ((= obj "LINE")
                (distance (cdr (assoc 10 ent))(cdr (assoc 11 ent))))
              ((= obj "ARC")
                (* (cdr (assoc 40 ent))
                   (if (minusp (setq l (- (cdr (assoc 51 ent))
                                          (cdr (assoc 50 ent)))))
                     (+ pi pi l) l)))
              ((or (= obj "CIRCLE")(= obj "SPLINE")(= obj "POLYLINE")
                   (= obj "LWPOLYLINE")(= obj "ELLIPSE"))
                (command "_.area" "_o" itm)
                (getvar "perimeter"))
              (T 0))
          tl (+ tl l)
          n (1- n)))
  (alert (strcat "Total length of selected objects is " (rtos tl)))
  (princ)
)
0 Likes
Accepted solutions (2)
1,609 Views
15 Replies
Replies (15)
Message 2 of 16

devitg
Advisor
Advisor

@joe.heslop 

 

There are 2 differents "center lines" ones are LINES and OTHERS are blockreferences 

It is control by DIMCEN variable system  for DIMCENTER  command , draw a inser , and the other es CENTERMARK command  draw lines 

devitg_0-1643133326185.png

 

So please upload your sample dwg , as to be clear what you have 

 

0 Likes
Message 3 of 16

devitg
Advisor
Advisor

@joe.heslop what acad version you use?

 

0 Likes
Message 4 of 16

joe.heslop
Participant
Participant

Hi thanks for the reply.

 

Please see the attached DWG. At the moment the LISP can select all the white lines to the right and tell me what the total length of them all are.

I want the LISP to select all the purple centerlines and tell me the total length of them all.

 

I'm working on Full Autocad Version 2022.

 

Thanks

0 Likes
Message 5 of 16

devitg
Advisor
Advisor
Accepted solution

@joe.heslop give a test , the problem is that the PURPLE center lines are NOT line it are Blockreferences 

;;*//*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*
;; Design by Gabo CALOS DE VIT from CORDOBA ARGENTINA
;;;    Copyleft 1995-2022 by Gabriel Calos De Vit ; DEVITG@GMAIL.COM    
;;

 ; Hecho por  Gabo CALOS DE VIT de CORDOBA ARGENTINA
;;;    Copyleft 1995-2021 por Gabriel Calos De Vit 
;; DEVITG@GMAIL.COM 
 ;
 ; ----------------------------------------------------------------------
 ;                   
 ;            Copyleft 1995-2021 by Gabriel Calos De Vit ; DEVITG@GMAIL.COM
 ;                      Website: 
 ; ----------------------------------------------------------------------
 ; DISCLAIMER:  Gabriel Calos De Vit Disclaims any and all liability for any damages
 ; arising out of the use or operation, or inability to use the software.
 ; FURTHERMORE, User agrees to hold Gabriel Calos De Vit harmless from such claims.
 ; Gabriel Calos De Vit makes no warranty, either expressed or implied, as to the
 ; fitness of this product for a particular purpose.  All materials are
 ; to be considered ‘as-is’, and use of this software should be
 ; considered as AT YOUR OWN RISK.
 ; ----------------------------------------------------------------------





;;;get-all-centerline-style-longitud********************************************;;;



(defun get-all-centerline-style-longitud (/ ACAD-OBJ ADOC CTR-LINE-OBJ-SS CTR-LINE-SS DL DL-XYZ
                                          LINE-LENGHT MODEL PURPLE-CTR-LIN-LENGTH UR UR-XYZ
                                         ) ;_  /
  (VL-LOAD-COM)
  (SETQ ACAD-OBJ (VLAX-GET-ACAD-OBJECT)) ;_ el programa ACAD 
  (SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ)) ;_ el DWG que esta abierto-
  (SETQ MODEL (VLA-GET-MODELSPACE ADOC))


  (setvar 'CLAYER "1")

  (setq ctr-line-ss
         (ssget "_X"
                '((0 . "INSERT")
                  (67 . 0)
                  (410 . "Model")
                  (8 . "1")
                  (6 . "CENTER2")
                 )
         ) ;_  ssget
  ) ;_  setq

  (setq Ctr-line-obj-ss (VLA-GET-ACTIVESELECTIONSET adoc))

  (setq purple-ctr-lin-length 0.0)
  (VLAX-FOR
         Ctr-line-obj
                     Ctr-line-obj-ss
    (VLA-GETBOUNDINGBOX Ctr-line-obj 'dl 'ur)

    (setq dl-xyz (VLAX-SAFEARRAY->LIST dl))
    (setq ur-xyz (VLAX-SAFEARRAY->LIST ur))

    (setq line-lenght (distance dl-xyz ur-xyz))
    (setq purple-ctr-lin-length (+ line-lenght purple-ctr-lin-length))
  ) ;_  VLAX-FOR
  (alert (strcat "\n All purple center-lines sum : " (rtos purple-ctr-lin-length 2 2)))
  purple-ctr-lin-length
) ;end defun get-all-centerline-style-longitud

(defun C:ctr-len ()
  (setq ctr-line-len (get-all-centerline-style-longitud))
  )

(princ (strcat "ctr-len \n at command line  " )) 




;;;;;;;|«Visual LISP© Format Options»
;;;(200 2 1 0 nil "end of " 100 20 2 2 nil nil T nil T)
;;;;*** DO NOT add text below the comment! ***|;

 

0 Likes
Message 6 of 16

joe.heslop
Participant
Participant

Hi @devitg that solution works great with the DWG I sent. I had to change the CLAYER value to "0" to get it to work otherwise it gave me an error.

 

joeheslop_0-1643188325370.png

 

Unfortunately I tried it on a DWG where I would use the LISP practically and it wouldn't work. I have included the DWG to this message. Do you have any ideas why?

 

0 Likes
Message 7 of 16

pbejse
Mentor
Mentor

@joe.heslop wrote:

Unfortunately I tried it on a DWG where I would use the LISP practically and it wouldn't work. I have included the DWG to this message. Do you have any ideas why?



(setvar 'CLAYER "1")  will generate an error as the target layer does not exists.

 

0 Likes
Message 8 of 16

joe.heslop
Participant
Participant

Hi @pbejse thanks for the reply.

 

This makes sense, I have changed the layer and line type and the LISP is now giving me the correct value.

 

This works great but I need the LISP to tell me the total length of the centerlines I have selected, similar to the LISP I initially posted, rather than the length of all centerlines on the layer.

 

Have any ideas?

 

Cheers.

0 Likes
Message 9 of 16

pbejse
Mentor
Mentor
Accepted solution

@joe.heslop wrote:

This works great but I need the LISP to tell me the total length of the centerlines I have selected, similar to the LISP I initially posted, rather than the length of all centerlines on the layer.


Using @devitg  lisp, remove the "X" and (410 . "Model") from this line and you're good to go. [ User select on screen ]

Be mindful thay the ssget line is full of filters and thats the reason why it does not work on the other drawing sample

(setq ctr-line-ss
 (ssget 
        '((0 . "INSERT")
          (67 . 0)
	  (8 . "1");<-- Filter for layer ( so this only works for "1" )
	  (6 . "CENTER2"); <-- Filter for Linetype ( only for "CENTER2" under layer "1")
         )
 ) 
) 

You need to generate a better layer name for the centerlines.

(setq ctr-line-ss
 (ssget 
        '((0 . "INSERT")(67 . 0)
          (8 . "*_CLine*");<-- layer names like "i0x-(ref) bubble_CLine"
    )
 ) 
) 

HTH

 

 

0 Likes
Message 10 of 16

joe.heslop
Participant
Participant

@pbejse Thank you so much, I have taken out that code and tweaked it and it works perfectly, exactly how I need it to work.

 

I am a coding novice so really appreciate the explanation on each line.

 

@devitg  Thank you for the original code, was the basis of what I was after.

0 Likes
Message 11 of 16

devitg
Advisor
Advisor

@joe.heslop , try it . 

;*//*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*
;; Design by Gabo CALOS DE VIT from CORDOBA ARGENTINA
;;;    Copyleft 1995-2022 by Gabriel Calos De Vit ; DEVITG@GMAIL.COM    
;;

 ; Hecho por  Gabo CALOS DE VIT de CORDOBA ARGENTINA
;;;    Copyleft 1995-2022 por Gabriel Calos De Vit 
;; DEVITG@GMAIL.COM 
 ;
 ; ----------------------------------------------------------------------
 ;                   
 ;            Copyleft 1995-2022 by Gabriel Calos De Vit ; DEVITG@GMAIL.COM
 ;                      Website: 
 ; ----------------------------------------------------------------------
 ; DISCLAIMER:  Gabriel Calos De Vit Disclaims any and all liability for any damages
 ; arising out of the use or operation, or inability to use the software.
 ; FURTHERMORE, User agrees to hold Gabriel Calos De Vit harmless from such claims.
 ; Gabriel Calos De Vit makes no warranty, either expressed or implied, as to the
 ; fitness of this product for a particular purpose.  All materials are
 ; to be considered ‘as-is’, and use of this software should be
 ; considered as AT YOUR OWN RISK.
 ; ----------------------------------------------------------------------





;;;get-all-centerline-style-longitud********************************************;;;



(defun get-all-centerline-style-longitud (/ ACAD-OBJ ADOC CTR-LINE-OBJ-SS CTR-LINE-SS DL DL-XYZ
                                          LINE-LENGHT MODEL PURPLE-CTR-LIN-LENGTH UR UR-XYZ
                                         ) ;_  /
  (VL-LOAD-COM)
  (SETQ ACAD-OBJ (VLAX-GET-ACAD-OBJECT)) ;_ el programa ACAD 
  (SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ)) ;_ el DWG que esta abierto-
  (SETQ MODEL (VLA-GET-MODELSPACE ADOC))


  
;;;((0 . "INSERT")
;;;(5 . "383ADC")
;;;(102 . "{ACAD_REACTORS")
;;;(330 . <Entity name: 1e276ed9dd0>)
;;;(330 . <Entity name: 1e276ee09d0>)
;;;(330 . <Entity name: 1e276ee0a00>)
;;;(102 . "}")
;;;(330 . <Entity name: 1d9e618da10>)
;;;(100 . "AcDbEntity")
;;;(67 . 0)
;;;(410 . "Model")
;;;(8 . "i0x-(ref) bubble")
;;;(100 . "AcDbBlockReference")
;;;(2 . "*U7")
;;;(10 -64427.9 -1196.98 0.0)
;;;(41 . 1.0)
;;;(42 . 1.0)
;;;(43 . 1.0)
;;;(50 . 0.0)
;;;(70 . 0)
;;;(71 . 0)
;;;(44 . 0.0)
;;;(45 . 0.0)
;;;(210 0.0 0.0 1.0)
;;;)

  ;(setvar 'CLAYER "1")

  (if (setq ctr-line-ss
         (ssget  
                '((0 . "INSERT")
                  (67 . 0)
                  (410 . "Model")
                  (8 .   "i0x-(ref) bubble") 
                  ;(6 . "CENTER2")
                  ;; I do not know why the new centerline do no hold the dxf (6 . "CENTER2")
                  ;as ai the original dwg 
                 )
         ) ;_  ssget
  ) ;_  setq
(progn
  (setq Ctr-line-obj-ss (VLA-GET-ACTIVESELECTIONSET adoc))

  (setq purple-ctr-lin-length 0.0)
  (VLAX-FOR
         Ctr-line-obj
                     Ctr-line-obj-ss
    (VLA-GETBOUNDINGBOX Ctr-line-obj 'dl 'ur)

    (setq dl-xyz (VLAX-SAFEARRAY->LIST dl))
    (setq ur-xyz (VLAX-SAFEARRAY->LIST ur))

    (setq line-lenght (distance dl-xyz ur-xyz))
    (setq purple-ctr-lin-length (+ line-lenght purple-ctr-lin-length))
  ) ;_  VLAX-FOR
  (alert (strcat "\n All purple center-lines sum : " (rtos purple-ctr-lin-length 2 2)))
  purple-ctr-lin-length
  )
(alert "\n there are no purple center-lines" ) 
   ) 
) ;end defun get-all-centerline-style-longitud


 ;(setq uno (/  purple-ctr-lin-length (vla-get-count Ctr-line-obj-ss )))

(defun C:ctr-len ()
  (setq ctr-line-len (get-all-centerline-style-longitud))
  )

(princ (strcat "ctr-len \n at command line  " )) 
;|«Visual LISP© Format Options»
(100 2 1 2 T " " 100 6 0 0 1 nil T nil T)
;*** DO NOT add text below the comment! ***|;

I have as My personal rule , to work on the ORIGINAL sample.dwg , and never make any GUESS, neither ask to an ORACLE what the OP what he could done or will do . 

 

Other fact that I do not know: is WHY such CERTERLINES are BLOCKREFERENCES. 

 

 

 

 

 

 

0 Likes
Message 12 of 16

Kent1Cooper
Consultant
Consultant

@devitg wrote:

....

Other fact that I do not know: is WHY such CERTERLINES are BLOCKREFERENCES. 


They're not actually "Blocks," though they are in the "INSERT" class of objects, but so are Xrefs, and Minserts, and Superhatches, and Windows Metafiles, and Lights under Architectural Desktop, and even Hatch patterns from way early versions of AutoCAD.

 

They have certain characteristics of Blocks, such as "insertion points" that you can Osnap to [dxf 10 in entity data, but not shown in Properties], but not others, such as scale factors, and "rotation" [dxf 50] always seems to be 0.  They have anonymous Block "names" like dynamic Blocks.  There are reactors involved, locking them into relationship with what they were defined to be the center-line between.

 

I found another way to get the length, if 'ent' is the entity name of the Centerline object, without the need to convert it to a VLA object to get its bounding box:

 

(distance
  (list (getpropertyvalue ent "StartPoint/X") (getpropertyvalue ent "StartPoint/Y"))
  (list (getpropertyvalue ent "EndPoint/X") (getpropertyvalue ent "EndPoint/Y"))
)

 

But interestingly, a Line drawn between the Start and End points extracted in the same way is not on the Centerline, nor even near it nor at the same angle, but it does seem to be the same length [in minimal testing -- maybe it would be on the
Centerline under some circumstances].

Kent Cooper, AIA
Message 13 of 16

devitg
Advisor
Advisor

@Kent1Cooper thanks to clear about centerlines are blockreference 

 

Please,  where I can see about 

(getpropertyvalue ent "StartPoint/X") (getpropertyvalue ent "StartPoint/Y"))

Its use , and arguments to send . "StartPoint/X" and so 

 

 

0 Likes
Message 14 of 16

Kent1Cooper
Consultant
Consultant

I had the "context" in dumping the properties giving me separate coordinates of the start and end points, but in fact properties exist that have them as point coordinate lists, so it can be simpler:

(distance
  (getpropertyvalue ent "StartPoint")
  (getpropertyvalue ent "EndPoint")
)

Read about the (getpropertyvalue) and (setpropertyvalue) and [to see all the properties of an entity] (dumpallproperties) AutoLisp functions.

Kent Cooper, AIA
Message 15 of 16

devitg
Advisor
Advisor

@Kent1Cooper , thanks , I will study . 

 

0 Likes
Message 16 of 16

Sea-Haven
Mentor
Mentor

Just for info as a lot of users now, Bricscad V20 does not support get set property functions. Not sure V22 can someone test. 

(GETPROPERTYVALUE (CAR (ENTSEL)) "StartPoint")
0 Likes