See the top rated post in this thread. Click here

Results 1 to 9 of 9

Thread: Creating a Lisp that makes a new layer the same as a the drawing name

  1. #1
    Member
    Join Date
    2020-07
    Posts
    7
    Login to Give a bone
    0

    Default Creating a Lisp that makes a new layer the same as a the drawing name

    I'm trying to create a .lsp to enable me to create a layer with the same name as the drawing title, below is what I have so far but it does not seem to work.

    any help would be appreciated.

    (setq _OldLayer (getvar "CLAYER")) ;get the current layer first
    (setq _NewLayer (getvar "DWGNAME"))

    (command "-layer" "m" DWGNAME "p" "n" DWGNAME "c" "White" DWGNAME "exit" "") ;create layer and set colour

  2. #2
    Administrator rkmcswain's Avatar
    Join Date
    2004-09
    Location
    Earth
    Posts
    9,803
    Login to Give a bone
    0

    Default Re: Creating a Lisp that makes a new layer the same as a the drawing name

    You are setting a variable named "_NewLayer", but in your command statement you are plugging in a variable named "DWGNAME", which is probably nil.
    R.K. McSwain | CAD Panacea |

  3. #3
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,658
    Login to Give a bone
    0

    Default Re: Creating a Lisp that makes a new layer the same as a the drawing name

    Try
    Code:
    (setq _NewLayer (vl-filename-base (getvar "DWGNAME")))
    (command "-layer" "m" _NewLayer "p" "n" _NewLayer "c" "White" _NewLayer "")

  4. #4
    Member
    Join Date
    2020-07
    Posts
    7
    Login to Give a bone
    0

    Default Re: Creating a Lisp that makes a new layer the same as a the drawing name

    Thanks Tom.

    This works however I want to set the layer back to the one before the command is invoked please see below code.

    Code:
    (defun c:vpoa ( / *error* idx sel )
    (setq 1 oldlayer (getvar "CLAYER")) ;get the current layer first
    (setq 2 _NewLayer (vl-filename-base (getvar "DWGNAME")))
    (command "-layer" "m" _NewLayer "p" "n" _NewLayer "c" "White" _NewLayer "")
    
        (defun *error* ( msg )
            (LM:endundo (LM:acdoc))
            (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
                (princ (strcat "\nError: " msg))
            )
            (princ)
        )
    
        (cond
            (   (setq sel (ssget "_X" '((0 . "VIEWPORT") (-4 . "<>") (69 . 1) (410 . "~Model"))))
                (LM:startundo (LM:acdoc))
                (repeat (setq idx (sslength sel))
                    (vpo:main (ssname sel (setq idx (1- idx))))
                )
                (LM:endundo (LM:acdoc))
            )
            (   (princ "\nNo viewports were found in any Paperspace layouts."))
        )
        (princ)
    	
    (setvar 1 "CLAYER" oldlayer)
    )
    Last edited by rkmcswain; 2020-12-21 at 01:43 PM. Reason: added [CODE] tags

  5. #5
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,658
    Login to Give a bone
    1

    Default Re: Creating a Lisp that makes a new layer the same as a the drawing name

    This should be what you're looking for.
    Code:
    (defun c:vpoa ( / oldlayer NewLayer)
      (setq oldlayer (getvar "CLAYER")) ;get the current layer first
      (setq NewLayer (vl-filename-base (getvar "DWGNAME")))
      (command "-layer" "m" NewLayer "p" "n" NewLayer "c" "White" NewLayer "")
      (setvar "CLAYER" oldlayer)
    )
    You've got too many arguments to your setq functions. Why are 1 & 2 added in there?

  6. #6
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    0

    Default Re: Creating a Lisp that makes a new layer the same as a the drawing name

    I have been recreating VisualLISP with only visual lisp coding....

    The updated syntax to add a layer with the name of the drawing...

    Code:
    (add 'layers (dwgbase))
    Definitely simplifies this

    Code:
    (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) (vl-filename-base (getvar "dwgname")))
    AutomateCAD

  7. #7
    AUGI Addict madcadder's Avatar
    Join Date
    2000-11
    Location
    Too far from the beach
    Posts
    1,054
    Login to Give a bone
    0

    Default Re: Creating a Lisp that makes a new layer the same as a the drawing name

    This is a little off topic, but I thought you might want to have something like this stashed away for possible later use.
    Code:
    (DEFUN c:np (/ |cmdecho|)
      (SETQ |cmdecho| (GETVAR "cmdecho"))
      (SETVAR "cmdecho" 0)
      (IF (NOT (TBLSEARCH "layer" "G-ANNO-NPLT"))
        (COMMAND "_.-layer"	   "_make"	 "G-ANNO-NPLT" "_color"
    	     "140"	   "G-ANNO-NPLT" "plot"	       "no"
    	     "G-ANNO-NPLT" ""
    	    )
        (COMMAND "_.-layer"	   "_thaw"	 "G-ANNO-NPLT" "_on"
    	     "G-ANNO-NPLT" "plot"	 "no"	       "G-ANNO-NPLT"
    	     "_set"	   "G-ANNO-NPLT" ""
    	    )
      )
      (SETVAR "cmdecho" |cmdecho|)
      (PRINC)
    ) ;_ end of DEFUN
    It is a command called NP.
    It searches to see if the layer "G-ANNO-NPLT" exists.
    If it doesn't exist, then it makes it, sets its color to 140, sets it to not plot, and sets it to the current layer.
    If it does exist, then it makes sure the layer is thawed, turned on, that it is set to not plot, and sets it to the current layer.

    I would also be pretty sure that Peter has a better way of doing this also. Dude has skills.

  8. #8
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    555
    Login to Give a bone
    0

    Default Re: Creating a Lisp that makes a new layer the same as a the drawing name

    For me make a Library layer routine very much like the posts already submitted make sure its autoloaded on startup then can be used in any code.

    (defun lay1 (name col LT /) so it accepts layer name colour and linetype
    (defun lay2 (name /) a name only
    (defun lay3 (name colour /) a name and colour

    Use the madcadder tblsearch method.

  9. #9
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    2

    Default Re: Creating a Lisp that makes a new layer the same as a the drawing name

    When I write code I always try to use the same naming convention. I use a Reddick style naming.

    When I am quick and dirty I use the command pipe, but I much more prefer the activex methods.

    I also try to make the routine handle a single instance and multiple instances in the code... like functions for Layer and Layers etc...

    I always make a header that explains what the functions do and I alphabetize them.

    I put the command line functions first and usually provide a hot key contraction...

    I have been coding LISP for a LONG time... (Almost every day since March of 1986)

    I wrote this library off the cuff... just to keep my skills up.

    I am also a big fan of error management in the code.

    It is not bullet proof but I do a try catch like syntax and try not to nest functions.

    There is nothing worse for a customer to watch a function crash...

    I would rather have it run and not do what it was supposed to do, rather than crash.

    Code:
    ;___________________________________________________________________________________________________________|
    ;
    ; Written By: Peter Jamtgaard C.E., P.E., S.E. copyright 2020 All Rights Reserved
    ;___________________________________________________________________________________________________________|
    ;
    ; Command Line Function Header List
    ;___________________________________________________________________________________________________________|
    ;
    ; Abstract: This program allows the user to specify a list of layers with linetype, color and plot setting.
    ; It will modify existing layers and/or create new layers with those settings.
    ; It also loads linetypes from acad.lin if they are not loaded.
    ;___________________________________________________________________________________________________________|
    
    ;* C:MLC
    ;* Command line function to create layers
    
    ;* C:MyLayersCreate
    ;* Command line function to create layers
    
    ;___________________________________________________________________________________________________________|
    ;
    ; General Function Header List 
    ;___________________________________________________________________________________________________________|
    
    ;* (Errortrap symFunction)
    ;* Function to trap errors and prevent crashes
    
    ;* (LayerCreate strLayerName strLinetype intColor intPlot)
    ;* Function to create or modify a layer with specified linetype, color and plot setting. Returns layer object
    
    ;* (LayersCreate lstLayersInformation)
    ;* Function to create or modify a list of layers with specified linetype, color and plot setting.
    
    ;* (LineTypeLoad lstLineTypes)
    ;* Function to load a linetype
    
    ;* (LineTypesLoad lstLineTypes)
    ;* Function to load a list of linetypes
    
    ;$ Header End
    
    ;___________________________________________________________________________________________________________|
    ;___________________________________________________________________________________________________________|
    ;
    ; Command Line Functions
    ;___________________________________________________________________________________________________________|
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Command line function to create layers
    ;___________________________________________________________________________________________________________|
    
    (defun C:MLC ()(C:MyLayersCreate))
    (defun C:MyLayersCreate (/ lstLayersInformation lstLinetypes objActiveDocument)
     (if (and (setq lstLayersInformation (list (list "G-ANNO-NPLT" "hidden"     140  0)
                                               (list "G-ANNO-PLOT" "continuous" 141 -1);< -1 is :vlax-true, 0 is :vlax-false
    ; You can add your more layers here
                                         )
              )
              (setq objActiveDocument (vla-get-activedocument (vlax-get-acad-object)))
              (setq lstLinetypes      (mapcar 'cadr lstLayersInformation))
              (LineTypesLoad lstLineTypes)
         )
      (LayersCreate lstLayersInformation)
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Functions and Subroutines
    ;___________________________________________________________________________________________________________|
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to trap errors and prevent crashes
    ;___________________________________________________________________________________________________________|
    
    (defun ErrorTrap (symFunction / objError result)
     (if (not
          (vl-catch-all-error-p
           (setq objError (vl-catch-all-apply
                          '(lambda (X)(set X (eval symFunction)))
                           (list 'result)))))
      (if result result 'T)
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create or modify a list of layers with specified linetype, color and plot setting.
    ;___________________________________________________________________________________________________________|
    
    (defun LayersCreate (lstLayersInformation)
     (mapcar '(lambda (X)(apply 'LayerCreate X)) lstLayersInformation)
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create or modify a layer with specified linetype, color and plot setting. Returns layer object 
    ;___________________________________________________________________________________________________________|
    
    (defun LayerCreate (strLayerName strLinetype intColor intPlot / colLayers entLayer objLayer )
     (if (and (setq colLayers (vla-get-layers objActiveDocument))
              (and (setq colLayers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
                   (or (errortrap '(setq objLayer  (vla-item colLayers strLayerName)))
                       (errortrap '(setq objLayer  (vla-add  colLayers strLayerName)))
                   )
              )
              (errortrap '(vlax-put objLayer "color"     intColor   ))
              (errortrap '(vlax-put objLayer "plottable" intPlot))
              (errortrap '(vlax-put objLayer "linetype"  strLineType))
         ) 
      objLayer
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to load a linetype
    ;___________________________________________________________________________________________________________|
    
    (defun LinetypeLoad (strLineType / colLinetypes)
     (if (and (setq colLinetypes (vla-get-linetypes objActiveDocument))           
              (or (errortrap '(vla-load colLinetypes strLinetype (findfile "acad.lin")))
                  T
              )
         )
      (errortrap '(vla-item colLinetypes strLinetype))
     ) 
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to load a list of linetypes
    ;___________________________________________________________________________________________________________|
    
    (defun LineTypesLoad (lstLineTypes)
     (mapcar 'LinetypeLoad lstLineTypes)
    )
    
    (vl-load-com)
    Attached Files Attached Files
    AutomateCAD

Similar Threads

  1. Replies: 0
    Last Post: 2013-02-27, 05:40 PM
  2. Two Blocks with the same name in the same dwg !
    By costas.vassiliou in forum AutoCAD General
    Replies: 7
    Last Post: 2009-09-11, 01:46 PM
  3. Create Layer name using the Drawing name
    By g_wong in forum AutoLISP
    Replies: 14
    Last Post: 2009-01-09, 02:25 AM
  4. Replies: 10
    Last Post: 2006-04-13, 10:55 AM
  5. Replies: 13
    Last Post: 2005-02-02, 11:02 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
  •