52
SD196926 - Using the Visual LISP Extension with AutoLISP Lee Ambrosius Principal Learning Experience Designer

SD196926 - Using the Visual LISP Extension with AutoLISP

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SD196926 - Using the Visual LISP Extension with AutoLISP

SD196926 - Using the Visual LISP

Extension with AutoLISP

Lee Ambrosius

Principal Learning Experience Designer

Page 2: SD196926 - Using the Visual LISP Extension with AutoLISP

Where Am I and Who Should Be Here

You are in session:

• SD196926 - Using the Visual LISP Extension with AutoLISP

You should know:

• AutoCAD 2019 (or AutoCAD 2012 and later)

• Fundamentals of AutoLISP programming

You should want to:

• Automate tasks through programming

• Extend the functionality of existing AutoLISP programs

Page 3: SD196926 - Using the Visual LISP Extension with AutoLISP

Who Am I?

My name is Lee Ambrosius:

• Principal Learning Experience Designer at Autodesk, Inc.

• Over 20+ years of AutoCAD customization and programming experience

• Customization, Developer, and CAD Administration documentation

• Author of the AutoCAD Customization Platform book series published by Wiley & Sons

My job in a nutshell:

• Document the past and present AutoCAD releases for the future

Page 4: SD196926 - Using the Visual LISP Extension with AutoLISP

Session Rules

• Silence your mobile phone, tablet, and any other device

• If you have to leave, please do so quietly

• I will allow time to ask questions during the session unless we start getting behind

Page 5: SD196926 - Using the Visual LISP Extension with AutoLISP

Enjoy the Journey

The path isn’t the same for everyone…

but the goal is often shared by many.

To the left and right of you is someone

with a shared interest, talk and learn from them.

Page 6: SD196926 - Using the Visual LISP Extension with AutoLISP

Let’s Get Going

Page 7: SD196926 - Using the Visual LISP Extension with AutoLISP

What You Will Learn Today

At the end of this session, you will know how to:

• Access objects in an open or closed drawing with the AutoCAD ActiveX/COM library

• Access and store values with Xdata, Dictionaries, and the Windows® Registry

• Monitor changes to objects in a drawing with reactors

• Manipulate the Windows environment

• Drive Microsoft® Office applications; Word, Excel, and Access

Page 8: SD196926 - Using the Visual LISP Extension with AutoLISP

What You Need to Get Started

For the samples covered in this session, you will need:

• AutoCAD 2019 (AutoCAD 2012 or later)

• Notepad or the Visual LISP IDE

• Microsoft Office

• Windows 7 or later

Page 9: SD196926 - Using the Visual LISP Extension with AutoLISP

Visual LISP Extension

Page 10: SD196926 - Using the Visual LISP Extension with AutoLISP

Visual LISP Extension

Introduced back in AutoCAD 2000

Extends the AutoLISP programming language

• New geometric functions

• ActiveX/COM technologies

Loaded using the vl-load-com function

The Visual LISP IDE was introduced too… But isn’t the focus of Today.

Page 11: SD196926 - Using the Visual LISP Extension with AutoLISP

AutoCAD ActiveX/COM Libraries

AutoCAD 2019 Type Library (acax23enu.tlb)

• Access to application, drawing windows, and objects in open drawings

• Imported with the vl-load-com function

AutoCAD/ObjectDBX Common 23.0 Type Library (axdb23enu.tlb)

• Access to files on disk without opening them in the application

• Imported with the vlax-import-type-library function

Page 12: SD196926 - Using the Visual LISP Extension with AutoLISP

AutoCAD ActiveX/COM Libraries

Once loaded/imported, the AutoCAD ActiveX/COM libraries allow you to

• Create and modify objects in Model or Paper space

• Work with named objects; blocks, layers, and text styles

• Manipulate a subset of the AutoCAD application options

• Open and work drawings in memory

Page 13: SD196926 - Using the Visual LISP Extension with AutoLISP

Members of a COM Object

Many AutoCAD COM related functions expect values of the vla-object data type

vla-object data type represents a COM object

vlax-dump-object function lists the methods/properties of a COM object

(vlax-dump-object obj [flag])

Example:

(vlax-dump-object (vlax-get-acad-object) T)

Page 14: SD196926 - Using the Visual LISP Extension with AutoLISP

Members of a COM Object

; IAcadApplication: An instance of the AutoCAD application

; Property values:

; ActiveDocument = #<VLA-OBJECT IAcadDocument 0000023b4d814a78>

; Application (RO) = #<VLA-OBJECT IAcadApplication 00007ff7f2b1b0a8>

; Name (RO) = "AutoCAD"

; Path (RO) = "C:\\Program Files\\Autodesk\\AutoCAD 2019"

; Methods supported:

; Eval (1)

; Quit ()

; ZoomAll ()

Page 15: SD196926 - Using the Visual LISP Extension with AutoLISP

Mixing “Legacy” AutoLISP and Visual LISP Functions

vla-object objects and entity names (ename) are not compatible

Must be transformed based on function usage:

• vlax-ename->vla-object – Transforms an ename into a vla-object

• vlax-vla-object->ename – Transforms a vla-object into an ename

ActiveX doesn’t know what a list is, but knows what an array is:

• vlax-make-safearray – Creates an array

• vlax-safearray-fill – Fills an array based on the values of a list

• vlax-safearray->list – Transforms an array to a list

Page 16: SD196926 - Using the Visual LISP Extension with AutoLISP

Accessing Property Values without DXF or ActiveX

AutoCAD 2012 introduced four functions that simplified access to object properties:

• dumpallproperties – Outputs all the properties and their current value for the

specified entity

• getpropertyvalue – Returns the current value of the specified property for an entity

• ispropertyreadonly – Returns T or nil of the specified property for an entity is read-

only or not

• setpropertyvalue – Sets the specified property to a new value for an entity

Page 17: SD196926 - Using the Visual LISP Extension with AutoLISP

Access Objects in an Open Drawing

Page 18: SD196926 - Using the Visual LISP Extension with AutoLISP

Access Objects in an Open Drawing

Sample Code: Create a blue circle in the current space and drawing

• CreateCircle-Cmds – Using commands

• CreateCircle-Dxf – Using DXF code values and no commands

• CreateCircle-SetProp – No commands or DXF manipulation

• CreateCircle-VL – Using the ActiveX via the Visual LISP extension

Page 19: SD196926 - Using the Visual LISP Extension with AutoLISP

Access Objects in an Open Drawing

Sample Code: Set color, linetype, and lineweight properties of objects to ByLayer

• ChangeAll2ByLayer-Cmds – Using commands

• ChangeAll2ByLayer-Dxf – Using DXF code values and no commands

• ChangeAll2ByLayer-SetProp – No commands or DXF manipulation

• ChangeAll2ByLayer-VL – Using the ActiveX via the Visual LISP extension

Page 20: SD196926 - Using the Visual LISP Extension with AutoLISP

Open and Close External Drawings

Page 21: SD196926 - Using the Visual LISP Extension with AutoLISP

Access Objects in an External Drawing File

AutoCAD/ObjectDBX Common 23.0 Type Library must be imported first

COM library is imported using the vlax-import-type-library function

(vlax-import-type-library :tlb-filename filename

[:methods-prefix mprefix

:properties-prefix pprefix

:constants-prefix cprefix])

vlax-create-object function is used to create an instance of a COM object

(vlax-create-object prog-id)

Page 22: SD196926 - Using the Visual LISP Extension with AutoLISP

Access Objects in an External Drawing File

Example:

(vlax-import-type-library

:tlb-filename "C:\\Program Files\\Common Files

\\Autodesk Shared\\axdb23enu.tlb"

:methods-prefix "acdbm-"

:properties-prefix "acdbp-"

:constants-prefix "acdbc-"

)

(vlax-create-object "ObjectDBX.AxDbDocument.23")

Page 23: SD196926 - Using the Visual LISP Extension with AutoLISP

Access Objects in an External Drawing File

Sample Code: Copy all dimension styles into the current drawing from an external

drawing

• AccessExternalDrawing

Page 24: SD196926 - Using the Visual LISP Extension with AutoLISP

Store and Access Information Later

Page 25: SD196926 - Using the Visual LISP Extension with AutoLISP

Store and Access Information Later

You can store values in different ways:

• Local variables hold values while a function is executing

• Global variables can hold information while a drawing remains open

• Variables can be defined on the blackboard and accessed across open drawings

• Windows Registry allows you to store and access information between different sessions

• Xdata, custom dictionaries, and xrecords can be used to store data in a drawing which

can be accessed when the drawing is opened

Page 26: SD196926 - Using the Visual LISP Extension with AutoLISP

Visual LISP Blackboard

Blackboard allows you to store and access values across all open drawings

• vl-bb-set – Assigns a value to a variable on the blackboard

• vl-bb-ref – Returns the value of a variable on the blackboard

Variables on the blackboard don’t affect local or global variables

Sample Code: Store and retrieve values from the blackboard

• Blackboard

Page 27: SD196926 - Using the Visual LISP Extension with AutoLISP

Windows Registry

Values in the Windows Registry persist between sessions; great for app settings

• vl-registry-write – Writes a value to a key

• vl-registry-read – Returns the value assigned to a key

• vl-registry-delete – Removes a key in the Registry

• vl-registry-descendents – Returns a list of subkeys

Note: PList files on the Mac OS mimic the Windows Registry to some extent

Page 28: SD196926 - Using the Visual LISP Extension with AutoLISP

Windows Registry

Sample Code: Read and write values stored in the Windows Registry

• WinReg

Page 29: SD196926 - Using the Visual LISP Extension with AutoLISP

eXtended Data (Xdata)

Custom data attached directly to a graphical object

Must be associated with a registered application

• Registration of an application is done with the regapp function

(regapp app_name)

Structured data based on DXF data type and value pairings

(-3 ("ACAD" (1000 . "DSTYLE") (1002 . "{") (1070 . 347)

(1005 . "220") (1002 . "}"))

Page 30: SD196926 - Using the Visual LISP Extension with AutoLISP

eXtended Data (Xdata)

Xdata can be managed with these Visual LISP functions

• vla-getxdata – Returns two arrays that represent DXF data type and value pairings

• vla-setxdata – Assigns the xdata to an object defined in two arrays

Page 31: SD196926 - Using the Visual LISP Extension with AutoLISP

eXtended Data (Xdata)

In comparison, Xdata can be managed with these “Legacy” AutoLISP functions

• entget – Gets the entity data associated with an object including its xdata

• assoc – Searches a list for an element and returns the associated element list

• subst – Substitutes an item in a list

• cons – Creates a dotted pair

• list – Creates a new list

• append – Combines two lists together

• entmod – Modifies an entity’s definition

• entupd – Updates the on-screen graphics for an entity

Page 32: SD196926 - Using the Visual LISP Extension with AutoLISP

eXtended Data (Xdata)

Sample Code: Assign and remove Xdata with Visual LISP functions

• SetXdata-VL

• RemoveXdata-VL

Sample Code: Retrieve and list assigned Xdata

• ListXdata

Sample Code: Assign and remove Xdata with “Legacy” AutoLISP

• SetXdata

• RemoveXdata

Page 33: SD196926 - Using the Visual LISP Extension with AutoLISP

Dictionaries and Xrecords

Custom data that can be

• Stored in the named dictionary collection of a

drawing

• Attached to a graphical or non-graphical object

via an extension dictionary

Doesn’t require an application to be registered

Structured data based on DXF data type and

value pairings

Page 34: SD196926 - Using the Visual LISP Extension with AutoLISP

Dictionaries and Xrecords

Dictionaries and Xrecords can be managed with these Visual LISP functions

• vla-get-dictionaries – Gets the Dictionaries collection of a drawing

• vla-addxrecord – Adds an xrecord to a dictionary

• vla-setxrecorddata – Sets the data for an xrecord

• vla-getxrecorddata – Gets the data assigned to an xrecord

• vla-getextensiondictionary – Returns an object’s extension dictionary

• vla-get-hasextensiondictionary – Returns :vlax-true or :vlax-false

whether an object has an extension dictionary

Page 35: SD196926 - Using the Visual LISP Extension with AutoLISP

Dictionaries and Xrecords

Xrecords and dictionaries can be managed with these “Legacy” AutoLISP functions

• namedobjdict – Returns the named object dictionary for the drawing

• dictadd – Adds a custom dictionary or xrecord to the specified entity

• dictremove – Removes a custom dictionary

• dictnext – Returns the next entry in a dictionary

• dictrename – Renames a dictionary from the old name to a new name

• dictsearch – Searches a dictionary for an entry

• entmakex – Creates a new graphical or non-graphical object in memory

Note: Functions previously mentioned for working with Xdata are also required

Page 36: SD196926 - Using the Visual LISP Extension with AutoLISP

Dictionaries and Xrecords

Sample Code: Create and remove a custom dictionary with Visual LISP functions

• CreateDictionary-VL

• DeleteDictionary-VL

Sample Code: Retrieve and print an xrecord attached to a dictionary

• PrintXrec-VL

• PrintXrec

Sample Code: Create and remove a custom dictionary with “Legacy” AutoLISP

• CreateDictionary

• DeleteDictionary

Page 37: SD196926 - Using the Visual LISP Extension with AutoLISP

Monitoring Activities in AutoCAD

Page 38: SD196926 - Using the Visual LISP Extension with AutoLISP

Reactors

Reactors watch for and react to certain situations that occur

• in a drawing database

• to an object

• at the application level

Must be registered with a callback function before they can be used

Callback functions define what happens when the reactor is fired by the application

command function CAN’T be used in a reactor

Page 39: SD196926 - Using the Visual LISP Extension with AutoLISP

Reactors

Registered using functions that start with vlr-

• VLR-Editor-Reactor - Drawing reactor (opening or closing a drawing file)

• VLR-Insert-Reactor - Block insertion reactor

• VLR-Command-Reactor - Command reactor (command starts, ends, and fails)

• VLR-Lisp-Reactor - LISP reactor

• …

Sample Code: Monitors for the use of the Qdim, Hatch, Batch, and Gradient

commands

Page 40: SD196926 - Using the Visual LISP Extension with AutoLISP

Working with Windows

Page 41: SD196926 - Using the Visual LISP Extension with AutoLISP

Working with Windows

Some of the Windows COM libraries available allow you to:

• Create file shortcuts

• Access special named folders; Documents, Pictures, …

• Query and set environment variables

• Access the drive, folder, and file structures of Windows

• Identify installed printers

Page 42: SD196926 - Using the Visual LISP Extension with AutoLISP

Working with Windows

Two of the most useful Windows COM libraries are:

• Windows Host Scripting Object (wshom.ocx)

• Microsoft Scripting Runtime libraries (scrrun.dll)

Libraries are located in c:\windows\system32

Page 43: SD196926 - Using the Visual LISP Extension with AutoLISP

Working with Windows

Sample Code: Create a shortcut on the Desktop

• CreateDesktopShortcut

Sample Code: Get, set, and expand environment variables

• GetEnvStr

• SetEnvStr

• ExpEnvStr

Sample Code: List available drives

• ListDrives

Page 44: SD196926 - Using the Visual LISP Extension with AutoLISP

Working with Microsoft Office

Page 45: SD196926 - Using the Visual LISP Extension with AutoLISP

Working with Microsoft Office

Microsoft provides a number of COM libraries to interact with Microsoft Office:

• Print a Microsoft Word document to the default printer

• Create a new Microsoft Word document based on information in an AutoCAD drawing

• Push and pull information from an Microsoft Excel spreadsheet to modify content in a

drawing

• Query and modify information in a Microsoft Access (MDB) database

Page 46: SD196926 - Using the Visual LISP Extension with AutoLISP

Working with Microsoft Office

Most common COM libraries associated with the applications in Microsoft Office:

• Microsoft Word 16.0 Object Library (msword.olb)

• Microsoft Excel 16.0 Object Library(excel.exe)

• Microsoft ActiveX Data Objects 6.1 Library (msado15.dll)

Libraries are located in the

• Microsoft Office installation folder

• Program Files\Common Files folder

Page 47: SD196926 - Using the Visual LISP Extension with AutoLISP

Working with Microsoft Office

Sample Code: Print and create Microsoft Word documents

• PrintMSWordDoc

• CreateMSWordDoc

Sample Code: Extract and update object property values with Microsoft Excel

• ExtractDrawingToExcel

• UpdateDrawingFromExcel

Sample Code: List available drives

• AccessDatabaseADO

Page 48: SD196926 - Using the Visual LISP Extension with AutoLISP

Final Thoughts and Questions

Page 49: SD196926 - Using the Visual LISP Extension with AutoLISP

Final Thoughts and Questions

Customization and programming can:

• Enhance productivity

• Improve or introduce new workflows

Customizing has many similarities to Wonderland in

Lewis Caroll’s Alice’s Adventures. Both

• are virtually endless

• hold many mysteries waiting to be discovered

Page 50: SD196926 - Using the Visual LISP Extension with AutoLISP

Closing Remarks

Thanks for choosing this session.

Don’t forget to complete this session’s survey.

If you have any further questions, contact me via:

email: [email protected]

twitter: @leeambrosius

Page 51: SD196926 - Using the Visual LISP Extension with AutoLISP

Want to be featured by

AutoCAD?

We want to hear your story!

Go to: autode.sk/autocadstory

We know that the best way to tell the AutoCAD story is

to tell the stories of you, the people who use it. Tell us

your project for a chance to be featured by AutoCAD.

SHARE

YOUR

STORY

Page 52: SD196926 - Using the Visual LISP Extension with AutoLISP

Autodesk and the Autodesk logo are registered trademarks or trademarks of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or

trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice, and is not responsible for

typographical or graphical errors that may appear in this document.

© 2018 Autodesk. All rights reserved.