Author Topic: need help with this new tool  (Read 3662 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
need help with this new tool
« on: November 02, 2004, 07:24:44 PM »
i want to make a lisp command that will change my layer by just typing

"1" and then "enter" to change a selected entity to a layer called "S-1"
and when i enter "2" it will change the selected layer to "S-2" and so forth.
it will be done all the way up to "S-9" i think i can do the rest but i dont know how to get the first thing working because i probably dont know the variable.

so far i got this, which is not much

Code: [Select]

(DEFUN C:1 () ^C(COMMAND "CHANGE"))


i know there are other variables but this part of code was from another lsp file. help me please

SMadsen

  • Guest
need help with this new tool
« Reply #1 on: November 03, 2004, 06:28:37 AM »
Long time ago I put a generic layer change function into a menu lisp and created a series of DEFUNs for the most frequently used standard layers.
It goes something like this:

Code: [Select]
(defun chlay (str / cmde sset)
  (setq cmde (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (cond ((not (tblsearch "LAYER" str))
         (prompt
           (strcat "\nLayer not found: " str "\n*Invalid*")
         )
        )
        ((or (setq sset (ssget "I")) (setq sset (ssget)))
         (princ (strcat "Placing objects on layer " str "..."))
         (command "CHPROP" sset "" "LA" str "")
         (princ (strcat "\n" (itoa (sslength sset))
                        " in selection changed to layer " str)
         )
        )
  )
  (setvar "CMDECHO" cmde)
  (princ)
)

;;; Layer standard stuff
(defun C:A21 () (chlay "A21----"))
(defun C:A22 () (chlay "A22-L--"))
(defun C:A24 () (chlay "A24----"))
(defun C:A32 () (chlay "A321---"))
;; ....etcetera


I still use it and still get annoyed when it doesn't create the layer if it doesn't exists .. ummm, now that it's open I just might correct that?

Anyway, is that something you could use?

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
need help with this new tool
« Reply #2 on: November 03, 2004, 07:39:49 AM »
Probably more than you need at this time.
Code: [Select]

(defun c:1 (/ ent entlst)

  ;;; changes the selected entity to layer 'S-1'.
  ;;; layer 'S-1' must exist in the dwg.
  ;;;
  ;;; version 1.0 Wed Nov 03, 2004
  ;;;
  ;;; you are free to use this code as you see fit
  ;;;
  ;;; Mark S. Thomas

  ;; first make sure the layer exists in the dwg
  ;; if is does not we inform the user below on line 52
  (if (tblsearch "layer" "S-1"); <- test

    ;; next make sure we get an entity selection
    ;; if the user picks nothing the rest will not be evaulated
    (if (setq ent (car (entsel "\nSelect entity to change: "))); <- test

      ;; now check the entities layer, if it's not 's-1' then we move
      ;; down to line 44 and inform the user of such
      ;; the dxf group code for layer is 8
      (if (not (= (cdr (assoc 8 (entget ent))) "S-1")); <- test

        ;; we use 'progn' because we have more than one expression
        ;; if our test is true
        (progn

          (setq entlst (entget ent))

          (entmod                ; modify the selected entity list

            (subst               ; substitute
              (cons 8 "S-1")     ; construct a list of 8 and "S-1" (8 . "S-1")
              (assoc 8 entlst)   ; retrieve the current value of 8
              entlst             ; the entity list to work on
              )
            )

          ); end of 'progn'

        ;; else
        ;; if entity is already on layer 'S-1' we inform the user
        (princ "\nSelected entity is already on layer 'S-1'")

        ); end of 3rd 'if'

      ); end of 2nd 'if'

    ;; else
    ;; if layer does not exist in the dwg we inform the user
    (princ "\nLayer S-1 does not exist in this dwg")

    ); end of 1st 'if

  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
need help with this new tool
« Reply #3 on: November 03, 2004, 08:23:25 AM »
I got the impression that he wanted to Set Current?
Code: [Select]
;;  Set current or Create
(defun LayerSetCurrent(LayerName LayerColor)
;;  expects var LayerColor
(if (null LayerColor)
  (setq LayerColor 7)
)
 (if (tblsearch "Layer" LayerName)
  (command "._layer" "_thaw" LayerName "_on"
  LayerName "_unlock" LayerName "_set"
  LayerName ""
  ) ;_ closes command
  (command "._layer" "_make" LayerName "_color"
  LayerColor LayerName ""
  ) ;_ closes command
 )
)

(defun c:1 () (LayerSetCurrent "S1" 7))
(defun c:2 () (LayerSetCurrent "S2" 7))
(defun c:3 () (LayerSetCurrent "S3" 7))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SMadsen

  • Guest
Re: need help with this new tool
« Reply #4 on: November 03, 2004, 08:25:22 AM »
Quote from: dubb
... that will change my layer ... and then "enter" to change a selected entity to a layer ...

Perhaps both?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
need help with this new tool
« Reply #5 on: November 03, 2004, 09:10:15 AM »
Combining the two routines.
Code: [Select]
(defun chlay (str clr / cmde sset ans)
  (setq cmde (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  ;;  get selection set first
  (if (setq sset (ssget "I"))
    (progn
      (layersetcurrent str clr)
      (initget "Yes No")
      (setq ans
             (getkword
               (strcat "\nChange selected to layer "
                       str " [Yes/No]<Yes>")
             )
      )
      (if (/= ans "No")
        (progn
          (princ (strcat "\nPlacing objects on layer " str "..."))
          (command "CHPROP" sset "" "LA" str "")
          (princ (strcat "\n"
                         (itoa (sslength sset))
                         " in selection changed to layer "
                         str
                 )
          )
        )
      )
    )
    ;;  ELSE just change/make layer current
    (layersetcurrent str clr)
  )
  (princ (strcat "\nCurrent layer change to " str "."))
  (setvar "CMDECHO" cmde)
  (princ)
)

;;  Set current or Create
(defun layersetcurrent (layername layercolor)
  ;;  expects var LayerColor
  (if (null layercolor)
    (setq layercolor 7)
  )
  (if (= (type layercolor) 'INT)
    (setq layercolor (itoa layercolor))
  )
  (if (tblsearch "Layer" layername)
    (command "._layer" "_thaw" layername "_on" layername "_unlock"
             layername "_set" layername "") ;_ closes command
    (command "._layer" "_make" layername "_color" layercolor
             layername "") ;_ closes command
  )
)

;;; Layer change calls
(defun c:1 () (chlay "S1" 7))
(defun c:2 () (chlay "S2" 7))
(defun c:3 () (chlay "S3" 7))
(defun c:4 () (chlay "S4" 7))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

dubb

  • Swamp Rat
  • Posts: 1105
need help with this new tool
« Reply #6 on: November 04, 2004, 11:09:26 AM »
i am very impressed by the responds. i can use all of these types of examples. thanks for helping me with this tool. it will make work a lil faster. :D

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
need help with this new tool
« Reply #7 on: November 04, 2004, 11:21:35 AM »
Do you understand any of the code?
TheSwamp.org  (serving the CAD community since 2003)

dubb

  • Swamp Rat
  • Posts: 1105
need help with this new tool
« Reply #8 on: November 04, 2004, 11:28:56 AM »
let see i understand "defun" "cmdecho" "setq" "getvar" "command" "\n" and that i need to compliment " ( " with " ) " in order to have complete routine...but i just cant get all the pieces working togehter because i dont understand where they belong and the rules. but i have made a box routine before. does that count? and im gonna make some changes to the code provided for me so that i can apply it towards my standards.