Author Topic: visual lisp qs - layer  (Read 2257 times)

0 Members and 1 Guest are viewing this topic.

rhino

  • Guest
visual lisp qs - layer
« on: April 07, 2009, 12:45:11 PM »
Hellos,

I,ve come up with this code to set up layers in a drawing for a routine that draws a layout plan.

Code: [Select]
(defun lay_man2 (/ acadobject lay_tbl lay_list clr_list)
  (vl-load-com)
  (setvar "expert" 3)
  (command "_-linetype" "load" "center" ""
   "load" "hidden" "" ""
)
  (princ)
    (setq activedoc  (vla-get-activedocument (vlax-get-acad-object))
  lay_tbl    (vla-get-layers activedoc)
  lay_list  '("BRACING" "CL" "GRID" "TEXT" "STEEL" "PPA" "HATCH" "WALL")
  clr_list  '(("BRACING" . 32) ("CL" . 8) ("GRID" . 9) ("TEXT" . 4) ("STEEL" . 3) ("PPA" . 2) ("HATCH" . 8) ("WALL" . 8))
)
  (foreach n lay_list (vla-add lay_tbl n));vlax-for does not work here for some reason :|

  (vlax-for n lay_tbl  (vla-put-LineWeight n 0));foreach wont work here :D
 
  (vla-put-color (vla-item lay_tbl "BRACING") (cdr (assoc "BRACING" clr_list)))
  (vla-put-color (vla-item lay_tbl "CL") (cdr (assoc "CL" clr_list)))
  (vla-put-color (vla-item lay_tbl "GRID") (cdr (assoc "GRID" clr_list)))
  (vla-put-color (vla-item lay_tbl "TEXT") (cdr (assoc "TEXT" clr_list)))
  (vla-put-color (vla-item lay_tbl "STEEL") (cdr (assoc "STEEL" clr_list)))
  (vla-put-color (vla-item lay_tbl "PPA") (cdr (assoc "PPA" clr_list)))
  (vla-put-color (vla-item lay_tbl "HATCH") (cdr (assoc "HATCH" clr_list)))
  (vla-put-color (vla-item lay_tbl "WALL") (cdr (assoc "WALL" clr_list)))
  (vla-put-linetype (vla-item lay_tbl "GRID") "CENTER")
  (vla-put-linetype (vla-item lay_tbl "BRACING") "HIDDEN")
 
  (setvar "expert" 0)
); end lay_man 2 - using Visual Lisp Code


question:

Is it possible to use the association list to set the colours of each layer in the layer table with just 1 or 2 calls to 'vla-put-color' bla bla? :|

cheers!

ronjonp

  • Needs a day job
  • Posts: 7531
Re: visual lisp qs - layer
« Reply #1 on: April 07, 2009, 01:10:53 PM »
Try something like this:

Code: [Select]
(defun lay_man2 (/ lay_list lay_tbl vlay)
  (vl-load-com)
  (setvar "expert" 3)
  (command "_-linetype" "load" "center" "" "load" "hidden" "" "")
  (princ)
  (setq lay_tbl  (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        lay_list '(("BRACING" . 32)
                   ("CL" . 8)
                   ("GRID" . 9)
                   ("TEXT" . 4)
                   ("STEEL" . 3)
                   ("PPA" . 2)
                   ("HATCH" . 8)
                   ("WALL" . 8)
                  )
  )
  (foreach n lay_list
    (if (setq vlay (vla-add lay_tbl (car n)))
      (vla-put-color vlay (cdr n))
    )
  )
  (setvar "expert" 0)
) ; end lay_man 2 - using Visual Lisp Code

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

rhino

  • Guest
Re: visual lisp qs - layer
« Reply #2 on: April 07, 2009, 01:33:01 PM »
Try something like this:

Code: [Select]
(defun lay_man2 (/ lay_list lay_tbl vlay)
  (vl-load-com)
  (setvar "expert" 3)
  (command "_-linetype" "load" "center" "" "load" "hidden" "" "")
  (princ)
  (setq lay_tbl  (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        lay_list '(("BRACING" . 32)
                   ("CL" . 8)
                   ("GRID" . 9)
                   ("TEXT" . 4)
                   ("STEEL" . 3)
                   ("PPA" . 2)
                   ("HATCH" . 8)
                   ("WALL" . 8)
                  )
  )
  (foreach n lay_list
    (if (setq vlay (vla-add lay_tbl (car n)))
      (vla-put-color vlay (cdr n))
    )
  )
  (setvar "expert" 0)
) ; end lay_man 2 - using Visual Lisp Code

Thank you Ron, just what I wanted to achieve!

Cheers!

ronjonp

  • Needs a day job
  • Posts: 7531
Re: visual lisp qs - layer
« Reply #3 on: April 07, 2009, 01:36:14 PM »
Glad to help  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: visual lisp qs - layer
« Reply #4 on: April 08, 2009, 09:12:48 AM »
this should eliminate the need to change the expert variable (quick & dirty, not tested).
Code: [Select]
(command "_.linetype")
(foreach x (list "Center" "Hidden")
 (if (tblsearch "ltype" x)
  (command "_load" x "_y" "")
  (command "_load" x "")
 )
)
(command "")
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

rhino

  • Guest
Re: visual lisp qs - layer
« Reply #5 on: April 09, 2009, 12:35:01 PM »
this should eliminate the need to change the expert variable (quick & dirty, not tested).

  (command "_load" x "")

thanks alan - was oblivious to the 'load' command  :ugly:

cheers!

cyberiq

  • Guest
Re: visual lisp qs - layer
« Reply #6 on: April 10, 2009, 06:00:21 PM »
Hi,

Maybe it's a little late, but here is my take on this thread. I've put together something a little bit more flexible and I think that this approach is also easier to further develop (like adding some other stuff to these layers), if the same approach on structuring the data and to apply it to the objects is maintained. Hope it's useful:

Code: [Select]
;;;=============================================================
(defun mylayman (ltypelist laydatalist / activedoc ltypes layer)

  (setq activedoc
    (vla-get-activedocument
      (vlax-get-acad-object)
    )
  )

  (setq ltypes (vla-get-linetypes activedoc))

  (defun ltypeisloaded (ltypename)
    (not
      (vl-catch-all-error-p
        (vl-catch-all-apply
          'vla-item
          (list ltypes ltypename)
        )
      )
    )
  )

  (defun loadltype (ltypename)
    (not
      (vl-catch-all-error-p
        (vl-catch-all-apply
          'vla-load
          (list
            ltypes
            ltypename
            (if (= (getvar 'measurement) 0)
              "acad.lin"
              "acadiso.lin"
            )
          )
        )
      )
    )
  )
 
  (mapcar
    '(lambda (x)
      (if (not (ltypeisloaded x))
        (if (not (loadltype x))
          (prompt
            (strcat
              "\nThe linetype "
              (strcase x)
              " could not be loaded."
            )
          )
        )
      )
    )
    ltypelist
  )

  (mapcar
   '(lambda (x)
      (if
        (not
          (vl-catch-all-error-p
            (setq layer
              (vl-catch-all-apply
                'vla-add
                (list
                  (vla-get-layers activedoc)
                  (car x)
                )
              )
            )
          )
        )
        (vla-put-color layer (cdr x))
        (prompt
          (strcat
            "\nThe layer "
            (strcase (car x))
            " could not be found."
          )
        )
      )
    )
    laydatalist
  )
  (princ)
)
;;;=============================================================
;;;Usage:
;;;=============================================================
(mylayman
  '("Center" "Hidden")
  '(("BRACING" . 32)("CL" . 8)("GRID" . 9)("TEXT" . 4)("STEEL" . 3)("PPA" . 2)("HATCH" . 8)("WALL" . 8))
)
;;;=============================================================

rhino

  • Guest
Re: visual lisp qs - layer
« Reply #7 on: April 11, 2009, 12:20:00 AM »
Thanks Constantin - learnt quite a bit from your code...

cheers!  :kewl:

cyberiq

  • Guest
Re: visual lisp qs - layer
« Reply #8 on: April 11, 2009, 01:43:57 PM »
Thanks Constantin - learnt quite a bit from your code...

You're welcome. I forgot to mention the fact that if you don't want the two function to be exposed outside of your main routine you could declare them as local:

(defun mylayman (ltypelist laydatalist / activedoc ltypeisloaded loadltype ltypes layer)

and of course, if you want to have them available for any other function, you could move them to your custom utilities and tools file. Hope I made my self clear.

Regards,

horsey :)