找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 998|回复: 0

[日积月累]:对话框及管理程序开发实例

[复制链接]

已领礼包: 943个

财富等级: 财运亨通

发表于 2002-12-19 13:04:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
这个程序所用到的对话框定义文件

  1.   [FONT=courier new]
  2. bmake : dialog {
  3.         label = "定义图块";
  4.         : edit_box {
  5.                 label = "图块名&N:";
  6.                 key = "bname";   
  7.                 edit_limit = 2048;
  8.         }
  9.         spacer;
  10.         : row {
  11.                 : boxed_column {
  12.                         label = "插入点";
  13.                         : button {
  14.                                 label = "选择点&P <";
  15.                                 key = "pick_pt";
  16.                         }
  17.                         : edit_box {
  18.                                 label = "&X:";
  19.                                 key = "x_pt";
  20.                                 edit_width = 10;
  21.                         }
  22.                         : edit_box {
  23.                                 label = "&Y:";
  24.                                 key = "y_pt";
  25.                                 edit_width = 10;
  26.                         }
  27.                         : edit_box {


  28.                                 label = "&Z:";
  29.                                 key = "z_pt";
  30.                                 edit_width = 10;
  31.                         }
  32.                 }
  33.                 : column {
  34.                         spacer_1;
  35.                         : button {
  36.                                 label = "选择实体&S<";
  37.                                 key = "sel_objs";
  38.                         }
  39.                         : concatenation {
  40.                                 : text_part {
  41.                                         label = "数目发现: ";
  42.                                 }
  43.                                 : text_part {
  44.                                         key = "how_many";
  45.                                         width = 5;
  46.                                 }
  47.                         }
  48.                         spacer;
  49.                         : button {
  50.                                 label = "图块名列表&L...";
  51.                                 key = "list_blocks";
  52.                         }
  53.                         : toggle {
  54.                                 label = "保留原实体&R";
  55.                                 key = "retain";
  56.                         }
  57.                 }
  58.         }
  59.         spacer;
  60.         spacer;
  61.         ok_cancel_help_errtile;
  62. }

  63. bmake_bname_exists : dialog {
  64.         label = "警告";
  65.         : paragraph {
  66.                 : text_part {
  67.                         label = "图中已有同名的图块。";
  68.                 }
  69.                 : text_part {
  70.                         label = "你想重定义它吗?";
  71.                 }
  72.         }
  73.         spacer_1;
  74.         : row {
  75.                 fixed_width = true;
  76.                 alignment = centered;
  77.                 : button {
  78.                         label = "重定义&R";
  79.                         key = "yes";
  80.                         width = 8;
  81.                 }
  82.                 : spacer {
  83.                         width = 2;
  84.                 }
  85.                 : default_button {
  86.                         label = "取消";
  87.                         key = "no";
  88.                         width = 8;
  89.                 }
  90.         }
  91. }
  92. bmake_list_blocks : dialog {
  93.         label = "图块名";
  94.         : edit_box {
  95.                 label = "图案&P:";
  96.                 key = "pattern";
  97.         }
  98.         : list_box {
  99.                 key = "bl_match";
  100.                 width = 32;
  101.         }
  102.         spacer;
  103.         ok_only;
  104. }
  105. [/FONT]


实现管理对话框并定义图块功能的Lisp程序






  1.   [FONT=courier new]
  2. (defun  ai_abort  (app  msg)
  3.         (defun  *error*  (s)
  4.                 (if  old_error  (setq  *error*  old_error))
  5.                 (princ)
  6.         )
  7.         (if  msg
  8.                 (alert  (strcat  " 应用程序错误: "  app  " \n\n  "  msg  "  \n"))
  9.         )
  10.         (exit)
  11. )
  12. (cond
  13.         (  (and  ai_dcl  (listp  ai_dcl)))          ; 该程序已经加载.
  14.         (  (not (findfile "ai_utils.lsp"))          ; 找文件ai_utils.lsp
  15.                 (ai_abort "BMAKE"
  16.                         (strcat "不能定位文件AI_UTILS.LSP."        "\n 检查support目录.")
  17.                 )
  18.         )
  19.         (  (eq  "failed"  (load  "ai_utils"  "failed"))         ; 加载AI_UTILS.LSP
  20.                 (ai_abort  "BMAKE"  "不能调入文件AI_UTILS.LSP")
  21.         )
  22. )
  23. (if  (not  (ai_acadapp))               ; 函数ai_acadapp在AI_UTILS.LSP中定义
  24.         (ai_abort "BMAKE" nil)         ;显示退出报警框
  25. )

  26. (defun c:bmake ( /  bname  x_pt  y_pt  dcl_id  z_pt   retain  selection_set
  27.         old_err  what_next  pick_pt  do_oops  pat  block_list  old_error)
  28.         (defun  bmk_defaults()
  29.                 (if  (not  klm)  (dscprinc))
  30.                 (if  bname  (set_tile  "bname"  bname))
  31.                 (if  (/=  0  retain)  (setq  retain  1))
  32.                 (set_tile  "retain"  (itoa retain))
  33.                 (if  (not  x_pt)  (setq  x_pt  0.0))
  34.                 (if  (not  y_pt)  (setq  y_pt  0.0))
  35.                 (if  (not  z_pt)  (setq  z_pt  0.0))
  36.                 (set_tile  "x_pt"  (rtos  x_pt))
  37.                 (set_tile  "y_pt"  (rtos  y_pt))
  38.                 (set_tile  "z_pt"  (rtos  z_pt))
  39.                 (set_tile  "how_many"
  40.                         (if  selection_set
  41.                                 (itoa  (sslength_CurrentSpace  selection_set))
  42.                                 "0"
  43.                         )
  44.                 )
  45.         )
  46.         (defun  bmk_check_real(real_number  coord)
  47.                 (if  (not  klm)  (dscprinc))
  48.                 (if  real_number  
  49.                         (progn
  50.                                 (rs_error)
  51.                                 (set_tile  coord  (rtos  real_number))
  52.                                 real_number
  53.                         )
  54.                         (progn
  55.                                 (set_tile  "error"  (strcat  "Invalid "
  56.                                         (strcase  (substr  coord  1  1))
  57.                                         " coordinate.")
  58.                                 )
  59.                                 (mode_tile  coord  2)
  60.                                 (mode_tile  coord  3)
  61.                                 nil
  62.                         )
  63.                 )
  64.         )
  65.         (defun  bmk_check_name(name)
  66.                 (if  (not  klm)
  67.                         (dscprinc)
  68.                 )
  69.                 (if  (not  (snvalid  name))
  70.                         (progn
  71.                                 (set_tile  "error"  "错误的图块名.")
  72.                                 nil
  73.                         )
  74.                         (progn
  75.                                 (rs_error)
  76.                                 name
  77.                         )
  78.                 )
  79.         )
  80.         (defun  bmk_bexist()
  81.                 (if  (not  klm)
  82.                         (dscprinc)
  83.                 )
  84.                 (cond
  85.                         ((not  (bmk_check_real  x_pt  "x_pt")))
  86.                         ((not  (bmk_check_real  y_pt  "y_pt")))
  87.                         ((not  (bmk_check_real  z_pt  "z_pt")))
  88.                         ((or  (not  bname)  (=  ""  bname))
  89.                 (set_tile  "error"  "不允许空的图块名。")
  90.                                 (mode_tile  "bname"  2)
  91.                                 (mode_tile  "bname"  3)
  92.                         )
  93.                         ((and  selection_set  (bmk_check_ref)))
  94.                         ((member  bname  block_list)
  95.                                 (if  (not  (new_dialog  "bmake_bname_exists"  dcl_id))  (exit))
  96.                                         (action_tile "yes" "(done_dialog 2)")
  97.                                         (action_tile "no"  "(done_dialog 0)")
  98.                                 (if (= (start_dialog) 2) (done_dialog 2))
  99.                                 )
  100.                         (T
  101.                                 (if  (bmk_check_name  bname)
  102.                                         (done_dialog  2)
  103.                                         (progn
  104.                                                 (mode_tile  "bname"  2)
  105.                                                 (mode_tile  "bname"  3)
  106.                                         )
  107.                                 )
  108.                         )
  109.                 )
  110.         )
  111.         (defun  bmk_check_ref (/  a  ref  ent_list  sslen  self_list)
  112.                 (if (not klm)
  113.                         (dscprinc)
  114.                 )
  115.                 (setq  a  0  self_list  nil  ref  nil
  116.                         sslen  (ai_sslength  selection_set)
  117.                 )
  118.                 (while  (<  a  sslen)
  119.                         (setq  ent_list  (entget  (ssname  selection_set  a)))
  120.                         (if  (=  "INSERT"  (cdr  (assoc  0  ent_list)))
  121.                                 (setq  self_list
  122.                                         (cons (cdr (assoc 2 ent_list))  self_list)
  123.                                 )
  124.                         )
  125.                         (setq a (1+ a))
  126.                 )
  127.                 (if  self_list
  128.                         (if (cond ((member bname self_list)) ((bmk_self_ref bname self_list)))
  129.                                 (set_tile "error"  "错误 - 该图块参照了它自身.")
  130.                         )
  131.                 )
  132.         )
  133.         (defun  bmk_self_ref  (self  others  /  ent  other_list)
  134.                 (if  (not  klm)
  135.                         (dscprinc)
  136.                 )
  137.                 (setq  other_list  nil)
  138.                 (foreach  n  others
  139.                         (setq  en1  (cdr  (assoc  -2  (tblsearch "block" n)))) ;第一个实体
  140.                         (while  en1
  141.                                 (setq  ent  (entget  en1))
  142.                                 (if  (and  (=  "INSERT"  (cdr  (assoc  0  ent)))
  143.                                         (not (member (setq other_name (cdr (assoc 2 ent))) others))
  144.                                         )
  145.                                         (setq  other_list  (cons  other_name  other_list))
  146.                                 )
  147.                                 (setq  en1  (entnext  en1))
  148.                         )
  149.                         (if  (and  other_list  (member  self  other_list))
  150.                                 (setq  ref  t)
  151.                                 (bmk_self_ref  self  other_list)
  152.                         )
  153.                 )
  154.                 ref                  ;当引用自身时返回t,否则返回nil.
  155.         )
  156.         (defun  bmk_list_blocks()
  157.                 (if  (not  klm)
  158.                         (dscprinc)
  159.                 )
  160.                 (if  (not  (new_dialog  "bmake_list_blocks"  dcl_id))  (exit))
  161.                 (if  (not  pat)  (setq  pat  "*"))
  162.                 (set_tile  "pattern"  pat)
  163.                 (bmk_pat_match)
  164.                 (action_tile  "pattern"  "(setq  pat  $value)  (bmk_pat_match)")
  165.                 (action_tile  "accept"  "(done_dialog  0)")
  166.                 (start_dialog)
  167.         )
  168.         (defun  bmk_pat_match  (/  pat)
  169.                 (if  (not  klm)
  170.                         (dscprinc)
  171.                 )
  172.                 (setq  bl_match  nil  pat  (strcase  (get_tile  "pattern")))
  173.                 (if  (=  pat  "")  (setq  pat  "*"))
  174.                 (foreach  n  block_list
  175.                         (if  (wcmatch  n  pat)
  176.                                 (setq  bl_match  (cons  n  bl_match))
  177.                         )
  178.                 )
  179.                 (if  (and  bl_match
  180.                         (>=  (getvar  "maxsort")  (length  bl_match)) ;按字母顺序排列
  181.                         )
  182.                         (setq  bl_match  (acad_strlsort  bl_match))
  183.                 )
  184.                 (start_list  "bl_match")
  185.                 (mapcar  'add_list  bl_match)
  186.                 (end_list)
  187.         )
  188.         (defun  bmk_make_block( /  old_exp  str_exp)
  189.                 (if  (not  klm)       
  190.                         (dscprinc)
  191.                 )
  192.                 (setq  str_exp  "expert"
  193.                         old_exp  (getvar  str_exp))      ;存储当前expert的设置
  194.                 (setvar  str_exp  2)
  195.                 (if  (and  selection_set
  196.                                 (<  0  (sslength_CurrentSpace  selection_set))
  197.                         )
  198.                         (progn
  199.                         (command "_block" bname "_none" (list x_pt y_pt z_pt) selection_set "")
  200.                         (setq  do_oops  1)
  201.                         )
  202.                         (progn
  203.                                 (command  "_block" bname "_none" (list x_pt y_pt z_pt) "")
  204.                                 (setq do_oops 0)
  205.                         )
  206.                 )
  207.                 (if  (and  (=  retain  1)
  208.                                 (=  do_oops  1)
  209.                         )
  210.                         (command "_oops")
  211.                 )
  212.                 (setvar  str_exp  old_exp)
  213.         )
  214.         (defun  bmk_bmake_main ()
  215.                 (if  (not  klm)
  216.                         (dscprinc)
  217.                 )
  218.                 (setq  block_list  (ai_table  "block"  2)
  219.                         what_next   5
  220.                 )
  221.                 (while  (<  2  what_next)
  222.                         (if  (not  (new_dialog  "bmake"  dcl_id))  (exit))
  223.                         (bmk_defaults)
  224.                 (if  (=  5  what_next)  (mode_tile  "bname"  2)) ;设置输入焦点到块名栏
  225. (action_tile "bname"  "(bmk_check_name (setq bname (strcase (get_tile $key))))")
  226.                         (action_tile  "pick_pt"  "(rs_error)  (done_dialog  4)")
  227. (action_tile "x_pt"  "(setq x_pt (bmk_check_real (distof (get_tile $key)) $key))")
  228. (action_tile "y_pt"  "(setq y_pt (bmk_check_real (distof (get_tile $key)) $key))")
  229. (action_tile "z_pt"  "(setq z_pt (bmk_check_real (distof (get_tile $key)) $key))")
  230.                         (action_tile  "sel_objs"  "(done_dialog 3)")
  231.                         (action_tile  "list_blocks"  "(rs_error)  (bmk_list_blocks)")
  232.                         (action_tile  "retain"  "(setq  retain  (atoi  $value))")
  233.                         (action_tile  "accept"  "(bmk_bexist)")
  234.                         (action_tile  "cancel"  "(done_dialog  0)")
  235.                 (action_tile  "help"  "(help  ""  "block_definition_dialog")")
  236.                         (setq  what_next  (start_dialog))   ; 将控制权交给对话框.
  237.                         (cond                               ; 决定下一步做什么.
  238.                                 ((=  what_next  3)
  239.                                         (setq  temp_ss  (ssget))
  240.                                         (if  temp_ss
  241.                                                 (setq  selection_set  (ai_ssget  temp_ss))
  242.                                         )
  243.                                         (rs_error)
  244.                                 )
  245.                                 ((=  what_next  4)
  246.                                         (initget  1)
  247.                                         (setq  pick_pt  (getpoint  "Insertion base point: "))
  248.                                         (if  pick_pt
  249.                                                 (setq  x_pt  (car  pick_pt)    ; 当前UCS
  250.                                                         y_pt  (cadr  pick_pt)
  251.                                                         z_pt  (caddr  pick_pt)
  252.                                                 )
  253.                                         )
  254.                                 )
  255.                         )
  256.                 )
  257.                 (if  (=  what_next  2)
  258.                         (bmk_make_block)
  259.                 )
  260.         )
  261.         (defun  rs_error ()
  262.                 (set_tile  "error" "")
  263.         )
  264.         (defun  sslength_CurrentSpace(ss)
  265.                 (setq  ss_count  (sslength  ss))
  266.                 (if  (and  (=  0  (getvar  "tilemode"))
  267.                                 (=  1  (getvar  "cvport"))
  268.                         )
  269.                         (setq  flag  1)
  270.                         (setq  flag  0)
  271.                 )
  272.                 (setq  a  0)
  273.                 (while  (<  a  (sslength  ss))
  274.                         (setq  ent_info  (entget  (ssname  ss  a)))
  275.                         (if  (or  (and  (=  "VIEWPORT"  (cdr  (assoc  '0  ent_info)))
  276.                                                 (=  1  (cdr  (assoc  '69  ent_info)))
  277.                                         )
  278.                                         (/=  flag  (cdr  (assoc  '67 ent_info)))
  279.                                 )
  280.                                 (setq ss_count (1- ss_count))
  281.                         )
  282.                         (setq  a  (1+  a))
  283.                 )
  284.                 (fix  ss_count)
  285.         )
  286.         (setq  old_cmd  (getvar  "cmdecho")  ;存储当前的cmdecho
  287.                 old_error  *error*
  288.                 *error*  ai_error
  289.         )
  290.         (setvar  "cmdecho"  0)
  291.         (setq  selection_set
  292.                 (if  (and  (eq  1  (logand  1  (getvar  "pickfirst")))
  293.                                 (setq  selection_set  (ssget  "_i"))
  294.                         )
  295.                         (ai_ssget  selection_set)
  296.                 )
  297.         )
  298.         (cond
  299.                 ( (not  (ai_notrans)))             ; transparent not OK
  300.                 ( (not  (setq  dcl_id  (ai_dcl  "bmake"))))
  301.                 (T  (ai_undo_push)
  302.                         (bmk_bmake_main)               ; 处理!
  303.                         (ai_undo_pop)
  304.                 )
  305.         )
  306.         (setq  *error*  old_error)
  307.         (setvar  "cmdecho"  old_cmd)
  308.         (princ)
  309. )
  310. (princ "  BMAKE loaded.")
  311. (princ)
  312. (princ)
  313. [/FONT]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-26 06:36 , Processed in 0.195974 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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