change one layer in all the layer states

Discussion in 'AutoCAD' started by alex, Feb 11, 2005.

  1. alex

    alex Guest

    say you have several layer states in your dwg and you want to change one
    layers properties in all the states (or maybe selected states).

    i would like to write a routine where i pick a layer and it gets the color,
    linetpe, and lineweight after it was altered and then a list of all the
    states are printed (eg.)

    1. state1
    2. state 2
    3. state 3
    4. state 4
    5. state 5

    1. all states 2. selected states
    state1 (y/n)
    state2 (y/n)

    etc... etc...

    I am not sure if there is a tblsearch or maybe a vlax function that could be
    best used to accomplish this.
    Any tips before i get started would be appreciated. If successful i will
    post it here when done.

    Thanks, Alex
    alex, Feb 11, 2005
    1. Advertisements

  2. alex

    James Buzbee Guest

    First, Look into the LayerState Object in the VBA help for properties and
    methods that can be used with vlax-get, vlax-put, vlax-invoke.

    Then, write a routine that has the layer name and layer properties saved in
    varibles. Iterate through all the layer states and process in this order:
    Restore Layer State - Restore Layer Properties - Delete Layer State - Save
    Layer State.

    James Buzbee, Feb 11, 2005
    1. Advertisements

  3. alex

    alex Guest


    I use this quite a bit ofter with a number a letter as a layer state name to
    quicly change displays on the fly. But if i alter a layer and pull back
    another state i lose the settings. maybe i will make an update all routine
    all where all layer states will be compared to current layer settings - for
    cases where different settings are not warranted.

    ; q.lsp sets layer on/off snapshots (not freeze and thaw) for work in a
    ; Sq records a snapshot in var set x)
    ; q restores a snapshot from var set x)

    (defun c:Sq ()
    (setq ls (getstring "Enter layer state name to save: "))
    (command "layer" "a" "d" ls "" "" "")
    (command "layer" "a" "s" ls "" "" "")

    (defun c:q ()
    (setq ls (getstring "Enter layer state name to restore: "))
    (command "layer" "a" "r" ls "" "")
    alex, Feb 11, 2005
  4. alex

    T.Willey Guest

    How would you you word it so that you could save the certain properties you wanted back to the layer state? Here is how I have tried it, without success (A2K4):

    Command: (setq LayStObj (vla-GetInterfaceObject (vlax-get-Acad-Object) "AutoCAD.AcadLayerStateManager.16"))
    #<VLA-OBJECT IAcadLayerStateManager 030d9454>

    Command: (vla-save LayStObj "Test" '("acColor"))
    ; error: lisp value has no coercion to VARIANT with this type: ("acColor")

    Command: (vlax-invoke LayStObj 'save "Test" '("acColor"))
    ; error: argument type mismatch: ("acColor")

    Command: (vlax-invoke LayStObj 'save "Test" "acColor")
    ; error: argument type mismatch: "acColor"

    Command: (vlax-invoke LayStObj 'save "Test" '("acLsColor"))
    ; error: argument type mismatch: ("acLsColor")

    Command: (vlax-invoke LayStObj 'save "Test" "acLsColor")
    ; error: argument type mismatch: "acLsColor"

    Command: (vlax-invoke LayStObj 'save "Test" acLsColor)
    ; error: AutoCAD.Application: Not initialized yet

    Command: (vlax-invoke LayStObj 'save "Test" 'acLsColor)
    ; error: AutoCAD.Application: Not initialized yet

    Thanks for any help.
    T.Willey, Feb 11, 2005
  5. alex

    ECCAD Guest

    This is what I use:

    ;; Function to grab an item 'name' from a collection
    (defun layexist (collection item / rslt)
    (setq rslt
    (vl-catch-all-apply 'vla-item
    (list collection item)
    );trap error
    );end setq
    );return T if successful, else nil
    );end not
    rslt; return object or nil
    );end if
    );end defun

    ;; Function to obtain Layer Status for Layer Chosen
    (defun get_layer_status ( layer_n / ado lyo lay )
    (setq ado (vla-get-ActiveDocument (vlax-get-acad-object)));get pointer to activeDocument
    (setq lyo (vla-get-layers ado))
    (if layer_n
    (setq lay (layexist5 lyo layer_n))
    (if lay
    (setq l_on 1
    l_freeze 0
    l_locked 0
    l_color 255
    l_linetype "Continuous"
    l_plotstylename "Normal"
    l_lineweight "Default"
    ); setq defaults
    (if (= (vla-get-LayerOn lay) :vlax-false);is layer off?
    (setq l_on 0)
    );end if
    (if (= (vla-get-Freeze lay) :vlax-true);is layer frozen?
    (setq l_freeze 1)
    );end if
    (if (= (vla-get-Lock lay) :vlax-true);is layer locked?
    (setq l_locked 1)
    );end if
    (setq l_color (eval (vlax-get-property lay 'Color)))
    (setq l_lineType (eval (vlax-get-property lay 'Linetype)))
    (setq l_lineweight (eval (vlax-get-property lay 'Lineweight)))
    (setq l_plotstylename (eval (vlax-get-property lay 'PlotStyleName)))
    ); progn
    ); if
    ); progn
    ); if
    (if ado (vlax-release-object ado))
    (if lyo (vlax-release-object lyo))
    (if lay (vlax-release-object lay))
    ); end function get_layer_status

    ECCAD, Feb 11, 2005
  6. alex

    Joe Burke Guest

    Hi Tim,

    Your last example was correct. Use a quoted symbol/constant. You can string them
    Notice the error. There's another step involved, shown below. Use the SetDatabase
    method to associate the document database with the LayerStateManager object. See help
    for more info.

    (setq *acad* (vlax-get-Acad-Object))
    (setq *doc* (vla-get-ActiveDocument *acad*))
    (setq LayStObj (vla-GetInterfaceObject *acad* "AutoCAD.AcadLayerStateManager.16"))
    (vla-setdatabase LayStObj (vla-get-database *doc*))) ;added
    ;save color and on/off status
    (vlax-invoke LayStObj 'Save "Test" 'acLsColoracLsOn)
    Joe Burke
    Joe Burke, Feb 12, 2005
  7. alex

    Joe Burke Guest

    Typo: extra right paren at the added line.

    Joe Burke, Feb 12, 2005
  8. alex

    T.Willey Guest

    Hey Joe,

    Thanks again. I looked in the help before I posted, and saw the "SetDatabse" thing, but didn't know where to use it. I also noticed that if you have a layer state by the same name it won't work, so you have to delete that one then save it.

    T.Willey, Feb 14, 2005
  9. alex

    T.Willey Guest

    New problem.

    I want to save a layer state before my routine changes anything, that is no problem. The problem is that I can't save a layer state with all the properties saved. This is my attempt

    (setq LayStObj (vla-GetInterfaceObject (vlax-get-Acad-Object) "AutoCAD.AcadLayerStateManager.16"))
    (vla-SetDatabase LayStObj (vla-get-Database ActDoc))
    (vlax-invoke LayStObj 'Save "---TempLayerState---" 'acLsAll)

    It doesn't seem to work. Anyone know how to save it?

    Ok... so I have two problems.

    I get my string on what properties to save, but I can't seem to pass the string to the LayStObj. Here is how I am attempting to do it.
    (vlax-invoke LayStObj 'Save State (read (cdr RtnOptions)))

    Where RtnOptions is formatted like.
    (:)vlax-false :vlax-false :vlax-false :vlax-false :vlax-true) .

    If you don't think this is enough info, and the lisp would help, I could post that also.

    Thanks in advance.
    T.Willey, Feb 14, 2005
  10. alex

    James Allen Guest

    I didn't look carefully, but I did notice 'acLsAll. I think it should just
    be acLsAll ( no ' ).
    James Allen, EIT
    Malicoat-Winslow Engineers, P.C.
    Columbia, MO

    no problem. The problem is that I can't save a layer state with all the
    properties saved. This is my attempt
    string to the LayStObj. Here is how I am attempting to do it.
    James Allen, Feb 15, 2005
  11. alex

    Joe Burke Guest

    Hi Tim,

    Sorry, seems I steered you wrong. I didn't look past the fact a layer state was
    created with the correct name.

    James is right. If you want to specify a single constant it should not be quoted.
    Both of these work for me and they do the same thing.

    (vlax-invoke LayStObj 'Save "---TempLayerState---" acLsAll)
    (vlax-invoke LayStObj 'Save "---TempLayerState---" 65535)

    Given your other example: color, frozen and on:

    (vlax-invoke LayStObj 'Save "---TempLayerState---" (+ 32 2 1))

    See help under the Mask property of the LayerStateManager object for the values of
    the various constants.

    Joe Burke
    Joe Burke, Feb 15, 2005
  12. alex

    Joe Burke Guest


    Here's some functions by Tony which deal with layer states. I haven't used them. Just
    something I stashed away. Either useful as is, or maybe to provide some insight into
    the questions you asked. I tried a few of them. They worked as expected with 2004.

    Formatting isn't what it should be. They were posted by someone other than Tony.

    Hope this compensates for my previous mis-information. :)

    Joe Burke

    REQUIREMENTS: AutoCAD ver 2000+
    OBJECT:   Save and Restore Layer States compatible with AutoCAD's Layer
    SUBR's:   (lsave name bit)
    (lrestore name)
    (ldelete name)
    Layer Bit Settings                     Bit Value
    All layer properties================== 65535
    Color================================= 32
    Frozen or thawed====================== 2
    Linetype============================== 64
    Lineweight============================ 128
    Locked or unlocked==================== 4
    New viewport layers frozen or thawed== 16
    None================================== 0
    On or off============================= 1
    Plotting on or off==================== 8
    Plot style============================ 256
    ;;;Author: Tony Tanzillo
    (defun LayerGetObject (ProgId / Result)
    ;; First try using explicit version-dependent progid:
    (setq result
    (strcat ProgId "." (substr (getvar "acadver") 1 2))
    ;; Else try the version-independent progid:
    (if (not result)
    (setq result
    ) ;end
    (setq *lsman* (LayerGetObject "autocad.acadlayerstatemanager"))
    (vla-setdatabase *lsman* (vla-get-database (vla-get-activedocument
    (defun lsave (nam bit)
    (setq nam (strcase nam))
    (if (member nam (getstatenames))
    (vl-catch-all-apply 'vla-delete (list *lsman* nam))
    (vl-catch-all-apply 'vla-save (list *lsman* nam bit))
    (prompt (strcat "\n" nam " Saved"))
    (defun lrestore (nam)
    (if (member (setq nam (strcase nam)) (getstatenames))
    (progn (vl-catch-all-apply 'vla-restore (list *lsman* nam))
    (vl-cmdf "_.regenall")
    (prompt (strcat "\n" nam " Restored"))
    (defun ldelete (nam)
    (if (member (setq nam (strcase nam)) (getstatenames))
    (progn (vl-catch-all-apply 'vla-delete (list *lsman* nam))
    (prompt (strcat "\n" nam " Deleted"))
    ;;;Utility Return Saved States
    (defun getstatenames (/ nams dict obj)
    (setq dict (vla-getextensiondictionary
    (vla-get-activedocument (vlax-get-acad-object))
    (if (not
    (setq obj (vl-catch-all-apply
    (function (lambda () (vla-item dict "ACAD_LAYERSTATES")))
    (vlax-for n obj
    (setq nams (cons (strcase (vla-get-name n)) nams))
    (if nams
    (acad_strlsort nams)
    ) ;end
    Joe Burke, Feb 15, 2005
  13. alex

    Joe Burke Guest

    Hi Tim,

    You're welcome. Glad we finally got it straight... learning as we go.

    I loaded what you posted. Looks pretty slick. I haven't beat on it yet, but I will
    when I have some time.

    Joe Burke
    Joe Burke, Feb 16, 2005
  14. alex

    T.Willey Guest

    Thanks Joe. Hope it holds up to all the weird testing you will do to it.

    T.Willey, Feb 16, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.