sales@landfx.com
  +1 805-541-1003

Activated

100% - 1 votes

Intermediate AutoLISP

Jul 20, 2018
Video Length:  1:07:09
Presented By:  Jeremiah Farmer
In this follow-up presentation, we’ll apply the principles covered in our AutoLISP Basics webinar to create another usable tool. This time, we’ll create a basic loop that processes all entities in the drawing, while discussing best practices in doing so. We’ll also show how to integrate your custom AutoLISP tools into the ribbon, and how best to organize your source files.

 

Webinar Contents:

Note: The following catalog of content covered in this webinar is time stamped to allow you to follow along or skip to sections of the video that are relevant to your questions. You can also search for content on this page using the FIND command in your browser (CTRL + F in Windows, Command + F in Mac OS.)

  • Intro/TOC
  • Recap AutoCAD Basics Webinar
  • Lists
  • Selection Sets
  • Ribbon Integration
  • Make a Function That Processes All Entities in the Drawing

 

0:00 – 3:44: Intro/TOC

3:45 – 7:26: Recap AutoCAD Basics Webinar

AutoLISP is a language of mathematics, encapsulated in parentheses. So:

2 + 2 becomes (+ 2 2)

 

Which means you can do: (+2  2 9 7) and assign variables: (set1 Radius (+ 2 2 9 7))

 

Recap data types (4:48)

  • Boolean: t / nil
  • Integer: 0, 1, 2, 3, -65536, 1073741824
  • Real: 3.141592653589793
  • String: “Hi, Mom!”
  • Lists: (101.222 319.045 0.0)
  • Entity name: <Entity name: 2094125b120>
  • Selection Sets: <Selection set: b5> (A selection set is a collection of entity names.)

 

Recap Lists (6:04)

  • Basic: (1 2 3 4)
  • Very common, a Point: (101.222 319.045 0.0)
  • Lists within lists (within lists (within lists)): ( (0 . 10.0) (1 . 4) (2 . “Hi, Mom!”) (3 . 75.44139) )
  • Entity access: ( (0 . “LINE”) (67 . 0) (8 . “0”) (10 250.831 43.694 0.0) (11 271.714 55.7062 0.0)

7:27 – 17:16: Lists

List construction (7:27)

How lists are created (7:27):

Take any number of arguments and spit out an actual list:

  • (list 1 2 3 4)
    • (1 2 3 4)

 

Appending lists (7:47):

  • (append (list 1 2 3 4) (list 5) )
  • (1 2 3 4 5)

 

 

  • (setq MyList (list 1 2 3 4))
    • (1 2 3 4)
  • (setq MyList (append MyList (list 5) ) )
    • (1 2 3 4 5)

 

More list construction (10:06)

  • (cons “distance” 3.5)
    • (“distance” 3.5)

 

  • (list (cons “distance” . 3.5) (cons “entity” E1) (cons “insertion” P1) )
    • ( (“distance” . 3.5) (“entity” . E1) (“insertion” . P1) )
    • (assoc “distance” MyList)
      • (“distance” . 3.5)

 

Control list (13:27)

Goal: Create a list of entities with their distance from a given point

  • One option: Have a list of entities and sort it by their distance. (Problem: This would require constant recalculation of the distance.)
  • Another option: Add to each entity info its distance from point (Problem: The entity definition will be illegal and will generate an error.)
  • Yet another option: Have two lists: one of entities and one of distances (also not a great idea)
  • Consider being able to work with X closest
    • Might want to modify the entity, or display the distance.

Possible result:

 

(

    (<distance> (<entity 1>) )

    ( <distance> (<entity 2>) )

)

17:17 – 18:49: Selection Sets

  • (ssget “X”) – where “X” is everything in the entire DWG
    • Returns a selection set of all entities in the drawing
  • (ssname (ssget “X”) 0)
    • Returns the entity name of first entity in the drawing

18:50 – 21:00: Ribbon Integration

All AutoLISP customization should be organized in the following way:

  • Custom CUIX
  • MNL file of same name, loading your LSP file
  • Menu folder (in Support File Search Path)
    • myOffice.CUIX
    • myOffice.MNL
    • myOffice.LSP
    • Images folder
      • toolA_16.png
      • toolA_32.png

21:01 – end: Make a Function That Processes All Entities in the Drawing

The menu folder from the AutoCAD Support File Search Path (as described above) (21:08)

 

Contents of the MNL file (22:25):

If the desired LISP file is in the Support File Search Path, it will load. If not, the user will be alerted.

 

The ribbon button for the tool we made in the AutoLISP Basics webinar (23:15)

 

Opening the CUI interface and creating a CUI entry for the tool and adding it to a ribbon panel (23:38)

 

Creating a macro and adding images for the tool button in the CUI (24:50)

 

Reloading the code and testing the tool (26:30)

 

Select a Block and Find the Closest (26:56)

Outlining (commenting) and coding the functions that will be built into the tool we’re creating (27:00)

 

Select an entity (29:08)

The importance of specifying the insertion point of the block rather than any point clicked on the block (30:20)

 

Get the entity info (30:50)

Saving, reloading, and testing (Do this often!) (31:30)

 

Make sure it’s a block (32:15)

 

Grab the block’s insertion point (33:02)

 

Grab the block’s name (33:53)

 

Get all of that block (34:04)

The importance of keeping variable names consistent (34:28)

 

The importance of always adding a trailing comment (35:50)

 

Process those blocks (40:00)

 

Calculate distance to the start point (42:33)

Putting in an access panel (44:00)

 

The framework shown at 50:55 should be a cornerstone of your AutoLISP programming.

 

Sort the control list (51:30)

Grabbing the necessary code from the Autodesk Knowledge Network (51:40)

 

Do something with the list (55:18)

  • Draw a line to the X closest (55:40)

 

The importance of naming variables carefully (1:02:42)

 

The importance of including C: at the beginning of commands and including an alert on each command (1:04:38)

Log in to landfx.com:

Username:

Password: