Jump to content
steven-g

My first Lisp

Recommended Posts

steven-g

After the last six months using full Autocad I have finally taken the plunge into Lisp (there's still life in the old dog yet)

So here is my first efforts it's actually designed to help me in Bricscad but Lisp is Lisp.

No question, I am looking for hard critical assesment on any point, if I'm going down this path I really don't want to start with bad habits, so please be harsh if needed, I'm thick skinned, some would even say "thick in head" (at least they would in Barnsley) and to be honest I'd tend to agree this isn't easy for me to pick up. The hardest part is reading Lisp, is it now a command, a keyword, a variable or what? at least in Excel with VBA you get some color coding clues in the editor.

VisLisp.lsp

Share this post


Link to post
Share on other sites
Dadgad

Congratulations steven-g on taking the plunge.

In light of some of the really interesting things you have done, and shared in the past, I have no doubt that you will quickly come to grips with it. :beer:

I am reasonably sure you will have researched the wealth of good information, functions and the like which Lee Mac provides on his site.

Share this post


Link to post
Share on other sites
BIGAL
Posted (edited)

As your starting your in the best position, some of the best are here at cadtutor. Lee-mac of course Tharwat, AlanJT, Ronjonp to mention a few.

 

Anyway back on track think about when writing keep stds like p1 pt1 are points txt txt1 are text and so forth. When I was taught programing some 30 + years ago you have stuff like strings numbers, reals and so forth reals X Y integers I J K so be consistent with your variable names, use defuns like Lee, LM: SG: then you know you did it.

 

When taught programming one of the important steps is using library functions so think about making a library rather than just continuously copying and pasting code. If you find your repeating steps then make it a defun.

 

On a commercial package I was involved in setting up a library as the 1st step was critical for all the layers created you drew an object it always went on the correct layer. The extra step was that the end user defined the layer properties used. It meant a few lines of code not 50+. Think about using data files TXT to store lots of info I have some with 200+ lines that we use in conjunction with our CIV3D.

 

Most of the posts here are 1 off's so no real use of a library. But in your end user world think about where your working and dont get into the habit of copy and pasting, I am sure some will disagree.

 

I know about one bit of coding submitted here and I sent it back with 6 defuns as the code continually repeated the same steps. The thing was that I had created library routines saving coding time and ensuring the code worked every time.

 

You can take advantage of complete routines by people like Lee by just 1 line (if ( not LM:xxx)(Load "LMXXX)) no need to copy and paste into every routine.

 

I have started using dcl's for input where its more than 1 or 2 requests and prefill whenever possible. Again a library function.

 

Lastly don't be like me and expect the user to do the right thing try to add some error checking did they pick a block ?

 

(defun SG:visibility ( )
(defun C:visoff ()

.......
)

 

Wrap your code in a defun then use (if (not sg:visibilty)(Load "vislisp")) hey its a library routine 1 line in your new code.

Edited by BIGAL

Share this post


Link to post
Share on other sites
Tharwat

Thank you BIGAL for your nice words.

 

@Steven-g, Why did you go with acet-* functions while with AutoLISP vanilla can get the job done the same way and guarantee the routine would work in all releases of AutoCAD?

So you can highlight / select the objects with the use of this function SSSETFIRST

 

Happy coding. :)

Share this post


Link to post
Share on other sites
Grrr

Hi, Steven

 

No question, I am looking for hard critical assesment on any point, if I'm going down this path I really don't want to start with bad habits, so please be harsh if needed

 

First very important thing is to localise the variables you use, you probably know this as 'declaring variables' in VBA:

 

(defun C:visoff ( / [b][color=Red]ss1[/color][/b] )
 (setq ss1 nil) ; <- you don't need this BTW
 (setq ss1 (ssget))
 (acet-ss-visible ss1 1)
)

 

What this means that when the routine finished with/without error the variables you localised are reset to nil.

Heres a more elaborated explanation by Lee Mac: Localising Variables

 

Second thing you probably know, that has to be done is to use some conditional functions like the if function:

 

(defun C:visoff ( / ss1 )
 [b][color=Red](if[/color][/b] (setq ss1 (ssget))
   (acet-ss-visible ss1 1)
 [b][color=Red]); if[/color][/b]
)

 

Although acet-ss-visible can handle null values, and does't crashes with error - overall its a good practice to make use of the conditionals.

Somewhere was written "never trust the user", which means theres always a chance to pass the wrong type of inputs.

 

The hardest part is reading Lisp, is it now a command, a keyword, a variable or what? at least in Excel with VBA you get some color coding clues in the editor.

 

If you practice alot you'll get used to it, btw VLIDE's console gives enough hints:

 

_$ (type if)
SUBR
_$ (type (lambda (x) x))
USUBR
_$ (type "this is a string")
STR
_$ (type 1)
INT
_$ (type 1.)
REAL
_$ (type 'a)
SYM
_$ (type nil)
nil

 

Also try not to rely on the acet-* functions.

The best experience would be to practice here on the forum and post your codes, so you'll recieve advices/critics and build-up your skill.

You might find this thread kinda related,

and you can see how I learnt something new there thanks to Tharwat and LM (Now I feel that thread so old, like its been from 2012).

Share this post


Link to post
Share on other sites
Tharwat

Thank you Grrr for the nice words.

 

Actually the functions acet-ss-visible won't error if the selection set is empty / nil so the conditional statement is that required in this case but its recommended. :P

Share this post


Link to post
Share on other sites
Grrr
Thank you Grrr for the nice words.

 

Thank you for the constant help all these years!

 

 

Actually the functions acet-ss-visible won't error if the selection set is empty / nil so the conditional statement is that required in this case but its recommended. :P

 

I kinda ment the same (but with different words) :P :

 

Although acet-ss-visible can handle null values, and does't crashes with error - overall its a good practice to make use of the conditionals.

Somewhere was written "never trust the user", which means theres always a chance to pass the wrong type of inputs.

 

Still if Steven coded in VBA he probably would have a good base.

Share this post


Link to post
Share on other sites
BIGAL

A quick one we use a lot of menu's to start a lisp and then want to run it again later where user knows command.

 

(defun c:xxxx ( / ......
.........
)
(c:xxxx) ; by adding this at end it will run after load so no need to type 
at command line note c: must be full defun name

same
(defun xyzc ( / ....
....
)
(xyzc) ; non command line version but makes use of localised varaiables.

Share this post


Link to post
Share on other sites
PDuMont

and down the rabbit hole he goes... ;)

Share this post


Link to post
Share on other sites
rlx

Just start simple (kiss : keep it simple stupid) and steal others programs, make a few changes and see what happens and document (not only for yourself) what it is you want to achieve and / or have changed... but mostly, try not to be intimidated by people with more experience (or brain cells)

 

 

:beer:

Share this post


Link to post
Share on other sites
steven-g

Coding and programming are very big words when applied to me, my very first computer was a 'Sinclair ZX81' which gives a pretty good clue as to which year that was, and I started straight away with Basic and the many variations that followed it, I also did a fair bit of machine language programming which ended when windows came into popular usage, And I progressed onto VBA because it felt natural. But I have a definite go round and kick the tires approach and if anything drops off that doesn't look important then it can probably be ditched.

 

Dadgad

Thank you for the encouragement I have regularly been looking trying to decipher what all this Lisp is about but it's like the Lisp Forum is only used by people speaking another language up to now, it's only just starting to feel like the veil is lifting.

 

BIGAL

I'm ashamed to say you lost me on the first post, that's how little I know, just with the line (defun SG:visibility () I was under the impression that the C: was to let Autocad know the name of the Command and you just blew that away by using SG:visibility, but actually the rest of the post does make a lot of sense.

 

Tharwat

The acet-ss-visible wasn't so much a concious choice it was the only way I found of performing a reinstatement of visibility for a selection and didn't involve just making everything visible again as in unhide, the reason I am trying to set this up is that in a busy drawing I can keep selecting odd iems and hide them, perform some actions then hide something else to get at other entities and then further down the line I might need to just be able to see a couple of objects again to see how thigs are progressing but the normal unhide from the ribbon will only unhide everyhthing. And in Autocad there is no way to select just a few items at random once they are no longer visible, in Bricscad you can, but unhide is still all or nothing, so the actual selection sets have no real value beyond "hide this now" and "show that then" it is an unlikely scenario that the two will act on the same iems and I havent found a method to unhide just a selection no matter how you select it, without using visibility.

 

Grrr

That makes sense about the variables and your color coding in just those couple of examples actually has cleared up a lot in my mind, to tell the truth, I hadn't tried using the VLIDE console and it does make things quite a bit clearer so I'm going to study that some more. But Bricscad's BLADE looks very interesting as well. And I will take the time to study Lee's site I have tried before but to be honest, it always lost me, but now I have some actual real problems to try and work out it's no longer just theory and it is all actually beginning to make a bit of sense.

 

rlx

All my best stuff in VBA is stolen (or should I say borrowed) and if I was intimidated by people with more brain cells than me I'd never leave the house.

 

PDuMont

It feels more like into the snake pit, but I'm in there now so no climbing out.

 

It's a breakthrough moment for me, just this silly first attempt probably already saved me half an hour just today, and when I can get this into a toolbar along with just a few other items it is going to make such a big difference.

Share this post


Link to post
Share on other sites
rlx

Sometimes the tiniest of things make the biggest difference. Half or most of us are just copy-cats (not me of course , I'm a copy-dragon haha) and of course you have the ones who , how can I put it mildly , just want to suck your brain dry :rtfm:

 

 

:P but I'm glad you want to :book: :thumbsup: :beer:

Share this post


Link to post
Share on other sites
Grrr
Half or most of us are just copy-cats (not me of course , I'm a copy-dragon haha)

 

:lol:

 

Thats true, attempting to do so many stuff without enough knowledge/experience only by yourself will lead to alot of wasted time (which inefficient for the 21st Century).

BTW I prefer the 'murican proverb: "Monkey see, monkey do".

Share this post


Link to post
Share on other sites
rlx

I wouldn't know about the monkey business because when I see one , cause of my dragon instinct , I would have eaten it before he / she could have said anything haha But apart from that I'm completely normal ... for a dragon that is :P

 

oh sorry :offtopic: :sick::mrgreen::censored:

Share this post


Link to post
Share on other sites
Grrr
:offtopic: :lol: The copy dragon Rlx eats the copy-cats and the copy monkies for breakfast!

Share this post


Link to post
Share on other sites
BIGAL
Posted (edited)

Another suggestion is don't forget about a piece of paper and pencil , when doing complicated editing using points I often draw a sketch and keep track of my point numbers and which object their pointing at. Bit like the old fashioned flow chart.

 

When dealing with angles its a good idea to work in radians, be well aware that Autocad expects zero as EAST and anticlockwise, this can catch you out, a simple way around is to use setvar's to match then return at end.

 

Converting VBA to VLISP is not to hard they are very similar.

 

Give some hints on what you want to automate. If you do it repeatedly then automate it.

 

SG:visibility was about using a library lisp and loading as required again for us it would be a toolbar or menu option maybe. Look into "Autoload" this will demand load a lisp via a command in our case its part of our start up lisp. Thats another area to look at also is lisps loaded on startup can be really simple, add to acaddoc.lsp

 

This will be your best friend if using VL there are others out there that go deeper.

 

(defun C:DumpIt ( / ent) 
 (while (setq ent (entsel)) 
   (vlax-Dump-Object 
     (vlax-Ename->Vla-Object (car ent)) T
   ) 
 ) 
 (princ) 
)

Edited by BIGAL

Share this post


Link to post
Share on other sites
hanhphuc

This thread reminds my 1st VLIDE experience the only thing i recognize was the

CAR :P

have you noticed at the search box ?

what the heck was that CAR for?

 

ie: Familiarizing the functions is fundamental & important :)

 

 

p/s : CAR - Content of Address Register

Share this post


Link to post
Share on other sites
steven-g
Posted (edited)

This will be your best friend if using VL there are others out there that go deeper.

 

(defun C:DumpIt ( / ent) 
 (while (setq ent (entsel)) 
   (vlax-Dump-Object 
     (vlax-Ename->Vla-Object (car ent)) T
   ) 
 ) 
 (princ) 
)

Thanks BIGAL I also found PRINTDXF on the Autodesk webpages which lists an awful lot of information about entities, I can see this is going to be hard going, just reading all the properties never mind actually trying to pick them out for use in selections etc. One of the next tasks I have set myself is selecting the faces of solids that all face the same way (ie all have the same normal) I'm just trying to figure that one out now.

 

 

@hanhphuc CAR? thats what gets me to work on a morning, which does bring up a valid point, for me at least it's confussing sometimes when odd naming conventions show up, it took me a long time to realise that just something simple like ss can sometimes be used in a keyword such as

(setq [color=black]ss[/color] ([color=blue]ss[/color]get)

and other times it actually represents a variable

(setq [color=magenta]ss[/color] (ssget)

and as such could just as easily be

(setq [color=magenta]PinkElephant[/color] (ssget)

but wouldn't then change to

(setq [color=magenta]PinkElephant[/color] ([color=magenta]PinkElephant[/color]get)

and I kept on trying to find the second PinkElephant in all the code I was seeing, (if that makes sense) and indeed using an actual code editor does make it a lot easier to follow.

 

 

Following on from my original code above, I did do some more digging and changed the code I am using to this which uses the "hideobjects/isolateobjects" commands which is a better method because everything is restored when you close a dwg unlike the "visible" attribute which may well leave my collegues scratching their heads when half a drawing is missing :shock:

[color=lime];[/color][color=lime]Based on this thread and BeekeeCZ's input [/color][url="https://forums.autodesk.com/t5/autocad-mep-forum/hidden-objects-list/td-p/6602446"][color=lime]https://forums.autodesk.com/t5/autocad-mep-forum/hidden-objects-list/td-p/6602446[/color][/url]
([color=blue]defun[/color] C:uhi ([color=blue]/[/color] ss ss1)
 ([color=blue]setq[/color] ss ([color=blue]ssget[/color]))
 ([color=blue]command[/color] [color=lime]"_.plan" ""[color=black])[/color][/color]
 ([color=blue]setq[/color] ss1 ([color=blue]ssget[/color] "_C" ([color=blue]getvar[/color] 'EXTMIN) ([color=blue]getvar[/color] 'EXTMAX)))
 ([color=blue]command[/color] [color=lime]"_.ZOOM" "_P"[color=black])[/color][/color]
 ([color=blue]command[/color] [color=lime]"unhide"[color=black])[/color][/color]
 ([color=blue]command[/color] [color=lime]"isolate"[/color] ss ss1 [color=lime]""[/color])
)

Edited by steven-g
Fixed code

Share this post


Link to post
Share on other sites
BIGAL
Posted (edited)

C:uhi is missing a closing bracket, you only posted as a sample ? :o

 

Just me type (defun xxx then a couple of blank lines then ) this way defuns, Ifs, etc are always closed when you get (if (and (or can get lost in bracket translation.

 

You seem to be doing the same thing twice if you pick all in the selection ss its the same as using extents in ss1 ? Even if you pick a few the extents selection just doubles up the selection. Trying to isolate the same layer twice.

(setq ss (ssget))

<Selection set: 30>
Command: (sslength ss)
52

Command: PLAN
Command: (setq ss1 (ssget "_C" (getvar 'EXTMIN) (getvar 'EXTMAX)))
<Selection set: 33>

Command: (sslength ss1)
52

Edited by BIGAL

Share this post


Link to post
Share on other sites
steven-g

Oops fixed now, it got lost in copy paste.

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

×