乐筑天下

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

[编程交流] Block's name to FIELD (ba

[复制链接]

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 20:52:28 | 显示全部楼层
This is fantastic! So easy to understand and modify!
 
 
  I’ve added another variable ‘zvalue’ so for point coordinates you can decide to show X,Y,Z or only X,Y
 
  If I can ask you for 2 last options, please:
 
 
 


  • Add code (in the same easy to understand and modify way) to ask user do define values for let say prec and suff, where asking for prec user have  only options to choose (like drop down menu with numbers 0-4 but no chance to type different value- see graphics below). And for suff user can type anything or if left empty will be nil.
  • Add IF function- so let say for point coordinates I want ask user: “Show Z value?” and user have 2 options Yes and No. If Yes – variable zvalue will be 3, if No- zvalue will be nil.

 
211754j50cu01cz152qz25.jpg
 
 
Hopefully this is not too complicated...
 
Cheers!
211756ixb2iz2r22rdd802.jpg
makefield-POINT.lsp
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 20:55:34 | 显示全部楼层
Try something like this:
 
  1. (defun c:MakeField ( / *error* object property units prec pref suff zval conv doc spc e p ) (vl-load-com) ;; © Lee Mac 2010;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;;;                                                                               ;;;;                                Adjustments                                    ;;;;                                                                               ;;;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;; (setq object   "INSERT"         ;; Object Type (cannot be nil)       property "InsertionPoint" ;; Field Property (must belong to object, else field = #### )       units     2               ;; Units (integer: 1-6 or nil)       prec      3               ;; Precision (integer: 0-8 or nil)       pref     "Lee"            ;; Prefix (string or nil)       suff     "Mac"            ;; Suffix (string or nil)       conv      nil             ;; Conversion Factor (real or nil)       zval      t               ;; Hide Z-Vale (t or nil) );;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;; (defun *error* ( msg )   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")       (princ (strcat "\n** Error: " msg " **")))   (princ) ) (LM:ActiveSpace 'doc 'spc) (while (not (vla-object e)) ">%)." property       (if (apply 'or (list units prec conv pref suff))         (strcat " \\f ""           (if units (strcat "%lu" (itoa units)) "")           (if zval  "%pt3" "")           (if prec  (strcat "%pr" (itoa prec )) "")           (if (or pref suff) (strcat "%ps[" (cond ( pref ) ( "" )) "," (cond ( suff ) ( "" )) "]") "")           (if conv  (strcat "%ct8[" (rtos conv) "]") "")           """         )         ""       )       ">%"     )   ) ) (princ))(defun LM:AddMText_MC ( space pt str / obj ) ;; © Lee Mac 2010 (if   (not     (vl-catch-all-error-p       (setq obj         (vl-catch-all-apply (function vla-AddMText)           (list space (vlax-3D-point pt) 0. str)         )       )     )   )   (progn     (vla-put-AttachmentPoint obj acAttachmentPointMiddleCenter)     (vla-put-InsertionPoint obj (vlax-3D-point pt))   ) ))(defun LM:GetObjectID ( doc obj ) ;; © Lee Mac 2010 (if (eq "X64" (strcase (getenv "PROCESSOR_ARCHITECTURE")))   (vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)   (itoa (vla-get-Objectid obj)) ));;--------------------=={ ActiveSpace }==---------------------;;;;                                                            ;;;;  Retrieves pointers to the Active Document and Space       ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  *doc - quoted symbol other than *doc                      ;;;;  *spc - quoted symbol other than *spc                      ;;;;------------------------------------------------------------;;(defun LM:ActiveSpace ( *doc *spc ) ;; © Lee Mac 2010 (set *spc   (if     (or       (eq AcModelSpace         (vla-get-ActiveSpace           (set *doc             (vla-get-ActiveDocument               (vlax-get-acad-object)             )           )         )       )       (eq :vlax-true (vla-get-MSpace (eval *doc)))     )     (vla-get-ModelSpace (eval *doc))     (vla-get-PaperSpace (eval *doc))   ) ));;-------------------=={ Select if Foo }==--------------------;;;;                                                            ;;;;  Continuous selection prompts until the predicate function ;;;;  foo is validated                                          ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  foo - predicate function taking ename argument            ;;;;  str - prompt string                                       ;;;;------------------------------------------------------------;;;;  Returns:  selected entity ename if successful, else nil   ;;;;------------------------------------------------------------;;(defun LM:SelectifFoo ( foo str / e ) ;; © Lee Mac 2010 (while   (progn     (setq e (car (entsel str)))          (cond       (         (eq 'ENAME (type e))         (if (not (foo e)) (princ "\n** Invalid Object Selected **"))       )     )   ) ) e)
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 20:57:52 | 显示全部楼层
This is excellent piece of work! Working perfectly, easy to understand for someone without lisp programming skills and easy to customise it to my needs!
 
  THANK YOU!
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 21:01:48 | 显示全部楼层
Hi Lee Mac,
 
Final question about code:
 
when you are using:
 
  
[code](while (not (
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 21:03:45 | 显示全部楼层
 
You're quite welcome Tom
 
 
I have used the getint function prompt for an integer, hence the function accepts an integer input, not a string. You can quite easily alter the string prompt to display as you posted, but the entry would have still have to be a number.
 
Lee
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 21:08:09 | 显示全部楼层
I've just spent 2 days trying to work it out and I'm stuck
 
What I want to do is create a label with area value of selected hatch- so is a modification of lisp attached as a first one.
 
Lisp should be like that:
 
Specify Precision:
                         1
                         2
 
Specify Conversion Factor:
                                    0.0001 (m2 ->ha)
                                    0.000001 (m2->km2)
 
And another tricky part here with suffix. If user selected 1 for conversion factor, suffix should be m2. If 0.0001- suffix ha, 0.000001- suffix km2. Don't want to ask user about suffix- just depends with conversion factor, suffix will be there.
 
I'm really fed up- I've sorted one thing, other is not working... Seems to be really simple, but after 2 days...
 
Can you help, please?
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 21:09:17 | 显示全部楼层
Something like this seems more intuitive to me:
 
  1. (defun c:MakeField ( / *error* object convlst property units prec pref suff zval conv doc spc e p unit ) (vl-load-com) ;; © Lee Mac 2010;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;;;                                                                               ;;;;                                Adjustments                                    ;;;;                                                                               ;;;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;; (setq object   "HATCH"          ;; Object Type (cannot be nil)       property "Area"           ;; Field Property (must belong to object, else field = #### )       units     2               ;; Units (integer: 1-6 or nil)       prec      3               ;; Precision (integer: 0-8 or nil)       pref      nil             ;; Prefix (string or nil)       suff      nil             ;; Suffix (string or nil)       conv      nil             ;; Conversion Factor (real or nil)       zval      nil             ;; Hide Z-Vale (t or nil) ) (setq convLst '(("m²" . 1) ("ha" . 0.0001) ("km²" . 0.000001)));;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;; (defun *error* ( msg )   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")       (princ (strcat "\n** Error: " msg " **")))   (princ) ) (LM:ActiveSpace 'doc 'spc) (initget "0 1 2") (setq prec   (atoi     (cond       (         (getkword "\nSpecify Precision [0/1/2]  : ")       )       ( "0" )     )   ) ) (initget (LM:lst->str (mapcar 'car convLst) " ")) (setq unit   (assoc     (cond       (         (getkword           (strcat "\nSpecify Unit ["             (LM:lst->str (mapcar 'car convLst) "/") "]  : "           )         )       )       ( (caar convLst) )     )     convLst   ) ) (setq suff (car unit) conv (cdr unit)) (while   (and     (setq e       (LM:SelectifFoo         (lambda ( x )           (eq object (cdr (assoc 0 (entget x))))         )         (strcat "\nSelect " object ": ")       )     )     (setq p (getpoint "\nPick Point for Field: "))   )   (LM:AddMText_MC spc p     (strcat "%%)." property       (if (apply 'or (list units prec conv pref suff))         (strcat " \\f ""           (if units (strcat "%lu" (itoa units)) "")           (if zval  "%pt3" "")           (if prec  (strcat "%pr" (itoa prec )) "")           (if (or pref suff) (strcat "%ps[" (cond ( pref ) ( "" )) "," (cond ( suff ) ( "" )) "]") "")           (if conv  (strcat "%ct8[" (rtos conv) "]") "")           """         )         ""       )       ">%"     )   ) ) (princ))(defun LM:AddMText_MC ( space pt str / obj ) ;; © Lee Mac 2010 (if   (not     (vl-catch-all-error-p       (setq obj         (vl-catch-all-apply (function vla-AddMText)           (list space (vlax-3D-point pt) 0. str)         )       )     )   )   (progn     (vla-put-AttachmentPoint obj acAttachmentPointMiddleCenter)     (vla-put-InsertionPoint obj (vlax-3D-point pt))   ) ))(defun LM:GetObjectID ( doc obj ) ;; © Lee Mac 2010 (if (eq "X64" (strcase (getenv "PROCESSOR_ARCHITECTURE")))   (vlax-invoke-method (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)   (itoa (vla-get-Objectid obj)) ));;--------------------=={ ActiveSpace }==---------------------;;;;                                                            ;;;;  Retrieves pointers to the Active Document and Space       ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  *doc - quoted symbol other than *doc                      ;;;;  *spc - quoted symbol other than *spc                      ;;;;------------------------------------------------------------;;(defun LM:ActiveSpace ( *doc *spc ) ;; © Lee Mac 2010 (set *spc   (if     (or       (eq AcModelSpace         (vla-get-ActiveSpace           (set *doc             (vla-get-ActiveDocument               (vlax-get-acad-object)             )           )         )       )       (eq :vlax-true (vla-get-MSpace (eval *doc)))     )     (vla-get-ModelSpace (eval *doc))     (vla-get-PaperSpace (eval *doc))   ) ));;-------------------=={ Select if Foo }==--------------------;;;;                                                            ;;;;  Continuous selection prompts until the predicate function ;;;;  foo is validated                                          ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  foo - predicate function taking ename argument            ;;;;  str - prompt string                                       ;;;;------------------------------------------------------------;;;;  Returns:  selected entity ename if successful, else nil   ;;;;------------------------------------------------------------;;(defun LM:SelectifFoo ( foo str / e ) ;; © Lee Mac 2010 (while   (progn     (setq e (car (entsel str)))          (cond       (         (eq 'ENAME (type e))         (if (not (foo e)) (princ "\n** Invalid Object Selected **"))       )     )   ) ) e);;-------------------=={ List to String }==-------------------;;;;                                                            ;;;;  Constructs a string from a list of strings separating     ;;;;  each element by a specified delimiter                     ;;;;------------------------------------------------------------;;;;  Author: Lee McDonnell, 2010                               ;;;;                                                            ;;;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  lst - a list of strings to process                        ;;;;  del - delimiter by which to separate each list element    ;;;;------------------------------------------------------------;;;;  Returns:  String containing each string in the list       ;;;;------------------------------------------------------------;;(defun LM:lst->str ( lst del ) ;; © Lee Mac 2010 (if (cdr lst)   (strcat (car lst) del (LM:lst->str (cdr lst) del))   (car lst) ))
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 21:11:53 | 显示全部楼层
Hi Lee,
 
This is magic and you're right- this is more intuitive
 
The only small problem is when asked for precision (is 0 set as default because black dot is next to 0?) and user hit Enter- there is a error: ** Error: bad argument type: stringp 0 **
Is working fine when user is clicking 0 from the list.
 
Is there any easy way to fix it, please?
 
Once again- thank you very much. This is far better than I expected....
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 21:15:22 | 显示全部楼层
Sorry Tom, I rushed it a bit and wrote it too quickly - I must've got the data types mixed up, a minor fix and code is now updated, please try it.
 
Lee
回复

使用道具 举报

6

主题

36

帖子

34

银币

初来乍到

Rank: 1

铜币
34
发表于 2022-7-5 21:17:58 | 显示全部楼层
Lee- you are officially my own favorite Lisp Guru!
 
  Massive thank you for that!
 
  If you are interested – I’ve posted new thread. This time something challenging
 
http://www.cadtutor.net/forum/showthread.php?51451-CHALLENGING-TASK!!!-%E2%80%93-GB-OS-Grid-involved!
 
 
Cheers,
Tom
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-11 08:53 , Processed in 0.647594 second(s), 72 queries .

© 2020-2025 乐筑天下

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