Jump to content

Sum of numbers in text strings


mikman

Recommended Posts

I was recently using a lisp that added up text and created a text string with the sum. It stopped working and quoted the function "mktext" as the problem. It will still add the strings but it will not create a new string. I can't modify the routine, its locked and the owner doesn't work with me anymore. :( /:D Where can I find a place that would have a lisp that I could read and learn for myself?

  • Like 1
Link to comment
Share on other sites

Here is a quick example, hope this helps

 

(defun C:STX (/ elist en ss sum sumtxt txt)
(princ "\n\t\t>>>  Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
 ;; set initial sum to zero :
 (setq sum 0.)
 ;; loop trough selected texts/mtexts :
 (while
   ;; get the first text in selection :
   (setq en (ssname ss 0))
   ;; get entity list of them :
   (setq elist (entget en))
   ;; get the textstring by key 1 from entity list :
   (setq txt (cdr (assoc 1 elist)))
   ;; create output string :
   (setq sumtxt
   ;; concatenate strings :
   (strcat
     ;; convert digits to string :
     (rtos
       ;; add to summ the digital value of text :
       (setq sum (+ (atof txt) sum))
       ;; 2 is for metric units (3 for engineering) :
       2
       ;; set precision by current :
       (getvar "dimdec")))
  )
   ;; delete entity from selection set :
   (ssdel en ss)
   )
 ;; display message :
 (alert (strcat "\nSumm = " sumtxt))
 )
)
(princ)
 )
(princ "\nStart command with STX...")
(princ)

 

Sorry for my poor english :)

 

~'J'~

  • Like 1
Link to comment
Share on other sites

(alert (strcat "\nSumm = " sumtxt))

That works pretty good Fixo. At this point where the window pops up with the number, do you know how to make a new text string? You are miles ahead of me so I hope you can help?

Link to comment
Share on other sites

You are miles ahead of me so I hope you can help?

 

Do not agreed with you

The Earth is so small, take a look on map -

just 5-6 thousand of miles or so :)

 

Here is edited version

This will create the new text entity with

the same properties as the first selected

text has

 

(defun C:STX (/ cpent elist en ip newtxt pt ss sum sumtxt txt)
(princ "\n\t\t>>>  Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
 ;; store the first text entity for using 'em further :
(setq cpent (ssname ss 0))
 ;; set initial sum to zero :
 (setq sum 0.)
 ;; loop trough selected texts/mtexts :
 (while
   ;; get the first text in selection :
   (setq en (ssname ss 0))
   ;; get entity list of them :
   (setq elist (entget en))
   ;; get the textstring by key 1 from entity list :
   (setq txt (cdr (assoc 1 elist)))
   ;; create output string :
   (setq sumtxt
   ;; concatenate strings :
   (strcat
     ;; convert digits to string :
     (rtos
       ;; add to summ the digital value of text :
       (setq sum (+ (atof txt) sum))
       ;; 2 is for metric units (3 for engineering) :
       2
       ;; set precision by current :
       (getvar "dimdec")))
  )
   ;; delete entity from selection set :
   (ssdel en ss)
   )
 ;; display message in the command line:
 (princ (strcat "\nSumm=" sumtxt))
 (setq pt (getpoint "\nSpecify the new text location: "))
 ;; get the insertion point of stored entity :
 (setq ip (cdr (assoc 10 (entget cpent))))
 ;; copy text entity to the new destination point :
 (command "_copy" cpent "" ip pt)
 ;; get the last created entity :
 (setq newtxt (entlast))
 ;; get entity list of them :
 (setq elist (entget newtxt))
 ;; modify entity list with new text string :
 (entmod (subst (cons 1 sumtxt)(assoc 1 elist) elist))
 ;; update changes :
 (entupd newtxt)
 )
)
(princ)
 )
(princ "\nStart command with STX...")
(princ)

 

 

~'J'~

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

hi!

i'm using this lisp but if it's possible i want an addition.

i want to sum texts with unit. for example:

i will select: 1, 2, and 3

it sums and creates a text which is: 6 m³/h or 6 Watt etc.

how can i do this?

 

Here is a quick example, hope this helps

 

(defun C:STX (/ elist en ss sum sumtxt txt)
(princ "\n\t\t>>>  Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
 ;; set initial sum to zero :
 (setq sum 0.)
 ;; loop trough selected texts/mtexts :
 (while
   ;; get the first text in selection :
   (setq en (ssname ss 0))
   ;; get entity list of them :
   (setq elist (entget en))
   ;; get the textstring by key 1 from entity list :
   (setq txt (cdr (assoc 1 elist)))
   ;; create output string :
   (setq sumtxt
   ;; concatenate strings :
   (strcat
     ;; convert digits to string :
     (rtos
       ;; add to summ the digital value of text :
       (setq sum (+ (atof txt) sum))
       ;; 2 is for metric units (3 for engineering) :
       2
       ;; set precision by current :
       (getvar "dimdec")))
  )
   ;; delete entity from selection set :
   (ssdel en ss)
   )
 ;; display message :
 (alert (strcat "\nSumm = " sumtxt))
 )
)
(princ)
 )
(princ "\nStart command with STX...")
(princ)

 

Sorry for my poor english :)

 

~'J'~

Link to comment
Share on other sites

Look near the end of the code for

;; display message :

and replace the line below with

 

 (setq ins (getpoint "\nSpecify text location: "))
 (entmake (list (cons 0 "text")
                    (cons 1 (strcat sumtxt "m3/h"));;text
                    (cons 7 "ROMANS");;text style
                    (cons 8 "acc");;textlayer
                    (cons 10 ins);;insertion point
                    (cons 11 ins)
                    (cons 40 2);;text height
                    (cons 62 256);;bylayer
                    (cons 72 1);;horizontal justification
                    )
             )

 

and replace

;; 2 is for metric units (3 for engineering) :
       2

with 5

 

by the way great routine fixo!

 

 

Why not put your name on it fixo?

Edited by Lt Dan's legs
additional info and question to fixo
Link to comment
Share on other sites

i'm sorry i couldn't make it. i changed the (setq ins ... code with your code but, it did not work. Could you add the whole code please? I will really appreciate it.

 

-i saw your changed message after my post sorry.

-yes i didn't want to see decimal part, so i changed "2" with so many numbers and catch the 5 (by luck) :D

-sorry for my poor English.

Link to comment
Share on other sites

(defun C:STX (/ elist en ss sum sumtxt txt ins)
;;created by Fixo
;;revised by Reid B. for sanalmakina
(princ "\n\t\t>>>  Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
 ;; set initial sum to zero :
 (setq sum 0.)
 ;; loop trough selected texts/mtexts :
 (while
   ;; get the first text in selection :
   (setq en (ssname ss 0))
   ;; get entity list of them :
   (setq elist (entget en))
   ;; get the textstring by key 1 from entity list :
   (setq txt (cdr (assoc 1 elist)))
   ;; create output string :
   (setq sumtxt
   ;; concatenate strings :
   (strcat
     ;; convert digits to string :
     (rtos
       ;; add to summ the digital value of text :
       (setq sum (+ (atof txt) sum))
       ;; 2 is for metric units (3 for engineering) :
       5;;revised by Reid B.
       ;; set precision by current :
       (getvar "dimdec")))
  )
   ;; delete entity from selection set :
   (ssdel en ss)
   )
 ;; Change to text :
   (setq ins (getpoint "\nSpecify text location: "));;start revision by Reid B.
 (entmake (list '(0 . "text")
                    (cons 100 "AcDbEntity")
                    (cons 100 "AcDbText")
                    (cons 1 (strcat sumtxt "m3/h"));;text
                    (cons 7 "ROMANS");;text style
                    (cons 8 "acc");;textlayer
                    (cons 10 ins);;insertion point
                    (cons 11 ins)
                    (cons 40 2);;text height
                    (cons 41 1);;text width
                    (cons 50 0);;text rotation
                    (cons 62 256);;bylayer
                    (cons 72 0);;horizontal justification
                    )
             );;end revision by Reid B.
 )
)
(princ)
 )
(princ "\nStart command with STX...")
(princ)

Edited by Lt Dan's legs
Link to comment
Share on other sites

(defun C:STX (/ elist en ss sum sumtxt txt ins)
;;created by Fixo
;;revised by Reid B. for sanalmakina
(princ "\n\t\t>>>  Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
 ;; set initial sum to zero :
 (setq sum 0.)
 ;; loop trough selected texts/mtexts :
 (while
   ;; get the first text in selection :
   (setq en (ssname ss 0))
   ;; get entity list of them :
   (setq elist (entget en))
   ;; get the textstring by key 1 from entity list :
   (setq txt (cdr (assoc 1 elist)))
   ;; create output string :
   (setq sumtxt
   ;; concatenate strings :
   (strcat
     ;; convert digits to string :
     (rtos
       ;; add to summ the digital value of text :
       (setq sum (+ (atof txt) sum))
       ;; 2 is for metric units (3 for engineering) :
       5;;revised by Reid B.
       ;; set precision by current :
       (getvar "dimdec")))
  )
   ;; delete entity from selection set :
   (ssdel en ss)
   )
 ;; Change to text :
   (setq ins (getpoint "\nSpecify text location: "));;start revision by Reid B.
 (entmake (list (cons 0 "text")
                    (cons 1 (strcat sumtxt "m3/h"));;text
                    (cons 7 "ROMANS");;text style
                    (cons 8 "acc");;textlayer
                    (cons 10 ins);;insertion point
                    (cons 11 ins)
                    (cons 40 2);;text height
                    (cons 41 1);;text width
                    (cons 50 0);;text rotation
                    (cons 62 256);;bylayer
                    (cons 72 0);;horizontal justification
                    )
             );;end revision by Reid B.
 )
)
(princ)
 )
(princ "\nStart command with STX...")
(princ)

 

Thank you Lt Dan's legs but, it doesn't work for me :s when i clicked for insertation, nothing happened.

Link to comment
Share on other sites

if I understand the OP correctly, you can simplify the deal:

 

[color=#8b4513];;;Given a true numeric string list[/color]
 [b][color=BLACK]([/color][/b]setq s1 [color=#2f4f4f]"3.4 5.6 7 8. 9"[/color][b][color=BLACK])[/color][/b]

[color=#8b4513];;;Format the string into an autolisp funtion[/color]
 [b][color=BLACK]([/color][/b]setq as [b][color=FUCHSIA]([/color][/b]strcat [color=#2f4f4f]"[b][color=NAVY]([/color][/b]+ "[/color] s1 [color=#2f4f4f]"[b][color=NAVY])[/color][/b]"[/color][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

[color=#8b4513];;;Evaluate the string[/color]
 [b][color=BLACK]([/color][/b]setq av [b][color=FUCHSIA]([/color][/b]eval [b][color=NAVY]([/color][/b]read as[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

[color=#8b4513];;;Contevrt the results into a string[/color]
 [b][color=BLACK]([/color][/b]setq ar [b][color=FUCHSIA]([/color][/b]rtos av[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

[color=#8b4513];;;Print the results[/color]
 [b][color=BLACK]([/color][/b]princ ar[b][color=BLACK])[/color][/b]

 

 

-David

Link to comment
Share on other sites

I think he means the following adds to meet his desire...

(defun C:STX (/ elist en ss sum sumtxt txt ins)
;;created by Fixo
;;revised by Reid B. for sanalmakina
(princ "\n\t\t>>>  Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
 ;; set initial sum to zero :
 (setq sum 0.)
 ;; loop trough selected texts/mtexts :
 (while
   ;; get the first text in selection :
   (setq en (ssname ss 0))
   ;; get entity list of them :
   (setq elist (entget en))
   ;; get the textstring by key 1 from entity list :
   (setq txt (cdr (assoc 1 elist)))
   ;; create output string :
   (setq sumtxt
   ;; concatenate strings :
   (strcat
     ;; convert digits to string :
     (rtos
       ;; add to summ the digital value of text :
       (setq sum (+ (atof txt) sum))
       ;; 2 is for metric units (3 for engineering) :
       5;;revised by Reid B.
       ;; set precision by current :
       (getvar "dimdec"))" " [color="red"]"m3/h"[/color])
  )
   ;; delete entity from selection set :
   (ssdel en ss)
   )
 ;; Change to text :
   (setq ins (getpoint "\nSpecify text location: "));;start revision by Reid B.
 (command "_.text" ins "" "" sumtxt)    
 )
)
(princ)
 )
(princ "\nStart command with STX...")
(princ)

Link to comment
Share on other sites

(defun C:STX (/ elist en ss sum sumtxt txt ins)
;;created by Fixo
;;revised by Reid B. for sanalmakina
(princ "\n\t\t>>>  Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
 ;; set initial sum to zero :
 (setq sum 0.)
 ;; loop trough selected texts/mtexts :
 (while
   ;; get the first text in selection :
   (setq en (ssname ss 0))
   ;; get entity list of them :
   (setq elist (entget en))
   ;; get the textstring by key 1 from entity list :
   (setq txt (cdr (assoc 1 elist)))
   ;; create output string :
   (setq sumtxt
   ;; concatenate strings :
   (strcat
     ;; convert digits to string :
     (rtos
       ;; add to summ the digital value of text :
       (setq sum (+ (atof txt) sum))
       ;; 2 is for metric units (3 for engineering) :
       5;;revised by Reid B.
       ;; set precision by current :
       (getvar "dimdec")))
  )
   ;; delete entity from selection set :
   (ssdel en ss)
   )
 ;; Change to text :
   (setq ins (getpoint "\nSpecify text location: "));;start revision by Reid B.
 (entmake (list '(0 . "text")
                    (cons 100 "AcDbEntity")
                    (cons 100 "AcDbText")
                    (cons 1 (strcat sumtxt "m3/h"));;text
                    (cons 7 "ROMANS");;text style
                    (cons 8 "acc");;textlayer
                    (cons 10 ins);;insertion point
                    (cons 11 ins)
                    (cons 40 2);;text height
                    (cons 41 1);;text width
                    (cons 50 0);;text rotation
                    (cons 62 256);;bylayer
                    (cons 72 0);;horizontal justification
                    )
             );;end revision by Reid B.
 )
)
(princ)
 )
(princ "\nStart command with STX...")
(princ)

 

i tried but it didn't work :(. command text:

 

Command: appload

asd.lsp successfully loaded.

asd.lsp successfully loaded.

 

 

Command:

Start command with STX...

Command:

Start command with STX...

Command:

Command: text

Current text style: "K2000Style" Text height: 0.1000 Annotative: No

Specify start point of text or [Justify/Style]:

Specify rotation angle of text :

Command: Specify opposite corner:

Command: c COPY 1 found

 

Current settings: Copy mode = Multiple

Specify base point or [Displacement/mOde] : Specify second point

or :

Specify second point or [Exit/Undo] : *Cancel*

 

Command: stx

>>> Select text to get summ >>>

Select objects: 1 found

 

Select objects: 1 found' date=' 2 total

 

Select objects:

 

Specify text location:[/quote']

 

I think he means the following adds to meet his desire...

(defun C:STX (/ elist en ss sum sumtxt txt ins)
;;created by Fixo
;;revised by Reid B. for sanalmakina
(princ "\n\t\t>>>  Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
 ;; set initial sum to zero :
 (setq sum 0.)
 ;; loop trough selected texts/mtexts :
 (while
   ;; get the first text in selection :
   (setq en (ssname ss 0))
   ;; get entity list of them :
   (setq elist (entget en))
   ;; get the textstring by key 1 from entity list :
   (setq txt (cdr (assoc 1 elist)))
   ;; create output string :
   (setq sumtxt
   ;; concatenate strings :
   (strcat
     ;; convert digits to string :
     (rtos
       ;; add to summ the digital value of text :
       (setq sum (+ (atof txt) sum))
       ;; 2 is for metric units (3 for engineering) :
       5;;revised by Reid B.
       ;; set precision by current :
       (getvar "dimdec"))" " [color="red"]"m3/h"[/color])
  )
   ;; delete entity from selection set :
   (ssdel en ss)
   )
 ;; Change to text :
   (setq ins (getpoint "\nSpecify text location: "));;start revision by Reid B.
 (command "_.text" ins "" "" sumtxt)    
 )
)
(princ)
 )
(princ "\nStart command with STX...")
(princ)

 

it writes it to command bar, not to the drawing:(.

 

command text:

 

Current text style: "K2000Style" Text height: 0.1000 Annotative: No

Specify start point of text or [Justify/Style]:

Specify rotation angle of text :

Command:

Command: c COPY 1 found

 

Current settings: Copy mode = Multiple

Specify base point or [Displacement/mOde] : Specify second point

or :

Specify second point or [Exit/Undo] : *Cancel*

 

Command: stx

>>> Select text to get summ >>>

Select objects: 1 found

 

Select objects: 1 found, 2 total

 

Select objects:

 

Specify text location: _.text

Current text style: "K2000Style" Text height: 0.1000 Annotative: No

Specify start point of text or [Justify/Style]:

Specify rotation angle of text :

Enter text:

Command: 246246 m3/h Unknown command "246246 M3/H". Press F1 for help.

 

Command:

 

Link to comment
Share on other sites

(cons 7 "ROMANS");;text style

You are relying on this textstyle actually existing.

Replace with

(cons 7 (getvar 'textstyle))

Link to comment
Share on other sites

The problem is with the Text Style which is current , I mean put the Text height for it zero and try again.
No, use entmake. Text in command is the most temperamental function that can be called.
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...