当前位置: 首页 > news >正文

CAD 二次开发应用 获取统计单行字体内的特定数据

应用场景:获取autoCAD中多个单行字体的特定符号前数据,连乘求和

工具

autolisp

主要代码

(defun c:CountTextNums (/ ss i ent txt val sum)(setq sum 0)(prompt "\n请选择包含文本的单行文字对象...")(setq ss (ssget '((0 . "TEXT"))))(if ss(progn(setq i 0)(repeat (sslength ss)(setq ent (entget (ssname ss i)))(setq txt (cdr (assoc 1 ent))) ; 提取文字内容(setq val (process-text-fixed txt))(setq sum (+ sum val))(setq i (1+ i)))(prompt (strcat "\n所有文本计算结果之和为: " (rtos sum 2 0))))(prompt "\n未选择任何文本对象。"))(princ)
)(defun process-text-fixed (txt / pos num-a num-b num-c);; === 1. 提取“台”前的连续数字 ===(setq pos (vl-string-position (ascii "台") txt))(if pos(setq num-a (extract-number-before txt pos))(setq num-a 1.0));; === 2. 提取“x”或“×”前的连续数字(支持普通x和乘号×)===(setq pos nil)(setq idx 0)(while (< idx (strlen txt))(setq char (substr txt (1+ idx) 1))(if (or (= (ascii char) 120)   ; 小写 x(= (ascii char) 88)    ; 大写 X(= (ascii char) 215)   ; 特殊乘号 ×)(setq pos idx) ; 记录最后出现的位置)(setq idx (1+ idx)))(if pos(setq num-b (extract-number-before txt pos))(setq num-b 1.0));; === 3. 提取“L”前的连续数字 ===(setq pos (vl-string-position (ascii "L") txt))(if pos(setq num-c (extract-number-before txt pos))(setq num-c 1.0))(* num-a num-b num-c) ; 返回三数相乘
)(defun extract-number-before (txt pos / str-backward char lst-num);; 从位置 pos 向前提取紧邻的连续数字(只取紧挨前面的部分)(setq str-backward (substr txt 1 pos)) ; 截取到该位置之前的字符串(setq lst-num '());; 从后往前逐字符判断是否为数字(while (and (/= str-backward "")(setq char (substr str-backward (strlen str-backward) 1))(wcmatch char "#"))(setq lst-num (cons char lst-num))(setq str-backward (substr str-backward 1 (1- (strlen str-backward)))));; 若提取到数字,则转为实数;否则返回 1.0(if lst-num(atof (apply 'strcat lst-num))1.0)
)

  

 

http://www.jsqmd.com/news/53326/

相关文章:

  • CF2157E Adjusting Drones
  • Day4 Scrum冲刺博客
  • 帮助类
  • 2025 Original Xhorse Condor XC-002 PRO Key Cutting Machine: Rapid Precise for EU/US Cars
  • JSON序列化类
  • 致深度学习小白:一文入门分布偏移
  • Day26过渡属性
  • Yanhua 2025 BMW BDC2 Key Learning Bench Platform with Module38 for G Series All Key Lost
  • 选购攻略!2025 厨余处理器 7大品牌,中餐适配款优先级推荐
  • Day4-20251127
  • 摄相机标定的基本原理 - 教程
  • 上海国际学科哪家好?2025 IB 培训机构权威榜单
  • WPF populate BooksCollection via Dispatcher.InvokeAsync,DispatcherPriority.Background in mvvm
  • 2025 年 12 月 AMC12 竞赛备考:上海补课机构优选,选对助力高效冲分
  • 痞子衡嵌入式:i.MXRT中FlexSPI外设速度上限的三个影响因子(数据手册里的纠结)
  • Yanhua Mini ACDP-2 Module 38: BMW BDC2 Keys Learning LF Smart Key Antenna Bench Platform
  • 第二天—C++语法基础
  • 自指自洽,别具一格的逻辑,千奇百怪的因果
  • Day26垂直对齐方式--vertical-align
  • 2025 年 12 月上海 AMC12 择校指南:选对专业机构,高效冲刺竞赛目标
  • USB输入监控
  • 2025年度绍兴交通事故优秀律师推荐|聚焦实力与口碑
  • 最小链覆盖 - Dilworth 定理 小记
  • 我研发了一款电影截图拼接神器:Eagle 插件让你的影片收藏更专业
  • 有种人
  • memset 破坏string
  • 关于字符串的小记
  • [NOIP2024] 编辑字符串-题解
  • 机器人设备端AI技术实现突破
  • 11月27日日记