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

    Default Help With Improving Electrical LISP

    Registered forum members do not see this ad.

    Hello,

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



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

    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 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)
    Last edited by ryankevin15; 11th May 2017 at 07:36 pm.

  2. #2
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,885

    Default

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

    Would it not be easier to use a table rather than text and use formulas like excel.
    A man who never made mistakes never made anything

  3. #3
    Senior Member
    Using
    not applicable
    Join Date
    Oct 2014
    Posts
    151

    Default

    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...

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

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

    Default

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

  5. #5
    Super Moderator SLW210's Avatar
    Computer Details
    SLW210's Computer Details
    Operating System:
    Windows 7 PRO 64-bit
    Computer:
    IBM Lenovo
    Motherboard:
    ACPI x64
    CPU:
    Pentium(R) i5 4570 @ 3.2GHz
    RAM:
    8 GB RAM
    Graphics:
    Nvidia Quadro 600 1GB
    Primary Storage:
    300 GB
    Secondary Storage:
    650GB
    Monitor:
    2x ThinkVision 24"
    Discipline
    Multi-disciplinary
    SLW210's Discipline Details
    Occupation
    Design Draftsman
    Discipline
    Multi-disciplinary
    Details
    Mostly do drafting related to manufacturing. From doing site layouts with proposed updates, additions and renovations to be budgeted and submitted for bid, to updating and changing existing drawings to reflect maintenance and repair/revision work done on site.
    Using
    AutoCAD 2011
    Join Date
    May 2007
    Location
    South Florida, USA
    Posts
    11,819

    Default

    Please read the Code Posting Guidelines and edit your Code to be included in Code Tags (not Quote Tags).[CODE]Your Code Here[/CODE] =
    Code:
    Your Code Here
    “A narrow mind and a fat head invariably come on the same person” Zig Zigler



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

    Default

    Sorry about that. Thanks for the tip!

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

    Default

    Quote Originally Posted by BIGAL View Post
    Check this in your code I got a smiley face (>= tmp 4 (<= tmp 57)))

    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.

  8. #8
    Super Moderator SLW210's Avatar
    Computer Details
    SLW210's Computer Details
    Operating System:
    Windows 7 PRO 64-bit
    Computer:
    IBM Lenovo
    Motherboard:
    ACPI x64
    CPU:
    Pentium(R) i5 4570 @ 3.2GHz
    RAM:
    8 GB RAM
    Graphics:
    Nvidia Quadro 600 1GB
    Primary Storage:
    300 GB
    Secondary Storage:
    650GB
    Monitor:
    2x ThinkVision 24"
    Discipline
    Multi-disciplinary
    SLW210's Discipline Details
    Occupation
    Design Draftsman
    Discipline
    Multi-disciplinary
    Details
    Mostly do drafting related to manufacturing. From doing site layouts with proposed updates, additions and renovations to be budgeted and submitted for bid, to updating and changing existing drawings to reflect maintenance and repair/revision work done on site.
    Using
    AutoCAD 2011
    Join Date
    May 2007
    Location
    South Florida, USA
    Posts
    11,819

    Default

    Using Code Tags will remove the smiley faces.
    “A narrow mind and a fat head invariably come on the same person” Zig Zigler



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

    Default

    Quote Originally Posted by ryankevin15 View Post
    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?

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

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by BKT View Post
    OP, did you try the code I posted in #3?
    Oh, wow just tried it. Works great! Everything I needed. Thank you.

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