明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 224|回复: 5

求程序,根据图纸示例,框选上面光缆程式,批量生成下方统计表。

[复制链接]
发表于 昨天 21:42 | 显示全部楼层 |阅读模式
本帖最后由 网络工作者 于 2025-10-5 11:24 编辑

求助,请过路的大师看看这个可以做成批量框选汇总吗?请赐教源码,十分感谢!

根据图纸示例,框选上面光缆程式,批量生成下方统计表。







提供一个半成品程序,过路的大神看到后麻烦指导修改下,成品结果是错的。
  1. [code=lisp](defun c:CBLSTATS ()
  2.     ; 初始化计数列表
  3.     (setq l '())

  4.     ; 选择光缆程式文本
  5.     (princ "\n选择光缆程式文本: ")
  6.     (setq s (ssget))
  7.     (if (null s)
  8.         (progn
  9.             (princ "\n未选择对象!")
  10.             (return)
  11.         )
  12.     )

  13.     ; 统计光缆程式数量
  14.     (setq n (sslength s)
  15.           i 0
  16.     )
  17.     (while (< i n)
  18.         (setq e (ssname s i)
  19.               d (entget e)
  20.               t (cdr (assoc 0 d))
  21.         )
  22.         ; 只处理TEXT和MTEXT对象
  23.         (if (or (= t "TEXT") (= t "MTEXT"))
  24.             (progn
  25.                 (setq c (cdr (assoc 1 d)))
  26.                 (if (not (null c))
  27.                     (progn
  28.                         ; 清理空格
  29.                         (setq cl c)
  30.                         (while (= (substr cl 1 1) " ")
  31.                             (setq cl (substr cl 2 (strlen cl)))
  32.                         )
  33.                         (while (= (substr cl (strlen cl) 1) " ")
  34.                             (setq cl (substr cl 1 (- (strlen cl) 1)))
  35.                         )
  36.                         
  37.                         ; 检查是否已存在该程式
  38.                         (setq f 0)
  39.                         (setq j 0)
  40.                         (while (< j (length l))
  41.                             (setq x (nth j l))
  42.                             (if (= (car x) cl)
  43.                                 (progn
  44.                                     ; 更新计数
  45.                                     (setq newitem (list (car x) (+ (cadr x) 1)))
  46.                                     (setq l (subst newitem x l))
  47.                                     (setq f 1)
  48.                                 )
  49.                             )
  50.                             (setq j (+ j 1))
  51.                         )
  52.                         ; 如果是新程式则添加
  53.                         (if (= f 0)
  54.                             (setq l (cons (list cl 1) l))
  55.                         )
  56.                     )
  57.                 )
  58.             )
  59.         )
  60.         (setq i (+ i 1))
  61.     )

  62.     ; 无数据则退出
  63.     (if (null l)
  64.         (progn
  65.             (princ "\n无有效数据!")
  66.             (return)
  67.         )
  68.     )

  69.     ; 获取插入点
  70.     (princ "\n指定表格插入点: ")
  71.     (setq p (getpoint))
  72.     (if (null p)
  73.         (setq p (list 0 0 0))
  74.     )

  75.     ; 表格参数
  76.     (setq h 2.5
  77.           w1 15
  78.           w2 15
  79.           w3 10
  80.           rh 3
  81.           y (cadr p)
  82.           tl (car p)
  83.           tr (+ (car p) w1 w2 w3)
  84.     )

  85.     ; 绘制标题
  86.     (command "TEXT" (list (/ (+ tl tr) 2) y 0) h 0 "光缆程式统计" "")
  87.     (setq y (- y (* rh 2)))

  88.     ; 绘制表头
  89.     (command "TEXT" (list (+ tl (/ w1 2)) y 0) h 0 "程式" "")
  90.     (command "TEXT" (list (+ tl w1 (/ w2 2)) y 0) h 0 "分类" "")
  91.     (command "TEXT" (list (+ tl w1 w2 (/ w3 2)) y 0) h 0 "数量" "")
  92.    
  93.     ; 表头线条
  94.     (setq sy (+ y rh 1)
  95.           ey (- y rh 0.5)
  96.     )
  97.     (command "LINE" (list tl sy) (list tr sy) "")
  98.     (command "LINE" (list tl ey) (list tr ey) "")
  99.     (command "LINE" (list (+ tl w1) sy) (list (+ tl w1) ey) "")
  100.     (command "LINE" (list (+ tl w1 w2) sy) (list (+ tl w1 w2) ey) "")
  101.     (setq y ey)

  102.     ; 分类项
  103.     (setq items '("拆管道" "拆架空" "拆引上" "管道" "架空" "引上" "余" "合计"))

  104.     ; 填充表格内容
  105.     (setq k 0)
  106.     (while (< k (length l))
  107.         (setq x (nth k l)
  108.               cn (car x)
  109.               cq (cadr x)
  110.               total 0
  111.               sy y
  112.         )
  113.         (setq m 0)
  114.         (while (< m (length items))
  115.             (setq it (nth m items)
  116.                   y (- y rh)
  117.             )
  118.             ; 填写光缆程式
  119.             (if (= m 0)
  120.                 (command "TEXT" (list (+ tl (/ w1 2)) y 0) h 0 cn "")
  121.                 (command "TEXT" (list (+ tl (/ w1 2)) y 0) h 0 "" "")
  122.             )
  123.             ; 填写分类项
  124.             (command "TEXT" (list (+ tl w1 (/ w2 2)) y 0) h 0 it "")
  125.             ; 填写数量
  126.             (if (= it "合计")
  127.                 (command "TEXT" (list (+ tl w1 w2 (/ w3 2)) y 0) h 0 (rtos total 2 1) "")
  128.                 (progn
  129.                     (setq val (* cq 40.5))
  130.                     (setq total (+ total val))
  131.                     (command "TEXT" (list (+ tl w1 w2 (/ w3 2)) y 0) h 0 (rtos val 2 1) "")
  132.                 )
  133.             )
  134.             (setq m (+ m 1))
  135.         )
  136.         ; 绘制子表格线条
  137.         (setq ey y)
  138.         (command "LINE" (list tl sy) (list tl ey) "")
  139.         (command "LINE" (list tr sy) (list tr ey) "")
  140.         (command "LINE" (list (+ tl w1) sy) (list (+ tl w1) ey) "")
  141.         (command "LINE" (list (+ tl w1 w2) sy) (list (+ tl w1 w2) ey) "")
  142.         (command "LINE" (list tl ey) (list tr ey) "")
  143.         (setq y (- y rh)
  144.               k (+ k 1)
  145.         )
  146.     )

  147.     ; 绘制外边框
  148.     (command "LINE" (list tl (cadr p)) (list tl y) "")
  149.     (command "LINE" (list tr (cadr p)) (list tr y) "")
  150.     (command "LINE" (list tl y) (list tr y) "")

  151.     (princ "\n统计完成!")
  152.     (princ)
  153. )

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复

使用道具 举报

发表于 昨天 22:04 | 显示全部楼层
问题太强了!先坐好沙发,再等待结果!
回复 支持 反对

使用道具 举报

发表于 13 小时前 | 显示全部楼层
目前只是生成表格行列的线有问题。
回复 支持 反对

使用道具 举报

发表于 13 小时前 | 显示全部楼层
这一看就是捕捉没关 要么关捕捉 要么换成entmake
回复 支持 反对

使用道具 举报

 楼主| 发表于 12 小时前 | 显示全部楼层
飞雪神光 发表于 2025-10-5 10:06
这一看就是捕捉没关 要么关捕捉 要么换成entmake

统计的数值也不正确,不知道问题出现在哪里了?
回复 支持 反对

使用道具 举报

发表于 5 小时前 | 显示全部楼层
网络工作者 发表于 2025-10-5 10:51
统计的数值也不正确,不知道问题出现在哪里了?

看了一下 有很多错误啊  逻辑也不通 还给 t 赋值 批量操作也没见数据分类和关联
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-10-5 23:24 , Processed in 0.193903 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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