+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
  1. #1
    Forum Newbie
    Using
    Map 3D 2007
    Join Date
    Mar 2007
    Location
    Sacramento, CA, USA
    Posts
    5

    Default Lisp for Changing Objects layer

    Registered forum members do not see this ad.

    I am a beginner Lisp and I need help in writing a routine for changing the layer of an object to a predefined layer. I have several employees working on a project and I want to be able to standardize the layer change. I started writing a basic routine and the command that I am stuck on is "copy to new layer", I don't know how to have Lisp stop and wait for user selection, and then return back to the lisp routine. I would have the lisp routine already know what layer to place the object on, all I want is for the user to have to just click on the object, have lisp automatically change the object's layer, and then the rest of the routine is to have the user change the object (the object is a text string). The rest of the routine I don't know how to write yet so help with this would be great too.
    I'm using AutoCAD 2007 3D Map with PC.
    Thanks for the help.

  2. #2
    Super Moderator fuccaro's Avatar
    Using
    AutoCAD 2006
    Join Date
    Nov 2002
    Location
    Romania, Marosvasarhely
    Posts
    3,540

    Default

    First you need to know the entity to modify. Let's say the entity name is stored in the variable ename. Now extract the data to get the entity list, store it in the variable elist:
    Code:
    (setq elist (entget ename))
    Store the name of the new layer in to_layer:
    Code:
    (setq to_layer "your layer name goes here")
    And finaly change in the elist the group 8 (this is how the layer is stored) with the new value:
    Code:
    (setq elist (subst (cons 8 to_layer) (assoc 8 elist) elist))
    The list is changed -but elist is just a list containing the values copied from the ename. To really make the change you need this:
    Code:
    (entmod elist)
    Donne.
    It's nice to be nice, but sometimes is nicer to be evil!.
    Tip: Please do not PM or email me with CAD questions - use the forums, you'll get an answer sooner.

  3. #3
    Forum Deity
    Using
    not specified
    Join Date
    Jul 2004
    Location
    Anchorage, Alaska
    Posts
    2,074

    Default

    For some simple AutoCAD operations, the simplest way to code is often by use of the "command" lisp function. For example, for your task to 'copy to layer' of a single item can be done with the following lisp code:

    (command "copy" pause "" "0,0" "0,0" "chprop" "l" "" "layer" "NewLayerName" "")

    The syntax is basically as you would enter commands on the comand line. The "puse" allows a single user respons (pick an item in this case). The double quotes act as an 'enter'.
    Last edited by CarlB; 28th Mar 2007 at 08:34 pm. Reason: because

  4. #4
    Banned Alan Cullen's Avatar
    Using
    Map 3D 2009
    Join Date
    Jun 2006
    Location
    Cairns, Queensland, Australia
    Posts
    4,181

    Default

    Following on from what CarlB said.....

    Precede Carl's command with the following line of code, it allows you to enter the New Layer Name on the command line.....

    (setq NewLayerName (getstring T "\n Enter new layer name : "))
    (command "copy" pause "" "0,0" "0,0" "chprop" "l" "" "layer" "NewLayerName" "")

    edit....I haven't checked any code, it's all just examples for you....

  5. #5
    Forum Newbie
    Using
    Map 3D 2007
    Join Date
    Mar 2007
    Location
    Sacramento, CA, USA
    Posts
    5

    Default

    Thanks everyone for your feedback. The simple command lisp is what I was looking for, that's all I know how to program at this point. The more complex language will come in handy when I learn more. Any more input from others on these basic simple command lisps would be helpfull.

  6. #6
    Banned Alan Cullen's Avatar
    Using
    Map 3D 2009
    Join Date
    Jun 2006
    Location
    Cairns, Queensland, Australia
    Posts
    4,181

    Default

    EScholtz.......

    There are many lisp routines already out there that do what you're after.......pick an entity "e1", then pick an entity "e2" that is on the layer you want to change "e1" to.....the routine then places "e1" onto layer "e2".

    I don't know the names of any of these routines.....but I'm sure someone will quickly tell you the names of these routines.

  7. #7
    Forum Newbie
    Using
    Map 3D 2007
    Join Date
    Mar 2007
    Location
    Sacramento, CA, USA
    Posts
    5

    Default

    Alan Cullen;
    I see that what you recommend to precede the command line to enter a new layer name, this lets the user enter the layer name at the command line each time they run the lisp, but what if I want the layer name to be predetermined by the lisp command? I don't want the users to have to enter in the layer name, I want the layer to always be the same so that it will be standardized. Is there a way to make a new layer in the lisp, then change the existing object to that layer name?
    Thanks for the help.

  8. #8
    Super Member ASMI's Avatar
    Using
    AutoCAD 2008
    Join Date
    Nov 2005
    Location
    Oceanus Procellarum, Moon
    Posts
    1,427

    Default

    For 'one click' layer selection:

    Code:
    (defun c:laysel(/ samObj filLst laySet)
      (and
        (setq samObj(entsel "\nSelect object at wanted layer > "))
        (setq filLst(assoc 8(entget(car samObj))))
        (sssetfirst nil (ssget "_X" (list filLst)))
        (princ(strcat "\nLayer name: " (cdr filLst)))
        ); end and
      (princ)
      ); end of c:laysel

  9. #9
    Forum Deity
    Using
    not specified
    Join Date
    Jul 2004
    Location
    Anchorage, Alaska
    Posts
    2,074

    Default

    Here's my contribution, following the simpler "command" syntax:

    Code:
    (defun c:c2l () ;command routine "c2l" for "copy to layer"
       (setq origlayer (getvar "clayer"));;stores current layer
       (setq DefLayer "DefLayerName");;stores layer name in variable
       ;;change "DefLayerName" to layer name you want
       (command "layer" "m" DefLayer "")
       ;;creates layer if not already present and sets it current
       (setvar "clayer" origlayer);;set original layer current
       (command "copy" pause "" "0,0" "0,0" "chprop" "l" "" "layer" DefLayer "")
       ;;copies object over itself, changes to default layer
       (princ);;quiet exit
    )

  10. #10
    Super Member ASMI's Avatar
    Using
    AutoCAD 2008
    Join Date
    Nov 2005
    Location
    Oceanus Procellarum, Moon
    Posts
    1,427

    Default

    Registered forum members do not see this ad.

    May be look layer objects and rename it if need?

    Code:
    (defun c:renlay(/ samObj filLst laySet layName Ans newName vlaLay)
      (vl-load-com)
      (if
        (setq samObj
    	   (entsel "\nSelect object at wanted layer > "))
        (progn
          (setq filLst
    	     (assoc 8(entget(car samObj))))
          (sssetfirst nil
    	(setq laySet(ssget "_X" (list filLst))))
          (initget "Yes No")
          (setq Ans(getkword
    		 (strcat "\n<<< Layer name: '"
    			 (setq layName(cdr filLst))
    			 "' >>>\nDo yo want to rename it [Yes/No]? <Yes>: ")))
          (if(null Ans)(setq Ans "Yes"))
          (if(= Ans "Yes")
    	(progn
    	  (if
    	    (/= ""
    		(setq newName
    		       (getstring T "\nType new layer name: ")))
    	    (progn
    	      (setq vlaLay
    		     (vla-Item
    		       (vla-get-Layers
    			 (vla-get-ActiveDocument
    			   (vlax-get-acad-object))) layName))
    	      (if
    		(vl-catch-all-error-p
    		  (vl-catch-all-apply
    		    'vla-put-Name
    		    (list vlaLay newName)))
    		(princ "\n>>> Can't rename this layer! <<< ")
    		(princ
    		  (strcat "\n<<< Layer '" layName
    			  "' renamed to '" newName "' >>>"))
    		); end if
    	      ); end progn
    	    ); end if
    	  ); end progn
    	); end if
          (sssetfirst nil nil)
         ); end progn
        ); end if
      (princ)
      ); end of c:renlay

Similar Threads

  1. Need LISP program to select all objects in layer
    By Vigilante in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 30th Nov 2006, 08:35 pm
  2. Changing the Layer
    By El Barto in forum AutoCAD Beginners' Area
    Replies: 5
    Last Post: 25th Oct 2006, 02:40 pm
  3. Lisp for moving objects from one layer to another
    By Yamma in forum AutoLISP, Visual LISP & DCL
    Replies: 20
    Last Post: 24th Feb 2006, 10:25 am
  4. Scaling without changing objects height
    By RJB in forum AutoCAD Drawing Management & Output
    Replies: 1
    Last Post: 6th Jan 2005, 06:22 pm
  5. Changing an objects layer?
    By grev in forum AutoCAD Drawing Management & Output
    Replies: 3
    Last Post: 27th Feb 2004, 07:44 pm

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