Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: My first lisp function...

  1. #1
    100 Club
    Join Date
    2015-10
    Location
    Vancouver Island
    Posts
    107
    Login to Give a bone
    0

    Default My first lisp function... Need help updated routine.

    Can I get some feedback on this, I have not tried to run it yet, I want people who actually know what they are doing to look it over.

    It will ask what layer for the pipe to be inserted on - RR RS Eff and FT, then ask for the diameter, then the length.

    any help is appreciated to troubleshoot this, I updated the routine from the last time.
    To be honest i'm not sure how to even run it.. I ran appload and loaded the file, but I just get command "Pipe".

    so at this point I'm not sure how to even troubleshoot it....

    I uploaded the lisp routine reformated with VisualLisp for AutoCAD.
    Attached Files Attached Files
    Last edited by JLHConsulting; 2010-06-07 at 05:30 AM.

  2. #2
    AUGI Addict
    Join Date
    2006-04
    Location
    (getpoint "Anywhere on the Enter Key =>")
    Posts
    1,160
    Login to Give a bone
    0

    Default Re: My first lisp function...

    Instead of putting your defined variables in another, you have to separate your variables in different lines:
    Code:
    (setq PI-Layer ...)
    (setq oldlayer ...)
    ...
    Hope this helps.

  3. #3
    I could stop if I wanted to kpblc2000's Avatar
    Join Date
    2006-09
    Posts
    212
    Login to Give a bone
    0

    Default Re: My first lisp function...

    There is no function definition:
    Code:
    pipel
    And what aboun varaibles like rr, rs, ef, ft etc?
    When you're using
    Code:
    (setq pipediameter
             '((0.5 "0.840")
               (0.75 "1.050")
               (1 "1.315")
               (1.25 "1.660")
               (1.5 "1.900")
               (2 "2.375")
               (2.5 "2.875")
               (3 "3.500")
               (4 "4.500")
               (5 "5.563")
               (6 "6.625")
               (8 "8.625")
               (10 "10.750")
               (12 "12.750")
               (14 "14")
               (16 "16")
               (18 "18")
               (20 "20")
               (22 "22")
               (24 "24")
               )
            )
    string like
    Code:
    (member "Pipesize" pipediameter)
    will return nil. Always.
    I tried to write code, test it:
    Code:
    (vl-load-com)
    
    (defun pipe (/ *error* lst layer size len ins res obj_layer)
      (defun *error* (msg)
        (vla-endundomark adoc)
        (princ msg)
        (princ)
        ) ;_ end of defun
      (setq lst '((0.5 . 0.840)
                  (0.75 . 1.050)
                  (1 . 1.315)
                  (1.25 . 1.660)
                  (1.5 . 1.900)
                  (2 . 2.375)
                  (2.5 . 2.875)
                  (3 . 3.500)
                  (4 . 4.500)
                  (5 . 5.563)
                  (6 . 6.625)
                  (8 . 8.625)
                  (10 . 10.750)
                  (12 . 12.750)
                  (14 . 14)
                  (16 . 16)
                  (18 . 18)
                  (20 . 20)
                  (22 . 22)
                  (24 . 24)
                  )
            ) ;_ end of setq
      (if (and (= (type (setq layer (vl-catch-all-apply
                                      (function
                                        (lambda (/ res)
                                          (cond
                                            ((/= (setq res (getstring t "\nLayer for the Pipe <Use current> : ")) "")
                                             res
                                             )
                                            (t (getvar "clayer"))
                                            ) ;_ end of cond
                                          ) ;_ end of lambda
                                        ) ;_ end of function
                                      ) ;_ end of vl-catch-all-apply
                              ) ;_ end of setq
                        ) ;_ end of type
                  'str
                  ) ;_ end of =
               (= (type (setq size (vl-catch-all-apply
                                     (function
                                       (lambda (/)
                                         (initget 1
                                                  (strcat (rtos (caar lst) 2)
                                                          (apply (function strcat)
                                                                 (mapcar
                                                                   (function
                                                                     (lambda (x)
                                                                       (strcat " " (rtos (car x) 2))
                                                                       ) ;_ end of lambda
                                                                     ) ;_ end of function
                                                                   (cdr lst)
                                                                   ) ;_ end of mapcar
                                                                 ) ;_ end of apply
                                                          ) ;_ end of strcat
                                                  ) ;_ end of initget
                                         (atof (getkword (strcat "\nDiam of the Pipe ["
                                                                 (strcat (rtos (caar lst) 2)
                                                                         (apply (function strcat)
                                                                                (mapcar
                                                                                  (function
                                                                                    (lambda (x)
                                                                                      (strcat "/" (rtos (car x) 2))
                                                                                      ) ;_ end of lambda
                                                                                    ) ;_ end of function
                                                                                  (cdr lst)
                                                                                  ) ;_ end of mapcar
                                                                                ) ;_ end of apply
                                                                         ) ;_ end of strcat
                                                                 "] <Cancel> : "
                                                                 ) ;_ end of strcat
                                                         ) ;_ end of getkword
                                               ) ;_ end of atof
                                         ) ;_ end of lambda
                                       ) ;_ end of function
                                     ) ;_ end of vl-catch-all-apply
                              ) ;_ end of setq
                        ) ;_ end of type
                  'real
                  ) ;_ end of =
               (= (type (setq len (vl-catch-all-apply
                                    (function
                                      (lambda ()
                                        (getdist "\nLength of the Pipe <Cancel> : ")
                                        ) ;_ end of lambda
                                      ) ;_ end of function
                                    ) ;_ end of vl-catch-all-apply
                              ) ;_ end of setq
                        ) ;_ end of type
                  'real
                  ) ;_ end of =
               (= (type (setq ins (vl-catch-all-apply
                                    (function
                                      (lambda ()
                                        (getpoint "\nInsertion point <Cancel> : ")
                                        ) ;_ end of lambda
                                      ) ;_ end of function
                                    ) ;_ end of vl-catch-all-apply
                              ) ;_ end of setq
                        ) ;_ end of type
                  'list
                  ) ;_ end of =
               ) ;_ end of and
        (progn
          (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
          (setq res (vla-addcylinder (vla-get-modelspace adoc)
                                     (vlax-3d-point ins)
                                     (cdr (assoc size lst))
                                     len
                                     ) ;_ end of vla-addcylinder
                ) ;_ end of setq
          (if (vl-catch-all-error-p
                (vl-catch-all-apply
                  (function
                    (lambda ()
                      (setq obj_layer (vla-item (vla-get-layers adoc) layer))
                      ) ;_ end of lambda
                    ) ;_ end of function
                  ) ;_ end of vl-catch-all-apply
                ) ;_ end of VL-CATCH-ALL-ERROR-P
            (setq obj_layer (vla-add (vla-get-layers adoc) layer))
            ) ;_ end of if
          (vla-put-layer res layer)
          (vla-endundomark adoc)
          ) ;_ end of progn
        ) ;_ end of if
      res
      ) ;_ end of defun

  4. #4
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: My first lisp function...

    (1) First off, to change your function into a command, add a C: prefix to the defun's name, i.e.: (defun C:Pipe ....)

    (2) Then, I'm assuming the 1st (getstring "\nLayer for the Pipe :") is intended for the user to specify RR/RS/Ef/FT. If so then I'd suggest you look into the initget function in the developer help and use the getkword instead of getstring. This could ensure that the user doesn't enter something different from what's intended. You may even want to do the same with the pipe dia.

    (3) The PipeDiameter seems to be a list of ID / OD pairs. Maybe you should have both as strings, or do you want to find the closest match to what the user entered? If the ID is a string it makes it a lot simpler to add it to the layer name. You can always use angtof to convert the string back to a number. But using rtos to convert the number into a string might cause some formatting issues. But, lets keep this as is for the moment, see (6), (7) & (8 ) below.

    (4) The cond seems to check something strange. I thin what you intended was something like this:
    Code:
    (cond
      ((= PipeLayer "RR") (setq PipeLayerFormat "Pi-RR-"))
      ((= PipeLayer "RS") (setq PipeLayerFormat "Pi-RS-"))
      ((= PipeLayer "Ef") (setq PipeLayerFormat "Pi-Ef-"))
      ((= PipeLayer "FT") (setq PipeLayerFormat "Pi-FT-"))
      (setq PipeLayerFormat "Pi-")
    )
    (5) Nearly there ... You use the member function to get the match of diameter from the list. I think what you're after is this:
    Code:
    (if (setq PipeDiam (member PipeSize PipeDiameter))
      (setq PipeDiam (car PipeDiam))
    )
    (6) The append function works for lists, what you want is the strcat (short for String Concatenate). But also, since you have obtained the ID/OD pair you want the layer to only use the ID. So:
    Code:
    (setq PipeLayer (strcat PipeLayerFormat (car (rtos (car PipeDiam)))))
    Note the number needs to be converted to a string for this. The rtos may convert some numbers incorrectly formatted, e.g. 10 may become "10.00" or 0.75 may become "0.8" - it all depends on your units settings. You could change it to (rtos (car PipeDiam) 2 2) to enforce a decimal with 2 digits to the right of the point, but then what about those diameters like 10, 12, 14 ...?

    (7) Therefore I'd suggest using string in the list instead. Thus:
    Code:
    (setq    PipeDiameter
         '(("0.5" "0.840")
           ("0.75" "1.050")
           ...
    Then you'd also have a slight difference in point (6) above: You won't need the rtos anymore, and you can have the formatting exactly as you'd like it. But the major change would be to the portion of point (2) where you get the diameter from the user ... see below:

    (8 ) Using initget to ensure the user picks only one of a set of diameters:
    Code:
    (initget "0.5 0.75 1 1.25 1.5 2 2.5 3 4 5 6 8 10 12 14 16 18 20 22 24")
    (setq PipeSize (getkword "\nDiameter of the Pipe [0.5|0.75|1|1.25|1.5|2|2.5|3|4|5|6|8|10|12|14|16|18|20|22|24]: "))
    This will ensure that the user can only pick one from that list. Same thing can be done with the RR/RS/Ef/FT.

    (9) Lastly (I think) you set the current layer using something like (setvar "CLAYER" PipeLayer). If you want to return the current layer back to what it was you need to save it to a variable first (using getvar)then use setvar to reset it after you've finished creating the cylinder. And then to create the cylinder:
    Code:
    (command "._CYLINDER" InsertionPoint "_Diameter" (cadr PipeDiam) PipeLength)
    I'm assuming you want to draw the OD of the pipe. If the ID use (car PipeLength) instead.

  5. #5
    AUGI Addict
    Join Date
    2008-02
    Posts
    1,141
    Login to Give a bone
    0

    Default Re: My first lisp function...

    You can actually get away with
    Code:
    (setq PipeDiam (car (member PipeSize PipeDiameter)))
    since you can car nil.

  6. #6
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: My first lisp function...

    I suppose you could do that yes. Looking through my post again I think I was a bit over protective in any case . Seeing as I'm suggesting using initget & getkword it's basically ensured that member will return a result.

  7. #7
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: My first lisp function...

    And actually ... I just noticed member is the wrong function to use there. It should be assoc (without car).

  8. #8
    100 Club
    Join Date
    2015-10
    Location
    Vancouver Island
    Posts
    107
    Login to Give a bone
    0

    Thumbs up Re: My first lisp function...

    Wow, thank you everyone..

    yes it's basically the ID/OD pair. since I only draw the OD ( draw what you see ) I am using the Shelf size of pipe.

    for information, if curious,

    RR RS etc are entered, aswell as the shelf size 4,6,8 etc
    RR - Recirc Return / RS - Recirc Supply / Ef - Effluent / FT - FlowThrough it is the 4 types of piping used, and then for the actual layer Pi-RR-4 - makes it easy to edit just the specific type and size of pipes in the facility

    I will start editing it now and see if I can get it to work.

    thanks again everyone!

    john.

  9. #9
    100 Club
    Join Date
    2015-10
    Location
    Vancouver Island
    Posts
    107
    Login to Give a bone
    0

    Default Re: My first lisp function...

    kpblc2000

    This worked, but unfortunatly I don't understand any of it, and it missed the layer convention I use.

    I'm trying to figure it out now

  10. #10
    100 Club
    Join Date
    2015-10
    Location
    Vancouver Island
    Posts
    107
    Login to Give a bone
    0

    Default Re: My first lisp function...

    ok this routine works, I am the only person using it..

    the one thing that continues to error is when I try to set the layer.

    ; (setq pipesize (itoa (pipesize)))
    ; (setq pipelayerformat (Strcat (pipelayerformat pipesize)))

    ;(getvar CurrentLayer "Clayer")

    ;(setvar "Clayer" pipelayerformat)

    ; (setvar "Clayer" Currentlayer)

    these parts I end up having to comment out.

    !pipelayerformat returns what it should.
    !pipesize returns what it should.

    I need to combine these, then set the layer.

    if I un-comment the first 2 lines, I get
    Insertion Point : ; error: bad function: "Pi-RR-"

    if i keep these commented the routine works great, but i sill have to change the layer manually.

    P.S. I just realized, the layer MAY exist or may not exist when the pipe is entered. so when I can figure out how to fix it, i have to add in a routine to see if the layer exists, if not make it.
    Attached Files Attached Files

Page 1 of 2 12 LastLast

Similar Threads

  1. New function in ObjectARX for LISP
    By erick_19_hk266024 in forum ARX
    Replies: 1
    Last Post: 2013-08-26, 12:46 PM
  2. changevpsettings .Net to LISP function
    By peter in forum Dot Net API
    Replies: 3
    Last Post: 2009-12-11, 03:44 PM
  3. Replies: 7
    Last Post: 2009-11-02, 05:38 AM
  4. Arrow OR Tab Key Function In Lisp?
    By omorah in forum AutoCAD Customization
    Replies: 2
    Last Post: 2008-11-20, 02:59 AM
  5. Using the inters function in Lisp
    By Lions60 in forum AutoLISP
    Replies: 5
    Last Post: 2007-07-09, 12:27 PM

Posting Permissions

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