• Home
  • Training Books
  • Subscribe to Our Email Newsletter
  • About
    • Contributors
    • Feedback
    • Contact
    • Privacy policy
    • Cookie Policy

CADnotes

CAD Tutorials and Best Practices for professionals and students

  • Featured
  • AutoCAD
    • AutoLISP
  • Revit
    • Revit Architecture Basic
    • Revit MEP Basic Tutorial
  • Inventor
  • MicroStation
    • MicroStation Basic Tutorial
You are here: Home / AutoCAD / AutoLISP / Modifying objects with AutoLISP: working with selection

Modifying objects with AutoLISP: working with selection

June 22, 2011 by Edwin Prakoso 1 Comment

In this Article...

  • Saving selection to a variable
    • Sample 1: Move to left
    • Sample 2: Reset properties to ByLayer
  • Using other selection mode
  • Further readings:

We covered several topics on how we can draw more efficiently using AutoLISP. Now it’s time to move on. We are going to learn how we can modify existing objects in our drawing.

The most important thing about editing objects is to select them. As we know, After we activate an AutoCAD modification command (like move, copy, etc) AutoCAD will ask us to select object.

autocad_selection

Saving selection to a variable

The function that allow us to do this is SSGET.

Sample 1: Move to left

This sample program will allow you to select object and move them 5 unit to left.

(defun c:mleft ()

(setq sel1 (ssget))

(command "move" sel1 "" "0,0,0" "-5,0,0")

)

SSGET will save your selection to sel1 variable. Then you can add it to move command.

Remember, you need to add “” to end selection. If you don’t add it, AutoCAD will continue to ask you to select object.

Sample 2: Reset properties to ByLayer

One thing that can annoy us when trying to apply CAD standard, is having the objects’ properties overridden. Set to other than ByLayer.

You can change object properties by using CHPROP. Then you can choose which properties you want to set to ByLayer. Try to use CHROP to understand what we’re doing below.

(defun c:rst ()

(setq sel1 (ssget))

(command "CHPROP" sel1 ""

"COLOR" "BYLAYER"

"LWEIGHT" "BYLAYER"

"LTYPE" "BYLAYER"

"")

)

Using other selection mode

Sometimes we need to select objects without asking users to give input. Or limit the method for user to use.

Using the code above will let users to use AutoCAD default mode.

You can use these code to select object from within the program:

(setq sel1 (ssget "w" '(0 0) '(10 10)))

That code will select all objects inside rectangular window from 0,0 to 10,10.

Other code that we can use are:

setq sel1 (ssget "c" '(0 0) '(10 10)) to select object inside and touching rectangular window (crossing polygon).

setq sel1 (ssget "l")) to select last object’s created.

setq sel1 (ssget "p")) to select objects in previous selection.

setq sel1 (ssget "x")) all objects in the drawing.

If you’re not familiar with the selection mode above, read using AutoCAD selection below.

For window and crossing polygon selection, we can also ask for user’s input.

For example:

(setq pt1 (getpoint "
Pick first point: "))

(setq pt2 (getpoint "
Pick other corner point: "))

(setq sel1 (ssget "c" pt1 pt2))

We will expand this selection using filter.

If you have a simple example that we can use in this exercise, please let me know. I will add your code here, and you will be credited.

Further readings:

  1. Selection sets. Good explanation by Kenny Ramage on AfraLISP.
  2. Using AutoCAD selection. Here are the list how you can select objects in AutoCAD.
  3. AutoLISP function list. See all LISP function that you can use to expand your LISP program.

About Edwin Prakoso

I work as a Sr. Consultant in PT Cipta Satria Informatika. I've been using AutoCAD since R14 and Revit since Revit Building 9. I occasionally write for AUGIWorld magazine and I am also active in Autodesk discussion forum. I'm a member of Autodesk Expert Elite, an appreciation for individuals who give contributions to the Autodesk community.
Connect with me on twitter or LinkedIn.

Filed Under: AutoLISP Tagged With: object selection

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Inline Feedbacks
View all comments
Jason
Jason
10 years ago

Can you use lisp to incrementally modify blocks with attributes? For example, i have 5 blocks 5′ apart that are numbered 1 and I want them numbered 1-5

0
Reply
wpdiscuz   wpDiscuz

Featured

Complete our challenges to work better with AutoCAD!

Learning AutoCAD is not only about knowing all the features. You need to know to to work effectively with it, and they don’t teach it in school or training classes. You can learn with us here!

Recent Articles

  • Autodesk Construction Cloud Activity Log
  • Exporting AutoCAD Plant 3D Model to Navisworks
  • Autodesk Data Connector for Power BI is Now Available

Advertisement

New on CADnotes

  • Autodesk Construction Cloud Activity Log
  • Exporting AutoCAD Plant 3D Model to Navisworks
  • Autodesk Data Connector for Power BI is Now Available
  • Autodesk Forma Design Contest
  • Revit 2025: Toposolid Enhancements

Meet the Authors

avatar for
avatar for
avatar for
avatar for
avatar for
avatar for

Get Connected

CADnotes on FacebookCADnotes on InstagramCADnotes on TwitterCADnotes on YouTube

© 2009 – 2025 CADnotes · Feedback · Privacy Policy · Become an affiliate

wpDiscuz