• 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 Exercise: Using Block and Conditional If

AutoLISP Exercise: Using Block and Conditional If

December 13, 2010 by Edwin Prakoso 3 Comments

In this Article...

  • Before We Start: Preparing the Block File
  • Command We will Use
  • More Consideration
  • Final Code

This time we are going to work with AutoCAD block. We had a good time when creating label for coordinate. Now we are going to create a similar AutoLISP program. We will create a program to label elevation. But this time we will also use block. Using block has several advantages. We can have more geometries without having to draw them and also can place the text to block attributes.

Before We Start: Preparing the Block File

Before we start, you need to download this AutoCAD file. This is the block we are going to use as elevation label.

elevation_label_block

Save the file to your library folder. I save it to D:\acadlib. You may have different location if you want to.

Now we need to set the location as AutoCAD support file search path. Open AutoCAD option. Add the folder location here.

AutoCAD_block_library_location

Command We will Use

Easy enough to guess: we are going to use INSERT to insert our block. If you try to type INSERT then [enter] in AutoCAD, it will open insert dialog box. That will not work with our AutoLISP program. AutoLISP will only work if we give input in command line or without dialog box.

To prevent AutoCAD to load the dialog box, we need to add – (dash) in front of the command. try to type –INSERT then [enter]. See what I’m talking about?

insert_block_without_dialog_box

We are going to use that.

Insert block will insert a block with the name we defined in our AutoLISP program. If it can’t find it, then it will search and load DWG name with the same name as the block. Then insert the DWG as block. That is why we define the file location in default search path. AutoCAD will load the file from that location.

The rest is easy. Like in previous tutorial, we can get the elevation value from the coordinate list and insert it to the block attribute.

So our basic program can be like this:

(defun c:cnannolevel (/ labelposition y)
(setq labelposition (getpoint "
Pick Label Position: "))
(setq y (cadr labelposition))
(setq y (rtos y))
(command "_-insert" "annolevel" labelposition 1 1 0 y)
)

More Consideration

If you already tried it, you can see it works nicely. But you may want to consider to allow AutoCAD users to change the block scale. We will do it later in this AutoLISP tutorial.

The next thing is change the label value when it’s at 0 elevation. In my country, many people like to use + 0.00 when it’s at zero elevation. We will add an if conditional to change the value.

Add Ability to Change Scale

If you examine the block, you will soon know that it was created in full scale 1:1. When you need to place it to a drawing with 1:100 scale, then you need to scale it after placing it. We don’t want that, don’t we?

So now we will add one more variable. This time we will let the variable become global variable. So AutoCAD will still recognize the variable after the application ends. And we can use it for other LISP application.

Because we don’t want the program to ask user for scale every time they use it, then we will use conditional IF.

The structure of conditional IF is

(IF (statement)  (things to do if true) (else, things to do if false))

I added conditional IF twice:

(if (= cnglobalscale nil)
(setq cnglobalscale (getreal "
Set Global Scale for CN Annotation <1/1>: "))
)
(if (= cnglobalscale nil)
(setq cnglobalscale 1)
)

The code in plain English
  1. The first conditional will check if the globalscale is not set (the value is NIL). If it’s true it will ask the user for input.
  2. The second conditional will check if the user don’t give any input and simply press [enter]. Because the user don’t provide value, then the scale still NIL.We assume that they want to use default scale 1:1. That’s why we provide 1/1 in the bracket. We tell user that if they don’t give any value, it will use full scale. This is common in AutoCAD tools to offer user to use default value.

Now the program become like this:

(defun c:cnannolevel (/ labelposition y)
(if (= cnglobalscale nil)
(setq cnglobalscale (getreal "
Set Global Scale for CN Annotation <1/1>: "))
)
(if (= cnglobalscale nil)
(setq cnglobalscale 1)
)
(setq labelposition (getpoint "
Pick Label Position: "))
(setq y (cadr labelposition))
(setq y (rtos y))
(command "_-insert" "annolevel" labelposition cnglobalscale cnglobalscale 0 y)
)

(defun c:cnannoscale ()
(setq cnglobalscale (getreal "
Set Global Scale for CN Annotation: "))
)

I added one more function at the bottom so users can change the scale anytime. Remember, we only ask for scale one time, when the global scale is NIL. So we need to provide them a way to change the scale manually later.

Change Text in Zero Elevation

As I mentioned before, we here would like to have + 0.00 than just 0 at zero level. This is simple. Just like before, we use conditional IF.

Instead of using

(setq y (rtos y))

We use

(if (= y 0) (setq y "%%p 0.00") (setq y (rtos y)))

The code will check if Y value is zero, and replace it with + 0.00. If it’s not, it will use the original value.

Final Code

After we added those features, then this is the complete code:

; CAD-Notes.com annotation utilities
; by: Edwin Prakoso

(defun c:cnannolevel (/ labelposition y)
(if (= cnglobalscale nil) ; this will check if user already has defined the drawing scale
(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)
)

(setq labelposition (getpoint "
Pick Label Position: "))
(setq y (cadr labelposition))
(if (= y 0) (setq y "%%p 0.00") (setq y (rtos y))) ; this will change text when in zero elevation
(command "_-insert" "annolevel" labelposition cnglobalscale cnglobalscale 0 y)
(princ)
)

(defun c:cnannoscale () ; this another function defined to enable user to change scale later
(setq cnglobalscale (getreal "
Set Global Scale for CN Annotation: "))
)

This program works nice. But the problem is the label can’t be updated automatically. Not like using fields. We will cover how to update the value later when we cover entity selection.

This is how our program will work. Have fun!

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, using block, using conditional IF

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.

3 Comments
Inline Feedbacks
View all comments
RAPHAEL
RAPHAEL
10 years ago

THANKS VERY MUCH EDWIN FOR YOUR NICE TUTORIAL.NOW I HAVE A QUESTION WHY DO WE USE SLASH (/ ) WHEN DEFINING A FUNCTION..

0
Reply
vivek khardenavis
vivek khardenavis
12 years ago

how to get mother board sl no using vlisp

how to get name of current running lisp programme by lisp

0
Reply
Paul Munford
Paul Munford
12 years ago

Nice one Edwin. Very clearly laid out. Well explained.

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

Work Better with AutoCAD | challenge 7: Evaluate your progress

You have completed 6 challenges to work better with AutoCAD. Now it’s time to evaluate your progress after completing the challenges. Is there are any area that you can improve?

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