乐筑天下

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

[编程交流] 更改说明符的值

[复制链接]

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:25:00 | 显示全部楼层
以下是另一种可能的解决方案:
  1. (defun c:test ( / *error* dim fun lst rgx str tag )
  2.    (setq tag "dimensions") ;; Tag to update
  3.    (defun *error* ( msg )
  4.        (if (= 'vla-object (type rgx)) (vlax-release-object rgx))
  5.        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
  6.            (princ (strcat "\nError: " msg))
  7.        )
  8.        (princ)
  9.    )
  10.    
  11.    (defun sel ( msg prd / ent ) (setq prd (eval prd))
  12.        (while
  13.            (progn (setvar 'errno 0) (setq ent (car (entsel msg)))
  14.                (cond
  15.                    (   (= 7 (getvar 'errno))
  16.                        (princ "\nMissed, try again.")
  17.                    )
  18.                    (   (null ent) nil)
  19.                    (   (null (prd ent)))
  20.                )
  21.            )
  22.        )
  23.        ent
  24.    )
  25.    (setq fun '(lambda ( x ) (or (wcmatch (cdr (assoc 0 (entget x))) "*DIMENSION") (prompt "\nInvalid object selected.")))
  26.          dim  (sel "\nSelect first dimension: " fun)
  27.    )
  28.    (cond
  29.        (   (not dim))
  30.        (   (or (null (setq rgx (vl-catch-all-apply 'vlax-get-or-create-object '("vbscript.regexp"))))
  31.                (vl-catch-all-error-p rgx)
  32.            )
  33.            (princ "\nUnable to interface with RegEx Object.")
  34.        )
  35.        (   t
  36.            (setq lst (cons (LM:getdimstring dim) lst))
  37.            (while (setq dim (sel "\nSelect next dimension <done>: " fun))
  38.                (setq lst (cons (LM:getdimstring dim) lst))
  39.            )
  40.            (setq str
  41.                (apply 'strcat
  42.                    (cdr
  43.                        (apply 'append
  44.                            (mapcar '(lambda ( x ) (list "*" (LM:quickunformat rgx x)))
  45.                                (reverse lst)
  46.                            )
  47.                        )
  48.                    )
  49.                )
  50.            )
  51.            (sel "\nSelect block: "
  52.               '(lambda ( e / x )
  53.                    (cond
  54.                        (   (not
  55.                                (and
  56.                                    (setq x (entget e))
  57.                                    (= "INSERT" (cdr (assoc 0 x)))
  58.                                    (= 1 (cdr (assoc 66 x)))
  59.                                )
  60.                            )
  61.                            (prompt "\nSelected object is not an attributed block.")
  62.                        )
  63.                        (   (LM:setattributevalue e tag str))
  64.                        (   (prompt (strcat "\nSelected block does not contain the tag "" tag "".")))
  65.                    )
  66.                )
  67.            )
  68.        )
  69.    )
  70.    (*error* nil)
  71.    (princ)
  72. )
  73. ;; Get Dimension String  -  Lee Mac
  74. ;; Returns the displayed content of a dimension
  75. (defun LM:getdimstring ( ent / enx rtn )
  76.    (if
  77.        (and
  78.            (setq enx (entget ent))
  79.            (wcmatch (cdr (assoc 0 enx)) "*DIMENSION")
  80.            (setq ent (tblobjname "block" (cdr (assoc 2 enx))))
  81.            (setq ent (entnext ent)
  82.                  enx (entget  ent)
  83.            )
  84.        )
  85.        (while (and ent (null rtn))
  86.            (if (= "MTEXT" (cdr (assoc 0 enx)))
  87.                (setq rtn  (cdr (assoc 1 enx)))
  88.            )
  89.            (setq ent (entnext ent)
  90.                  enx (entget  ent)
  91.            )
  92.        )
  93.    )
  94.    rtn
  95. )
  96. ;; Quick Unformat  -  Lee Mac
  97. ;; Returns a string with all MText formatting codes removed.
  98. ;; rgx - [vla] Regular Expressions (RegExp) Object
  99. ;; str - [str] String to process
  100. (defun LM:quickunformat ( rgx str )
  101.    (if
  102.        (null
  103.            (vl-catch-all-error-p
  104.                (setq str
  105.                    (vl-catch-all-apply
  106.                       '(lambda nil
  107.                            (foreach pair
  108.                               '(
  109.                                    ("\032"     . "\\\\\\\")
  110.                                    (" "        . "\\\\P|\\n|\\t")
  111.                                    ("$1"       . "\\\\(\\\\[ACcFfHKkLlOopQTW])|\\\\[ACcFfHKkLlOopQTW][^\\\\;]*;|\\\\[ACcFfKkHLlOopQTW]")
  112.                                    ("$1$2/$3"  . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
  113.                                    ("$1$2"     . "\\\\(\\\\S)|[\\\\](})|}")
  114.                                    ("$1"       . "[\\\\]({)|{")
  115.                                    ("\\$1$2$3" . "(\\\\[ACcFfHKkLlOoPpQSTW])|({)|(})")
  116.                                    ("\\\"     . "\032")
  117.                                )
  118.                                (vlax-put-property rgx 'pattern (cdr pair))
  119.                                (setq str (vlax-invoke rgx 'replace str (car pair)))
  120.                            )
  121.                        )
  122.                    )
  123.                )
  124.            )
  125.        )
  126.        str
  127.    )
  128. )
  129. ;; Set Attribute Value  -  Lee Mac
  130. ;; Sets the value of the first attribute with the given tag found within the block, if present.
  131. ;; blk - [ent] Block (Insert) Entity Name
  132. ;; tag - [str] Attribute TagString
  133. ;; val - [str] Attribute Value
  134. ;; Returns: [str] Attribute value if successful, else nil.
  135. (defun LM:setattributevalue ( blk tag val / enx )
  136.    (if (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))
  137.        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
  138.            (if (entmod (subst (cons 1 val) (assoc 1 enx) enx))
  139.                (progn
  140.                    (entupd blk)
  141.                    val
  142.                )
  143.            )
  144.            (LM:setattributevalue blk tag val)
  145.        )
  146.    )
  147. )
  148. (vl-load-com) (princ)
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 20:27:07 | 显示全部楼层
Lee和往常一样棒,但我认为你错过了一件事,OP希望能够选择一个dim或输入一个手动值,你的代码要求选择一个dim,并给出信息,这不是dim,而不是我倾向于选择dim的概念,或者说按enter键输入一个值。我没有在之前的帖子中明确地问到你如何在没有额外步骤的情况下选择dim或value,我认为这是不可能的
 
尺寸值尺寸=123.45*弗雷德*456.78
回复

使用道具 举报

5

主题

31

帖子

26

银币

初来乍到

Rank: 1

铜币
26
发表于 2022-7-5 20:33:07 | 显示全部楼层
 
谢谢你,塔瓦先生。
也许我的解释不清楚。
以下李代码是正确的。但仍然是一个缺陷。
 
无法手动输入值。只能通过“维度”获取。
回复

使用道具 举报

5

主题

31

帖子

26

银币

初来乍到

Rank: 1

铜币
26
发表于 2022-7-5 20:34:03 | 显示全部楼层
 
谢谢你,李,非常好。
 
有可能这样做吗?
 
选择第一个维度:
选择下一个维度:
选择下一个维度:
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 20:37:01 | 显示全部楼层
不幸的是,实际工作遇到了阻碍,这段代码有一个问题,我们会尽快找到时间解决,但方法是存在的。
 
  1. (vl-load-com)
  2. (defun pullapart ()
  3. (setq val (rtos (vla-get-Measurement (vlax-ename->vla-object(car obj)))2 2))
  4. )
  5. (defun pickobj ()
  6. (setq obj (entsel "\nPick a dim - Enter for value - Double Enter to exit"))
  7. (if (= obj nil)
  8. (setq Val (getstring "\nEnter Value"))
  9. (pullapart) ; a defun that checks for a "DIM" and returns val=measurement
  10. ) ; if
  11. ) ; defun
  12. (setq y 1)
  13. (setq ss1 (car (entsel "\nSelect block")))
  14. (setq bname (vla-get-name(vlax-ename->vla-object SS1)))
  15. (setq x (getint "\nEnter attribute position within block as a Number "))
  16. (pickobj) ; need at least one value
  17. (setq newstrblank val) ; dummy value
  18. (while (=/ val nil)
  19. (SETQ newstrblank (strcat newstrblank "*" val)) ;
  20. (pickobj)
  21. )
  22. (foreach att (vlax-invoke (vlax-ename->vla-object SS1) 'getattributes)
  23.    (if (= y x)
  24.    (progn
  25.    (setq newstr (vla-get-textstring att ))
  26.    (vla-put-textstring att newstrblank)
  27.    )
  28.    )
  29.    (setq y (+ Y 1))
  30. )
  31. (princ)
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:40:15 | 显示全部楼层
 
 
 
谢谢大家
 
以下是一种可能的解决方案,允许在同一提示下进行对象选择和任意输入:
  1. (defun c:test ( / *error* dim lst rgx str tag )
  2.    (setq tag "dimensions") ;; Tag to update
  3.    (defun *error* ( msg )
  4.        (if (= 'vla-object (type rgx)) (vlax-release-object rgx))
  5.        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
  6.            (princ (strcat "\nError: " msg))
  7.        )
  8.        (princ)
  9.    )
  10.    
  11.    (defun sel ( msg prd / ent ) (setq prd (eval prd))
  12.        (while
  13.            (progn (setvar 'errno 0) (setq ent (car (entsel msg)))
  14.                (cond
  15.                    (   (= 7 (getvar 'errno))
  16.                        (princ "\nMissed, try again.")
  17.                    )
  18.                    (   (null ent) nil)
  19.                    (   (null (prd ent)))
  20.                )
  21.            )
  22.        )
  23.        ent
  24.    )
  25.    (cond
  26.        (   (= "" (setq dim (LM:select-or-text "\nSelect first dimension or enter value: " '((0 . "*DIMENSION"))))))
  27.        (   (or (null (setq rgx (vl-catch-all-apply 'vlax-get-or-create-object '("vbscript.regexp"))))
  28.                (vl-catch-all-error-p rgx)
  29.            )
  30.            (princ "\nUnable to interface with RegEx Object.")
  31.        )
  32.        (   t
  33.            (setq lst (cons (if (= 'str (type dim)) dim (LM:getdimstring dim)) lst))
  34.            (while (/= "" (setq dim (LM:select-or-text "\nSelect next dimension or enter value <done>: " '((0 . "*DIMENSION")))))
  35.                (setq lst (cons (if (= 'str (type dim)) dim (LM:getdimstring dim)) lst))
  36.            )
  37.            (setq str
  38.                (apply 'strcat
  39.                    (cdr
  40.                        (apply 'append
  41.                            (mapcar '(lambda ( x ) (list "*" (LM:quickunformat rgx x)))
  42.                                (reverse lst)
  43.                            )
  44.                        )
  45.                    )
  46.                )
  47.            )
  48.            (sel "\nSelect block: "
  49.               '(lambda ( e / x )
  50.                    (cond
  51.                        (   (not
  52.                                (and
  53.                                    (setq x (entget e))
  54.                                    (= "INSERT" (cdr (assoc 0 x)))
  55.                                    (= 1 (cdr (assoc 66 x)))
  56.                                )
  57.                            )
  58.                            (prompt "\nSelected object is not an attributed block.")
  59.                        )
  60.                        (   (LM:setattributevalue e tag str))
  61.                        (   (prompt (strcat "\nSelected block does not contain the tag "" tag "".")))
  62.                    )
  63.                )
  64.            )
  65.        )
  66.    )
  67.    (*error* nil)
  68.    (princ)
  69. )
  70. ;; Get Dimension String  -  Lee Mac
  71. ;; Returns the displayed content of a dimension
  72. (defun LM:getdimstring ( ent / enx rtn )
  73.    (if
  74.        (and
  75.            (setq enx (entget ent))
  76.            (wcmatch (cdr (assoc 0 enx)) "*DIMENSION")
  77.            (setq ent (tblobjname "block" (cdr (assoc 2 enx))))
  78.            (setq ent (entnext ent)
  79.                  enx (entget  ent)
  80.            )
  81.        )
  82.        (while (and ent (null rtn))
  83.            (if (= "MTEXT" (cdr (assoc 0 enx)))
  84.                (setq rtn  (cdr (assoc 1 enx)))
  85.            )
  86.            (setq ent (entnext ent)
  87.                  enx (entget  ent)
  88.            )
  89.        )
  90.    )
  91.    rtn
  92. )
  93. ;; Quick Unformat  -  Lee Mac
  94. ;; Returns a string with all MText formatting codes removed.
  95. ;; rgx - [vla] Regular Expressions (RegExp) Object
  96. ;; str - [str] String to process
  97. (defun LM:quickunformat ( rgx str )
  98.    (if
  99.        (null
  100.            (vl-catch-all-error-p
  101.                (setq str
  102.                    (vl-catch-all-apply
  103.                       '(lambda nil
  104.                            (foreach pair
  105.                               '(
  106.                                    ("\032"     . "\\\\\\\")
  107.                                    (" "        . "\\\\P|\\n|\\t")
  108.                                    ("$1"       . "\\\\(\\\\[ACcFfHKkLlOopQTW])|\\\\[ACcFfHKkLlOopQTW][^\\\\;]*;|\\\\[ACcFfKkHLlOopQTW]")
  109.                                    ("$1$2/$3"  . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
  110.                                    ("$1$2"     . "\\\\(\\\\S)|[\\\\](})|}")
  111.                                    ("$1"       . "[\\\\]({)|{")
  112.                                    ("\\$1$2$3" . "(\\\\[ACcFfHKkLlOoPpQSTW])|({)|(})")
  113.                                    ("\\\"     . "\032")
  114.                                )
  115.                                (vlax-put-property rgx 'pattern (cdr pair))
  116.                                (setq str (vlax-invoke rgx 'replace str (car pair)))
  117.                            )
  118.                        )
  119.                    )
  120.                )
  121.            )
  122.        )
  123.        str
  124.    )
  125. )
  126. ;; Set Attribute Value  -  Lee Mac
  127. ;; Sets the value of the first attribute with the given tag found within the block, if present.
  128. ;; blk - [ent] Block (Insert) Entity Name
  129. ;; tag - [str] Attribute TagString
  130. ;; val - [str] Attribute Value
  131. ;; Returns: [str] Attribute value if successful, else nil.
  132. (defun LM:setattributevalue ( blk tag val / enx )
  133.    (if (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))
  134.        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
  135.            (if (entmod (subst (cons 1 val) (assoc 1 enx) enx))
  136.                (progn
  137.                    (entupd blk)
  138.                    val
  139.                )
  140.            )
  141.            (LM:setattributevalue blk tag val)
  142.        )
  143.    )
  144. )
  145. ;; Selection or Text  -  Lee Mac
  146. ;; Prompts the user to select an object or enter an arbitrary string.
  147. ;; msg - [str] [Optional] Prompt string
  148. ;; ftr - [lst] [Optional] ssget filter list
  149. ;; Returns: [ent/str] Entity name of selected entity or entered string; "" if enter is pressed.
  150. (defun LM:select-or-text ( msg ftr / gr1 gr2 rtn sel )
  151.    (setq msg (princ (cond (msg) ("\nSelect object: ")))
  152.          rtn ""
  153.    )
  154.    (while
  155.        (progn
  156.            (setq gr1 (grread nil 14 2)
  157.                  gr2 (cadr gr1)
  158.                  gr1 (car  gr1)
  159.            )
  160.            (cond
  161.                (   (= 3 gr1)
  162.                    (if (ssget gr2) ;; nentselp is slow for xrefs
  163.                        (if (setq sel (ssget gr2 ftr))
  164.                            (progn (setq rtn (ssname sel 0)) nil)
  165.                            (princ (strcat "\nInvalid object selected." msg))
  166.                        )
  167.                        (princ (strcat "\nMissed, try again." msg))
  168.                    )
  169.                )
  170.                (   (= 2 gr1)
  171.                    (cond
  172.                        (   (< 31 gr2 127)
  173.                            (setq rtn (strcat rtn (princ (chr gr2))))
  174.                        )
  175.                        (   (= 13 gr2)
  176.                            nil
  177.                        )
  178.                        (   (and (= 8 gr2) (< 0 (strlen rtn)))
  179.                            (setq rtn (substr rtn 1 (1- (strlen rtn))))
  180.                            (princ "\010 \010")
  181.                        )
  182.                        (   t   )
  183.                    )
  184.                )
  185.                (   (= 25 gr1)
  186.                    nil
  187.                )
  188.                (   t   )
  189.            )
  190.        )
  191.    )
  192.    rtn
  193. )
  194. (vl-load-com) (princ)
204953q4yylo4ehdwnlyl8.png
回复

使用道具 举报

5

主题

31

帖子

26

银币

初来乍到

Rank: 1

铜币
26
发表于 2022-7-5 20:45:37 | 显示全部楼层
 
难以置信的李,非常非常好。非常感谢你。
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:49:20 | 显示全部楼层
 
欢迎你,卡德凡-写作是一个有趣的挑战。
 
我现在意识到,我将属性标记保留为“tag1”,这是我用于测试的内容-我现在在上述代码中对此进行了修改。
 
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 20:50:44 | 显示全部楼层
Lee很好的代码与往常一样,我之所以表示使用属性创建顺序,是因为它使函数与任何属性块一起工作,您不需要硬编码的属性名称。因此,对于不同的块,如果有不同的标记名,则必须复制所有代码。在我写的第一篇小文章中,我在一个有12个属性的块上进行了测试,并通过重复代码更改了多个属性。正如您和其他阅读本文的人所知,可能99%的属性请求都是硬编码到标记名的,但可能更具全局性。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-12 13:32 , Processed in 0.494314 second(s), 70 queries .

© 2020-2025 乐筑天下

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