Author Topic: how can i keep settings  (Read 2478 times)

0 Members and 1 Guest are viewing this topic.

masao

  • Newt
  • Posts: 82
how can i keep settings
« on: December 04, 2023, 09:44:27 AM »
my cad is 2012 and i use dimcenter must set layer and color.

i have a question,if i missed circle select setting would back(i have *error*code so i missed select would enter *error*code)

if must make a code by yourself, i cant get "circle or arc on block" center point.

Code: [Select]
(defun C:CTR (/ e_lst ocolor oltype odimcen)

(setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cecolor" "celtype" "dimcen")) )

(defun *error* (msg)

  (mapcar 'eval e_lst)

  (princ "")

)

(setvar "cmdecho" 0)

 (if (= (tblsearch "ltype" "center") nil)

 (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")

 )

 (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "_yes" "")

(while

(setq ocolor (getvar "cecolor")
oltype (getvar "celtype")
odimcen (getvar "dimcen")
  )

(setvar "cecolor" "1")

(setvar "celtype" "CENTER")

(setvar "dimcen" -2)

(command "dimcenter" pause)

  (setvar "cecolor" ocolor)
  (setvar "celtype" oltype)
  (setvar "dimcen" odimcen)

);while

(princ)
)
(princ)

mhupp

  • Bull Frog
  • Posts: 250
Re: how can i keep settings
« Reply #1 on: December 05, 2023, 08:04:19 AM »
Here is a simple trick to store, set, and recall a list of system variables. (probably found on here)

Code - Auto/Visual Lisp: [Select]
  1. (defun C:CTR (/ vars vals)
  2.   (setq vars '("cecolor" "celtype" "dimcen")   ;list of variables
  3.         vals (mapcar 'getvar vars)             ;store old values in a list called vals
  4.   )
  5.   (mapcar 'setvar vars '(1 "CENTER" -2))       ;set new values
  6.   (setvar "cmdecho" 0)
  7.   (if (= (tblsearch "ltype" "center") nil)
  8.     (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  9.   )
  10.   (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "_yes" "")
  11.   (command "dimcenter")
  12.   (while (> (getvar 'cmdactive) 0) (vl-cmdf "\\")) ;waits for dimcenter command to finish
  13.   (mapcar 'setvar vars vals) ;sets all values back to before
  14.   (princ)
  15. )

masao

  • Newt
  • Posts: 82
Re: how can i keep settings
« Reply #2 on: December 05, 2023, 09:10:59 AM »
Here is a simple trick to store, set, and recall a list of system variables. (probably found on here)

Code - Auto/Visual Lisp: [Select]
  1. (defun C:CTR (/ vars vals)
  2.   (setq vars '("cecolor" "celtype" "dimcen")   ;list of variables
  3.         vals (mapcar 'getvar vars)             ;store old values in a list called vals
  4.   )
  5.   (mapcar 'setvar vars '(1 "CENTER" -2))       ;set new values
  6.   (setvar "cmdecho" 0)
  7.   (if (= (tblsearch "ltype" "center") nil)
  8.     (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  9.   )
  10.   (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "_yes" "")
  11.   (command "dimcenter")
  12.   (while (> (getvar 'cmdactive) 0) (vl-cmdf "\\")) ;waits for dimcenter command to finish
  13.   (mapcar 'setvar vars vals) ;sets all values back to before
  14.   (princ)
  15. )

thank you ,but can not use while loop.

if use while can not end loop.
« Last Edit: December 05, 2023, 09:17:20 AM by masao »

JohnK

  • Administrator
  • Seagull
  • Posts: 10552
Re: how can i keep settings
« Reply #3 on: December 05, 2023, 09:37:42 AM »
Your error handler will restore the variables so all you have to do is invoke the error handler (which you can do with `QUIT`).

I have highlighted the lines that I changed.
Code - Auto/Visual Lisp: [Select]
  1. (defun C:CTR (/ e_lst ocolor oltype odimcen
  2.                 *error*)
  3.   (defun *error* (msg)
  4.     (mapcar 'eval e_lst)
  5.     (princ "")
  6.     )
  7.   (setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cecolor" "celtype" "dimcen")) )
  8.  
  9.   (setvar "cmdecho" 0)
  10.   (if (= (tblsearch "ltype" "center") nil)
  11.     (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  12.     )
  13.  
  14.   (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "_yes" "")
  15.   (while
  16.     (setq ocolor (getvar "cecolor")
  17.           oltype (getvar "celtype")
  18.           odimcen (getvar "dimcen"))
  19.     (setvar "cecolor" "1")
  20.     (setvar "celtype" "CENTER")
  21.     (setvar "dimcen" -2)
  22.  
  23.     (command "dimcenter" pause)
  24.  
  25.     (setvar "cecolor" ocolor)
  26.     (setvar "celtype" oltype)
  27.     (setvar "dimcen" odimcen)
  28.  
  29.     );while
  30.   (quit)
  31.   (princ)
  32.   )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

masao

  • Newt
  • Posts: 82
Re: how can i keep settings
« Reply #4 on: December 05, 2023, 09:53:58 AM »
Your error handler will restore the variables so all you have to do is invoke the error handler (which you can do with `QUIT`).

I have highlighted the lines that I changed.
Code - Auto/Visual Lisp: [Select]
  1. (defun C:CTR (/ e_lst ocolor oltype odimcen
  2.                 *error*)
  3.   (defun *error* (msg)
  4.     (mapcar 'eval e_lst)
  5.     (princ "")
  6.     )
  7.   (setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cecolor" "celtype" "dimcen")) )
  8.  
  9.   (setvar "cmdecho" 0)
  10.   (if (= (tblsearch "ltype" "center") nil)
  11.     (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  12.     )
  13.  
  14.   (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "_yes" "")
  15.   (while
  16.     (setq ocolor (getvar "cecolor")
  17.           oltype (getvar "celtype")
  18.           odimcen (getvar "dimcen"))
  19.     (setvar "cecolor" "1")
  20.     (setvar "celtype" "CENTER")
  21.     (setvar "dimcen" -2)
  22.  
  23.     (command "dimcenter" pause)
  24.  
  25.     (setvar "cecolor" ocolor)
  26.     (setvar "celtype" oltype)
  27.     (setvar "dimcen" odimcen)
  28.  
  29.     );while
  30.   (quit)
  31.   (princ)
  32.   )

sorry ,this code has same result.

if i use CTR and missed click ,color and linetype has change old setting.

mhupp post code can prevent it,but can not use while to loop.

like cad code "*^C^C_dimcenter" can loop and missed click has not change old setting.

PKENEWELL

  • Bull Frog
  • Posts: 308
Re: how can i keep settings
« Reply #5 on: December 05, 2023, 10:58:31 AM »
I was thinking this perhaps?

Let me know if this works on your version of AutoCAD. I am having trouble getting it to work on my system however and I don't know why; it has something to do with the "DIMCENTER" command itself:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:DCEN (/ i ss vals vars *error*)
  2.    
  3.    (defun *error* (msg)
  4.       (mapcar 'setvar vars vals)
  5.       (princ msg)
  6.    )
  7.    
  8.    (setq vars '("cmdecho" "cecolor" "celtype" "dimcen")
  9.          vals  (mapcar 'getvar vars)
  10.    )
  11.  
  12.    (setvar "cmdecho" 0)
  13.  
  14.    (if (= (tblsearch "ltype" "center") nil)
  15.      (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  16.    )
  17.    
  18.    (princ "\nSelect Arcs or Circles: ")
  19.    (if (setq ss (ssget '((0 . "ARC,CIRCLE"))))
  20.       (progn
  21.          (mapcar 'setvar (cdr vars) '("1" "CENTER" -2))
  22.          (repeat (setq i (sslength ss))
  23.             (command "._dimcenter" (ssname ss (setq i (1- i))))
  24.          )
  25.          (mapcar 'setvar vars vals)
  26.       )
  27.    )
  28.    (princ)
  29. )
  30.  
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

JohnK

  • Administrator
  • Seagull
  • Posts: 10552
Re: how can i keep settings
« Reply #6 on: December 05, 2023, 11:19:33 AM »
sorry ,this code has same result.

if i use CTR and missed click ,color and linetype has change old setting.

--->%

Not sure what you mean, if you mis-pick the error hander should restore the variable values to their original settings. You do not want the variables to be restored to their original state?

I did take another quick run though of the code and I cleaned out a few redundant areas. For example: there is not a need to set and reset variables in a while loop, you can just loop for the selection.

Code - Auto/Visual Lisp: [Select]
  1. (defun C:CTR (/ e_lst ocolor oltype odimcen
  2.                 *error*)
  3.   (defun *error* (msg)
  4.     (mapcar 'eval e_lst)
  5.     (princ "")
  6.     )
  7.  
  8.   (setq e_lst
  9.         (mapcar
  10.           (function
  11.             (lambda (n)
  12.               (list 'setvar n (getvar n))))
  13.           '("cmdecho"
  14.             "cecolor"
  15.             "celtype"
  16.             "dimcen")) )
  17.  
  18.   (if (= (tblsearch "ltype" "center") nil)
  19.     (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  20.     )
  21.  
  22.   (setvar "cmdecho" 0)
  23.   (setvar "cecolor" "1")
  24.   (setvar "celtype" "CENTER")
  25.   (setvar "dimcen" -2)
  26.  
  27.   (while (setq ent (entsel))
  28.          (command "dimcenter" ent)
  29.          )
  30.   (quit)
  31.   (princ)
  32. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PKENEWELL

  • Bull Frog
  • Posts: 308
Re: how can i keep settings
« Reply #7 on: December 05, 2023, 11:57:10 AM »
OK - It seems the DIMCENTER command doesn't like the (ssname ss n) input, so I altered this version to work like John's:

P.S. this version prevents missed picks from exiting the command.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:DCEN (/ i ent vals vars _Entsel *error*)
  2.    
  3.    (defun *error* (msg)
  4.       (mapcar 'setvar vars vals)
  5.       (princ msg)
  6.    )
  7.  
  8.    (defun _Entsel (pr / ent)
  9.       (setvar "errno" 0)
  10.         (while (and (not (setq ent (entsel pr)))(= (getvar "errno") 7))
  11.                 (princ "\nNo Object Selected. Try Again...\n")
  12.         )
  13.         ent
  14.    )
  15.    
  16.    (setq vars '("cmdecho" "cecolor" "celtype" "dimcen")
  17.          vals  (mapcar 'getvar vars)
  18.    )
  19.  
  20.    (setvar "cmdecho" 0)
  21.  
  22.    (if (= (tblsearch "ltype" "center") nil)
  23.      (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  24.    )
  25.    
  26.    (mapcar 'setvar (cdr vars) '("1" "CENTER" -2))
  27.  
  28.    (while (setq ent (_Entsel "\nSelect Circles or Arcs: "))(command "._dimcenter" ent))
  29.  
  30.    (mapcar 'setvar vars vals)
  31.    (princ)
  32. )
  33.  
« Last Edit: December 05, 2023, 12:11:26 PM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

ribarm

  • Gator
  • Posts: 3180
  • Marko Ribar, architect
Re: how can i keep settings
« Reply #8 on: December 05, 2023, 12:38:49 PM »
These 2 lines at the end :

(mapcar 'setvar vars vals)
(princ)

should just be :

(*error* nil)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

masao

  • Newt
  • Posts: 82
Re: how can i keep settings
« Reply #9 on: December 06, 2023, 06:38:47 AM »
OK - It seems the DIMCENTER command doesn't like the (ssname ss n) input, so I altered this version to work like John's:

P.S. this version prevents missed picks from exiting the command.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:DCEN (/ i ent vals vars _Entsel *error*)
  2.    
  3.    (defun *error* (msg)
  4.       (mapcar 'setvar vars vals)
  5.       (princ msg)
  6.    )
  7.  
  8.    (defun _Entsel (pr / ent)
  9.       (setvar "errno" 0)
  10.         (while (and (not (setq ent (entsel pr)))(= (getvar "errno") 7))
  11.                 (princ "\nNo Object Selected. Try Again...\n")
  12.         )
  13.         ent
  14.    )
  15.    
  16.    (setq vars '("cmdecho" "cecolor" "celtype" "dimcen")
  17.          vals  (mapcar 'getvar vars)
  18.    )
  19.  
  20.    (setvar "cmdecho" 0)
  21.  
  22.    (if (= (tblsearch "ltype" "center") nil)
  23.      (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  24.    )
  25.    
  26.    (mapcar 'setvar (cdr vars) '("1" "CENTER" -2))
  27.  
  28.    (while (setq ent (_Entsel "\nSelect Circles or Arcs: "))(command "._dimcenter" ent))
  29.  
  30.    (mapcar 'setvar vars vals)
  31.    (princ)
  32. )
  33.  

thank you,i know can use entsel but if circle on block can not get info.

so i use pause,but click missed has go to error code.

cad "*^C^C_dimcenter " ← how to doing loop like this?

PKENEWELL

  • Bull Frog
  • Posts: 308
Re: how can i keep settings
« Reply #10 on: December 06, 2023, 10:29:32 AM »

thank you,i know can use entsel but if circle on block can not get info.

so i use pause,but click missed has go to error code.

cad "*^C^C_dimcenter " ← how to doing loop like this?

Sorry - what your asking for cannot be done with the dimcenter command in a LISP as far as I can tell. I've tested it using (nentsel) to see the circle entity in the block, but it doesn't work. The "*^C^C_dimcenter " is a menu/toolbar macro and only works within that context - I recommend you stick with that. Better yet - switch to a custom centerline program such as:
http://www.lee-mac.com/centreline.html

P.S. if you upgrade your AutoCAD, it now has a built-in command called CENTERMARK that works better then dimcenter.

"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

masao

  • Newt
  • Posts: 82
Re: how can i keep settings
« Reply #11 on: December 06, 2023, 10:46:16 AM »

thank you,i know can use entsel but if circle on block can not get info.

so i use pause,but click missed has go to error code.

cad "*^C^C_dimcenter " ← how to doing loop like this?

Sorry - what your asking for cannot be done with the dimcenter command in a LISP as far as I can tell. I've tested it using (nentsel) to see the circle entity in the block, but it doesn't work. The "*^C^C_dimcenter " is a menu/toolbar macro and only works within that context - I recommend you stick with that. Better yet - switch to a custom centerline program such as:
http://www.lee-mac.com/centreline.html

P.S. if you upgrade your AutoCAD, it now has a built-in command called CENTERMARK that works better then dimcenter.

thank you ,but i use cad2012.

i just want to know how to like this loop "*^C^C_dimcenter " by autolisp.

but can not do it by autolisp.


JohnK

  • Administrator
  • Seagull
  • Posts: 10552
Re: how can i keep settings
« Reply #12 on: December 06, 2023, 04:42:17 PM »
Sorry, just passing through but what is the goal?

Concept code below (nentsel finding circle in block):
Code - Auto/Visual Lisp: [Select]
  1. (defun getentsel ( )
  2.   ;; Prompts for an entity selection.
  3.   ;; If there is already object selected, then returns the first
  4.   ;; item in the selection set.
  5.   ;;
  6.   ;; EX: (getentsel)
  7.   ;; RETURNS: ent
  8.   (cond
  9.     ((cadr (ssgetfirst)) (ssname (cadr (ssgetfirst)) 0))
  10.     ((car (nentsel)))) )
  11. (command "._point" (cdr (assoc 10 (entget (getentsel)))))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

masao

  • Newt
  • Posts: 82
Re: how can i keep settings
« Reply #13 on: December 06, 2023, 06:25:29 PM »
Sorry, just passing through but what is the goal?

Concept code below (nentsel finding circle in block):
Code - Auto/Visual Lisp: [Select]
  1. (defun getentsel ( )
  2.   ;; Prompts for an entity selection.
  3.   ;; If there is already object selected, then returns the first
  4.   ;; item in the selection set.
  5.   ;;
  6.   ;; EX: (getentsel)
  7.   ;; RETURNS: ent
  8.   (cond
  9.     ((cadr (ssgetfirst)) (ssname (cadr (ssgetfirst)) 0))
  10.     ((car (nentsel)))) )
  11. (command "._point" (cdr (assoc 10 (entget (getentsel)))))

Hi,I just want to change setting and loop dimcenter.

but use lisp can not like "*^C^C_dimcenter "  use on “circle” “arc” “circle or arc on block” and loop.

masao

  • Newt
  • Posts: 82
Re: how can i keep settings
« Reply #14 on: December 10, 2023, 12:43:18 AM »
Here is a simple trick to store, set, and recall a list of system variables. (probably found on here)

Code - Auto/Visual Lisp: [Select]
  1. (defun C:CTR (/ vars vals)
  2.   (setq vars '("cecolor" "celtype" "dimcen")   ;list of variables
  3.         vals (mapcar 'getvar vars)             ;store old values in a list called vals
  4.   )
  5.   (mapcar 'setvar vars '(1 "CENTER" -2))       ;set new values
  6.   (setvar "cmdecho" 0)
  7.   (if (= (tblsearch "ltype" "center") nil)
  8.     (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "")
  9.   )
  10.   (vl-cmdf "_.-linetype" "_load" "center" "acadiso.lin" "_yes" "")
  11.   (command "dimcenter")
  12.   (while (> (getvar 'cmdactive) 0) (vl-cmdf "\\")) ;waits for dimcenter command to finish
  13.   (mapcar 'setvar vars vals) ;sets all values back to before
  14.   (princ)
  15. )

If your code use while loop,how can I use ESC end loop?