Jump to content

LISP | IF Conditions | Copy to Existing Layer


bluebravo

Recommended Posts

Goal: LISP that copies selected objects to existing layer, using layer name shortcuts.

 

Example:

1. Select objects

2. "CX" (Lisp command) asks for layer to copy into

3. "1CC" (Shortcut) Copies to actual layer name 1_COLS-CONT

OR "2CC" copies to actual layer name 2_COLS-CONT, etc.

 

I have the following LISP to start, and I just need to add all of the conditions for accepting a layer name shortcut and changing the layer to the corresponding one. I have a large number of layers, and so one lisp command for each seems too much.

 

I understand that I would need multiple if conditions, but I need help with the actual code (fairly new to lisp).

 

Thank you for your time!

 

;FILENAME cx.LSP
;THIS LISP ROUTINE copies selected objects in place then prompts
;the user to change the layer of previus objects selected.
;BY Ryan 3/5/02
(defun c:cx()
(setq ss1 (ssget))
(setq laycx (getstring "Destination layer for copy: "))
(command "_copy" "P" "" "@" "@")
(command "_chprop" "P" "" "la" laycx "")
)

Link to comment
Share on other sites

Here is my attempt. I didn't add any error handler, undo points, or control echo. I also made some changes in the command line.

 

*edit* I also did not include any Layer Check/Create function.

 

(defun c:cx( / ss1 laycx Layer )
 (setq ss1 (ssget))

 (setq laycx (strcase (getstring "\nDestination layer for copy: ")))

 (cond					;;add as many conditions as you like.
   ((= laycx "1CC")
    (setq Layer "1_COLS-CONT"))
   ((= laycx "2CC")
    (setq Layer "2_COLS-CONT"))
   )
 (command "_copy" ss1 "" "@" "@")
 (command "_chprop" "P" "" "la" Layer "")
 (princ)
 )

Edited by guitarguy1685
additional information.
Link to comment
Share on other sites

I would suggest the following:

([color=BLUE]defun[/color] c:cx ( [color=BLUE]/[/color] abr idx lay sel )
   ([color=BLUE]setq[/color] abr
      '(
           ([color=MAROON]"1CC"[/color] . [color=MAROON]"1_COLS-CONT"[/color])
           ([color=MAROON]"2CC"[/color] . [color=MAROON]"2_COLS-CONT"[/color])
       )
   )
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] [color=MAROON]"_:L"[/color]))
       ([color=BLUE]progn[/color]
           ([color=BLUE]initget[/color] (LM:lst->str ([color=BLUE]mapcar[/color] '[color=BLUE]car[/color] abr) [color=MAROON]" "[/color]))
           ([color=BLUE]setq[/color] lay
               ([color=BLUE]cond[/color]
                   (   ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] ([color=BLUE]getkword[/color] ([color=BLUE]strcat[/color] [color=MAROON]"\nLayer shortcut ["[/color] (LM:lst->str ([color=BLUE]mapcar[/color] '[color=BLUE]car[/color] abr) [color=MAROON]"/"[/color]) [color=MAROON]"] <"[/color] ([color=BLUE]caar[/color] abr) [color=MAROON]">: "[/color])) abr)))
                   (   ([color=BLUE]cdar[/color] abr))
               )
           )
           ([color=BLUE]if[/color] ([color=BLUE]not[/color] ([color=BLUE]tblsearch[/color] [color=MAROON]"layer"[/color] lay))
               ([color=BLUE]entmake[/color]
                   ([color=BLUE]list[/color]
                      '(000 . [color=MAROON]"LAYER"[/color])
                      '(100 . [color=MAROON]"AcDbSymbolTableRecord"[/color])
                      '(100 . [color=MAROON]"AcDbLayerTableRecord"[/color])
                      '(070 . 0)
                       ([color=BLUE]cons[/color] 2 lay)
                   )
               )
           )
           ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] idx ([color=BLUE]sslength[/color] sel))
               ([color=BLUE]vla-put-layer[/color] ([color=BLUE]vla-copy[/color] ([color=BLUE]vlax-ename->vla-object[/color] ([color=BLUE]ssname[/color] sel ([color=BLUE]setq[/color] idx ([color=BLUE]1-[/color] idx))))) lay)
           )
       )
   )
   ([color=BLUE]princ[/color])
)

[color=GREEN];; List to String  -  Lee Mac[/color]
[color=GREEN];; Concatenates each string in a supplied list, separated by a given delimiter[/color]
[color=GREEN];; lst - [lst] List of strings to concatenate[/color]
[color=GREEN];; del - [str] Delimiter string to separate each item[/color]

([color=BLUE]defun[/color] LM:lst->str ( lst del )
   ([color=BLUE]if[/color] ([color=BLUE]cdr[/color] lst)
       ([color=BLUE]strcat[/color] ([color=BLUE]car[/color] lst) del (LM:lst->str ([color=BLUE]cdr[/color] lst) del))
       ([color=BLUE]car[/color] lst)
   )
)

([color=BLUE]vl-load-com[/color]) ([color=BLUE]princ[/color])

Link to comment
Share on other sites

Thanks for the reply guitarguy!

 

Your edit works well, except that if I enter an unavailable shortcut it copies the objects to the 0 layer. I know you did mention there is no 'error handler', but it would be really useful to avoid unwanted duplicates.

 

Thank you for your time!

Link to comment
Share on other sites

Thanks for the reply Lee Mac!

 

The lisp really meets my needs, but I do have another request. I find the dynamic list feature really cool, except there will be at least 200 shortcuts/layers and so the list would be too long. Could you please tell me how to suppress the display of the list? Everything else I really like, especially the 'Invalid option'.

 

Thank you for your time!

Link to comment
Share on other sites

You could make a CSV text file with two items "shortcut,new layer name" and just read the file it could have as many lines as you like, Lee has a read csv so can get at the two items then just (if (= shortcut fileshortcut)(setq layname newlay)) post if you need an example.

Link to comment
Share on other sites

Just thinking a bit more I was involved in a commercial product and we got round the layer problem by providing a user interface if you drew a particular object like say a wall the software knew what layers to put everthing on, so no layer problems. It had a text file in the back end for layer names.

 

It sounds like your random drafting then fixing up, rather than trying to control as you create. Maybe use the same idea. I have posted some reactor code which could do what you want type 1CC 2CC etc and it knows you want to draw on layer 1_COLS-CONT.

 

http://www.cadtutor.net/forum/showthread.php?93661-Lisp-for-fillet-radius./page3

Link to comment
Share on other sites

Thanks for the reply bigal!

 

At this point, I am most comfortable implementing Lee Mac's solution. Except for that dynamic list I cannot figure out how to suppress/remove, it meets our current needs.

 

However, I will look into your suggestions for future lisp developments :)

 

Thank you for your time

Link to comment
Share on other sites

Take this line

(   (cdr (assoc (getkword (strcat "\nLayer shortcut [" (LM:lst->str (mapcar 'car abr) "/") "] <" (caar abr) ">: ")) abr)))

 

Change it to this.

(   (cdr (assoc (getkword (strcat "\nLayer shortcut: <"   (caar abr) ">: ")) abr)))

 

That should suppress all the layer names.

Link to comment
Share on other sites

Have a look at this post it can be changed to work with layers as you want so no need for multiple shortcuts looking at other code I would use "L" then your shortcut L1CC" You can though use 1 if you want or 2 or 3 etc.

 

http://www.cadtutor.net/forum/showthread.php?100895-turn-macro-script-to-a-lisp-for-insert-block-from-another-drawing

Edited by BIGAL
link added
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...