TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Dahzee on November 28, 2018, 03:17:22 AM

Title: Change Entities to new layer
Post by: Dahzee on November 28, 2018, 03:17:22 AM
I have the below routine which I found on one of the forums (sadly no header information to acknowledge the author).

After selecting entities It will change them to the Crease layer and if this layer doesn't exist create it.

I receive lots of files which don't match my layer standards so this is very useful to change them easily (I use the same routine with a different layer name to change to other layers I use).

This is great for when I open someone else's drawing but when I am using it to change layers in my normal draughting it always requires me to run the routine first before I can select any entities.

How can I adjust this routine so that I can select the entities first and then run the routine?

I am no lisp expert, so I appreciate this routine could be an awkward way to achieve what I am asking but it serves the purpose I require.

If there is an easier way to write this so I can create layers (if missing) and change layers and have everything set to bylayer then I am open to suggestions.

Code: [Select]
; Changes selected objects to Layer Crease
(defun c:CreaseV2 (/ CNT DXDT DXFDNEW DXFNEW DXFOLD ENT SSET )
  (command "_.Layer" "_Make" "Crease" "_Color" "3" "" "_LType" "Continuous" "" "")
  (setq sset (ssget))
  (setq cnt 0)
  (repeat (sslength sset)
    (setq ent (ssname sset cnt))
    (setq dxdt (entget ent))
    (setq dxfold (assoc 8 dxdt))
    (setq dxfnew '(8 . "Crease"))
    (setq dxfdnew (subst dxfnew dxfold dxdt))
    (entmod dxfdnew)
    (setq cnt (1+ cnt))
  )
(command "CHPROP" sset "" "COLOR" "BYLAYER" "LTYPE" "BYLAYER" "LW" "BYLAYER" "")
  (princ)
)

I am running Bricscad V17 Pro at present.

Many thanks in advance.
Title: Re: Change Entities to new layer
Post by: kdub_nz on November 28, 2018, 05:00:01 AM
It's amusing how we see a challenge !!

Instead of resolving the technical issue you have, I chose to find the original (perhapa) source.

If you want to know how I found it just ask.

I wouuldn't be surprised it the "original" thread has hints to solving your issues.

https://www.cadtutor.net/forum/topic/62101-set-by-layer/

Regards


added: a reasonable resolution to the "missing source link" is to add attributions as a comment to the file when you save/copy it.
Title: Re: Change Entities to new layer
Post by: ribarm on November 28, 2018, 09:17:47 AM
I suppose, you can change this line :
Code: [Select]
(setq sset (ssget))
To this :
Code: [Select]
(setq sset (ssget "_:L-I"))

HTH., M.R.
Title: Re: Change Entities to new layer
Post by: Dahzee on November 29, 2018, 04:55:59 AM
Thanks kdub for finding the original thread, I will go through the posts and see if anything makes any sense to me!

Hi ribarm, I changed the line you indicated but this made no difference to the routine, it still unselects what I have already selected and then asks me to select objects.

Thanks for the replies.
Title: Re: Change Entities to new layer
Post by: Tharwat on November 29, 2018, 06:29:10 AM
Hi Dahzee,

As marko stated the mode string of the ssget function so here is a new way of writing the codes without the use of command calls and shorter way to do such a simple task with respect to the same variable names in the above posted codes by you.
EDIT: {
NOTE: maybe the mode string did not work for you due to the use of Bricscad program but it still a guess until you run the following codes.
}
Code - Auto/Visual Lisp: [Select]
  1. (defun c:CreaseV2 (/ sset cnt dxdt)
  2.   (or (tblsearch "LAYER" "Crease")
  3.       (entmake '((0 . "LAYER") (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "Crease") (62 . 3) (6 . "Continuous") (70 . 0)))
  4.       )
  5.  
  6.   (if (or (setq sset (ssget "_:L-I"  '((0 . "~VIEWPORT"))))
  7.           (setq sset (ssget "_:L" '((0 . "~VIEWPORT"))))
  8.           )
  9.     (repeat (setq cnt (sslength sset))
  10.       (setq dxdt (entget (ssname sset (setq cnt (1- cnt)))))
  11.       (entmod (subst '(8 . "Crease") (assoc 8 dxdt) dxdt))
  12.       )
  13.     )
  14.   (princ)
  15. )
  16.  
Title: Re: Change Entities to new layer
Post by: roy_043 on November 29, 2018, 09:27:45 AM
Ribarm's mod won't work because of the command call on the previous line which cancels the active (implied) selection. Switching lines 3 and 4 solves this.
Code - Auto/Visual Lisp: [Select]
  1. ; Changes selected objects to Layer Crease
  2. (defun c:CreaseV2 (/ CNT DXDT DXFDNEW DXFNEW DXFOLD ENT SSET )
  3.   (setq sset (ssget "_:L-I"))
  4.   (command "_.Layer" "_Make" "Crease" "_Color" "3" "" "_LType" "Continuous" "" "")
  5.   (setq cnt 0)
  6.   (repeat (sslength sset)
  7.     (setq ent (ssname sset cnt))
  8.     (setq dxdt (entget ent))
  9.     (setq dxfold (assoc 8 dxdt))
  10.     (setq dxfnew '(8 . "Crease"))
  11.     (setq dxfdnew (subst dxfnew dxfold dxdt))
  12.     (entmod dxfdnew)
  13.     (setq cnt (1+ cnt))
  14.   )
  15.   (command "CHPROP" sset "" "COLOR" "BYLAYER" "LTYPE" "BYLAYER" "LW" "BYLAYER" "")
  16.   (princ)
  17. )

An improved version using command calls:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:CreaseV3 ( / sset)
  2.   (setvar "cmdecho" 0)
  3.   (if (setq sset (ssget "_:L-I"))
  4.     (command
  5.       "_.undo" "_end"
  6.       "_.undo" "_group"
  7.       "_.layer" "_make" "Crease" "_color" "3" "" "_ltype" "Continuous" "" ""
  8.       "_.chprop" sset "" "_layer" "Crease" "_color" "_ByLayer" "_ltype" "_ByLayer" "_lw" "_ByLayer" ""
  9.       "_.undo" "_end"
  10.     )
  11.   )
  12.   (setvar "cmdecho" 1)
  13.   (princ)
  14. )
Title: Re: Change Entities to new layer
Post by: Dahzee on November 29, 2018, 11:16:12 AM
@Tharwat, thanks for the routine, it worked with a preselection, but didn't turn the lineweight to bylayer.

@Roy, thanks for your suggestion, it works exactly as I need.

Thanks for everybody's help.
Title: Re: Change Entities to new layer
Post by: Tharwat on November 29, 2018, 11:34:32 AM
@Tharwat, thanks for the routine, it worked with a preselection, but didn't turn the lineweight to bylayer.
Sorry, I forgot about that.
Just replace the entire line for the entmod function with the following:
Code - Auto/Visual Lisp: [Select]
  1. (entmod (append dxdt '((8 . "Crease") (62 . 256) (6 . "ByLayer") (370 . -1))))
  2.