Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need a lisp to calculate numbers

Status
Not open for further replies.

troydawes

Civil/Environmental
Dec 19, 2012
2
I am trying to find a lisp that I can use to select 2 entities of text that contain numbers, and have it create a new text with the sum of the selected.

example: I want to be able to select the text entities that contain 15 & 5, then pick a point a point anywhere, like near 20 and have it create text with the sum of the 2 selected numbers. It would be nice if it would create the new text on the current layer, text style, color, ect. (or match the text selected, as long as they match each other)

15
|
-----20
|
5

Thank you for your time,
Troy Dawes
 
Replies continue below

Recommended for you

Not too difficult, depending..
-Text and/or mtext entities?
-do numbers have prefix or suffix, such as "ft"
-are any numbers in architectural format, say ft & in
 
I found one that will add text only, not mtext. Currently there is no need for any text other than the numbers.

The last time I wrote a lisp was in 1995, not sure how to edit this to be able to have this work with mtext.

Here is what I have:

;******************************************************************
;Add.lsp 6/17/96
;v1.01 7/2/96 fixed to use local variables
;v1.02 1/15/97 Cleaned up commented out code. Added more instructions.
;v1.03 5/12/00 sets LUPREC to 0 decimal places. Should improve to allow other values with 0 as default.
;*****************************************************************
;Add string lisp routine will take a selection of text and add the
;numbers together.
;Use:
;Select some stuff. All non-text items are ignored. Any numbers in
;the selected text will be added together. Select a text item to be
;updated. The selected text item will be replaced with the result of
;the addition.
;
;Note that the units command will affect the format of the results.
;If you get a number will a bunch of trailing 0's then change units
;to fix the problem.
;
; * Copyright 1996 by J. Marsden DeLapp *
;*************************************************************
;dxf function takes an integer dxf code & ent data list and
;returns the value for that dxf code.
(defun dxf (code elist)
(cdr (assoc code elist))
);defun
;*************************************************************
;ss1 - selection set
;n - number of items in selection set (counter)
;total - total of float numbers in selection set
;e -
;
(defun C:Add ( / ed en et i n oluprec ss1 text1 total)
(setq OLUPREC (getvar "LUPREC"))
(setvar "LUPREC" 0);set precision to 0.0 decimal places
(setq ss1 (ssget '((0 . "TEXT")))) ; Select objects, only text
(if ss1 ; If any objects selected
(progn
(setq i 0
total 0
n (sslength ss1)); reset tot, set n to number of items
(while (< i n) ; For each selected object...
(setq text1 (cdr (assoc 1 (setq e (entget (ssname ss1 i))))))
(setq total (+ total (atof text1)))
(setq i (1+ i)) ; increment counter
);while
);progn
);if
(princ "Total ")
(princ total)
(terpri)
(setq en (car (entsel "\nSelect text to update to total"))
ed (entget en)
et (dxf 0 ed)
)
(if (= et "TEXT") ; verify text was selected
;(rtos total 2) returns total formated as a string in decimal format
;substitute the new text for the old text...
(progn
(entmod
(setq ed (subst (cons 1 (rtos total 2)) (assoc 1 ed) ed))
);entmod
)
);if
(setvar "LUPREC" oluprec);reset precision
(princ)
);defun
(PRINC "\n ADD.LSP v1.03 Copyright (c) 1997-2000 by J. Marsden DeLapp")(princ)

Thanks for your time,
Troy
 
It took just 2 edits to allow mtext to be accepted. But, will not work on mtext that has any special formatting, as the format characters are combined with the numbers in the entity data that is used.
I also remove (commented out) the 3 lines that had to do with setting results to zero decimal places, so instead it displays number of decimals currently est with the "units" command.

Code:
;******************************************************************
 ;Add.lsp 6/17/96
 ;v1.01 7/2/96 fixed to use local variables
 ;v1.02 1/15/97 Cleaned up commented out code. Added more instructions.
 ;v1.03 5/12/00 sets LUPREC to 0 decimal places. Should improve to allow other values with 0 as default.
 ;v1.05 12/19/12 edits to allow mtext objects as well as text. CarlB Eng-Tips
 ;*****************************************************************
 ;Add string lisp routine will take a selection of text and add the
 ;numbers together.
 ;Use:
 ;Select some stuff. All non-text items are ignored. Any numbers in
 ;the selected text will be added together. Select a text item to be
 ;updated. The selected text item will be replaced with the result of
 ;the addition.
 ;
 ;Note that the units command will affect the format of the results.
 ;If you get a number will a bunch of trailing 0's then change units
 ;to fix the problem.
 ; 
; * Copyright 1996 by J. Marsden DeLapp *
 ;*************************************************************
 ;dxf function takes an integer dxf code & ent data list and
 ;returns the value for that dxf code.
 (defun dxf (code elist)
 (cdr (assoc code elist))
 );defun
 ;*************************************************************
 ;ss1 - selection set
 ;n - number of items in selection set (counter)
 ;total - total of float numbers in selection set
 ;e - 
;
(defun C:Add ( / ed en et i n oluprec ss1 text1 total)
   ;(setq OLUPREC (getvar "LUPREC"))
   ;(setvar "LUPREC" 0);set precision to 0.0 decimal places
   (setq ss1 (ssget '((0 . "TEXT,MTEXT")))) ; Select objects, only text
   (if ss1 ; If any objects selected
     (progn 
       (setq i 0 
             total 0 
             n (sslength ss1)); reset tot, set n to number of items
       (while (< i n) ; For each selected object...
          (setq text1 (cdr (assoc 1 (setq e (entget (ssname ss1 i))))))
          (setq total (+ total (atof text1)))
          (setq i (1+ i)) ; increment counter
       );while
     );progn
   );if
   (princ "Total ") 
   (princ total)
   (terpri)
   (setq en (car (entsel "\nSelect text to update to total"))
         ed (entget en) 
         et (dxf 0 ed)
   )
   (if (or (= et "TEXT")(= et "MTEXT")) ; verify text or mtext was selected  **12/20/12**
   ;(rtos total 2) returns total formated as a string in decimal format
   ;substitute the new text for the old text...
      (progn
         (entmod
            (setq ed (subst (cons 1 (rtos total 2)) (assoc 1 ed) ed))
         );entmod
      )
    );if
   ;(setvar "LUPREC" oluprec);reset precision
   (princ)
);defun
(PRINC "\n ADD.LSP v1.03 Copyright (c) 1997-2000 by J. Marsden DeLapp")(princ)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor