Results 1 to 8 of 8

Thread: Changing Layer Settings to Standards Using Visual LISP

  1. #1
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Changing Layer Settings to Standards Using Visual LISP

    All,

    Happy Wednesday! Here is my new issue. I have written the LISP routine below. The idea is that it will get the layers in the current drawing, check them against a list of standard layers and if the layer is a standard layer the routine will set the color, linetype, plot setting and description from the standard. I can get it to run and set colors, but once I add any of the other settings, it stops working. As you can see, I have commented out the settings I can't get to run.

    Any help is appreciated.

    Code:
    (defun c:standard (/ activedocument eachlayer dwglayername checklayer standardlayers standardlayername layercolor)
                (setq standardlayers
                   (list
                     '("-ANNO-BRNG"
                       132
                       "Continuous"
                       1
                       "Annotation, Bearings and distance labels"
                      )
                     '("G-ANNO-DIMS"       132
                       "Continuous"       1
                       "Annotation, Dimensions"
                      )
                     '("G-ANNO-KEYN"      122
                       "Continuous"      1
                       "Annotation, Keynotes"
                      )
                     '("G-ANNO-LABL"     132
                       "Continuous"     1
                       "Annotation, Labels"
                      )
                     '("G-ANNO-LEGN"
                       132
                       "Continuous"
                       1
                       "Annotation, Legend, symbol keys"
                      )
                     '("G-ANNO-LOGO"
                       132
                       "Continuous"
                       1
                       "Annotation, Company logo"
                      )
                   )
                )
      (vl-load-com)
      (setq activedocument (vla-get-activedocument (vlax-get-Acad-Object)))
      (vlax-for eachlayer (vla-get-layers activedocument)
        (setq dwglayername (vla-get-name eachlayer))
            (foreach checklayer standardlayers
          (setq standardlayername (nth 0 checklayer))
          (setq layercolor (nth 1 checklayer))
          ;(setq layerlinetype (nth 2 checklayer))
          ;(setq layerplotsetting (nth 3 checklayer))
          ;(setq layerdescription (nth 4 checklayer))
          (if (= dwglayername standardlayername)
            (vla-put-color eachlayer layercolor)
            ;(vla-put-linetype eachlayer layerlinetype)
            ;(vla-put-plottable eachlayer layerplotsetting)
            ;(vla-put-description eachlayer layerdescription)
            nil
          )
        )
    )
    )

  2. #2
    Member
    Join Date
    2013-08
    Posts
    23
    Login to Give a bone
    0

    Default Re: Changing Layer Settings to Standards Using Visual LISP

    It looks like to me you're just missing some progn's at your vla-for and foreach statements.

    Try this (worked for me when I did a quick test):


    Code:
      (vlax-for 
        eachlayer 
        (vla-get-layers activedocument)
        (progn
          (setq dwglayername (vla-get-name eachlayer))
          (foreach 
            checklayer 
            standardlayers 
            (progn
              (setq standardlayername (nth 0 checklayer))
              (setq layercolor (nth 1 checklayer))
              (setq layerlinetype (nth 2 checklayer))
              (setq layerplotsetting (nth 3 checklayer))
              (setq layerdescription (nth 4 checklayer))
              (if (= dwglayername standardlayername)
                (progn
                  (vla-put-color eachlayer layercolor)
                  (vla-put-linetype eachlayer layerlinetype)
                  (vla-put-plottable eachlayer layerplotsetting)
                  (vla-put-description eachlayer layerdescription)
                
                )
              )
            )
          )
        )
      )

    You should also check at some point that your linetype's are loaded before you try to set the layer's linetype.

  3. #3
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,106
    Login to Give a bone
    0

    Default Re: Changing Layer Settings to Standards Using Visual LISP

    Do you have a list of layers you are wanting to standardize? Do all of them reside in the drawing?

    Which would be faster?
    1. Cycle through all of the layers in the drawing and for each layer found, cycle through the standard layer list to find a match OR
    2. Cycle through the standard layer list to see if that layer is in the drawing


    For me, I would cycle through the standard layer list to find a match. When a match is found, send it to a sub-routine to do the processing.
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  4. #4
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Re: Changing Layer Settings to Standards Using Visual LISP

    Thanks Opie and Ngwalters! I am almost there. Ng's suggestion worked very well, but now I am trying to implement Opie's recommendations on subroutines. Maybe I am misunderstanding the use of arguments again, but I can get this to run as is and dump layer information. However, when I add the variables and send to my subroutine, no go.

    Any suggestions or brilliant insights?

    Code:
    (defun c:laystand (/ activedocument       dwglayername
    			  standardlayers       standardlayername
    			  layercolor
    			 )
    ;;;;; Subroutine
      (defun layerstandardsub (layername layercolor layerlinetype layerplotsetting layerdescription /)
      (vl-load-com)
      (setq activedocument (vla-get-activedocument (vlax-get-Acad-Object)))
      (setq layertable (vla-get-layers activedocument))
      (setq thelayer (vla-item layertable layername))
      (vla-put-color thelayer layercolor)
        (vla-put-linetype thelayer layerlinetype)
        (vla-put-plottable thelayer layerplotsetting)
        (vla-put-description thelayer layerdescription)
      )
    ;;;End Subroutine
      (setq activedocument(vla-get-activedocument (vlax-get-Acad-Object)))
      (setq	standardlayers
    	 (list
    	   '("Layer1" 1 "Continuous" 1 "Layer 1 Description")
    	   '("Layer2" 2 "Continuous" 1 "Layer 2 Description")
    	   '("Layer3" 3 "Continuous" 1 "Layer 3 Description")
    	   '("Layer4" 4 "Continuous" 1 "Layer 4 Description")
    	   '("Layer5" 5 "Continuous" 0 "Layer 5 Description")
    	  )
      )
      (foreach checklayer standardlayers
        (progn
          (setq standardlayername (nth 0 checklayer))
          (vlax-for	eachlayer (vla-get-layers activedocument)
    	(progn
    	  (setq dwglayername (vla-get-name eachlayer))
    	  (if (= standardlayername dwglayername)
    	    (setq layercolor (nth 1 checklayer))
    	    ;(setq layerlinetype (nth 2 checklayer))
    	    ;(setq layerplotsetting (nth 3 checklayer))
    	    ;(setq layerdescription (nth 4 checklayer))
    					;(vlax-dump-object eachlayer)
    	    ;(layerstandardsub)
    	    nil
    	  )
    	)
          )
        )
      )
    )
    Thanks,

    Jeff

  5. #5
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,106
    Login to Give a bone
    0

    Default Re: Changing Layer Settings to Standards Using Visual LISP

    Jeff, Does this help explain the subroutine concept better?
    Code:
    (defun c:OpieStandardLayers (
    			     ;;_ Any Arguments?
    			     /
    			     ;;_ Localized variables
    			     LayerData
    			     _Doc
    			     )
      ;;_ Define layer standard as a list of data
      ;;_ Eventually, this could be placed as an external
      ;;_   data file, separate from the code
      (setq	LayerData
    	 (list
    	   '("Layer1" 1 "Continuous" 1 "Layer 1 Description")
    	   '("Layer2" 2 "Continuous" 1 "Layer 2 Description")
    	   '("Layer3" 3 "Continuous" 1 "Layer 3 Description")
    	   '("Layer4" 4 "Continuous" 1 "Layer 4 Description")
    	   '("Layer5" 5 "Continuous" 0 "Layer 5 Description")
    	  )
      )
      (setq _Doc (vla-get-activedocument (vlax-get-acad-object)))
    
      (defun SubLayerStandards (DataList _Doc / LayerObject )
        ;;_ DataList consists of
        ;;_   Layer Name
        ;;_   Color
        ;;_   Linetype
        ;;_   Plottable state
        ;;_   Description
        (if	(and
    	  ;;_ Check to see if datalist is a list
    	  (= (type DataList) 'LIST)
    	  ;;_ Search for layer name in drawing
    	  (tblsearch "LAYER" (car DataLIST))
    	  ;;_ Assign layer object to variable
    	  (setq	LayerObject
    		 (vla-get-layer
    		   (vla-get-layers _Doc)
    		   (car DataList)
    		 )
    	  )
    	)
          ;; Map the following temporary function to the following lists
          (mapcar
    	;;_ Temporary function to set properties
    	'
    	 (lambda (x y) (vlax-put-property LayerObject x y))
    	;;_ List of properties to change
    	'
    	 ("Color" "Linetype" "Plottable" "Description")
    	;;_ Data to assign to the properties
    	(cdr DataList)
          )
        )
      )
    
      ;;_ Begin main function
    
      ;;_ Foreach item in the LayerData variable, temporarily assign it to 'n'
      (foreach n LayerData
        ;;_ Call subroutine to process data
        (SubLayerStandards n _Doc)
      )
    
      ;;_ Release object
      (vlax-release-object _Doc)
      ;;_ Return nil to command prompt
      (princ)
    )
    I have not tested this code to verify that it works.
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  6. #6
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Re: Changing Layer Settings to Standards Using Visual LISP

    Opie,

    Yes, I see how moving to a strategy of matching layer information handed off to the subroutine is much more efficient and also how arguments to the subroutine work. You are introducing two functions I've never used before, mapcar and lambda. I will undoubtedly be breaking them, pulling hair out and then reaching my eureka moments. I'll dig into this over the next couple of days as scheduling permits (i.e. in between meetings, regular work and unannounced deadlines )

    As always, thanks Opie!

  7. #7
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Changing Layer Settings to Standards Using Visual LISP

    Quote Originally Posted by jeff.richards594944 View Post
    ... You are introducing two functions I've never used before, mapcar and lambda. I will undoubtedly be breaking them, pulling hair out and then reaching my eureka moments. I'll dig into this over the next couple of days as scheduling permits (i.e. in between meetings, regular work and unannounced deadlines )
    Be sure to add both the FUNCTION, and QUOTE " ' " functions to the required reading list.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  8. #8
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Re: Changing Layer Settings to Standards Using Visual LISP

    Well, here is the next iteration. I really appreciate all of the support and when my brain is "rested", I will try to take on loading the layer settings from a separate file.

    Code:
    ;;;; This routine loads a list of standard layers and settings
    ;;;; then passes each layer to a subroutine and searches the
    ;;;; current drawing for that layer.  If the layer exists, it
    ;;;; changes the settings to the match the standards.
    ;;;; Thanks to Kenny Ramage, Opie, ngwalters and BlackBox.
    
    ;; Subroutine to search the current drawing and set layer settings
    ;; to standard
    (defun SUBROUTINE (n _Doc /)
      (if (and
    	(= (type n) 'LIST)
    	(tblsearch "LAYER" (nth 0 n))
          )
        (progn
          (setq LayerName (nth 0 n))
          (setq DocumentLayers (vla-get-layers _doc))
          (setq LayerObject (vla-item DocumentLayers LayerName))
          (mapcar
    	'
    	 (lambda (x y) (vlax-put-property LayerObject x y))
    	'
    	 ("Color" "Linetype" "Plottable" "Description")
    	(cdr n
    	)
          )
          nil
        )
      )
    )
    
    ;; Initial routine to load standard layer settings,
    ;; then pass them to the subroutine.
    (defun C:MAINCOMMAND (/)
      (setq	LayerData
    	 (list
    	   '("Layer1" 1 "Continuous" 1 "Layer 1 Description") '("Layer2" 2 "Continuous" 1 "Layer 2 Description")
    	   '("Layer3" 3 "Continuous" 1 "Layer 3 Description") '("Layer4" 4 "Continuous" 1 "Layer 4 Description")
    	   '("Layer5" 5 "Continuous" 0 "Layer 5 Description"))
      )
      (setq _Doc (vla-get-activedocument (vlax-get-acad-object)))
      (foreach n LayerData
        (SUBROUTINE n _Doc)
      )
    )
    Peace,

    Jeff

Similar Threads

  1. CM318-2: Enforcing CAD Standards Using Visual LISP
    By Autodesk University in forum CAD Management and IT
    Replies: 0
    Last Post: 2014-11-17, 05:55 AM
  2. changing layer properties with lisp (line type)
    By maomuchmar in forum AutoLISP
    Replies: 7
    Last Post: 2014-06-23, 06:23 AM
  3. Changing Layer Key Overrides with VBA or Lisp
    By jgardner.79905 in forum VBA/COM Interop
    Replies: 5
    Last Post: 2006-12-08, 04:46 PM
  4. Bug? Layer settings keep changing back
    By rad.77676 in forum AutoCAD Civil 3D - General
    Replies: 4
    Last Post: 2005-12-21, 06:20 PM
  5. Lisp for changing layer color in XREF's
    By cadd4la in forum AutoLISP
    Replies: 5
    Last Post: 2005-09-28, 01:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •