乐筑天下

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

[编程交流] 向lis添加增量编号

[复制链接]

5

主题

24

帖子

19

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-5 15:05:01 | 显示全部楼层 |阅读模式
你以前帮过我一个Lisp程序,我正试图对另一个Lisp程序做同样的事情,但没有成功。
我原来的帖子
http://www.cadtutor.net/forum/showthread.php?104566-修改lisp将增量编号添加到区域文本
 
我的目标和我的第一篇帖子一样,在文本中的“L”字母旁边添加增量数字
我要更改的lisp:
  1. ;;-------------------------=={ Length Field }==-------------------------;;
  2. ;;                                                                      ;;
  3. ;;  This program allows a user to generate a field expression           ;;
  4. ;;  referencing the length/perimeter/circumference of one or more       ;;
  5. ;;  selected objects. In the case of selecting multiple objects, the    ;;
  6. ;;  field expression will reference the sum of the lengths of all       ;;
  7. ;;  objects in the selection.                                           ;;
  8. ;;                                                                      ;;
  9. ;;  The user may opt to specify a point at which to create a new        ;;
  10. ;;  multiline text object housing the field expression, pick a table    ;;
  11. ;;  cell in which the field should be inserted, or select an existing   ;;
  12. ;;  single-line text, multiline text, multileader, or attribute to      ;;
  13. ;;  be populated with the field expression.                             ;;
  14. ;;                                                                      ;;
  15. ;;  Upon issuing the command syntax 'LF' (Length Field) at the AutoCAD  ;;
  16. ;;  command-line, the program first prompts the user to make a          ;;
  17. ;;  selection of objects for which to return the length summation.      ;;
  18. ;;                                                                      ;;
  19. ;;  At this prompt, the user may select any number of Arcs, Circles,    ;;
  20. ;;  Lines, 2D Polylines (light or heavy), or 3D Polylines.              ;;
  21. ;;                                                                      ;;
  22. ;;  The user is then prompted to specify a point or table cell to       ;;
  23. ;;  insert a field expression referencing the summation of the lengths  ;;
  24. ;;  of the selected objects.                                            ;;
  25. ;;                                                                      ;;
  26. ;;  At this prompt, the user may also choose the 'Object' option in     ;;
  27. ;;  order to populate the content of an existing annotation object      ;;
  28. ;;  with the field expression.                                          ;;
  29. ;;                                                                      ;;
  30. ;;  Upon choosing this option, the user may select any single-line      ;;
  31. ;;  text (DText), multiline text (MText), single-line or multiline      ;;
  32. ;;  attribute, attributed block, or multileader (MLeader) with either   ;;
  33. ;;  multiline text or attributed block content.                         ;;
  34. ;;                                                                      ;;
  35. ;;  If the user selects an attributed block or attributed multileader   ;;
  36. ;;  with more than one attribute, the user is presented with a dialog   ;;
  37. ;;  interface listing the available attributes, and is prompted to      ;;
  38. ;;  select a destination for the field expression.                      ;;
  39. ;;                                                                      ;;
  40. ;;  The user may optionally predefine the target block/multileader      ;;
  41. ;;  attribute by specifying the attribute tag where noted at the top    ;;
  42. ;;  of the program source code.                                         ;;
  43. ;;                                                                      ;;
  44. ;;  The resulting field expression will display the sum of the lengths  ;;
  45. ;;  of the selected objects, formatted using the field formatting code  ;;
  46. ;;  specified at the top of the program.                                ;;
  47. ;;                                                                      ;;
  48. ;;----------------------------------------------------------------------;;
  49. ;;  Author:  Lee Mac, Copyright © 2017  -  www.lee-mac.com              ;;
  50. ;;----------------------------------------------------------------------;;
  51. ;;  Version 1.0    -    2017-08-06                                      ;;
  52. ;;                                                                      ;;
  53. ;;  - First release.                                                    ;;
  54. ;;----------------------------------------------------------------------;;
  55. ;;  Version 1.1    -    2017-08-06                                      ;;
  56. ;;                                                                      ;;
  57. ;;  - Program modified to account for selection of existing annotation  ;;
  58. ;;    objects which already contain a field expression.                 ;;
  59. ;;----------------------------------------------------------------------;;
  60. (defun c:lf ( / *error* ent enx flg fmt idx lst obj oid prp sel str tab tag tmp typ )
  61.    (setq fmt "L=%lu6%pr2 cm" ;; Field Formatting ;; L1, L2, L3, L...
  62.          tag nil    ;; Optional predefined attribute tag
  63.    )
  64.    
  65.    (defun *error* ( msg )
  66.        (LM:endundo (LM:acdoc))
  67.        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
  68.            (princ (strcat "\nError: " msg))
  69.        )
  70.        (princ)
  71.    )
  72.    (LM:startundo (LM:acdoc))
  73.    (setq prp
  74.       '(
  75.            ("AcDbArc"        . "ArcLength")
  76.            ("AcDbCircle"     . "Circumference")
  77.            ("AcDbLine"       . "Length")
  78.            ("AcDbPolyline"   . "Length")
  79.            ("AcDb2dPolyline" . "Length")
  80.            ("AcDb3dPolyline" . "Length")
  81.        )
  82.    )
  83.    (if
  84.        (setq sel
  85.            (LM:ssget "\nSelect objects to obtain total length <exit>: "
  86.                (list
  87.                    (list
  88.                       '(000 . "ARC,CIRCLE,LINE,*POLYLINE")
  89.                       '(-04 . "<NOT")
  90.                           '(-04 . "<AND")
  91.                               '(000 . "POLYLINE") '(-04 . "&") '(070 . 80)
  92.                           '(-04 . "AND>")
  93.                       '(-04 . "NOT>")
  94.                        (if (= 1 (getvar 'cvport))
  95.                            (cons 410 (getvar 'ctab))
  96.                           '(410 . "Model")
  97.                        )
  98.                    )
  99.                )
  100.            )
  101.        )
  102.        (progn
  103.            (if (= 1 (sslength sel))
  104.                (setq obj (vlax-ename->vla-object (ssname sel 0))
  105.                      str
  106.                    (strcat
  107.                        "%<\\AcObjProp Object(%<\\_ObjId "
  108.                        (LM:objectid obj)
  109.                        ">%)." (cdr (assoc (vla-get-objectname obj) prp)) " \\f "" fmt "">%"
  110.                    )
  111.                )
  112.                (progn
  113.                    (repeat (setq idx (sslength sel))
  114.                        (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx))))
  115.                              lst
  116.                            (vl-list*
  117.                                "%<\\AcObjProp Object(%<\\_ObjId "
  118.                                (LM:objectid obj)
  119.                                ">%)." (cdr (assoc (vla-get-objectname obj) prp)) ">%" " + "
  120.                                lst
  121.                            )
  122.                        )
  123.                    )
  124.                    (setq str
  125.                        (strcat
  126.                            "%<\\AcExpr "
  127.                            (apply 'strcat (reverse (cdr (reverse lst))))
  128.                            " \\f "" fmt "">%"
  129.                        )
  130.                    )
  131.                )
  132.            )
  133.            (if
  134.                (setq tmp
  135.                    (ssget "_X"
  136.                        (list '(0 . "ACAD_TABLE")
  137.                            (if (= 1 (getvar 'cvport))
  138.                                (cons 410 (getvar 'ctab))
  139.                               '(410 . "Model")
  140.                            )
  141.                        )
  142.                    )
  143.                )
  144.                (repeat (setq idx (sslength tmp))
  145.                    (setq tab (cons (vlax-ename->vla-object (ssname tmp (setq idx (1- idx)))) tab))
  146.                )
  147.            )
  148.            (while
  149.                (not
  150.                    (progn
  151.                        (if flg
  152.                            (progn
  153.                                (setvar 'errno 0)
  154.                                (initget "Point eXit")
  155.                                (setq sel (nentsel "\nSelect text, mtext, mleader, attribute or attributed block [Point/eXit] <eXit>: "))
  156.                            )
  157.                            (progn
  158.                                (initget "Object eXit")
  159.                                (setq sel (getpoint "\nSpecify point or cell for field [Object/eXit] <eXit>: "))
  160.                            )
  161.                        )
  162.                        (cond
  163.                            (   (= 7 (getvar 'errno))
  164.                                (prompt "\nMissed, try again.")
  165.                            )
  166.                            (   (or (null sel) (= "eXit" sel)))
  167.                            (   (= "Point" sel)
  168.                                (setq flg nil)
  169.                            )
  170.                            (   (= "Object" sel)
  171.                                (not (setq flg t))
  172.                            )
  173.                            (   flg
  174.                                (setq ent (car sel)
  175.                                      enx (entget ent)
  176.                                      typ (cdr (assoc 0 enx))
  177.                                      obj (vlax-ename->vla-object ent)
  178.                                )
  179.                                (cond
  180.                                    (   (and (= 2 (length sel)) (wcmatch typ "TEXT,MTEXT"))
  181.                                        (if (vlax-write-enabled-p obj)
  182.                                            (LF:puttextstring obj str)
  183.                                            (prompt "\nThe selected text object is on a locked layer.")
  184.                                        )
  185.                                    )
  186.                                    (   (and (= "ATTRIB" typ)
  187.                                             (/= 'str (type tag))
  188.                                        )
  189.                                        (if (vlax-write-enabled-p obj)
  190.                                            (progn
  191.                                                (LF:puttextstring obj str)
  192.                                                (LF:updatefield ent)
  193.                                            )
  194.                                            (prompt "\nThe selected attribute is on a locked layer.")
  195.                                        )
  196.                                    )
  197.                                    (   (and
  198.                                            (or
  199.                                                (and (= "ATTRIB" typ)
  200.                                                     (setq tmp (cdr (assoc 330 enx)))
  201.                                                )
  202.                                                (and (setq tmp (last (cadddr sel)))
  203.                                                     (= "INSERT" (cdr (assoc 0 (entget tmp))))
  204.                                                )
  205.                                            )
  206.                                            (setq tmp (vlax-invoke (vlax-ename->vla-object tmp) 'getattributes))
  207.                                            (or
  208.                                                (and (= 'str (type tag))
  209.                                                     (setq idx (vl-position (strcase tag) (mapcar 'vla-get-tagstring tmp)))
  210.                                                     (setq obj (nth idx tmp))
  211.                                                )
  212.                                                (and (not (cdr tmp))
  213.                                                     (setq obj (car tmp))
  214.                                                )
  215.                                                (and (setq idx (LM:listbox "Choose Attribute" (mapcar 'vla-get-tagstring tmp) 2))
  216.                                                     (setq obj (nth (car idx) tmp))
  217.                                                )
  218.                                            )
  219.                                        )
  220.                                        (if (vlax-write-enabled-p obj)
  221.                                            (progn
  222.                                                (LF:puttextstring obj str)
  223.                                                (LF:updatefield (vlax-vla-object->ename obj))
  224.                                            )
  225.                                            (prompt "\nThe selected attribute is on a locked layer.")
  226.                                        )
  227.                                    )
  228.                                    (   (and (= 2 (length sel)) (= "MULTILEADER" typ))
  229.                                        (setq typ (cdr (assoc 172 (reverse enx))))
  230.                                        (cond
  231.                                            (   (and (<= acblockcontent typ acmtextcontent) (not (vlax-write-enabled-p obj)))
  232.                                                (prompt "\nThe selected multileader is on a locked layer.")
  233.                                            )
  234.                                            (   (= acmtextcontent typ)
  235.                                                (LF:puttextstring obj str)
  236.                                                (vla-regen (LM:acdoc) acactiveviewport)
  237.                                                t
  238.                                            )
  239.                                            (   (and
  240.                                                    (= acblockcontent typ)
  241.                                                    (setq tmp (LM:getmleaderattributes obj))
  242.                                                    (or
  243.                                                        (and (= 'str (type tag))
  244.                                                             (setq oid (cdr (assoc (strcase tag) tmp)))
  245.                                                        )
  246.                                                        (and (not (cdr tmp))
  247.                                                             (setq oid (cdar tmp))
  248.                                                        )
  249.                                                        (and (setq idx (LM:listbox "Choose Attribute" (mapcar 'car tmp) 2))
  250.                                                             (setq oid (cdr (nth (car idx) tmp)))
  251.                                                        )
  252.                                                    )
  253.                                                )
  254.                                                (LM:setmleaderattributevalue obj oid str)
  255.                                                (vla-regen (LM:acdoc) acactiveviewport)
  256.                                                t
  257.                                            )
  258.                                            (   (prompt "\nThe select multileader has no editable content."))
  259.                                        )
  260.                                    )
  261.                                    (   (prompt "\nThe selected object is not text, mtext, multileader, attribute or attributed block."))
  262.                                )
  263.                            )
  264.                            (   (setq tmp (LM:getcell tab (trans sel 1 0)))
  265.                                (if (vlax-write-enabled-p (car tmp))
  266.                                    (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-settext (append tmp (list str)))))
  267.                                    (prompt "\nThe selected table cell belongs to a table on a locked layer.")
  268.                                )
  269.                            )
  270.                            (   (vla-addmtext
  271.                                    (vlax-get-property (LM:acdoc) (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))
  272.                                    (vlax-3D-point (trans sel 1 0))
  273.                                    0.0
  274.                                    str
  275.                                )
  276.                            )
  277.                        )
  278.                    )
  279.                )
  280.            )
  281.        )
  282.    )
  283.    (*error* nil) (princ)
  284. )
  285. (defun LF:puttextstring ( obj str )
  286.    (vla-put-textstring obj "") ;; To clear any existing field
  287.    (vla-put-textstring obj str)
  288.    t
  289. )
  290. (defun LF:updatefield ( ent / cmd rtn )
  291.    (setq cmd (getvar 'cmdecho))
  292.    (setvar 'cmdecho 0)
  293.    (setq rtn (vl-cmdf "_.updatefield" ent ""))
  294.    (setvar 'cmdecho cmd)
  295.    rtn
  296. )
  297. ;; ssget  -  Lee Mac
  298. ;; A wrapper for the ssget function to permit the use of a custom selection prompt
  299. ;; msg - [str] selection prompt
  300. ;; arg - [lst] list of ssget arguments
  301. (defun LM:ssget ( msg arg / sel )
  302.    (princ msg)
  303.    (setvar 'nomutt 1)
  304.    (setq sel (vl-catch-all-apply 'ssget arg))
  305.    (setvar 'nomutt 0)
  306.    (if (not (vl-catch-all-error-p sel)) sel)
  307. )
  308. ;; Get MLeader Attributes  -  Lee Mac
  309. ;; Returns an association list of attribute tags & object IDs for all attributes held by an mleader block
  310. ;; mld - [vla] MLeader vla-object
  311. ;; Returns: [lst] List of ((<Attribute Tag> . <Object ID>) ... )
  312. (defun LM:getmleaderattributes ( mld / rtn )
  313.    (vlax-for obj (vla-item (vla-get-blocks (vla-get-document mld)) (vla-get-contentblockname mld))
  314.        (if
  315.            (and
  316.                (= "AcDbAttributeDefinition" (vla-get-objectname obj))
  317.                (= :vlax-false (vla-get-constant obj))
  318.            )
  319.            (setq rtn (cons (cons (strcase (vla-get-tagstring obj)) (LM:intobjectid obj)) rtn))
  320.        )
  321.    )
  322.    (reverse rtn)
  323. )
  324. ;; Object ID (integer)  -  Lee Mac
  325. ;; Returns an integer representing the ObjectID of a supplied VLA-Object
  326. ;; Compatible with 32-bit & 64-bit systems
  327. (defun LM:intobjectid ( obj )
  328.    (if (vlax-property-available-p obj 'objectid32)
  329.        (defun LM:intobjectid ( obj ) (vla-get-objectid32 obj))
  330.        (defun LM:intobjectid ( obj ) (vla-get-objectid   obj))
  331.    )
  332.    (LM:intobjectid obj)
  333. )
  334. ;; Set MLeader Attribute Value  -  Lee Mac
  335. ;; obj - [vla] MLeader vla-object
  336. ;; idx - [int] Attribute Definition Object ID
  337. ;; str - [str] Attribute value
  338. (defun LM:setmleaderattributevalue ( obj idx str )
  339.    (if (vlax-method-applicable-p obj 'setblockattributevalue32)
  340.        (defun LM:setmleaderattributevalue ( obj idx str ) (vla-setblockattributevalue32 obj idx str))
  341.        (defun LM:setmleaderattributevalue ( obj idx str ) (vla-setblockattributevalue   obj idx str))
  342.    )
  343.    (LM:setmleaderattributevalue obj idx str)
  344. )
  345. ;; List Box  -  Lee Mac
  346. ;; Displays a DCL list box allowing the user to make a selection from the supplied data.
  347. ;; msg - [str] Dialog label
  348. ;; lst - [lst] List of strings to display
  349. ;; bit - [int] 1=allow multiple; 2=return indexes
  350. ;; Returns: [lst] List of selected items/indexes, else nil
  351. (defun LM:listbox ( msg lst bit / dch des tmp rtn )
  352.    (cond
  353.        (   (not
  354.                (and
  355.                    (setq tmp (vl-filename-mktemp nil nil ".dcl"))
  356.                    (setq des (open tmp "w"))
  357.                    (write-line
  358.                        (strcat "listbox:dialog{label="" msg "";spacer;:list_box{key="list";multiple_select="
  359.                            (if (= 1 (logand 1 bit)) "true" "false") ";width=50;height=15;}spacer;ok_cancel;}"
  360.                        )
  361.                        des
  362.                    )
  363.                    (not (close des))
  364.                    (< 0 (setq dch (load_dialog tmp)))
  365.                    (new_dialog "listbox" dch)
  366.                )
  367.            )
  368.            (prompt "\nError Loading List Box Dialog.")
  369.        )
  370.        (   t     
  371.            (start_list "list")
  372.            (foreach itm lst (add_list itm))
  373.            (end_list)
  374.            (setq rtn (set_tile "list" "0"))
  375.            (action_tile "list" "(setq rtn $value)")
  376.            (setq rtn
  377.                (if (= 1 (start_dialog))
  378.                    (if (= 2 (logand 2 bit))
  379.                        (read (strcat "(" rtn ")"))
  380.                        (mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
  381.                    )
  382.                )
  383.            )
  384.        )
  385.    )
  386.    (if (< 0 dch)
  387.        (unload_dialog dch)
  388.    )
  389.    (if (and tmp (setq tmp (findfile tmp)))
  390.        (vl-file-delete tmp)
  391.    )
  392.    rtn
  393. )
  394. ;; Get Cell  -  Lee Mac
  395. ;; If the supplied point lies within a cell boundary,
  396. ;; returns a list of: (<VLA Table Object> <Row> <Col>)
  397. (defun LM:getcell ( lst pnt / dir )
  398.    (setq dir (vlax-3D-point (trans (getvar 'viewdir) 1 0))
  399.          pnt (vlax-3D-point pnt)
  400.    )
  401.    (vl-some
  402.       '(lambda ( tab / row col )
  403.            (if (= :vlax-true (vla-hittest tab pnt dir 'row 'col))
  404.                (list tab row col)
  405.            )
  406.        )
  407.        lst
  408.    )
  409. )
  410. ;; ObjectID  -  Lee Mac
  411. ;; Returns a string containing the ObjectID of a supplied VLA-Object
  412. ;; Compatible with 32-bit & 64-bit systems
  413. (defun LM:objectid ( obj )
  414.    (eval
  415.        (list 'defun 'LM:objectid '( obj )
  416.            (if (wcmatch (getenv "PROCESSOR_ARCHITECTURE") "*64*")
  417.                (if (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring)
  418.                    (list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false)
  419.                   '(LM:ename->objectid (vlax-vla-object->ename obj))
  420.                )
  421.               '(itoa (vla-get-objectid obj))
  422.            )
  423.        )
  424.    )
  425.    (LM:objectid obj)
  426. )
  427. ;; Entity Name to ObjectID  -  Lee Mac
  428. ;; Returns the 32-bit or 64-bit ObjectID for a supplied entity name
  429. (defun LM:ename->objectid ( ent )
  430.    (LM:hex->decstr
  431.        (setq ent (vl-string-right-trim ">" (vl-prin1-to-string ent))
  432.              ent (substr ent (+ (vl-string-position 58 ent) 3))
  433.        )
  434.    )
  435. )
  436. ;; Hex to Decimal String  -  Lee Mac
  437. ;; Returns the decimal representation of a supplied hexadecimal string
  438. (defun LM:hex->decstr ( hex / foo bar )
  439.    (defun foo ( lst rtn )
  440.        (if lst
  441.            (foo (cdr lst) (bar (- (car lst) (if (< 57 (car lst)) 55 48)) rtn))
  442.            (apply 'strcat (mapcar 'itoa (reverse rtn)))
  443.        )
  444.    )
  445.    (defun bar ( int lst )
  446.        (if lst
  447.            (if (or (< 0 (setq int (+ (* 16 (car lst)) int))) (cdr lst))
  448.                (cons (rem int 10) (bar (/ int 10) (cdr lst)))
  449.            )
  450.            (bar int '(0))
  451.        )
  452.    )
  453.    (foo (vl-string->list (strcase hex)) nil)
  454. )
  455. ;; Start Undo  -  Lee Mac
  456. ;; Opens an Undo Group.
  457. (defun LM:startundo ( doc )
  458.    (LM:endundo doc)
  459.    (vla-startundomark doc)
  460. )
  461. ;; End Undo  -  Lee Mac
  462. ;; Closes an Undo Group.
  463. (defun LM:endundo ( doc )
  464.    (while (= 8 (logand 8 (getvar 'undoctl)))
  465.        (vla-endundomark doc)
  466.    )
  467. )
  468. ;; Active Document  -  Lee Mac
  469. ;; Returns the VLA Active Document Object
  470. (defun LM:acdoc nil
  471.    (eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
  472.    (LM:acdoc)
  473. )
  474. ;;----------------------------------------------------------------------;;
  475. (vl-load-com)
  476. (princ
  477.    (strcat
  478.        "\n:: LengthField.lsp | Version 1.0 | \\U+00A9 Lee Mac "
  479.        (menucmd "m=$(edtime,0,yyyy)")
  480.        " www.lee-mac.com ::"
  481.        "\n:: Type "LF" to Invoke ::"
  482.    )
  483. )
  484. (princ)
  485. ;;----------------------------------------------------------------------;;
  486. ;;                             End of File                              ;;
  487. ;;----------------------------------------------------------------------;;
回复

使用道具 举报

2

主题

4

帖子

2

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-5 15:17:56 | 显示全部楼层
尝试查找代码numinc。它做你想要的。你找不到,给我一个信号。我会看着你的。
回复

使用道具 举报

5

主题

24

帖子

19

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-5 15:40:13 | 显示全部楼层
我发现它,非常好的Lisp程序,但不是我想要的。
我只想修改附加的lisp,而不是将“L=10 cm”修改为“L1=x cm”,将下一个修改为“L2=x cm”
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 15:43:29 | 显示全部楼层
只需应用与另一个lisp相同的代码更改,将它们并排打开并进行比较。
回复

使用道具 举报

5

主题

24

帖子

19

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-5 16:01:34 | 显示全部楼层
我试过了,我得到了不同的结果,我不知道为什么
回复

使用道具 举报

5

主题

24

帖子

19

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-5 16:16:37 | 显示全部楼层
这就是问题所在:
仍然不工作
  1. (setq mynumber 0)
  2. (defun increment (str / )
  3. (setq mynumber (+ mynumber 1))
  4. (vl-string-subst (itoa mynumber) "*" str)
  5. )
  6. ;;-------------------------=={ Length Field }==-------------------------;;
  7. ;;                                                                      ;;
  8. ;;  This program allows a user to generate a field expression           ;;
  9. ;;  referencing the length/perimeter/circumference of one or more       ;;
  10. ;;  selected objects. In the case of selecting multiple objects, the    ;;
  11. ;;  field expression will reference the sum of the lengths of all       ;;
  12. ;;  objects in the selection.                                           ;;
  13. ;;                                                                      ;;
  14. ;;  The user may opt to specify a point at which to create a new        ;;
  15. ;;  multiline text object housing the field expression, pick a table    ;;
  16. ;;  cell in which the field should be inserted, or select an existing   ;;
  17. ;;  single-line text, multiline text, multileader, or attribute to      ;;
  18. ;;  be populated with the field expression.                             ;;
  19. ;;                                                                      ;;
  20. ;;  Upon issuing the command syntax 'LF' (Length Field) at the AutoCAD  ;;
  21. ;;  command-line, the program first prompts the user to make a          ;;
  22. ;;  selection of objects for which to return the length summation.      ;;
  23. ;;                                                                      ;;
  24. ;;  At this prompt, the user may select any number of Arcs, Circles,    ;;
  25. ;;  Lines, 2D Polylines (light or heavy), or 3D Polylines.              ;;
  26. ;;                                                                      ;;
  27. ;;  The user is then prompted to specify a point or table cell to       ;;
  28. ;;  insert a field expression referencing the summation of the lengths  ;;
  29. ;;  of the selected objects.                                            ;;
  30. ;;                                                                      ;;
  31. ;;  At this prompt, the user may also choose the 'Object' option in     ;;
  32. ;;  order to populate the content of an existing annotation object      ;;
  33. ;;  with the field expression.                                          ;;
  34. ;;                                                                      ;;
  35. ;;  Upon choosing this option, the user may select any single-line      ;;
  36. ;;  text (DText), multiline text (MText), single-line or multiline      ;;
  37. ;;  attribute, attributed block, or multileader (MLeader) with either   ;;
  38. ;;  multiline text or attributed block content.                         ;;
  39. ;;                                                                      ;;
  40. ;;  If the user selects an attributed block or attributed multileader   ;;
  41. ;;  with more than one attribute, the user is presented with a dialog   ;;
  42. ;;  interface listing the available attributes, and is prompted to      ;;
  43. ;;  select a destination for the field expression.                      ;;
  44. ;;                                                                      ;;
  45. ;;  The user may optionally predefine the target block/multileader      ;;
  46. ;;  attribute by specifying the attribute tag where noted at the top    ;;
  47. ;;  of the program source code.                                         ;;
  48. ;;                                                                      ;;
  49. ;;  The resulting field expression will display the sum of the lengths  ;;
  50. ;;  of the selected objects, formatted using the field formatting code  ;;
  51. ;;  specified at the top of the program.                                ;;
  52. ;;                                                                      ;;
  53. ;;----------------------------------------------------------------------;;
  54. ;;  Author:  Lee Mac, Copyright © 2017  -  www.lee-mac.com              ;;
  55. ;;----------------------------------------------------------------------;;
  56. ;;  Version 1.0    -    2017-08-06                                      ;;
  57. ;;                                                                      ;;
  58. ;;  - First release.                                                    ;;
  59. ;;----------------------------------------------------------------------;;
  60. ;;  Version 1.1    -    2017-08-06                                      ;;
  61. ;;                                                                      ;;
  62. ;;  - Program modified to account for selection of existing annotation  ;;
  63. ;;    objects which already contain a field expression.                 ;;
  64. ;;----------------------------------------------------------------------;;
  65. (defun c:lf ( / *error* ent enx flg fmt idx lst obj oid prp sel str tab tag tmp typ )
  66.    (setq fmt "L=%lu6%pr2 cm" ;; Field Formatting
  67.    (setq fmt (increment fmt))
  68.          tag nil    ;; Optional predefined attribute tag
  69.    )
  70.    
  71.    (defun *error* ( msg )
  72.        (LM:endundo (LM:acdoc))
  73.        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
  74.            (princ (strcat "\nError: " msg))
  75.        )
  76.        (princ)
  77.    )
  78.    (LM:startundo (LM:acdoc))
  79.    (setq prp
  80.       '(
  81.            ("AcDbArc"        . "ArcLength")
  82.            ("AcDbCircle"     . "Circumference")
  83.            ("AcDbLine"       . "Length")
  84.            ("AcDbPolyline"   . "Length")
  85.            ("AcDb2dPolyline" . "Length")
  86.            ("AcDb3dPolyline" . "Length")
  87.        )
  88.    )
  89.    (if
  90.        (setq sel
  91.            (LM:ssget "\nSelect objects to obtain total length <exit>: "
  92.                (list
  93.                    (list
  94.                       '(000 . "ARC,CIRCLE,LINE,*POLYLINE")
  95.                       '(-04 . "<NOT")
  96.                           '(-04 . "<AND")
  97.                               '(000 . "POLYLINE") '(-04 . "&") '(070 . 80)
  98.                           '(-04 . "AND>")
  99.                       '(-04 . "NOT>")
  100.                        (if (= 1 (getvar 'cvport))
  101.                            (cons 410 (getvar 'ctab))
  102.                           '(410 . "Model")
  103.                        )
  104.                    )
  105.                )
  106.            )
  107.        )
  108.        (progn
  109.            (if (= 1 (sslength sel))
  110.                (setq obj (vlax-ename->vla-object (ssname sel 0))
  111.                      str
  112.                    (strcat
  113.                        "%<\\AcObjProp Object(%<\\_ObjId "
  114.                        (LM:objectid obj)
  115.                        ">%)." (cdr (assoc (vla-get-objectname obj) prp)) " \\f "" fmt "">%"
  116.                    )
  117.                )
  118.                (progn
  119.                    (repeat (setq idx (sslength sel))
  120.                        (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx))))
  121.                              lst
  122.                            (vl-list*
  123.                                "%<\\AcObjProp Object(%<\\_ObjId "
  124.                                (LM:objectid obj)
  125.                                ">%)." (cdr (assoc (vla-get-objectname obj) prp)) ">%" " + "
  126.                                lst
  127.                            )
  128.                        )
  129.                    )
  130.                    (setq str
  131.                        (strcat
  132.                            "%<\\AcExpr "
  133.                            (apply 'strcat (reverse (cdr (reverse lst))))
  134.                            " \\f "" fmt "">%"
  135.                        )
  136.                    )
  137.                )
  138.            )
  139.            (if
  140.                (setq tmp
  141.                    (ssget "_X"
  142.                        (list '(0 . "ACAD_TABLE")
  143.                            (if (= 1 (getvar 'cvport))
  144.                                (cons 410 (getvar 'ctab))
  145.                               '(410 . "Model")
  146.                            )
  147.                        )
  148.                    )
  149.                )
  150.                (repeat (setq idx (sslength tmp))
  151.                    (setq tab (cons (vlax-ename->vla-object (ssname tmp (setq idx (1- idx)))) tab))
  152.                )
  153.            )
  154.            (while
  155.                (not
  156.                    (progn
  157.                        (if flg
  158.                            (progn
  159.                                (setvar 'errno 0)
  160.                                (initget "Point eXit")
  161.                                (setq sel (nentsel "\nSelect text, mtext, mleader, attribute or attributed block [Point/eXit] <eXit>: "))
  162.                            )
  163.                            (progn
  164.                                (initget "Object eXit")
  165.                                (setq sel (getpoint "\nSpecify point or cell for field [Object/eXit] <eXit>: "))
  166.                            )
  167.                        )
  168.                        (cond
  169.                            (   (= 7 (getvar 'errno))
  170.                                (prompt "\nMissed, try again.")
  171.                            )
  172.                            (   (or (null sel) (= "eXit" sel)))
  173.                            (   (= "Point" sel)
  174.                                (setq flg nil)
  175.                            )
  176.                            (   (= "Object" sel)
  177.                                (not (setq flg t))
  178.                            )
  179.                            (   flg
  180.                                (setq ent (car sel)
  181.                                      enx (entget ent)
  182.                                      typ (cdr (assoc 0 enx))
  183.                                      obj (vlax-ename->vla-object ent)
  184.                                )
  185.                                (cond
  186.                                    (   (and (= 2 (length sel)) (wcmatch typ "TEXT,MTEXT"))
  187.                                        (if (vlax-write-enabled-p obj)
  188.                                            (LF:puttextstring obj str)
  189.                                            (prompt "\nThe selected text object is on a locked layer.")
  190.                                        )
  191.                                    )
  192.                                    (   (and (= "ATTRIB" typ)
  193.                                             (/= 'str (type tag))
  194.                                        )
  195.                                        (if (vlax-write-enabled-p obj)
  196.                                            (progn
  197.                                                (LF:puttextstring obj str)
  198.                                                (LF:updatefield ent)
  199.                                            )
  200.                                            (prompt "\nThe selected attribute is on a locked layer.")
  201.                                        )
  202.                                    )
  203.                                    (   (and
  204.                                            (or
  205.                                                (and (= "ATTRIB" typ)
  206.                                                     (setq tmp (cdr (assoc 330 enx)))
  207.                                                )
  208.                                                (and (setq tmp (last (cadddr sel)))
  209.                                                     (= "INSERT" (cdr (assoc 0 (entget tmp))))
  210.                                                )
  211.                                            )
  212.                                            (setq tmp (vlax-invoke (vlax-ename->vla-object tmp) 'getattributes))
  213.                                            (or
  214.                                                (and (= 'str (type tag))
  215.                                                     (setq idx (vl-position (strcase tag) (mapcar 'vla-get-tagstring tmp)))
  216.                                                     (setq obj (nth idx tmp))
  217.                                                )
  218.                                                (and (not (cdr tmp))
  219.                                                     (setq obj (car tmp))
  220.                                                )
  221.                                                (and (setq idx (LM:listbox "Choose Attribute" (mapcar 'vla-get-tagstring tmp) 2))
  222.                                                     (setq obj (nth (car idx) tmp))
  223.                                                )
  224.                                            )
  225.                                        )
  226.                                        (if (vlax-write-enabled-p obj)
  227.                                            (progn
  228.                                                (LF:puttextstring obj str)
  229.                                                (LF:updatefield (vlax-vla-object->ename obj))
  230.                                            )
  231.                                            (prompt "\nThe selected attribute is on a locked layer.")
  232.                                        )
  233.                                    )
  234.                                    (   (and (= 2 (length sel)) (= "MULTILEADER" typ))
  235.                                        (setq typ (cdr (assoc 172 (reverse enx))))
  236.                                        (cond
  237.                                            (   (and (<= acblockcontent typ acmtextcontent) (not (vlax-write-enabled-p obj)))
  238.                                                (prompt "\nThe selected multileader is on a locked layer.")
  239.                                            )
  240.                                            (   (= acmtextcontent typ)
  241.                                                (LF:puttextstring obj str)
  242.                                                (vla-regen (LM:acdoc) acactiveviewport)
  243.                                                t
  244.                                            )
  245.                                            (   (and
  246.                                                    (= acblockcontent typ)
  247.                                                    (setq tmp (LM:getmleaderattributes obj))
  248.                                                    (or
  249.                                                        (and (= 'str (type tag))
  250.                                                             (setq oid (cdr (assoc (strcase tag) tmp)))
  251.                                                        )
  252.                                                        (and (not (cdr tmp))
  253.                                                             (setq oid (cdar tmp))
  254.                                                        )
  255.                                                        (and (setq idx (LM:listbox "Choose Attribute" (mapcar 'car tmp) 2))
  256.                                                             (setq oid (cdr (nth (car idx) tmp)))
  257.                                                        )
  258.                                                    )
  259.                                                )
  260.                                                (LM:setmleaderattributevalue obj oid str)
  261.                                                (vla-regen (LM:acdoc) acactiveviewport)
  262.                                                t
  263.                                            )
  264.                                            (   (prompt "\nThe select multileader has no editable content."))
  265.                                        )
  266.                                    )
  267.                                    (   (prompt "\nThe selected object is not text, mtext, multileader, attribute or attributed block."))
  268.                                )
  269.                            )
  270.                            (   (setq tmp (LM:getcell tab (trans sel 1 0)))
  271.                                (if (vlax-write-enabled-p (car tmp))
  272.                                    (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-settext (append tmp (list str)))))
  273.                                    (prompt "\nThe selected table cell belongs to a table on a locked layer.")
  274.                                )
  275.                            )
  276.                            (   (vla-addmtext
  277.                                    (vlax-get-property (LM:acdoc) (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))
  278.                                    (vlax-3D-point (trans sel 1 0))
  279.                                    0.0
  280.                                    str
  281.                                )
  282.                            )
  283.                        )
  284.                    )
  285.                )
  286.            )
  287.        )
  288.    )
  289.    (*error* nil) (princ)
  290. )
  291. (defun LF:puttextstring ( obj str )
  292.    (vla-put-textstring obj "") ;; To clear any existing field
  293.    (vla-put-textstring obj str)
  294.    t
  295. )
  296. (defun LF:updatefield ( ent / cmd rtn )
  297.    (setq cmd (getvar 'cmdecho))
  298.    (setvar 'cmdecho 0)
  299.    (setq rtn (vl-cmdf "_.updatefield" ent ""))
  300.    (setvar 'cmdecho cmd)
  301.    rtn
  302. )
  303. ;; ssget  -  Lee Mac
  304. ;; A wrapper for the ssget function to permit the use of a custom selection prompt
  305. ;; msg - [str] selection prompt
  306. ;; arg - [lst] list of ssget arguments
  307. (defun LM:ssget ( msg arg / sel )
  308.    (princ msg)
  309.    (setvar 'nomutt 1)
  310.    (setq sel (vl-catch-all-apply 'ssget arg))
  311.    (setvar 'nomutt 0)
  312.    (if (not (vl-catch-all-error-p sel)) sel)
  313. )
  314. ;; Get MLeader Attributes  -  Lee Mac
  315. ;; Returns an association list of attribute tags & object IDs for all attributes held by an mleader block
  316. ;; mld - [vla] MLeader vla-object
  317. ;; Returns: [lst] List of ((<Attribute Tag> . <Object ID>) ... )
  318. (defun LM:getmleaderattributes ( mld / rtn )
  319.    (vlax-for obj (vla-item (vla-get-blocks (vla-get-document mld)) (vla-get-contentblockname mld))
  320.        (if
  321.            (and
  322.                (= "AcDbAttributeDefinition" (vla-get-objectname obj))
  323.                (= :vlax-false (vla-get-constant obj))
  324.            )
  325.            (setq rtn (cons (cons (strcase (vla-get-tagstring obj)) (LM:intobjectid obj)) rtn))
  326.        )
  327.    )
  328.    (reverse rtn)
  329. )
  330. ;; Object ID (integer)  -  Lee Mac
  331. ;; Returns an integer representing the ObjectID of a supplied VLA-Object
  332. ;; Compatible with 32-bit & 64-bit systems
  333. (defun LM:intobjectid ( obj )
  334.    (if (vlax-property-available-p obj 'objectid32)
  335.        (defun LM:intobjectid ( obj ) (vla-get-objectid32 obj))
  336.        (defun LM:intobjectid ( obj ) (vla-get-objectid   obj))
  337.    )
  338.    (LM:intobjectid obj)
  339. )
  340. ;; Set MLeader Attribute Value  -  Lee Mac
  341. ;; obj - [vla] MLeader vla-object
  342. ;; idx - [int] Attribute Definition Object ID
  343. ;; str - [str] Attribute value
  344. (defun LM:setmleaderattributevalue ( obj idx str )
  345.    (if (vlax-method-applicable-p obj 'setblockattributevalue32)
  346.        (defun LM:setmleaderattributevalue ( obj idx str ) (vla-setblockattributevalue32 obj idx str))
  347.        (defun LM:setmleaderattributevalue ( obj idx str ) (vla-setblockattributevalue   obj idx str))
  348.    )
  349.    (LM:setmleaderattributevalue obj idx str)
  350. )
  351. ;; List Box  -  Lee Mac
  352. ;; Displays a DCL list box allowing the user to make a selection from the supplied data.
  353. ;; msg - [str] Dialog label
  354. ;; lst - [lst] List of strings to display
  355. ;; bit - [int] 1=allow multiple; 2=return indexes
  356. ;; Returns: [lst] List of selected items/indexes, else nil
  357. (defun LM:listbox ( msg lst bit / dch des tmp rtn )
  358.    (cond
  359.        (   (not
  360.                (and
  361.                    (setq tmp (vl-filename-mktemp nil nil ".dcl"))
  362.                    (setq des (open tmp "w"))
  363.                    (write-line
  364.                        (strcat "listbox:dialog{label="" msg "";spacer;:list_box{key="list";multiple_select="
  365.                            (if (= 1 (logand 1 bit)) "true" "false") ";width=50;height=15;}spacer;ok_cancel;}"
  366.                        )
  367.                        des
  368.                    )
  369.                    (not (close des))
  370.                    (< 0 (setq dch (load_dialog tmp)))
  371.                    (new_dialog "listbox" dch)
  372.                )
  373.            )
  374.            (prompt "\nError Loading List Box Dialog.")
  375.        )
  376.        (   t     
  377.            (start_list "list")
  378.            (foreach itm lst (add_list itm))
  379.            (end_list)
  380.            (setq rtn (set_tile "list" "0"))
  381.            (action_tile "list" "(setq rtn $value)")
  382.            (setq rtn
  383.                (if (= 1 (start_dialog))
  384.                    (if (= 2 (logand 2 bit))
  385.                        (read (strcat "(" rtn ")"))
  386.                        (mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
  387.                    )
  388.                )
  389.            )
  390.        )
  391.    )
  392.    (if (< 0 dch)
  393.        (unload_dialog dch)
  394.    )
  395.    (if (and tmp (setq tmp (findfile tmp)))
  396.        (vl-file-delete tmp)
  397.    )
  398.    rtn
  399. )
  400. ;; Get Cell  -  Lee Mac
  401. ;; If the supplied point lies within a cell boundary,
  402. ;; returns a list of: (<VLA Table Object> <Row> <Col>)
  403. (defun LM:getcell ( lst pnt / dir )
  404.    (setq dir (vlax-3D-point (trans (getvar 'viewdir) 1 0))
  405.          pnt (vlax-3D-point pnt)
  406.    )
  407.    (vl-some
  408.       '(lambda ( tab / row col )
  409.            (if (= :vlax-true (vla-hittest tab pnt dir 'row 'col))
  410.                (list tab row col)
  411.            )
  412.        )
  413.        lst
  414.    )
  415. )
  416. ;; ObjectID  -  Lee Mac
  417. ;; Returns a string containing the ObjectID of a supplied VLA-Object
  418. ;; Compatible with 32-bit & 64-bit systems
  419. (defun LM:objectid ( obj )
  420.    (eval
  421.        (list 'defun 'LM:objectid '( obj )
  422.            (if (wcmatch (getenv "PROCESSOR_ARCHITECTURE") "*64*")
  423.                (if (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring)
  424.                    (list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false)
  425.                   '(LM:ename->objectid (vlax-vla-object->ename obj))
  426.                )
  427.               '(itoa (vla-get-objectid obj))
  428.            )
  429.        )
  430.    )
  431.    (LM:objectid obj)
  432. )
  433. ;; Entity Name to ObjectID  -  Lee Mac
  434. ;; Returns the 32-bit or 64-bit ObjectID for a supplied entity name
  435. (defun LM:ename->objectid ( ent )
  436.    (LM:hex->decstr
  437.        (setq ent (vl-string-right-trim ">" (vl-prin1-to-string ent))
  438.              ent (substr ent (+ (vl-string-position 58 ent) 3))
  439.        )
  440.    )
  441. )
  442. ;; Hex to Decimal String  -  Lee Mac
  443. ;; Returns the decimal representation of a supplied hexadecimal string
  444. (defun LM:hex->decstr ( hex / foo bar )
  445.    (defun foo ( lst rtn )
  446.        (if lst
  447.            (foo (cdr lst) (bar (- (car lst) (if (< 57 (car lst)) 55 48)) rtn))
  448.            (apply 'strcat (mapcar 'itoa (reverse rtn)))
  449.        )
  450.    )
  451.    (defun bar ( int lst )
  452.        (if lst
  453.            (if (or (< 0 (setq int (+ (* 16 (car lst)) int))) (cdr lst))
  454.                (cons (rem int 10) (bar (/ int 10) (cdr lst)))
  455.            )
  456.            (bar int '(0))
  457.        )
  458.    )
  459.    (foo (vl-string->list (strcase hex)) nil)
  460. )
  461. ;; Start Undo  -  Lee Mac
  462. ;; Opens an Undo Group.
  463. (defun LM:startundo ( doc )
  464.    (LM:endundo doc)
  465.    (vla-startundomark doc)
  466. )
  467. ;; End Undo  -  Lee Mac
  468. ;; Closes an Undo Group.
  469. (defun LM:endundo ( doc )
  470.    (while (= 8 (logand 8 (getvar 'undoctl)))
  471.        (vla-endundomark doc)
  472.    )
  473. )
  474. ;; Active Document  -  Lee Mac
  475. ;; Returns the VLA Active Document Object
  476. (defun LM:acdoc nil
  477.    (eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
  478.    (LM:acdoc)
  479. )
  480. ;;----------------------------------------------------------------------;;
  481. (vl-load-com)
  482. (princ
  483.    (strcat
  484.        "\n:: LengthField.lsp | Version 1.0 | \\U+00A9 Lee Mac "
  485.        (menucmd "m=$(edtime,0,yyyy)")
  486.        " www.lee-mac.com ::"
  487.        "\n:: Type "LF" to Invoke ::"
  488.    )
  489. )
  490. (princ)
  491. ;;----------------------------------------------------------------------;;
  492. ;;                             End of File                              ;;
  493. ;;----------------------------------------------------------------------;;
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-15 00:51 , Processed in 1.424625 second(s), 64 queries .

© 2020-2025 乐筑天下

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