Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Active Contributor
tmblackflag
Posts: 48
Registered: ‎07-20-2011
Message 1 of 7 (1,490 Views)

Change width of all polylines on a specified layer using script?

1490 Views, 6 Replies
08-01-2012 06:31 PM

Hello,

 

I need to be able to grab all polylines and change global width at once. This is a repetitve task that i have created a button for. I am trying to use this but it is not working. It gets hung up on me wanting to manually select the polylines when i thought i grabbed them with the first line.

 

(ssget "X" '((8 . "LayerName")))
(command "PEDIT" "w" "9")

Distinguished Mentor
_Tharwat
Posts: 642
Registered: ‎07-02-2010
Message 2 of 7 (1,481 Views)

Re: Change width of all polylines on a specified layer using script?

08-02-2012 12:09 AM in reply to: tmblackflag

Be careful to have your layer name is unlocked .

 

 

(Defun c:Test  (/ ss)
  (if (setq ss (ssget "_x" '((0 . "*POLYLINE") (8 . "Layername"))))
    (command "_.pedit" "_m" ss "" "w" 9 ""))
  (princ))

 



____________________________________________________
Get learn and learn and practice , to become experienced guy .
*Expert Elite*
Kent1Cooper
Posts: 5,871
Registered: ‎09-13-2004
Message 3 of 7 (1,452 Views)

Re: Change width of all polylines on a specified layer using script?

08-03-2012 01:48 PM in reply to: _Tharwat

_Tharwat wrote:

.... 

(Defun c:Test  (/ ss)
  (if (setq ss (ssget "_x" '((0 . "*POLYLINE") (8 . "Layername"))))
    (command "_.pedit" "_m" ss "" "w" 9 ""))
  (princ))

 


If you ever use 3D Polylines, you'll need to make that more sophisticated.  I find that with a selection of multiple Polylines, if any of them are 3D, the Width option is not offered, and the "w" will result in an invalid-option message, even if there are 2D and/or LW Polylines in the selection that width can be assigned to.

Kent Cooper
Distinguished Mentor
_Tharwat
Posts: 642
Registered: ‎07-02-2010
Message 4 of 7 (1,420 Views)

Re: Change width of all polylines on a specified layer using script?

08-05-2012 01:47 AM in reply to: Kent1Cooper

You are right Kent , 3dpolylines should be wxcluded from the selection set of the 2d polylines , so here it goes .

 

To OP , Don't forget to change the layer name to meet your needs .

 

(Defun c:Test (/ s ss sn i)
  (if (setq s  (ssadd)
        ss (ssget "_x" '((0 . "*POLYLINE") (8 . "LayerName")))
      )
    (progn
      (repeat (setq i (sslength ss))
    (setq sn (ssname ss (setq i (1- i))))
    (if (not (eq (cdr (assoc 100 (reverse (entget sn))))
             "AcDb3dPolyline"
         )
        )
      (ssadd sn s)
    )
      )
      (if (> (sslength s) 0)
    (command "_.pedit" "_m" s "" "w" 9 "")
      )
      (princ "\n .... ")
      (princ (strcat "Number of Polylines changed : "
             " < "
             (itoa (sslength s))
             " > "
         )
      )
    )
    (princ "\n Couldn't find any 2D polyline !!")
  )
  (princ)
)

 

____________________________________________________
Get learn and learn and practice , to become experienced guy .
*Expert Elite*
Kent1Cooper
Posts: 5,871
Registered: ‎09-13-2004
Message 5 of 7 (1,409 Views)

Re: Change width of all polylines on a specified layer using script?

08-05-2012 04:04 AM in reply to: Kent1Cooper

Kent1Cooper wrote:

....

If you ever use 3D Polylines, you'll need to make that more sophisticated.  ... if any of them are 3D, the Width option is not offered, and the "w" will result in an invalid-option message....


Another way to go about it:

 

(foreach
  x
  (mapcar 'cadr ;; entity names in a list
    (ssnamex (ssget "X" '((0 . "*POLYLINE") (8 . "YourLayerName"))))
  )
  (if (/= (substr (cdr (assoc 100 (reverse (entget x)))) 5 1) "3")
    (vla-put-ConstantWidth (vlax-ename->vla-object x) YourWidth)
  )
)

 

to which, of course, you can also add a report on how many it did, as _Tharwat did.

 

I actually tried that hoping that maybe you could not bother to check whether any were 3D -- that maybe it would just ignore those.  But no, it gives an error if you try to "put" that property onto something that doesn't have it.  You could check for (vlax-property-available-p) instead of the (assoc 100) approach, but I think it would take more code, including presumably storing of the vla-object conversion in a variable name, since you would need to use it twice as a vla object [to check whether it has that property, and if so, to put a value on it].

Kent Cooper
Valued Mentor
Ian_Bryant
Posts: 362
Registered: ‎06-02-2005
Message 6 of 7 (1,384 Views)

Re: Change width of all polylines on a specified layer using script?

08-05-2012 06:19 PM in reply to: Kent1Cooper

Hi,

you can use vl-catch-all-apply to ignore 3D polylines e.g.

 

(if (setq ss (ssget "X" '((0 . "*POLYLINE") (8 . "YourLayerName"))))
   (foreach
      x (mapcar 'cadr (ssnamex ss))
     (vl-catch-all-apply
          'vlax-put-property
          (list  (vlax-ename->vla-object x) 'ConstantWidth YourWidth)
     )  
   )
)

 

Ian

 

 

Mentor
devitg
Posts: 1,705
Registered: ‎03-14-2004
Message 7 of 7 (1,375 Views)

Re: Change width of all polylines on a specified layer using script?

08-05-2012 08:09 PM in reply to: Ian_Bryant

A way to exclude  3dpolyline  can be using this filter 

 

 

(if (setq s  (ssadd)
        ss (ssget "_x" '((0 . "*POLYLINE") (8 . "LayerName") ( 100 . "~AcDb3dPolyline"  ))
      )

~ (tilde)
 If it is the first character in the pattern, it matches anything except the pattern

 

 

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.