arkv2006 发表于 2006-8-29 08:50:06

给AutoCAD增加计算器功能

<P>在用<FONT face="Times New Roman">AutoCAD</FONT>进行绘图时,经常需要进行一些必要的计算工作。尽管<FONT face="Times New Roman">Autodesk</FONT>公司随<FONT face="Times New Roman">AutoCAD</FONT>一起提供了一个可进行简单计算的<FONT face="Times New Roman">LISP</FONT>程序<FONT face="Times New Roman">CALC.LSP</FONT>,但该程序开发较早,在<FONT face="Times New Roman">COMMAND</FONT>命令下使用,界面不直观,且使用不方便。本文通过对<FONT face="Times New Roman">AutoCAD</FONT>内对话框结构及<FONT face="Times New Roman">AutoLISP</FONT>进行研究,提出了一种用对话框结构实现计算器功能的方法,同时给出程序流程图,供大家参考。该程序已应用于我院的实际工作中。<FONT face="Times New Roman"> </FONT></P>
<P><FONT face="Times New Roman">1 </FONT>原理及方法</P>
<P>  <FONT face="Times New Roman">AutoCAD</FONT>对话框结构包含多种构件,如<FONT face="Times New Roman">edit_box(</FONT>编辑框<FONT face="Times New Roman">)</FONT>、<FONT face="Times New Roman">text(</FONT>文字<FONT face="Times New Roman">)</FONT>、<FONT face="Times New Roman">button(</FONT>按钮<FONT face="Times New Roman">)</FONT>等。利用这些工具,可以开发出界面友好的实用程序。图<FONT face="Times New Roman">1</FONT>就是用这种方法实现的。<FONT face="Times New Roman"> </FONT>将计算器中数字及各种运算符号以按钮<FONT face="Times New Roman">(button)</FONT>形式排成<FONT face="Times New Roman">5</FONT>行<FONT face="Times New Roman">5</FONT>列,并把计算结果以文字构件方式在对话框内显示,即可得到图<FONT face="Times New Roman">1</FONT>所示对话框结构<FONT face="Times New Roman">(</FONT>对话框文件<FONT face="Times New Roman">#CAL.DCL)</FONT>。最后编制对话框显示及完成各种运算功能的驱动程序即可<FONT face="Times New Roman">(</FONT>驱动程序<FONT face="Times New Roman">#CAL.LSP)</FONT>,程序流程图见图<FONT face="Times New Roman">2</FONT>。</P>
<P>  在<FONT face="Times New Roman">#CAL.LSP</FONT>程序中用到了<FONT face="Times New Roman">AutoLISP</FONT>提供的多种数值计算及变量转换函数。需要说明的是,笔者在调试过程中发现某些函数对变量有一些特殊要求,现介绍如下,与读者共同探讨:</P>
<P>  <FONT face="Times New Roman">1</FONT>、在幂运算函数<FONT face="Times New Roman">(expt base power)</FONT>中,对变量<FONT face="Times New Roman">base(</FONT>底<FONT face="Times New Roman">)</FONT>和<FONT face="Times New Roman">power(</FONT>幂<FONT face="Times New Roman">)</FONT>的要求与代数学中幂的运算要求是不同的。在代数学中对变量<FONT face="Times New Roman">base</FONT>和<FONT face="Times New Roman">power</FONT>的要求如下:</P>
<P>  当<FONT face="Times New Roman"> base&gt;0</FONT>时,<FONT face="Times New Roman">power </FONT>可为任意实数;</P>
<P>  当<FONT face="Times New Roman">base=0</FONT>时,<FONT face="Times New Roman">power</FONT>须大于<FONT face="Times New Roman">0</FONT>;</P>
<P>  当<FONT face="Times New Roman">base&lt;0</FONT>时,<FONT face="Times New Roman">power</FONT>可为整数,若为小数或分数时要求当其化为最简分数时分母不能为偶数。</P>
<P>  但在<FONT face="Times New Roman">AutoLISP</FONT>中当变量<FONT face="Times New Roman">base&lt;0</FONT>时,要求<FONT face="Times New Roman">power</FONT>只能是整型数或小数点后均为<FONT face="Times New Roman">0</FONT>的实数,如<FONT face="Times New Roman">2</FONT>,<FONT face="Times New Roman">-2.0</FONT>等,否则将得到错误信息或返回一个错误结果<FONT face="Times New Roman">(</FONT>视<FONT face="Times New Roman">power</FONT>值而定<FONT face="Times New Roman">)</FONT>。此外,当计算结果太大而溢出时,对于<FONT face="Times New Roman">For&nbsp;DOS</FONT>版的<FONT face="Times New Roman">AutoCAD</FONT>将返回<FONT face="Times New Roman">1.67e+308</FONT>,而<FONT face="Times New Roman">WINDOWS</FONT>版则返回<FONT face="Times New Roman">1.79769e+308</FONT>。</P>
<P>  <FONT face="Times New Roman">2</FONT>、正余弦函数<FONT face="Times New Roman">(sin x)</FONT>及<FONT face="Times New Roman">(cos x)</FONT>要求变量<FONT face="Times New Roman">x</FONT>必须小于等于<FONT face="Times New Roman">9.223372e+18(</FONT>当然还可再延长小数位数<FONT face="Times New Roman">)</FONT>,否则得不到正确结果。例如当变量为<FONT face="Times New Roman">9.223373e+18</FONT>时,对于<FONT face="Times New Roman">For&nbsp;DOS</FONT>版的<FONT face="Times New Roman">AutoCAD</FONT>返回<FONT face="Times New Roman">NAN</FONT>,而<FONT face="Times New Roman">WINDOWS</FONT>版则返回变量值本身。</P>
<P>  <FONT face="Times New Roman">3</FONT>、在使用函数<FONT face="Times New Roman">(rtos)</FONT>将数据转化为字符串时,笔者发现当数据大于<FONT face="Times New Roman">1e+100(</FONT>即<FONT face="Times New Roman">10</FONT>的<FONT face="Times New Roman">100</FONT>次方<FONT face="Times New Roman">)</FONT>时,会将指数<FONT face="Times New Roman">100</FONT>自动地改为<FONT face="Times New Roman">99</FONT>。例如,执行<FONT face="Times New Roman">(rtos 6e+100 2 6)</FONT>后得到<FONT face="Times New Roman">"6.000000E+99"</FONT>,而不是所期待的<FONT face="Times New Roman">"6.000000E+100"</FONT>。所以本程序规定最大数据不能大于<FONT face="Times New Roman">1e+99</FONT>,否则将显示溢出错误。当然,通过编制另外的转换程序是可以将最大数据提高到<FONT face="Times New Roman">AutoCAD</FONT>所能接受的最大数据<FONT face="Times New Roman">(</FONT>经测试发现其最大值可为<FONT face="Times New Roman">1.79769e+308)</FONT>。限于篇幅,本文不再提供该转换函数,需要的同志可与笔者联系。</P>
<P><FONT face="Times New Roman">2 </FONT>程序使用方法</P>
<P>  将程序<FONT face="Times New Roman">#CAL.LSP </FONT>及对话框文件<FONT face="Times New Roman">#CAL.DCL</FONT>拷贝到<FONT face="Times New Roman">AutoCAD</FONT>所能找到的目录中,在<FONT face="Times New Roman">Command</FONT>:命令下键入<FONT face="Times New Roman">(LOAD "#CAL")</FONT>进行程序的加载,即可象其它<FONT face="Times New Roman">AutoCAD</FONT>内部命令一样使用<FONT face="Times New Roman">#CAL</FONT>了。也可在所使用的菜单文件<FONT face="Times New Roman">(</FONT>如<FONT face="Times New Roman">ACAD.MNU)</FONT>中的下拉式菜单或屏幕菜单部分加上下面一行:<FONT face="Times New Roman">[</FONT>计算器<FONT face="Times New Roman">]^C^C^P(if (null c:#cal) (load"#cal")) #cal</FONT>以后即可直接从菜单中驱动该绦颉?/P&gt;
<P>  对话框内各个图标与普通计算器相似,<FONT face="Times New Roman">"C"</FONT>表示清零,<FONT face="Times New Roman">"M+"</FONT>表示存储结果,<FONT face="Times New Roman">"MR"</FONT>表示弹出所存储的结果,<FONT face="Times New Roman">"sqrt"</FONT>表示开方,<FONT face="Times New Roman">"X**y"</FONT>表示求幂。当所输入的数据不合适或计算结果溢出时对话框内会显示错误提示信息。</P>  本程序在<FONT face="Times New Roman">AutoCAD R12.0 For&nbsp;DOS</FONT>及<FONT face="Times New Roman">For&nbsp;WINDOWS</FONT>版上均调试通过,但在<FONT face="Times New Roman">WINDOWS</FONT>版本中所显示的图形与图<FONT face="Times New Roman">1</FONT>略有不同。

ch402 发表于 2010-4-26 19:23:08

:):):)!!!!!!!!!!!!!!!!!!

sitoo 发表于 2010-4-27 10:05:06

哈哈,AUTOCAD08版已经有了计算器啦,O(∩_∩)O哈哈~
页: [1]
查看完整版本: 给AutoCAD增加计算器功能