请大大门指点一下,精简一下,
再帮我扩展下,
大圆外不统计,
不同颜色的面积统计。
嘿嘿,多多指教,下面贴源码,莫笑。
- (defun c:tte (/ e pts ss d lst s1 s2 lst1 cl nl lst2 hl)
- (if (and (setq e (car (xdrx_entsel
- "\nPick Curve: "
- '((0 . "*Polyline,CIRCLE,ELLIPSE,SPLINE"))
- )
- );拾取一个曲线
- )
-
- (vlax-curve-isclosed e) ;确定指定曲线是否闭合(即起点与端点是否重合)
- (setq pts (xdrx_getsamplept e));;得到这个曲线的模拟顶点表
- (setq ss (ssget "WP" pts '((0 . "CIRCLE"))));根据 圈围选定对像创建选择集
- (setq d (getdist "\nRadius: "))
-
- (setq t0 (xdl-getutime))
- )
- (progn
- (setq lst (mapcar
- '(lambda (x / p)
- (setq p (xdrx_getpropertyvalue x "Center"));实体属性获取 (p 圆心)
- (setq cl (cons (xdrx_circle_make p d) cl));创建圆实体
- p
- )
- (xdrx_pickset->ents ss);选择集中的实体表
- )
- lst1 (cons (ssname (xdrx_entity_copy e) 0) cl) ;实体拷贝
- sm (XD::Entity->Pickset lst1)
-
- )
- ;(mbb);交点打断
-
- (setq
- ;s1 (xdrx_curve_intersectbreak (XD::Entity->Pickset lst1) 0);将选择集中的曲线在所有交点处break
- ;s1 (setq ss (ssget "WP" pts '((0 . "arc"))))
- slst (mbb)
-
- s1 (XD::Entity->Pickset slst)
-
- s2 (xdrx_geom_searchregions s1 0);求给定选择集内的实体的连通区域所有边界
- lst2 (xdrx_pickset->ents s2);选择集中的实体表
- lst2 (vl-sort lst2 ;根据给定的比较函数来对表中的元素排序
- '(lambda (x1 x2)
- (> (car (xdrx_getarea x1));求AutoCAD AcDbCurve(曲线)类的面积和周长(长度)
- (car (xdrx_getarea x2))
- )
- )
- )
- )
- (xdrx_entity_delete (car lst2));删除当前空间或者块定义中的实体
- (setq
- nl (mapcar
- '(lambda (x / p)
- (if (setq
- p
- (cadr (assoc "Centroid" (xdrx_curve_info x)));获得AcDbCurve曲线实体(polyline,lwpolyline,arc,circle,ellipse,spline) 和AcDbRegion(REGION面域)实体的面积,周长,质心,惯性矩,回转半径,边界盒等信息。
- )
- (list
- (length
- (vl-remove nil
- (mapcar '(lambda (a)
- (if (< (distance p a) d)
- t
- nil
- )
- )
- lst;->a
- )
- )
- )
- x
- )
- )
- )
- (cdr lst2)
- )
- )
- (mapcar
- '(lambda (x / ent)
- (setq ent (xdrx_hatch_make (cadr x)));创建HATCH填充实体
- (setq hl (cons ent hl))
- (if (zerop (car x));查验是否是0
- (xdrx_entity_setcolor ent 15);设置一个实体的颜色
- (xdrx_entity_setcolor ent (car x))
- )
- )
- (vl-remove nil nl)
- )
- (apply 'xdrx_draworder->back hl);将参数实体和选择集的任意组合的实体顺序放到最后面
- )
- )
- (princ (strcat "\n*****""耗时"
- (rtos (- (xdl-getutime)t0)2 3)
- "秒"
- ))
- (princ)
- )
- (defun XD::Entity->PickSet (entl / n ss x)
- (setq ss (ssadd))
- (foreach n entl
- (ssadd n ss)
- )
- ss
- )
- (defun xdl-getutime()
- (* 86400 (getvar "tdusrtimer")
- )
- )
- ;;交点打断主函数
- (defun MBB (/ elist ssg n t0 lst)
- (setq lst '()) ;empty list
- (VL-LOAD-COM)
- (fy:clearcset)
- ;;; (setq t0 (xdl-getutime))
- (if ;(setq ssg (ssget '((0 . "line,arc,circle,ellipse"))))
- (setq ssg sm)
- (progn
- (command "_.select" ssg "")
- (vlax-for obj (vla-get-activeselectionset
- (vla-get-activedocument (vlax-get-acad-object))
- )
- (setq elist (cons obj elist)) ; ssg->elist
- )
- )
- )
-
- (setq lst (DoEntMake (InterSort (ssinter elist))))
- (princ)
- lst
- )
- ;;求交点集函数-nth
- ;;经过测试,nth函数仅比assoc函数快一点点。
- ;;故此函数也可取消i,j变量,直接使用assoc函数
- (defun ssinter (el / el1 obj1 obj2 ipts pts list1 outlst i j)
- (setq outlst (mapcar 'list el)
- i -1 ;obj1位置指针
- n 0 ;交点数计数器
- )
- (while el
- (setq obj1 (car el)
- list1 (nth (setq i (1+ i)) outlst) ;obj1已有的交点列表
- el (cdr el)
- el1 el
- j i ;obj2位置指针
- )
- (while el1
- (setq obj2 (car el1)
- el1 (cdr el1)
- j (1+ j)
- )
- ;;取交点
- (if (and (setq ipts (vla-intersectwith obj1 obj2 0))
- (setq ipts (vlax-variant-value ipts))
- (> (vlax-safearray-get-u-bound ipts 1) 0)
- )
- (progn
- (setq ipts (vlax-safearray->list ipts)
- pts '() ;obj1,obj2交点临时列表变量
- )
- (while (> (length ipts) 0)
- (setq pts (cons (list (car ipts)
- (cadr ipts)
- (caddr ipts)
- )
- pts
- )
- ipts (cdddr ipts)
- )
- )
- (setq list1 (append list1 pts) ;存obj1交点表,循环结束后再更新
- n (+ n (length pts)) ;交点计数累加
- )
- ;;obj2的交点列表立即更新
- (setq
- outlst (subst (append (nth j outlst) pts)
- (nth j outlst)
- outlst
- )
- )
- )
- )
- )
- ;;当obj1存在交点,且非封闭曲线,添加两端点
- (if (and (cdr list1) (not (vlax-curve-isClosed obj1)))
- (setq list1 (append list1
- (list (vlax-curve-getEndPoint obj1))
- (list (vlax-curve-getStartPoint obj1))
- )
- )
- )
- (setq outlst (subst list1 (nth i outlst) outlst)) ;更新obj1交点列表
- )
- outlst
- )
- ;;点集排序及删除重复点函数
- (defun InterSort (el / obj1 pts plst outlst)
- (setq outlst '()) ;empty list
- (foreach item el
- (setq obj1 (car item)
- pts (cdr item)
- plst '() ;empty list
- )
- (if pts ;若无交点,则不修改该实体
- (progn
- ;;交点排序,列表为逆序
- (setq
- pts (vl-sort
- pts
- (function (lambda (p1 p2)
- (< (vlax-curve-getParamAtPoint obj1 p1)
- (vlax-curve-getParamAtPoint obj1 p2)
- )
- )
- )
- )
- )
- ;;剔除重复点并将列表顺序转正
- (foreach p pts
- (if plst
- (if (not (equal p (car plst) 0.00001))
- (setq plst (cons p plst))
- )
- (setq plst (cons p plst))
- )
- )
- ;;闭合曲线需再添加首个交点以使新实体完全封闭
- (if (vlax-curve-isClosed obj1)
- (setq plst (cons (last plst) plst))
- )
- (setq plst (cons (vlax-vla-object->ename obj1) plst)
- outlst (cons plst outlst)
- )
- )
- )
- )
- outlst
- )
- ;;调用entmake生成新实体
- (defun DoEntMake (el / obj objlst objname objcen objratio objaxis outlst)
- (setq outlst '()) ;empty list
- (foreach e el
- (setq obj (car e)
- objlst (entget obj)
- objlst (vl-remove (assoc -1 objlst) objlst) ;去除图元名
- objlst (vl-remove (assoc 330 objlst) objlst) ;去除id
- objlst (vl-remove (assoc 5 objlst) objlst) ;去除句柄
- objname (cdr (assoc 0 objlst))
- )
- (cond
- ((= objname "LINE")
- (repeat (- (length e) 2)
- (setq e (cdr e))
- (setq objlst (subst (cons 10 (car e)) (assoc 10 objlst) objlst))
- (setq objlst (subst (cons 11 (cadr e)) (assoc 11 objlst) objlst)
-
- )
- (entmake objlst)
- (setq outlsttem (entmakex objlst)
- outlst (cons outlsttem outlst)
- )
- )
- (entdel obj)
- )
- ((= objname "CIRCLE")
- (setq objcen (cdr (assoc 10 objlst)))
- (setq objlst (subst (cons 0 "ARC") (assoc 0 objlst) objlst))
- (setq objlst (append objlst
- (list (cons 100 "AcDbArc")
- (cons 50 0.0)
- (cons 51 0.0)
- )
- )
- )
- (repeat (- (length e) 2)
- (setq e (cdr e))
- (setq objlst (subst (cons 50 (angle objcen (cadr e)))
- (assoc 50 objlst)
- objlst
- )
- )
- (setq objlst (subst (cons 51 (angle objcen (car e)))
- (assoc 51 objlst)
- objlst
- )
- )
- (entmake objlst)
- (setq outlsttem (entmakex objlst)
- outlst (cons outlsttem outlst)
- )
- )
- (entdel obj)
- )
- ((= objname "ARC")
- (setq objcen (cdr (assoc 10 objlst)))
- (repeat (- (length e) 2)
- (setq e (cdr e))
- (setq objlst (subst (cons 50 (angle objcen (cadr e)))
- (assoc 50 objlst)
- objlst
- )
- )
- (setq objlst (subst (cons 51 (angle objcen (car e)))
- (assoc 51 objlst)
- objlst
- )
- )
- (entmake objlst)
- (setq outlsttem (entmakex objlst)
- outlst (cons outlsttem outlst)
- )
- )
- (entdel obj)
- )
- ((= objname "ELLIPSE")
- ;;椭圆圆心
- (setq objcen (cdr (assoc 10 objlst)))
- ;;相对于中心的长轴矢量
- (setq objaxis (cdr (assoc 11 objlst)))
- ;;短轴与长轴的比例
- (setq objratio (cdr (assoc 40 objlst)))
- (repeat (- (length e) 2)
- (setq e (cdr e))
- (setq objlst (subst (cons 41 (pt->param (cadr e) objcen objaxis objratio))
- (assoc 41 objlst)
- objlst
- )
- )
- (setq objlst (subst (cons 42 (pt->param (car e) objcen objaxis objratio))
- (assoc 42 objlst)
- objlst
- )
- )
- (entmake objlst)
- (setq outlsttem (entmakex objlst)
- outlst (cons outlsttem outlst)
- )
- )
- (entdel obj)
- )
- )
- )
- outlst
- )
- ;;计算耗时
- (defun xdl-getutime ()
- (* 86400 (getvar "tdusrtimer"))
- )
- ;;求椭圆曲线参数
- (defun pt->param (pt cen axis ratio / ang param)
- (setq ang (- (angle cen pt) (angle '(0. 0. 0.) axis)))
- (cond ((= (cos ang) 0.0) ;防止分母cos为零出错
- (if (> (sin ang) 0.0)
- (setq param (* 0.5 PI))
- (setq param (* 1.5 PI))
- )
- )
- ((= (sin ang) 0.0)
- (if (> (cos ang) 0.0)
- (setq param 0.0)
- (setq param PI)
- )
- )
- (T
- (setq param (atan (/ (sin ang) (* (cos ang) ratio))))
- (if (< (cos ang) 0.0)
- (setq param (+ pi param))
- )
- )
- )
- param
- )
- (princ)
- (defun fy:acapp nil
- (eval (list 'defun
- 'fy:acapp
- 'nil
- (vlax-get-acad-object)
- )
- )
- (fy:acapp)
- )
- (defun fy:docs nil
- (eval (list 'defun
- 'fy:docs
- 'nil
- (vla-get-documents (fy:acapp))
- )
- )
- (fy:docs)
- )
- (defun Fy:acDoc nil
- (eval (list 'defun
- 'FY:acdoc
- 'nil
- (vla-get-activedocument (vlax-get-acad-object))
- )
- )
- (fy:acdoc)
- )
- (defun fy:acsets nil
- (eval (list 'defun
- 'fy:acsets
- 'nil
- (vla-get-Selectionsets (fy:acdoc))
- )
- )
- (fy:acsets)
- )
- ;; From eachy
- (defun fy:Clearcset (/ cset)
- (if (not (vl-catch-all-error-p
- (setq cset
- (vl-catch-all-apply
- 'vla-item
- (list
- (fy:acsets)
- "CURRENT"
- )
- )
- )
- )
- )
- (vla-delete cset)
- )
- (princ)
- )
- (defun fy:cset ()
- (vla-get-activeselectionset (fy:acdoc))
- )
- (defun fy:cset->objs (/ ol)
- (vlax-for obj (fy:cset)
- (setq ol (cons obj ol))
- )
- (reverse ol)
- )
多余的代码就不删了,都比我水平高,呵呵 |