Author Topic: The same list, again and again and again....  (Read 2173 times)

0 Members and 1 Guest are viewing this topic.

Paul Munford

  • Guest
The same list, again and again and again....
« on: January 28, 2008, 03:39:57 AM »
This routine works for me, but it strikes me that I am using the same information again and again and again...

I have explored 'apply', 'foreach' and 'mapcar' but I can't see a clear way of applying them to the code.

I would welcome any tips on making the code more efficient.

Code: [Select]


(defun C:dimnudge ()

(initget  "1 2 5 10 20 25 50 100 U D") ;set key words
(setq input ; get keywords
    (getkword "Enter Dimscale or Nudge up/down [1/2/5/10/20/25/50/100 U/D]: "))
 
(cond ;convert string to real
  ((= input "1")(setq dimsc (atof input)))
((= input "2")(setq dimsc (atof input)))
((= input "5")(setq dimsc (atof input)))
((= input "10")(setq dimsc (atof input)))
((= input "20")(setq dimsc (atof input)))
((= input "25")(setq dimsc (atof input)))
((= input "50")(setq dimsc (atof input)))
((= input "100")(setq dimsc (atof input)))
((= input "U") (c:dsup)) ;run command dimscale up
  ((= input "D") (c:dsdown)) ;run command dimscale down
);end cond

(cond ;set dimscale and mode macro to value
  ((= dimsc 1.0) (setvar "dimscale" 1.0)(setvar "modemacro" "\nDIMSCALE 1:1"))
  ((= dimsc 2.0) (setvar "dimscale" 2.0)(setvar "modemacro" "\nDIMSCALE 1:2"))
  ((= dimsc 5.0) (setvar "dimscale" 5.0)(setvar "modemacro" "\nDIMSCALE 1:5"))
  ((= dimsc 10.0) (setvar "dimscale" 10.0)(setvar "modemacro" "\nDIMSCALE 1:10"))
  ((= dimsc 20.0) (setvar "dimscale" 20.0)(setvar "modemacro" "\nDIMSCALE 1:20"))
  ((= dimsc 25.0) (setvar "dimscale" 25.0)(setvar "modemacro" "\nDIMSCALE 1:25"))
  ((= dimsc 50.0) (setvar "dimscale" 50.0)(setvar "modemacro" "\nDIMSCALE 1:50"))
  ((= dimsc 100.0) (setvar "dimscale" 100.0)(setvar "modemacro" "\nDIMSCALE 1:100"))
);end cond
 
  );end dimnudge

;build list

(defun C:dsd()

(setq dimsc (getvar "dimscale")) ;get current dimscale

(cond ;set a marker
  ((= dimsc 1.0) (setq nu 1))
  ((= dimsc 2.0) (setq nu 2))
  ((= dimsc 5.0) (setq nu 3))
  ((= dimsc 10.0) (setq nu 4))
  ((= dimsc 20.0) (setq nu 5))
  ((= dimsc 25.0) (setq nu 6))
  ((= dimsc 50.0) (setq nu 7))
  ((= dimsc 100.0) (setq nu 8))
);end cond

(setq nu (- nu 1)) ;take one away from the marker
 
(cond ;set dimscale and modemacro to the new value
  ((<= nu 0) (setvar "dimscale" 1.0)(princ "\nDimscale 1:1") (setvar "modemacro" "\nDIMSCALE 1:1"))
  ((= nu 1) (setvar "dimscale" 1.0)(setvar "modemacro" "\nDIMSCALE 1:1"))
  ((= nu 2) (setvar "dimscale" 2.0)(setvar "modemacro" "\nDIMSCALE 1:2"))
  ((= nu 3) (setvar "dimscale" 5.0)(setvar "modemacro" "\nDIMSCALE 1:5"))
  ((= nu 4) (setvar "dimscale" 10.0)(setvar "modemacro" "\nDIMSCALE 1:10"))
  ((= nu 5) (setvar "dimscale" 20.0)(setvar "modemacro" "\nDIMSCALE 1:20"))
  ((= nu 6) (setvar "dimscale" 25.0)(setvar "modemacro" "\nDIMSCALE 1:25"))
  ((= nu 7) (setvar "dimscale" 50.0)(setvar "modemacro" "\nDIMSCALE 1:50"))
  ((= nu 8) (setvar "dimscale" 100.0)(setvar "modemacro" "\nDIMSCALE 1:100"))
  ((>= nu 9)(setvar "dimscale" 100.0)(princ "\nDimscale 1:100") (setvar "modemacro" "\nDIMSCALE 1:100"))
);end cond
);end defun

(defun C:dsu()

(setq dimsc (getvar "dimscale")) ;get the current dimscale

(cond ;set a marker
  ((= dimsc 1.0) (setq nu 1))
  ((= dimsc 2.0) (setq nu 2))
  ((= dimsc 5.0) (setq nu 3))
  ((= dimsc 10.0) (setq nu 4))
  ((= dimsc 20.0) (setq nu 5))
  ((= dimsc 25.0) (setq nu 6))
  ((= dimsc 50.0) (setq nu 7))
  ((= dimsc 100.0) (setq nu 8))
);end cond

(setq nu (+ nu 1)) ;add one to the marker
 
(cond ;set dimscale and modemaco to the new value
  ((<= nu 0) (setvar "dimscale" 1.0)(princ "\nDimscale 1:1") (setvar "modemacro" "\nDIMSCALE 1:1"))
  ((= nu 1) (setvar "dimscale" 1.0)(setvar "modemacro" "\nDIMSCALE 1:1"))
  ((= nu 2) (setvar "dimscale" 2.0)(setvar "modemacro" "\nDIMSCALE 1:2"))
  ((= nu 3) (setvar "dimscale" 5.0)(setvar "modemacro" "\nDIMSCALE 1:5"))
  ((= nu 4) (setvar "dimscale" 10.0)(setvar "modemacro" "\nDIMSCALE 1:10"))
  ((= nu 5) (setvar "dimscale" 20.0)(setvar "modemacro" "\nDIMSCALE 1:20"))
  ((= nu 6) (setvar "dimscale" 25.0)(setvar "modemacro" "\nDIMSCALE 1:25"))
  ((= nu 7) (setvar "dimscale" 50.0)(setvar "modemacro" "\nDIMSCALE 1:50"))
  ((= nu 8) (setvar "dimscale" 100.0)(setvar "modemacro" "\nDIMSCALE 1:100"))
  ((>= nu 9)(setvar "dimscale" 100.0)(princ "\nDimscale 1:100") (setvar "modemacro" "\nDIMSCALE 1:100"))
);end cond
))

efernal

  • Bull Frog
  • Posts: 206
Re: The same list, again and again and again....
« Reply #1 on: January 28, 2008, 07:20:44 AM »
Code: [Select]
;; a possible code

(DEFUN c:dimnudge (/ lista input)
  (SETQ lista '("1" "2" "5" "10" "20" "25" "50" "100" "U" "D"))
  (INITGET "1 2 5 10 20 25 50 100 U D" 1)
  (SETQ input
         (GETKWORD
           "\nEnter Dimscale or Nudge up/down [1/2/5/10/20/25/50/100 U/D]: "
         )
  )
  (COND ((NUMBERP (READ input))
         (SETQ dims (ATOF input))
         (SETVAR "DIMSCALE" (ATOF input))
         (SETVAR "MODEMACRO" (STRCAT "Dimscale 1:" input))
         (PRINC "\nDimscale 1:")
         (PRINC input)
        )
        ((= input "U") (c:dsup))
        ((= input "D") (c:dsdown))
  )
  (PRINC)
)


(DEFUN c:dsd (/ n dimsc lista)
  (SETQ dimsc (GETVAR "dimscale")
        dimsc (ATOF (RTOS dimsc 2 1))
        lista '((0.0 0)
                (1.0 1)
                (2.0 2)
                (5.0 3)
                (10.0 4)
                (20.0 5)
                (25.0 6)
                (50.0 7)
                (100.0 8)
                (110.0 9)
               )
  )
  (COND ((< dimsc 1.0) (SETQ dimsc 1.0))
        ((> dimsc 100.0) (SETQ dimsc 100.0))
  )
  (IF (ASSOC dimsc lista)
    (PROGN (SETQ dimsc (CAR (NTH (1- (CADR (ASSOC dimsc lista))) lista)))
           (PRINC "\nDimscale = 1:")
           (PRINC dimsc)
           (SETVAR "MODEMACRO" (STRCAT "DIMSCALE 1:" (RTOS dimsc 2 2)))
    )
    (PRINC "\nValue not in list...")
  )
  (PRINC)
)

;; DSDOWN is similar to dsd...

e.fernal

efernal

  • Bull Frog
  • Posts: 206
Re: The same list, again and again and again....
« Reply #2 on: January 28, 2008, 07:33:07 AM »
Code: [Select]
;; fixing errors...
(DEFUN c:dimnudge (/ lista input)
  (SETQ lista '("1" "2" "5" "10" "20" "25" "50" "100" "U" "D"))
  (INITGET "1 2 5 10 20 25 50 100 U D" 1)
  (SETQ input
         (GETKWORD
           "\nEnter Dimscale or Nudge up/down [1/2/5/10/20/25/50/100 U/D]: "
         )
  )
  (COND ((NUMBERP (READ input))         
         (SETVAR "DIMSCALE" (ATOF input))
         (SETVAR "MODEMACRO" (STRCAT "Dimscale 1:" input))
         (PRINC "\nDimscale 1:")
         (PRINC input)
        )
        ((= input "U") (c:dsup))
        ((= input "D") (c:dsdown))
  )
  (PRINC)
)
;; changing the name...
(DEFUN c:dsdown (/ n dimsc lista)
  (SETQ dimsc (GETVAR "dimscale")
        dimsc (ATOF (RTOS dimsc 2 1))
        lista '((0.0 0)
                (1.0 1)
                (2.0 2)
                (5.0 3)
                (10.0 4)
                (20.0 5)
                (25.0 6)
                (50.0 7)
                (100.0 8)
                (110.0 9)
               )
  )
  (COND ((< dimsc 1.0) (SETQ dimsc 1.0))
        ((> dimsc 100.0) (SETQ dimsc 100.0))
  )
  (IF (ASSOC dimsc lista)
    (PROGN (SETQ dimsc (CAR (NTH (1- (CADR (ASSOC dimsc lista))) lista)))
           (IF (= dimsc 0.0)
             (SETQ dimsc 1.0)
           )
           (PRINC "\nDimscale = 1:")
           (PRINC dimsc)
           (SETVAR "DIMSCALE" dimsc)
           (SETVAR "MODEMACRO" (STRCAT "DIMSCALE 1:" (RTOS dimsc 2 2)))
    )
    (PRINC "\nValue not in list...")
  )
  (PRINC)
)

;; DSup is similar to dsdown...

[/codé]
e.fernal

SomeCallMeDave

  • Guest
Re: The same list, again and again and again....
« Reply #3 on: January 28, 2008, 07:52:35 AM »
For what it is worth,  here is my version (similar to efernal's)


But I pass one Master List to the the nudge functions  so it only has to be changed in one place is scales are added or removed.

Of course,  it needs testing, error catching, localized variable, etc.

Code: [Select]
(defun c:Paul()
   (initget  "1 2 5 10 20 25 50 100 U D") ;set key words
   (setq input (getkword "Enter Dimscale or Nudge up/down [1/2/5/10/20/25/50/100 U/D]: ")
         scaleList (list 1 2 5 10 20 25 50 100)
   
   );setq
   (setq dimsc (atoi input))
   (cond
      ((member dimsc scaleList)
       (progn
           (setq ModeMacroString (strcat "DIMSCALE 1:" input));setq
           (setvar "DIMSCALE" dimsc)
   (setvar "ModeMacro" ModeMacroString)
        );progn
      );
      ((= input "U")
         (nudgeUp (getvar "DIMSCALE") scaleList)
      );
      ((= input "D")
         (nudgeDn (getvar "DIMSCALE") scaleList)
      );               
   ); cond
   

)

(defun nudgeUp (pCurr pScaleList)
  (setq currPos (vl-position (fix pCurr) pScaleList)
        newPos (min (+ currPos 1) (- (length pScaleList) 1)) ;; don't run over the end of the list
        currScale (nth newPos pScaleList)
        ModeMacroString (strcat "DIMSCALE 1:" (rtos currScale 2 0))
  )
  (setvar "DIMSCALE" currScale)
  (setvar "ModeMacro" ModeMacroString)
);nudgeUp

(defun nudgeDn (pCurr pScaleList)
  (setq currPos (vl-position (fix pCurr) pScaleList)
        newPos (max (- currPos 1) 0) ;; don't run over the end of the list
        currScale (nth newPos pScaleList)
        ModeMacroString (strcat "DIMSCALE 1:" (rtos currScale 2 0))
  )
  (setvar "DIMSCALE" currScale)
  (setvar "ModeMacro" ModeMacroString)
);nudgeUp

efernal

  • Bull Frog
  • Posts: 206
Re: The same list, again and again and again....
« Reply #4 on: January 28, 2008, 09:35:56 AM »
Code: [Select]
;; here is another...
;; I prefer alloways the use of a dcl dialogue...

;; writting the dcl file necessary to function...
(IF (NOT (SETQ g::ef::001 (FINDFILE "EF_DIMNUDGE.dcl")))
  (PROGN (SETQ g::ef::002 '("dimscale:dialog{label=\"Dimscale\";key=\"efernal\";"
                            "width=80;fixed_width=true;"
                            ":text{label=\"\";key=\"dimscale\";alignment=centered;"
                            "width=40;fixed_width=true;}"
                            ":spacer{height=1;}"
                            ":column{alignment=centered;fixed_width=true;"
                            ":text{label=\"Select dimscale for 1:\";}"
                            ":radio_row{alignment=centered;fixed_width=true;"
                            ":radio_button{label=\"1\";key=\"1\";}"
                            ":radio_button{label=\"2\";key=\"2\";}"
                            ":radio_button{label=\"5\";key=\"5\";}"
                            ":radio_button{label=\"10\";key=\"10\";}"
                            ":radio_button{label=\"20\";key=\"20\";}"
                            ":radio_button{label=\"25\";key=\"25\";}"
                            ":radio_button{label=\"50\";key=\"50\";}"
                            ":radio_button{label=\"100\";key=\"100\";}}}"
                            ":spacer{height=1;}"
                            ":row{alignment=centered;fixed_width=true;height=3;"
                            "fixed_height=true;"
                            ":button{label=\"Como usar\";key=\"help\";width=16;"
                            "height=2;alignment=top;}"
                            ":button{label=\"Cancelar\";key=\"cancel\";is_cancel=true;"
                            "width=16;height=2;alignment=top;}"
                            ":button{label=\"Prosseguir\";key=\"accept\";is_default=true;"
                            "width=16;height=2;alignment=top;}}}"
                           )
               g::ef::003 (OPEN
                            (STRCAT (SUBSTR (FINDFILE "ACAD.EXE")
                                            1
                                            (- (STRLEN (FINDFILE "ACAD.EXE")) 8)
                                    )
                                    "EF_DIMNUDGE.dcl"
                            )
                            "w"
                          )
         )
         (FOREACH g::ef::004 g::ef::002 (PRINC g::ef::004 g::ef::003))
         (CLOSE g::ef::003)
         (SETQ g::ef::001 nil
               g::ef::002 nil
               g::ef::003 nil
               g::ef::004 nil
         )
  )
  nil
)
;;
;; the function
(DEFUN c:dimnudge (/ dh w@ msg current setup)
  (IF (> (SETQ dh (LOAD_DIALOG "EF_DIMNUDGE.dcl")) 0)
    (IF (NEW_DIALOG "dimscale" dh)
      (PROGN
        (SETQ msg "Write your help message here...")
        (SET_TILE "dimscale"
                  (STRCAT "Current dimscale is 1:"
                          (SETQ current (RTOS (GETVAR "DIMSCALE") 2 0))
                  )
        )
        (SET_TILE current "1")
        (FOREACH x '("1" "2" "5" "10" "20" "25" "50" "100")
          (ACTION_TILE x "(SETQ setup $key)")
        )
        (ACTION_TILE "help" "(ALERT msg)")
        (ACTION_TILE "cancel" "(DONE_DIALOG 0)")
        (ACTION_TILE "accept" "(DONE_DIALOG 1)")
        (SETQ w@ (START_DIALOG))
        (UNLOAD_DIALOG dh)
        (COND ((= w@ 0) (PRINC "\n-> Cancelled..."))
              ((AND setup (= w@ 1))
               (SETVAR "DIMSCALE" (ATOF setup))
               (SETVAR "MODEMACRO" (STRCAT "DIMSCALE 1:" setup))
              )
              (T nil)
        )
      )
      nil
    )
    (PRINC "\n-> Error loading dcl file...")
  )
  (PRINC)
)
;; by eduardo fernal, 01/28/2008 http://www.gr-acad.com.br

e.fernal

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: The same list, again and again and again....
« Reply #5 on: January 28, 2008, 11:25:41 AM »
Here is my version:
Code: [Select]
(defun C:dimnudge (/ input)
  ;;  You need to deal with a null response, so I added 1 to initget
  (initget 1 "1 2 5 10 20 25 50 100 U D") ;set key words
  (setq input       ; get keywords
         (getkword "Enter Dimscale or Nudge up/down [1/2/5/10/20/25/50/100/U/D]: ")
  ); added slash 100/U/D
  (cond 
    ((= (type (read input)) 'INT)
     (setvar "dimscale" (atof input))
     (setvar "modemacro" (strcat "\nDIMSCALE 1:" input))
    )
    ((= input "U") (dsNudge 1))
    ((= input "D") (dsNudge -1))
  )
  (princ)
)


(defun dsNudge (dir ; direction +1 or -1
                / sclist pos)
  (setq sclist '(1.0 2.0 5.0 10.0 20.0 25.0 50.0 100.0)
        pos    (vl-position (getvar "dimscale") sclist)
  )
  (cond
    ((null pos))    ; DimScale Not in list
    ((and (= dir 1) (= pos (1- (length sclist))))) ; at end of list
    ((and (= dir -1) (zerop pos))) ; at start of list
    (t
     (setvar "dimscale" (nth (+ dir pos) sclist))
     (setvar "modemacro"
             (strcat "\nDIMSCALE 1:" (rtos (nth (+ dir pos) sclist) 2 0))
     )
    )
  )
)
« Last Edit: January 28, 2008, 11:30:24 AM by CAB »
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.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: The same list, again and again and again....
« Reply #6 on: January 28, 2008, 11:42:21 AM »
and a piece of mine
Code: [Select]
(defun C:dimnudge (/ Input ScalesList DimScale OldDimScale)
  (setq ScalesList  '(1. 2. 5. 10. 20. 25. 50. 100.)
OldDimScale (getvar 'DIMSCALE)
  )
  (initget
    1
    (strcat (apply 'strcat
   (mapcar (function (lambda (i) (strcat (rtos i 2 0) " "))) ScalesList)
    )
    "U D"
    )
  )
  (setq Input
(getkword
   (strcat
     "Enter Dimscale or Nudge up/down ["
     (apply 'strcat
    (mapcar (function (lambda (i) (strcat (rtos i 2 0) "/"))) ScalesList)
     )
     "U/D]: "
   )
)
  )
  (or (setq
DimScale (cond ((= Input "U") (cadr (member OldDimScale ScalesList)))
       ((= Input "D") (cadr (member OldDimScale (reverse ScalesList))))
       (t (atof Input))
)
      )
      (setq DimScale OldDimScale)
  )
  (prompt
    (setvar 'MODEMACRO
    (strcat "DIMSCALE 1:" (rtos (setvar 'DIMSCALE DimScale) 2 0))
    )
  )
  (princ)
)


Paul Munford

  • Guest
Re: The same list, again and again and again....
« Reply #7 on: January 28, 2008, 03:59:24 PM »
Wow! What a great response! Thanks very much... Now I just have to break all these down to try and figure out how they work...
  :-o

SomeCallMeDave

  • Guest
Re: The same list, again and again and again....
« Reply #8 on: January 28, 2008, 04:20:54 PM »
Paul,  there are some very nice code segments here.  I plan on studying them too.



VovKa,  nice work building the InitGet  and GetKword strings from the ScaleList.  Very Elegant

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: The same list, again and again and again....
« Reply #9 on: January 28, 2008, 06:22:25 PM »
thanks David, i'm glad you like it. was coded specially for Paul to play with 'mapcar' and 'apply', sorry didn't find any use for 'foreach' :)