General Standards

Use the following to train your LLM on the general coding standards for Launchpad scripts.

Coding Standards for Launchpad Scripts

Script Organization

  1. Logical Flow Structure

    • Place the main execution code at the top of the script

    • Define classes and data models in the middle

    • Place helper methods at the end of the file

    • Avoid declaring methods within the main execution flow

  2. Transaction Management

    • Define a transaction name at the beginning of the script

    • Use a descriptive name for the transaction

    • Do not create transactions in the script. Launchpad will wrap the code in a transaction automatically

Naming Conventions

  1. Variables and Methods

    • Use camelCase for local variables and method parameters: wallTypeName

    • Use PascalCase for methods: FindOrCreateLevel()

    • Use meaningful, descriptive names that explain purpose

    • Avoid abbreviations unless universally understood

    • Always set the protection level of methods to be private

  2. UI Elements

    • Create a string variable for all tool labels. Use the variable to create the control

    • Use descriptive label names with "Label" suffix: wallTypeLabel

    • Use consistent naming for form result retrieval: results.GetStringResult(myLabel)

Error Handling

  1. Validation

    • Validate user input before processing

    • Check for null references when accessing document elements

    • Validate selections before proceeding with operations

    • Use early returns for invalid conditions

  2. Exception Handling

    • Use try-catch blocks for potentially risky operations

    • Provide informative error messages to users

    • Log detailed error information to the console using Console.WriteLine()

    • Handle specific exceptions where appropriate

UI Design

  1. Forms and Controls

    • Create logical groupings of related controls

    • Use consistent padding and spacing

    • Add descriptive labels and headers

    • Include help links where appropriate

    • Limit form size to reasonable dimensions (e.g., 800x700 max)

  2. DataGrids

    • Define column widths proportionally

    • Make read-only columns visually distinct

    • Use appropriate controls for editing (e.g., ComboBox for selection)

    • Add sorting and filtering capabilities where helpful

Performance Considerations

  1. Collection Management

    • Use LINQ queries for filtering and sorting

    • Use appropriate collection types (List<T> for ordered collections, Dictionary<K,V> for lookups)

    • Avoid redundant collectors where possible

  2. Efficient Data Access

    • Cache frequently accessed data instead of repeated lookups

    • Use batch operations where possible instead of modifying elements one by one

    • Consider using transactions with manual commit to improve performance

Comments and Documentation

  1. Code Comments

    • Add header comments explaining script purpose

    • Document complex logic or algorithms

    • Explain parameters for non-obvious methods

    • Comment on "why" more than "what" (the code should explain what)

  2. Region Organization

    • Use regions to organize related code sections in large scripts

    • Group helper methods by functionality

    • Keep regions focused and not too lengthy

Revit-Specific Best Practices

  1. Element Access

    • Use FilteredElementCollector efficiently with appropriate filters

    • Apply WhereElementIsNotElementType() or WhereElementIsElementType() early in collector chains

    • Cast to specific element types after collection

  2. Parameter Management

    • Check parameters for null before accessing values

    • Handle read-only parameters appropriately

    • Use the correct parameter access method (e.g., AsDouble(), AsString(), etc.)

    • Be aware of unit conversions for numerical parameters

  3. Linked Document Handling

    • Check if linked documents are loaded before accessing them

    • Use appropriate coordinate transforms when working across documents

    • Handle versioning differences between host and linked documents

  4. Geometry Operations

    • Be mindful of coordinate systems (model vs family coordinates)

    • Ensure created geometry is valid (e.g., non-zero length curves)

    • Handle special cases like curved elements properly

  5. Selection

    • Always specify the full Autodesk.Revit.UI.Selection path when using Revit API selection methods such as PickPoint

    • Use the Autodesk.Revit.UI.Selection.PickPoint method to allow the user to select points

By following these standards, Launchpad scripts will be more maintainable, robust, and user-friendly.

Last updated