Fabs91 Posted April 15 Posted April 15 Hello is it possible to add decimals to fields in a lisp mtext, in my case 16 fields in the total mtext. thanks in advance. Fabs Quote
pkenewell Posted April 15 Posted April 15 @Fabs91 The decimal precision can be set in Fields.Are you trying to do in AutoLISP code, or just in the FIELD dialog box? Please share what you mean. If in code, then add to the back of your field expression: \f "%pr6" >%, where the "6" is your precision value. Example: %<\AcObjProp Object(%<\_ObjId 2553648256448>%).Length \f "%lu2%pr2">% The "\f" is for formatting, "%lu2" is the linear units setting (2 is decimal), the "%pr2" is the decimal precision. Quote
Fabs91 Posted April 15 Author Posted April 15 Hello Pkenewell. I want to use a lisp to select my MTEXT, which contains 16 fields. so that it modifies "%pr0" by "%pr2" by reading the fields it finds one by one. Fabs91 Quote
Tsuky Posted April 15 Posted April 15 A start whith this? (defun c:foo ( / ss n ent obj old new) (setq ss (ssget '((0 . "MTEXT")))) (cond (ss (repeat (setq n (sslength ss)) (setq ent (ssname ss (setq n (1- n))) obj (vlax-ename->vla-object ent) old (vla-fieldcode obj) new (vl-string-subst "%pr2" "%pr0" old) ) (vlax-put obj 'TextString new) ) ) ) (prin1) ) 1 Quote
Fabs91 Posted April 16 Author Posted April 16 Hi Tsuky. it's great, it's close to what I need. Just a small improvement: instead of clicking X times on the MTEXT, with 1 click it reads the MTEXT and tends that it finds a "%pr0" it continues. thanks in advance. You save me time as I have 64 plans to modify. PS: may be replace "repeat" with a "while" loop. Quote
Tsuky Posted April 16 Posted April 16 Hi, At the message "Select objects:", you can select an object but also by several individual clicks, by window, by crossing or other form of selection. Only mtexts are taken into account even if you select other objects. Quote
Fabs91 Posted April 16 Author Posted April 16 Hi Tsuky I agree with you, it works. I'm attaching a sketch to show you my Mtext, which has 8 fields inside. At the moment, I have to click 8 times for the MTEXT to update. thanks for your help. Fabs91 Quote
Tsuky Posted April 16 Posted April 16 Ok, You have multi lines or multi row/column in your Mtext. Try this (defun c:foo ( / ss n ent obj old new) (defun string-subst (nam_obj / value_string nbs tmp_nbs) (setq value_string (vla-fieldcode nam_obj) nbs 0) (while nbs (if (setq nbs (vl-string-search "%pr0" value_string (setq tmp_nbs nbs))) (setq value_string (vl-string-subst "%pr2" "%pr0" value_string tmp_nbs) nbs (1+ nbs) ) ) ) (vlax-put nam_obj 'TextString value_string) ) (setq ss (ssget '((0 . "MTEXT")))) (cond (ss (repeat (setq n (sslength ss)) (setq ent (ssname ss (setq n (1- n))) obj (vlax-ename->vla-object ent) ) (string-subst obj) ) ) ) (prin1) ) 1 Quote
Fabs91 Posted April 16 Author Posted April 16 Hi Tsuky a BIG thank you for the solution that works, it's going to save me an enormous amount of time Fabs91 Quote
pkenewell Posted April 16 Posted April 16 (edited) @Tsuky Good Job. I did notice a bug however, and I am not sure why it is behaving like this. Try running your command on an MTEXT object that has only 1 field with no other text around it. For some reason it will kill the Field object, leaving behind "####" or "---". EDIT: I figured it out - thanks to reviewing Lee Mac's FieldArithmetic program. You need to clear the text contents before re-adding them for some reason. (defun c:foo ( / ss n ent obj old new) (defun string-subst (nam_obj / value_string nbs tmp_nbs) (setq value_string (vla-fieldcode nam_obj) nbs 0) (while nbs (if (setq nbs (vl-string-search "%pr0" value_string (setq tmp_nbs nbs))) (setq value_string (vl-string-subst "%pr2" "%pr0" value_string tmp_nbs) nbs (1+ nbs) ) ) ) (vlax-put nam_obj 'TextString "") ;; Add this line (vlax-put nam_obj 'TextString value_string) ) (setq ss (ssget '((0 . "MTEXT")))) (cond (ss (repeat (setq n (sslength ss)) (setq ent (ssname ss (setq n (1- n))) obj (vlax-ename->vla-object ent) ) (string-subst obj) ) ) ) (prin1) ) Edited April 16 by pkenewell 1 1 Quote
Fabs91 Posted April 17 Author Posted April 17 pkenewell. I've just tested it. Indeed, if the MTEXT contains only 1 field, it goes to ####. congratulations on seeing this bug. Sincerely Fab91 1 Quote
Tsuky Posted April 18 Posted April 18 @pkenewell Very good remark! I had only briefly tested my function and I missed this situation. Thank you for your correction which resolves this problem. 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.