乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 86|回复: 4

[编程交流] Lisp to round text (with numbe

[复制链接]

3

主题

9

帖子

6

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-5 19:40:04 | 显示全部楼层 |阅读模式
Hi all,
 
First post, was hoping to get some help.
 
I need a lisp that will round a number in autocad to the nearest 0.05, I have a numerous drawings with bench levels for lots (civil), I would like to be able to swipe multiple pieces of text and round them all to nearest 0.05.
 
Any help with this would be much appreciated
 
Cheers
回复

使用道具 举报

17

主题

1274

帖子

25

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
260
发表于 2022-7-5 20:01:09 | 显示全部楼层
I've seen many versions of this over the years, but this was the first one I saw and it still works.
  1. ;;; Written by Doug Broad;;; If value given 'to' argument is a real, a real is returned.;;; Rounds to nearest multiple of 'to' real or integer.(defun round (value to / try) (setq to (abs to)) (* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))); examples:; (round 12232e-015 1); 0; (round 12232e-015 1.0); 0.0; (round 123.6 1.0); 124.0; (round -123.6 1.0); -124.0; (round 6.00000000008 4); 8; 6.00000000008  rounded to the nearest multiple of 4 is 8
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:12:25 | 显示全部楼层
Just be careful if your text is formatted MText, since for such cases the text content will contain numerical formatting codes.
回复

使用道具 举报

3

主题

9

帖子

6

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-5 20:36:18 | 显示全部楼层
Hi guys,
 
thanks for the help.
 
I changed it to this
 
;;; Written by Doug Broad
;;; If value given 'to' argument is a real, a real is returned.
;;; Rounds to nearest multiple of 'to' real or integer.
 
(defun c:bench (value to / try)
  (setq to (abs to))
  (* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))
)
 
; examples:
; (round 12232e-015 1)
; 0
; (round 12232e-015 1.0)
; 0.0
; (round 123.6 1.0)
; 124.0
; (round -123.6 1.0)
; -124.0
; (round 6.00000000008 4)
; 8
; 6.00000000008  rounded to the nearest multiple of 4 is 8
 
when I type bench into the command line I get this message '; error: too few arguments'
 
Not sure what im doing wrong. I have no real experience writing lisp code. Have I missed something?
 
Thanks again for your help
回复

使用道具 举报

17

主题

1274

帖子

25

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
260
发表于 2022-7-5 20:44:36 | 显示全部楼层
You need to use the original code, look at the examples.  To round 1.2345 to the nearest 0.05 enter (round 1.2345 0.05)
It's set up this way so you can easily call it from another routine using the "value" you want to round and the "to" you want it rounded to.  
A command function that starts with "c:" cannot have the "value" and "to" arguments.  For a command function you would have to add prompts for and save those values.
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-12 00:44 , Processed in 0.403754 second(s), 62 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表