此程序是在网上收集所得。
但是感觉功能使用起来较麻烦,所以想请大大位帮忙修改一下,谢谢。
原代码功能:选择要计算的对象后,需要点击屏幕指定位置,输入文字高度,然后生成文字放在图纸中。
修改后功能:选择计算对象后,直接在命令提示栏显示总长度和面积,不需要再生成文字在图纸中。
如果方便,再在此代码基础上,修改一个只使用统计长度的功能
代码下方有lsp文件的压缩包。
代码如下:
;;;===================
;;;面积和长度统计程序
;;;highflybird kunming
;;;===================
(prompt "命令为:qq")
(defun C:qq (/ f ss l i SSarea totlen entlen
ename name obj text-S text-L insPt0 height
insPt1 insPt2 text-1 text-2 *APP *DOC *MSP
)
(vl-load-com)
(setq *APP (vlax-get-acad-object))
(setq *DOC (vla-get-activeDocument *APP))
(setq *MSP (vla-get-Modelspace *DOC))
(initget 1 "1 2 3")
(setq f (getkword "\n请输入你要统计的面积(1)长度(2)两者(3):"))
(if
(and
(setq ss (ssget))
(setq insPt0 (getpoint "\n请输入文字插入点: "))
(setq height (getdist "\n请输入文字高度:"))
)
(progn
(setq l (sslength ss))
(setq i 0)
(setq SSarea 0)
(setq totlen 0)
(setq insPt1 (vlax-3d-point insPt0))
(setq insPt2 (polar insPt0 (* 1.5 Pi) (* 1.5 height)))
(setq insPt2 (vlax-3d-point insPt2))
(cond
( (= f "1")
(repeat l
(func-1)
(func-2)
(setq i (1+ i))
)
(setq text-S (strcat (convert1 SSarea 6) "平方米"));总面积为:小数后6位
(vla-addtext *MSP text-S insPt1 height)
)
( (= f "2")
(repeat l
(func-1)
(func-3)
(setq i (1+ i))
)
(setq text-L (strcat (convert1 totlen 3) "米")) ;总长度为:小数后3位
(vla-addtext *MSP text-L insPt2 height)
)
( (= f "3")
(repeat l
(func-1)
(func-2)
(func-3)
(setq i (1+ i))
)
(setq text-S (strcat (convert1 SSarea 6) "平方米"));总面积为:小数后6位
(setq text-L (strcat (convert1 totlen 3) "米")) ;总长度为:小数后3位
(vla-addtext *MSP text-S insPt1 height)
(vla-addtext *MSP text-L insPt2 height)
)
)
)
(alert "你没有选取物体或者输入正确的数据!")
)
(princ)
)
(defun func-1 ()
(setq ename (ssname ss i))
(setq obj (vlax-ename->vla-object ename))
(setq elist (entget ename))
(setq name (cdr (assoc 0 elist)))
)
;;面积的统计
(defun func-2 (/ p1 p2 p3 p4)
(if (vlax-property-available-p obj "area")
(setq SSarea (+ (vla-get-area obj) SSarea))
(if (= name "SOLID")
(setq p1 (cdr (assoc 10 elist))
p2 (cdr (assoc 11 elist))
p3 (cdr (assoc 12 elist))
p4 (cdr (assoc 13 elist))
SSarea (+ (area-of-verties (list p1 p2 p4 p3)) SSarea)
)
)
)
)
;;长度的统计
(defun func-3 (/ p1 p2 p3 p4)
(cond
( (= name "MLINE")
(setq totlen (+ totlen (ml-length ename)))
)
( (or (= name "ARC")
(= name "CIRCLE")
(= name "LINE")
(= name "POLYLINE")
(= name "LWPOLYLINE")
(= name "SPLINE")
(= name "ELLIPSE")
)
(setq entlen (vlax-curve-getdistatparam ename (vlax-curve-getendparam ename)))
(setq totlen (+ totlen entlen))
)
( (= name "SOLID")
(setq p1 (cdr (assoc 10 elist)))
(setq p2 (cdr (assoc 11 elist)))
(setq p3 (cdr (assoc 12 elist)))
(setq p4 (cdr (assoc 13 elist)))
(setq totlen (+ (length-of-verties (list p1 p2 p4 p3)) totlen))
)
)
)
;;Mline的长度
(defun ml-length (ename / j d ptlist)
(foreach n (entget ename)
(if (= (car n) 11)
(setq ptlist (cons (cdr n) ptlist))
)
)
(setq d 0 j -1)
(repeat (1- (length ptlist))
(setq j (1+ j))
(setq d (+ d (distance (nth j ptlist) (nth (1+ j) ptlist))))
)
)
;;单位转化
(defun convert (x n /)
(rtos (/ x (expt 10 n)) 2 n)
)
(defun convert1 (x n /)
(rtos (/ x (expt 10 n)) 2 3)
)
;;n个点的长度
(defun length-of-verties (pts / i l len pts1)
(setq i -1 len 0)
(setq pts1 (cons (last pts) pts))
(repeat (length pts)
(setq i (1+ i))
(setq l (distance (nth i pts1) (nth (1+ i) pts1)))
(setq len (+ l len))
)
)
;;n个点的面积
(defun area-of-verties (pts / i area PX0 PY0 x1 y1 x2 y2)
(setq i 0)
(setq area 0)
(setq px0 (caar pts))
(setq py0 (cadar pts))
(repeat (- (length pts) 1)
(setq x1 (- (car (nth i pts)) px0)
y1 (- (cadr (nth i pts)) py0)
x2 (- (car (nth (1+ i) pts)) px0)
y2 (- (cadr (nth (1+ i) pts)) py0)
)
(setq area (+ (- (* x1 y2)(* x2 y1)) area))
(setq i (1+ i))
)
(abs (/ area 2))
)
这是代码的lsp文件压缩包
[
本帖最后由 jk4461 于 2011-5-22 01:29 编辑 ]
全部回复(2 )
只看楼主 我来说两句抢地板回复 举报
回复 举报