本帖最后由 fools 于 2018-12-19 16:20 编辑
提供两种方法,第一种是通过矩形嵌套几何关系判断
- ;;几何关系判断
- (defun c:tt1 (/ box e i ss lst bound rects)
- (defun ebox (e / pa pb)
- (and (= 'ename (type e)) (setq e (vlax-ename->vla-object e)))
- (vlax-invoke-method e 'GetBoundingBox 'pa 'pb)
- (setq pa (trans (vlax-safearray->list pa) 0 1)
- pb (trans (vlax-safearray->list pb) 0 1)
- )
- (list pa pb)
- )
- (defun area (pts) (apply '* (cdr (reverse (apply 'mapcar (cons '- pts)))))) ;_求面积
- (defun pt4 (pt2)
- (list (car pt2) (list (caadr pt2) (cadar pt2)) (cadr pt2) (list (caar pt2) (cadadr pt2)))
- ) ;_对角点生成四角点
- (defun PtInPoly (pt pts)
- (equal pi
- (abs
- (apply '+ (mapcar '(lambda (x y) (rem (- (angle pt x) (angle pt y)) pi)) (cons (last pts) pts) pts))
- )
- 1e-6
- )
- ) ;_点是否在凸多边形内(角度法)
- ;;
- (setq ss (ssget '((0 . "INSERT"))))
- (repeat (setq i (sslength ss))
- (setq e (ssname ss (setq i (1- i))))
- (setq lst (cons (ebox e) lst)) ;_提取边界对角点,不生产矩形
- )
- (setq lst (vl-sort lst '(lambda (x1 x2) (> (area x1) (area x2))))) ;_按面积大小排序
- (while lst
- (setq rects (cons (car lst) rects)) ;_矩形对角点集
- (setq bound (pt4 (car lst))) ;_矩形边界
- (setq lst (vl-remove-if '(lambda (x) (and (PtInPoly (car x) bound) (PtInPoly (cadr x) bound))) (cdr lst))) ;_移除大矩形边界内的小矩形
- )
- (mapcar '(lambda (x) (command-s "rectang" (car x) (cadr x))) rects) ;_批量生成矩形
- (princ)
- )
第二种方法是将所有矩形转换成面域后,再进行并集运算后,炸开重新生成矩形,源代码在附件中,收一点零花钱
|