明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: xyxy

请教如何以图块最外层的边界修剪图块下面的直线?

  [复制链接]
发表于 2013-1-2 01:52 | 显示全部楼层
我就不写了,lee大已经写过一个这样的程序了,很强大
非常好的例子
  1. ;;----------------=={ Automatic Block Break }==---------------;;
  2. ;;                                                            ;;
  3. ;;  Prompts user for selection of a block, then point for     ;;
  4. ;;  insertion.                                                ;;
  5. ;;                                                            ;;
  6. ;;  If a curve is detected at the selected point, the         ;;
  7. ;;  inserted block is rotated to align with the curve.        ;;
  8. ;;                                                            ;;
  9. ;;  All surrounding objects found to intersect with the block ;;
  10. ;;  are then trimmed to the rectangular block outline.        ;;
  11. ;;                                                            ;;
  12. ;;  Program works in all views & UCS.                         ;;
  13. ;;------------------------------------------------------------;;
  14. ;;  Author: Lee Mac, Copyright ?2011 - www.lee-mac.com       ;;
  15. ;;------------------------------------------------------------;;
  16. ;;  Version 1.0    -    22.11.2010                            ;;
  17. ;;                                                            ;;
  18. ;;  First Release.                                            ;;
  19. ;;------------------------------------------------------------;;
  20. ;;  Version 1.1    -    07.02.2011                            ;;
  21. ;;                                                            ;;
  22. ;;  Entire program rewritten to allow subfunction to be       ;;
  23. ;;  called with block object argument.                        ;;
  24. ;;  Multiple intersecting objects are trimmed.                ;;
  25. ;;------------------------------------------------------------;;
  26. ;;  Version 1.2    -    08.02.2011                            ;;
  27. ;;                                                            ;;
  28. ;;  Changed block insertion to VL InsertBlock method.         ;;
  29. ;;  Added calling function to trim a block in-situ (ABBE).    ;;
  30. ;;------------------------------------------------------------;;
  31. ;;  Version 1.3    -    03.08.2011                            ;;
  32. ;;                                                            ;;
  33. ;;  Altered method to create bounding polyline to exclude     ;;
  34. ;;  attributes when trimming objects surrounding block.       ;;
  35. ;;  Objects surrounding blocks whose insertion point does not ;;
  36. ;;  lie on a curve are now also trimmed.                      ;;
  37. ;;------------------------------------------------------------;;
  38. ;;  Version 1.4    -    30.09.2011                            ;;
  39. ;;                                                            ;;
  40. ;;  Added option to enable/disable automatic block rotation.  ;;
  41. ;;  Updated code formatting.                                  ;;
  42. ;;------------------------------------------------------------;;

  43. (defun c:ABB ( / *error* _StartUndo _EndUndo acspc block obj pt sel )

  44.     (defun *error* ( msg )
  45.         (if acdoc (_EndUndo acdoc))
  46.         (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
  47.             (princ (strcat "\nError: " msg))
  48.         )
  49.         (princ)
  50.     )

  51.     (defun _StartUndo ( doc )
  52.         (_EndUndo doc)
  53.         (vla-StartUndoMark doc)
  54.     )

  55.     (defun _EndUndo ( doc )
  56.         (if (= 8 (logand 8 (getvar 'UNDOCTL)))
  57.             (vla-EndUndoMark doc)
  58.         )
  59.     )

  60.     (setq acdoc (cond (acdoc) ((vla-get-activedocument (vlax-get-acad-object))))
  61.           acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'paperspace 'modelspace))
  62.     )
  63.     (if
  64.         (and
  65.             (progn
  66.                 (while
  67.                     (progn (setvar 'ERRNO 0) (initget "Browse Rotation")
  68.                         (princ (strcat "\nAutomatic Block Rotation: " (getenv "LMac\\ABBRotation")))
  69.                         (setq sel
  70.                             (entsel
  71.                                 (strcat "\nSelect Block [Browse/Rotation]"
  72.                                     (if (eq "" (setq block (getvar 'INSNAME))) ": " (strcat " <" block "> : "))
  73.                                 )
  74.                             )
  75.                         )
  76.                         (cond
  77.                             (   (= 7 (getvar 'ERRNO))
  78.                                 (princ "\nMissed, Try Again.")
  79.                             )
  80.                             (   (not sel)
  81.                                 (if (eq "" block) (setq block nil))
  82.                             )
  83.                             (   (eq "Rotation" sel)
  84.                                 (initget "ON OFF")
  85.                                 (setenv "LMac\\ABBRotation"
  86.                                     (cond
  87.                                         (
  88.                                             (getkword
  89.                                                 (strcat "\nAutomatic Block Rotation [ON/OFF] <"
  90.                                                     (getenv "LMac\\ABBRotation") ">: "
  91.                                                 )
  92.                                             )
  93.                                         )
  94.                                         (   (getenv "LMac\\ABBRotation")   )
  95.                                     )
  96.                                 )
  97.                             )
  98.                             (   (eq "Browse" sel)
  99.                                 (setq block (getfiled "Select Block" "" "dwg" 16))
  100.                                 nil
  101.                             )
  102.                             (   (listp sel)
  103.                                 (if (not (eq "INSERT" (cdr (assoc 0 (entget (car sel))))))
  104.                                     (princ "\nObject Must be a Block.")
  105.                                     (not (setq obj (vla-copy (vlax-ename->vla-object (car sel)))))
  106.                                 )
  107.                             )
  108.                         )
  109.                     )
  110.                 )
  111.                 block
  112.             )
  113.             (setq pt (getpoint "\nSpecify Point for Block: "))
  114.             (or obj
  115.                 (setq obj
  116.                     (vla-InsertBlock acspc (vlax-3D-point (trans pt 1 0)) block 1. 1. 1.
  117.                         (angle '(0. 0. 0.) (trans (getvar 'UCSXDIR) 0 (trans '(0. 0. 1.) 1 0 t) t))
  118.                     )
  119.                 )
  120.             )
  121.         )
  122.         (progn
  123.             (_StartUndo acdoc)
  124.             (if block (setvar 'INSNAME (vl-filename-base block)))
  125.             (vla-put-InsertionPoint obj (vlax-3D-point (trans pt 1 0)))
  126.             (LM:AutoBlockBreak obj (eq "ON" (getenv "LMac\\ABBRotation")))
  127.             (_EndUndo acdoc)
  128.         )
  129.     )
  130.     (princ)
  131. )

  132. ;;------------=={ Automatic Block Break Existing }==----------;;
  133. ;;                                                            ;;
  134. ;;  Prompts user for selection of a block and, if a curve is  ;;
  135. ;;  detected at the block insertion point, the block is       ;;
  136. ;;  rotated to align with the curve. All objects found to     ;;
  137. ;;  intersect with the block are then trimmed to the          ;;
  138. ;;  rectangular block outline.                                ;;
  139. ;;                                                            ;;
  140. ;;  Program works in all views & UCS.                         ;;
  141. ;;------------------------------------------------------------;;
  142. ;;  Author: Lee Mac, Copyright ?2011 - www.lee-mac.com       ;;
  143. ;;------------------------------------------------------------;;

  144. (defun c:ABBE ( / sel )
  145.     (while
  146.         (progn
  147.             (setvar 'ERRNO 0)
  148.             (initget "Rotation")
  149.             (princ (strcat "\nAutomatic Block Rotation: " (getenv "LMac\\ABBRotation")))
  150.             (setq sel (entsel "\nSelect Block to Trim [Rotation]: "))
  151.             (cond
  152.                 (   (= 7 (getvar 'ERRNO))
  153.                     (princ "\nMissed, Try Again.")
  154.                 )
  155.                 (   (eq "Rotation" sel)
  156.                     (initget "ON OFF")
  157.                     (setenv "LMac\\ABBRotation"
  158.                         (cond
  159.                             (
  160.                                 (getkword
  161.                                     (strcat "\nAutomatic Block Rotation [ON/OFF] <"
  162.                                         (getenv "LMac\\ABBRotation") ">: "
  163.                                     )
  164.                                 )
  165.                             )
  166.                             (   (getenv "LMac\\ABBRotation")   )
  167.                         )
  168.                     )
  169.                 )
  170.                 (   (eq 'ENAME (type (car sel)))
  171.                     (if (eq "INSERT" (cdr (assoc 0 (entget (car sel)))))
  172.                         (LM:AutoBlockBreak (car sel) (eq "ON" (getenv "LMac\\ABBRotation")))
  173.                         (princ "\nInvalid Object Selected.")
  174.                     )
  175.                     t
  176.                 )
  177.             )
  178.         )
  179.     )
  180.     (princ)
  181. )

  182. ;;-----------=={ Automatic Block Break Selection }==----------;;
  183. ;;                                                            ;;
  184. ;;  Prompts user for selection of a set of blocks and, if a   ;;
  185. ;;  curve is detected at each block insertion point, the      ;;
  186. ;;  block is rotated to align with the curve. All objects     ;;
  187. ;;  found to intersect with the block are then trimmed to the ;;
  188. ;;  rectangular block outline.                                ;;
  189. ;;                                                            ;;
  190. ;;  Program works in all views & UCS.                         ;;
  191. ;;------------------------------------------------------------;;
  192. ;;  Author: Lee Mac, Copyright ?2011 - www.lee-mac.com       ;;
  193. ;;------------------------------------------------------------;;

  194. (defun c:ABBS ( / ss i )
  195.     (if (setq ss (ssget "_:L" '((0 . "INSERT"))))
  196.         (repeat (setq i (sslength ss))
  197.             (LM:AutoBlockBreak (ssname ss (setq i (1- i))) (eq "ON" (getenv "LMac\\ABBRotation")))
  198.         )
  199.     )
  200.     (princ)
  201. )

  202. ;;----------=={ Automatic Block Break SubFunction }==---------;;
  203. ;;                                                            ;;
  204. ;;  Takes a block reference argument and trims surrounding    ;;
  205. ;;  geometry if curve is detected at the insertion point of   ;;
  206. ;;  the block.                                                ;;
  207. ;;                                                            ;;
  208. ;;  If a curve is detected, the block is rotated to align     ;;
  209. ;;  with the curve and all  objects found to intersect with   ;;
  210. ;;  the block are trimmed to the rectangular block outline.   ;;
  211. ;;                                                            ;;
  212. ;;  Program works in all views & UCS.                         ;;
  213. ;;------------------------------------------------------------;;
  214. ;;  Author: Lee Mac, Copyright ?2011 - www.lee-mac.com       ;;
  215. ;;------------------------------------------------------------;;
  216. ;;  Arguments:                                                ;;
  217. ;;  block - EName or VLA-Object of Block Reference object     ;;
  218. ;;------------------------------------------------------------;;

  219. (defun LM:AutoBlockBreak

  220.     ( block rotate / *error* _GetFurthestApart acspc bbx brk cmd crv en ent enx int lst mat nme ply pt ss x )

  221.     (defun *error* ( msg )
  222.         (if (and ply (not (vlax-erased-p ply))) (vla-delete ply))
  223.         (if cmd  (setvar 'CMDECHO cmd))
  224.         (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
  225.             (princ (strcat "\nError: " msg))
  226.         )
  227.         (princ)
  228.     )

  229.     (defun _GetFurthestApart ( lst / mx p1 p2 ds rslt )
  230.         (setq mx 0.0)
  231.         (while (setq p1 (car lst))
  232.             (foreach p2 (setq lst (cdr lst))
  233.                 (if (< mx (setq ds (distance p1 p2))) (setq mx ds rslt (list p1 p2)))
  234.             )
  235.         )
  236.         rslt
  237.     )

  238.     (setq cmd (getvar 'CMDECHO))
  239.     (setvar 'CMDECHO 0)

  240.     (setq acdoc (cond (acdoc) ((vla-get-activedocument (vlax-get-acad-object))))
  241.           acblk (cond (acblk) ((vla-get-blocks acdoc)))
  242.           acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'paperspace 'modelspace))
  243.     )
  244.   
  245.     (if
  246.         (and
  247.             (setq ent
  248.                 (cond
  249.                     (   (eq 'ENAME (type block))
  250.                         block
  251.                     )
  252.                     (   (eq 'VLA-OBJECT (type block))
  253.                         (vlax-vla-object->ename block)
  254.                     )
  255.                 )
  256.             )
  257.             (setq enx (entget ent))
  258.             (eq "INSERT" (cdr (assoc 0 enx)))
  259.         )
  260.         (progn
  261.             (if rotate
  262.                 (progn
  263.                     (setq pt (cdr (assoc 10 enx)))
  264.                     (if
  265.                         (setq ss
  266.                             (ssget "_C"
  267.                                 (polar (trans pt ent 1) (/       pi -4.) 1e-4)
  268.                                 (polar (trans pt ent 1) (/ (* 3. pi) 4.) 1e-4)
  269.                                '((0 . "~INSERT,ARC,ELLIPSE,CIRCLE,*LINE"))
  270.                             )
  271.                         )
  272.                         (progn (setq crv (ssname ss 0))
  273.                             (entupd
  274.                                 (cdr
  275.                                     (assoc -1
  276.                                         (entmod
  277.                                             (subst
  278.                                                 (cons 50
  279.                                                     (LM:MakeReadable
  280.                                                         (angle '(0. 0. 0.)
  281.                                                             (trans
  282.                                                                 (vlax-curve-getFirstDeriv crv
  283.                                                                     (vlax-curve-getParamatPoint crv
  284.                                                                         (vlax-curve-getClosestPointto crv (trans pt ent 0))
  285.                                                                     )
  286.                                                                 )
  287.                                                                 0 crv
  288.                                                             )
  289.                                                         )
  290.                                                     )
  291.                                                 )
  292.                                                 (assoc 50 enx) enx
  293.                                             )
  294.                                         )
  295.                                     )
  296.                                 )
  297.                             )
  298.                         )
  299.                     )
  300.                 )
  301.             )
  302.             (setq nme (cdr (assoc 2 enx))
  303.                   mat (RefGeom ent)
  304.             )
  305.             (setq bbx
  306.                 (mapcar '(lambda ( x ) (mapcar '+ (mxv (car mat) x) (cadr mat)))
  307.                     (cond
  308.                         (   (cdr (assoc nme *blockboundingboxes*))   )
  309.                         (   (cdar
  310.                                 (setq *blockboundingboxes*
  311.                                     (cons
  312.                                         (cons nme (LM:BlockDefinitionBoundingBox acblk nme)) *blockboundingboxes*
  313.                                     )
  314.                                 )
  315.                             )
  316.                         )
  317.                     )
  318.                 )
  319.             )
  320.             (if
  321.                 (setq ss
  322.                     (ssget "_C"
  323.                         (trans (car   bbx) 0 1)
  324.                         (trans (caddr bbx) 0 1)
  325.                        '((0 . "~INSERT,ARC,ELLIPSE,CIRCLE,*LINE"))
  326.                     )
  327.                 )
  328.                 (progn
  329.                     (vla-put-closed (setq ply (vlax-invoke acspc 'add3dpoly (apply 'append bbx))) :vlax-true)
  330.                     (while (setq en (ssname ss 0))
  331.                         (if (setq int (LM:GroupByNum (vlax-invoke (vlax-ename->vla-object en) 'IntersectWith ply acExtendThisEntity) 3))
  332.                             (setq lst (cons (cons en int) lst))
  333.                         )
  334.                         (ssdel en ss)
  335.                     )
  336.                     (vla-delete ply)
  337.                     (foreach int lst
  338.                         (setq brk (_GetFurthestApart (cdr int)))
  339.                         (command
  340.                             "_.break" (list  (car int) (trans (car brk) 0 1)) "_F"
  341.                                "_non" (trans (car  brk) 0 1)
  342.                                "_non" (trans (cadr brk) 0 1)
  343.                         )
  344.                     )
  345.                 )
  346.             )
  347.         )
  348.     )
  349.     (setvar 'CMDECHO cmd)
  350.     (princ)
  351. )

  352. ;;-------------=={ Block Definition BoundingBox }==-----------;;
  353. ;;                                                            ;;
  354. ;;  Returns a point list describing a rectangular frame       ;;
  355. ;;  bounding all objects in a block definition.               ;;
  356. ;;------------------------------------------------------------;;
  357. ;;  Author: Lee Mac, Copyright ?2011 - www.lee-mac.com       ;;
  358. ;;------------------------------------------------------------;;
  359. ;;  Arguments:                                                ;;
  360. ;;  blocks - The Block Collection in which the block resides  ;;
  361. ;;  block  - The name of the block                            ;;
  362. ;;------------------------------------------------------------;;
  363. ;;  Returns: Point list describing boundingbox of definition  ;;
  364. ;;------------------------------------------------------------;;

  365. (defun LM:BlockDefinitionBoundingBox ( blocks block / l1 l2 ll ur )
  366.     (vlax-for obj (vla-item blocks block)
  367.         (if
  368.             (and
  369.                 (vlax-method-applicable-p obj 'getboundingbox)
  370.                 (not (eq "AcDbAttributeDefinition" (vla-get-objectname obj)))
  371.             )
  372.             (if
  373.                 (not
  374.                     (vl-catch-all-error-p
  375.                         (vl-catch-all-apply 'vla-getboundingbox (list obj 'll 'ur))
  376.                     )
  377.                 )
  378.                 (setq l1 (cons (vlax-safearray->list ll) l1)
  379.                       l2 (cons (vlax-safearray->list ur) l2)
  380.                 )
  381.             )
  382.         )
  383.     )
  384.     (if l1
  385.         (
  386.             (lambda ( boundingbox )
  387.                 (mapcar
  388.                     (function
  389.                         (lambda ( _functionlist )
  390.                             (mapcar
  391.                                 (function
  392.                                     (lambda ( _function ) ((eval _function) boundingbox))
  393.                                 )
  394.                                 _functionlist
  395.                             )
  396.                         )
  397.                     )
  398.                    '((caar cadar) (caadr cadar) (caadr cadadr) (caar cadadr))
  399.                 )
  400.             )
  401.             (list
  402.                 (apply 'mapcar (cons 'min l1))
  403.                 (apply 'mapcar (cons 'max l2))
  404.             )
  405.         )
  406.     )
  407. )

  408. ;; RefGeom (gile)
  409. ;; Returns a list which first item is a 3x3 transformation matrix (rotation,
  410. ;; scales, normal) and second item the object insertion point in its parent
  411. ;; (xref, block or space)
  412. ;;
  413. ;; Argument : an ename

  414. (defun RefGeom ( ename / elst ang norm mat )
  415.     (setq  elst (entget ename)
  416.           ang  (cdr (assoc 50 elst))
  417.           norm (cdr (assoc 210 elst))
  418.     )
  419.     (list
  420.         (setq mat
  421.             (mxm
  422.                 (mapcar '(lambda ( v ) (trans v 0 norm T))
  423.                    '(
  424.                         (1.0 0.0 0.0)
  425.                         (0.0 1.0 0.0)
  426.                         (0.0 0.0 1.0)
  427.                     )
  428.                 )
  429.                 (mxm
  430.                     (list
  431.                         (list (cos ang) (- (sin ang)) 0.0)
  432.                         (list (sin ang) (cos ang)     0.0)
  433.                         (list    0.0       0.0        1.0)
  434.                     )
  435.                     (list
  436.                         (list (cdr (assoc 41 elst)) 0.0 0.0)
  437.                         (list 0.0 (cdr (assoc 42 elst)) 0.0)
  438.                         (list 0.0 0.0 (cdr (assoc 43 elst)))
  439.                     )
  440.                 )
  441.             )
  442.         )
  443.         (mapcar '- (trans (cdr (assoc 10 elst)) norm 0)
  444.             (mxv mat (cdr (assoc 10 (tblsearch "BLOCK" (cdr (assoc 2 elst))))))
  445.         )
  446.     )
  447. )

  448. ;; Matrix x Vector - Vladimir Nesterovsky
  449. ;; Args: m - nxn matrix, v - vector in R^n

  450. (defun mxv ( m v ) (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m))

  451. ;; Matrix Transpose - Doug Wilson
  452. ;; Args: m - nxn matrix

  453. (defun trp ( m ) (apply 'mapcar (cons 'list m)))

  454. ;; Matrix x Matrix - Vladimir Nesterovsky
  455. ;; Args: m,n - nxn matrices

  456. (defun mxm ( m n ) ( (lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n)))

  457. ;;-----------------=={ Group by Number }==--------------------;;
  458. ;;                                                            ;;
  459. ;;  Groups a list into a list of lists, each of length 'n'    ;;
  460. ;;------------------------------------------------------------;;
  461. ;;  Author: Lee Mac, Copyright ?2011 - www.lee-mac.com       ;;
  462. ;;------------------------------------------------------------;;
  463. ;;  Arguments:                                                ;;
  464. ;;  l - List to process                                       ;;
  465. ;;  n - Number of elements by which to group the list         ;;
  466. ;;------------------------------------------------------------;;
  467. ;;  Returns:  List of lists, each of length 'n'               ;;
  468. ;;------------------------------------------------------------;;

  469. (defun LM:GroupByNum ( l n / r)
  470.     (if l
  471.         (cons
  472.             (reverse (repeat n (setq r (cons (car l) r) l (cdr l)) r))
  473.             (LM:GroupByNum l n)
  474.         )
  475.     )
  476. )

  477. ;;-------------------=={ Make Readable }==--------------------;;
  478. ;;                                                            ;;
  479. ;;  Returns an angle corrected for text readability           ;;
  480. ;;------------------------------------------------------------;;
  481. ;;  Author: Lee Mac, Copyright ?2011 - www.lee-mac.com       ;;
  482. ;;------------------------------------------------------------;;
  483. ;;  Arguments:                                                ;;
  484. ;;  a - angle to process                                      ;;
  485. ;;------------------------------------------------------------;;
  486. ;;  Returns:  angle corrected for text readability            ;;
  487. ;;------------------------------------------------------------;;

  488. (defun LM:MakeReadable ( a )
  489.     (   (lambda ( a ) (if (and (< (/ pi 2.0) a) (<= a (/ (* 3.0 pi) 2.0))) (+ a pi) a))
  490.         (rem (+ a pi pi) (+ pi pi))
  491.     )
  492. )

  493. ;;------------------------------------------------------------;;

  494. (if (null (getenv "LMac\\ABBRotation"))
  495.     (setenv "LMac\\ABBRotation" "ON")
  496. )

  497. ;;------------------------------------------------------------;;

  498. (vl-load-com) (princ)
  499. (princ "\n:: AutoBlockBreak.lsp | Version 1.4 | ?Lee Mac 2011 www.lee-mac.com ::")
  500. (princ "\n:: Type \"ABB\" to insert & break or \"ABBE\"/\"ABBS\" to break existing ::")
  501. (princ)

  502. ;;------------------------------------------------------------;;
  503. ;;                        End of File                         ;;
  504. ;;------------------------------------------------------------;;




发表于 2013-1-2 08:49 | 显示全部楼层
不错,挺实用
发表于 2015-5-24 21:13 | 显示全部楼层
11楼的朋友,谢谢,代码已下载。
发表于 2023-6-28 18:32 | 显示全部楼层
3、4、11楼的程序内容都很实用,虽然有功能重叠,都能做为学习的依据,感谢分享。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-18 10:53 , Processed in 0.231571 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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