TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: PPETROS on September 18, 2019, 03:50:13 AM

Title: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: PPETROS on September 18, 2019, 03:50:13 AM
Dear all,
I am struggling to write a lisp routine which does the followings:
    • Select *Every polyline with a width larger than 0 (zero)
    • Change their color to a specific one, predefined (i.e. 252)
*Every polyline including blocks.(And nested)

The attached routine “Code 1” changes the color of all polyline with width larger that 0 (zero), but not that inside blocks
Code: [Select]
(DEFUN C:P252 ()
(setq S2 (ssget "_X" (list (cons 0 "*POLYLINE")
 (cons -4 ">")
 (cons 40 0.0))) )
(command "Change" S2 "" "p" "c" 252 "")
(princ)
)

Furthermore, the attached routine “Code 2” changes the color (globally) of every polyline, no matter the width. (polyline with width 0)

Code: [Select]
(defun c:PL-ALL-252 ( / doc )
    (vlax-for block (vla-get-blocks (setq doc (vla-get-activedocument (vlax-get-acad-object))))
        (if (eq :vlax-false (vla-get-isxref block))
            (vlax-for obj block
                (if (eq "AcDbPolyline" (vla-get-objectname obj))
                    (vl-catch-all-apply 'vla-put-color (list obj 252))
                )
            )
        )
    )
    (vla-regen doc acallviewports)
    (princ)
)
(vl-load-com) (princ)

I would be grateful if someone can fix my code, or suggest a new one.
Thank you in advance.

Attaching example drawing:

Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: kpblc on September 18, 2019, 06:20:55 AM
I didn't check code:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:pl-all-252 (/ doc)
  2.     (if (eq :vlax-false (vla-get-isxref block))
  3.       (vlax-for obj block
  4.         (if (and (eq "AcDbPolyline" (vla-get-objectname obj))
  5.                  (vlax-property-available-p obj "ConstantWidth")
  6.                  (> (vla-get-constantwidth obj 0.0))
  7.                  ) ;_ end of and
  8.           (vl-catch-all-apply 'vla-put-color (list obj 252))
  9.           ) ;_ end of if
  10.         ) ;_ end of vlax-for
  11.       ) ;_ end of if
  12.     ) ;_ end of vlax-for
  13.   (vla-regen doc acallviewports)
  14.   (princ)
  15.   ) ;_ end of defun
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: PPETROS on September 18, 2019, 06:55:45 AM
thanks for your effort
but something goes wrong
Autocad report the message  “; error: Too many actual parameters”
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: roy_043 on September 18, 2019, 07:11:11 AM
This:
Code: [Select]
(> (vla-get-constantwidth obj 0.0))Should be:
Code: [Select]
(> (vla-get-constantwidth obj) 0.0)
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: PPETROS on September 18, 2019, 07:17:33 AM
thanks
but something goes wrong again
Autocad report the message  “; error: Automation Error. Invalid input”
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: kpblc on September 18, 2019, 09:58:28 AM
I tried to start this code, it worked correctly:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:pl-all-252 (/ doc)
  2.     (if (equal (vla-get-isxref block) :vlax-false)
  3.       (vlax-for obj block
  4.         (if (and (eq "AcDbPolyline" (vla-get-objectname obj))
  5.                  (vlax-property-available-p obj "ConstantWidth")
  6.                  (> (vla-get-constantwidth obj) 0.0)
  7.                  ) ;_ end of and
  8.           (vl-catch-all-apply 'vla-put-color (list obj 252))
  9.           ) ;_ end of if
  10.         ) ;_ end of vlax-for
  11.       ) ;_ end of if
  12.     ) ;_ end of vlax-for
  13.   (vla-regen doc acallviewports)
  14.   (princ)
  15.   ) ;_ end of defun
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: PPETROS on September 18, 2019, 10:08:46 AM
Run your code in my attach dwg file
it does not work there
Autocad report again the message  “; error: Automation Error. Invalid input”
thanks again for your time
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: kpblc on September 18, 2019, 10:38:58 AM
Ah, sorry
Code - Auto/Visual Lisp: [Select]
  1. (defun c:pl-all-252 (/ doc width)
  2.   (vlax-for block (vla-get-blocks doc)
  3.     (if (equal (vla-get-isxref block) :vlax-false)
  4.       (vlax-for obj block
  5.         (if (and (eq "AcDbPolyline" (vla-get-objectname obj))
  6.                  (not (vl-catch-all-error-p
  7.                         (setq width (vl-catch-all-apply (function (lambda () (vla-get-constantwidth obj)))))
  8.                         ) ;_ end of vl-catch-all-error-p
  9.                       ) ;_ end of not
  10.                  (> width 0.0)
  11.                  ) ;_ end of and
  12.           (vl-catch-all-apply 'vla-put-color (list obj 252))
  13.           ) ;_ end of if
  14.         ) ;_ end of vlax-for
  15.       ) ;_ end of if
  16.     ) ;_ end of vlax-for
  17.   (vla-regen doc acallviewports)
  18.   (princ)
  19.   ) ;_ end of defun
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: PPETROS on September 18, 2019, 12:00:03 PM
Very good job my friend.
I would like to ask something more of you.
It is easy to fix your code so that it can also change the polylines with variable width.
Check out my new attach file for more (red text).

Thanks again for your time
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: ronjonp on September 18, 2019, 04:50:46 PM
Try this quick mod:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:pl-all-252 (/ doc width)
  2.   (vlax-for block (vla-get-blocks doc)
  3.     (if (equal (vla-get-isxref block) :vlax-false)
  4.       (vlax-for obj block
  5.         (if (and (eq "AcDbPolyline" (vla-get-objectname obj))
  6.                  (or (vl-catch-all-error-p
  7.                        (setq width (vl-catch-all-apply 'vla-get-constantwidth (list obj)))
  8.                      ) ;_ end of vl-catch-all-error-p
  9.                      (> width 0.0)
  10.                  ) ;_ end of not
  11.             ) ;_ end of and
  12.           (vl-catch-all-apply 'vla-put-color (list obj 252))
  13.         ) ;_ end of if
  14.       ) ;_ end of vlax-for
  15.     ) ;_ end of if
  16.   ) ;_ end of vlax-for
  17.   (vla-regen doc acallviewports)
  18.   (princ)
  19. ) ;_ end of defun
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: PPETROS on September 19, 2019, 03:02:26 AM
Excellent, that is what i wanted.
Thank you very mach
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: roy_043 on September 19, 2019, 03:03:07 AM
It is surprising that using vla-get-constantwidth on an "AcDbPolyline" can cause an error (BricsCAD shows the same behavior).
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: kpblc on September 19, 2019, 05:33:35 AM
That polyline has a different widths at start and at end of some segments.
Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: David Bethel on September 19, 2019, 08:34:07 AM
Because you are using both light and heavy POLYLINEs, you will need to deal with them differently ( no BLOCKS Included )

Code - Auto/Visual Lisp: [Select]
  1.  
  2.  
  3. (defun c:pl-wide (/ ws ss en ed et i vn vd ws)
  4.  
  5.   (setq ws (ssadd))
  6.  
  7.   (and (setq ss (ssget "X" (list (cons -4 "<OR")
  8.                                    (cons 0 "LWPOLYLINE")
  9.                                    (cons -4 "<AND")
  10.                                      (cons 0 "POLYLINE")
  11.                                      (cons -4 "<")
  12.                                        (cons 70 8)
  13.                                    (cons -4 "AND>")
  14.                                  (cons -4 "OR>"))))
  15.         (setq i 0)
  16.         (while (setq en (ssname ss i))
  17.                (setq ed (entget en)
  18.                      et (cdr (assoc 0 ed)))
  19.                (cond ((= et "LWPOLYLINE")
  20.                       (foreach g ed
  21.                         (and (member (car g)'(40 41))
  22.                              (not (zerop (cdr g)))
  23.                              (not (ssmemb en ws))
  24.                              (ssadd en ws))))
  25.                      ((= et "POLYLINE")
  26.                       (setq vn (entnext en)
  27.                             vd (entget vn))
  28.                       (while (= "VERTEX" (cdr (assoc 0 vd)))
  29.                              (and (= 0 (cdr (assoc 70 vd)))
  30.                                   (or (/= 0 (cdr (assoc 40 vd)))
  31.                                       (/= 0 (cdr (assoc 41 vd))))
  32.                                   (not (ssmemb en ws))
  33.                                   (ssadd en ws))
  34.                              (setq vn (entnext vn)
  35.                                    vd (entget vn)))))
  36.                (setq i (1+ i))))
  37.  
  38.   (if (> (sslength ws) 0)
  39.       (command "_.CHPROP" ws "" "_Color" 252 ""))
  40.  
  41.   (prin1))
  42.  



Not tested very thoroughly.  -David

Title: Re: LISP FOR CHANGING EVERY POLYLINE WITH WIDHT, COLOUR TO 252.(INCLUDING BLOCKS)
Post by: roy_043 on September 19, 2019, 03:25:00 PM
That polyline has a different widths at start and at end of some segments.
Yes, I do understand that. I meant that it is strange on a fundamental level: An object has a property, but requesting the value of that property results in an error.