> ## Documentation Index
> Fetch the complete documentation index at: https://docs.luminicad.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Scripting Patterns

> Common patterns and best practices for LuminiCAD scripting

## Basic Patterns

### Building Blocks

<CodeGroup>
  ```text Basic Shapes theme={null}
  CREATE BOX ORIGIN 0 0 0 SIZE 100 50 HEIGHT 75
  CREATE CIRCLE CENTER 0 0 0 RADIUS 50 NORMAL 0 0 1
  CREATE LINE FROM 0 0 0 TO 100 0 0
  ```

  ```text Combinations theme={null}
  CREATE FUSE 
  BOTTOM [first-shape-command] 
  TOP [second-shape-command]

  CREATE BOOLEAN CUT 
  FIRST [base-shape] 
  SECOND [cutting-shape]
  ```
</CodeGroup>

## Common Patterns

### Circular Patterns

```text theme={null}
// Create base hole
CREATE CIRCLE CENTER 40 0 0 RADIUS 5 NORMAL 0 0 1
CREATE PRISM SECTION [previous-circle] LENGTH 10

// Pattern around center
CREATE REVOLVE PROFILE [previous-hole] 
AXIS ORIGIN 0 0 0 DIRECTION 0 0 1 ANGLE 360
```

### Linear Arrays

```text theme={null}
// Create base feature
CREATE RECTANGLE ORIGIN 0 0 0 SIZE 20 20
CREATE PRISM SECTION [previous-rectangle] LENGTH 10

// Create linear pattern
CREATE SWEEP PROFILE [previous-prism] 
PATH [CREATE LINE FROM 0 0 0 TO 200 0 0]
```

## Organization Patterns

### Folder Structure

```text theme={null}
// Create main assembly folder
CREATE FOLDER NAME "Main Assembly"

// Create component folders
CREATE FOLDER NAME "Base Components"
CREATE FOLDER NAME "Features"
CREATE FOLDER NAME "Patterns"
```

### Component Building

```text theme={null}
// Base structure
CREATE BOX ORIGIN 0 0 0 SIZE 100 100 HEIGHT 50

// Features
CREATE CIRCLE CENTER 50 50 0 RADIUS 20 NORMAL 0 0 1
CREATE PRISM SECTION [previous-circle] LENGTH 50

// Combine
CREATE BOOLEAN CUT 
FIRST [base-box] 
SECOND [hole-prism]
```

## Best Practices

### 1. Structure Your Code

* Group related operations
* Use clear naming

### 2. Build Parametrically

```text theme={null}
// Define key dimensions
CREATE BOX ORIGIN 0 0 0 
SIZE [width] [length] 
HEIGHT [height]

// Reference existing geometry
CREATE CIRCLE CENTER [width/2] [length/2] 0 
RADIUS [hole_diameter/2] 
NORMAL 0 0 1
```

### 3. Error Prevention

<CodeGroup>
  ```text Check Parameters theme={null}
  // Validate dimensions
  if width < minimum_width:
      show error "Width too small"

  // Check relationships
  if hole_diameter > width:
      show error "Hole larger than part"
  ```

  ```text Safe Operations theme={null}
  // Create base first
  CREATE base_shape

  // Verify before boolean
  if base_shape exists:
      CREATE BOOLEAN CUT
  ```
</CodeGroup>

## Advanced Patterns

### Complex Geometry

```text theme={null}
// Create profile
CREATE WIRE EDGES 
[CREATE LINE FROM 0 0 0 TO 100 0 0] AND
[CREATE ARC CENTER 100 50 0 START 100 0 0 NORMAL 0 0 1 ANGLE 180] AND
[CREATE LINE FROM 100 100 0 TO 0 100 0] AND
[CREATE LINE FROM 0 100 0 TO 0 0 0]

// Create solid
CREATE PRISM SECTION [previous-wire] LENGTH 50
```

### Reusable Components

```text theme={null}
// Define component
CREATE FOLDER NAME "Standard Holes"

// Create template
CREATE CIRCLE CENTER x y z RADIUS r NORMAL nx ny nz
CREATE PRISM SECTION [previous-circle] LENGTH l

// Save for reuse
SAVE AS "Through Hole Template"
```

## Performance Tips

### 1. Optimize Operations

* Combine similar operations
* Reduce boolean operations
* Use patterns instead of individual features

### 2. Memory Management

* Clean up temporary geometry
* Use folders for organization
* Reference existing features when possible

<Tip>
  When creating complex patterns, test with a small number of iterations first before scaling up.
</Tip>

## Debugging Strategies

### Common Issues

1. **Geometry Fails**
   * Check input parameters
   * Verify relationships
   * Ensure valid operations
2. **Performance Problems**
   * Reduce operation complexity
   * Use efficient patterns
   * Clean up unused features

<Note>
  Keep your scripts modular. This makes debugging and modifications much easier later.
</Note>

## Examples

### Parametric Flange

```text theme={null}
// Base parameters
SET flange_diameter = 100
SET hole_count = 6
SET hole_diameter = 8

// Create base
CREATE CIRCLE CENTER 0 0 0 
RADIUS [flange_diameter/2] 
NORMAL 0 0 1
CREATE PRISM SECTION [previous-circle] LENGTH 10

// Create hole pattern
CREATE CIRCLE CENTER [flange_diameter*0.4] 0 0 
RADIUS [hole_diameter/2] 
NORMAL 0 0 1
CREATE PRISM SECTION [previous-circle] LENGTH 10
CREATE REVOLVE PROFILE [previous-hole] 
AXIS ORIGIN 0 0 0 DIRECTION 0 0 1 ANGLE 360
```

<Tip>
  Test your scripts with different parameters to ensure they work across a range of values.
</Tip>
