Jump to content
dak

Make a loop return the result for each time its body is obeyed

Recommended Posts

dak

Hi everyone,

 

Just trying to pass a list of points to a "pline" command.

The problem is num of points is initially unknown, though being calculated within a routine.

The following code (as smth to start with) evolves known num of points.

(defun c:pltest6 ()
(setq pts (list '(0 0) '(0 10) '(10 10) '(10 20) '(20 20) '(20 30) '(30 30)))
(setq ptqty 7)
(setq ptnum 0)
(command "pline"
	(car pts)
	(pt_retr pts)
	(pt_retr pts)
	(pt_retr pts)
	(pt_retr pts)
	(pt_retr pts)
	(pt_retr pts)
	""
)
)
(defun pt_retr (lst)
(setq ptnum (1+ ptnum))
(repeat ptnum
	(setq lst (cdr lst))
)
(setq pt (car lst))
)

Repeat/foreach don't do, 'cause they return the value of the last iteration..

Now my Q is,

is it possible to modify the code above to use loop instead of repeat one and the same line (moreover, unknown number of times :shock:),

OR some other approach would rather fix the task?

 

Thank you in advance,

Dmitro

Share this post


Link to post
Share on other sites
BlackBox
(defun c:FOO (/ _FOO pts)

 (defun _FOO (pts /)
   (command "._pline") (foreach pt pts (command pt)) (command)
 )

 (if (setq pts '((0 0)(0 10)(10 10)(10 20)(20 20)(20 30)(30 30)))
   (_FOO pts)
 )
 (princ)
)

Share this post


Link to post
Share on other sites
Lee Mac

Example:

(defun c:pline-example ( / lst )
   (setq lst '((0 0) (0 10) (10 10) (10 20) (20 20) (20 30) (30 30)))
   (apply 'command (append '("_.pline") lst '("")))
   (princ)
)

Share this post


Link to post
Share on other sites
fixo

Or

(command "_pline")(mapcar 'command pt)(command "_cl")

to make them closed, of course osmode is set to 0 before

Share this post


Link to post
Share on other sites
dak

BlackBox, Lee Mac & fixo,

 

Thank you so much.

I see I need 1,000 x more practice to get accustomed to lisp.

Sorry if you found my question borring.

 

Regards,

Dmitro

 

P.S. Does there exist a Forum/Thread where LISP is compared with another languages?

E.g. "THAT's C/Pascal/Basic code - and THIS is how we do it in LISP".

Share this post


Link to post
Share on other sites
neophoible
P.S. Does there exist a Forum/Thread where LISP is compared with another languages?

E.g. "THAT's C/Pascal/Basic code - and THIS is how we do it in LISP".

You might ought to ask this in a separate thread. But note that AutoLISP and Visual LISP are not really LISP, but dialects of it intended specifically for AutoCAD. Also, LISP is not the same type of language, so if you want to do it "right", it may be better not to try to think too much in terms of such languages. BTW, interestingly, no one mentioned that while would be a substitute looping function for repeat when stepping through a variable number of items, or when the number is of no importance. Could this be because AutoLISP works differently than the other languages you mention?

Share this post


Link to post
Share on other sites
neophoible
Or
(command "_pline")(mapcar 'command pt)(command "_cl")

to make them closed, of course osmode is set to 0 before

Good point about OSMODE! Why not just apply instead of mapcar?

(command "_pline")(apply 'command pts)(command "")

Or were you just showing that there is yet another function option?

Share this post


Link to post
Share on other sites
BlackBox

P.S. Does there exist a Forum/Thread where LISP is compared with another languages?

E.g. "THAT's C/Pascal/Basic code - and THIS is how we do it in LISP".

 

There is no specific place for that sort of comparison, but it makes logical sense to want to understand how to accomplish similar task in different languages... In terms of AutoCAD, methinks the most common is quickly becoming Visual LISP vs. .NET (predominantly C# from what I see, but also some VB.NET, and F#)... I suspect you might see more VBA now that VBA 7.1 is supported.

 

As an aside, only when I was adept at coding LISP did I make the leap into the deep end of the .NET API, teaching myself C# & VB... It is then that I truly realized just how elegant LISP code can be, despite it's performance hit, with it's utter lack of Transactions, DocumentLocks, static Fields, permissions, etc.... It made me appreciate LISP even more than I thought I already had.

 

If you really have something in mind that 'here's how I do this in LISP, how can I do the same in .NET?' etc., post a thread in the .NET forum, include your LISP code, and we'll help you out.

 

Cheers

Share this post


Link to post
Share on other sites
dak

neophoible,

 

You right this could not of course! :D ; post #6 is concerned

 

My problem was in fact that I had no idea of how I would "screw" loops to the "pline" command. And that was because since you used to code a great deal in some language, you have adopted its specific tricks. In this case I concluded list functions rule 8) (which was not obvious for me when I raised this thread).

 

Regards,

Dmitro

Share this post


Link to post
Share on other sites
neophoible
neophoible,

 

You right this could not of course! :D ; post #6 is concerned

 

My problem was in fact that I had no idea of how I would "screw" loops to the "pline" command. And that was because since you used to code a great deal in some language, you have adopted its specific tricks. In this case I concluded list functions rule 8) (which was not obvious for me when I raised this thread).

 

Regards,

Dmitro

Actually, what was not obvious to me, until I saw someone do it, is that you can split a command up with multiple calls to command. That was a great revelation for me. FWIW, here is a trivial example of how while could be used:

(defun C:Q (/ P)
 (command "_.PLINE")
 (while
   (setq P (getpoint))
   (command P)
 )
 (command "")
)

Share this post


Link to post
Share on other sites
neophoible
There is no specific place for that sort of comparison, but it makes logical sense to want to understand how to accomplish similar task in different languages...
Yes, that does make sense.
...It made me appreciate LISP even more than I thought I already had.
In this case I concluded list functions rule 8)

Yeah, from what I've read (meaning I really don't know anything about it myself!), after half a century plus, other languages (or their descendants) are starting to catch up to LISP.:lol::P I vaguely recall decades ago when a well-meaning friend enthusiastically but futilely tried to explain its recursive powers. Alas, I'm a bit slow. All I could think was, Once is more than enough for me. I mean, who wants to be repeatedly cursed?:shock:Him::facepalm::facepalm::facepalm: But then I met AutoCAD.

Share this post


Link to post
Share on other sites
BlackBox

LISP is great generally, and a good place to start if (like me) you've never written code before, methinks.

 

Mode mode = this.Mode;
this.Mode = Modes.Rant;

 

My issues with LISP are not issues with the language, but with the VLIDE, lack of Autodesk enhancement to LispFunction Methods (especially for verticals like Civil 3D), etc..

 

this.Mode = mode;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×