Author Topic: sum text contained number ?  (Read 4192 times)

0 Members and 1 Guest are viewing this topic.

dussla

  • Bull Frog
  • Posts: 291
sum text contained number ?
« on: May 24, 2016, 03:31:01 AM »
sorry my poor english
if you see my attached , friends understand ?
pls pls can you help ?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: sum text contained number ?
« Reply #1 on: May 24, 2016, 05:11:34 AM »
Just to start:
Code: [Select]
(defun Test (Str001 Str002 / Num001 Num002 Nm1Str)
  (setq
    Num001 (atof Str001)   Num002 (atof Str002)   Nm1Str (rtos Num001)
  )
  (strcat (rtos (+ Num001 Num002)) (substr Str001 (1+ (strlen Nm1Str))))
)
Comand: (Test "1.3m-b" "1.3m-b") "2.6m-b"
Comand: (Test "1.3m-b" "3.3m-b") "4.6m-b"
Comand: (Test "2100-a" "3100-a") "5200-a"
Comand: (Test "2.72m2-c" "3.72m2-c") "6.44m2-c"
Comand: (Test "212.12f-d" "322.12f-d") "534.24f-d"
Comand: (Test "27.05kg-e" "60.05kg-e") "87.1kg-e"

ronjonp

  • Needs a day job
  • Posts: 7529
Re: sum text contained number ?
« Reply #2 on: May 24, 2016, 12:30:15 PM »
Like so?  :)
Code - Auto/Visual Lisp: [Select]
  1. (defun c:sumitupyo (/ fuzz ip n1 o o2 o3 r ss suf txt x)
  2.   ;; 05.24.2016 RJP
  3.   ;; Sums up text items on similar Y plane
  4.   (if
  5.     (and ;; Get text
  6.          (setq ss (ssget ":L" '((0 . "Text"))))
  7.          ;; Convert selection set to list
  8.          (setq ss (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))))
  9.          ;; Sort by smalles X coordinate
  10.          (setq
  11.            ss (vl-sort ss
  12.                        '(lambda (a b)
  13.                           (< (car (vlax-get a 'insertionpoint)) (car (vlax-get b 'insertionpoint)))
  14.                         )
  15.               )
  16.          )
  17.     );; While we have a list
  18.      (while (setq o (car ss))
  19.        ;; Remove the first item
  20.        (setq ss (cdr ss))
  21.        ;; Get insertion point
  22.        (setq ip (vlax-get o 'insertionpoint))
  23.        ;; Get texstring
  24.        (setq txt (vla-get-textstring o))
  25.        ;; Convert text to number
  26.        (setq n1 (atof txt))
  27.        ;; Get suffix
  28.        (setq suf (substr txt (1+ (strlen (rtos n1)))))
  29.        ;; Fuzz value = 1/2 text height
  30.        (setq fuzz (* 0.5 (vla-get-height o)))
  31.        ;; Find the other item in the list on the same Y plane within a fuzz value
  32.        (setq o2
  33.               (vl-remove-if-not '(lambda (x) (equal (cadr ip) (cadr (vlax-get x 'insertionpoint)) fuzz))
  34.                                 ss
  35.               )
  36.        )
  37.        ;; Remove those items from the list
  38.        (mapcar '(lambda (x) (setq ss (vl-remove x ss))) o2)
  39.        ;; Add the values
  40.        (setq
  41.          r (strcat
  42.              (rtos
  43.                (apply '+ (mapcar '(lambda (x) (atof (vla-get-textstring x))) (append (list o) o2)))
  44.              )
  45.              suf
  46.            )
  47.        )
  48.        ;; If we've added items, make a copy of the last text object & put summed value
  49.        (if o2
  50.          (progn (setq o3 (vlax-invoke (last o2) 'copy))
  51.                 (vla-put-color o3 3)
  52.                 (vlax-invoke o3
  53.                              'move
  54.                              ip
  55.                              (polar ip
  56.                                     (angle ip (vlax-get (car o2) 'insertionpoint))
  57.                                     (distance ip (vlax-get (car o2) 'insertionpoint))
  58.                              )
  59.                 )
  60.                 (vla-put-textstring o3 r)
  61.          )
  62.        )
  63.      )
  64.   )
  65.   (princ)
  66. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: sum text contained number ?
« Reply #3 on: May 24, 2016, 01:48:54 PM »
Nice one Ron  :lol:

ronjonp

  • Needs a day job
  • Posts: 7529
Re: sum text contained number ?
« Reply #4 on: May 24, 2016, 02:49:20 PM »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: sum text contained number ?
« Reply #5 on: May 24, 2016, 03:01:27 PM »
I'll wager that the OP actually wants a general purpose program will no constraints on the position of the text items, but your program exactly performs to the requirements given, so they can't complain. :lol:

ronjonp

  • Needs a day job
  • Posts: 7529
Re: sum text contained number ?
« Reply #6 on: May 24, 2016, 03:08:29 PM »
I'll wager that the OP actually wants a general purpose program will no constraints on the position of the text items, but your program exactly performs to the requirements given, so they can't complain. :D
I bet you're right .. but for a freebie it's a good start  8)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: sum text contained number ?
« Reply #7 on: May 24, 2016, 04:24:21 PM »
I'll wager that the OP actually wants a general purpose program will no constraints on the position of the text items, but your program exactly performs to the requirements given, so they can't complain. :D
I bet you're right .. but for a freebie it's a good start  8)

Oh definitely  :-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: sum text contained number ?
« Reply #8 on: May 25, 2016, 02:36:51 AM »
Ron you have done an excellent job but, I believe, the greatest satisfaction I think it needs to come from the OP to know if his request was fulfilled.
Unfortunately many, solved their need, do not bother to inform future readers of these messages if the given problem has been solved and how.  :knuppel2:

See also: https://www.theswamp.org/index.php?topic=51489.0.

I think a new acronym (Mosquito, Swamp Rat, Water Moccasin...) needs for these gentlemen: NED (not educated person).  :-)
Ciao.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: sum text contained number ?
« Reply #9 on: May 25, 2016, 03:43:33 AM »

Marc'Antonio,
Yes, posters who don't bother to respond are annoying, but I don't advocate forcing them to wear a "scarlet letter".

gentle nudges sometimes work.

A post by a peer, as you have done, lauding the veracity of a solution does help those who follow.

Thanks.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: sum text contained number ?
« Reply #10 on: May 25, 2016, 03:53:23 AM »

Marc'Antonio,
Yes, posters who don't bother to respond are annoying, but I don't advocate forcing them to wear a "scarlet letter".

gentle nudges sometimes work.

A post by a peer, as you have done, lauding the veracity of a solution does help those who follow.

Thanks.
Prego. I agree.  :-)

dussla

  • Bull Frog
  • Posts: 291
Re: sum text contained number ?
« Reply #11 on: May 25, 2016, 05:16:26 AM »
perpect  wow  wow
friend  is really genius
wow  wow  wow ~~~~

really thank you ~~~

dussla

  • Bull Frog
  • Posts: 291
Re: sum text contained number ?
« Reply #12 on: May 25, 2016, 05:24:08 AM »
thank you   friends

but there is some problem ~~
pls check ~~~
sorry for my many ask

if you see image  , you can see error~

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: sum text contained number ?
« Reply #13 on: May 25, 2016, 05:38:23 AM »

dussla,

You've been visiting here for 9 or 10  years.
Have you learnt how to read code yet ?
How much code writing have you done.
How many posts have you made trying to help people.

The reason you are getting the result you get is because of the color of your text.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: sum text contained number ?
« Reply #14 on: May 25, 2016, 06:21:42 AM »
..
The reason you are getting the result you get is because of the color of your text.
Reading :idea: ronjons code I think the (shifted) Y coordinates are causing this issue.