本帖最后由 fangmin723 于 2024-6-21 11:31 编辑
CAD官方自带了(vla-get-Area obj) (command AREA)来获取面积,
但vla-get-Area需现有个对象才行,
command呢,需要考虑捕捉的问题
![](source/plugin/imc_colorcode/images/loading.gif) - 出自明经 AutoLISP 编程②群 @必强 大佬
- (and
- (setq en (entsel))
- (vl-cmdf "area" "o"(car en ) "" "" "" )
- (alert (rtos(getvar 'area)))
- )
上述代码也需要对象
如果想要获取如下如所示的部分面积,按照上述两种方法,
要么需要对象,要么需要考虑捕捉的问题
所以,写了按照顶点表获取面积的函数,如下:
![](source/plugin/imc_colorcode/images/loading.gif) - ;;说明:获取封闭多边形面积
- ;;参数:ptlst:按参数排序点的表
- ;;返回:多边形的面积
- (defun polygonarea(ptlst / area pt1 pt2 pts)
- (if(> (length ptlst) 2)
- (progn
- (setq pt1 (car ptlst) pts pt1 ptlst (cdr ptlst) area 0)
- (while (setq pt2 (car ptlst))
- (setq area (+ area (- (* (car pt1) (cadr pt2)) (* (cadr pt1) (car pt2)))))
- (setq pt1 pt2 ptlst (cdr ptlst))
- )
- (setq area (* 0.5 (+ area (- (* (car pt1) (cadr pts)) (* (cadr pt1) (car pts))))))
- (abs area)
- )
- 0
- )
- )
![](source/plugin/imc_colorcode/images/loading.gif) - (defun c:GetPolygonArea(/ ent polygonarea)
- (if (setq ent (car (entsel)))
- (princ
- (rtos
- (polygonarea
- (mapcar
- 'cdr
- (vl-remove-if-not '(lambda(x) (= 10 (car x)))
- (entget ent)
- )
- )
- ) 2 2
- )
- )
- )
- (prin1)
- )
|