Jump to content

add decimal to a numeric field


Fabs91

Recommended Posts

Hello

is it possible to add decimals to fields in a lisp mtext, in my case 16 fields in the total mtext.

 

image.png.01c9f83f3c5330518a21ccd499d5ad69.png

 

thanks in advance. Fabs

Link to comment
Share on other sites

@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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

image.thumb.png.ca2cb91e01c7e54baeaa188b99fa22c5.png

 

thanks for your help. Fabs91

image.png

Link to comment
Share on other sites

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

 

  • Like 1
Link to comment
Share on other sites

Hi Tsuky

 

a BIG thank you for the solution that works, it's going to save me an enormous amount of time

 

Fabs91

Link to comment
Share on other sites

@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 by pkenewell
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

pkenewell.

 

I've just tested it.
Indeed, if the MTEXT contains only 1 field, it goes to ####.
congratulations on seeing this bug.

 

Sincerely Fab91

  • Like 1
Link to comment
Share on other sites

@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.🍻

  • Like 1
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...