×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

I need a lisp to calculate numbers

I need a lisp to calculate numbers

I need a lisp to calculate numbers

(OP)
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

RE: I need a lisp to calculate numbers

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

RE: I need a lisp to calculate numbers

(OP)
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

RE: I need a lisp to calculate numbers

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) 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources