Author Topic: add text strings by suffix filter  (Read 7677 times)

0 Members and 1 Guest are viewing this topic.

fathihvac

  • Guest
add text strings by suffix filter
« on: June 30, 2011, 02:01:46 PM »
Hello,
 This my first post and it's a question but i will be useful by submitting helpful lisps in the near future.
My question is : How can i add text string numbers with same suffixes  in an autocad drawing and paste results as multiple lines texts.example : 12 m(diam 1/4)   136 m(diam 1/2)   30 m(diam 1/4)  2 m(diam 1/2) So the result would be:

42 m(diam 1/4)
138 m(diam 1/2)
Thaning You IN Advance :-)

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: add text strings by suffix filter
« Reply #1 on: June 30, 2011, 02:04:50 PM »
Welcome to theSwamp  :-)

You would need to parse the numbers from the string, group the items and perform the arithmetic, then construct a resultant string.

I wrote a simple function to parse numbers from a string here.

Lee

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: add text strings by suffix filter
« Reply #2 on: June 30, 2011, 02:18:23 PM »
Welcome to the Swamp 8-)

I'm not sure I understand the request.
Could you post a sample drawing with a before and after example?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: add text strings by suffix filter
« Reply #3 on: June 30, 2011, 02:21:21 PM »
This is how I understood it, quickly hacked together:

Code: [Select]
(defun c:test ( / _assoc++ i l lst ss )

  (defun _assoc++ ( key value alist )
    (
      (lambda ( pair )
        (if pair
          (subst (list key (+ value (cadr pair))) pair alist)
          (cons  (list key value) alist)
        )
      )
      (assoc key alist)
    )
  )

  (if (setq ss (ssget '((0 . "TEXT") (1 . "*#*#*"))))
    (progn
      (repeat (setq i (sslength ss))
        (if
          (setq l
            (LM:ParseNumbers
              (cdr
                (assoc 1
                  (entget (ssname ss (setq i (1- i))))
                )
              )
            )
          )
          (setq lst (_assoc++ (cdr l) (car l) lst))
        )
      )
      (print lst)
    )
  )
  (princ)
)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: add text strings by suffix filter
« Reply #4 on: July 01, 2011, 01:24:14 AM »
I'm also a bit confused by the request. Although I think Lee's code will solve the most difficult portion (whatever the result may be). What confused me is the word "paste" in the OP. Do you want the text lines to be available on the Windows Clipboard? Or do you simply want the pipe lengths to be listed in the DWG somewhere?

If the later, I'd have made block-tags with 2 attributes instead of text. One attr would be the pipe-dia, while the 2nd would be the length. That way you'd be able to use Data Extraction to have a table of pipe lengths (even totals combined as your example) constantly updating without running extra code all the time.

Even if you want the totals into something like Excel - the Data Extract could still work, though it's not as "automatic" then.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: add text strings by suffix filter
« Reply #5 on: July 01, 2011, 06:29:15 AM »
Lee would adding this to your parser be out of line?
Code: [Select]
(and (= 47 b) (< 47 a 58) (< 47 c 58)) ; fractions

PS Do you use fractions at all in the UK?
« Last Edit: July 01, 2011, 06:58:54 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: add text strings by suffix filter
« Reply #6 on: July 01, 2011, 07:44:03 AM »
Lee would adding this to your parser be out of line?
Code: [Select]
(and (= 47 b) (< 47 a 58) (< 47 c 58)) ; fractions

A nice idea, I considered including fractions when writing it, however, since my function uses the AutoLISP 'read' function to convert the resultant expression to the various numerical data types, fractions would be returned as symbols. I include a few notes about this behaviour in the description  :-)

PS Do you use fractions at all in the UK?

AFAIK, predominantly decimal.
« Last Edit: July 01, 2011, 07:53:08 AM by Lee Mac »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: add text strings by suffix filter
« Reply #7 on: July 01, 2011, 08:09:58 AM »
Worked for me 8-)
Code: [Select]
_$ (LM:ParseNumbers "ab1/2cd1.5b3/4")
(1/2 1.5 3/4)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: add text strings by suffix filter
« Reply #8 on: July 01, 2011, 08:21:46 AM »
Worked for me 8-)
Code: [Select]
_$ (LM:ParseNumbers "ab1/2cd1.5b3/4")
(1/2 1.5 3/4)

But 1/2 is a symbol in LISP, not a number  :?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: add text strings by suffix filter
« Reply #9 on: July 01, 2011, 09:12:45 AM »
OK I see, you would need distof to extract fractions.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: add text strings by suffix filter
« Reply #10 on: July 01, 2011, 10:53:22 AM »
Lee how about this one?
Code: [Select]
;;; Split a string into portions of specified char-sets
(defun split-parse-char (char-sets str / cur-char-set cur-char-set-idx current lst)
  (setq char-sets (mapcar 'vl-string->list char-sets))
  (foreach char (vl-string->list str)
    (setq cur-char-set-idx -1)
    (if (vl-some '(lambda (char-set / pos)
                    (setq cur-char-set-idx (1+ cur-char-set-idx))
                    (vl-position char char-set)
                  )
                 char-sets
        )
      (if (vl-position char cur-char-set)
        (setq current (cons char current))
        (setq lst          (cons current lst)
              current      (list char)
              cur-char-set (nth cur-char-set-idx char-sets)
        )
      )
      (setq lst (cons current lst) current nil)
    )
  )
  (setq lst (cons current lst))
  (mapcar '(lambda (portion) (vl-list->string (reverse portion))) (reverse (vl-remove-if 'null lst)))
)

;;; Parse a string into its number portions, including real numbers and fractions
(defun parsenum (str /)
  (mapcar
    '(lambda (portion) (if (wcmatch portion "*/*") (distof portion) (read portion)))
    (split-parse-char '("0123456789.-/") str)
  )
)
I have other uses also for that split-parse-char  ;) . It's a bit over the top for this case, but I didn't go and re-invent the wheel. Probably you'll come up with a simpler method of doing that as well!

OK I see, you would need distof to extract fractions.
That seems to work, though if there's any decimals around the distof seems to give a nil result on fractions. Not that fractions "aught" to have decimals mixed up inside them, but consider the following result:
Code: [Select]
_$ (parsenum "123/50 1.0/3 2/5.6 -78")
(2.46 nil nil -78)
And you do need to use it only on fractions, otherwise all the integers gets returned as floating point.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: add text strings by suffix filter
« Reply #11 on: July 01, 2011, 11:22:58 AM »
Nice one Irne, that works well  8-)

I think I'll stick to mine however, since I like its simplicity and don't really deal in fractions  ;-)

fathihvac

  • Guest
Re: add text strings by suffix filter
« Reply #12 on: July 02, 2011, 09:16:56 AM »

Hello everybody

Thank you very much Lee Mac,CAB and irneb for your quick answers .I will try what you suggested to me  and i will feedback.

fathihvac

  • Guest
Re: add text strings by suffix filter
« Reply #13 on: July 02, 2011, 10:35:25 AM »
Hello everybody

Here is an example attached to explain what i want.And i think it will be (the solution ) helpful for many. :-)

hermanm

  • Guest
Re: add text strings by suffix filter
« Reply #14 on: July 04, 2011, 01:15:12 AM »
FWIW:

Code: [Select]
;;;----------------------str2int.lsp-------------------
;;;converts a string representing a positive integer into the integer
;;;----------------------------------------------------
(defun str2int (str / chlst)
  (cond
    ((= (type str) 'INT) str);is an integer, so return it
    ((= (strcase str) "ONE") 1);represents 1, so return 1
    ((= (type str) 'STR)
     (progn
       (setq chlst (vl-string->list str))
       (if (vl-some 'null
                   (mapcar
                     (function (lambda (x)
                                 (and (/= x 47)
                                      (> 58 x 44)
                                 )
                               )
                     )
                     chlst
                   )
           )
         nil ; the string does not represent an integer
         (atoi str) ;integer
       );if
     );progn
    )
    ((= (type str) 'SYM) (str2int (eval str)));recurse on evaluated arg
    (T nil);not an integer or a string which represents an integer
  )
);str2int

;;function to "sum" a list of strings which represent integers
;;and return a string representing the result
(defun sumstrings (alist)
  (setq alist (vl-catch-all-apply '+ (mapcar 'str2int alist)))
  (if (vl-catch-all-error-p alist)
    (setq alist "??")
    (itoa alist)
  )
)