Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AutoLisp beginners exercises

20 REPLIES 20
Reply
Message 1 of 21
Anonymous
4710 Views, 20 Replies

AutoLisp beginners exercises

Hei!
I wanna start using AutoLisp. I have gone trough an Autolisp beginners books. Since year 1993. So I would like to move on and improve my knowledge. Does anybody have some good practice exercises to try?
Cheers James
20 REPLIES 20
Message 2 of 21
bhull1985
in reply to: Anonymous

Take a look at the various posts in this forum, you may find good examples within each one.

For instance this is a thread where I had a problem in trying to accomplish a task with minimal knowledge, but through forum replies I was able to complete my task and learn a great deal in the meantime.

 

http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/TRIM-DIFFERING-LENGTHS-BASED-ON-SLIDE...

 

This of course is just 1 thread, but one that I am familiar with, amongst an entire forum of other messages. Good luck in your programming!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 3 of 21
BlackBox_
in reply to: Anonymous

I've only been programming for a few short years, and very much consider myself still somewhat of a beginner... What helped me, so take from this what you will, was to imagine new ideas of what I wanted to happen (within reason), and see if I could bring that creative idea into fruition.

 

For example, back when using Land Desktop, I wanted a routine that would allow me to make offsets in a cross section by selecting points in plan view... Another is to take a task that would previously require I go to each Layout in a given DWG, to perform a task such as locking Viewports, and develop a routine which does this for me without requiring I go to each Layout, etc... Just as a couple of personal examples.

 

Think about what would help you, or your coworkers be more productive, or take a series of difficult, or repetitive tasks, and streamline them.

 

I hope you'll find, as I did, that programming can be a gift... One that is quite powerful when used well.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 4 of 21
bhull1985
in reply to: BlackBox_


BlackBox wrote:

 

Think about what would help you, or your coworkers be more productive, or take a series of difficult, or repetitive tasks, and streamline them.

 

 



and therein is the core of at least 99% what i can see myself learning lisp for

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 5 of 21
BlackBox_
in reply to: bhull1985


@bhull1985 wrote:

@blackbox wrote:

 

Think about what would help you, or your coworkers be more productive, or take a series of difficult, or repetitive tasks, and streamline them.

 

 



and therein is the core of at least 99% what i can see myself learning lisp for


I'm just a CAD user who does production daily, but I've found much more than a hobby in programming... I'm fortunate to have been given much help in the few short years I've been coding... And I strive to give back as often as time permits.

 

Despite my now branching out into the .NET API, I still personally find LISP to be my go-to language when I'm under a deadline, and 'need' some automation now. It's succint. It's elegent. It's fast enough (given the trade off between code performance as compared to .NET API, and the time it takes me to write the code itself, include the error handling, etc..). Win-win for daily use in my [Black-]book.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 6 of 21
hmsilva
in reply to: BlackBox_


@BlackBox_ wrote:

I'm just a CAD user who does production daily, but I've found much more than a hobby in programming... I'm fortunate to have been given much help in the few short years I've been coding... And I strive to give back as often as time permits.

...


 

Kudos:

1+ Smiley Happy

 

Cheers

Henrique

EESignature

Message 7 of 21
BlackBox_
in reply to: hmsilva


@hmsilva wrote:

@BlackBox_ wrote:

I'm just a CAD user who does production daily, but I've found much more than a hobby in programming... I'm fortunate to have been given much help in the few short years I've been coding... And I strive to give back as often as time permits.

...


 

Kudos:

1+ Smiley Happy

 

Cheers

Henrique


That is very kind of you, Henrique - If only I had more friends like you here.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 8 of 21
hmsilva
in reply to: BlackBox_


@BlackBox_ wrote:
...
If only I had more friends like you here.

 ...


A life journey isn't made only in one forum...
But, I am sure you have.

 

Cheers
Henrique

EESignature

Message 9 of 21
BlackBox_
in reply to: hmsilva


@hmsilva wrote:

@BlackBox_ wrote:
...
If only I had more friends like you here.

 ...


A life journey isn't made only in one forum...
But, I am sure you have.

 

Cheers
Henrique


We'll leave it at that, methinks... This too shall pass.

 

 

 

"There are two ways of spreading light, to be the candle or the mirror that reflects it." - Edith Wharton

 

"One who is offended by truth, has no place among those who seek wisdom." - RenderMan



"How we think determines what we do, and what we do determines what we get."

Message 10 of 21
hmsilva
in reply to: BlackBox_

@BlackBox
Is nice to hear the RenderMan again! Smiley Wink

 

@Einsuner30
my apologies for hijack this thread! Smiley Embarassed

 

Welcome to the Autodesk Forums!

 

In addition to what have been written, a good way to improve your knowledge, is stay in this forum, trying to solve the problems/questions that are posted, and learn from the solutions...

 

Henrique

EESignature

Message 11 of 21
Anonymous
in reply to: hmsilva

 
Message 12 of 21
Anonymous
in reply to: hmsilva

Ok!

here is one thing that i have in mind.

i wanna greate an application.
Sipmple explanation is:
It should open many drawings, find certain block attribute in it, change it, save the drawing and that is it
I'm very beginner in Autolisp, but i read an old book and i understand a bit about that.
So next steps must be made:
First i should make a dialog, where i can chosse certain drawings
2. program should open these drawings
3. It should start searching block attribitues in drawings
4. open these attributes, do some if functions and then change the data and close the block attribiutes
4. Save the drawing and move on to other.
So i should start with first one and try to make dialog and open these drawings?
Right?
Any good examples for that?
Cheers
martin
Message 13 of 21
bhull1985
in reply to: Anonymous

To do that you'd need a few different files. A script, a .bat, and your lisp routine.

A .bat file will be the file that control the action behind windows (the operating system), it's what you will use to invoke your .scr (script) which in turn will invoke your lisp routine and the command that runs it, along with any other controls you'd like to modify each drawing with such as Zoom+extents, Qsave and Quit.

The lsp routine will do most of the work as both of the other pieces (the .bat and the .scr) are meant to solely allow your routine to operate correctly on a given set of drawings.

 

The syntax for the .bat file will be something similar to :

 

FOR %%f in (C:YOURDRIVEPATH/*.dwg) do start /wait C:\"Program Files"\Autodesk\"AutoCAD Mechanical 2012"\acad.exe "%%f" /b C:/YOURSCRIPTDRIVE/plotst.scr

 

 

What this is saying is for each drawing in the folder pointed to start/wait Autocad, opening each drawing sequentially, and executing plotst.scr onto each one. Start/wait will cause autocad to open the first drawing until autocad is closed again before it moves down the list to the next %%f (drawing in your folder). So by attaching the script to this .bat we allow full automation in so far as opening each drawing in a folder and having the lsp ran on it. The users will have to manually enter the information required by your routine for each drawing but if you have a "standard" change such as changing the various dates on stamps to one date, then you could hard-code that into your lsp and if it requires no user input, it would run through all of the drawings in your folder without requiring a single keystroke beyond that which starts the batch.

 

The script would also be a supplemental few commands that ready the autocad for executing your lisp. The .scr that runs off of the .bat I posted above reads:

 

(load "Y:/bhull/plotst.lsp")
mcp
zoom extents
QSAVE
QUIT

 

with of course the carriage return after "QUIT" which cannot be forgotten. As you can see all this is doing is loading the relevant lisp routine, then executing it, before resetting the drawing focus and saving, then issuing the QUIT and return keystroke which forces acad to shut down after saving it's changes, and thereby forcing the start/wait action in the .bat file to open acad but with the next file.

 

Your routine can be found on the various forums for autolisp, as there are a variety of block attribute replacement lisps. Find one that is suitable and then use the two files outlined here to automate it all together.

 

Cheers, hope this is useful.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 14 of 21
Lee_Mac
in reply to: bhull1985


@bhull1985 wrote:

To do that you'd need a few different files. A script, a .bat, and your lisp routine.

A .bat file will be the file that control the action behind windows (the operating system), it's what you will use to invoke your .scr (script) which in turn will invoke your lisp routine and the command that runs it, along with any other controls you'd like to modify each drawing with such as Zoom+extents, Qsave and Quit.


I would strongly advise against the use of a Batch file to iterate over a set of drawings to be processed - this form of batch processing can be easily achieved using only an AutoCAD Script and suitable AutoLISP program (indeed, for some minor tasks, the AutoLISP program may not even be required).

 

By using a Batch file, it becomes necessary to repeatedly terminate & launch the entire AutoCAD application, a process that is incredibly slow & inefficient; whereas, when using an AutoCAD Script file to iterate over a set of drawings, a single instance of the AutoCAD application is all that is required and drawings may be closed and opened within the same session.

 

This is in fact the method that I use when performing an operation to modify a set of drawings (that is, when ObjectDBX is unsuitable for the task). By using a Script file in conjunction with an AutoLISP program, one can exploit the batch processing ability of the Script whilst harnessing the superior power, control & error trapping afforded by an AutoLISP program. This method is further demonstrated by my Batch Attribute Editor program.

Message 15 of 21
bhull1985
in reply to: Lee_Mac

Lee Mac is one end of the spectrum, probably one of the most advanced coders on these forums (from what I've been able to tell at least) , and as such his methods are surely correct over mine. You may still want to look into the various ways to do this so that you may find one you are capable of handling and modifying and suiting for your needs. Unless of course you're able to decipher advanced code without understanding the principles behind it.

 

Perhaps he would be so kind as to explain to the newer people *HOW* it's possible to have a script do what was described there? As the wheel was invented before the ferrari, and the title clearly states "Autolisp beginners exercises", that may be helpful.

 

I'm just one beginner , trying to help another....

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 16 of 21
BlackBox_
in reply to: Lee_Mac

 

@ Einsuner30 - 

 

In deed; welcome to the forums! :beer: ... My apologies for the tangent.

 

Cheers

 

 

 

@ Lee -


@Lee_Mac wrote:
 By using a Script file in conjunction with an AutoLISP program, one can exploit the batch processing ability of the Script whilst harnessing the superior power, control & error trapping afforded by an AutoLISP program.

... This has 'new LISP book author' written all over it; just saying.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 17 of 21
Lee_Mac
in reply to: bhull1985


@bhull1985 wrote:

 

Perhaps he would be so kind as to explain to the newer people *HOW* it's possible to have a script do what was described there? As the wheel was invented before the ferrari, and the title clearly states "Autolisp beginners exercises", that may be helpful.


Sure -

 

To follow the method I have described above, you would essentially require two files: the AutoCAD Script to open, load & run the AutoLISP program, save & close each drawing to be processed; and an AutoLISP file containing the program or function to be evaluated on each drawing.

 

As noted earlier, for relatively simple tasks, the separate AutoLISP program may not even be required, since the operations could be performed directly using the Script (since a Script file is merely a list of AutoCAD commands to be automatically entered consecutively at the command-line) - indeed, without access to AutoLISP customisation, this is how those unfortunate LT users would need to proceed.

 

To give an example, consider the following AutoLISP function:

 

(defun myfunction ( )
    (entmake '((0 . "LINE") (10 0.0 0.0 0.0) (11 1.0 1.0 0.0)))
    (princ)
)

 

This function will simply construct a LINE entity in the active drawing layout from (0,0) to (1,1). You can test this by loading the code and typing (myfunction) at the AutoCAD command-line to evaluate the function.

 

Of course, this is an incredibly simple example, but the program could potentially be any program or function which does not require user input (there is one exception to this rule: since, as noted above, a Script file is merely a list of AutoCAD commands entered at the command-line, if the AutoLISP program submits a nil to the command-line, to cancel a command, this will terminate the Script evaluation).

 

Now, let's assume that we wish to evaluate the above function on a set of drawings.

 

Consider the following Script file:

 

_.open "C:/Drawing1.dwg" (load "myfunction.lsp" nil) (if myfunction (myfunction)) _.qsave _.close
_.open "C:/Drawing2.dwg" (load "myfunction.lsp" nil) (if myfunction (myfunction)) _.qsave _.close
_.open "C:/Drawing3.dwg" (load "myfunction.lsp" nil) (if myfunction (myfunction)) _.qsave _.close

 

The above Script assumes that the aforementioned AutoLISP function has been saved to an AutoLISP file with filename myfunction.lsp and that this file resides either in the same directory as the set of drawings to be processed [C:] or within an AutoCAD Support File Search Path.

 

The Script itself would be saved with an arbitrary filename and in an arbitrary location, but with a .scr extension.

 

When analysing the Script file itself, note that a space or new-line in a Script represents the user pressing Enter at the command-line. The fact that the operations for each drawing are on a new-line in the Script is merely for clarity and convenience - all operations could potentially be on the same line, separated by spaces; or each operation could be on a new line in the file. More information on Script Writing, including the meaning of the command prefixes shown above, can be found in my tutorial here.

 

So, what does the Script do?

 

The Script should be run using the AutoCAD SCRIPT command in a new blank drawing. The Script will then start the OPEN command and will open the drawing with filename "C:/Drawing1.dwg" (double-quotes are only necessary if the filename contains spaces (since these would otherwise be interpreted as part of the Script), however, in my opinion, it is easier to include them every time).

 

The Script will then attempt to load the AutoLISP file myfunction.lsp, and will return nil if this file cannot be loaded. Note that the second optional argument for the AutoLISP load function has been supplied to ensure that the load function does not error if the AutoLISP file in question cannot be loaded.

 

The Script file the includes a conditional if statement testing whether the symbol myfunction evaluates to a non-nil value, i.e. whether the function myfunction is defined. (Aside: the reason that the if statement is not testing the return of the load expression is because one cannot be guaranteed that the final expression evaluated within a loaded AutoLISP file will return a non-nil value, hence it is safe to test whether the function has in fact been defined following the load).

 

If the function is indeed defined, the Script will evaluate the AutoLISP function to operate on the opened drawing.

 

Following completion of the AutoLISP program (since AutoLISP does not permit the use of multiple processor threads, the Script must wait until the AutoLISP function has completed evaluation before proceeding), the Script will then issue the QSAVE command to save the opened drawing, followed by the CLOSE command to close the open drawing and return to the blank new drawing from which the Script was initiated.

 

Note that there is no space following the CLOSE command, since, as noted above, the new-line also represents the user issuing Enter at the command-line, and will ensure the CLOSE command is executed.

 

Now, the Script will move to the next line and commence the OPEN command to process the next drawing.

 

Of course, this is the bare minimum requirements for this method of batch processing - there are many enhancements which may be implemented to streamline the procedure.

 

For example, rather than writing the Script manually, the first improvement could be to utilise an AutoLISP program to construct the Script - indeed, this is the motivation behind my Script Writer program.

 

A natural progression would then be to also automatically write the AutoLISP program to an AutoCAD Support File Search Path, leaving the user with one less item to worry about in the procedure.

 

Finally, the whole process can be wrapped up with a fancy GUI, allowing the user to indirectly tailor the AutoLISP function to be evaluated on each drawing, and with prompts for drawing selection allowing the user to indirectly tailor the generation of the Script file - these concepts are aptly demonstrated by my Batch Attribute Editor program suggested earlier.

 

I hope this explanation is clear & sufficiently meets your expectations.

Message 18 of 21
Lee_Mac
in reply to: BlackBox_


@BlackBox_ wrote:

@Lee_Mac wrote:

By using a Script file in conjunction with an AutoLISP program, one can exploit the batch processing ability of the Script whilst harnessing the superior power, control & error trapping afforded by an AutoLISP program.


... This has 'new LISP book author' written all over it; just saying.


 

I think I just wrote it...

Message 19 of 21
bhull1985
in reply to: Lee_Mac

Ahhh, I was just reading another thread full of knowledge from you...

http://www.cadtutor.net/forum/showthread.php?81195-Getting-help-from-the-Autocad-ActiveX-and-VBA-ref...

 

And it's great to see you having taken the time to explain..... Now, to indulge...

Thanks Lee Mac !

 

You were very sufficient in your explination. That's for sure, and now the OP has some great meat to sink their teeth into when ready to tackle this subject as they expressed wanting to do in a previous post. Now not only do they have the required information to begin creating their own version, but myself and anyone else not-nearly-experienced as you have something to absorb and learn from, which I believe is the intent of this thread entirely. With my menial knowledge I took the time to explain what bits of information I know that would have been useful for the OPs question, in hopes that they would learn something. That was my only motive, and misguided as it might have been, it's brought to light the more correct way to accomplish this task. If you're going to tell me i'm wrong in my thinking it'd be good to know why, as you've done in this explination. That was my only expectation, and you've exceeded it indeed. We (myself and any other newbies) could learn a great deal from you....

 

There are of course your tutorials which I will link to for anyone interested.

 

There are also the cadtutor forums for autolisp

 

 the swamp is another good autolisp forum

 

the forums at augi are another

 

Along with the forum we're in are what i'm finding to be the most effective in finding information about autolisp but again, nothing beats personalized responses such as yours previous. Hopefully the OP appreciates as much as I do. Thanks for showing how a script could emulate the batch file without leaving autocad.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 20 of 21
Lee_Mac
in reply to: bhull1985

Thank you for your kind words bhull Smiley Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost