Author Topic: Still learning, but now trying to make an "L" shape.  (Read 928 times)

0 Members and 1 Guest are viewing this topic.

Curious Drafter

  • Mosquito
  • Posts: 15
  • Marine r try use big brain not stronk arms...
Still learning, but now trying to make an "L" shape.
« on: June 17, 2022, 02:41:45 PM »
Thank you to everyone who helped last time I tried making a LISP and a pre thank you for any advice and guidance.

I am trying to make an "L" shape using polylines. The goal is to be able to set the leg lengths and thickness. I will eventually need to be able to have the legs vary in length, but keep the same thickness. So far I have the following thrown together:

Code - Auto/Visual Lisp: [Select]
  1. (defun C:BAA ()
  2.  
  3.         (setq pt (getpoint "Choose a starting point"))
  4.                
  5.                 (setq ptx (nth 0 pt))
  6.                        
  7.                         (setq pty (nth 1 pt))
  8.  
  9.                                 (setq LG1 (getreal "How long is the 1st leg?"))
  10.  
  11.                                         (setq THK1 (getreal "How thick is the member?"))
  12.  
  13.                                                         (setq x1 (+ pty LG1))
  14.  
  15.                                                 (setq pt1 (list x1 ptx 0))
  16.                                                                
  17.                                         (setq y1 (+ pty THK1))
  18.  
  19.                                 (setq pt2 (- x1 THK1))
  20.  
  21.                         (setq pt3 (list ptx y1 0))
  22.  
  23.                 (setq pt4 (- pty THK1))
  24.  
  25.         (command "pline" pt pt1 pt2 pt3 pt4 "c")
  26.  
  27. ) ;Closes defun.

I know I need to adjust the points and lengths (as it's trying to make a triangle right now), but I am trying to narrow down what needs switched around.

I am very grateful for any and all help.

Grrr1337

  • Swamp Rat
  • Posts: 783
Re: Still learning, but now trying to make an "L" shape.
« Reply #1 on: June 17, 2022, 06:19:13 PM »
Hi, heres a quick example. Not sure if thats what you are after -
Code - Auto/Visual Lisp: [Select]
  1. ; Sample chart:
  2. ;
  3. ;   pt_legYaxis
  4. ;    |  | ipt_legYaxis
  5. ;    |  |
  6. ;    |  |
  7. ;    |  | ipt
  8. ;    |  |___________ ipt_legXaxis
  9. ; pt |________________ pt_legXaxis
  10. ;
  11. ;
  12. (defun C:BAA ( / pt LG1 LG2 THK1 pt_legYaxis pt_legXaxis ipt_legYaxis ipt_legXaxis ipt ) ; always localise your variables
  13.  
  14.   (if
  15.     (and ; code block that collects all the user's inputs
  16.       (setq pt (getpoint "Choose a starting point"))
  17.       (setq LG1 (getreal "How long is the 1st leg?"))
  18.       (setq LG2 (getreal "How long is the 2nd leg?"))
  19.       (setq THK1 (getreal "How thick is the member?"))
  20.     ); and
  21.     (progn ; code block that processes the inputs
  22.       (setq pt_legYaxis (mapcar '+ pt (list 0.0 LG1 0.0))) ; calculate the vertical leg by adding 'LG1' distance to 'pt's Y coordinate
  23.       (setq pt_legXaxis (mapcar '+ pt (list LG2 0.0 0.0))) ; calculate the horizontal leg by adding 'LG2' distance to 'pt's X coordinate
  24.       (setq ipt_legYaxis (mapcar '+ pt_legYaxis (list THK1 0.0 0.0))) ; calculate the inner top vertex by adding 'THK1' distance on 'ipt_legYaxis' s X coordinate
  25.       (setq ipt_legXaxis (mapcar '+ pt_legXaxis (list 0.0 THK1 0.0))) ; calculate the inner bottom vertex by adding 'THK1' distance on 'ipt_legXaxis' s Y coordinate
  26.       (setq ipt (mapcar '+ pt (list THK1 THK1 0.0))) ; calculate the inner intersection by adding 'THK1' distance on 'pt's X coordinate and Y coordinate
  27.      
  28.       (command "_.pline" pt pt_legXaxis ipt_legXaxis ipt ipt_legYaxis pt_legYaxis "c") ; invoke polyline command and order the point arguments to shape an outline
  29.     ); progn
  30.     (alert "User interrupted an input")
  31.   ); if
  32.   (princ)
  33. ); defun C:BAA
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

BIGAL

  • Swamp Rat
  • Posts: 1032
  • 40 + years of using Autocad
Re: Still learning, but now trying to make an "L" shape.
« Reply #2 on: June 17, 2022, 10:12:44 PM »
Grrr1337 has provided a good solution

You can change input to this if you want.



Code: [Select]
(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter values " "1st leg Len " 10 9 "6" "2nd leg len" 10 9 "6" "Thickness " 10 9 "1" )))
A man who never made a mistake never made anything

Curious Drafter

  • Mosquito
  • Posts: 15
  • Marine r try use big brain not stronk arms...
Re: Still learning, but now trying to make an "L" shape.
« Reply #3 on: June 20, 2022, 09:07:58 AM »
That command does work amazing and I am learning a lot from checking out what each line does, but I was wondering if there would be an easy way to incorporate the angle (plus or minus 90 degrees) and a fillet on the outside of the angle. I am hoping to start the incorporation now, so that later I can have it change based on user input.

thank you for all your help and guidance, it is greatly appreciated.

Grrr1337

  • Swamp Rat
  • Posts: 783
Re: Still learning, but now trying to make an "L" shape.
« Reply #4 on: June 20, 2022, 11:11:05 AM »
You have your 'regular' points for describing the shape, for such recalculation you might need some point-rotating and/or point-reflecting subfunctions.

Here are few links from a quick google search:
Rotate a list of points
How to calculate mirrored point

For me personally to perform such tasks I use Lee Mac's Matrix transform functions (because I'm not that good at math):
Matrix Transformation Functions

Your next objective is how to incorporate them into your code, and also decide on what kind of user input to draw the rotated/mirrored shape.

(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

BIGAL

  • Swamp Rat
  • Posts: 1032
  • 40 + years of using Autocad
Re: Still learning, but now trying to make an "L" shape.
« Reply #5 on: June 20, 2022, 08:46:46 PM »
It maybe that entering -ve value for lengths may invert the "L" using GRRR code but it will need a couple of values changed. Will have a think if Grrr does not do 1st.

A man who never made a mistake never made anything

BIGAL

  • Swamp Rat
  • Posts: 1032
  • 40 + years of using Autocad
Re: Still learning, but now trying to make an "L" shape.
« Reply #6 on: June 22, 2022, 01:38:23 AM »
Found some time and had a play sorry Grrr got lost in pt names, accepts negative distances.

Code: [Select]
; Sample chart:
; Original code by Grrr
; Modified by Alanh to do 4 directions

;      pt2  pt3
;       |  |
;       |  |
;       |  |
;       |  | pt4
;       |  |___________ pt5
; pt1 |________________ pt6
;
;
(defun C:BAA ( / oldsnap pt1 LG1 LG2 THK1 l1 l2 l3 l4 pt1 pt2 pt3 pt4 pt5 pt6) ; always localise your variables



(defun l1 ( / )
(setq pt2 (mapcar '+ pt1 (list 0.0 LG1 0.0))) ; calculate the vertical leg by adding 'LG1' distance to 'pt's Y coordinate
(setq pt3 (mapcar '+ pt2 (list THK1 0.0 0.0))) ; calculate the inner top vertex by adding 'THK1' distance on 'ipt_legYaxis' s X coordinate
(setq pt4 (mapcar '+ pt1 (list THK1 THK1 0.0))) ; calculate the inner intersection by adding 'THK1' distance on 'pt's X coordinate and Y coordinate
(setq pt6 (mapcar '+ pt1 (list LG2 0.0 0.0))) ; calculate the horizontal leg by adding 'LG2' distance to 'pt's X coordinate
(setq pt5 (mapcar '+ pt6 (list 0.0 THK1 0.0))) ; calculate the inner bottom vertex by adding 'THK1' distance on 'ipt_legXaxis' s Y coordinate
(command "_.pline" pt1 pt2 pt3 pt4 pt5 pt6  "c")
)

(defun l2 ( / )
(setq pt2 (mapcar '+ pt1 (list 0.0 LG1 0.0))) ; calculate the vertical leg by adding 'LG1' distance to 'pt's Y coordinate
(setq pt3 (mapcar '+ pt2 (list THK1 0.0 0.0))) ; calculate the inner top vertex by adding 'THK1' distance on 'ipt_legYaxis' s X coordinate
(setq pt4 (mapcar '+ pt1 (list THK1 (- THK1) 0.0))) ; calculate the inner intersection by adding 'THK1' distance on 'pt's X coordinate and Y coordinate
(setq pt6 (mapcar '+ pt1 (list LG2 0.0 0.0))) ; calculate the horizontal leg by adding 'LG2' distance to 'pt's X coordinate
(setq pt5 (mapcar '+ pt6 (list 0.0 (- THK1) 0.0))) ; calculate the inner bottom vertex by adding 'THK1' distance on 'ipt_legXaxis' s Y coordinate
(command "_.pline" pt1 pt2 pt3 pt4 pt5 pt6  "c")
)

(defun l3 ( / )
(setq pt2 (mapcar '+ pt1 (list 0.0 LG1 0.0))) ; calculate the vertical leg by adding 'LG1' distance to 'pt's Y coordinate
(setq pt3 (mapcar '+ pt2 (list (- THK1) 0.0 0.0))) ; calculate the inner top vertex by adding 'THK1' distance on 'ipt_legYaxis' s X coordinate
(setq pt4 (mapcar '+ pt1 (list (- THK1) THK1 0.0))) ; calculate the inner intersection by adding 'THK1' distance on 'pt's X coordinate and Y coordinate
(setq pt6 (mapcar '+ pt1 (list LG2 0.0 0.0))) ; calculate the horizontal leg by adding 'LG2' distance to 'pt's X coordinate
(setq pt5 (mapcar '+ pt6 (list 0.0 THK1 0.0))) ; calculate the inner bottom vertex by adding 'THK1' distance on 'ipt_legXaxis' s Y coordinate
(command "_.pline" pt1 pt2 pt3 pt4 pt5 pt6  "c")
)

(defun l4 (  / )
(setq pt2 (mapcar '+ pt1 (list 0.0 LG1 0.0))) ; calculate the vertical leg by adding 'LG1' distance to 'pt's Y coordinate
(setq pt3 (mapcar '+ pt2 (list (- THK1) 0.0 0.0))) ; calculate the inner top vertex by adding 'THK1' distance on 'ipt_legYaxis' s X coordinate
(setq pt4 (mapcar '+ pt1 (list (- THK1) (- THK1) 0.0))) ; calculate the inner intersection by adding 'THK1' distance on 'pt's X coordinate and Y coordinate
(setq pt6 (mapcar '+ pt1 (list LG2 0.0 0.0))) ; calculate the horizontal leg by adding 'LG2' distance to 'pt's X coordinate
(setq pt5 (mapcar '+ pt6 (list 0.0 (- THK1) 0.0))) ; calculate the inner bottom vertex by adding 'THK1' distance on 'ipt_legXaxis' s Y coordinate
(command "_.pline" pt1 pt2 pt3 pt4 pt5 pt6  "c")
)

(if (not AH:getvalsm)(load "Multi Getvals.lsp"))

(if (= (setq  pt1 (getpoint "Choose a starting point")) nil)
(alert "User interrupted an input")
(progn
  (setq ans (AH:getvalsm (list "Enter values " "Height  " 6 5 "100" "Length " 6 5 "150" "Thick " 6 5 "10" )))
  (setq LG1 (atof (nth 0 ans)) lG2 (atof (nth 1 ans)) THK1 (atof (nth 2 ans)))
  (setq oldsnap (getvar 'osmode))
  (setvar 'osmode 0)

  (cond
    ((and (> lg1 0.0)(> LG2 0.0))(L1))
    ((and (< lg1 0.0)(> LG2 0.0))(L2))
    ((and (> lg1 0.0)(< lg2 0.0))(L3))
    ((and (< lg1 0.0)(< lg2 0.0))(L4))
 ) ; cond

 (setvar 'osmode oldsnap)
 
   ); progn
 ); if
  (princ)
); defun C:BAA
(c:baa)
A man who never made a mistake never made anything

Curious Drafter

  • Mosquito
  • Posts: 15
  • Marine r try use big brain not stronk arms...
Re: Still learning, but now trying to make an "L" shape.
« Reply #7 on: June 23, 2022, 03:08:46 PM »
I think I will end up having to set the base point and leg orientation later, but what I meant when I was talking about changing the angle of the angle member I meant how would I go about making it either acute or obtuse instead of a 90 degree angle. I am currently working on the best way to incorporate the angle as well as a fillet on the "outside corner". If I can figure it out I will attach a dwg of what I hope the end product to look similar to.

BIGAL

  • Swamp Rat
  • Posts: 1032
  • 40 + years of using Autocad
Re: Still learning, but now trying to make an "L" shape.
« Reply #8 on: June 23, 2022, 07:57:12 PM »
Good task to learn lisp, look at what I posted, you would remove the mapcar functions and replace with a (setq pt2 (polar pt1 ang dist))

So you home work is to google help about the polar function. Start  here https://www.afralisp.net/autolisp/tutorials/calculating-polar-points.php#:~:text=The%20Polar%20function%20is%20defined%20in%20the%20AutoCAD,distance%20from%20a%20point.%20%28polar%20pt%20ang%20dist%29





« Last Edit: June 23, 2022, 08:00:50 PM by BIGAL »
A man who never made a mistake never made anything

Curious Drafter

  • Mosquito
  • Posts: 15
  • Marine r try use big brain not stronk arms...
Re: Still learning, but now trying to make an "L" shape.
« Reply #9 on: July 06, 2022, 09:58:49 AM »
Well, I am still working on it and I am trying to incorporate everything I have found so far, but this is what I have and it isn't loading properly. Any and all feedback is greatly appreciated.

Code - Auto/Visual Lisp: [Select]
  1. (defun C:BAA ( / pt LG1 LG2 THK1 pty ptx ipty iptx ipt dtr) ; Localizes the variables.
  2.  
  3. (setq CL (getvar "clayer")); Gets the current layer.
  4.        
  5. (command "layer" "m" "0" "c" 4 "" "l" "continuous" "" nil); Creates and sets layer 0.
  6.  
  7. (setq lm (getvar "limcheck")); Gets the current limit setting.
  8.  
  9. (setvar "limcheck" 0); Sets limits to 0 or off.
  10.  
  11. (defun dtr (x); Defines the degrees to radians. Remember to change it back.
  12.  
  13. (* pi (/ x 180.0)); Angle divided by 180, then total is multiplied by Pi.
  14.  
  15. ); Closes Rad function.
  16.  
  17.  
  18.  
  19.         (if
  20.  
  21.                 (and
  22.  
  23.                         (setq pt (getpoint "Please choose a start?")); Bottom left starting point of leg.
  24.  
  25.                                 (setq LG1 (getreal "Please enter the 1st leg length?")); Vertical leg length.
  26.  
  27.                                         (setq LG2 (getreal "Please enter the 2nd leg length?")); horizontal leg length.
  28.  
  29.                                                 (setq THK1 (getreal "How thick is the member?")); Thickness of the angle member.
  30.  
  31.                                                         ); Closes and
  32.  
  33.                                                                 (progn ; code block that processes the inputs
  34.  
  35.                                                                         (setq pty (polar '+ pt (dtr 0.0 LG1 0.0))) ; Outside of vertical leg.
  36.  
  37.                                                                 (setq ptx (polar '+ pt (dtr LG2 0.0 0.0))) ; Outside of horizontal leg.
  38.  
  39.                                                         (setq ipty (polar '+ pty (dtr THK1 0.0 0.0))) ; Inside of vertical leg.
  40.  
  41.                                                 (setq iptx (polar '+ ptx (dtr 0.0 THK1 0.0))) ; Inside horizontal leg.
  42.  
  43.                                         (setq ipt (polar '+ pt (dtr THK1 THK1 0.0))) ; Inside intersection.
  44.  
  45.                                 (command "pline" pt ptx iptx ipt ipty pty "c") ; Polyline to make the angle.
  46.  
  47.                                                                 ); Closes progn
  48.  
  49.         ); Closed if
  50.  
  51. (command "layer" "s" cl ""); To set layer back to original.
  52.  
  53. (setvar "limcheck" lm); To reset limit to starting limit.
  54.  
  55. ); Closes defun

JohnK

  • Administrator
  • Seagull
  • Posts: 10139
Re: Still learning, but now trying to make an "L" shape.
« Reply #10 on: July 06, 2022, 10:34:48 AM »
The problem with your current code is that you are trying to pass too many arguments to the `dtr` function.


Little tips, there are simple standard conventions for some (they may seem minor but they will help you later).

1. Comment the function close with a " ;_ <function> " string.
2. You can line up your inline comments for easier reading (I tend to use column 57 if you are using a text editor and have control over that).
3. I prefix my comments with a dash so I can tell where a new comment (sentence/statement) starts.

Code - Auto/Visual Lisp: [Select]
  1. ;; -X-          This code does not work          -X-
  2. (defun C:BAA ( / pt LG1 LG2 THK1 pty ptx ipty iptx ipt dtr) ; -Localizes the variables.
  3.   (setq CL (getvar "clayer"))                           ; -Gets the current layer.
  4.   (command "layer" "m" "0" "c" 4 "" "l" "continuous" "" nil); -Creates and sets layer 0.
  5.   (setq lm (getvar "limcheck"))                         ; -Gets the current limit setting.
  6.   (setvar "limcheck" 0)                                 ; -Sets limits to 0 or off.
  7.  
  8.   (defun dtr (x)                                        ; -Defines the degrees to radians. Remember
  9.                                                         ;  to change it back.
  10.     (* pi (/ x 180.0))                                  ; -Angle divided by 180, then total
  11.                                                         ;  is multiplied by Pi.
  12.     );_ defun
  13.  
  14.  
  15.   (if
  16.     (and
  17.       (setq pt (getpoint "Please choose a start?"))     ; -Bottom left starting point of leg.
  18.       (setq LG1 (getreal "Please enter the 1st leg length?")); -Vertical leg length.
  19.       (setq LG2 (getreal "Please enter the 2nd leg length?")); -Horizontal leg length.
  20.       (setq THK1 (getreal "How thick is the member?"))  ; -Thickness of the angle member.
  21.       );_ and
  22.  
  23.     (progn                                              ; -code block that processes the inputs
  24.       (setq pty (polar '+ pt (dtr 0.0 LG1 0.0)))        ; -Outside of vertical leg.
  25.       (setq ptx (polar '+ pt (dtr LG2 0.0 0.0)))        ; -Outside of horizontal leg.
  26.       (setq ipty (polar '+ pty (dtr THK1 0.0 0.0)))     ; -Inside of vertical leg.
  27.       (setq iptx (polar '+ ptx (dtr 0.0 THK1 0.0)))     ; -Inside horizontal leg.
  28.       (setq ipt (polar '+ pt (dtr THK1 THK1 0.0)))      ; -Inside intersection.
  29.       (command "pline" pt ptx iptx ipt ipty pty "c")    ; -Polyline to make the angle.
  30.       );_ progn
  31.     );_ if
  32.  
  33.   (command "layer" "s" cl "")                           ; -To set layer back to original.
  34.   (setvar "limcheck" lm)                                ; -To reset limit to starting limit.
  35.   );_ defun
  36.  
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1032
  • 40 + years of using Autocad
Re: Still learning, but now trying to make an "L" shape.
« Reply #11 on: July 06, 2022, 08:28:12 PM »
Did you look at all the code in my post 6 it is full 4 direction "L's". Just make sure that Multi Getvals.lsp is in support path or Appload add to Startup Suite to it gets loaded on startup.
A man who never made a mistake never made anything

Curious Drafter

  • Mosquito
  • Posts: 15
  • Marine r try use big brain not stronk arms...
Re: Still learning, but now trying to make an "L" shape.
« Reply #12 on: July 07, 2022, 09:04:24 AM »
Is there a good way to make it work without the "Multi GETVALS" LISP?

dexus

  • Newt
  • Posts: 75
Re: Still learning, but now trying to make an "L" shape.
« Reply #13 on: July 07, 2022, 09:12:57 AM »
Is there a good way to make it work without the "Multi GETVALS" LISP?
You can always combine the two files by putting al the code from multi getvals.lsp at the end of your lisp file.
That way you only have to load one file.

Edit, make sure to leave the header in, its good practice and a good way to see where you found the code so you can find the source a couple of years later if needed.

Curious Drafter

  • Mosquito
  • Posts: 15
  • Marine r try use big brain not stronk arms...
Re: Still learning, but now trying to make an "L" shape.
« Reply #14 on: July 07, 2022, 09:35:31 AM »
I think I will try that. I did want to see where I went wrong with incorporating this following link.

https://www.afralisp.net/autolisp/tutorials/calculating-polar-points.php#:~:text=The%20Polar%20function%20is%20defined%20in%20the%20AutoCAD,distance%20from%20a%20point.%20%28polar%20pt%20ang%20dist%29

I tried to use the "dtr" command and I am getting some pushback from the linked forum. I see where it is overused, but I am still learning programing language.

 https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/creating-custom-shape-with-formed-leg/m-p/11281305#M433384

Any and all help is greatly appreciated.