Lessons

User Interaction: Configurations

In this lesson, we will delve into the exciting world of configurations in AutoLISP. Configurations allow us to interact with the user by accessing and modifying system settings and environment variables. These interactions are vital for creating customizable scripts, tailoring CAD designs to individual user preferences, and ensuring seamless software integration. Let’s get started!

Learning Outcomes

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

  • Understand the importance of configurations in AutoLISP.
  • Utilize functions to retrieve and modify system settings.
  • Interact with environment variables to enhance script customization.
  • Apply these functions to real-world CAD design scenarios.

Diving into Configuration Functions

getfiled

Definition: This function prompts the user to select a file using a standard dialog box.

Syntax:

(getfiled [prompt [default [flags [dialog_title]]]])

Examples:

  1. Simple file selection:
(getfiled “Select a file”)

Displays a standard dialog box prompting the user to select a file.

  1. File selection with a default filename and specific file type:
(getfiled “Select a DWG file” “default.dwg” “dwg” “DWG File Selection”)

Prompts the user to select a .dwg file with a default filename suggestion and a custom dialog title.

  1. File saving dialog:
(getfiled “Save your file” “default.txt” “txt” “Save As Dialog” 1)

Displays a “Save As” dialog box for .txt files.


getcfg

Definition: Retrieves the value of a configuration setting from a specified configuration file.

Syntax:

(getcfg setting [filename])

Examples:

  1. Retrieve a setting value from the default configuration file:
(getcfg “DisplayResolution”)

Returns the value of the “DisplayResolution” setting.

  1. Retrieve a setting value from a specific configuration file:
(getcfg “ColorScheme” “customconfig.cfg”)

Fetches the “ColorScheme” value from the “customconfig.cfg” file.

  1. Using the setting value in a conditional statement:
(setq display_res (getcfg “DisplayResolution”))
(if (= display_res “High”)
(alert “High-resolution display detected!”)
(alert “Standard-resolution display detected!”))

Displays a message based on the display resolution setting.


setcfg

Definition: Modifies or adds a configuration setting in a specified configuration file.

Syntax:

(setcfg setting value [filename])

Examples:

  1. Set a configuration setting in the default file:
(setcfg “DisplayMode” “Dark”)

Sets the “DisplayMode” to “Dark” in the default configuration file.

  1. Modify a setting in a specific configuration file:
(setcfg “FontSize” “12” “customconfig.cfg”)

Changes the “FontSize” setting to “12” in the “customconfig.cfg” file.

  1. Verify if the setting has been modified successfully:
(if (= (getcfg “FontSize” “customconfig.cfg”) “12”)
(alert “FontSize has been set to 12!”)
(alert “Failed to modify FontSize.”))

Checks and confirms whether the “FontSize” setting has been updated.


getvar

Definition: Retrieves the current value of a system variable.

Syntax:

(getvar variable_name)

Examples:

  1. Retrieve the current value of the “OSMODE” system variable:
(getvar “OSMODE”)

Returns the value of the “OSMODE” system variable.

  1. Check the current workspace:
(if (= (getvar “WORKSPACE”) “Drafting”)
(alert “You are in the Drafting workspace!”)
(alert “You are in a different workspace.”))

Displays a message based on the current workspace setting.

  1. Fetch the current drawing units:
(getvar “LUNITS”)

Returns the current unit format used for linear measurements.


setvar

Definition: Modifies the value of a system variable.

Syntax:

(setvar variable_name new_value)

Examples:

  1. Set the “OSMODE” system variable to a new value:
(setvar “OSMODE” 3)

Modifies the value of the “OSMODE” system variable to 3.

  1. Change the current workspace:
(setvar “WORKSPACE” “3D Modeling”)

Switches the current workspace to “3D Modeling”.

  1. Adjust the units for angular measurements:
(setvar “AUNITS” 4)

Sets the unit format for angular measurements to decimal degrees.


getenv

Definition: Retrieves the value of an environment variable.

Syntax:

(getenv variable_name)

Examples:

  1. Fetch the value of the “PATH” environment variable:
(getenv “PATH”)

Returns the directories listed in the “PATH” environment variable.

  1. Check the system’s architecture:
(if (= (getenv “PROCESSOR_ARCHITECTURE”) “x64”)
(alert “You are running a 64-bit operating system!”)
(alert “You are not running a 64-bit operating system.”))

Displays a message based on the system’s architecture.

  1. Retrieve the username of the currently logged-in user:
(getenv “USERNAME”)

Returns the username of the current user.


setenv

Definition: Sets the value of an environment variable for the duration of the current drawing session.

Syntax:

(setenv variable_name value)

Examples:

  1. Set a temporary environment variable:
(setenv “TEMP_VARIABLE” “12345”)

Creates a “TEMP_VARIABLE” environment variable with a value of “12345” for the current drawing session.

  1. Modify an existing environment variable:
(setenv “PATH” “C:\\MyDirectory;”)

Prepends “C:\MyDirectory;” to the “PATH” environment variable for the current session.

  1. Verify if the environment variable has been set:
(if (= (getenv “TEMP_VARIABLE”) “12345”)
(alert “TEMP_VARIABLE has been set to 12345!”)
(alert “Failed to set TEMP_VARIABLE.”))

Checks and confirms whether the “TEMP_VARIABLE” environment variable has been set.


Conclusion

User interaction and configurations play pivotal roles in tailoring AutoLISP scripts to individual user needs. By mastering these configuration functions, you can ensure your CAD designs and applications are both dynamic and adaptable. Remember, practice is the key to mastery. Challenge yourself by integrating these functions into your projects and see the magic unfold. Happy coding!