找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 18800|回复: 16

[转贴]:一堆 Lisp 函数

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2003-11-23 00:07:02 | 显示全部楼层 |阅读模式

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

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

×

  1. ;;;; FAQ-CODE.LSP
  2. ;;;; Code from the comp.cad.autocad AutoLISP FAQ
  3. ;;;; (c) 1991-1997 Reini Urban  <rurban@x-ray.at>
  4. ;;;;
  5. ;;;; This code may only be redistributed together with the FAQ document.
  6. ;;;; The FAQ may be freely redistributed in its entirety without
  7. ;;;; modification provided that this copyright notice is not removed. It
  8. ;;;; may not be sold for profit or incorporated in commercial documents
  9. ;;;; (e.g. published for sale on CD-ROM, floppy disks, books, magazines,
  10. ;;;; or other print form) without the prior written permission of the
  11. ;;;; copyright holder. Permission is expressly granted for this document
  12. ;;;; to be made available for file transfer from installations offering
  13. ;;;; unrestricted anonymous file transfer on the Internet and to be
  14. ;;;; included into the official AutoCAD FAQ.
  15. ;;;;
  16. ;;;; These functions are, if not otherwise stated, (c) 1991-97
  17. ;;;; by Reini Urban and may be freely used. If you include some of those
  18. ;;;; functions in your code, you have to add a short line if you intend
  19. ;;;; to ship source code or a seperate document to your program where to
  20. ;;;; find the FAQ and this code.
  21. ;;;;
  22. ;;;; This code is provided AS IS without any expressed or implied warranty.
  23. ;;;;
  24. ;;;; If you intentionally got this copy without the FAQ, get the FAQ at
  25. ;;;;   [url]http://xarch.tu-graz.ac.at/autocad/news/faq/autolisp.html[/url]
  26. ;;;;-----------------------------------------------------------------------
  27. ;;;;
  28. ;;;; These are some basic AutoLISP functions to make life, faq-writing
  29. ;;;; and posting news-answers easier. For more see /autocad/stdlib/
  30. ;;;;
  31. ;;;; Last update: 6.Dec 99
  32. ;;;;
  33. ;;;; Version 2.2     13.Jul 99                    renamed ssapply to ssmap
  34. ;;;; Version 2.0     11.May 98                    fixed: arc2bul, ddecmd, tan
  35. ;;;; Version 1.10    22.July 97                   LWPOLYLINE support and fixes
  36. ;;;; Version 1.9     26.June 97                   pline-segs, ddecmd, getval
  37. ;;;; Version 1.8     15.May 97                    R14 fixes, ssapply
  38. ;;;; Version 1.71    7.May 97                     added (dxf)
  39. ;;;;
  40. ;;;;-----------------------------------------------------------------------


  41. ;;;; [3.3] You may include break functions and debug print into your source
  42. ;;;;       code.

  43. ;;; Debugging functions
  44. (defun break (s)
  45.   (if *BREAK*
  46.     (progn
  47.       (princ "BREAK>> (stop with <Enter>)\nBREAK>> ")
  48.       (princ s)
  49.       (while (/= (setq s (getstring "\nBREAK>> ")) "")
  50.         (print (eval (read s)))
  51.       )
  52.     )
  53.   )
  54. )                                        ;bugfix from v1.3!
  55. (defun dbg-print (s)                        ;accepts atoms and lists
  56.   (if *DEBUG*
  57.     (if        (listp s)
  58.       (mapcar 'print s)
  59.       (print s)
  60.     )
  61.   )
  62. )
  63. (defun C:DEBUG () (setq *DEBUG* (not *DEBUG*))) ;switch it on and off
  64. (defun C:BREAK () (setq *BREAK* (not *BREAK*)))
  65. (defun CONT () (setq *BREAK* nil))        ;cont. without any interruption


  66. ;;;;[15] How can I pass a variable number of arguments to a lisp function?

  67. ;;; MY-PRINC  - print a variable number of arguments (of any type)
  68. (defun my-princ        (x)
  69.   ;; simple version, for better stuff look at the SDK2: PRINTF.LLB
  70.   (if (listp x)
  71.     (mapcar 'princ x)
  72.     (princ x)
  73.   )
  74. )

  75. ;;;; [16] How can I avoid stack overflows?

  76. ;;; INTLST  - create '(0 1 2 ... n)
  77. (defun intlst (n / l)
  78.   (repeat n
  79.     (setq l (cons (setq n (1- n)) l))
  80.   )
  81. )                                        ;this looks ugly but it works


  82. ;;;; [20] general Helper functions

  83. ;;; DXF  - return the DXF group code of an (entget) list
  84. (defun dxf (grp ele) (cdr (assoc grp ele)))

  85. ;;;; [20.1] List manipulation

  86. ;;; CONSP  - a not empty list
  87. (defun consp (x) (and x (listp x)))

  88. ;;; POSITION - returns the index of the first element in the list,
  89. ;;; base 0, or nil if not found
  90. ;;;   (pos x '(a b c)) -> nil, (pos b '(a b c d)) -> 1
  91. (defun position        (x lst / ret)
  92.   (if (not (zerop (setq ret (length (member x lst)))))
  93.     (- (length lst) ret)
  94.   )
  95. )

  96. ;;; REMOVE - Removes an item from a list (double elements allowed)
  97. ;;;   (remove 0 '(0 1 2 3 0)) -> (1 2 3)
  98. (defun remove (ele lst)                        ; by Serge Volkov
  99.   (apply 'append (subst nil (list ele) (mapcar 'list lst)))
  100. )

  101. ;;; REMOVE-IF - Conditional remove from flat list,
  102. ;;; pred requires exactly 1 arg
  103. ;;;   (remove-if 'zerop '(0 1 2 3 0)) -> (1 2 3)
  104. ;;;   (remove-if 'numberp '(0 (0 1) "")) -> ((0 1) "")
  105. (defun remove-if (pred from)
  106.   (cond
  107.     ((atom from) from)                        ;nil or symbol (return that)
  108.     ((apply pred (list (car from))) (remove-if pred (cdr from)))
  109.     (t (cons (car from) (remove-if pred (cdr from))))
  110.   )
  111. )

  112. ;;; REMOVE-IF-NOT  - keeps all elements to which the predicate applies
  113. ;;; say: "keep if", it need not be defined recursively, also like this
  114. (defun remove-if-not (pred lst)                ; by Vladimir Nesterowsky
  115.   (apply 'append
  116.          (mapcar '(lambda (e)
  117.                     (if        (apply pred (list e))
  118.                       (list e)
  119.                     )
  120.                   )
  121.                  lst
  122.          )
  123.   )
  124. )

  125. ;;; ADJOIN - conses ele to list if not already in list
  126. ;;; trick: accepts quoted lists too, such as
  127. ;;;   (setq l '(1 2 3) (adjoin 0 'l)
  128. ;;;    -> !l (0 1 2 3)
  129. (defun adjoin (ele lst / tmp)
  130.   (if (= (type lst) 'SYM)
  131.     (setq tmp lst
  132.           lst (eval tmp)
  133.     )
  134.   )
  135.   (setq        lst (cond ((member ele lst) lst)
  136.                   (t (cons ele lst))
  137.             )
  138.   )
  139.   (if tmp
  140.     (set tmp lst)
  141.     lst
  142.   )
  143. )

  144. ;;; ROT1 - put the first element to the end, simple version
  145. ;;;        Say "rotate by one" or "rotate left"
  146. (defun rot1 (lst) (append (cdr lst) (list (car lst))))

  147. ;;; BUTLAST - the list without the last element
  148. (defun butlast (lst)
  149.   (reverse (cdr (reverse lst)))
  150. )


  151. ;;;; [20.2] string predicates

  152. ;;; STRINGP  - string predicate: "is s a string?"
  153. (defun stringp (s)
  154.   (= (type s) 'STR)
  155. )

  156. ;;; STRING-NOT-EMPTYP  - is str a not empty string?
  157. (defun string-not-emptyp (s)
  158.   (and (stringp s) (/= s ""))
  159. )

  160. ;;; for more list and string manipulation code see
  161. ;;; AI_UTILS.LSP or
  162. ;;; [url]ftp://xarch.tu-graz.ac.at/pub/autocad/lisp/lisp.lsp[/url] and string.lsp
  163. ;;; or at [url]http://xarch.tu-graz.ac.at/autocad/code/vnestr/strtok.lsp[/url]

  164. ;;;; [20.3] symbol->string

  165. ;;; SYMBOL-NAME - returns the name of a symbol as string
  166. ;;; converts any valid lisp expression to its printed representation
  167. ;;; (symbol-name a) -> "a",  (symbol-name '(0 1 2 a)) -> "(0 1 2 A)"
  168. (defun symbol-name (sym / f str tmp)
  169.   (setq tmp "$sym.tmp")                        ;temp. filename, should be deleted
  170.   (setq f (open tmp "w"))
  171.   (princ sym f)
  172.   (close f)
  173.   (setq        f   (open tmp "r")
  174.         str (read-line f)
  175.         f   (close f)
  176.   )
  177.   str
  178. )

  179. ;;;; [20.4] AutoCAD entity access

  180. ;;; GETVAL  - returns the group value of an entity.
  181. ;;; like the wellknown (dxf) function but accepts all kinds of
  182. ;;; entity representations (ename, entget list, entsel list)
  183. (defun GETVAL (grp ele)                        ;"dxf value" of any ent...
  184.   (cond        ((= (type ele) 'ENAME)                ;ENAME
  185.          (cdr (assoc grp (entget ele)))
  186.         )
  187.         ((not ele) nil)                        ;empty value
  188.         ((not (listp ele)) nil)                ;invalid ele
  189.         ((= (type (car ele)) 'ENAME)        ;entsel-list
  190.          (cdr (assoc grp (entget (car ele))))
  191.         )
  192.         (T (cdr (assoc grp ele)))
  193.   )
  194. )                                        ;entget-list

  195. ;;; Ex: (gettyp pline) => "POLYLINE"
  196. (defun GETTYP (ele)                        ;return type
  197.   (getval 0 ele)
  198. )

  199. ;;; ENTITY  - assure ENAME
  200. ;;; convert the entity to type ENAME (to write shorter code)
  201. (defun ENTITY (ele)                        ;convert to element name
  202.   (cond                                        ;accepts the following types:
  203.     ((= (type ele) 'ENAME) ele)                ; ENAME
  204.     ((not (listp ele)) nil)                ; error: no list
  205.     ((= (type (car ele)) 'ENAME) (car ele)) ; entsel-list
  206.     ((cdr (assoc -1 ele)))                ; entget-list or nil
  207.   )
  208. )
  209.                                         ;and now just: (defun getval (grp ele) (cdr (assoc grp (entget (entity ele)))))

  210. ;;; Ex: (istypep ele "TEXT")
  211. ;;; is element a "SOLID"?
  212. (defun istypep (ele typ)                ;check type
  213.   (= (gettyp ele) typ)
  214. )

  215. ;;; Ex: (istypep ele '("TEXT" "ATTDEF"))
  216. ;;; is element a "TEXT" or a "ATTDEF"?
  217. (defun ISTYPEP (ele typ)                ;better implementation to accept lists too
  218.   (cond
  219.     ((listp typ) (member (gettyp ele) typ))
  220.     ((stringp typ) (= (gettyp ele) typ));assume typ uppercase, wcmatch is slower but neater
  221.     (T nil)
  222.   )
  223. )

  224. ;;; Ex: (getpt (entsel))  => ( 0.1 10.0 24)
  225. (defun GETPT (ele)                        ;return the startpoint of any element
  226.   (getval 10 ele)
  227. )                                        ;group 10

  228. ;;; Ex: (getflag pline)  => 1 if closed
  229. (defun GETFLAG (ele) (getval 70 ele))        ;same with the entity flag

  230. ;;; FLAGSETP  - bitvalue val in flag of element set?
  231. ;;; Ex: (flagsetp 1 pline)   => T if closed
  232. ;;; Ex: (flagsetp 16 vertex) => T if spline control point
  233. (defun FLAGSETP        (val ele)
  234.   (bitsetp val (getflag ele))
  235. )

  236. ;;; Ex: (bitsetp 4 12) => T   ;bitvalue 4 (=2.Bit) in 12 (=4+8) is set
  237. (defun BITSETP (val flag)
  238.   (= (logand val flag) val)
  239. )

  240. ;;; SSLIST - convert selection set to list. slow, but easy to write.
  241. ;;; Note: it's also wise to use ai_ssget, because some ents could be
  242. ;;;       on locked layers
  243. ;;; Ex: (sslist (ai_ssget (ssget))) => list of selected unlocked ents
  244. ;;; or  (mapcar 'entupd (sslist (ssget "X" '((8 . "TEMP")))))
  245. ;;;       - regens all entities on layer TEMP
  246. (defun SSLIST (ss / n lst)
  247.   (if (= 'PICKSET (type ss))
  248.     (repeat (setq n (sslength ss))
  249.       (setq n        (1- n)
  250.             lst        (cons (ssname ss n) lst)
  251.       )
  252.     )
  253.   )
  254. )

  255. ;;; SSMAP - apply a function to each ent in ss, in reversed order
  256. ;;; Faster, but not so easy to understand. see [22.2] Datestamp
  257. ;;; [renamed from SSAPPLY to SSMAP to match the stdlib name]
  258. ;;; Ex: (ssmap 'entupd (ssget))   ; regenerate only some entities
  259. (defun SSMAP (fun ss / n)
  260.   (if (= 'PICKSET (type ss))
  261.     (repeat (setq n (sslength ss))
  262.       (apply fun (list (ssname ss (setq n (1- n)))))
  263.     )
  264.   )
  265. )
  266. ;;; backwards compatibility alias:
  267. (setq ssapply ssmap)

  268. ;;; [21.2] Plot dialog from within Lisp. Using DDE

  269. ;;;  R13 code! For R12 use "autocad.dde" as the server name. Then, inside your lisp
  270. ;;;  or script, you can do (ddecmd "_plot "). Function DDECMD will return
  271. ;;;  nil if something wrong, or the string you passed if successful. The
  272. ;;;  string is just like what you type under the command prompt from
  273. ;;;  keyboard, so you need put a space or a return, which is "^13" here,
  274. ;;;  to end the string.
  275. ;;;  Besides, the function is very useful in the following situation: If
  276. ;;;  within a lisp, you need call an AutoCAD transparent command like
  277. ;;;  LAYER, normally you will use (command "_layer"), but after use this
  278. ;;;  line, the lisp own will not be transparent. Using the function, you
  279. ;;;  will solve this problem.
  280. ;;; [fixed for all releases]
  281. (defun DDECMD (str / tmp acadver ddestr)
  282.   (if (not (boundp 'initiate))
  283.     (cond
  284.       ((= 14 (setq acadver (atoi (getvar "ACADVER"))))
  285.        (setq ddestr "AutoCAD.R14.DDE")
  286.        (arxload "ddelisp")
  287.       )
  288.       ((= 13 acadver)
  289.        (setq ddestr "autocad.r13.dde")
  290.        (xload "ddelisp")
  291.       )
  292.       ((= 12 acadver)
  293.        (setq ddestr "autocad.dde")
  294.        (xload "ddelisp")
  295.       )
  296.       (T (princ "DDE not supported") (exit))
  297.     )
  298.   )
  299.   (if (not (zerop (setq tmp (initiate ddestr "system"))))
  300.     (progn
  301.       (execute tmp (strcat "[" str "]"))
  302.       (terminate tmp)
  303.       str
  304.     )
  305.   )
  306. )

  307. ;;; Visual Lisp Example:
  308. ;|
  309.   (setq vlax:ActiveDocument (vla-get-ActiveDocument (vlax-get-Acad-Object)))
  310.   (setq plt (vla-get-plot vlax:ActiveDocument))   ;=> plot object
  311.   (vla-PlotWindow plt pt1 pt2)                    ; define window (pts in WCS)
  312.   (vla-PlotPreview plt 1)                         ; 0 for partial, 1 for full
  313.   (vla-PlotToDevice plt "Default System Printer") ; if it exists
  314. |;

  315. ;;;; [21.3] (entmod) and (entmake) Layers, without (command "_LAYER"...)

  316. ;;; This sample routine will create a layer with any name you type:
  317. ;;; by Reinaldo Togores <rtogores@mundivia.es>
  318. (defun C:MLAY ()
  319.   (setq laynam (getstring "\nLayer name: "))
  320.   (entmake
  321.     (list
  322.       '(0 . "LAYER")
  323.       '(5 . "28")
  324.       '(100 . "AcDbSymbolTableRecord")
  325.       '(100 . "AcDbLayerTableRecord")
  326.       (cons 2 laynam)
  327.       '(70 . 64)
  328.       '(62 . 7)
  329.       '(6 . "CONTINUOUS")
  330.     )
  331.   )
  332. )

  333. ;;;; [21.6] (vports), VIEWPORT entity, pixel conversion

  334. ;;; Conversion pixel to drawing units
  335. (defun pix2units (pix)
  336.   (* pix (/ (getvar "VIEWSIZE") (cadr (getvar "SCREENSIZE"))))
  337. )

  338. ;;; Conversion drawing units to pixel
  339. (defun units2pix (units)
  340.   (* units
  341.      (/ (cadr (getvar "SCREENSIZE")) (getvar "VIEWSIZE"))
  342.   )
  343. )

  344. ;;;;[21.7] Select all visible objects: zoom coordinates

  345. ;;; returns a list of the actual viewport corners in WCS
  346. (defun zoompts (/ ctr h screen ratio size size_2)
  347.   (setq        ctr    (xy-of (getvar "VIEWCTR")) ;3D -> 2D
  348.         h      (getvar "VIEWSIZE")        ;real
  349.         screen (getvar "SCREENSIZE")        ;2D: Pixel x,y
  350.         ratio  (/ (float (car screen))        ;aspect ratio
  351.                   (cadr screen)
  352.                )
  353.         size   (list (* h ratio) h)        ;screensize in coords
  354.         size_2 (mapcar '/ size '(2.0 2.0))
  355.   )
  356.   (list        (mapcar '- ctr size_2)
  357.         (mapcar '+ ctr size_2)
  358.   )
  359. )
  360. (defun xy-of (pt) (list (car pt) (cadr pt))) ;assure 2D coords

  361. ;;; returns all visible entities as a selection set
  362. ;;; one way to define this function
  363. (defun ssall-visible (/ l)
  364.   (ssget "C" (car (setq l (maptrans0-1 (zoompts)))) (cadr l))
  365. )
  366. ;;; or another
  367. (defun ssall-visible-1 ()                ;combine "C" and (p1 p2) to one list
  368.   (apply 'ssget (append '("C") (maptrans0-1 (zoompts))))
  369. )

  370. ;;; map some pts from WCS to UCS, easier with just one argument
  371. (defun maptrans0-1 (pts)
  372.   (mapcar '(lambda (pt) (trans pt 0 1)) pts)
  373. )


  374. ;;;; [21.8] How to write XYZ data of selected objects to a file?

  375. ;;; CDF - comma delimited string
  376. (defun cdf-point (pt)
  377.   (strcat (car pt) ", " (cadr pt) ", " (caddr pt))
  378. )

  379. ;;; SDF - space delimited, may easier be read back in to AutoCAD
  380. (defun sdf-point (pt)
  381.   (strcat (car pt) " " (cadr pt) " " (caddr pt))
  382. )

  383. ;;; convert this SDF format back to a point with
  384. (defun str->point (s)
  385.   (eval (read (strcat "(" s ")")))
  386. )

  387. ;;; Write a XYZ file of all selected objects (SDF see below)
  388. (defun C:XYZ (/ ss fname f)
  389.   (if (and (setq ss (ssget))
  390.            (setq fname (getfiled "Write XYZ to file"
  391.                                  (strcat (getvar "DWGNAME") ".XYZ")
  392.                                  "XYZ"
  393.                                  7
  394.                        )
  395.            )
  396.            (setq f (open fname "w"))
  397.       )
  398.     (foreach ele (sslist ss)                ; -> [20.4]
  399.       (foreach pt (getpts ele)                ; -> [23.1]
  400.         (write-line (cdf-point pt) f)
  401.       )
  402.     )
  403.   )
  404.   (if f
  405.     (close f)
  406.   )
  407. )
  408. ;;; => <fname>.xyz
  409. ;;; 0.45, 12.3, -34.0

  410. ;;; For a ASC file (SDF-format) simply change all XYZ to ASC
  411. ;;; and cdf-point to sdf-point above.
  412. (defun C:ASC (/ ss fname f)
  413.   (if (and (setq ss (ssget))
  414.            (setq fname (getfiled "Write ASC to file"
  415.                                  (strcat (getvar "DWGNAME") ".ASC")
  416.                                  "ASC"
  417.                                  7
  418.                        )
  419.            )
  420.            (setq f (open fname "w"))
  421.       )
  422.     (foreach ele (sslist ss)                ; -> [20.4]
  423.       (foreach pt (getpts ele)                ; -> [23.1]
  424.         (write-line (sdf-point pt) f)
  425.       )
  426.     )
  427.   )
  428.   (if f
  429.     (close f)
  430.   )
  431. )

  432. ;;;; [22] Block Attributes

  433. ;;; ATTELE  - returns entget-list of attribute attname (STRING) in element
  434. ;;; ele or nil if not found
  435. (defun attele (ele attname / rslt)
  436.   (if (and (istypep ele "INSERT")
  437.            (= (getval 66 ele) 1)
  438.       )
  439.     (progn
  440.       (setq ele (entnext (entity ele)))
  441.       (while (and ele (istypep ele "ATTRIB"))
  442.         (if (= (strcase (getval 2 ele)) (strcase attname))
  443.           (setq        rslt (entget ele)
  444.                 ele  nil
  445.           )                                ;break the loop
  446.           (setq ele (entnext ele))
  447.         )
  448.       )
  449.     )
  450.   )
  451.   rslt
  452. )

  453. ;;; ATTCHG  - change the attribute value of INSERT ele to new (group 1)
  454. (defun attchg (ele attname new / b)
  455.   (if (setq b (attele ele attname))
  456.     (entmod (subst (cons 1 new) (getval 1 b) b))
  457.   )
  458. )

  459. ;;; Change all DATESTAMP attributes in all inserted PLOT* blocks
  460. (defun C:DATESTAMP ()
  461.   (ssmap
  462.     '(lambda (ele)
  463.        (attchg ele "DATESTAMP" (today))
  464.        (entupd ele)
  465.      )
  466.     (ssget "X" '((0 . "INSERT") (2 . "PLOT*")))
  467.   )
  468. )

  469. ;;; TODAY  - return todays date, could be a DIESEL or this string conversion
  470. ;;; with DIESEL it's easier to define it according your format (i.e day of week)
  471. (defun today (/ s)
  472.   (setq s (rtos (getvar "CDATE") 2))        ;gets the julian date
  473.   (strcat (substr s 5 2)
  474.           "-"
  475.           (substr s 7 2)
  476.           "-"
  477.           (substr s 3 2)
  478.   )
  479. )

  480. ;;; MAIN-ENTITY - some more helper funcs to get the main entity of any attribute
  481. ;;; or vertex
  482. (defun main-entity (ele)
  483.   (setq b (entity b))                        ;force ENAME
  484.   (while (istypep b '("ATTRIB" "ATTDEF" "VERTEX"))
  485.     (setq b (entnext b))
  486.   )                                        ;loop until no more sub-ents
  487.   (if (istypep b '("SEQEND" "ENDBLK"))
  488.     (getval -2 b)                        ;complex entity -> header
  489.     b                                        ;normal entity
  490.   )
  491. )

  492. ;;;; [23] Polylines

  493. ;;; return only some assoc values in the list (for LWPOLYLINE)
  494. (defun GROUP-ONLY (grp lst)
  495.   (mapcar 'cdr
  496.           (remove-if-not '(lambda (pair) (= grp (car pair))) lst)
  497.   )
  498. )

  499. ;;; return the vertex list of a polyline or of any other element
  500. ;;; Note that with edlgetent mentioned in [22.1] it's a one-liner
  501. (defun GETPTS (ele / pts)
  502.   (setq ele (entity ele))                ;force type ENAME
  503.   (cond
  504.     ((istypep ele "POLYLINE")
  505.      (while (istypep (setq ele (entnext ele)) "VERTEX")
  506.        ;; omit fit and spline points  (conservative style)
  507.        (if (not (or (flagsetp 1 ele) (flagsetp 8 ele))) ;bugfix!
  508.          (setq pts (cons (trans (getpt ele) ele 0) pts))
  509.        )
  510.        (reverse pts)
  511.      )
  512.     )
  513.     ;; Special case: you have to map it, assoc finds only the first.
  514.     ;; Fix a LWPOLYLINE bug in R14: internally stored as 2d point,
  515.     ;;   (entget) returns fantasy z-values.
  516.     ((istypep ele "LWPOLYLINE")
  517.      (mapcar '(lambda (pt) (trans (list (car pt) (cadr pt) 0.0) ele 0))
  518.              (group-only 10 (entget ele))
  519.      )
  520.     )
  521.     ;; insert here possible other types, such as
  522.     ((istypep ele '("TEXT" "CIRCLE")) (list (getpt ele)))
  523.     ;; more like this (serge's style)
  524.     (T
  525.      (apply 'append
  526.             (mapcar
  527.               '(lambda (n / p)
  528.                  (if (setq p (getval n ele))
  529.                    (list p)
  530.                  )
  531.                )
  532.               '(10 11 12 13)
  533.             )
  534.      )
  535.     )
  536.     ;; or like this (conservative style)
  537.     ;;(T (foreach n '(10 11 12 13)
  538.     ;;     (if (setq p (getval n ele)) (setq pts (cons p pts))))
  539.     ;;  pts
  540.     ;;)
  541.   )
  542. )

  543. ;;; This sample converts all selected elements to polylines and
  544. ;;;  tries to join as much as possible.
  545. (defun C:JOINPOLY (/ ele ss)
  546.   (foreach ele (sslist (setq ss (ssget))) ;better process lists
  547.     (if        (entget ele)                        ;not already joined
  548.       (cond                                ;(then it would be nil)
  549.         ((istypep ele '("ARC" "LINE"))
  550.          ;; in fact you should check Z of lines and UCS here too
  551.          (command "_PEDIT" ele "_Y" "_J" ss "" "") ; convert and JOIN
  552.         )
  553.         ((and (istypep ele '("POLYLINE" "LWPOLYLINE")) ;bugfix
  554.               (not (flagsetp 1 ele))        ;not closed
  555.               (< (rem (getflag ele) 128) 8)
  556.          )                                ;ignore meshes and such
  557.          (command "_PEDIT" ele "_J" ss "" "") ;ucs check omitted
  558.         )
  559.       )
  560.     )
  561.   )
  562. )

  563. ;;; Sets new polywidth for multiple plines
  564. (defun C:POLYWID (/ wid ele)
  565.   (initget 5)
  566.   (setq wid (getdist "New Polyline Width: ")) ;not negative
  567.   (foreach ele (sslist (ssget '((0 . "*POLYLINE")))) ;only PLINES
  568.     (command "_PEDIT" ele "_W" wid "")
  569.   )
  570. )

  571. ;;; Draws a POLYLINE entity from a list of points (same with SPLINE,
  572. ;;;  or LINE), on the actual UCS, with actual OSNAP settings
  573. (defun draw-pline (pts)
  574.   (command "_PLINE")
  575.   (mapcar 'command pts)
  576.   (command "")
  577. )
  578. (defun draw-spline (pts)
  579.   (command "_SPLINE")
  580.   (mapcar 'command pts)                        ; the pts must be the fitpoints then
  581.   (command "" "" "")
  582. )

  583. ;;; add up the LENGTH of all selected objects, NOISY, you can do the
  584. ;;; same with AREAs: simply change the last line to (getvar "AREA")
  585. (defun C:LEN-OF        ()
  586.   (command "_AREA" "_A" "_E")                ;add up objects (R12+13)
  587.   (ssmap 'command (ssget))                ;pass all elements to AutoCAD
  588.   (command "" "")                        ;two returns
  589.   (getvar "PERIMETER")
  590. )                                        ;this is the length

  591. ;;; calculates length of a pline, quiet
  592. (defun POLY-LENGTH (poly / seg)
  593.   (apply '+                                ; the sum of all single segment lengths
  594.          (mapcar
  595.            '(lambda (seg)                ;length of one segment
  596.               (if (zerop (car seg))        ;is it straight?
  597.                 (distance (cadr seg) (caddr seg)) ; line segment or
  598.                 (abs (arclen seg))
  599.               )
  600.             )                                ; curved: -> [24]
  601.            (pline-segs poly)
  602.          )
  603.   )
  604. )                                        ;segment list (bulge p1 p2)

  605. ;;; returns all group codes of the complex element
  606. ;;; (vertices, attributes) as list, similar to (edlgetent)
  607. (defun CPLX-LIST (grp ele / lst)
  608.   (if (= 1 (getval 66 ele))
  609.     (progn (setq ele (entnext (entity ele)))
  610.            (while (and ele (not (istypep ele "SEQEND")))
  611.              (setq lst (cons (getval grp ele) lst)
  612.                    ele (entnext ele)
  613.              )
  614.            )
  615.            (reverse lst)
  616.     )
  617.   )
  618. )

  619. ;;; PLINE-SEGS  - Creates a segment list for the polyline pname
  620. ;;;   as a list of '(bulge p1 p2). A straight line has bulge 0.0
  621. ;;; Compute pts in ECS of pname. Accepts LWPOLYLINE's
  622. (defun pline-segs (pname / pts segs)
  623.   (setq        segs
  624.          (mapcar 'list
  625.                  (if (istypep pname "LWPOLYLINE")
  626.                    (group-only 42 (entget pname))
  627.                    (cplx-list 42 pname)
  628.                  )
  629.                  (setq pts (getpts pname)) ; ->[23.1]
  630.                  (rot1 pts)
  631.          )
  632.   )                                        ; ->[20.1]
  633.   (if (flagsetp 1 pname)
  634.     segs                                ;closed
  635.     (butlast segs)
  636.   )
  637. )                                        ;open: without the last segment, ->[20.1]

  638. ;;; Example:   (a bit optimized for brevity :)
  639. ;;; Add up all the lengths of all selected polylines, quiet
  640. ;;; To accept also other entities, add those to pline-segs
  641. (defun C:POLYLEN ()
  642.   (apply '+ (ssmap 'poly-length (ssget '((0 . "*POLYLINE")))))
  643. )

  644. ;;;; [24] Circle/Arc Geometry: BULGE conversion, some trigonometry

  645. ;;; SEG2CIR - converts a bulged segment (bulge pt1 pt2) of a polyline
  646. ;;;   to a circle (ctr rad), the start- and endpoints are known
  647. ;;;   therefore the angles too: (angle ctr pt1)(angle ctr pt2)
  648. ;;; returns nil on a straight segment!
  649. (defun seg2cir (seg / bulge p1 p2 cot x y rad dummy)
  650.   (if (zerop (car seg))                        ;straight line => invalid circle
  651.     nil
  652.     (setq bulge        (car seg)
  653.           p1        (cadr seg)
  654.           p2        (caddr seg)
  655.           cot        (* 0.5 (- (/ 1.0 bulge) bulge))
  656.           x        (/ (- (+ (car p1) (car p2)) (* (- (cadr p2) (cadr p1)) cot))
  657.                    2.0
  658.                 )
  659.           y        (/ (+ (+ (cadr p1) (cadr p2)) (* (- (car p2) (car p1)) cot))
  660.                    2.0
  661.                 )
  662.           rad        (distance (list (car p1) (cadr p1)) (list x y))
  663.           dummy        (list (list x y) rad)        ; return this, I hate progn's
  664.     )
  665.   )
  666. )

  667. ;;; ARC2SEG - inverse conversion:
  668. ;;; calculates segment (bulge p1 p2) of arc
  669. ;;;   with given circle (ctr rad), start-angle, end-angle
  670. ;;;   (arc2seg cir (angle (car cir) p1) (angle (car cir) p2)) =>seg
  671. (defun arc2seg (cir ang1 ang2 / p1 p2)
  672.   (setq        p1 (polar (car cir) ang1 (cadr cir))
  673.         p2 (polar (car cir) ang2 (cadr cir))
  674.   )
  675.   (list (arc2bul p1 p2 cir) p1 p2)
  676. )

  677. ;;; ARC2BUL - calculates bulge of arc given the arc points and the
  678. ;;;   circle (ctr rad) [fixed by Serge Pashkov]
  679. (defun arc2bul (p1 p2 cir / ang)
  680.   (setq ang (- (angle (car cir) p2) (angle (car cir) p1)))
  681.   (if (minusp ang)
  682.     (setq ang (+ (* 2.0 pi) ang))
  683.   )
  684.   (tan (/ ang 4.0))
  685. )

  686. ;;; BUL2ANG - returns inner angle of arc (bulge)
  687. (defun bul2ang (seg / ctr)
  688.   (- (angle (setq ctr (car (seg2cir seg))) (cadr seg))
  689.      (angle ctr (caddr seg))
  690.   )
  691. )

  692. ;;; ARC2ANG
  693. ;;; calculates angle of arc given the chord distance and radius
  694. (defun arc2ang (chord rad)
  695.   (* 2.0
  696.      (atan
  697.        (/ chord
  698.           2.0
  699.           (sqrt        (- (expt rad 2)
  700.                    (expt (/ chord 2.0) 2)
  701.                 )
  702.           )
  703.        )
  704.      )
  705.   )
  706. )

  707. ;;; ARCLEN   - length of arc   = radius*angle,
  708. ;;; Note: +-, you'll need (abs (arclen seg))
  709. (defun arclen (seg)
  710.   (* (cadr (seg2cir seg))                ; radius
  711.      4.0
  712.      (atan (car seg))
  713.   )
  714. )                                        ; angle = 4*atan(bulge)

  715. (setq *INFINITY* 1.7e308)                ; largest double
  716. (defun tan (z / cosz)                        ; [fixed]
  717.   (if (zerop (setq cosz (cos z)))
  718.     *INFINITY*
  719.     (/ (sin z) cosz)
  720.   )
  721. )
  722. (defun dtr (ang) (* pi (/ ang 180.0)))        ; degree to radian
  723. (defun rtd (ang) (/ (* ang 180.0) pi))        ; radian to degree

  724. ;;;;[26] EED Extended Entity Data: Select, Get and Store

  725. ;;; here is how to get the eed list from one element for all regapps
  726. (defun get-eedlist-all (ele)
  727.   (cdadr (assoc -3 (entget (entity ele) '("*"))))
  728. )

  729. ;;; this gets all elements of appnames rname (wildcards allowed)
  730. (defun ssget-app (rname)
  731.   (ssget "X" (list (list -3 (list rname))))
  732. )

  733. ;;; Check any XDATA with:   (entget (car (entsel)) '("*"))

  734. ;;; GETXDATA - get all XDATA lists from an element
  735. ;;; i.e with XDATA:
  736. ;;; (-3  ("HUBU-1" (1000 ."ASSHATCH")(1002 ."{")
  737. ;;;                (1070 . 1)(1002 ."}")))
  738. ;;; =>(("HUBU-1" (1000 ."ASSHATCH")(1002 ."{")(1070 . 1)(1002 ."}")))
  739. (defun getxdata        (e apnlst)
  740.   (cdr (assoc -3 (entget e apnlst)))
  741. )

  742. ;;; GETXDATA-ALL - all lists without the regapp name
  743. ;;; => ((1000 ."ASSHATCH")(1002 ."{")(1070 . 1)(1002 ."}"))
  744. (defun getxdata-all (e apnlst)
  745.   (apply 'append (mapcar 'cdr (getxdata e apnlst)))
  746. )
  747. ;;; Conversion pixel to drawing units
  748. (defun PIX2UNITS (pix)
  749.   (* pix (/ (getvar "VIEWSIZE") (cadr (getvar "SCREENSIZE"))))
  750. )

  751. ;;; Conversion drawing units to pixel
  752. (defun UNITS2PIX (units)
  753.   (* units
  754.      (/ (cadr (getvar "SCREENSIZE")) (getvar "VIEWSIZE"))
  755.   )
  756. )

本帖被以下淘专辑推荐:

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

使用道具 举报

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

使用道具 举报

发表于 2004-6-11 21:26:54 | 显示全部楼层
太完美了,对于组、弧线等各种方面进行了编程,唯一的遗憾兄弟们都发现了,完全英文使用起来并不是非常舒适,望楼主提供使用说明,哪怕是很简单的都好,谢谢!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

使用道具 举报

已领礼包: 194个

财富等级: 日进斗金

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

使用道具 举报

已领礼包: 593个

财富等级: 财运亨通

 楼主| 发表于 2005-2-7 12:15:48 | 显示全部楼层
最初由 shuaier 发布
[B]请使用国语,谢谢! [/B]

要是能有懂国语的编程语言岂不更好?!
直接写“打印面积”,就给你显示出来:)
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

使用道具 举报

已领礼包: 717个

财富等级: 财运亨通

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

已领礼包: 20个

财富等级: 恭喜发财

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

使用道具 举报

已领礼包: 7个

财富等级: 恭喜发财

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 23:08 , Processed in 0.290308 second(s), 61 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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