Is there a Lisp that can sum two texts and bring the total to the next text?

Is there a Lisp that can sum two texts and bring the total to the next text?

Oliver_C3D
Advocate Advocate
8,340 Views
48 Replies
Message 1 of 49

Is there a Lisp that can sum two texts and bring the total to the next text?

Oliver_C3D
Advocate
Advocate

Is there a Lisp that can sum two texts and bring the total to the next text?

S01.png

0 Likes
Accepted solutions (1)
8,341 Views
48 Replies
Replies (48)
Message 2 of 49

devitg
Advisor
Advisor

Please upload your sample.dwg 

 

0 Likes
Message 3 of 49

Kent1Cooper
Consultant
Consultant

I'm sure that's possible, but explain more....

 

What are we looking at in the image?  The "before" condition?  [It can't be "after" since no Text content in the box appears to be the sum of any two others.]  What would the "after" condition be?  An additional Text object to be added?  Or to change the content of one already drawn?  [That is, what is meant by "next" text?]  What would the User's process be?  Asked to select two of them and then where to put a new one that represents their sum?  Asked to select two, and then to select one to change to represent their sum?  Asked to select two, and the routine would somehow find the "next" one to change the content of?  Etc., etc.

Kent Cooper, AIA
0 Likes
Message 4 of 49

Oliver_C3D
Advocate
Advocate

That is, I select the first text and then the second text, and put the sum of the two in the second text.

0 Likes
Message 5 of 49

Kent1Cooper
Consultant
Consultant

@Oliver_C3D wrote:

That is, I select the first text and then the second text, and put the sum of the two in the second text.


Something like this [minimally tested]:

(defun C:SNT2 ; = Sum of Numerical Texts into 2nd one selected
  (/ txt1 txt2 txt2data sum)
  (setq
    txt1 (car (entsel "\nFirst numerical Text object: "))
    txt2 (car (entsel "\nSecond numerical Text object to have content changed to sum with First: "))
    txt2data (entget txt2)
    sum (+ (atof (cdr (assoc 1 (entget txt1)))) (atof (cdr (assoc 1 txt2data))))
  ); setq
  (entmod (subst (cons 1 (rtos sum 2 2)) (assoc 1 txt2data) txt2data))
  (princ)
); defun

It doesn't check that you actually picked things, nor that they're both Text objects, nor that their content represents numbers, but all that can be added.  It would also work with Mtext if it has no internal formatting  but just plain content representing a number.

Kent Cooper, AIA
0 Likes
Message 6 of 49

Oliver_C3D
Advocate
Advocate

Unfortunately, text format does not work for this model!

St01.png

0 Likes
Message 7 of 49

Kent1Cooper
Consultant
Consultant

@Oliver_C3D wrote:

Unfortunately, text format does not work for this model!


No, it wouldn't -- chainage numbers are a very different animal from ordinary numbers.

 

To do that would require the routine to determine that it's dealing with chainage numbers, find the part of the text content that follows the + sign in each, work with the resulting numbers, check whether the sum needs any leading zero(s), and put the sum back with the + sign and what comes before it.

 

But what if the first is 0+234.56 and the second is 0+890.12 -- should the end result be 1+124.68?  You see the complications....

Kent Cooper, AIA
0 Likes
Message 8 of 49

dlanorh
Advisor
Advisor

I've tweaked Kents lisp to work with chainages. This assumes the number before the + is in thousands of units.

 

(defun rh:ri_str (new old str) (while (vl-string-search old str) (setq str (vl-string-subst new old str))) str)

(defun C:SNT2 ; = Sum of Numerical Texts into 2nd one selected
  (/ txt1 txt2 txt2data sum tsum)
  (setq
    txt1 (car (entsel "\nFirst numerical Text object: "))
    txt2 (car (entsel "\nSecond numerical Text object to have content changed to sum with First: "))
    txt2data (entget txt2)
    sum (+ (atof (rh:ri_str "" "+" (cdr (assoc 1 (entget txt1))))) (atof (rh:ri_str "" "+" (cdr (assoc 1 txt2data)))))
  ); setq
  (setq tsum (strcat (if (< sum 1000) "0" (itoa (fix (/ sum 1000)))) "+" (rtos (rem sum 1000) 2 2)))
  (entmod (subst (cons 1 tsum) (assoc 1 txt2data) txt2data))
  (princ)
); defun

I am not one of the robots you're looking for

Message 9 of 49

Oliver_C3D
Advocate
Advocate

This Lisp is exactly what I wanted. It was great and wonderful.
Thank you very much

@dlanorh 

0 Likes
Message 10 of 49

Oliver_C3D
Advocate
Advocate

This Lisp does not work properly in this section!

For example, adding 1 + 76.84 with 100.45 gives the result: 0 + 277.29 !!!

0 Likes
Message 11 of 49

Oliver_C3D
Advocate
Advocate

This Lisp does not work properly in this section!

For example, adding 1 + 76.84 with 100.45 gives the result: 0 + 277.29 !!!

@dlanorh 

0 Likes
Message 12 of 49

Kent1Cooper
Consultant
Consultant

That's clearly because it should be  1 + 076.84.

Kent Cooper, AIA
0 Likes
Message 13 of 49

john.uhden
Mentor
Mentor

Of course it doesn't.  The original question apparently involved metric chainages (x,xxx.xx), not imperial stations (x+xx.xx).  Where are the kudos for what @dlanorh did?

John F. Uhden

0 Likes
Message 14 of 49

Oliver_C3D
Advocate
Advocate

Yes, that's the problem!

0 Likes
Message 15 of 49

Oliver_C3D
Advocate
Advocate

his Lisp does not work properly in this section!

For example, adding 1 + 76.84 with 100.45 gives the result: 0 + 277.29 !!!

0 Likes
Message 16 of 49

Kent1Cooper
Consultant
Consultant

@Oliver_C3D wrote:

Yes, that's the problem!


So is the problem that it was incorrectly formatted, or that the procedure needs to be able to work in either kind of format?  @dlanorh 's routine figures out the numerical value by simply removing the + sign, so if there may not always be three characters between that and the decimal point, a different approach will be needed.

Kent Cooper, AIA
0 Likes
Message 17 of 49

Oliver_C3D
Advocate
Advocate

Yes there should be three characters!

0 Likes
Message 18 of 49

devitg
Advisor
Advisor

Mr. Soheiloliazadeh 

 

Are you afraid to get COVID19 , if you upload the sample.dwg. ??

 

0 Likes
Message 19 of 49

Kent1Cooper
Consultant
Consultant

[I don't think there's any need for a sample drawing -- the concept is simple enough.]  Is it likely that you will sometimes get similarly incorrectly-formatted text objects?  It would certainly be possible to write something could account for varying numbers [up to 3, anyway] of characters between the + sign and the decimal, though not for other kinds of incorrect formats [raw typing errors such as more than 3 characters there, or letters included, or other weirdnesses].

Kent Cooper, AIA
0 Likes
Message 20 of 49

Oliver_C3D
Advocate
Advocate

Sample

0 Likes