Jump to content

Writing my first lisp


YZ

Recommended Posts

I am attempting my first lisp routine today.

 

I have practiced a little with loading in some samples, and reading them. I can follow (somewhat) the syntax.

 

I don't expect any one to do it for me, but I wonder if someone could steer me in the right direction for which command to use.

 

I want a routine that, among other things, can enquire a particular layer and return true or false whether or not that layer is empty.

 

Thanks!

Link to comment
Share on other sites

I want a routine that, among other things, can enquire a particular layer and return true or false whether or not that layer is empty.

 

Hi,

 

I guess you have started with a bit long codes to check that out if the your would dive into Blocks as well otherwise it is a good start :)

But anyway, here are the steps for you to start coding them:

 

  • Search for your layer name if it is existed in a drawing.
  • Make a global selection set with the layer name only.
  • If the selection set is not nil , so it is equal to True otherwise it is False.

 

NOTE: the above direction points can not test out if the layer name is used by blocks.

Link to comment
Share on other sites

I know that the layer will already exist in the drawing, so that is fine.

 

Although, if it was easier, I could purge the drawing, and then get the lisp to ask if the layer exists.

 

If the layer exists, that is proof enough that it is TRUE that something is on that layer.

Link to comment
Share on other sites

Okay,

 

To check if a layer "MyLayer" is existed and notice that the name of your layer is a string type.

 

(tblsearch "LAYER" "MyLayer")

This should return dxf data that ensure "MyLayer" is already existed otherwise nil which means not found or not existed.

 

After guarantying that our layer "MyLayer" is existed then we make a global selection set filtering for our layer name only.

 

(setq ss (ssget "_X" '((8 . "MyLayer"))))

So the above line of codes should return a selection set of object(s) if object(s) found on our layer name "MyLayer" otherwise would return nil.

Link to comment
Share on other sites

Okay, this is a huge help, thanks!

 

I will put together some code based on what you have started for me.

 

Is there a good place to read some helpful codes to get started? (Apart from the AutoCAD Help full list!)

Link to comment
Share on other sites

You are welcome.

 

Let me know how you get on with the codes and just ask if you have any question.

 

I think firstly you need to read about the basics about functions in AutoLISP and after diving into codes and what they do and return you can search for any task / codes in this forum and if you did not find what you are looking forward , you can ask your question here as you did with this thread and many helpful users would not hesitate to help you out as long as your task is possible to be coded and don't need lots of codes to get the job done.

 

Hit this link => AutoLISP FUNCTIONS

Link to comment
Share on other sites

Thanks again Thawart.

 

I have watched a bunch of videos and read heaps of examples. Things are becoming much clearer!

 

The code I ended up using is:

(setq checkwater (ssget "_A" '((8 . "water"))))

 

I don't know what the "8 ." is for, but it works. Another example replaced that with "0 ." but that doesn't work in this case.

 

Also, the "_A" selects everything in the any layout of that drawing. Is there a way to focus the selection on only the active layout?

Link to comment
Share on other sites

Hi,

 

The number 8 represent the DXF code for Layer name see THIS LINK

To focus a selection set on current active layout , you need to use the DXF 410 with Layout name like this;

 

(setq checkwater (ssget "_A" (list '(8 . "water") (cons 410 (getvar 'CTAB)))))

 

Notice that the System Variable CTAB get the Active layout name that is current and to know how the DXF 410 works have a look again in the previous link that I posted above and search for DXF 410 on the left side hand.

Link to comment
Share on other sites

Wow, that is a helpful link! Thanks.

 

So how would I use that if instead of layers (8) I wanted to focus a selection on all the points in a dwg?

Link to comment
Share on other sites

Wow, that is a helpful link! Thanks.

 

You're welcome.

 

So how would I use that if instead of layers ( 8 ) I wanted to focus a selection on all the points in a dwg?

Just remove the complete filter DXF codes that is for Layer name

(8 . "Mylayer") 

Link to comment
Share on other sites

(ssget "_X" '((0 . "Point")))

 

Yes, this seems to work.

 

And if I needed to perform an action on that selection set? Like erase them? Or move them in to another layer? The ssget might not be the best command to use?

Link to comment
Share on other sites

And if I needed to perform an action on that selection set? Like erase them?

You have two options. first use the command call erase or iterate through the selection set and delete each entity with the use of function entdel.

 

...... Or move them in to another layer?

 

Also you need to iterate through the selection set then move the entity name to any layer name that MUST be existed into your drawing.

 

The ssget might not be the best command to use?

What makes you to think so?

Link to comment
Share on other sites

Wait! I got it:

(command "erase" (ssget "_X" '((0 . "Point")))"")

 

You've been super helpful. Thanks again!

 

That should work if you have Point objects into your drawing. But if there isn't, an error should occur.

 

Try this instead to check first if points are available then delete them with erase command as in your example.

 

(if (setq ss (ssget "_X" '((0 . "Point"))))
 (command "erase" ss "")
 )

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...