Print Page | Close Window

LISP Error

Printed From: CAD Forum
Category: EN
Forum Name: AutoCAD
Forum Description: Discussion about AutoCAD and AutoCAD LT, viewers, DWG and DWF formats, Design Review, AutoCAD 360, add-ons
URL: https://www.cadforum.cz/forum_en/forum_posts.asp?TID=5351
Printed Date: 21.Apr.2026 at 22:51


Topic: LISP Error
Posted By: kameron1967
Subject: LISP Error
Date Posted: 21.Feb.2011 at 22:11
Hi.  Someone helped me with this, but I'm not able to get it to run. It has a syntax error.
[CODE]
(defun C:NEWL ();R-1
(vl-load-com);R-1
(setq PickItem (car (entsel "\nPick object for new layer: ")));R-1
(setq ObjLayer (cdr (assoc 8 (entget pickItem))));R-1
(setq ObjType (cdr (assoc 0 (entget pickItem))));R-1
(princ (strcat ObjLayer is layer name of " ObjType " selected"));R-1
(initget "N D V");R-1
(Setq LyrSuffix (getkword "\nEnter suffix type [N-PKG-1/D-PKG1/V-PKG-1]:"));R-1
);R-0



Replies:
Posted By: HAWDesigner
Date Posted: 21.Feb.2011 at 23:27
The initial error I got was "ERROR: malformed string on input".

By changing Line7 to the following, it cleared that error, but every time I enter a suffix type, I now get the error: "Invalid Option Keyword"

(princ (strcat ObjLayer " is layer name of " ObjType " selected"));R-1


NOTE: You were missing a " (quote) between ObjLayer and is.


-------------
--
R. Williams
AutoCAD 2010 Certified Professional
<!-- If all else fails hit F1 -->
<<AutoCAD 2009


Posted By: kameron1967
Date Posted: 22.Feb.2011 at 01:43
Thanks.  So if I wanted to create a new layer based on the entity that I selected (for instance a line on H-ROOM layer), how would I attach the modifier to that entity so that if I choose D for suffix, it will automatically create a new layer based on the entity so that the new layer would look like this: H-ROOM-D-PKG1?  Thanks.
 
(Setq LyrSuffix (getkword "\nEnter suffix type [N-PKG-1/D-PKG1/V-PKG-1]:"))
 
Would it be something like this..?
 
      (setq oLayers
      (vla-get-layers
        (vla-get-activedocument
   (vlax-get-acad-object))))
(if (not (tblsearch "LAYER" objlayer))
       (progn 
         (setq LyrNew (vla-add oLayers LYR)
        LyrTab (vla-item oLayers sLYR)
        )
  (vla-put-linetype LyrNew (vla-get-linetype LyrTab))
  (vla-put-truecolor LyrNew (vla-get-truecolor LyrTab))
  (vla-put-truecolor LyrNew (vla-get-truecolor LyrTab))
  (vla-put-freeze LyrNew (vla-get-freeze LyrTab))
  (vla-put-layeron LyrNew (vla-get-layeron LyrTab))
  (vla-put-lineweight LyrNew (vla-get-lineweight LyrTab))
  (vla-put-lock LyrNew (vla-get-lock LyrTab))
  (vla-put-material LyrNew (vla-get-material LyrTab))
  ;(vla-put-plotstylename LyrNew (vla-get-plotstylename LyrTab))
  ))
(setq ent (car(entsel)))
(setq nlayer
(cdr (assoc 8 (entget ent)))
)
(setq LyrNew (strcat LYR lyrsuffix))


Posted By: HAWDesigner
Date Posted: 22.Feb.2011 at 14:00
I'm not an expert on LISP yet, sorry. Finding syntax errors is about as far as my LISP abilities go. Perhaps CarlB can lend you a hand on this.

Good Luck!!


-------------
--
R. Williams
AutoCAD 2010 Certified Professional
<!-- If all else fails hit F1 -->
<<AutoCAD 2009


Posted By: Vladimir Michl
Date Posted: 22.Feb.2011 at 14:58
Yes, this is one of the ways how to do it. This LISP code seems to be mixed from several sources - the names of layer variables are mixed: objlayer, LYR, nlayer.
 
Assumes e.g. that objlayer is already filled with the old layer name.


-------------
Vladimir Michl (moderator)
ARKANCE - https://arkance.world" rel="nofollow - https://arkance.world - Autodesk Platinum Partner


Posted By: kameron1967
Date Posted: 22.Feb.2011 at 18:00

yes, you're right.  It was taken from various source.  I need to somehow tie them all in so that whatever letter I choose, it appends the right suffix to the new layer that it will be creating.

If anyone has a shortcut to creating a simple layer modifer, I'd love to see it.  Basically, if N is chosen, it will add the -N-PKG1 in the new layer based on the entity selected.  Thanks.


Posted By: CarlB
Date Posted: 23.Feb.2011 at 02:06
OK since my name was mentioned..here's my take at a simple lisp..
 

(defun C:NEWL ();R-1
   (setq PickItem (car (entsel "\nPick object for new layer: ")));R-1
   (setq ObjLayer (cdr (assoc 8 (entget pickItem))));R-1
   (setq ObjType (cdr (assoc 0 (entget pickItem))));R-1
   (princ (strcat ObjLayer " is layer name of " ObjType " selected"));R-1
   (initget "N D V");R-1
   (Setq LyrSuffix (getkword "\nEnter suffix type [N-PKG-1/D-PKG1/V-PKG-1]:"));R-1
   (setq NewLyrName
      (cond
         ((= LyrSuffix "N") (strcat ObjLayer "-N-PKG-1" ))
         ((= LyrSuffix "D") (strcat ObjLayer "-D-PKG-1"))
         ((= LyrSuffix "V") (strcat ObjLayer "-V-PKG-1"))
       )
    )
    (command "._Layer" "_M" NewLyrname "")
   (princ)
)


Posted By: kameron1967
Date Posted: 23.Feb.2011 at 02:24
Thanks, CarlB.  You're a savior!


Posted By: HAWDesigner
Date Posted: 23.Feb.2011 at 15:26
Nice Carl, thanks for that. I hope I didn't throw you under the bus there. Shocked

Here's something I noticed in v2009:

(Setq LyrSuffix (getkword "\nEnter suffix type [N-PKG-1/D-PKG1/V-PKG-1]:"));R-1


The code above results in an error if you select the value from the Drafting ToolTips.



... because it is entering the entire value instead of just the N, D, or V as the "(initget "N D V");R-1" requests.

In order to prevent that error, I had to change the line to the following, but I know that doesn't fully explain what's going on.

(Setq LyrSuffix (getkword "\nEnter suffix type [N/D/V]:"));R-1



Is there any way to have it both ways?


-------------
--
R. Williams
AutoCAD 2010 Certified Professional
<!-- If all else fails hit F1 -->
<<AutoCAD 2009


Posted By: kameron1967
Date Posted: 23.Feb.2011 at 17:48
CarlB - is there a way to make it so that the newly created layer of the entity that we selected retains that same layer information as the one entity that we selected?  As of right now, it creates a new layer, but with default color and linetype.  If we can specify the linetype and color associated to each modifier, that would work also.  Thanks.
 
Originally posted by CarlB CarlB wrote:

OK since my name was mentioned..here's my take at a simple lisp..
 

(defun C:NEWL ();R-1
   (setq PickItem (car (entsel "\nPick object for new layer: ")));R-1
   (setq ObjLayer (cdr (assoc 8 (entget pickItem))));R-1
   (setq ObjType (cdr (assoc 0 (entget pickItem))));R-1
   (princ (strcat ObjLayer " is layer name of " ObjType " selected"));R-1
   (initget "N D V");R-1
   (Setq LyrSuffix (getkword "\nEnter suffix type [N-PKG-1/D-PKG1/V-PKG-1]:"));R-1
   (setq NewLyrName
      (cond
         ((= LyrSuffix "N") (strcat ObjLayer "-N-PKG-1" ))
         ((= LyrSuffix "D") (strcat ObjLayer "-D-PKG-1"))
         ((= LyrSuffix "V") (strcat ObjLayer "-V-PKG-1"))
       )
    )
    (command "._Layer" "_M" NewLyrname "")
   (princ)
)


Posted By: CarlB
Date Posted: 23.Feb.2011 at 19:50
Revised to take on layer color & linetype. no checking for locked status, etc.  Enjoy!
 
(defun C:NEWL ();R-1, R-2 to reproduce layer settings 2/23/11
   (setq PickItem (car (entsel "\nPick object for new layer: ")));R-1
   (setq Entdata (entget pickItem))
   (setq ObjLayer (cdr (assoc 8 EntData)));R-1
   (setq ObjType (cdr (assoc 0 EntData)));R-1
   (princ (strcat ObjLayer " is layer name of " ObjType " selected"));R-1
   (setq Lyrdata (tblsearch "LAYER" ObjLayer));R-2
   (setq LyrLtype (cdr (assoc 6 Lyrdata)));R-2
   (setq LyrColor (cdr (assoc 62 Lyrdata)));R-2
   (initget "N D V");R-1
   (Setq LyrSuffix (getkword "\nEnter suffix type [N-PKG-1/D-PKG1/V-PKG-1]:"));R-1
   (setq NewLyrName
      (cond
        ((= LyrSuffix "N") (strcat ObjLayer "-N-PKG-1" ))
        ((= LyrSuffix "D") (strcat ObjLayer "-D-PKG-1"))
        ((= LyrSuffix "V") (strcat ObjLayer "-V-PKG-1"))
      )
   )
   (command "._Layer" "_M" NewLyrname "_C" LyrColor "" "_L" LyrLtype "" "");R-2
   (princ)
)


Posted By: kameron1967
Date Posted: 28.Feb.2011 at 19:06
CarlB,
 
Sorry to keep bothering you.  But if I wanted to also include lineweight, but have the ability to modify each aspect of the layer color, linetype and lineweight within the selected suffix - for instance D, can I specify the layer information within the parenthesis?  See below.  Thanks.
 
        ((= LyrSuffix "A")
(setq LyrLtype "sphantom")
(setq LyrColor "1")
;(setq LyrLw "0.25") - what's the variable for lineweight?
(strcat ObjLayer "-A-D1C1270" ))
 
  


Posted By: CarlB
Date Posted: 28.Feb.2011 at 20:02
Well the 'cond' was a subfunction of a 'setq layer' so you should rearrange like below to add more 'setqs' to the cond:
 
(cond
  ((= LyrSuffix "A")
     (setq xxxlayer (strcat ObjLayer "-A-D1C1270" ))
    (setq lyrcolor "1")
   (setq lyrltype "continuous")
   (setq lyrweight "0.25")
);R-3
 
-or shorter versio-
 
(cond
  ((= LyrSuffix "A")
     (setq xxxlayer (strcat ObjLayer "-A-D1C1270" )
    lyrcolor "1"
    lyrltype "continuous"
   lyrweight "0.25"
);R-3
 


Posted By: kameron1967
Date Posted: 28.Feb.2011 at 20:34
Thanks, CarlB.  I was trying so hard to find the LYRweight variable.  I tried LYRLW, LYRLWEIGHT, but couldn't get it to work.  I kind of makes sense for the variable to be called LYRweight, but I would never have guessed it.  Once again, you're my savior!
 
One last question:  What is the lyrweight number?  I never know where to find this info (see below).  Thanks.
 
(setq Lyrdata (tblsearch "LAYER" ObjLayer));R-2  
(setq LyrLtype (cdr (assoc 6 Lyrdata)));R-2  
(setq LyrColor (cdr (assoc 62 Lyrdata)));R-2  

(setq Lyrweight (cdr (assoc ?? Lyrdata)));R-2  
 
Big%20smile


Posted By: CarlB
Date Posted: 28.Feb.2011 at 21:03

I think we have more to work out...

I had thought you were just assigning a lineweight to a layer with this code, so the variable name could be whatever you want to call it, then that same variable name would be used later in the command to define the new layer:
(command ".-layer" etc.... "_LW" lyrweight etc...)
 
It you want to assign the same layer weight as object layer selected, will need some other steps...


Posted By: kameron1967
Date Posted: 28.Feb.2011 at 21:07
CarlB - there are some layers that require different lineweight to be defined, where the rest uses whatever was assigned in the original layer.  I thought if I just set the variable for the lineweight on the layer table, it will default to that, unless I specified it as shown below:
 
 
(setq Lyrdata (tblsearch "LAYER" ObjLayer));R-2  
(setq LyrLtype (cdr (assoc 6 Lyrdata)));R-2  
(setq LyrColor (cdr (assoc 62 Lyrdata)));R-2  
(setq Lyrweight (cdr (assoc 370 Lyrdata)));R-2   (LINEWEIGHT)
 
        ((= LyrSuffix "X")
(setq LyrLtype "sphantom")
(setq LyrColor "8")
(setq Lyrweight "0.13")
 (strcat ObjLayer "-X-D1C1270" ));R-3

       );R-2

    );R-1

(command "-Layer" "_M" NewLyrname "_C" LyrColor "" "_L" LyrLtype "_lw" Lyrweight "" "");R-2   ADDED LINEWEIGHT HERE AS WELL



Posted By: CarlB
Date Posted: 01.Mar.2011 at 07:44
Your idea is sound, extracting default data, overriding it later if desired. Since i don't have AutoCADd to check, not sure the 370 code & tblsearch will give you lineweight, or if so will be the correct format to feed back to the "layer" command. You can give me an update on that :)
 
(defun C:NEWL ();R-1, R-2 to reproduce layer settings 2/23/11
   (setq PickItem (car (entsel "\nPick object for new layer: ")));R-1
   (setq Entdata (entget pickItem))   (setq ObjLayer (cdr (assoc 8 EntData)));R-1
   (setq ObjType (cdr (assoc 0 EntData)));R-1   (princ (strcat ObjLayer " is layer name of " ObjType " selected"));R-1
   (setq Lyrdata (tblsearch "LAYER" ObjLayer));R-2
   (setq LyrLtype (cdr (assoc 6 Lyrdata)));R-2
   (setq LyrColor (cdr (assoc 62 Lyrdata)));R-2
   (setq LyrWeight (cdr (assoc 370 Lyrdata)));;R-3
   (initget "N D V X");R-1
   (setq LyrSuffix (getkword "\nEnter suffix type [N-PKG-1/D-PKG1/V-PKG-1/X-PKG-1]:"));R-1
   (cond
       ((= LyrSuffix "N")
            (setq NewLyrName (strcat ObjLayer "-N-PKG-1"))
        )
       ((= LyrSuffix "D")
              (setq NewLyrName (strcat ObjLayer "-D-PKG-1"))
        )
       ((= LyrSuffix "V")
              (setq NewLyrName (strcat ObjLayer "-V-PKG-1"))
        )
       ((= LyrSuffix "X");;R-3
              (setq NewLyrName (strcat ObjLayer "-X-PKG-1"))
              (setq LyrLtype "PHANTOM")
              (setq LyrColor "8")
              (setq LyrWeight "0.13")
              
        )
   )
   (command "._Layer" "_M" NewLyrname "_C" LyrColor "" "_L" LyrLtype "_LW" LyrWeight "" "");R-2&3
   (princ)
)


Posted By: kameron1967
Date Posted: 01.Mar.2011 at 22:04
CarlB,
 
This is the solution.  In addition to the code shown below, the lineweight needs to be 13 instead of 0.13.
 
Thanks though for your valuable help.  I really appreciate your help in this.
 
[code]
 
(setq
LyrEdata (entget (tblobjname "Layer" ObjLayer))
LyrWeight (cdr (assoc 370 LyrEdata))
)
((= LyrSuffix "X") (setq NewLyrName (strcat ObjLayer "-X-PKG-1"))
(setq LyrLtype "PHANTOM") (setq LyrColor "8") (setq LyrWeight 53)) )

(command "-Layer" "_M" NewLyrname "_C" LyrColor "" "_L" LyrLtype "" "LW" (if (= LyrWeight -3) "D" (/ LyrWeight 100.0)) "" "")
(princ)
)
(princ "\n<!> Type {LAX} and choose your layer modifier <!>")



Print Page | Close Window