Jump to content
ryankevin15

Help With Improving Electrical LISP

Recommended Posts

ryankevin15

Hello,

 

Below is a LISP for calculating the amps based on a wattage

 

A95RDo5.png

 

The LISP works by clicking the wattage number and then the amperage number and after already updating the wattage total. The updated amperage will just overwrite the current number.

 

However, when you accidentally miss a piece of text, it executes the command anyway. Or, if you select a line it finishes the command. Also, if there is a comma, say XX,XXX, then it cannot compute the correct total.

 

Here is the code for the '208' LISP:


(defun c:208 (/ watts amps ent)

 (while (= watts nil)
   (setq watts (car (entsel "\nSelect Demand Watts: ")))
 )

(setq watts (cdr (assoc 1 (entget watts)))
     amps (rtos (/ (atof watts) 360) 2 1)
)

 (while (= ent nil)
   (setq ent (car (entsel "\nSelect Target Entity: ")))
 )

(setq ent (subst (cons 1 amps) (assoc 1 (entget ent)) (entget ent)))

(entmod ent)

(princ)

)

 

And, here is the ADD LISP that works by adding up any selection of text, with or without commas, and will keep prompting the user for a selection, saying '0 items selected' until the user selects a piece of text. It works with commas as well. I just want to take the '208' LISP and make sure it has some of the those capabilities that the ADD LISP. Just needs a little fine tuning.

 


;******************************************************************
;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 10/3/06 revised the program to work with numbers that include commas.
;v1.04 11/20/06 revised the program to stop adding zeros on the end.
;*****************************************************************
;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.
;
;*************************************************************
; below is the subroutine that removes the commas from the numbers and changes decimal points
; added by me

(defun comma ( / nc nt m tmp ntext )

 (setq nc 0)                   ;set counter to 0
 (setq m T)                    ;set m to true
 (setq ntext "")               ;blank out ntext
 (setq nt (strlen text1))      ;initalize counter (text1 from calling program)

 ; while loop test for ascii decimal point or number between 0 and 9 and eliminates none decimal or number char.

 (while m
   (setq nc (+ nc 1))
   (setq tmp (ascii (substr text1 nc 1)))
   (if (or (= tmp 46) (and (>= tmp 48) (<= tmp 57)))
     (setq ntext (strcat ntext (substr text1 nc 1)))
   )
   (if (= tmp 46)          ;this will adjust the decimal point to 1 if a decimal is present in the number.
     (setvar "luprec" 1)
   )
 (setq nt (- nt 1))
 (if (= nt 0) (setq m F))
 )

(setq text1 ntext)
)
ntext

;below is the original program
;end of by me


;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     -
;au    - current unit precision

(defun C:A ( / lu ed en et i n ss1 text1 total )

 ;added by me to remove the trailing zeros
 (setq lu (getvar "luprec")) ; save current precision
 (setvar "luprec" 0) ; set precision to 0


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

           ;below is the subroutine call to remove the commas

           (comma)


           (setq total (+ total (atof text1)))
           (setq i (1+ i))          ; increment counter
        );while
     );progn
  );if
  (princ "Total ")
  (princ total)
  (terpri)
  (setq en (entsel "\nSelect text to update to total")
        ed  (entget (car 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" lu) ;return precision to it's original value
(princ)
);defun
(princ)

Edited by ryankevin15

Share this post


Link to post
Share on other sites
BIGAL

Check this in your code I got a smiley face (>= tmp 4:) (

 

Would it not be easier to use a table rather than text and use formulas like excel.

Share this post


Link to post
Share on other sites
BKT

Interesting. I was jumping back and forth between ACAD and BricsCAD and couldn't get your ADD program to show "0 items selected" and keep prompting for text selection while in BricsCAD. Try this and let me know what it does. I kept your comma code in this, although you don't show any commas in your example...

 

(princ "\nEnter \"208\" To Start Program.")

(defun comma ( / nc nt m tmp ntext )

 (setq nc 0)                   ;set counter to 0
 (setq m T)                    ;set m to true
 (setq ntext "")               ;blank out ntext
 (setq nt (strlen text1))      ;initalize counter (text1 from calling program)

 ; while loop test for ascii decimal point or number between 0 and 9 and eliminates none decimal or number char.

 (while m
   (setq nc (+ nc 1))
   (setq tmp (ascii (substr text1 nc 1)))
   (if (or (= tmp 46) (and (>= tmp 48) (<= tmp 57)))
     (setq ntext (strcat ntext (substr text1 nc 1)))
   )
   (if (= tmp 46)          ;this will adjust the decimal point to 1 if a decimal is present in the number.
     (setvar "luprec" 1)
   )
 (setq nt (- nt 1))
 (if (= nt 0) (setq m F))
 )

(setq text1 ntext)
)
ntext

;below is the original program
;end of by me


;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     -
;au    - current unit precision

(defun c:208 (/ en1 en2 ed1 ed2 et1 et2 watts amps ent text1)

 ;added by me to remove the trailing zeros
 (setq lu (getvar "luprec")) ; save current precision
 (setvar "luprec" 0) ; set precision to 0

;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------

(while
 (not
   (and
     (setq
       en1 (entsel "\nSelect DEMAND WATTS: ")
       ed1 (if en1 (entget (car en1)))
       et1 (cdr (assoc 0 ed1))
     )
     (= et1 "TEXT")
   )
 )
 (prompt "\n-- 0 items selected, or not TEXT --")
)

(setq watts (cdr (assoc 1 ed1))
     text1 watts
)

(comma)

(setq amps (rtos (/ (atof text1) 360) 2 1))

(princ (strcat "\nTOTAL DEMAND AMPS = " amps))

(while
 (not
   (and
     (setq
       en2 (entsel "\nSelect DEMAND AMPS: ")
       ed2 (if en2 (entget (car en2)))
       et2 (cdr (assoc 0 ed2))
     )
     (= et2 "TEXT")
   )
 )
 (prompt "\n-- 0 items selected, or not TEXT --")
)

(entmod (setq ed2 (subst (cons 1 amps) (assoc 1 ed2) ed2)))

;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------

(setvar "luprec" lu) ;return precision to it's original value
(princ)
);defun
(princ)

Share this post


Link to post
Share on other sites
ryankevin15

Sometimes there is a comma sometimes there is not. It really just depends on who was working on it. Thank you for your help.

Share this post


Link to post
Share on other sites
SLW210

Please read the Code Posting Guidelines and edit your Code to be included in Code Tags (not Quote Tags).[NOPARSE]

Your Code Here[/NOPARSE]

=

Your Code Here

Share this post


Link to post
Share on other sites
ryankevin15

Sorry about that. Thanks for the tip!

Share this post


Link to post
Share on other sites
ryankevin15
Check this in your code I got a smiley face (>= tmp 4:) (

 

Would it not be easier to use a table rather than text and use formulas like excel.

 

It would be easier to link an excel file, yes. But, only for us. It looks better, and is easier to read in CAD.

Share this post


Link to post
Share on other sites
SLW210

Using Code Tags will remove the smiley faces.

Share this post


Link to post
Share on other sites
BKT
It would be easier to link an excel file, yes. But, only for us. It looks better, and is easier to read in CAD.

OP, did you try the code I posted in #3?

Share this post


Link to post
Share on other sites
ryankevin15
OP, did you try the code I posted in #3?

 

Oh, wow just tried it. Works great! Everything I needed. Thank you.

Share this post


Link to post
Share on other sites
ryankevin15

Would it be possible to make a similar command to the ADD command that will put in a comma for the answer? Maybe call it AddC.

Share this post


Link to post
Share on other sites
BKT

There's a nice piece of code written by John Uhden over on the Autodesk Community Forums that puts commas into strings. I've integrated his code into the ADD program here. Give it a try:

 

;******************************************************************
;AddC.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 10/3/06 revised the program to work with numbers that include commas.
;v1.04 11/20/06 revised the program to stop adding zeros on the end.
;*****************************************************************
;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.
;
;*************************************************************
; below is the subroutine that removes the commas from the numbers and changes decimal points
; added by me

(princ "\nEnter \"addc\" To Start Program.")

(defun comma ( / nc nt m tmp ntext )

 (setq nc 0)                   ;set counter to 0
 (setq m T)                    ;set m to true
 (setq ntext "")               ;blank out ntext
 (setq nt (strlen text1))      ;initalize counter (text1 from calling program)

 ; while loop test for ascii decimal point or number between 0 and 9 and eliminates none decimal or number char.

 (while m
   (setq nc (+ nc 1))
   (setq tmp (ascii (substr text1 nc 1)))
   (if (or (= tmp 46) (and (>= tmp 48) (<= tmp 57)))
     (setq ntext (strcat ntext (substr text1 nc 1)))
   )
   (if (= tmp 46)          ;this will adjust the decimal point to 1 if a decimal is present in the number.
     (setvar "luprec" 1)
   )
 (setq nt (- nt 1))
 (if (= nt 0) (setq m F))
 )

(setq text1 ntext)
)
ntext

;below is the original program
;end of by me


;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     -
;au    - current unit precision

(defun C:addc ( / lu ed en et i n num ss1 text1 total # p#)

 ;added by me to remove the trailing zeros
 (setq lu (getvar "luprec")) ; save current precision
 (setvar "luprec" 0) ; set precision to 0


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

           ;below is the subroutine call to remove the commas

           (comma)


           (setq total (+ total (atof text1)))
           (setq i (1+ i))          ; increment counter
        );while
     );progn
  );if
  (princ "Total ")
  (princ total)
  (terpri)
  (setq en (entsel "\nSelect text to update to total")
        ed  (entget (car 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...


;*************************************************************
; RTOC Comma code by: John Uhden (Autodesk Community Forums)

    (progn
      (setq num total)
      (setq # 1)
      (setq num (rtos num 2 #) # 1)
        (while (and (/= (substr num # 1) ".")(<= # (strlen num)))
          (setq # (1+ #))
        )
      (setq # (1- #) p# #)
        (if (= (setq # (rem # 3)) 0)(setq # 3))
        (while (< # p#)
          (setq num (strcat (substr num 1 #) "," (substr num (1+ #)))
                # (+ 4 #)
                p# (1+ p#)
          )
        )

;*************************************************************

      (entmod (setq ed (subst (cons 1 num) (assoc 1 ed) ed)))
    )

  );if

(setvar "luprec" lu) ;return precision to it's original value
(princ)
);defun
(princ)

Share this post


Link to post
Share on other sites
ryankevin15

Thanks! How do I get it to do that without the decimal point?

Share this post


Link to post
Share on other sites
BKT
Thanks! How do I get it to do that without the decimal point?

 

Change the "1" to a "0" here:

 

; RTOC Comma code by: John Uhden (Autodesk Community Forums)

    (progn
      (setq num total)
      [color=red](setq # 0)[/color]
      (setq num (rtos num 2 #) # 1)
        (while (and (/= (substr num # 1) ".")(<= # (strlen num)))
          (setq # (1+ #))
        )
      (setq # (1- #) p# #)
        (if (= (setq # (rem # 3)) 0)(setq # 3))
        (while (< # p#)
          (setq num (strcat (substr num 1 #) "," (substr num (1+ #)))
                # (+ 4 #)
                p# (1+ p#)
          )
        )

Share this post


Link to post
Share on other sites
ryankevin15

So, this is the code to find 25% continuous lighting load. Could you help make it to where it will round to the nearest whole number and insert a comma?

 

(princ "\nEnter \"208\" To Start Program.")

(defun comma ( / nc nt m tmp ntext )

 (setq nc 0)                   ;set counter to 0
 (setq m T)                    ;set m to true
 (setq ntext "")               ;blank out ntext
 (setq nt (strlen text1))      ;initalize counter (text1 from calling program)

 ; while loop test for ascii decimal point or number between 0 and 9 and eliminates none decimal or number char.

 (while m
   (setq nc (+ nc 1))
   (setq tmp (ascii (substr text1 nc 1)))
   (if (or (= tmp 46) (and (>= tmp 48) (<= tmp 57)))
     (setq ntext (strcat ntext (substr text1 nc 1)))
   )
   (if (= tmp 46)          ;this will adjust the decimal point to 1 if a decimal is present in the number.
     (setvar "luprec" 1)
   )
 (setq nt (- nt 1))
 (if (= nt 0) (setq m F))
 )

(setq text1 ntext)
)
ntext

;below is the original program
;end of by me


;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     -
;au    - current unit precision

(defun c:25 (/ en1 en2 ed1 ed2 et1 et2 watts amps ent text1)

 ;added by me to remove the trailing zeros
 (setq lu (getvar "luprec")) ; save current precision
 (setvar "luprec" 0) ; set precision to 0

;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------

(while
 (not
   (and
     (setq
       en1 (entsel "\nSelect TOTAL LIGHTING LOAD: ")
       ed1 (if en1 (entget (car en1)))
       et1 (cdr (assoc 0 ed1))
     )
     (= et1 "TEXT")
   )
 )
 (prompt "\n-- 0 items selected, or not TEXT --")
)

(setq watts (cdr (assoc 1 ed1))
     text1 watts
)

(comma)

(setq amps (rtos (/ (atof text1) 4) 2 1))

(princ (strcat "\n25% LIGHTING LOAD = " watts))

(while
 (not
   (and
     (setq
       en2 (entsel "\nSelect DEMAND AMPS: ")
       ed2 (if en2 (entget (car en2)))
       et2 (cdr (assoc 0 ed2))
     )
     (= et2 "TEXT")
   )
 )
 (prompt "\n-- 0 items selected, or not TEXT --")
)

(entmod (setq ed2 (subst (cons 1 amps) (assoc 1 ed2) ed2)))

;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------

(setvar "luprec" lu) ;return precision to it's original value
(princ)
);defun
(princ)

Share this post


Link to post
Share on other sites
ryankevin15
Using Code Tags will remove the smiley faces.

 

Fixed, finally understood what the problem was. haha.

Share this post


Link to post
Share on other sites
BKT

This should work. I added a function that rounds up at 0.5 or greater since you're dividing by 4.

(princ "\nEnter \"25\" To Start Program.")

(defun comma ( / nc nt m tmp ntext )

 (setq nc 0)                   ;set counter to 0
 (setq m T)                    ;set m to true
 (setq ntext "")               ;blank out ntext
 (setq nt (strlen text1))      ;initalize counter (text1 from calling program)

 ; while loop test for ascii decimal point or number between 0 and 9 and eliminates none decimal or number char.

 (while m
   (setq nc (+ nc 1))
   (setq tmp (ascii (substr text1 nc 1)))
   (if (or (= tmp 46) (and (>= tmp 48) (<= tmp 57)))
     (setq ntext (strcat ntext (substr text1 nc 1)))
   )
   (if (= tmp 46)          ;this will adjust the decimal point to 1 if a decimal is present in the number.
     (setvar "luprec" 1)
   )
 (setq nt (- nt 1))
 (if (= nt 0) (setq m F))
 )

(setq text1 ntext)
)
ntext

;below is the original program
;end of by me


;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     -
;au    - current unit precision

(defun c:25 (/ en1 en2 ed1 ed2 et1 et2 num rndamps watts amps text1)

 ;added by me to remove the trailing zeros
 (setq lu (getvar "luprec")) ; save current precision
 (setvar "luprec" 0) ; set precision to 0

;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------

(while
 (not
   (and
     (setq
       en1 (entsel "\nSelect TOTAL LIGHTING LOAD: ")
       ed1 (if en1 (entget (car en1)))
       et1 (cdr (assoc 0 ed1))
     )
     (= et1 "TEXT")
   )
 )
 (prompt "\n-- 0 items selected, or not TEXT --")
)

(setq watts (cdr (assoc 1 ed1))
     text1 watts
)

(comma)

(setq amps (rtos (/ (atof text1) 4) 2 2))

(princ (strcat "\n25% LIGHTING LOAD = " watts))

(while
 (not
   (and
     (setq
       en2 (entsel "\nSelect DEMAND AMPS: ")
       ed2 (if en2 (entget (car en2)))
       et2 (cdr (assoc 0 ed2))
     )
     (= et2 "TEXT")
   )
 )
 (prompt "\n-- 0 items selected, or not TEXT --")
)

(setq amps (/ (atof text1) 4))
(if (< (- amps (fix amps)) 0.5)(setq rndamps (fix amps))(setq rndamps (1+ (fix amps)))) ;Rounding
(setq rndamps (rtos rndamps 2 0))

  (if (= et2 "TEXT")

;*************************************************************
; RTOC Comma code by: John Uhden (Autodesk Community Forums)
; Changing the "#" from 1 to 0 removes the decimal place.

    (progn
      (setq # 1)
      (setq num rndamps)
        (while (and (/= (substr num # 1) ".")(<= # (strlen num)))
          (setq # (1+ #))
        )
      (setq # (1- #) p# #)
        (if (= (setq # (rem # 3)) 0)(setq # 3))
        (while (< # p#)
          (setq num (strcat (substr num 1 #) "," (substr num (1+ #)))
                # (+ 4 #)
                p# (1+ p#)
          )
        )

;*************************************************************

(entmod (setq ed2 (subst (cons 1 num) (assoc 1 ed2) ed2)))

    )
  )

;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------

(setvar "luprec" lu) ;return precision to it's original value
(princ)
);defun
(princ)

Share this post


Link to post
Share on other sites
ryankevin15

xWSSCRJ.png

 

Using these all on a daily basis. Thanks!!

Share this post


Link to post
Share on other sites
BKT

Good to hear! Glad to have been of help.

:)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×