How to wright the auto lisp codes for calculation

How to wright the auto lisp codes for calculation

Boopathy_Karthikeyan
Advocate Advocate
2,466 Views
15 Replies
Message 1 of 16

How to wright the auto lisp codes for calculation

Boopathy_Karthikeyan
Advocate
Advocate

Hi Friends, 

I am practicing to wright the Lisp Code, i just follow the online guides of lisp coding and created it, but i need to improve it more, lisp code is pasted below. some formulas i highlighted in red below, other i will try to do in my won after your guidance.

I try to make the formula but its not works well.

anyone could help me on this? 

 

 

(defun c:drill()

                 (setq ip (getpoint "\nBase Point : "))

                (setq sd1 (getdist "\nShank Diameter1 : "))

                (setq sd2 (getdist "\nShank Diameter2 : "))

                (setq sl (getdist "\nShank Length : "))

                (setq d1 (getdist "\nCutting Diameter : "))

                (setq tl (getdist "\nTool Length : "))

                (setq fl (getdist "\nFlute Length : "))

                (setq cx (getangle "\nCone Angle : "))

                (setq cy (getangle "\nChamfer Angle : "))

                (setq xx (getangle "\nChamfer Angle : "))

 

                (setq p1 (polar ip (dtr 90.0) 5)) ; 5 = (D2/2)

                (setq p2 (polar p1 (dtr 135.0) 1.414214)) ; 1.414214 = ((sd1-sd2)/2)/sin45°)

                (setq p3 (polar p2 (dtr 180.0) 52)) ; 52 = ((sd1-sd2)-sl)

                (setq p4 (polar p3 (dtr 225.0) 1.414214)) ; ((sd1-d1)/2)/sin45°)

                (setq p5 (polar p4 (dtr 180.0) 16))

                (setq p6 (polar p5 (dtr 180.0) 27.11))

                (setq p7 (polar p6 (dtr 240.0) 5.77))

        

                (command "pline" ip p1 p2 p3 p4 p5 p6 p7 )

 (princ)

 )

 (defun dtr(x)

 

                (* pi (/ x 180.0))

 

)

(princ)



Best Regards..
Boopathy K
0 Likes
Accepted solutions (3)
2,467 Views
15 Replies
Replies (15)
Message 2 of 16

ВeekeeCZ
Consultant
Consultant
;; ((sd1-sd2)/2)/sin45°)
(/ (- sd1 sd2)
   2
   (sin (dtr 45)))
; or in line:
(/ (- sd1 sd2) 2 (sin (dtr 45)))

;; ((sd1-sd2)-sl)
(- sd1 sd2 sl)

;; ((sd1-d1)/2)/sin45°)
(/ (- sd1 d1) 2 (sin (dtr 45)))

 

Message 3 of 16

Boopathy_Karthikeyan
Advocate
Advocate
Accepted solution

its no working properly there are some error showing 

Could you check it for me? please 

 

(defun c:drill()

(setq ip (getpoint "\nBase Point : "))
(setq sd1 (getdist "\nShank Diameter1 : "))
(setq sd2 (getdist "\nShank Diameter2 : "))
(setq sl (getdist "\nShank Length : "))
(setq d1 (getdist "\nCutting Diameter : "))
(setq tl (getdist "\nTool Length : "))
(setq fl (getdist "\nFlute Length : "))
(setq cx (getangle "\nCone Angle : "))
(setq cy (getangle "\nChamfer Angle : "))
(setq xx (getangle "\nChamfer Angle : "))

(setq p1 (polar ip (dtr 90.0) (/ sd2 2)))
(setq p2 (polar p1 (dtr 135.0) (/ (- sd1 sd2) 2 (sin (dtr 45)))))
(setq p3 (polar p2 (dtr 180.0) (- sd1 sd2 s1))))
(setq p4 (polar p3 (dtr 225.0) 1.414214))
(setq p5 (polar p4 (dtr 180.0) (- tl (+ fl sl (/ (- sd1 sd2) 2) (/ (- sd1 d1) 2)))))
(setq p6 (polar p5 (dtr 180.0) 27.11))
(setq p7 (polar p6 (dtr 240.0) 5.77))

(command "pline" ip p1 p2 p3 p4 p5 p6 p7 )

)

(princ)

(defun dtr(a)

(* pi (/ a 180.0)))

(princ)



Best Regards..
Boopathy K
0 Likes
Message 4 of 16

ВeekeeCZ
Consultant
Consultant

Watch the parenthesis!!

And be careful about s1 vs. sl.

You should also turn off the OSNAPs (eg. using "_none" osmode)

 

(defun c:drill()
  
  (setq ip (getpoint "\nBase Point : "))
  (setq sd1 (getdist "\nShank Diameter1 : "))
  (setq sd2 (getdist "\nShank Diameter2 : "))
  (setq sl (getdist "\nShank Length : "))
  (setq d1 (getdist "\nCutting Diameter : "))
  (setq tl (getdist "\nTool Length : "))
  (setq fl (getdist "\nFlute Length : "))
  (setq cx (getangle "\nCone Angle : "))
  (setq cy (getangle "\nChamfer Angle : "))
  (setq xx (getangle "\nChamfer Angle : "))
  (setq p1 (polar ip (dtr 90.0) (/ sd2 2)))
  (setq p2 (polar p1 (dtr 135.0) (/ (- sd1 sd2) 2 (sin (dtr 45)))))
  (setq p3 (polar p2 (dtr 180.0) (- sd1 sd2 sl)))
  (setq p4 (polar p3 (dtr 225.0) 1.414214))
  (setq p5 (polar p4 (dtr 180.0) (- tl (+ fl sl (/ (- sd1 sd2) 2) (/ (- sd1 d1) 2)))))
  (setq p6 (polar p5 (dtr 180.0) 27.11))
  (setq p7 (polar p6 (dtr 240.0) 5.77))
  (command "pline" "_none" ip "_none" p1 "_none" p2 "_none" p3 "_none" p4 "_none" p5 "_none" p6 "_none" p7 "")
  (princ)
  )

(defun dtr(a)
  (* pi (/ a 180.0)))

(princ)
0 Likes
Message 5 of 16

Kent1Cooper
Consultant
Consultant
Accepted solution

@Boopathy_Karthikeyan wrote:

its no working properly there are some error showing 

....


Not enough information.  What error(s)?  Narrow it down for us.

 

One thing I notice is that you don't complete the PLINE command.  Try:

(command "pline" ip p1 p2 p3 p4 p5 p6 p7 "")

 

Additionally, you can be more precise by replacing 1.414214 with (sqrt 2).

 

Some other little things to consider:

 

You can set any number of variables in one  (setq) function:

 

(setq

  ip (getpoint "\nBase Point : ")
  sd1 (getdist "\nShank Diameter1 : ")
....
  p6 (polar p5 (dtr 180.0) 27.11)
  p7 (polar p6 (dtr 240.0) 5.77)

); end setq

 

But note that each line ends with a single  right parenthesis, because the additional one at the end of each in your original was to end the separate (setq) function for each, but here, those are all covered by the one at the bottom.

 

You don't need the decimals on the degree numbers.  As you already do with (dtr 45), you can do (dtr 90), etc.  BUT with most of those, it's actually shorter  to use the radian value as a calculation with pi directly, and forget (dtr):

(dtr) version:         can be replaced by direct radian value:

(dtr 45)              (/ pi 4)

(dtr 90.0)          (/ pi 2)

(dtr 135.0)         (* pi 0.75)

(dtr 180.0)         pi

(dtr 225.0)         (* pi 1.25)

(dtr 240.0)         (/ (* pi 4) 3)

Kent Cooper, AIA
0 Likes
Message 6 of 16

Boopathy_Karthikeyan
Advocate
Advocate

Hi,

Thanks for your concern! 

error is like bad argument type 2d/3d point nil

its about the parenthesis issue. ( BeekeeCZ ) Modified the error 

I am not closing the command still, i need to create another half of the drawing.

1.414214 will be arrived from another formula i am working on that   

 

 



Best Regards..
Boopathy K
0 Likes
Message 7 of 16

Kent1Cooper
Consultant
Consultant

@Boopathy_Karthikeyan wrote:

....Thanks for your concern! ....


[I now notice that the new Forum design doesn't include any indication that a Post has been Edited, as it did before.  So I just wanted to let you know that I added some things since the original Reply.]

Kent Cooper, AIA
0 Likes
Message 8 of 16

_gile
Consultant
Consultant

Hi,

1.414214 is the square root of 2:

(sqrt 2)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 16

martti.halminen
Collaborator
Collaborator

Not a problem here as pi is not an integer, but this might be a good place to add a reminder of one AutoLISP oddity almost everybody gets bitten with sometimes:

 

Integer division is truncating.

 

$ (/ 5 3)
1
_$ (/ 5 3.0)
1.66667

 

-- 

 

0 Likes
Message 10 of 16

Boopathy_Karthikeyan
Advocate
Advocate

Hi Mr. Kent,

Shall i call you Kent? 

Thanks that's a cool idea so i can wright directly write the Radiant Value, 

But i am sorry i couldn't use it here because some of the inputs are in degrees i am going to add here! 

 

Could you help me to simply this calculation like some of the calculations are repeating again again  if i am having a common code like (defun drt (a)) it will same my time.

Did you have any Study Martial for lisp? that will help to correct myself

 

Thanks for your concern!

 



Best Regards..
Boopathy K
0 Likes
Message 11 of 16

Kent1Cooper
Consultant
Consultant
Accepted solution

@Boopathy_Karthikeyan wrote:

.... Shall i call you Kent? 

.... 

Could you help me to simply this calculation like some of the calculations are repeating again again  ....

Did you have any Study Martial for lisp? that will help to correct myself

 

....

1.  Yes.

2.  The only thing other than (dtr) that it looks like you could be shortcutted is the splitting-the-difference thing, e.g.:

  (/ (- sd1 sd2) 2)

It won't save much, but you could define that:

(defun hd (a b) (/ (- a b) 2)); = half the difference

and then:

....

  p2 (polar p1 (dtr 135.0) (/ (hd sd1 sd2) (sin (dtr 45))))

....

  p5 (polar p4 (dtr 180.0) (- tl fl sl (hd sd1 sd2) (hd sd1 d1)))

....

[I also changed subtracting a sum of things from tl to just subtracting them all serially from it.]

 

3.  Search this Forum for terms like "learn AutoLisp" or "Lisp training" or "AutoLisp tutorials" and you will find very many  suggestions.

Kent Cooper, AIA
0 Likes
Message 12 of 16

Boopathy_Karthikeyan
Advocate
Advocate

hi

thanks for all who supported on this.

Here is the final out come i successful got what i want.

Be still here i add the "OSMode" to On/Off but i don't think  its working, better i can go with adding "_None" in front of the point. If you have better solution kindly suggest be.

thanks.

 

(defun c:drill()

(setvar "osmode" 32)

(setq ip (getpoint "\nBase Point : "))

(

(setvar "osmode" 0)

(setq
sd1 (getdist "\nShank Diameter1 : ")
sd2 (getdist "\nShank Diameter2 : ")
sl (getdist "\nShank Length : ")
d1 (getdist "\nCutting Diameter : ")
tl (getdist "\nTool Length : ")
fl (getdist "\nFlute Length : ")
cx (getangle "\nCone Angle : ")

p1 (polar ip (dtr 90.0) (/ sd2 2))
p2 (polar p1 (dtr 135.0) (/ (- sd1 sd2) 2 (sin (dtr 45))))
p3 (polar p2 (dtr 180.0) (- sl (/ (- sd1 sd2) 2)))
p4 (polar p3 (dtr 225.0) (/ (- sd1 d1) 2 (sin (dtr 45))))
p5 (polar p4 (dtr 180.0) (- tl (+ fl sl (/ (- sd1 d1) 2))))
p6 (polar p5 (dtr 180.0) (- fl (cl cx)))
p7 (polar p6 (- cx (dtr 180.0)) (/ (/ d1 2) (sin cx)))
p8 (Polar p7 (- (dtr 360) cx) (/ (/ d1 2) (sin cx)))
p9 (polar p8 (dtr 0.0) (- fl (cl cx)))
p10 (polar p9 (dtr 0.0) (- tl (+ fl sl (/ (- sd1 d1) 2))))
p11 (polar p10 (dtr 315.0) (/ (- sd1 d1) 2 (sin (dtr 45))))
p12 (polar p11 (dtr 0.0) (- sl (/ (- sd1 sd2) 2)))
p13 (polar p12 (dtr 45.0) (/ (- sd1 sd2) 2 (sin (dtr 45))))
)

(command "pline" ip p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 "c"
"line" "_non" p2 "_non" p12 ""
"line" "_non" p3 "_non" p11 ""
"line" "_non" p4 "_non" p10 ""
"line" "_non" p5 "_non" p9 ""
"line" "_non" p6 "_non" p8 ""

)

(setvar "osmode" 32)
)

(princ)

)

(setvar "osmode" 32)

(defun dtr(a)
(* pi (/ a 180.0)))

(princ)

(defun tan(a)
(/ (sin a) (cos a)))

(princ)

(defun cl(a)
(/ (/ d1 2) (tan a)))

(princ)

 

 

 



Best Regards..
Boopathy K
0 Likes
Message 13 of 16

ВeekeeCZ
Consultant
Consultant

- osmode 0 vs. none: one way or another. "none" is simpler, but if you need to use it too many times it makes lines the too long, less readable. Changing the osmode requires the *error* function that in case the program fails the *error* function restores changed value to the previous state.

- localize your variables

- good programmer does not allow to the user set values that fail in the program. Don't allow setting which leads to division by 0. Or if same value is necessarily higher than other, your shouldn't allow the lower one... In following code I've showed you how to test if (sin cx) will not be 0... - try to set 0 for a test.

Anyway, this first step always should be using the (initget) command which tests the base conditions like: it the user input nothing?, non-rezo?, not a negative?? read HELP

- good thing is add the UNDO grouping, then if you undo things you can make it in just one step

 

(vl-load-com) ; load library to allow vl* functions. In this case of undo grouping.

(defun c:Drill ( / *error* adoc cx d1 fl ip osm p1 p10 p11 p12 p13 p2 p3 p4 p5 p6 p7 p8 p9 sd1 sd2 sl tl dtr tan cl) ; localize your vars - than you overcome a potential interference with war of same name but from another lisps
  
  (defun *error* (errmsg) 	; error function. In case of error this will reset your changed systen vars - like osmode.
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if osm (setvar 'OSMODE))	; reset osmode
    (vla-endundomark adoc)	; end undo group
    (princ))

  (defun dtr (a) (* pi (/ a 180.))) ; there is necessay have a real number. nowhere else in the program (as 'a' varible)
  (defun tan (a) (/ (sin a) (cos a)))
  (defun cl (a) (/ (/ d1 2.) (tan a))) ; real!

  ; -------------------------------------------------------------------------------------------------------------------------------------------------------------
  
  (setq osm (getvar 'OSMODE)) 	; save current osmode
  (setvar 'OSMODE 0)		; turn off osmode
  
  (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))  ; start undo group.
  
  ; make sure that user inputs are suitable for your program - use the (initget) funtion
  (initget (+ 1 ; can the value of sd1 be nothing? definitelly no
              2 ; can that be 0?
              3 ; or negative?)
              ))
  (setq sd1 (getdist "\nShank Diameter1 : "))
  
  (initget (+ 1 2 3))
  (setq sd2 (getdist "\nShank Diameter2 : "))
  
  (initget (+ 1 2 3))
  (setq sl (getdist "\nShank Length : "))
  
  (initget (+ 1 2 3))
  (setq d1 (getdist "\nCutting Diameter : "))
  
  (initget (+ 1 2 3))
  (setq tl (getdist "\nTool Length : "))
  
  (initget (+ 1 2 3))
  (setq fl (getdist "\nFlute Length : "))
  
  (initget (+ 1 2 3))
  (while (not (and (setq cx (getangle "\nCone Angle : "))
                   (or (/= (sin cx) 0) 						; <- test condition
                       (prompt "Wrong input: sin angle can't be zero.")		; <- error info
                       )
                   )))
  
  (setq p1 (polar ip (dtr 90) (/ sd2 2))
        p2 (polar p1 (dtr 135) (/ (- sd1 sd2) 2 (sin (dtr 45))))
        p3 (polar p2 (dtr 180) (- sl (/ (- sd1 sd2) 2)))
        p4 (polar p3 (dtr 225) (/ (- sd1 d1) 2 (sin (dtr 45))))
        p5 (polar p4 (dtr 180) (- tl (+ fl sl (/ (- sd1 d1) 2))))
        p6 (polar p5 (dtr 180) (- fl (cl cx)))
        p7 (polar p6 (- cx (dtr 180)) (/ (/ d1 2) (sin cx)))
        p8 (Polar p7 (- (dtr 360) cx) (/ (/ d1 2) (sin cx)))
        p9 (polar p8 (dtr 0) (- fl (cl cx)))
        p10 (polar p9 (dtr 0) (- tl (+ fl sl (/ (- sd1 d1) 2))))
        p11 (polar p10 (dtr 315) (/ (- sd1 d1) 2 (sin (dtr 45))))
        p12 (polar p11 (dtr 0) (- sl (/ (- sd1 sd2) 2)))
        p13 (polar p12 (dtr 45) (/ (- sd1 sd2) 2 (sin (dtr 45))))
        )
  
  (command "_.pline" ip p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 "c"
           "_.line" p2 p12 ""
           "_.line" p3 p11 ""
           "_.line" p4 p10 ""
           "_.line" p5 p9 ""
           "_.line" p6 p8 "")
  (error "end")
)

HTH, sorry if the any is hard to understand. 

Message 14 of 16

Kent1Cooper
Consultant
Consultant

@Boopathy_Karthikeyan wrote:

....

Be still here i add the "OSMode" to On/Off but i don't think  its working, better i can go with adding "_None" in front of the point. ....

 

(defun c:drill()

(setvar "osmode" 32)

(setq ip (getpoint "\nBase Point : "))

(

 

....

 


That fourth-line left parenthesis is extraneous.  Does it work without it?

Kent Cooper, AIA
0 Likes
Message 15 of 16

Boopathy_Karthikeyan
Advocate
Advocate

thanks you kent. Smiley Happy

 



Best Regards..
Boopathy K
0 Likes
Message 16 of 16

Boopathy_Karthikeyan
Advocate
Advocate

Thanks you Beekee,

still i am learner i feel difficult to understand this, but surely i will understand your points and come back to you!

still i am trying to improve this program much better help of you peoples experience!

 

 



Best Regards..
Boopathy K