找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 981|回复: 3

[LISP函数]:刚找到的一些实用函数

[复制链接]
发表于 2003-8-25 17:01:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

  1.   [FONT=courier new]
  2. ;;; Michels Visual LISP Functions
  3. ;;; These functions all work correctly, but containg only minimal error checking
  4. ;;;
  5. ;;; There are five Trigonometric functions
  6. ;;;
  7. ;;; (deg->rad angle): input angle in degrees as a real or int, returns angle in radians as real
  8. ;;; (rad->deg angle): input angle in radians as a real or int, returns angle in degrees as real
  9. ;;; (tan angle): input angle in radians, retuns tangent of angle as real
  10. ;;; (asin real): input real, returns arcsine of number in radians
  11. ;;; (acos real): input real, returns arcosine of number in radians
  12. ;;;
  13. ;;; Four general information functions
  14. ;;;
  15. ;;; (3d->2d 3d point) returns a 2d point
  16. ;;; (2d->3d 2d point level) returns a 3d point with z value equal to level or 0.0 if nil
  17. ;;; info: select object, retuns a dialog listing entity type and layer, also if in a block and/or XREF
  18. ;;;          displays names and layers of blocks/XREF's
  19. ;;; (stringp atom): returns T if atom is a string
  20. ;;; (intp atom): returns T if atom is an integer
  21. ;;; (realp atom): returns T if atom is a real number
  22. ;;;
  23. ;;; Ten list manipulation tools
  24. ;;;
  25. ;;; (vl-remove-last list): input list, removes last element from list
  26. ;;; (strl_sort list): input list of strings, similar to (acad_strlsort) but sorts numbers numerically
  27. ;;; (remove atom list): similar to (vl-remove) but only removes first occurance of atom in list
  28. ;;; (nth-remove nth list): removes nth element from list
  29. ;;; (vl-add atom nth list): adds atom as nth element of list
  30. ;;; (sublist start length list): returns a number of elements after start position from a list
  31. ;;; (remlist start length list): removes a number of elements after start position from a list
  32. ;;; (list->string delim list): input deliminator as string, list, converts elements of list to a deliminated string
  33. ;;; (sslist pickset): input selectionsel, returns list of selected entities
  34. ;;; (strconv list): converts elements of list into strings
  35. ;;;
  36. ;;; Ten String functions
  37. ;;;
  38. ;;; (ltrim string): input string, removes any spaces from the beginning of the string
  39. ;;; (rtrim string): input string, removes any spaces from the end of the string
  40. ;;; (trim string): input string, removes speces from beginning and end of string
  41. ;;; (string->list string): input string, returns the string a list using spaces a delimineater
  42. ;;; (mid string string): input two strings, returns first string if it is found within second string else nil
  43. ;;; (stringp val): input a value, returns true if value is a string else nil
  44. ;;; (strrev string): reverses string
  45. ;;; (replace oldstring newstring string): replaces occurances of oldstring in string with newstring
  46. ;;; (stringtype string): returns data type of contents of the string
  47. ;;; (list->string delim list): converts list into delimted string
  48. ;;; (right string int): returns number of charachters from right of string
  49. ;;; (left string int): returns number of charachters from left of string
  50. ;;; (multiline string int): returns string as a list of strings, each element being no longer than int but finishing with a full word if possible
  51. ;;;

  52. ;;;(if (null c:fun)        checks if lisp routine is loaded
  53. ;;;        (load fun.lsp) load function
  54. ;;;        )


  55. (defun c:functions () ;provide to check if routine is loaded
  56.   (alert "Functions Routine Loaded")
  57. )

  58. (defun 3d->2d (f_3d)        ;converts 3d co-ordinate to 2d
  59.   (list (car f_3d) (cadr f_3d))
  60.   )
  61. ;eg (setq a (3d->2d '(0 0 0))) = (0 0)

  62. (defun 2d->3d (f_d f_3d) ;converts 2d co-ordinate to 3d with given level
  63.   (if (not (numberp f_3d))
  64.     (setq f_3d 0)
  65.     )
  66.   (if f_3d
  67.     (append f_2d (list f_3d))
  68.     (append f_2d '(0.0))
  69.     )
  70.   )
  71. ;eg (setq a (2d->3d '(0 0) 0)) = (0 0 0)

  72. (defun vl-remove-last (f_l) ;removes last element from a list
  73.   (setq f_l (reverse (cdr (reverse f_l))))
  74. )
  75. ;eg (setq a (vl-remove-last '(1 2 3))) = (1 2)

  76. (defun deg->rad        (f_ang) ;converts angles from degrees to radians
  77.   (setq f_ang (* pi (/ f_ang 180.0)))
  78. )
  79. ;eg (setq a (deg->rad 180)) = 3.14

  80. (defun rad->deg        (f_ang) ;converts angles from radians to degrees
  81.   (setq f_ang (* 180.0 (/ f_ang pi)))
  82. )
  83. ;eg (setq a (rad->deg (/ pi 2.0))) = 90

  84. (defun tan (f_ang / f_tangent) ;tangent funtion usesangles in radians
  85.   (setq f_tangent (/ (sin f_ang) (cos f_ang)))
  86. )
  87. ;eg (setq a (tan (/ pi 4))) = 1.0

  88. (defun asin (f_ang) ;arcsin function uses angles in radians
  89.   (if (= (atof (rtos (abs f_ang))) 1)
  90.     0
  91.     (atan (/ f_ang (sqrt (+ (* (- f_ang) f_ang) 1))))
  92.     )
  93. )
  94. ;eg (setq a (asin 0.5)) = 0.523

  95. (defun acos (f_ang) ;arcosine function uses angles in radians
  96.   (if (= (atof (rtos (abs f_ang))) 1)
  97.     0
  98.     (+ (atan (/ (- f_ang) (sqrt (+ (* (- f_ang) f_ang) 1))))
  99.        (* 2 (atan 1))
  100.     )
  101.   )
  102. )
  103. ;eg (setq a (acos 0.5)) = 10.0472

  104. (defun strl_sort (f_stringlist / f_numl f_strl f_n f_cur) ; similar to acad_strlsort, but sorts string representing numbers numberically
  105.   (setq        f_numl nil
  106.         f_strl nil
  107.   )
  108.   (foreach f_n f_stringlist                ;loop through each element in list
  109.     (progn
  110.       (if (numberp (read f_n))                ;test if number
  111.         (setq f_numl (append f_numl (list f_n)))
  112.                                         ;create list of numbers
  113.         (setq f_strl (append f_strl (list f_n)))
  114.                                         ;create list of strings
  115.       )
  116.     )
  117.   )
  118.   (setq f_stringlist nil)
  119.   (repeat (length f_numl)                ;loop through list of numbers
  120.     (progn
  121.       (setq f_cur (car f_numl))
  122.       (foreach f_n (cdr f_numl)                ;loop through remainder of number list
  123.         (if (> (read f_cur) (read f_n))        ;test if current value smaller than elements in list
  124.           (setq f_cur f_n)                ;if so replace current value with smaller value
  125.         )
  126.       )
  127.       (setq f_numl (remove f_cur f_numl))
  128.                                         ;remove current (smallest) value from number list
  129.       (setq f_stringlist (append f_stringlist (list f_cur)))
  130.                                         ;add current (smallest) value to end of final list
  131.     )
  132.   )
  133.   (repeat (length f_strl)                ;loop through list of strings
  134.     (progn
  135.       (setq f_cur (car f_strl))
  136.       (foreach f_n (cdr f_strl)                ;loop though remainder of string list
  137.         (if (> f_cur f_n)                ;test for alphabetical order
  138.           (setq f_cur f_n)                ;replace current value for precending value
  139.         )
  140.       )
  141.       (setq f_strl (remove f_cur f_strl))
  142.                                         ;remove current value from string list
  143.       (setq f_stringlist (append f_stringlist (list f_cur)))
  144.                                         ;add current value to end of final list
  145.     )
  146.   )
  147. )
  148. ;eg (strl_sort '("b" "a" "10" "1")) = ("1" "10" "a" "b")

  149. (defun remove (f_new f_newlist / f_test) ;removes first occurance of an element from a list
  150.   (if (member f_new f_newlist)                ;is value present in list
  151.     (progn
  152.       (setq f_test nil)                        ;test to see if element removed
  153.       (repeat (length f_newlist)        ;loop through list
  154.         (progn
  155.           (if (not (and (= f_test nil) (= (car f_newlist) f_new)))
  156.                                         ;test if element removed or element is to be removed
  157.             (setq f_newlist
  158.                    (cdr (append f_newlist (list (car f_newlist))))
  159.             )                                ;swaps moves first element to end of list
  160.             (progn
  161.               (setq f_test t)                ;element has been removed
  162.               (setq f_newlist (cdr f_newlist))
  163.                                         ;remove first element from list
  164.             )
  165.           )
  166.         )
  167.       )
  168.     )
  169.   )
  170. )
  171. ;eg (remove 1 '(1 1 2 1)) = (1 2 1)

  172. (defun nth-remove (f_n f_list / ) ;removes nth element from a list
  173.   (if (and (numberp f_n) (listp f_list))
  174.     (if (and (>= f_n 0) (< f_n (length f_list)))
  175.     (progn
  176.       (repeat f_n
  177.         (setq f_list (append (cdr f_list) (list (car f_list))))
  178.         )
  179.       (setq f_list (cdr f_list))
  180.       (repeat (- (length f_list) f_n)
  181.         (setq f_list (append (cdr f_list) (list (car f_list))))
  182.         )
  183.       )
  184.     )
  185.     )
  186.   f_list
  187.   )
  188. ;eg (nth-remove (0 '(1 2 3))) = (2 3)

  189. (defun vl-add (f_new f_n f_newlist)        ;add element to nth position in a list
  190.   (if (not (listp f_newlist))                ;test if a list
  191.     (setq f_newlist (list f_newlist))        ;creates list
  192.   )
  193.   (cond
  194.     ((= f_n 0) (setq f_newlist (append (list f_new) f_newlist)))
  195.                                         ;if target first element, make into list and add list
  196.     ((>= f_n (length f_newlist))        ;is target end of list
  197.      (setq f_newlist (append f_newlist (list f_new)))
  198.                                         ;add element to end of list
  199.     )
  200.     (t
  201.      (progn
  202.        (setq f_loop -1)                        ;initialize counter
  203.        (foreach        f_x f_newlist                ;loop through list
  204.          (progn
  205.            (setq f_loop (1+ f_loop))        ;increment counter
  206.            (if (= f_n f_loop)                ;if target element
  207.              (setq f_newlist (cdr (append f_newlist (list f_new f_x))))
  208.                                         ;add element to end of list, move first element to end of list
  209.              (setq f_newlist (cdr (append f_newlist (list f_x))))
  210.                                         ;move first element to end of list
  211.            )
  212.          )
  213.        )
  214.      )
  215.     )
  216.   )
  217. )
  218. ;eg (vl-add 2 1 '(3 3)) = (3 2 3)

  219. (defun sublist (f_n f_m f_newlist / f_loop f_x) ;returns sublist of a list, similar to substr function
  220.   (if (>= (length f_newlist) f_n)        ;test range
  221.     (progn
  222.       (setq f_loop -1)                        ;initialize counter
  223.       (setq f_m (+ f_n (- f_m 1)))        ;setq end element
  224.       (foreach f_x f_newlist                ;loop through list
  225.         (progn
  226.           (setq f_loop (1+ f_loop))        ;increment counter
  227.           (if (and (<= f_n f_loop) (>= f_m f_loop)) ;check within range
  228.             (setq f_newlist (cdr (append f_newlist (list f_x))))
  229.                                         ;move first element to end of list
  230.             (setq f_newlist (cdr f_newlist)) ;remove first element
  231.           )
  232.         )
  233.       )
  234.     )
  235.   )
  236. )
  237. ;eg (sublist 1 2 '(1 2 3 4 5)) = (2 3)

  238. (defun remlist (f_n f_m f_newlist / f_loop f_x) ;removes number of elemts from list
  239.   (if (>= (length f_newlist) f_n)        ;test range
  240.     (progn
  241.       (setq f_loop -1)                        ;initialize counter
  242.       (setq f_m (+ f_n (- f_m 1)))        ;set end element
  243.       (foreach f_x f_newlist                ;loop through list
  244.         (progn
  245.           (setq f_loop (1+ f_loop))        ;increment counter
  246.           (if (and (<= f_n f_loop) (>= f_m f_loop)) ;check within range
  247.             (setq f_newlist (cdr f_newlist))
  248.                                         ;remove first element from list
  249.             (setq f_newlist (cdr (append f_newlist (list f_x))))
  250.                                         ;move first element to end of list
  251.           )
  252.         )
  253.       )
  254.     )
  255.   )
  256. )
  257. ;eg (remlist 1 2 '(1 2 3 4 5)) = (1 4 5)


  258. (defun string->list (f_string / f_ls f_str f_len f_loop f_cur) ;converts string to a list using spaces as deliminators
  259.   (setq        f_ls nil
  260.         f_str ""
  261.   )
  262.   (setq f_loop 0)
  263.   (repeat (strlen f_string)
  264.     (progn
  265.       (setq f_loop (1+ f_loop))
  266.       (setq f_cur (substr f_string f_loop 1))
  267.       (if (= (ascii f_cur) 32)
  268.         (if (/= f_str "")
  269.           (progn (setq f_ls (append f_ls (list f_str)))
  270.                  (setq f_str "")
  271.           )
  272.         )
  273.         (setq f_str (strcat f_str f_cur))
  274.       )
  275.     )
  276.   )
  277.   (if (/= f_str "")
  278.     (setq f_ls (append f_ls (list f_str)))
  279.   )
  280.   f_ls
  281. )
  282. ;eg (string->list "Hello World") = ("Hello" "World")

  283. (defun ltrim (f_string / f_loop) ;removes spaces from left of string
  284.   (setq f_loop 1)
  285.   (while (= (ascii (substr f_string f_loop 1)) 32)
  286.     (setq f_loop (1+ f_loop))
  287.   )
  288.   (setq f_string (substr f_string f_loop (strlen f_string)))
  289. )
  290. ;eg (ltrim "  trim  ") = "trim  "

  291. (defun rtrim (f_string) ;removes spaces from right of string
  292.   (setq f_loop 0)
  293.   (setq f_len (strlen f_string))
  294.   (while (= (ascii (substr f_string (- f_len f_loop) 1)) 32)
  295.     (setq f_loop (1+ f_loop))
  296.   )
  297.   (setq f_string (substr f_string 1 (- f_len f_loop)))
  298. )
  299. ;eg (rtrim "  trim  ") = "  trim"

  300. (defun trim (f_string) ;removes spaces from outside of string
  301.   (setq f_string (ltrim f_string))
  302.   (setq f_string (rtrim f_string))
  303. )
  304. ;eg (trim "  trim  ") = "trim"

  305. (defun mid (f_string1 f_string2 / f_test f_loop) ;tests if string1 is contained in string2
  306.   (setq f_test nil)
  307.   (cond
  308.     ((or (= (strlen f_string1) 0) (= (strlen f_string2) 0))
  309.      (setq f_test nil)
  310.     )
  311.     ((> (strlen f_string1) (strlen f_string2))
  312.      (setq f_test nil)
  313.     )
  314.     (t
  315.      (progn
  316.        (setq f_loop 1)
  317.        (repeat (- (strlen f_string2) (- (strlen f_string1) 1))
  318.          (progn
  319.            (if
  320.              (=        (strcase f_string1)
  321.                 (strcase (substr f_string2 f_loop (strlen f_string1)))
  322.              )
  323.               (setq f_test f_string1)
  324.            )
  325.            (setq f_loop (1+ f_loop))
  326.          )
  327.        )
  328.      )
  329.     )
  330.   )
  331.   f_test
  332. )
  333. ;eg (mid "to" "AutoCAD") = "to"

  334. (defun sslist (f_sel) ;converts a selection set to a list of enames
  335.   (if f_sel
  336.     (progn
  337.   (if (/= (type f_sel) 'PICKSET)
  338.     (exit)
  339.   )
  340.   (setq        f_ss_list nil
  341.         f_loop -1
  342.   )
  343.   (repeat (sslength f_sel)
  344.     (progn
  345.       (setq f_loop (1+ f_loop))
  346.       (princ (strcat "\rSorting " (itoa f_loop) " of " (itoa (sslength f_sel)) "     "))
  347.       (setq f_ss_list (append f_ss_list (list (ssname f_sel f_loop))))
  348.     )
  349.   )
  350.   )
  351.     nil
  352.     )
  353. )
  354. ;eg (sslist (ssget)) = (<Entity name: 40061d70> <Entity name: 40061d68>)

  355. (defun strrev (f_string / f_cur) ;reverses a string
  356.   (setq f_cur "")
  357.   (repeat (strlen f_string)
  358.     (progn
  359.       (setq f_cur (strcat f_cur (substr f_string (strlen f_string) 1)))
  360.       (setq f_string (substr f_string 1 (- (strlen f_string) 1)))
  361.     )
  362.   )
  363.   f_cur
  364. )
  365. ;eg (strrev "Hello") = "olleH"

  366. (defun list->string (f_delim f_ls) ;converts a list of strings to a deliminated string
  367.   (setq f_string "")
  368.   (foreach f_i f_ls
  369.     (progn
  370.       (setq f_string (strcat f_string f_i f_delim))
  371.     )
  372.   )
  373.   (setq f_string (substr f_string 1 (- (strlen f_string) 1)))
  374. )
  375. ;eg (list->string "*" '("1" "2")) = "1*2"


  376. (defun stringp (f_val) ;test if a string
  377.   (if (= (type f_val) 'STR)
  378.     t
  379.     nil
  380.   )
  381. )
  382. ;eg (stringp "Hello") = true

  383. (defun realp (f_val) ;tests if a real number
  384.   (if (= (type f_val) 'REAL)
  385.     t
  386.     nil
  387.   )
  388. )
  389. ;eg (realp 1.0) = true

  390. (defun intp (f_val) ;tests if an integer
  391.   (if (= (type f_val) 'INT)
  392.     t
  393.     nil
  394.   )
  395. )
  396. ;eg (intp 1) = true

  397. (defun strconv (f_ls) ;converts elements of a list to a list of strings
  398.   (if (null (listp f_ls))
  399.     (exit)
  400.   )
  401.   (setq f_loop 0)
  402.   (while (and (>= f_loop 0) (< f_loop (length f_ls)))
  403.     (setq f_cur (car f_ls))
  404.     (cond
  405.       ((stringp f_cur)
  406.        (setq f_ls (append (cdr f_ls) (list f_cur)))
  407.       )
  408.       ((realp f_cur)
  409.        (setq f_ls (append (cdr f_ls) (list (rtos f_cur))))
  410.       )
  411.       ((intp f_cur)
  412.        (setq f_ls (append (cdr f_ls) (list (itoa f_cur))))
  413.       )
  414.       (t (setq f_loop -2))
  415.     )
  416.     (setq f_loop (1+ f_loop))
  417.   )
  418.   (if (>= f_loop 0)
  419.     f_ls
  420.     nil
  421.   )
  422. )
  423. ;eg (strconv '(1 2.5 "h")) = ("1" "2.5" "h")

  424. (defun replace (f_oldstr f_newstr f_string / f_cur f_loop) ;like subst but with strings not lists
  425.   (if (null (and (stringp f_oldstr)
  426.                  (stringp f_newstr)
  427.                  (stringp f_string)
  428.             )
  429.       )
  430.     (exit)
  431.   )
  432.   (if (> (strlen f_oldstr) (strlen f_string))
  433.     (exit)
  434.   )
  435.   (setq f_loop 1)
  436.   (repeat (strlen f_string)
  437.     (progn
  438.       (setq f_cur (substr f_string f_loop (strlen f_oldstr)))
  439.       (if (= f_cur f_oldstr)
  440.         (setq f_string (strcat (substr f_string 1 (- f_loop 1))
  441.                                f_newstr
  442.                                (substr f_string
  443.                                        (+ f_loop (strlen f_oldstr))
  444.                                        (strlen f_string)
  445.                                )
  446.                        )
  447.         )
  448.       )
  449.       (setq f_loop (1+ f_loop))
  450.     )
  451.   )
  452.   f_string
  453. )
  454. ;eg (replace "\t" " " "Line one\tLine two") = "Line one Line two"

  455. (defun strtype (f_string) ;returns type of data represented by a string, real int or string
  456.   (if (not (stringp f_string))
  457.     (exit)
  458.   )
  459.   (setq        f_test nil
  460.         f_loop 1
  461.   )
  462.   (repeat (strlen f_string)
  463.     (setq f_cur (substr f_string f_loop 1))
  464.     (cond
  465.       ((= (ascii f_cur) 45)
  466.        (if (= f_loop 1)
  467.          (setq f_test "REAL")
  468.          (setq f_test "STRING")
  469.          )
  470.        )
  471.       ((= (ascii f_cur) 46)
  472.        (if (/= f_test "STRING")
  473.          (setq f_test "REAL")
  474.        )
  475.       )
  476.       ((and (>= (ascii f_cur) 48) (<= (ascii f_cur) 57))
  477.        (if (and (/= f_test "STRING") (/= f_test "REAL"))
  478.          (setq f_test "INT")
  479.        )
  480.       )
  481.       (t (setq f_test "STRING"))
  482.     )
  483.     (setq f_loop (1+ f_loop))
  484.   )
  485.   f_test
  486. )
  487. ;eg (strtype "1.5") = "REAL"

  488. (defun c:blktxt        () ;converts text representing co-ordinates to a block
  489.   (setq f_all (ssget "_x" '((0 . "TEXT"))))
  490.   (if (null f_all)
  491.     (exit)
  492.   )
  493.   (setq f_all (sslist f_all))
  494.   (foreach f_i f_all
  495.     (progn
  496.       (setq f_tmp (entget i))
  497.       (if (/= (strtype (cdr (assoc 1 f_tmp))) "STRING")
  498.         (progn
  499.           (setq f_x (cadr (assoc 10 f_tmp)))
  500.           (setq f_y (caddr (assoc 10 f_tmp)))
  501.           (setq f_z (atof (cdr (assoc 1 f_tmp))))
  502.           (setq f_ins (list f_x f_y f_z))
  503.           (command "-insert"
  504.                    "pl"
  505.                    f_ins
  506.                    ""
  507.                    ""
  508.                    ""
  509.                    (cdr (assoc 1 f_tmp))
  510.                    ""
  511.                    ""
  512.           )
  513.         )
  514.       )
  515.     )
  516.   )
  517.   (princ)
  518. )

  519. (defun c:ca (/ f_sel f_pt1 f_pt2 f_pt3 f_pt4 f_test f_osmode) ;similar to align tool but can copy item
  520.   (setq f_osmode -1)
  521.   (princ "\nSelect Object(s): ")
  522.   (while (null (setq f_sel (ssget))))
  523.   (setq f_pt1 (getpoint "\rSelect First Source Point: "))
  524.   (setq f_pt2 (getpoint "\rSelect First Destination Point: "))
  525.   (setq f_pt3 (getpoint "\rSelect Second Source Point:     "))
  526.   (setq f_pt4 (getpoint "\rSelect Second Destination Point: "))
  527.   (if (not
  528.         (or (null f_pt1) (null f_pt2) (null f_pt3) (null f_pt4))
  529.       )
  530.     (progn
  531.       (initget "Yes No")
  532.       (setq f_test (getkword "\nScale Objects [Yes/No]<Yes>: "))
  533.       (if (null f_test)
  534.         (setq f_test "Yes")
  535.       )
  536.       (if (< (getvar "osmode") 16384)
  537.         (progn
  538.           (setq f_osmode (getvar "osmode"))
  539.           (setvar "osmode" (+ f_osmode 16384))
  540.         )
  541.       )
  542.       (command "copy" f_sel "" f_pt1 f_pt1)
  543.       (if (= f_test "Yes")
  544.         (command "scale"
  545.                  f_sel
  546.                  ""
  547.                  f_pt1
  548.                  "r"
  549.                  f_pt1
  550.                  f_pt3
  551.                  (distance f_pt2 f_pt4)
  552.         )
  553.       )
  554.       (command "ucs" "z" f_pt2 f_pt4)
  555.       (command "rotate"
  556.                f_sel
  557.                ""
  558.                (trans f_pt1 0 1)
  559.                "r"
  560.                (trans f_pt1 0 1)
  561.                (trans f_pt3 0 1)
  562.                "0"
  563.       )
  564.       (command "ucs" "p")
  565.       (command "move" f_sel "" f_pt1 f_pt2)
  566.       (if (/= f_osmode -1)
  567.         (setvar "osmode" f_osmode)
  568.       )
  569.     )
  570.   )
  571.   (princ)
  572. )

  573. (defun c:lbo (/ f_ent f_col f_layer) ;changes colour of layer to same as selected object
  574.   (setq f_ent (entsel "\nselect object: "))
  575.   (if (null f_ent)
  576.     (exit)
  577.   )
  578.   (setq f_ent (entget (car f_ent)))
  579.   (if (assoc 62 f_ent)
  580.     (setq f_col (assoc 62 f_ent))
  581.     (exit)
  582.   )
  583.   (if (or (= (cdr f_col) 0) (= (cdr f_col) 256))
  584.     (exit)
  585.     )
  586.   (setq f_layer (entget (tblobjname "Layer" (cdr (assoc 8 f_ent)))))
  587.   (entmod (subst f_col (assoc 62 f_layer) f_layer))
  588.   (entmod (subst (cons 62 256) (assoc 62 f_ent) f_ent))
  589.   (princ)
  590. )

  591. (defun right (f_string f_len) ;returns x number of characters from right of string
  592.   (if (and (stringp f_string) (intp f_len))
  593.     (if        (and (> f_len 0) (<= f_len (strlen f_string)))
  594.       (substr f_string (1+ (- (strlen f_string) f_len)) f_len)
  595.       (progn (princ "Out of Range") (princ))
  596.     )
  597.     (progn (princ "Usage: right <string> <integer>") (princ))
  598.   )
  599. )
  600. ;eg (right "AutoCAD" 3) = "CAD"

  601. (defun left (f_string f_len) ;returns x number of characters from left of string
  602.   (if (and (stringp f_string) (intp f_len))
  603.     (if        (and (> f_len 0) (<= f_len (strlen f_string)))
  604.       (substr f_string 1 f_len)
  605.       (progn (princ "Out of Range") (princ))
  606.     )
  607.     (progn (princ "Usage: right <string> <integer>") (princ))
  608.   )
  609. )
  610. ;eg (left "Hello" 4) = "Hell"

  611. (defun multiline (f_string f_maxlen) ;converts a string to a list where each element has a length no greater than maxlen
  612.   (if (null (stringp f_string))
  613.     (exit)
  614.   )
  615.   (if (null (intp f_maxlen))
  616.     (exit)
  617.   )
  618.   (setq f_strout nil)
  619.   (while (< f_maxlen (strlen f_string))
  620.     (setq f_newstr (substr f_string 1 f_maxlen))
  621.     (setq f_oldstr (substr f_string (+ f_maxlen 1) (strlen f_string)))
  622.     (while (and        (/= (right f_newstr 1) " ")
  623.                 (/= (left f_oldstr 1) " ")
  624.                 (mid " " f_newstr)
  625.            )
  626.       (setq f_oldstr (strcat (right f_newstr 1) f_oldstr))
  627.       (setq f_newstr (substr f_newstr 1 (- (strlen f_newstr) 1)))
  628.     )
  629.     (setq f_string f_oldstr)
  630.     (setq f_strout (append f_strout (list f_newstr)))
  631.   )
  632.   (setq f_strout (append f_strout (list f_string)))
  633. )
  634. ;

  635. (defun stringcase (f_string f_code / f_cur f_i) ;converts case of string, 2 Upper case initial lower case rest, 1 lower case, else uppercase
  636.   (if (null c:functions)
  637.     (load "functions.vlx")
  638.     )
  639.   (if (null f_code)
  640.     (setq f_code 0)
  641.     )
  642.   (if (or (null (stringp f_string)) (null (intp f_code)))
  643.     (exit)
  644.     )
  645.   (cond
  646.     ((= f_code 1) (setq f_string (strcase f_string t)))
  647.     ((= f_code 2) (progn
  648.                   (setq f_cur "")
  649.                   (setq f_string (string->list f_string))
  650.                   (foreach f_i f_string
  651.                     (progn
  652.                       (setq f_i (strcat (strcase (substr f_i 1 1)) (strcase (substr f_i 2 (strlen f_i)) t)))
  653.                       (setq f_cur (strcat f_cur " " f_i))
  654.                       )
  655.                     )
  656.                   (setq f_string (substr f_cur 2 (strlen f_cur)))
  657.                   ))
  658.     (t (setq f_string (strcase f_string)))
  659.     )
  660.   f_string
  661.   )
  662. ;eg (stringcase "hello world" 2) = "Hello World"

  663. (defun foreachss (f_var f_ssset f_fun / f_loop)
  664.                                         ;function with three parameters variable name,selection set and function
  665.   (if (and (eq (type f_var) 'STR)        ;check for valid values
  666.            (eq (type f_ssset) 'PICKSET)
  667.            (eq (type f_fun) 'LIST)
  668.       )
  669.     (progn
  670.       (setq f_loop 0)                        ;intitialise counter
  671.       (while (< f_loop (sslength f_ssset))
  672.                                         ;iterate through each item in selection set
  673.         (set (read f_var) (ssname f_ssset f_loop))
  674.                                         ;populate varible with name provided with ename from selection set
  675.         (eval f_fun)                        ;preform funtion on ename
  676.         (setq f_loop (1+ f_loop))        ;next iteration
  677.       )
  678.     )
  679.   )
  680.   (princ)                                ;exit quietly
  681. )
  682. ;eg (foreachss "i" (ssget) '(entdel i)) this will delete all of the selected entities  [/FONT]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2003-8-28 12:56:30 | 显示全部楼层
初步看了一下,挺好!我想有哪位再稍加解释,最好不过了,别笑话本人e文水平有限
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-8-31 09:59:43 | 显示全部楼层
能否讲解一下如何用
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-8-31 11:45:00 | 显示全部楼层
该函数有bug,
(replace "o" "n" "test oooo")
(replace "o" "ne" "test oooo")
(replace "o" "new" "test oooo")
(replace "o" "newo" "test oooo")
(replace "o" "newoo" "test oooo")
有可能得出错误的结果

谁能改改吗?
我实在改不过来----脑袋太小

  1.   [FONT=courier new]
  2. (defun replace (f_oldstr f_newstr f_string / f_cur f_loop) ;like subst but with strings not lists
  3.   (if (null (and (stringp f_oldstr)
  4.                  (stringp f_newstr)
  5.                  (stringp f_string)
  6.             )
  7.       )
  8.     (exit)
  9.   )
  10.   (if (> (strlen f_oldstr) (strlen f_string))
  11.     (exit)
  12.   )
  13.   (setq f_loop 1)
  14.   (repeat (strlen f_string)
  15.     (progn
  16.       (setq f_cur (substr f_string f_loop (strlen f_oldstr)))
  17.       (if (= f_cur f_oldstr)
  18.         (setq f_string (strcat (substr f_string 1 (- f_loop 1))
  19.                                f_newstr
  20.                                (substr f_string
  21.                                        (+ f_loop (strlen f_oldstr))
  22.                                        (strlen f_string)
  23.                                )
  24.                        )
  25.         )
  26.       )
  27.       (setq f_loop (1+ f_loop))
  28.     )
  29.   )
  30.   f_string
  31. )
  32. ;eg (replace "\t" " " "Line one\tLine two") = "Line one Line two"

  33.   [/FONT]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-26 17:43 , Processed in 0.228192 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表