Author Topic: LayerStates  (Read 11735 times)

0 Members and 1 Guest are viewing this topic.

mpeterson79

  • Guest
Re: LayerStates
« Reply #30 on: February 27, 2008, 05:55:44 PM »
Sooo....

About those layer states.    :-)

Am I thinking about this the wrong way - I mean, this system doesn't seem to be working, what would be wrong with doing it with layer states?

ronjonp

  • Needs a day job
  • Posts: 7529
Re: LayerStates
« Reply #31 on: February 27, 2008, 06:10:50 PM »
I'm out of ideas...sorry.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: LayerStates
« Reply #32 on: February 27, 2008, 06:16:16 PM »
Why not put your layers in your DWT?

Then when you need layers, they are already there.  And or, if they have been purged from the working drawing they are easily re-imported using design center (CTRL+2) to drag the layers back into the working file.
Be your Best


Michael Farrell
http://primeservicesglobal.com/

mpeterson79

  • Guest
Re: LayerStates
« Reply #33 on: February 27, 2008, 06:26:23 PM »
We do initiate all our drawing files with a DWT that contains all our standard layers. Through the process of working with the files, which often inclues purging them, many layers are often lost or changed by users. The lisp routine we have is a quick & easy way to reinstate any missing layers, as well as repair existing ones to the company standard (colors, linetypes, etc). Going through design center to recreate layers is long & cumbersome for the number of times our users perform this function.

Also, the way we have our CUI's setup, I can quickly edit the LISP routine to either alter an existing layer's properties, or create an entirely new layer with all the appropriate properties. The next time a user invokes the LISP routine, the new layer settings are produced in their drawing.
« Last Edit: February 27, 2008, 06:29:43 PM by mpeterson79 »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: LayerStates
« Reply #34 on: February 27, 2008, 06:26:56 PM »
If they are new layers, they will be on and thawed and unlocked because they were not in the drawing when the first piece of code was ran.  If you want the new layers be off or frozen or locked, then you will need to create a new way to create the layers, which wouldn't be that had to do.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: LayerStates
« Reply #35 on: February 27, 2008, 06:30:34 PM »
There are some good routines to create layers, or import them from an other drawing in this thread.

http://www.theswamp.org/index.php?topic=12845.0
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: LayerStates
« Reply #36 on: February 27, 2008, 06:36:49 PM »
We do initiate all our drawing files with a DWT that contains all our standard layers. Through the process of working with the files, which often inclues purging them, many layers are often lost or changed by users. The lisp routine we have is a quick & easy way to reinstate any missing layers, as well as repair existing ones to the company standard (colors, linetypes, etc). Going through design center to recreate layers is long & cumbersome for the number of times our users perform this function.

Also, the way we have our CUI's setup, I can quickly edit the LISP routine to either alter an existing layer's properties, or create an entirely new layer with all the appropriate properties. The next time a user invokes the LISP routine, the new layer settings are produced in their drawing.

From this end it sounds like the solution IS the problem.  Or you wouldn't need to invest in a lisp file to solve it. Perhaps the users should be trained to NOT purge out your layers, and or follow the standards more closely, nor changed so frequently that they aren't really standards at all. IMHO
Be your Best


Michael Farrell
http://primeservicesglobal.com/

mpeterson79

  • Guest
Re: LayerStates
« Reply #37 on: February 27, 2008, 06:53:48 PM »
Arguements as to the 'right way' we create & repair our layers aside, can anyone explain to me how the following code might be used?

Half the credit goes to Se7en.
Code: [Select]
;;; return the LayerStateManager object ready to use
;;; tested on 2004
;;; for earlier versions change the '16' below to '15'
(defun return-lsm (/ db lsm)
  (vl-load-com)
  (setq db (vlax-get-property
             (vla-get-activedocument (vlax-get-acad-object))
             'Database
             )
        )
  (setq lsm
     (vl-catch-all-apply
        'vla-Getinterfaceobject
          (list (vlax-get-object "AutoCAD.Application")
             "AutoCAD.AcadLayerStateManager.16");< or 15
           )
        )
  (if (not (vl-catch-all-error-p lsm))
    (progn (vla-SetDatabase lsm db) lsm)
    )
  )

; Methods supported:
;   Delete (1)
;   Export (2)
;   Import (1)
;   Rename (2)
;   Restore (1)
;   Save (2)
;   SetDatabase (1)

; now do some testing
; save the current state
(if (setq lsm (return-lsm))
  (vlax-invoke-method lsm 'Save "new-state" acLsAll)
  )

; export it
(if lsm
  (vlax-invoke-method lsm 'Export "new-state" "c:/new-state.las")
  )

; change your layers so we can restore them
(if lsm
  (vlax-invoke-method lsm 'Restore "new-state")
  )

(vlax-release-object lsm)

daron

  • Guest
Re: LayerStates
« Reply #38 on: February 28, 2008, 10:18:30 AM »
It depends on what version of acad you're using. I'm testing it on ACA2008 and this line: "AutoCAD.AcadLayerStateManager.16" needs to be "AutoCAD.AcadLayerStateManager.17" in in 2008, possibly 2007. As for using it, open the vlide, select (double click the parenthesis behind the defun to highlight the function) and load the  function using the partial load button on the editor. Then highlight
Code: [Select]
(if (setq lsm (return-lsm))
  (vlax-invoke-method lsm 'Save "new-state" acLsAll)
  )
and hit the inspect button, (the microscope on the toolbar). As well, inspect this:
Code: [Select]
(if lsm
  (vlax-invoke-method lsm 'Export "new-state" "c:/new-state.las")
  )
That will save your layer state. You can now proceed to make changes. After you're done and want to revert to the old layerstate, inspect:
Code: [Select]
(if lsm
  (vlax-invoke-method lsm 'Restore "new-state")
  )
and
Code: [Select]
(vlax-release-object lsm)

ronjonp

  • Needs a day job
  • Posts: 7529
Re: LayerStates
« Reply #39 on: February 28, 2008, 10:50:45 AM »
Maybe getting away from using command to make the layers will fix your problem......give this a try:

Code: [Select]
(defun unlockthawlayers (/)
  (vlax-map-collection
    (vla-get-layers
      (vla-get-activedocument (vlax-get-acad-object))
    )
    '(lambda (x)
       (cond ((eq (vla-get-lock x) :vlax-true)
      (setq lock (cons x lock))
      (vla-put-lock x :vlax-false)
     )
       )
       (cond ((eq (vla-get-freeze x) :vlax-true)
      (setq freeze (cons x freeze))
      (vla-put-freeze x :vlax-false)
     )
       )
     )
  )
  (princ)
)

(defun relockfreezelayers (/)
  (if lock
    (mapcar '(lambda (x)
       (vla-put-lock x :vlax-true)
     )
    lock
    )
  )
  (if freeze
    (mapcar '(lambda (x)
       (vl-catch-all-apply 'vla-put-freeze (list x :vlax-true))
     )
    freeze
    )
  )
  (setq lock nil)
  (setq freeze nil)
  (princ)
)

(defun layercreate (layname color linetype desc / lyr)
  (if (not (tblobjname "ltype" linetype))
    (setq linetype "continuous")
  )
  (if (tblsearch "layer" layname)
    (progn
      (setq lyr (vlax-ename->vla-object (tblobjname "layer" layname)))
      (vla-put-color lyr color)
      (vla-put-linetype lyr linetype)
    )
    (setq lyr (vlax-ename->vla-object
(entmakex
  (list (cons 0 "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 70 0)
(cons 2 layname)
(cons 62 color)
(cons 6 linetype)
  )
)
      )
    )
  )
  (vla-put-description lyr desc)
  (princ)
)


(defun C:MakeLAYER (/ CCL LAYLIST)
  (unlockthawlayers)
  (setvar "cmdecho" 0)
  (setq ccl (getvar "clayer"))
  (setq laylist '(("BASEPLATE"
   3
   "continous"
   "Baseplate Outline and Mounting Holes"
  )
  ("BLDGS"
   1
   "continous"
   "Non-test Buldings (foam)"
  )
  ("BLDGS DATUM"
   51
   "continous"
   "Base elevation datum of non-test buildings"
  )
)
  )
  (foreach lyr laylist
    (layercreate (car lyr) (cadr lyr) (caddr lyr) (cadddr lyr))
    (princ (strcat "\n Layer - " (car lyr) " created..."))
  )
  (setvar "clayer" ccl)
  (relockfreezelayers)
  (princ)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mpeterson79

  • Guest
Re: LayerStates
« Reply #40 on: February 28, 2008, 12:52:46 PM »
First off, thanks again to you guys for helping. I'm really trying to understand what is happening in the code being presented here so I can learn more about LISP & vba.

Ronjonp, one thing I noticed in the code you last posted, was that there wasn't a 'switch' (feel free to correct me if I'm using incorrect terms here) for whether or not the layer you created in the C:makelayer list was set to plot. I thought it would be good practice for me to try & add this in order to gain some more knowledge about how all this works.

Doing some research, I found that the DXF code for the plotting flag was 290. (I found this from HERE). By working my way through your code, I simply added a (cons 290 plotflag) to the list you make in layercreate. Then, down in the laylist for c:makelayer, I added either a 1 or a 0 (assuming the plot flag is a binary switch - I couldn't confirm this) to represent whether I wanted the layer to plot or not.

Then, where you are actually creating each layer in the laylist, I see where you have your car's, cadr's, caddr's and cadddr. And you lost me. I can't seem to find out/figure out how to add the fourth object in the laylist to the layer creation.

Can you give me some hints as to how I might add the fourth (maybe even fifth - lineweights) object type?

Here's where I'm at so far:
Code: [Select]
;-------------------------------------------------------------------------------------------------
(defun unlockthawlayers (/)
  (vlax-map-collection
    (vla-get-layers
      (vla-get-activedocument (vlax-get-acad-object))
    )
    '(lambda (x)
       (cond ((eq (vla-get-lock x) :vlax-true)
      (setq lock (cons x lock))
      (vla-put-lock x :vlax-false)
     )
       )
       (cond ((eq (vla-get-freeze x) :vlax-true)
      (setq freeze (cons x freeze))
      (vla-put-freeze x :vlax-false)
     )
       )
     )
  )
  (princ)
)
;-------------------------------------------------------------------------------------------------
(defun relockfreezelayers (/)
  (if lock
    (mapcar '(lambda (x)
       (vla-put-lock x :vlax-true)
     )
    lock
    )
  )
  (if freeze
    (mapcar '(lambda (x)
       (vl-catch-all-apply 'vla-put-freeze (list x :vlax-true))
     )
    freeze
    )
  )
  (setq lock nil)
  (setq freeze nil)
  (princ)
)
;-------------------------------------------------------------------------------------------------
(defun layercreate (layname color linetype desc / lyr)
  (if (not (tblobjname "ltype" linetype))
    (setq linetype "continuous")
  )
  (if (tblsearch "layer" layname)
    (progn
      (setq lyr (vlax-ename->vla-object (tblobjname "layer" layname)))
      (vla-put-color lyr color)
      (vla-put-linetype lyr linetype)
    )
    (setq lyr (vlax-ename->vla-object
(entmakex
  (list (cons 0 "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 70 0)
(cons 2 layname)
(cons 62 color)
(cons 6 linetype)
(cons 290 plotflag)
  )
)
      )
    )
  )
  (vla-put-description lyr desc)
  (princ)
)
;-------------------------------------------------------------------------------------------------
(defun C:MakeLAYER (/ CCL LAYLIST)
  (unlockthawlayers)
  (setvar "cmdecho" 0)
  (setq ccl (getvar "clayer"))
  (setq laylist '(("TEST LAYER 01"
   3
   "continous"
   "TESTING LAYER 01"
   1
  )
  ("TEST LAYER 02"
   1
   "DASHED"
   "TEST LAYER 02"
   0
  )
  ("TEST LAYER 03"
   51
   "CENTER"
   "Testing layer 03"
   0
  )
)
  )
  (foreach lyr laylist
    (layercreate (car lyr) (cadr lyr) (caddr lyr) (cadddr lyr) (cadadr lyr))
    (princ (strcat "\n Layer - " (car lyr) " created..."))
  )
  (setvar "clayer" ccl)
  (relockfreezelayers)
  (princ)
)
;-------------------------------------------------------------------------------------------------
« Last Edit: February 28, 2008, 12:56:10 PM by mpeterson79 »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: LayerStates
« Reply #41 on: February 28, 2008, 01:38:22 PM »
Have a look at this:

I only made changes to two lines...

You have to add the argument of plot status to the function.
(defun layercreate (layname color linetype desc plotflag / lyr)

Nth may be easier to visualize (0 is the first element, 1 is the second.....)
(layercreate (nth 0 lyr) (nth 1 lyr) (nth 2 lyr) (nth 3 lyr) (nth 4 lyr))

HTH :)

Code: [Select]
;-------------------------------------------------------------------------------------------------
(defun unlockthawlayers (/)
  (vlax-map-collection
    (vla-get-layers
      (vla-get-activedocument (vlax-get-acad-object))
    )
    '(lambda (x)
       (cond ((eq (vla-get-lock x) :vlax-true)
      (setq lock (cons x lock))
      (vla-put-lock x :vlax-false)
     )
       )
       (cond ((eq (vla-get-freeze x) :vlax-true)
      (setq freeze (cons x freeze))
      (vla-put-freeze x :vlax-false)
     )
       )
     )
  )
  (princ)
)
;-------------------------------------------------------------------------------------------------
(defun relockfreezelayers (/)
  (if lock
    (mapcar '(lambda (x)
       (vla-put-lock x :vlax-true)
     )
    lock
    )
  )
  (if freeze
    (mapcar '(lambda (x)
       (vl-catch-all-apply 'vla-put-freeze (list x :vlax-true))
     )
    freeze
    )
  )
  (setq lock nil)
  (setq freeze nil)
  (princ)
)
;-------------------------------------------------------------------------------------------------
(defun layercreate (layname color linetype desc plotflag / lyr)
  (if (not (tblobjname "ltype" linetype))
    (setq linetype "continuous")
  )
  (if (tblsearch "layer" layname)
    (progn
      (setq lyr (vlax-ename->vla-object (tblobjname "layer" layname)))
      (vla-put-color lyr color)
      (vla-put-linetype lyr linetype)
    )
    (setq lyr (vlax-ename->vla-object
(entmakex
  (list (cons 0 "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 70 0)
(cons 2 layname)
(cons 62 color)
(cons 6 linetype)
(cons 290 plotflag)
  )
)
      )
    )
  )
  (vla-put-description lyr desc)
  (princ)
)
;-------------------------------------------------------------------------------------------------
(defun C:MakeLAYER (/ CCL LAYLIST)
  (unlockthawlayers)
  (setvar "cmdecho" 0)
  (setq ccl (getvar "clayer"))
  (setq laylist '(("TEST LAYER 01"
   3
   "continous"
   "TESTING LAYER 01"
   1
  )
  ("TEST LAYER 02"
   1
   "DASHED"
   "TEST LAYER 02"
   0
  )
  ("TEST LAYER 03"
   51
   "CENTER"
   "Testing layer 03"
   0
  )
)
  )
  (foreach lyr laylist
    (layercreate (nth 0 lyr) (nth 1 lyr) (nth 2 lyr) (nth 3 lyr) (nth 4 lyr))
    (princ (strcat "\n Layer - " (car lyr) " created..."))
  )
  (setvar "clayer" ccl)
  (relockfreezelayers)
  (princ)
)
;-------------------------------------------------------------------------------------------------

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mpeterson79

  • Guest
Re: LayerStates
« Reply #42 on: February 28, 2008, 02:06:20 PM »
Ah yes - fantastic! I find the nth argument to be much easier to follow. I really have a hard time with car's, cdr's & such.


Thanks for your help!

ronjonp

  • Needs a day job
  • Posts: 7529
Re: LayerStates
« Reply #43 on: February 28, 2008, 02:28:04 PM »
Glad we got it figured out :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

KewlToyZ

  • Guest
Re: LayerStates
« Reply #44 on: March 03, 2008, 02:53:58 PM »
I have an issue using the following:
Code: [Select]
;==========================================================================================================================================
(command "._-layer" "state" "save" "KTA-Scripted" "color" "" "" "unlock" "*" "thaw" "*" "on" "*" "")  ;===================Save Layer Settings
(prompt "\nLayer State Saved!!!")
;==========================================================================================================================================

;run some layer color settings and routines

;==========================================================================================================================================
(command "._-layer" "state" "restore" "KTA-Scripted" "" "" "")    ;===================Restore Layer Settings
(command "._-layer" "state" "delete" "KTA-Scripted" "" "" "")
(prompt "\nLayer State Restored and Deleted !!!")
;==========================================================================================================================================


The problem is that the Color is a toggle so some drawings toggle it back to Yes instead of No and make the routine pretty much a null.