• 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
  • CADnotes on YouTube
You are here: Home / AutoCAD / AutoLISP / AutoLISP tutorial: system variable and conditional COND

AutoLISP tutorial: system variable and conditional COND

January 24, 2011 by Edwin Prakoso 1 Comment

In this Article...

  • Using Conditional COND
  • Changing AutoCAD system variable
  • The complete AutoLISP program
  • Error trapping

In the last AutoLISP tutorial, we have tried to place block and change the attribute value. We also use conditional IF. Using AutoCAD block in AutoLISP is a great benefit. We can use our reusable content to make AutLISP programming less complex, and we can maintain our drawing standard in AutoCAD. This time we will try to use another conditional, using COND. We will also touch AutoCAD system variables, one of the most important thing in AutoLISP.

The program we will make in this AutoLISP tutorial will draw grid line, and automatically place label at the end of line.

Before you start, you need to download this file first. The zip file has 4 AutoCAD DWG files, each file is a grid label for each direction. We will use conditional COND to choose which AutoCAD block we should use. This AutoLISP tutorial requires you to understand what we did previously. So if you haven’t read it or want to refresh your memory, read the AutoLISP tutorial here. Or if you completely new to AutoLISP, follow the tutorial from beginning here.

grid_label

Using Conditional COND

COND is a bit different with IF. When using IF, you can tell AutoCAD to do A when condition is met. Or else, do B. COND will only do things when the condition is met.

The basic of using COND is:

(cond

((condition a) (things to do when condition a is met)

((condition b) (things to do when condition b is met)

((condition c) (things to do when condition c is met)

and so on…

)

Let’s see what can we do with COND in our case.


(cond
((AND (= pt1x pt2x) (> pt1y pt2y)) (setq gridblockname "vgridbottom")) ;change block name when meet the condition
((AND (= pt1x pt2x) (< pt1y pt2y)) (setq gridblockname "vgridtop"))

((AND (> pt1x pt2x) (= pt1y pt2y)) (setq gridblockname "hgridleft"))
)

First, we set the block name to HGRIDRIGHT. We will use this block when we draw grid line to the right. But when we draw it to bottom, we need to use block VGRIDBOTTOM.

When we draw a grid to bottom, there are two conditions are met:

  1. X1 and X2 are the same.
  2. Y2 is larger than Y1.

X1, Y1 is the first picked point, and X2, Y2 is the 2nd point picked by user. We define the three conditions in our program. For right direction, we don’t have to define it because we already set it as default block name.

Changing AutoCAD system variable

Another thing we need to do is change an AutoCAD system variable temporarily. We can do it in AutoLISP using SETVAR. We want to create the grid horizontal or vertical. To achieve that, we need to draw line with ortho mode is on.

This behavior is controlled by system variable ORTHOMODE. We need to change it to 1 to make ortho mode active. But before we change it, it is a good thing to restore the system variable after the program ends. We don’t want the program to change our AutoCAD behavior and we need to change option every time we use the program.

We need to save the current ortho mode first to a temporary variable. We can get the system variable and save it to user variable in AutoLISP using GETVAR.

(setq CurrentOrthomode (getvar "orthomode"))
(setvar "orthomode" 1)

The first line will get the system variable and save it to variable CurrentOrthomode. OK, if you feel the variable name is ridiculously long, you can choose your own name :)

The second line, we set ORTHOMODE system variable to 1.

After our AutoLISP program ends, we can restore the system variable to the original value.

(setvar "orthomode" CurrentOrthomode)

The complete AutoLISP program

Now we have everything we need. The AutoLISP program will run like this:

  1. It will check the drawing scale. Refer to previous tutorial to see how it works.
  2. It will save the current orthomode and change orthomode to 1.
  3. It will ask user to place two points for the grid.
  4. It will check which it should use.
  5. It will draw grid line and insert the block.
  6. It will restore the orthomode.

The complete AutoLISP code is like this:
(defun c:cngrid (/ pt1)
(if (= cnglobalscale nil)
(c:cnannoscale)
) ; end if

(setq CurrentOrthomode (getvar "orthomode"))
(setvar "orthomode" 1)
(setq pt1 (getpoint "
Pick Grid Start Point: "))
(setq pt2 (getpoint pt1 "nPick Grid Last Point: "))
(setq pt1x (car pt1))(setq pt1y (cadr pt1))
(setq pt2x (car pt2))(setq pt2y (cadr pt2))
(setq gridblockname "hgridright") ;set default grid name
(cond
((AND (= pt1x pt2x) (> pt1y pt2y)) (setq gridblockname "vgridbottom")) ;change grid name when meet the condition
((AND (= pt1x pt2x) (< pt1y pt2y)) (setq gridblockname "vgridtop")) ((AND (> pt1x pt2x) (= pt1y pt2y)) (setq gridblockname "hgridleft"))
)

(setq gridnumber (getstring "
Enter Grid Number: "))
(command "_line" pt1 pt2 "")
(command "_-insert" gridblockname pt2 cnglobalscale cnglobalscale 0 gridnumber)
(setvar "orthomode" CurrentOrthomode)
(princ)
)

(defun c:cnannoscale () ; this another function defined to enable user to change scale later
(setq cnglobalscale (getreal "
Set Global Scale for CN Annotation <1/1>: "))
(if (= cnglobalscale nil) ; this will check if the user choose default value
(setq cnglobalscale 1)
) ; end if
)

Error trapping

To complete this AutoLISP program, it is a good thing that you create an error trapping. We change an AutoCAD system variable: ORTHOMODE. We do set it back to original value. The problem is, when an error occur and the program ends prematurely, the system variable is not restored. The variable will not be restored when you press esc key!

This is what happen in this selection issue and the missing dialog box. Many problem can happen if you don’t set error trapping in an AutoLISP program!

You can read it in AfraLISP about error trapping here.

About Edwin Prakoso

I work as a Solution Consultant in Datech Solutions, Tech Data Indonesia. 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. If you want to have my new articles sent to your email inbox, you can subscribe to the newsletter.

Filed Under: AutoLISP Tagged With: autolisp tutorial, cond, system variable

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
Roberto Duran
Roberto Duran
9 years ago

hopefully you can give me a hand..
I’m creating a pull down menu, but I want to specify the lineweights and linetype, so far this is what I have.

(defun c:layA-SEC-LIH-005()
(command “layer” “m” “A-SEC-LIH-005” “C” “magenta” “” “l” “DASHED2” “” “”)

In order to add lineweight I added the “_LW”, but I’m not able to specify the linetype… any advise?

(defun c:layA-SEC-LIH-005()
(command “layer” “m” “A-SEC-LIH-005” “C” “magenta” “” “_LW” 0.10 “” “”)

regards

regards

0
Reply
wpdiscuz   wpDiscuz
Join Our Free Email Newsletter
  Thank you for Signing Up
Please correct the marked field(s) below.
1,true,6,Contact Email,2 1,false,1,First Name,2 1,false,1,Last Name,2

Featured

8 Frequent Issues when installing Autodesk Products

Many times the installation failed. We need to know what the problem is and what the workaround is. If you don’t know what’s the problem, you may not be able to install it even you have tried several times. Let’s try to fix it!

Recent Articles

  • Revit 2024.1 Update is Released
  • What’s New in Revit 2024: Bending Detail
  • What’s New in Revit 2024: The Dark Theme

Advertisement

New on CADnotes

  • Revit 2024.1 Update is Released
  • What’s New in Revit 2024: Bending Detail
  • What’s New in Revit 2024: The Dark Theme
  • Autodesk Build: Using Assets for Progress Tracking
  • My Home on the ACC Unified Platform

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 – 2023 CADnotes · Feedback · Privacy Policy · Become an affiliate

wpDiscuz