"""
PyRx Basic Command Module with Event Handling
This module demonstrates a comprehensive PyRx command structure that mimics
ObjectARX-style event handling for AutoCAD Python extensions. It provides
a foundation for creating robust AutoCAD Python add-ins with proper initialization,
cleanup, and command management.
The module includes:
- Application lifecycle event handlers (init, unload, reload)
- Drawing lifecycle event handlers (load, unload)
- Multiple command definitions with different flags
- Lisp function integration
- Proper error handling and logging
Usage:
Load this script into AutoCAD's Python environment.
The module automatically registers all commands and events when loaded.
Features:
- Application-level event callbacks for startup/shutdown management
- Drawing-level event callbacks for document lifecycle management
- Command definitions with various execution flags (modal, transparent)
- Lisp function integration for compatibility with existing AutoLISP code
- Comprehensive error handling throughout all functions
Event Handlers:
- OnPyInitApp: Called when the Python application is initialized
- OnPyUnloadApp: Called when the Python application is unloaded
- OnPyReload: Called when the module is reloaded via pyreload command
- OnPyLoadDwg: Called when a drawing document is opened
- OnPyUnloadDwg: Called when a drawing document is closed
Commands:
- mycommand: Basic modal command that prints "Hello world!"
- mycommand2: Transparent command that prints "Hello world too!"
Lisp Functions:
- mylisp: Simple Lisp function that returns its arguments unchanged
"""
import traceback
from pyrx import Ap
# === Application Lifecycle Events ===
def OnPyInitApp():
"""
Called when the Python application is initialized.
This event handler is invoked automatically when the module is first loaded
into AutoCAD. It serves as the entry point for application-level initialization
and setup operations.
Usage:
Automatically called by PyRx framework on module load
Side Effects:
- Prints initialization message to console
- Can be used for global variable initialization, logging setup, etc.
Example Output:
OnPyInitApp
"""
print("\nOnPyInitApp")
def OnPyUnloadApp():
"""
Called when the Python application is unloaded.
This event handler is invoked automatically when AutoCAD shuts down or
when the module is explicitly unloaded. It provides a clean shutdown mechanism
for resource cleanup and finalization operations.
Usage:
Automatically called by PyRx framework on application shutdown
Side Effects:
- Prints unloading message to console
- Can be used for cleanup of global resources, closing files, etc.
Example Output:
OnPyUnloadApp
"""
print("\nOnPyUnloadApp")
def OnPyReload():
"""
Called when the pyreload command is executed on this module.
This event handler is invoked when a user runs the 'pyreload' command
targeting this specific module. It allows for hot-reloading of code without
restarting AutoCAD, making development and testing more efficient.
Usage:
Run 'pyreload' command in AutoCAD while this module is loaded
Side Effects:
- Prints reload message to console
- Allows developers to reinitialize state or perform updates
Example Output:
OnPyReload
"""
print("\nOnPyReload")
def OnPyLoadDwg():
"""
Called when a drawing document is opened.
This event handler is invoked whenever a new AutoCAD drawing file is loaded.
It provides an opportunity to initialize drawing-specific state or perform
operations that should occur with each new document context.
Usage:
Automatically called by PyRx framework when opening drawings
Side Effects:
- Prints document load message to console
- Can be used for per-document initialization tasks
Example Output:
OnPyLoadDwg
"""
print("\nOnPyLoadDwg")
def OnPyUnloadDwg():
"""
Called when a drawing document is closed.
This event handler is invoked whenever an AutoCAD drawing file is closed.
It provides cleanup opportunities for any temporary resources or state that
was created during the document's lifetime.
Usage:
Automatically called by PyRx framework when closing drawings
Side Effects:
- Prints document unload message to console
- Can be used for per-document cleanup operations
Example Output:
OnPyUnloadDwg
"""
print("\nOnPyUnloadDwg")
# === Command Definitions ===
@Ap.Command()
def mycommand():
"""
Basic modal command that prints "Hello world!".
This is a simple demonstration command that outputs a greeting message
when executed. It uses the default modal execution mode, meaning it will
block further command input until completed.
Usage:
Run 'mycommand' from AutoCAD command line
Execution Mode:
Modal (default) - Blocks command prompt until complete
Side Effects:
- Prints "Hello world!" to console
Example Output:
Hello world!
Error Handling:
- Catches and prints any exceptions during execution
"""
try:
print("Hello world!")
except Exception as err:
traceback.print_exception(err)
@Ap.Command("mycommand2", Ap.CmdFlags.TRANSPARENT)
def foo():
"""
Transparent command that prints "Hello world too!".
This command demonstrates the use of the TRANSPARENT flag, which allows
the command to execute without blocking the AutoCAD command prompt.
Users can continue issuing commands while this one is running or suspended.
Usage:
Run 'mycommand2' from AutoCAD command line
Execution Mode:
Transparent - Does not block command prompt
Parameters:
CmdFlags: Set to Ap.CmdFlags.TRANSPARENT (default)
Side Effects:
- Prints "Hello world too!" to console
Example Output:
Hello world too!
Error Handling:
- Catches and prints any exceptions during execution
"""
try:
print("Hello world too!")
except Exception as err:
traceback.print_exception(err)
# === Lisp Function Integration ===
@Ap.LispFunction()
def mylisp(args):
"""
Simple Lisp function that returns its arguments unchanged.
This function demonstrates how to integrate Python functions with AutoLISP
through PyRx. It accepts a list of arguments (similar to LISP) and returns them,
making it compatible with existing AutoLISP code or testing environments.
Usage:
Call from AutoLISP as: (mylisp "hello world" 1 2 3 4 (1 10 100))
Parameters:
args: List of arguments passed from LISP (can be any Python objects)
Returns:
The same list of arguments that was passed in
Example Usage:
In AutoLISP console: (mylisp "hello world" 1 2 3 4 (1 10 100))
Example Output:
('hello world', 1, 2, 3, 4, (1, 10, 100))
Error Handling:
- Catches and prints any exceptions during execution
"""
try:
return args
except Exception as err:
print(err)
No comments:
Post a Comment