tomjas 发表于 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 haveonly 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.

 

 
 
Hopefully this is not too complicated...
 
Cheers!

makefield-POINT.lsp

Lee Mac 发表于 2022-7-5 20:55:34

Try something like this:
 

(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)

tomjas 发表于 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!

tomjas 发表于 2022-7-5 21:01:48

Hi Lee Mac,
 
Final question about code:
 
when you are using:
 

(while (not (

Lee Mac 发表于 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

tomjas 发表于 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?

Lee Mac 发表于 2022-7-5 21:09:17

Something like this seems more intuitive to me:
 

(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" )   )   ) ) (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) ))

tomjas 发表于 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....

Lee Mac 发表于 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

tomjas 发表于 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
页: 1 [2]
查看完整版本: Block's name to FIELD (ba