+ Reply to Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 19 of 19
  1. #11
    Full Member
    Discipline
    Electrical
    Using
    AutoCAD 2016
    Join Date
    Apr 2017
    Posts
    56

    Default

    Registered forum members do not see this ad.

    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.

  2. #12
    Senior Member
    Using
    not applicable
    Join Date
    Oct 2014
    Posts
    152

    Default

    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:

    Code:
    ;******************************************************************
    ;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)

  3. #13
    Full Member
    Discipline
    Electrical
    Using
    AutoCAD 2016
    Join Date
    Apr 2017
    Posts
    56

    Default

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

  4. #14
    Senior Member
    Using
    not applicable
    Join Date
    Oct 2014
    Posts
    152

    Default

    Quote Originally Posted by ryankevin15 View Post
    Thanks! How do I get it to do that without the decimal point?
    Change the "1" to a "0" here:

    Code:
    ; RTOC Comma code by: John Uhden (Autodesk Community Forums)
    
         (progn
           (setq num total)
           (setq # 0)
           (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#)
               )
             )
    BricsCAD V15

  5. #15
    Full Member
    Discipline
    Electrical
    Using
    AutoCAD 2016
    Join Date
    Apr 2017
    Posts
    56

    Default

    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?

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

  6. #16
    Full Member
    Discipline
    Electrical
    Using
    AutoCAD 2016
    Join Date
    Apr 2017
    Posts
    56

    Default

    Quote Originally Posted by SLW210 View Post
    Using Code Tags will remove the smiley faces.
    Fixed, finally understood what the problem was. haha.

  7. #17
    Senior Member
    Using
    not applicable
    Join Date
    Oct 2014
    Posts
    152

    Default

    This should work. I added a function that rounds up at 0.5 or greater since you're dividing by 4.
    Code:
    (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)
    BricsCAD V15

  8. #18
    Full Member
    Discipline
    Electrical
    Using
    AutoCAD 2016
    Join Date
    Apr 2017
    Posts
    56

    Default



    Using these all on a daily basis. Thanks!!

  9. #19
    Senior Member
    Using
    not applicable
    Join Date
    Oct 2014
    Posts
    152

    Default

    Registered forum members do not see this ad.

    Good to hear! Glad to have been of help.
    BricsCAD V15

Similar Threads

  1. Electrical Homerun Lisp
    By caditnow in forum AutoLISP, Visual LISP & DCL
    Replies: 10
    Last Post: 11th Sep 2015, 02:11 am
  2. Useful lisp for electrical engineers
    By hosyn in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 22nd May 2013, 01:40 pm
  3. Tips for improving workflow.
    By spittle in forum SolidWorks
    Replies: 13
    Last Post: 15th Jan 2009, 04:37 pm
  4. Improving my cad skills
    By Faceless Master in forum AutoCAD Beginners' Area
    Replies: 1
    Last Post: 26th Aug 2006, 10:51 am

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts