Lessons

File & Memory Management Functions

Today’s lesson is going to be a combination of vitality and efficiency as we explore the functions related to File & Memory Management in AutoLISP. These functions are the backbone of any advanced AutoLISP program as they facilitate interaction with files and manage memory effectively.

Learning Outcomes

By the end of this lesson, you will be able to:

  • Open, read, and close files using AutoLISP functions.
  • Load external files and applications into your current session.
  • Search for specific files within the system.
  • Efficiently manage memory allocation and garbage collection.
  • Exit an AutoLISP application gracefully.

File Management Functions

open

Definition: Opens a file for reading or writing.

Syntax:

(open filename mode)

Examples:

  1. Opening a file for reading:
(setq fileObj (open “myfile.txt” “r”))
  1. Opening a file for writing:
(setq fileObj (open “myfile.txt” “w”))

close

Definition: Closes a file that was previously opened.

Syntax:

(close fileObj)

Examples:

  1. Closing a previously opened file:
(close fileObj)
  1. Trying to close an unopened or already closed file (results in error):
(close unopenedFileObj)

load

Definition: Loads and evaluates an AutoLISP file.

Syntax:

(load filename)

Examples:

  1. Loading an AutoLISP file:
(load “myfunctions.lsp”)
  1. Loading a non-existent file (results in error):
(load “nonexistentfile.lsp”)

findfile

Definition: Searches for a specified file in the AutoCAD search path.

Syntax:

(findfile filename)

Examples:

  1. Finding an existing file:
(setq foundFile (findfile “myfunctions.lsp”))
  1. Searching for a non-existent file:
(setq notFound (findfile “nonexistentfile.lsp”))

autoload

Definition: Loads a specified function only when it is first invoked.

Syntax:

(autoload filename function-list)

Examples:

  1. Autoloading functions from a file:
(autoload “myfunctions.lsp” ‘(“func1” “func2”))
  1. Autoloading multiple functions:
(autoload “myfunctions.lsp” ‘(“funcA” “funcB” “funcC”))

autoarxload

Definition: Automatically loads an ARX or DBX application when a command is invoked.

Syntax:

(autoarxload appname command-list)

Examples:

  1. Auto loading an ARX application:
(autoarxload “myapp.arx” ‘(“command1” “command2”))
  1. Loading multiple commands:
(autoarxload “myapp.arx” ‘(“cmdA” “cmdB” “cmdC”))

arxload

Definition: Loads an ARX or DBX application.

Syntax:

(arxload appname)

Examples:

  1. Loading an ARX application:
(arxload “myapp.arx”)
  1. Loading a DBX application:
(arxload “myplugin.dbx”)

arxunload

Definition: Unloads a loaded ARX or DBX application.

Syntax:

(arxunload appname)

Examples:

  1. Unloading an ARX application:
(arxunload “myapp.arx”)
  1. Unloading a DBX application:
(arxunload “myplugin.dbx”)

Memory Management Functions

alloc

Definition: Allocates memory for a given number of bytes.

Syntax:

(alloc numbytes)

Examples:

  1. Allocating 100 bytes of memory:
(setq memPtr (alloc 100))
  1. Allocating 500 bytes of memory:
(setq anotherMemPtr (alloc 500))

gc

Definition: Initiates garbage collection to free up unused memory.

Syntax:

(gc)

Example:

  1. Running garbage collection:
(gc)

 


mem

Definition: Returns the amount of memory (in bytes) currently being used.

Syntax:

(mem)

Examples:

  1. Checking current memory usage:
(setq currentMemory (mem))
  1. Checking memory after performing some operations:
; … some operations …
(setq postOpMemory (mem))

exit

Definition: Exits the current AutoLISP session or application.

Syntax:

(exit)

Examples:

  1. Exiting the AutoLISP session:
(exit)
  1. Exiting after checking a condition:
(if (not valid)
(exit)
)

In conclusion, File & Memory Management Functions are essential tools for maintaining the efficiency and functionality of your AutoLISP applications. They allow you to interact with files, manage memory, and ensure that your applications run smoothly. Remember to practice using these functions and incorporate them into your daily programming tasks. Happy coding!

Further Learning: Dive into Our Tutorials!

Congratulations on completing the AutoLISP course for beginners! However, the journey doesn’t end here. To further enhance and solidify your AutoLISP knowledge, we strongly encourage you to visit our Tutorials section. This comprehensive resource is brimming with step-by-step tutorials tailored for all levels — whether you’re just starting out or already an advanced AutoLISP programmer.

Each tutorial delves deep into various AutoLISP programs, providing practical insights, best practices, and hands-on experience. These tutorials are designed to give you real-world exposure and help you tackle challenges that you might face when developing your projects.

Remember, like any other skill, mastering AutoLISP requires consistent practice and application. Our tutorials section offers a golden opportunity to apply what you’ve learned, experiment, make mistakes, and, most importantly, grow as an AutoLISP programmer.

So, don’t wait! Dive right into the Tutorials section and continue your journey towards AutoLISP mastery. Happy coding, and we hope to see you there!