明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 26026|回复: 323

初学者请进,告诉你一些自定义命令的编程范例,样板程序。

  [复制链接]
发表于 2012-5-14 13:18 | 显示全部楼层 |阅读模式
本帖最后由 fawn_lgc 于 2012-5-19 12:07 编辑



我发现包括我自己在内,不少朋友都是通过自学掌握Lisp编程的,程序写得往往不规范。
程序怎么写才是完美的呢,到哪里才能得到可供初学者参考规范的样板程序呢。
其实,AutoCAD软件里就有这样的程序。
下面是来自AutoCAD\Support\acad.mnl的一段程序
  1. ;;;重生成布局
  2. (defun c:ai_fms ( / fmsa fmsb)
  3.   (setq m:err *error* *error* *merr*)
  4.   (ai_undo_push)
  5.   (if (getvar "TILEMODE") (setvar "TILEMODE" 0))
  6.   (setq fmsa (vports) fmsb (nth 0 fmsa))
  7.   (if (member 1 fmsb)
  8.     (if (> (length fmsa) 1)
  9.       (command "_.mspace")
  10.       (progn
  11.         (ai_sysvar '("cmdecho" . 1))
  12.         (command "_.mview")
  13.         (while (eq 1 (logand 1 (getvar "CMDACTIVE")))
  14.           (command pause)
  15.         )
  16.         (ai_sysvar NIL)
  17.         (command "_.mspace")
  18.       )
  19.     )
  20.   )
  21.   (ai_undo_pop)
  22.   (setq *error* m:err m:err nil)
  23.   (princ)
  24. )

下面是来自AutoCAD\Support\acad.dcl的一段对话框文件
  1. acad_plot : dialog {
  2.     label = "打印配置";
  3.     : row {
  4.         : column {
  5.             : boxed_column {
  6.                 label = "设备和默认信息";
  7.                 : text {
  8.                     key = "userid";
  9.                     width = 35;
  10.                 }
  11.                 : button {
  12.                     label = "设备和默认选择(&D)...";
  13.                     fixed_width = true;
  14.                     key = "devsel";
  15.                     alignment = centered;
  16.                 }
  17.             }
  18.             : boxed_row {
  19.                 label = "画笔参数";
  20.                 children_fixed_width = true;
  21.                 : button {
  22.                     label = "画笔指定(&P)...";
  23.                     key = "assign";
  24.                 }
  25.                 : button {
  26.                     label = "优化(&O)...";
  27.                     key = "optimize";
  28.                 }
  29.             }
  30.             : boxed_column {
  31.                 label = "其他参数";
  32.                 : row {
  33.                     : radio_column {
  34.                         fixed_width = true;
  35.                         : radio_button {
  36.                             label = "显示(&Y)";
  37.                             key = "D";
  38.                         }
  39.                         : radio_button {
  40.                             label = "图形范围(&X)";
  41.                             key = "E";
  42.                         }
  43.                         : radio_button {
  44.                             label = "图形界限(&L)";
  45.                             key = "L";
  46.                         }
  47.                         : radio_button {
  48.                             label = "视图(&V)";
  49.                             key = "V";
  50.                         }
  51.                         : radio_button {
  52.                             label = "窗口(&W)";
  53.                             key = "W";
  54.                         }
  55.                     }
  56.                     : column {
  57.                         : edit12_box {
  58.                             label = "文字分辨率";
  59.                             key = "textreso";
  60.                             errmsg = "Text Resolution";
  61.                         }
  62.                         : toggle {
  63.                             label = "文字填充";
  64.                             key = "textfill";
  65.                         }
  66.                         : toggle {
  67.                             label = "隐藏线(&H)";
  68.                             key = "hide";
  69.                         }
  70.                         : toggle {
  71.                             label = "调整区域填充(&J)";
  72.                             key = "plfill";
  73.                         }
  74.                         : toggle {
  75.                             label = "打印到文件(&F)";
  76.                             key = "file";
  77.                         }
  78.                         : toggle {
  79.                             label = "Auto&spool";
  80.                             key = "aspool";
  81.                         }
  82.                     }
  83.                 }
  84.                 : row {
  85.                     alignment = centered;
  86.                     : button {
  87.                         label = "视图(&E)...";
  88.                         key = "view";
  89.                         fixed_width = true;
  90.                     }
  91.                     : button {
  92.                         label = "窗口(&N)...";
  93.                         key = "selwind";
  94.                         fixed_width = true;
  95.                     }
  96.                     : button {
  97.                         label = "文件名(&A)...";
  98.                         key = "sfile";
  99.                         fixed_width = true;
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.         : column {
  105.                 : boxed_column {
  106.                     label = "图纸尺寸和方向";
  107.                     : row {
  108.                         : radio_column {
  109.                             fixed_width = true;
  110.                             : radio_button {
  111.                                 label = "英寸(&C)";
  112.                                 key = "I";
  113.                             }
  114.                             : radio_button {
  115.                                 label = "毫米(&M)";
  116.                                 key = "M";
  117.                             }
  118.                         }
  119.                         : row {
  120.                             fixed_width = true;
  121.                             : button {
  122.                                 label = "尺寸(&Z)...";
  123.                                 key = "sizbuttn";
  124.                                 fixed_width = true;
  125.                             }
  126.                             : text {
  127.                                 key = "size";
  128.                                 width = 6;
  129.                             }
  130.                             : column {
  131.                                 spacer;
  132.                                 : image {
  133.                                     key = "vec_image";
  134.                                     width = 4.0;
  135.                                     fixed_width = true;
  136.                                     aspect_ratio = 1.0;
  137.                                     color = dialog_background;
  138.                                 }
  139.                                 spacer;
  140.                             }
  141.                         }
  142.                     }
  143.                     : text {
  144.                         key = "area";
  145.                         width = 25;
  146.                     }
  147.                 }
  148.                 : boxed_column {
  149.                     label = "比例、旋转和原点";
  150.                     : button {
  151.                         label = "旋转和原点(&G)...";
  152.                         key = "rot&or";
  153.                         alignment = centered;
  154.                         fixed_width = true;
  155.                     }
  156.                     : row {
  157.                         : column {
  158.                             children_fixed_width = true;
  159.                             : text {
  160.                                 key = "pu";
  161.                                 width = 14;
  162.                                 alignment = right;
  163.                             }
  164.                             : edit_box {
  165.                                 edit_width = 10;
  166.                                 key = "punit";
  167.                                 alignment = centered;
  168.                             }
  169.                         }
  170.                         : column {
  171.                             children_fixed_width = true;
  172.                             children_alignment = centered;
  173.                             : text {
  174.                                 label = "=";
  175.                             }
  176.                             : text {
  177.                                 label = "=";
  178.                             }
  179.                         }
  180.                         : column {
  181.                             children_fixed_width = true;
  182.                             : text {
  183.                                 label = "图形单位";
  184.                             }
  185.                             : edit_box {
  186.                                 edit_width = 10;
  187.                                 key = "dunit";
  188.                                 alignment = centered;
  189.                             }
  190.                         }
  191.                     }
  192.                     : toggle {
  193.                         label = "缩放到充满(&T)";
  194.                         key = "fit";
  195.                     }
  196.                 }
  197.                 : boxed_column {
  198.                     label = "打印预览";
  199.                     : row {
  200.                         children_fixed_width = true;
  201.                         : button {
  202.                             label = "预览(&R)...";
  203.                             key = "prev";
  204.                         }
  205.                         : radio_row {
  206.                             : radio_button {
  207.                                 label = "部分(&I)";
  208.                                 key = "earea";
  209.                             }
  210.                             : radio_button {
  211.                                 label = "全部(&U)";
  212.                                 key = "full";
  213.                             }
  214.                         }
  215.                     }
  216.                 }
  217.         }
  218.     }
  219.     ok_cancel_help;
  220.     : row {
  221.         : errtile { fixed_width = true; }

  222.         // This spacer_0 between the two fixed-width items serves
  223.         // to left-justify the first and right-justify the second,
  224.         // since it's the only flexible-width item in the row.
  225.         spacer_0;

  226.         : row {
  227.             fixed_width = true;
  228.             : text {
  229.                 key = "zero";
  230.                 width = 3;
  231.             }
  232.             : image {
  233.                 key = "pre_image";
  234.                 color = dialog_background;
  235.                 height = 1;
  236.                 width = 18;
  237.             }
  238.             : text {
  239.                 key = "comp";
  240.                 width = 5;
  241.             }
  242.         }
  243.     }
  244. }

谢谢大家的回复!
补充一句,不少网友希望加些文字说明。
我开始也有这个想法,但是官方的东西太权威,我也是菜鸟一个,胡乱注释,反倒误导大家。
建议大家通过逐行运行,理解它的真谛。
如果还有什么不明白的,建议大家另行发帖提问。


"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2021-8-8 18:25 | 显示全部楼层

学习。谢谢分享
学习。谢谢分享
发表于 2022-6-2 14:16 | 显示全部楼层
谢谢楼主分享
发表于 2021-8-9 22:50 | 显示全部楼层
谢谢楼主分享!!!!!
发表于 2012-5-14 13:28 | 显示全部楼层
学习。谢谢分享
发表于 2012-5-14 13:45 | 显示全部楼层
规范的程序代码,可读性很高,程序层次感清晰。
发表于 2012-5-14 13:54 | 显示全部楼层
楼主太省了,不光看例子,应该说些心得吧?
这话题不错!
发表于 2012-5-14 14:08 | 显示全部楼层
顶上顶上~~~~~~~~~~~~~~
发表于 2012-5-14 14:13 | 显示全部楼层
学习学习!正需要了!
发表于 2012-5-14 14:16 | 显示全部楼层
ai开头的函数全是自带的吗,系统里找到ai_utils.lsp,确实是很好的例子
发表于 2012-5-14 16:33 | 显示全部楼层
不错,学习看看
发表于 2012-5-14 16:35 | 显示全部楼层
呵呵,自己用的随便改改都很乱
发表于 2012-5-14 19:11 来自手机 | 显示全部楼层
向前辈学习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-28 03:36 , Processed in 0.315777 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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