CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoLISP 开发指南

关于字符串转换(AutoLISP)

2023-1-6 12:20| 发布者: admin| 查看: 1419| 评论: 0|来自: AutoCAD

摘要: 数值可以转换为字符串值,以便在输出或文本数据中使用。

数值可以转换为字符串值,以便在输出或文本数据中使用。

以下函数可用于将实数值和角度值转换为字符串,然后再转换回来:

  • rtos– 根据指定的线性单位模式将实数转换为格式化字符串。
  • distof– 将表示距离值的格式化字符串转换为实数(浮点)值。
  • angtos– 根据指定的角度单位模式,将以弧度为单位的角度值转换为格式化字符串。
  • angtof– 将表示角度的格式化字符串转换为以弧度为单位的实际(浮点)值。

将实数转换为线性单位格式的字符串

该函数将实际值转换为字符串。结果字符串的格式可以使用函数的参数指定,也可以由 AutoCAD LUNITS 和 LUPREC 系统变量指定(如果未提供)。AutoCAD DIMZIN 系统变量控制前导零和尾随零写入结果字符串的方式。rtos

以下示例代码演示了 和 的用法(假设 AutoCAD DIMZIN 系统变量等于 0)。精度(第三个参数 to)在第一次调用中设置为 4 位,在其他调用中设置为 2 位。rtosrtos

(setq x 17.5)
(setq str "\nValue formatted as ")

(setq fmtval (rtos x 1 4)) ; Mode 1 = scientific
(princ (strcat str fmtval))
Value formatted as 1.7500E+01

(setq fmtval (rtos x 2 2)) ; Mode 2 = decimal
(princ (strcat str fmtval))
Value formatted as 17.50

(setq fmtval (rtos x 3 2)) ; Mode 3 = engineering
(princ (strcat str fmtval))
Value formatted as 1'-5.50"

(setq fmtval (rtos x 4 2)) ; Mode 4 = architectural
(princ (strcat str fmtval))
Value formatted as 1'-5 1/2"

(setq fmtval (rtos x 5 2)) ; Mode 5 = fractional
(princ (strcat str fmtval))
Value formatted as 17 ½

当 AutoCAD UNITMODE 系统变量设置为 1 时,单位将显示为输入的单位,返回的字符串由工程(模式等于 3)、建筑(模式等于 4)和分数(模式等于 5)单位的不同。例如,前面示例输出的前两行将是相同的,但最后三行将如下所示:rtos

Value formatted as 1'5.50"
Value formatted as 1'5-1/2"
Value formatted as 17-1/2''

将线性单位格式的字符串转换为实数

(到浮点的距离)函数是的补充。以下所有调用都返回相同的值:17.5。(请注意在模式 3 和 4 中使用反斜杠 (\)。distofrtos

(distof "1.7500E+01" 1) ; Mode 1 = scientific
(distof "17.50" 2)      ; Mode 2 = decimal
(distof "1'-5.50\"" 3)  ; Mode 3 = engineering
(distof "1'-5 1/2\"" 4) ; Mode 4 = architectural
(distof "17 1/2" 5)     ; Mode 5 = fractional

如果字符串指定了以英尺和英寸为单位的距离,则必须在引号前面加上反斜杠 (\“),使其看起来不像字符串的末尾。前面的示例演示了此操作。distof

将实数转换为具有角度单位格式的字符串

该函数将角度值转换为字符串。结果字符串的格式可以使用函数的参数指定,也可以由 AutoCAD AUNITS 和 AUPREC 系统变量指定(如果未提供)。AutoCAD DIMZIN 系统变量控制前导零和尾随零写入结果字符串的方式。angtos

由于该函数考虑了 AutoCAD ANGBASE 系统变量,因此以下示例代码始终返回“0”:angtos

(angtos (getvar "angbase"))

没有 AutoLISP 函数返回字符串版本(在当前模式/精度下),该字符串版本是 ANGBASE 从真零(东)的旋转量或以弧度为单位的任意角度。

可以通过执行下列操作之一找到 ANGBASE 从 AutoCAD 零(东)开始的旋转量或任意角度的大小:

  • 将所需角度添加到当前 ANGBASE 中,然后检查结果的绝对值是否大于 2pi;(2 * pi)。如果是这样,则减去 2pi;,如果结果为负数,则添加 2pi;,然后在结果上使用函数。angtos
  • 将 ANGBASE 的值存储在临时变量中,将 ANGBASE 设置为 0,计算函数,然后将 ANGBASE 设置为其原始值。angtos

Subtracting the result of from 360 degrees (2pi; radians or 400 grads) also yields the rotation of ANGBASE from 0. (atof (angtos 0))

The following example code demonstrates the use of and the values returned (still assuming that DIMZIN equals 0). Precision (the third argument to ) is set to 0 places in the first call, 4 places in the next three calls, and 2 places in the last. angtosangtos

(setq ang 3.14159 str2 "\nAngle formatted as ")
(setq fmtval (angtos ang 0 0)) ; Mode 0 = degrees
(princ (strcat str2 fmtval))
Angle formatted as 180

(setq fmtval (angtos ang 1 4)) ; Mode 1 = deg/min/sec
(princ (strcat str2 fmtval))
Angle formatted as 180d0'0"

(setq fmtval (angtos ang 2 4)) ; Mode 2 = grads
(princ (strcat str2 fmtval)) ; displays Angle formatted as
200.0000g

(setq fmtval (angtos ang 3 4)) ; Mode 3 = radians
(princ (strcat str2 fmtval))
Angle formatted as 3.1416r

(setq fmtval (angtos ang 4 2)) ; Mode 4 = surveyor's
(princ (strcat str2 fmtval))
Angle formatted as W

UNITMODE 系统变量还会影响返回的字符串,当它返回以测量员单位(模式等于 4)的字符串时。如果 UNITMODE 等于 0,则返回的字符串可以包含空格(例如,“N 45d E”);如果 UNITMODE 等于 1,则字符串不包含空格(例如,“N45dE”)。angtos

将角度单位格式的字符串转换为实数

函数补充,以下所有调用返回相同的值:3.14159。angtofangtos

(angtof "180" 0)       ; Mode 0 = degrees
(angtof "180d0'0\"" 1) ; Mode 1 = deg/min/sec
(angtof "200.0000g" 2) ; Mode 2 = grads
(angtof "3.14159r" 3)  ; Mode 3 = radians
(angtof "W" 4)         ; Mode 4 = surveyor's

如果字符串指定了以度、分和秒为单位的角度,则必须在引号前面加上反斜杠 (\“),这样它看起来就不像字符串的末尾。前面的示例演示了此操作。angtof


路过

雷人

握手

鲜花

鸡蛋

最新评论

CAD软件2007~2024远程安装服务

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-6 20:23

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部