Jump to content

Lisp - circle


Lukepeanuts

Recommended Posts

Hy,

 

im new in the forum and a rookie with lisp. Im trying to create a lisp, but it doesnt work. If somebody could help me, i will apreciate a lot.

 

I need a lisp with a conditional, were i write a key word and the lisp associate a number to the key. i was writing something like this:

 

(DEFUN c:HM (CNT DIAM)
 (SETQ CNT (GETPOINT "\nselect circle center"))
 ;click the center of the circle

 (INITGET 1 "1Pul 2Pul")
 (SETQ PUL (GETKWORD "\n1Pul, 2Pul:"))
 ;To do not write the real diameter number

 	(cond
  	((= PUL "1Pul") (SETQ DIAM 0.033401))
	((= PUL "2Pul") (SETQ DIAM 0.060325))
) ;End conditional
 
 			(command "circle" cnt "D" DIAM)
 			;draw the circle

 (PRINC)
 )

Edited by SLW210
Code Tags!
Link to comment
Share on other sites

  • Replies 20
  • Created
  • Last Reply

Top Posters In This Topic

  • Lukepeanuts

    6

  • tombu

    4

  • Grrr

    4

  • David Bethel

    3

Top Posters In This Topic

Posted Images

Your code is fine, just the arguments are wrong,

Change this:

(DEFUN c:HM (CNT DIAM)

To:

(DEFUN c:HM ([color="red"][b] / [/b][/color]CNT DIAM)

And some hint, Tharwat showed me:

(command "_.circle" [color="red"][b]"_non"[/b][/color] cnt "D" DIAM)

Link to comment
Share on other sites

As a newbie, it's a great time to get into good programming habits :

 

[b][color=BLACK]([/color][/b]defun c:hm [b][color=FUCHSIA]([/color][/b]/ cnt pul diam[b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]initget 1[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq cnt [b][color=NAVY]([/color][/b]getpoint [color=#2f4f4f]"\nSelect circle center:   "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]initget 1 [color=#2f4f4f]"1Pul 2Pul"[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq pul [b][color=NAVY]([/color][/b]getkword[color=#2f4f4f]"\nDiameter : 1Pul/2Pul:   "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]setq diam [b][color=NAVY]([/color][/b]cond [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]= pul [color=#2f4f4f]"1Pul"[/color][b][color=GREEN])[/color][/b] 0.033401[b][color=MAROON])[/color][/b]
                  [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]= pul [color=#2f4f4f]"2Pul"[/color][b][color=GREEN])[/color][/b] 0.060325[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]command [color=#2f4f4f]"_.CIRCLE"[/color] [color=#2f4f4f]"_non"[/color] cnt [color=#2f4f4f]"_D"[/color] diam[b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

 

Try to be very consistent in the format you use. It makes searching for phrases or tests later much easier.

 

As this and a lot of peer to peer forums are visited by international language members, using the underscore for command and parameters is helpful. Also the period prior to commands insures that a redefined command is over ridden.

 

AutoLisp is a very human readable language and can be very easy on your eyes if thought is given to the overall layout of the code.

 

Good Luck and Have Fun ! -David

 

PS Look into the [noparse]


[/noparse] tags when posting code in threads

Link to comment
Share on other sites

Your code is fine, just the arguments are wrong,

Change this:

(DEFUN c:HM (CNT DIAM)

To:

(DEFUN c:HM ([color="red"][b] / [/b][/color]CNT DIAM)

And some hint, Tharwat showed me:

(command "_.circle" [color="red"][b]"_non"[/b][/color] cnt "D" DIAM)

 

what is it the "_non" that you putted to draw the circle? what is it the meaning?

Link to comment
Share on other sites

Thanks a lot David for your time! I will try to follow your advices.

 

i have a question about the:

 

(prin1)

 

what is it the (prin1)? the difference with the (prin)? Sorry if the questions are...stupid, but its 2 weeks that im studying autolisp language.

Link to comment
Share on other sites

what is it the "_non" that you putted to draw the circle? what is it the meaning?

 

Sometimes when the code has to calculate points or location of newly created objects it fails and user ends up with drawed objects on incorrect places.

For example the code randomly snaps to some points where you didn't wanted to. Heres an recent thread with such problem:

http://www.cadtutor.net/forum/showthread.php?98541-Trying-to-create-2-lines-but-autolisp-randomly-overplaces-them.-Why

Then you have 2 options:

- Store the osmode variable, set it to 0 and when you are done, to reset it.

- Simply include "_non" before each point argument within the command call. In your case that variable is " cnt ". for more info about "_non" : http://www.lee-mac.com/scriptwriting.html

 

Heres a example of storing and reseting the osmode system variable:

(defun C:test ( / *error* osm ) ; always localise our variables (unless we know what we are doing)

(defun *error* ; define the error-handler function
	(alert (strcat "\nError occured, now you are inside the *error* function \nand reseting the osmode variable!"))
	(if osm (setvar 'osmode osm)) ; when your code crashes, it will check and reset the osmode value
	(princ)
); defun *error*
(setq osm (getvar 'osmode)) ; store our osmode value into a variable
(setvar 'osmode 0) ; set osmode to 0

; < here the code creates/draws its stuff >
(initget 1) (getstring "\nPress ESC to force an error: ")
(alert (strcat "\nThe code is done, exiting without errors \nand reseting the osmode variable!"))
(setvar 'osmode osm) ; reset the osmode variable
(princ)
); defun

Run the code, type something and press "ENTER", then run the code again and then press "ESC".

Link to comment
Share on other sites

Thanks a lot David for your time!

what is it the (prin1)? the difference with the (prin)?

 

You are welcome.

 

In this case, not much. The aim here is to exit loading and running the program cleanly as far as the command line looks visually.

 

http://forums.augi.com/showthread.php?111705-Difference-Between-PRINT-PRINC-amp-PRIN1

 

IRNEB has pretty simple explanation in post #6.

 

-David

Link to comment
Share on other sites

As a newbie, it's a great time to get into good programming habits :

 

Try to be very consistent in the format you use. It makes searching for phrases or tests later much easier.

 

As this and a lot of peer to peer forums are visited by international language members, using the underscore for command and parameters is helpful. Also the period prior to commands insures that a redefined command is over ridden.

 

AutoLisp is a very human readable language and can be very easy on your eyes if thought is given to the overall layout of the code.

 

Good Luck and Have Fun ! -David

 

PS Look into the [noparse]


[/noparse] tags when posting code in threads[/quote]

I added getkword [] brackets to allow using the mouse to select the size from either the command line or Shortcut Menu:
(defun c:hm (/ cnt pul diam)

 (initget 1)
 (setq cnt (getpoint "\nSelect circle center:   "))

 (initget 1 "1Pul 2Pul")
 (setq pul (getkword"\nDiameter : [1Pul/2Pul]"))

 (setq diam (cond ((= pul "1Pul") 0.033401)
                  ((= pul "2Pul") 0.060325)))

 (command "_.CIRCLE" "_non" cnt "_D" diam)

 (prin1))

 

Not documented well, but really makes selecting options easier. Stuff I learn on the Forums I like to share on the Forums.

Link to comment
Share on other sites

I added getkword [] brackets to allow using the mouse to select the size from either the command line or Shortcut Menu:

 

Not documented well, but really makes selecting options easier. Stuff I learn on the Forums I like to share on the Forums.

 

Never heard of it. But I'm pretty sure I wouldn't like it.

 

I turn off all toolbar / icons / floating thingies. A 2 button mouse with a wheel and a keyboard. And the mouse only picks points 99.9% of the time. My $0.02

 

I finally got 2012 to look decent with help from the folks here.

screen.jpg

Link to comment
Share on other sites

Never heard of it. But I'm pretty sure I wouldn't like it.

 

I turn off all toolbar / icons / floating thingies. A 2 button mouse with a wheel and a keyboard. And the mouse only picks points 99.9% of the time. My $0.02

 

I finally got 2012 to look decent with help from the folks here.

 

You can still type in the option you want same as before and it doesn't use a toolbar or icon, it just makes the options displayed on the command line easier to see and selectable with a mouse or from the right-click Shortcut Menu as an option as well. Just like standard AutoCAD commands like FILLET. To make typing in an option easier the text portion required for each option is displayed blue so it actually makes it easier to do it your way.

 

Check it out!

Link to comment
Share on other sites

I was update the lisp with yours recommendations and tips in this way:

 

(DEFUN c:CPU ( / CNT DIAM INCH)

 (setq CNT (getpoint "\nselect circle center"));click the center of the circle
 
 	(initget 1 "2 2.5 3 3.5 4 6 8 10 12 14 16 18 20")
 	(setq INCH (getkword "\nSelect Diameter: [2 / 2.5 / 3 / 3.5 / 4 / 6 / 8 / 10 / 12 / 14 / 16 / 18 / 20]"))
 	;To do not write the real diameter number

 	(setq DIAM (cond ((= INCH "2") 0.060325)
		 ((= INCH "2.5") 0.073025)
		 ((= INCH "3") 0.0889)
		 ((= INCH "3.5") 0.1016)
		 ((= INCH "4") 0.1143)			 
		 ((= INCH "6") 0.1683)
		 ((= INCH "8") 0.2191)
		 ((= INCH "10") 0.2730)
		 ((= INCH "12") 0.3238)
		 ((= INCH "14") 0.3556)
		 ((= INCH "16") 0.4064)
		 ((= INCH "18") 0.4572)
		 ((= INCH "20") 0.5080)			 
	    );End conditional
 	) ;End setq

  		(command "circle" "_non" CNT "D" DIAM);draw the circle
	
 (PRIN1)
 )

 

and the lisp works as i was needing. After that i was thinking to put the circle in a layer different of the current (selecting an object) and then return in my original layer. So i wrote this new lisp:

 

(DEFUN c:CPU2 ( / CNT DIAM INCH P1)

 		(setq P1 (entsel "\nSelect an object to create the circle in the same layer: "))
 		(setq CNT (getpoint "\nselect circle center"));click the center of the circle
 
 		(initget 1 "2 2.5 3 3.5 4 6 8 10 12 14 16 18 20")
 		(setq INCH (getkword "\nSelect Diameter: [2 / 2.5 / 3 / 3.5 / 4 / 6 / 8 / 10 / 12 / 14 / 16 / 18 / 20]"))
 		;To do not write the real diameter number

 		(setq DIAM (cond ((= INCH "2") 0.060325)
		 ((= INCH "2.5") 0.073025)
		 ((= INCH "3") 0.0889)
		 ((= INCH "3.5") 0.1016)
		 ((= INCH "4") 0.1143)			 
		 ((= INCH "6") 0.1683)
		 ((= INCH "8") 0.2191)
		 ((= INCH "10") 0.2730)
		 ((= INCH "12") 0.3238)
		 ((= INCH "14") 0.3556)
		 ((= INCH "16") 0.4064)
		 ((= INCH "18") 0.4572)
		 ((= INCH "20") 0.5080)			 
	    	   );End conditional
 		) ;End setq DIAM

  		(command "laymcur" P1; to move in other layer with a select
 		(command "circle" "_non" CNT "D" DIAM);draw the circle
	(command "layerp"); return in the original layer
			 
 (PRINC)
 )

 

As usual i was going in the load application of autocad. The program is saying that the lisp is successfully loaded, but when im writing the lisp name in autocad later, the program didnt find the lisp.

If i write CPU, autocad find the first lisp, but if i write CPU2 it doesnt find the second lisp (it finds only the first one. I was trying to load it in another computer, but i was with the same problem. Is it because the new lisp is not correct or what?

Link to comment
Share on other sites

Where is closing bracket here :

 

(command "laymcur" P1; to move in other layer with a select

 

And you are actually supplying list obtained with (entsel), and IMO you should specify ENAME (car P1)...

Never used command "laymcur"; You can achieve the same with :

 

(setvar 'clayer (cdr (assoc 8 (entget (car P1)))))

 

HTH, M.R.

Link to comment
Share on other sites

Where is closing bracket here :

 

(command "laymcur" P1; to move in other layer with a select

 

And you are actually supplying list obtained with (entsel), and IMO you should specify ENAME (car P1)...

Never used command "laymcur"; You can achieve the same with :

 

(setvar 'clayer (cdr (assoc 8 (entget (car P1)))))

 

HTH, M.R.

 

Thanks. Another rookie mistake. I didnt understood how to use the setvar that you wrote me. Could you insert in my lisp? in that way i will try to study it.

With 'clayer, do you mean to use the autocad command CLAYER? In that way, why is it not wrote like "clayer" ? Anyway i cant use the clayer command because the layer name are not easy to remember, with the files i wanna use the lisp

Link to comment
Share on other sites

Cool thing about AutoCAD is there's lots of different ways to do anything.

(vl-load-com)
(DEFUN c:CPU2 ( / OBJLAY CNT INCH DIAM radius circleObj)
 		(setq OBJLAY (cdr (assoc 8 (entget (car (entsel "\nSelect an object to create the circle in the same layer: ")))))
 		      CNT (vlax-3d-point (getpoint "\nSelect circle center: "))		;click the center of the circle
 		)
 
 		(initget 1 "2 2.5 3 3.5 4 6 8 10 12 14 16 18 20")
 		(setq INCH (getkword "\nSelect Diameter: [2 / 2.5 / 3 / 3.5 / 4 / 6 / 8 / 10 / 12 / 14 / 16 / 18 / 20]"))
 		;To do not write the real diameter number

 		(setq DIAM (cond ((= INCH "2") 0.060325)
		 ((= INCH "2.5") 0.073025)
		 ((= INCH "3") 0.0889)
		 ((= INCH "3.5") 0.1016)
		 ((= INCH "4") 0.1143)			 
		 ((= INCH "6") 0.1683)
		 ((= INCH "8") 0.2191)
		 ((= INCH "10") 0.2730)
		 ((= INCH "12") 0.3238)
		 ((= INCH "14") 0.3556)
		 ((= INCH "16") 0.4064)
		 ((= INCH "18") 0.4572)
		 ((= INCH "20") 0.5080)			 
	    	   );End conditional
	    	   radius (/ DIAM 2)
 		) ;End setq DIAM
 		
 		(if (= 1 (getvar 'cvport))
		(setq circleObj (vla-AddCircle (vla-get-PaperSpace (vla-get-ActiveDocument (vlax-get-Acad-Object))) CNT radius))
		(setq circleObj (vla-AddCircle (vla-get-ModelSpace (vla-get-ActiveDocument (vlax-get-Acad-Object))) CNT radius))
 		)
 		(vl-catch-all-apply 'vla-put-Layer (list circleObj OBJLAY))
 (PRINC)
 )

Link to comment
Share on other sites

Heres my revision on that code:

(defun C:CPU ( / e p Lst str r )
(setq e (car (entsel "\nSelect an object to create the circle in the same layer: ")))
(initget 1) (setq p (getpoint "\nSpecify circle's center: "))
(setq Lst 
	(mapcar 'list 
		(mapcar 'rtos (list 2 2.5 3 3.5 4 6 8 10 12 14 16 18 20)) 
		(list 0.060325 0.073025 0.0889 0.1016 0.1143 0.1683 0.2191 0.2730 0.3238 0.3556 0.4064 0.4572 0.5080)
	)
)
(apply '(lambda (x) (initget 1 x)) (list (vl-string-translate "/" " " (setq str (apply 'strcat (mapcar '(lambda (x) (strcat x "/")) (mapcar 'car Lst)))))))
(setq r (/ (nth (vl-position (getkword (strcat "\nSelect Diameter: [" (vl-string-right-trim " /" str) "]")) (mapcar 'car Lst)) (mapcar 'cadr Lst)) 2.))
(and p r (entmakex (list (cons 0 "CIRCLE")(cons 10 p)(if e (assoc 8 (entget e))(cons 8 (getvar 'clayer)))(cons 40 r))))
(princ)
);| defun |; (vl-load-com) (princ)

Edited by Grrr
Link to comment
Share on other sites

Heres my revision on that code:

 

Nice code, here are a few pointers:

 

apply is not required here:

(apply '(lambda (x) (initget 1 x)) (list (vl-string-translate "/" " " (setq str (apply 'strcat (mapcar '(lambda (x) (strcat x "/")) (mapcar 'car Lst)))))))

(initget 1 (vl-string-translate "/" " " (setq str (apply 'strcat (mapcar '(lambda (x) (strcat x "/")) (mapcar 'car Lst))))))

Note that assoc could be used here:

(nth (vl-position (getkword (strcat "\nSelect Diameter: [" (vl-string-right-trim " /" str) "]")) (mapcar 'car Lst)) (mapcar 'cadr Lst))

(cadr (assoc (getkword (strcat "\nSelect Diameter: [" (vl-string-right-trim " /" str) "]")) lst))

Link to comment
Share on other sites

Nice code, here are a few pointers:

Thanks Lee, I try to learn from the best!

 

I'm brainwashing myself with list manipulation and I did not notice that (apply) could be skipped.

I thought that I could use (assoc), but decided to practice with not-so-known-for-me functions, such as (vl-position). :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...