+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Junior Member
    Using
    AutoCAD 2015
    Join Date
    Nov 2016
    Posts
    11

    Default LISP | IF Conditions | Copy to Existing Layer

    Registered forum members do not see this ad.

    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!

    Code:
    ;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 "")
    )

  2. #2
    Senior Member
    Computer Details
    guitarguy1685's Computer Details
    Operating System:
    Window 10
    Computer:
    Dell Precision Tower 5810
    Motherboard:
    Dell
    CPU:
    Intel Xeon ES-1620 v3 @ 3.5GHz
    RAM:
    16GB
    Graphics:
    Nvidia Quadro K4200
    Monitor:
    Dual Dell U2412m
    Discipline
    See details...
    guitarguy1685's Discipline Details
    Occupation
    CAD Manager
    Discipline
    See details below.
    Details
    Building envelops & AES
    Using
    AutoCAD 2017
    Join Date
    Oct 2009
    Posts
    286

    Default

    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.

    Code:
    (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)
      )
    Last edited by guitarguy1685; 6th Jun 2017 at 04:52 pm. Reason: additional information.

  3. #3
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,322

    Default

    I would suggest the following:
    Code:
    (defun c:cx ( / abr idx lay sel )
        (setq abr
           '(
                ("1CC" . "1_COLS-CONT")
                ("2CC" . "2_COLS-CONT")
            )
        )
        (if (setq sel (ssget "_:L"))
            (progn
                (initget (LM:lst->str (mapcar 'car abr) " "))
                (setq lay
                    (cond
                        (   (cdr (assoc (getkword (strcat "\nLayer shortcut [" (LM:lst->str (mapcar 'car abr) "/") "] <" (caar abr) ">: ")) abr)))
                        (   (cdar abr))
                    )
                )
                (if (not (tblsearch "layer" lay))
                    (entmake
                        (list
                           '(000 . "LAYER")
                           '(100 . "AcDbSymbolTableRecord")
                           '(100 . "AcDbLayerTableRecord")
                           '(070 . 0)
                            (cons 2 lay)
                        )
                    )
                )
                (repeat (setq idx (sslength sel))
                    (vla-put-layer (vla-copy (vlax-ename->vla-object (ssname sel (setq idx (1- idx))))) lay)
                )
            )
        )
        (princ)
    )
    
    ;; List to String  -  Lee Mac
    ;; Concatenates each string in a supplied list, separated by a given delimiter
    ;; lst - [lst] List of strings to concatenate
    ;; del - [str] Delimiter string to separate each item
    
    (defun LM:lst->str ( lst del )
        (if (cdr lst)
            (strcat (car lst) del (LM:lst->str (cdr lst) del))
            (car lst)
        )
    )
    
    (vl-load-com) (princ)
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  4. #4
    Junior Member
    Using
    AutoCAD 2015
    Join Date
    Nov 2016
    Posts
    11

    Default

    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!

  5. #5
    Junior Member
    Using
    AutoCAD 2015
    Join Date
    Nov 2016
    Posts
    11

    Default

    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!

  6. #6
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,720

    Default

    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.
    A man who never made mistakes never made anything

  7. #7
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,720

    Default

    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/showth...-radius./page3
    A man who never made mistakes never made anything

  8. #8
    Junior Member
    Using
    AutoCAD 2015
    Join Date
    Nov 2016
    Posts
    11

    Default

    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

  9. #9
    Senior Member
    Computer Details
    guitarguy1685's Computer Details
    Operating System:
    Window 10
    Computer:
    Dell Precision Tower 5810
    Motherboard:
    Dell
    CPU:
    Intel Xeon ES-1620 v3 @ 3.5GHz
    RAM:
    16GB
    Graphics:
    Nvidia Quadro K4200
    Monitor:
    Dual Dell U2412m
    Discipline
    See details...
    guitarguy1685's Discipline Details
    Occupation
    CAD Manager
    Discipline
    See details below.
    Details
    Building envelops & AES
    Using
    AutoCAD 2017
    Join Date
    Oct 2009
    Posts
    286

    Default

    Take this line
    Code:
    (   (cdr (assoc (getkword (strcat "\nLayer shortcut [" (LM:lst->str (mapcar 'car abr) "/") "] <" (caar abr) ">: ")) abr)))
    Change it to this.
    Code:
    (   (cdr (assoc (getkword (strcat "\nLayer shortcut: <"   (caar abr) ">: ")) abr)))
    That should suppress all the layer names.

  10. #10
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,720

    Default

    Registered forum members do not see this ad.

    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/showth...nother-drawing
    Last edited by BIGAL; 13th Jun 2017 at 03:54 am. Reason: link added
    A man who never made mistakes never made anything

Similar Threads

  1. .LIN file missing and i need to copy/edit and existing linetype!
    By R1CK3R5 in forum The CUI, Hatches, Linetypes, Scripts & Macros
    Replies: 2
    Last Post: 26th Mar 2015, 12:17 pm
  2. Replies: 9
    Last Post: 15th Dec 2011, 08:26 pm
  3. copy layer and sort, sometime duble copy
    By nalsur8 in forum AutoLISP, Visual LISP & DCL
    Replies: 42
    Last Post: 25th Feb 2011, 02:38 pm
  4. Tough Questions - Copy Xref Layer Lisp
    By johnshar123xx in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 7th Dec 2009, 04:04 pm
  5. Conditions with LISP
    By homer in forum AutoCAD Beginners' Area
    Replies: 17
    Last Post: 10th May 2007, 03:21 pm

Tags for this Thread

Bookmarks

Posting Permissions

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