Author Topic: Best example of using the REPEAT function.  (Read 6569 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Best example of using the REPEAT function.
« on: December 07, 2005, 12:51:04 PM »
Let's see you best example of using the repeat function.
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Best example of using the REPEAT function.
« Reply #1 on: December 07, 2005, 12:53:17 PM »
(repeat OneMillionTimes (princ "TheSwamp.org rocks!\n"))
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Sdoman

  • Guest
Re: Best example of using the REPEAT function.
« Reply #2 on: December 07, 2005, 03:12:37 PM »
Here's one I found interesting.

Code: [Select]
(defun detab (col tab / retval)
  ;; By Steve Doman
  ;;
  ;; Given the column number which a tab is set
  ;; in a textstring, and the width of the tabs,
  ;; this function returns a string of spaces
  ;; which can be used to substitute for the tabs.
  ;;
  ;; col: is an integer representing column number
  ;;      first column of page is 1
  ;;
  ;; tab: is integer representing width of tab
  ;;
  (setq retval "")
  (repeat (- tab (rem (1- col) tab))
    (setq retval (strcat retval " "))
  )
)


Sample results:

Code: [Select]
(detab 2 4) -> "   "
(detab 12 4) -> " "
(detab 5 4) -> "    "


Andrea

  • Water Moccasin
  • Posts: 2372
Re: Best example of using the REPEAT function.
« Reply #3 on: December 07, 2005, 10:46:22 PM »
try this in a new drawing....



Code: [Select]
(setvar "OSMODE" 0)

(vl-cmdf "_.pline"
"77.5576,348.0032" "_w" "0.5" ""
"78.0611,344.0350"
"75.5467,340.9241"
"76.3257,340.2971" "")
(setq it1 (entlast))

(vl-cmdf "_.pline"
    "80.8563,340.7297" "_w" "0.5" ""
            "79.8563,340.7297"
            "79.8563,344.7297"
            "77.5576,348.0032" "")
(setq it2 (entlast))

(vl-cmdf "_.arc" "3p" "77.5576,348.0032" "77.5676,350.5462" "78.3737,352.6818")
(setq body (entlast))
(vl-cmdf "_.arc" "3p" "77.5740,352.1771" "74.7587,351.7825" "72.9441,349.6956")
(setq it3 (entlast))
(vl-cmdf "_.arc" "3p" "78.5000,351.4620" "79.3976,349.1588" "82.1197,348.6441")
(setq it4 (entlast))
(vl-cmdf "_.circle" "79.2576,354.0697" "80.7557,354.0697")
(setq head (entlast))
(vl-cmdf "_.zoom" "_w" "62.7075,330.5851" "182.9904,398.6603")
(repeat 25
  (vl-cmdf "_.rotate" it1 it2 "" "77.5576,348.0032" "-30")
  (vl-cmdf "_.delay" "60")
  (vl-cmdf "_.rotate" it1 it2 "" "77.5576,348.0032" "30")
  (vl-cmdf "_.delay" "60")
  (vl-cmdf "_.rotate" it3 it4 "" "77.9814,351.8993" "-20")
  (vl-cmdf "_.delay" "60")
  (vl-cmdf "_.rotate" it3 it4 "" "77.9814,351.8993" "20")
  (vl-cmdf "_.delay" "60")
  (vl-cmdf "_-pan"  "88.6913,345.6748" "92.0177,345.6748")
  )
(vl-cmdf "_.delay" "150")

(vl-cmdf "_.ellipse" "78.3920,340.0718" " 78.3920,340.3877" "79.0117,340.2298")
(setq it7 (entlast))
(vl-cmdf "_.delay" "200")
(vl-cmdf "_.pline" "77.6892,359.6984" "_w" "0.5" "" "78.3172,357.6962" "")
(setq qm1 (entlast))
(vl-cmdf "_.donut" "0" "0.5" "78.6435,356.5732" "")
(setq qm1a (entlast))
(vl-cmdf "_.delay" "300")
(vl-cmdf "_.scale" it7 "" "78.3920,340.2298" "1.2")
(vl-cmdf "_.delay" "300")
(vl-cmdf "_.pline" "81.3919,357.3493" "81.7233,357.8223" "82.2440,357.7277" "_a" "81.6759,359.2415" "")
(setq qm2 (entlast))
(vl-cmdf "_.donut" "0" "0.5" "80.9881,356.3819" "")
(setq qm2a (entlast))
(vl-cmdf "_.delay" "500")

(repeat 10
  (vl-cmdf "_.scale" it7 "" "78.3920,340.2298" "1.22")
  (vl-cmdf "_.delay" "20")
 )

(vl-cmdf "_.delay" "500")

(vl-cmdf "_.circle" "79.7785,353.8816" "79.9650,353.8816")
(setq it5 (entlast))
(vl-cmdf "_.circle" "81.6433,353.8816" "81.8298,353.8816")
(setq it6 (entlast))

(repeat 10
  (vl-cmdf "_.scale" it5 "" "79.7329,353.9547" "1.2")
  (vl-cmdf "_.delay" "20")
  (vl-cmdf "_.scale" it6 "" "81.6172,353.9863" "1.2")
  (vl-cmdf "_.delay" "20")
 )
(repeat 10
  (vl-cmdf "_.scale" it5 "" "79.7329,353.9547" "0.8")
  (vl-cmdf "_.delay" "20")
  (vl-cmdf "_.scale" it6 "" "81.6172,353.9863" "0.8")
  (vl-cmdf "_.delay" "20")
 )

(vl-cmdf "_.delay" "500")

(entdel it1)(entdel it2)(entdel it3)(entdel it4)(entdel body)(entdel it5)(entdel it6)(entdel qm1)(entdel qm2)(entdel qm1a)(entdel qm2a)
(vl-cmdf "_.pline" "76.5822,339.4330" "76.8503,342.9721" "78.3930,344.7324" "79.1164,343.0592"
"79.0829,339.2606" "")
(setq short (entlast))
(vl-cmdf "_.line" "78.5224,345.4648" "78.4607,349.7562" "")
(setq body (entlast))
(vl-cmdf "_.arc" "3p" "77.4488,349.7562" "75.7915,352.0743" "75.2987,356.2020")
(setq hand1 (entlast))
(vl-cmdf "_.arc" "3p" "80.1049,350.1353" "81.6765,352.5794" "82.1286,356.4548")
(setq hand2 (entlast))
(vl-cmdf "_.move" head "" "79.2576,354.0697" "78.6674,352.9244")
(vl-cmdf "_.delay" "150")
(entdel short)

(vl-cmdf "_.move" body hand1 hand2 head "" "" "64.8873,353.2961" "64.8873,347.8614")
(vl-cmdf "_.delay" "50")
(entdel body)
(vl-cmdf "_.move" hand1 hand2 head "" "64.8873,353.2961" "64.8873,347.8614")
(entdel hand1)(entdel hand2)(entdel head)

(vl-cmdf "_.delay" "100")
(vl-cmdf "_.line" "78.3388,342.7947" "78.3388,346.2111" "")
(setq ent1 (entlast))
(repeat 20
(vl-cmdf "_.move" ent1 "" "66.7766,344.7203" "66.7766,345.7203")
  (vl-cmdf "_.delay" "9")
)
(vl-cmdf "_.erase" "_l" "")

(vl-cmdf "_.delay" "100")

(setvar "OSMODE" 0)
(repeat 20
  (vl-cmdf "_.scale" it7 "" "78.3920,340.2298" "0.9")
  (vl-cmdf "_.delay" "45")
 )
(entdel it7)
(vl-cmdf "_.line" "76.4906,340.2295" "76.1334,340.1938" "")
(vl-cmdf "_.line" "77.6872,340.9790" "77.2228,341.4430" "")
(vl-cmdf "_.line" "79.1874,340.7827" "79.4731,341.0683" "")
(vl-cmdf "_.line" "79.5445,339.9439" "79.8660,339.7833" "")
(vl-cmdf "_.line" "78.1158,339.5870" "78.0086,339.4264" "")

(repeat 5
  (vl-cmdf "_.scale" "_all" "" "78.3920,340.2298" "1.4")
  (vl-cmdf "_.delay" "45")
 )

(repeat 8
  (vl-cmdf "_.scale" "_all" "" "78.3920,340.2298" "1.1")
  (vl-cmdf "_.delay" "88")
 )
(vl-cmdf "_.erase" "_all" "")
Keep smile...

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Re: Best example of using the REPEAT function.
« Reply #4 on: December 08, 2005, 02:48:49 AM »
One of my favorite:
Code: [Select]
;
; -- Function MeBinary2Dec
; Convert Binary to a decimal number.
; Arguments [Type]:
;   Bin = Binary data, eg. 11101 [STR]
;   Msd = Most significant digit [BOOLEAN] *)
; Return [Type]:
;   > Decimal number [INT]
; Notes:
;   *) If true, first digit is MSD, else last digit
;
(defun MeBinary2Dec (Bin Msd / CurBit ExpCnt RetVal StrCnt)
 (setq StrCnt (if Msd (strlen Bin) 1)
       ExpCnt 0
       RetVal 0
 )
 (repeat (strlen Bin)
  (setq CurBit (atoi (substr Bin StrCnt 1))
        RetVal (+ (lsh CurBit ExpCnt) RetVal)
        StrCnt ((if Msd 1- 1+) StrCnt)
        ExpCnt (1+ ExpCnt)
  )
 )
 RetVal
)
Cheers
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18

hudster

  • Gator
  • Posts: 2848
Re: Best example of using the REPEAT function.
« Reply #5 on: December 08, 2005, 03:46:13 AM »
Not as good as the rest of these guys but it does the job I want it to.

Code: [Select]
(setq c:pur ()
(REPEAT 5 (COMMAND "-PURGE" "ALL" "*" "NO"))
(princ)
)
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

whdjr

  • Guest
Re: Best example of using the REPEAT function.
« Reply #6 on: December 08, 2005, 08:18:47 AM »
Code: [Select]
(defun *ssnames* (selection_set / num lst)
  (repeat (setq num (sslength selection_set))
    (setq num (1- num)
  lst (cons (ssname selection_set num) lst)
    )
  )
  lst
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10665
Re: Best example of using the REPEAT function.
« Reply #7 on: December 08, 2005, 09:52:41 AM »
Prolly my favorite use. (Very inspiring for me in my learnings)

Code: [Select]
(defun strtol ( s / lst c )
  ;; strtol convert string of chars into list of 1-char strings
  ;; By: Vladimir Nesterovsky
  ;; From: venestr.llb
  ;; Ex: (strtol "test")
  ;;   > ("t" "e" "s" "t")
  (repeat
    (setq c (strlen s))
    (Setq lst (cons (substr s c 1) lst)
          c   (1- c)))
  lst
 )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Joe Burke

  • Guest
Re: Best example of using the REPEAT function.
« Reply #8 on: December 08, 2005, 10:18:47 AM »
An alternaitve...

Command: (mapcar 'chr (vl-string->list "test"))
("t" "e" "s" "t")

whdjr

  • Guest
Re: Best example of using the REPEAT function.
« Reply #9 on: December 08, 2005, 10:48:05 AM »
Yeah I think 'mapcar' would be my favorite method of repeat.

JohnK

  • Administrator
  • Seagull
  • Posts: 10665
Re: Best example of using the REPEAT function.
« Reply #10 on: December 08, 2005, 10:58:01 AM »
An alternaitve...

Command: (mapcar 'chr (vl-string->list "test"))
("t" "e" "s" "t")

Yeah, but you have to remember that that function was most likely written before VL was implimented. Besides, that was just a good example of how repete is used. And that was one of favorite.

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10665
Re: Best example of using the REPEAT function.
« Reply #11 on: December 08, 2005, 11:05:40 AM »
I better back up my reasoning(s) for my statment of it being a good example.

Repete is better because: Its faster and more efficient then doing the same thing with a traditional looping construct.

And example of that would be something like this:
Code: [Select]
(defun String->StrList (str / alst cntr)
  ;; This procedure will take a given string and parse it out to a
  ;; list of characters.
  ;; By: John K (Se7en)
  ;; Ex: (String->StrList "This is a test")
  ;;  > ("T" "h" "i" "s" " " "i" "s" " " "a" " " "t" "e" "s" "t")
  (if (= cntr '()) (setq cntr 1))
  (while (<= cntr (strlen str))
    (setq alst (cons (substr str cntr 1) alst)
          cntr (1+ cntr)))
  (reverse alst)
 )
But if you use repeat:
Code: [Select]
(defun strtol ( s / lst c )
  ;; strtol convert string of chars into list of 1-char strings
  ;; By: Vladimir Nesterovsky
  ;; From: venestr.llb
  ;; Ex: (strtol "test")
  ;;   > ("t" "e" "s" "t")
  (repeat
    (setq c (strlen s))
    (Setq lst (cons (substr s c 1) lst)
          c   (1- c)))
  lst
)

The code becomes more readalble and less cluttered. (And less 'bloated' as well.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Joe Burke

  • Guest
Re: Best example of using the REPEAT function.
« Reply #12 on: December 09, 2005, 09:52:21 AM »
An alternaitve...

Command: (mapcar 'chr (vl-string->list "test"))
("t" "e" "s" "t")

Yeah, but you have to remember that that function was most likely written before VL was implimented. Besides, that was just a good example of how repete is used. And that was one of favorite.



Not, IMO. The method I posted is the most efficient way to do it, AFAIK.

I guess if you want to play with code, well... that's OK. But I fail to see where your example proves anything about repeat.

It's like saying I should use a typewritter when I have a computer. Just because it was the only way back when, doesn't mean it serves as a good example now. The mechanism is out-of-date. Why belabor the point?

Regards

Sdoman

  • Guest
Re: Best example of using the REPEAT function.
« Reply #13 on: December 09, 2005, 09:57:50 AM »
True there are better ways today to tokenize a string to a list, but the idea to parse a string using repeat is good IMO.

JohnK

  • Administrator
  • Seagull
  • Posts: 10665
Re: Best example of using the REPEAT function.
« Reply #14 on: December 09, 2005, 10:24:45 AM »
Joe, When did i say that "my" method was better then "your" method? (I said that using repeat in this instance is better then a traditional looping construct.)

If you would like to have a discussion about strings and the parsing/tokenizing of them, then start a discussion.

The fact that its a good example still stands. Its easy to read and easy to understand for those new to lisp.

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org