明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3507|回复: 21

[求助][AUTOLISP]可否找回HATCH的邊界?

  [复制链接]
发表于 2004-10-31 13:43 | 显示全部楼层 |阅读模式
求助各位﹐可否用AUTOLISP程序求得一个HATCH对象的边界?





本帖子中包含更多资源

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

x
发表于 2004-10-31 14:39 | 显示全部楼层
  1. ;;; HATCHB.LSP ver 1.6
  2. ;;; Recreates hatch boundary by selecting a hatch
  3. ;;; Boundary is created in current layer/color/linetype in WCS
  4. ;;; By Jimmy Bergmark
  5. ;;; Copyright (C) 1997-2002 JTB World, All Rights Reserved
  6. ;;; Website: www.jtbworld.com / http://jtbworld.vze.com
  7. ;;; E-mail: info@jtbworld.com / jtbworld@hotmail.com
  8. ;;; 2000-02-12 - First release
  9. ;;; 2000-03-27 - Counterclockwise arc's and ellipse's fixed
  10. ;;;                           Objects created joined to lwpolyline if possible
  11. ;;;                           Error-handling, undo of command
  12. ;;;                           Can handle PLINETYPE = 0,1,2
  13. ;;; 2000-03-30 - Integrating hatchb and hatchb14
  14. ;;;                           Selection of many hatches
  15. ;;;                           Splines supported if closed.
  16. ;;; 2001-04-02 - Fixed bug with entmake of line with no Z for r14
  17. ;;; 2001-07-31 - Removed an irritating semicolon to enable polylines to be created.
  18. ;;; 2001-10-04 - Changed mail and homepage so it's easy to find when new versions comes up.
  19. ;;; Tested on AutoCAD r14, 2000, 2000i, 2002
  20. ;;; should be working on older versions too.(defun c:hb () (c:hatchb)) ; this line can be commented out if there is an existing command called hb
  21. (defun c:hatchb (/         es       blay   ed1     ed2     loops1           bptf   part
  22.                          et       noe     plist ic       bul     nr       ang1   ang2   obj *ModelSpace* *PaperSpace*
  23.                          space cw errexit undox olderr oldcmdecho ss1 lastent en1 en2 ss lwp
  24.                          list->variantArray 3dPoint->2dPoint A2k ent i ss2
  25.                          knot-list controlpoint-list kn cn pos xv
  26.                        )
  27.    (setq A2k (wcmatch (getvar "ACADVER") "15*"))
  28. (if A2k
  29.    (defun list->variantArray (ptsList / arraySpace sArray)
  30.        (setq arraySpace
  31.            (vlax-make-safearray
  32.                vlax-vbdouble
  33.                (cons 0 (- (length ptsList) 1))
  34.            )
  35.        )
  36.        (setq sArray (vlax-safearray-fill arraySpace ptsList))
  37.        (vlax-make-variant sArray)
  38.    )
  39. )
  40. (if A2k
  41.    (defun 3dPoint->2dPoint (3dpt)
  42.        (list (float (car 3dpt)) (float (cadr 3dpt)))
  43.    )
  44. )   (defun errexit (s)
  45.        (princ "\nError:   ")
  46.        (princ s)
  47.        (restore)
  48.    )   (defun undox ()
  49.        (command "._ucs" "_p")
  50.        (command "._undo" "_E")
  51.        (setvar "cmdecho" oldcmdecho)
  52.        (setq *error* olderr)
  53.        (princ)
  54.    )   (setq olderr   *error*
  55.                restore undox
  56.                *error* errexit
  57.    )
  58.    (setq oldcmdecho (getvar "cmdecho"))
  59.    (setvar "cmdecho" 0)
  60.    (command "._UNDO" "_BE")
  61.    (if A2k (progn
  62.        (vl-load-com)
  63.        (setq *ModelSpace* (vla-get-ModelSpace
  64.                                                  (vla-get-ActiveDocument (vlax-get-acad-object))
  65.                                              )
  66.                    *PaperSpace* (vla-get-PaperSpace
  67.                                                  (vla-get-ActiveDocument (vlax-get-acad-object))
  68.                                              )
  69.        ))
  70.    )
  71. ; For testing purpose
  72. ; (setq A2k nil)
  73.    
  74.    (if (/= (setq ss2 (ssget '((0 . "HATCH")))) nil)
  75.      (progn
  76.        (setq i 0)
  77.        (while (setq ent (ssname ss2 i))
  78.            (setq ed1 (entget ent))
  79.            (if (not (equal (assoc 210 ed1) '(210 0.0 0.0 1.0))) (princ "\nHatch not in WCS!"))
  80.            (setq xv (cdr (assoc 210 ed1)))
  81.            (command "._ucs" "_w")
  82.            (setq loops1 (cdr (assoc 91 ed1))) ; number of boundary paths (loops)
  83.            (if (and A2k (= (strcase (cdr (assoc 410 ed1))) "MODEL"))
  84.                (setq space *ModelSpace*)
  85.                (setq space *PaperSpace*)
  86.            )
  87.            (repeat loops1
  88.                (setq ed1 (member (assoc 92 ed1) ed1))
  89.                (setq bptf (cdr (car ed1))) ; boundary path type flag
  90.                (setq ic (cdr (assoc 73 ed1))) ; is closed
  91.                (setq noe (cdr (assoc 93 ed1))) ; number of edges
  92.                (setq ed1 (member (assoc 72 ed1) ed1))
  93.                (setq bul (cdr (car ed1))) ; bulge
  94.                (setq plist nil)
  95.                (setq blist nil)
  96.                (cond
  97.                    ((> (boole 1 bptf 2) 0) ; polyline
  98.                      (repeat noe
  99.                          (setq ed1 (member (assoc 10 (cdr ed1)) ed1))
  100.                          (setq plist (append plist (list (cdr (assoc 10 ed1)))))
  101.                          (setq blist (append blist
  102.                                                                  (if (> bul 0)
  103.                                                                      (list (cdr (assoc 42 ed1)))
  104.                                                                      nil
  105.                                                                  )
  106.                                                  )
  107.                          )
  108.                      )
  109.                      (if A2k (progn
  110.                          (setq polypoints
  111.                                        (apply 'append
  112.                                                      (mapcar '3dPoint->2dPoint plist)
  113.                                        )
  114.                          )
  115.                          (setq VLADataPts (list->variantArray polypoints))
  116.                          (setq obj (vla-addLightweightPolyline space VLADataPts))
  117.                          (setq nr 0)
  118.                          (repeat (length blist)
  119.                              (if (/= (nth nr blist) 0)
  120.                                  (vla-setBulge obj nr (nth nr blist))
  121.                              )
  122.                              (setq nr (1+ nr))
  123.                          )
  124.                          (if (= ic 1)
  125.                              (vla-put-closed obj T)
  126.                          )
  127.                        )
  128.                        (progn
  129.                            (if (= ic 1)
  130.                                (entmake '((0 . "POLYLINE") (66 . 1) (70 . 1)))
  131.                                (entmake '((0 . "POLYLINE") (66 . 1)))
  132.                            )
  133.                            (setq nr 0)
  134.                            (repeat (length plist)
  135.                                (if (= bul 0)
  136.                                    (entmake (list (cons 0 "VERTEX")
  137.                                                                  (cons 10 (nth nr plist))
  138.                                                      )
  139.                                    )
  140.                                    (entmake (list (cons 0 "VERTEX")
  141.                                                                  (cons 10 (nth nr plist))
  142.                                                                  (cons 42 (nth nr blist))
  143.                                                      )
  144.                                    )
  145.                                )
  146.                                (setq nr (1+ nr))
  147.                            )
  148.                            (entmake '((0 . "SEQEND")))
  149.                        )
  150.                      )
  151.                    )
  152.                    (t ; not polyline
  153.                      (setq lastent (entlast))
  154.                      (setq lwp T)
  155.                      (repeat noe
  156.                          (setq et (cdr (assoc 72 ed1)))
  157.                          (cond
  158.                              ((= et 1) ; line
  159.                                (setq ed1 (member (assoc 10 (cdr ed1)) ed1))
  160.                                (if A2k
  161.                                    (vla-AddLine
  162.                                        space
  163.                                        (vlax-3d-point (cdr (assoc 10 ed1)))
  164.                                        (vlax-3d-point (cdr (assoc 11 ed1)))
  165.                                    )
  166.                                    (entmake
  167.                                        (list
  168.                                            (cons 0 "LINE")
  169.                                            (list 10 (cadr (assoc 10 ed1)) (caddr (assoc 10 ed1)) 0)
  170.                                            (list 11 (cadr (assoc 11 ed1)) (caddr (assoc 11 ed1)) 0)
  171.        ;   (cons 210 xv)
  172.                                        )
  173.                                    )
  174.                                )
  175.                                (setq ed1 (cddr ed1))
  176.                              )
  177.                              ((= et 2) ; circular arc
  178.                                  (setq ed1 (member (assoc 10 (cdr ed1)) ed1))
  179.                                  (setq ang1 (cdr (assoc 50 ed1)))
  180.                                  (setq ang2 (cdr (assoc 51 ed1)))
  181.                                  (setq cw (cdr (assoc 73 ed1)))
  182.                                  (if (equal ang2 6.28319 0.00001)
  183.                                      (progn
  184.                                          (if A2k
  185.                                              (vla-AddCircle
  186.                                                  space
  187.                                                  (vlax-3d-point (cdr (assoc 10 ed1)))
  188.                                                  (cdr (assoc 40 ed1))
  189.                                              )
  190.                                              (entmake (list (cons 0 "CIRCLE")
  191.                                                                            (assoc 10 ed1)
  192.                                                                            (assoc 40 ed1)
  193.                                                                )
  194.                                              )
  195.                                          )
  196.                                          (setq lwp nil)
  197.                                      )
  198.                                      (if A2k
  199.                                          (vla-AddArc
  200.                                              space
  201.                                              (vlax-3d-point (cdr (assoc 10 ed1)))
  202.                                              (cdr (assoc 40 ed1))
  203.                                              (if (= cw 0)
  204.                                                  (- 0 ang2)
  205.                                                  ang1
  206.                                              )
  207.                                              (if (= cw 0)
  208.                                                  (- 0 ang1)
  209.                                                  ang2
  210.                                              )
  211.                                          )
  212.                                          (entmake (list (cons 0 "ARC")
  213.                                                                        (assoc 10 ed1)
  214.                                                                        (assoc 40 ed1)
  215.                                                                        (cons 50
  216.                                                                                    (if (= cw 0)
  217.                                                                                        (- 0 ang2)
  218.                                                                                        ang1
  219.                                                                                    )
  220.                                                                        )
  221.                                                                        (cons 51
  222.                                                                                    (if (= cw 0)
  223.                                                                                        (- 0 ang1)
  224.                                                                                        ang2
  225.                                                                                    )
  226.                                                                        )
  227.                                                            )
  228.                                          )
  229.                                      )
  230.                                  )
  231.                                  (setq ed1 (cddddr ed1))
  232.                              )
  233.                              ((= et 3) ; elliptic arc
  234.                                (setq ed1 (member (assoc 10 (cdr ed1)) ed1))
  235.                                (setq ang1 (cdr (assoc 50 ed1)))
  236.                                (setq ang2 (cdr (assoc 51 ed1)))
  237.                                (setq cw (cdr (assoc 73 ed1)))
  238.                                (if A2k (progn
  239.                                    (setq obj (vla-AddEllipse
  240.                                                            space
  241.                                                            (vlax-3d-point (cdr (assoc 10 ed1)))
  242.                                                            (vlax-3d-point (cdr (assoc 11 ed1)))
  243.                                                            (cdr (assoc 40 ed1))
  244.                                                        )
  245.                                    )
  246.                                    (vla-put-startangle obj (if (= cw 0) (- 0 ang2) ang1))
  247.                                    (vla-put-endangle obj (if (= cw 0) (- 0 ang1) ang2))
  248.                                  )
  249.                                  (princ "\nElliptic arc not supported!")
  250.                                )
  251.                                (setq lwp nil)
  252.                              )
  253.                              ((= et 4) ; spline
  254.                                (setq ed1 (member (assoc 94 (cdr ed1)) ed1))
  255.                                (setq knot-list nil)
  256.                                (setq controlpoint-list nil)
  257. (setq kn (cdr (assoc 95 ed1)))
  258.                                (setq cn (cdr (assoc 96 ed1)))
  259.                                (setq pos (vl-position (assoc 40 ed1) ed1))
  260.                                (repeat kn
  261.                                    (setq knot-list (cons (cons 40 (cdr (nth pos ed1))) knot-list))
  262.                                    (setq pos (1+ pos))
  263.                                )
  264.                                (setq pos (vl-position (assoc 10 ed1) ed1))
  265.                                (repeat cn
  266.                                    (setq controlpoint-list (cons (cons 10 (cdr (nth pos ed1))) controlpoint-list))
  267.                                    (setq pos (1+ pos))
  268.                                )
  269.                                (setq knot-list (reverse knot-list))
  270.                                (setq controlpoint-list (reverse controlpoint-list))
  271.                                (entmake (append
  272.                              (list '(0 . "SPLINE"))
  273.                                                              (list (cons 100 "AcDbEntity"))
  274.                                                              (list (cons 100 "AcDbSpline"))
  275.                                                              (list (cons 70 (+ 1 8 (* 2 (cdr (assoc 74 ed1))) (* 4 (cdr (assoc 73 ed1))))))
  276.                                                              (list (cons 71 (cdr (assoc 94 ed1))))
  277.                                                              (list (cons 72 kn))
  278.                                                              (list (cons 73 cn))
  279.                                                              knot-list
  280.                                                              controlpoint-list
  281.                                            )
  282.                                )
  283. (setq ed1 (member (assoc 10 ed1) ed1))
  284.                                (setq lwp nil)
  285.                              )
  286.                          ) ; end cond
  287.                      ) ; end repeat noe
  288.                      (if lwp (progn
  289.                          (setq en1 (entnext lastent))
  290.                          (setq ss (ssadd))
  291.                          (ssadd en1 ss)
  292.                          (while (setq en2 (entnext en1))
  293.                              (ssadd en2 ss)
  294.                              (setq en1 en2)
  295.                          )
  296.                          (command "_.pedit" (entlast) "_Y" "_J" ss "" "")
  297.                    ))
  298.                    ) ; end t
  299.                ) ; end cond
  300.            ) ; end repeat loops1
  301.            (setq i (1+ i))
  302.        )
  303.      )
  304.    )
  305.    (restore)
  306.    (princ)
  307. )
 楼主| 发表于 2004-10-31 14:49 | 显示全部楼层
谢谢alin。你帮了我的大忙﹗再次感谢!!!
 楼主| 发表于 2004-10-31 15:03 | 显示全部楼层
alin,为什么你的程序对一些HATCH对象无效?你能帮我解决一下吗?


本帖子中包含更多资源

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

x
发表于 2004-10-31 16:18 | 显示全部楼层
虽然不敢保证该程序对所有HATCH都有效,但至少对你图中的都有效。一个个来做,我刚试过。
 楼主| 发表于 2004-10-31 16:30 | 显示全部楼层
alin﹐我在2000i版使用也是可以的﹐但在R14版里使用就不行了﹐我主要是用R14版的。程序好象是VL吧? 有办法解决吗?
发表于 2004-10-31 16:39 | 显示全部楼层
好像R14版也可以安装VLISP吧。没试过。
 楼主| 发表于 2004-10-31 16:55 | 显示全部楼层
alin,我是个超级菜鸟﹐不太懂LISP﹐你教我装VL到R14上面吧。我真的需要这个程序来救急!
发表于 2004-10-31 17:06 | 显示全部楼层
my god,写论文呢?好长啊。。。。。
发表于 2004-10-31 18:56 | 显示全部楼层
那个程序本来就有Bug
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-10 14:04 , Processed in 0.177473 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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