Author Topic: help with a lisp: Change Layer  (Read 5599 times)

0 Members and 1 Guest are viewing this topic.

pedroantonio

  • Guest
help with a lisp: Change Layer
« on: October 17, 2019, 02:48:37 AM »
I use this code to check the layers for the viewports in layout. When i have 2 or more layouts i have to run this lisp code for each  one layout. Is any way to check all layouts from any place. From example i will be in model space and run the command and chech all layouts or i will be in one layout and check all layouts

Code - Auto/Visual Lisp: [Select]
  1. (defun c:LV (/ sel1 CLAYER )
  2. ;Switch from Layout Tab To Model Tab before stating LISP
  3.     (setq sel1 (ssget "X" '((0 . "VIEWPORT")))) ; SELECT ALL VIEWPORT
  4.     (setq OLDLAYER (getvar "CLAYER")) ;GET CURRENT LAYER
  5.     (COMMAND "_layer" "_m" "VIEWPORT" "_c" "7""" "") ;CREATE NEW LAYER
  6.     (setvar "CLAYER" OLDLAYER) ; SET ACTIVE LAYER TO PREVIOUS
  7.     (command "CHPROP" sel1 "" ; CHANGE VIEWPORT LAYER TO NEW LAYER
  8.     "LAYER" "VIEWPORT"
  9.     "")
  10.     );END PROGRAM
  11.  
  12.  

Thanks

kpblc

  • Bull Frog
  • Posts: 396
Re: help with a lisp: Change Layer
« Reply #1 on: October 17, 2019, 05:03:23 AM »
Something like this?
Code - Auto/Visual Lisp: [Select]
  1. (defun t3 (/ selset name adoc)
  2.   (if (setq selset (ssget "_X" '((0 . "VIEWPORT"))))
  3.            (if (not (tblobjname "layer" (setq name "ViewPortLayer")))
  4.              (vla-add (vla-get-layers adoc) name)
  5.              ) ;_ end of if
  6.            (foreach vp (mapcar (function vlax-ename->vla-object)
  7.                                ((lambda (/ tab item)
  8.                                   (repeat (setq tab  nil
  9.                                                 item (sslength selset)
  10.                                                 ) ;_ end setq
  11.                                     (setq tab (cons (ssname selset (setq item (1- item))) tab))
  12.                                     ) ;_ end of repeat
  13.                                   ) ;_ end of lambda
  14.                                 )
  15.                                ) ;_ end of mapcar
  16.              (vl-catch-all-apply (function (lambda () (vla-put-layer vp name))))
  17.              ) ;_ end of foreach
  18.            (vla-endundomark adoc)
  19.            ) ;_ end of progn
  20.     ) ;_ end of if
  21.   (princ)
  22.   ) ;_ end of defun
Sorry for my English.

pedroantonio

  • Guest
Re: help with a lisp: Change Layer
« Reply #2 on: October 17, 2019, 06:13:35 AM »
hI  kpbl. Thank you for the help .Work fine

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: help with a lisp: Change Layer
« Reply #3 on: October 17, 2019, 08:28:00 AM »
Although the function is probably only working with a small set of objects, it would be more efficient to iterate over the object set only once, rather than 3 times (once within the repeat loop, once for the mapcar, and finally within the foreach loop). You could also only process viewports which are not already assigned to the target layer (assuming the layer name does not contain wildcards):
Code - Auto/Visual Lisp: [Select]
  1. (defun c:vpl ( / idx lay obj sel )
  2.    
  3.     (setq lay "VIEWPORT") ;; Viewport Layer
  4.  
  5.     (if (setq sel (ssget "_X" (list '(0 . "VIEWPORT") (cons 8 (strcat "~" lay)))))
  6.         (progn
  7.             (if (not (tblsearch "layer" lay))
  8.                 (entmake
  9.                     (list
  10.                        '(000 . "LAYER")
  11.                        '(100 . "AcDbSymbolTableRecord")
  12.                        '(100 . "AcDbLayerTableRecord")
  13.                        '(070 . 0)
  14.                         (cons 2 lay)
  15.                     )
  16.                 )
  17.             )
  18.             (repeat (setq idx (sslength sel))
  19.                 (setq idx (1- idx)
  20.                       obj (vlax-ename->vla-object (ssname sel idx))
  21.                 )
  22.                 (if (vlax-write-enabled-p obj) (vla-put-layer obj lay))
  23.             )
  24.         )
  25.     )
  26.     (princ)
  27. )

pedroantonio

  • Guest
Re: help with a lisp: Change Layer
« Reply #4 on: October 17, 2019, 08:36:53 AM »
Thank you Lee Mac

pedroantonio

  • Guest
Re: help with a lisp: Change Layer
« Reply #5 on: October 17, 2019, 08:45:07 AM »
Lee Mac can you help me to correct other 2 codes.

the first change the the layer to DIMENSIONS and the second change the the layer HATCH

ihave hatch and in the model  and in the paper space

dimmensions i have only in model space

Code - Auto/Visual Lisp: [Select]
  1. ;;;;;;;;;; for DIMENSIONS
  2.  
  3.     (defun c:DLDL (/ sel1 CLAYER )
  4. ;Switch from Layout Tab To Model Tab before stating LISP
  5.     (command "_.Tilemode" 1) ; To model space
  6.     (setq sel1 (ssget "X" '((0 . "DIMENSION")))) ; SELECT ALL DIMENSION
  7.     (setq OLDLAYER (getvar "CLAYER")) ;GET CURRENT LAYER
  8.     (COMMAND "_layer" "_m" "DIM" "_c" "10""" "") ;CREATE NEW LAYER
  9.     (setvar "CLAYER" OLDLAYER) ; SET ACTIVE LAYER TO PREVIOUS
  10.     (command "CHPROP" sel1 "" ; CHANGE DIMENSION LAYER TO NEW LAYER
  11.     "LAYER" "DIM"
  12.     "")
  13.    (command "_.Tilemode" 0) ;And back to last current layout
  14.     );END PROGRAM
  15.  
  16.  
  17. ;;;;;;;;;; for HATCH
  18.  
  19.     (defun c:DLHATCH (/ sel1 CLAYER )
  20. ;Switch from Layout Tab To Model Tab before stating LISP
  21.     (command "_.Tilemode" 1) ; To model space
  22.     (setq sel1 (ssget "X" '((0 . "HATCH")))) ; SELECT ALL HATCH
  23.     (setq OLDLAYER (getvar "CLAYER")) ;GET CURRENT LAYER
  24.     (COMMAND "_layer" "_m" "HATCH" "_c" "155" "" "_lw" "0.18" "" "") ;CREATE NEW LAYER
  25.     (setvar "CLAYER" OLDLAYER) ; SET ACTIVE LAYER TO PREVIOUS
  26.     (command "CHPROP" sel1 "" ; CHANGE HATCH LAYER TO NEW LAYER
  27.     "LAYER" "HATCH"
  28.     "")
  29.    (command "_.Tilemode" 0) ;And back to last current layout
  30.  
  31.     (setq sel1 (ssget "X" '((0 . "HATCH")))) ; SELECT ALL HATCH
  32.     (setq OLDLAYER (getvar "CLAYER")) ;GET CURRENT LAYER
  33.     (COMMAND "_layer" "_m" "HATCH" "_c" "155" "" "_lw" "0.18" "" "") ;CREATE NEW LAYER
  34.     (setvar "CLAYER" OLDLAYER) ; SET ACTIVE LAYER TO PREVIOUS
  35.     (command "CHPROP" sel1 "" ; CHANGE HATCH LAYER TO NEW LAYER
  36.     "LAYER" "HATCH"
  37.     "")
  38.     );END PROGRAM
  39.  
  40.  
  41.  

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help with a lisp: Change Layer
« Reply #6 on: October 17, 2019, 09:07:09 AM »
I haven't had my first coffee yet but perhaps this approach ...

Code: [Select]
(defun _change_layer ( type-pattern layer-name / ss i )
    (if (setq ss (ssget "_X" (list (cons 0 type-pattern) (cons 8 (strcat "~" layer-name)))))
        (progn
            (vl-load-com)
            (vl-catch-all-apply 'vla-add (list (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) layer-name))
            (repeat (setq i (sslength ss))
                (vl-catch-all-apply 'vlax-put (list (vlax-ename->vla-object (ssname ss (setq i (1- i)))) 'layer layer-name))
            )
            (sslength ss)
        )
    )
)

Then ...

(defun c:LV ( )
    (_change_layer "VIEWPORT" "VIEWPORT")       
    (princ)
)

(defun c:DLDL  ( )
    ;; may need to wildcard the type-pattern       
    (_change_layer "DIMENSION" "DIM")
    (princ)
)

(defun c:DLHATCH ( )
    (_change_layer "HATCH" "HATCH")       
    (princ)
)


Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

pedroantonio

  • Guest
Re: help with a lisp: Change Layer
« Reply #7 on: October 17, 2019, 06:41:58 PM »
I want to add an extra filter to this codes/

for dimensions code

if the drawind has no dimension then don;t creare the layes and print text (no dimensions found ....)

for hatch code

if the drawind has no hatch then don;t creare the layes and print text (no hatch found ....)

thanks

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help with a lisp: Change Layer
« Reply #8 on: October 17, 2019, 06:59:14 PM »
Pretty simple:

Code: [Select]
(defun _change_layer ( type-pattern layer-name / ss i )
    (if (setq ss (ssget "_X" (list (cons 0 type-pattern) (cons 8 (strcat "~" layer-name)))))
        (progn
            (vl-load-com)
            (vl-catch-all-apply 'vla-add (list (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) layer-name))
            (repeat (setq i (sslength ss))
                (vl-catch-all-apply 'vlax-put (list (vlax-ename->vla-object (ssname ss (setq i (1- i)))) 'layer layer-name))
            )
            (sslength ss)
        )
        (progn (princ (strcat "\nNo " type-pattern " entities found.")) nil)
    )
)

Other defuns ok as-is.

Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: help with a lisp: Change Layer
« Reply #9 on: October 17, 2019, 10:55:58 PM »
I would look at a library routine to check if layer exist if not make you can add the color ltpype etc as well for me its part of my custom lisp loaded at startup

Code: [Select]
(defun lay_miss (lay col lt / )
(if (tblsearch "LAYER" lay)
(prink "OK")
(command "-layer" "m"  lay "c" col "" "Lt" lt "" "")
)
)
; (lay_miss "Newlay" 5 "Dashed")

A man who never made a mistake never made anything

pedroantonio

  • Guest
Re: help with a lisp: Change Layer
« Reply #10 on: October 18, 2019, 03:07:13 AM »
H Bigal.

I know this methond to search i the layer exist. Is any way to search  if any Hatch or dimension exist in the drawing before create the layer. With your way if is not a layer DIMSION and is not adimension in the drawing the lisp creates al layer Dimension anyway . I dont want that. I want if dimensions does not exist in the drawing just pass and dont create a layer


Thanks

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: help with a lisp: Change Layer
« Reply #11 on: October 19, 2019, 12:53:09 AM »
Hi, what does (setq ss (ssget "x" (list (cons 0 "Dimension")))) reveal.
A man who never made a mistake never made anything

pedroantonio

  • Guest
Re: help with a lisp: Change Layer
« Reply #12 on: October 19, 2019, 03:03:56 AM »
Search for dimensions  in the drawing and select them

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: help with a lisp: Change Layer
« Reply #13 on: October 19, 2019, 06:23:50 PM »
Yeah but what does ss is nil mean ? Its the answer to your question.
A man who never made a mistake never made anything