# NX Documentation Lookup Module

## Overview

This module provides on-demand access to Siemens NX Open and Simcenter documentation via the Dalidou MCP server. Use these tools when building new extractors, NX automation scripts, or debugging NX-related issues.

## CRITICAL: When to AUTO-SEARCH Documentation

**You MUST call `siemens_docs_search` BEFORE writing any code that uses NX Open APIs.**

### Automatic Search Triggers

| User Request | Action Required |
|--------------|-----------------|
| "Create extractor for {X}" | → `siemens_docs_search("{X} NXOpen")` |
| "Get {property} from part" | → `siemens_docs_search("{property} NXOpen.Part")` |
| "Extract {data} from FEM" | → `siemens_docs_search("{data} NXOpen.CAE")` |
| "How do I {action} in NX" | → `siemens_docs_search("{action} NXOpen")` |
| Any code with `NXOpen.*` | → Search before writing |

### Example: User asks "Create an extractor for inertia values"

```
STEP 1: Immediately search
→ siemens_docs_search("inertia mass properties NXOpen")

STEP 2: Review results, fetch details
→ siemens_docs_fetch("NXOpen.MeasureManager")

STEP 3: Now write code with correct API calls
```

**DO NOT guess NX Open API names.** Always search first.

## When to Load

Load this module when:
- Creating new NX Open scripts or extractors
- Working with `NXOpen.*` namespaces
- Debugging NX automation errors
- User mentions "NX API", "NX Open", "Simcenter docs"
- Building features that interact with NX/Simcenter

## Available MCP Tools

### `siemens_docs_search`

**Purpose**: Search across NX Open, Simcenter, and Teamcenter documentation

**When to use**:
- Finding which class/method performs a specific task
- Discovering available APIs for a feature
- Looking up Nastran card references

**Examples**:
```
siemens_docs_search("get node coordinates FEM")
siemens_docs_search("CQUAD4 element properties")
siemens_docs_search("NXOpen.CAE mesh creation")
siemens_docs_search("extract stress results OP2")
```

### `siemens_docs_fetch`

**Purpose**: Fetch a specific documentation page with full content

**When to use**:
- Need complete class reference
- Getting detailed method signatures
- Reading full examples

**Examples**:
```
siemens_docs_fetch("NXOpen.CAE.FemPart")
siemens_docs_fetch("Nastran Quick Reference CQUAD4")
```

### `siemens_auth_status`

**Purpose**: Check if the Siemens SSO session is valid

**When to use**:
- Before a series of documentation lookups
- When fetch requests fail
- Debugging connection issues

### `siemens_login`

**Purpose**: Re-authenticate with Siemens if session expired

**When to use**:
- After `siemens_auth_status` shows expired
- When documentation fetches return auth errors

## Workflow: Building New Extractor

When creating a new extractor that uses NX Open APIs:

### Step 1: Search for Relevant APIs
```
→ siemens_docs_search("element stress results OP2")
```
Review results to identify candidate classes/methods.

### Step 2: Fetch Detailed Documentation
```
→ siemens_docs_fetch("NXOpen.CAE.Result")
```
Get full class documentation with method signatures.

### Step 3: Understand Data Formats
```
→ siemens_docs_search("CQUAD4 stress output format")
```
Understand Nastran output structure.

### Step 4: Build Extractor
Following EXT_01 template, create the extractor with:
- Proper API calls based on documentation
- Docstring referencing the APIs used
- Error handling for common NX exceptions

### Step 5: Document API Usage
In the extractor docstring:
```python
def extract_element_stress(op2_path: Path) -> Dict:
    """
    Extract element stress results from OP2 file.

    NX Open APIs Used:
        - NXOpen.CAE.Result.AskElementStress
        - NXOpen.CAE.ResultAccess.AskResultValues

    Nastran Cards:
        - CQUAD4, CTRIA3 (shell elements)
        - STRESS case control
    """
```

## Workflow: Debugging NX Errors

When encountering NX Open errors:

### Step 1: Search for Correct API
```
Error: AttributeError: 'FemPart' object has no attribute 'GetNodes'

→ siemens_docs_search("FemPart get nodes")
```

### Step 2: Fetch Correct Class Reference
```
→ siemens_docs_fetch("NXOpen.CAE.FemPart")
```
Find the actual method name and signature.

### Step 3: Apply Fix
Document the correction:
```python
# Wrong: femPart.GetNodes()
# Right: femPart.BaseFEModel.FemMesh.Nodes
```

## Common Search Patterns

| Task | Search Query |
|------|--------------|
| Mesh operations | `siemens_docs_search("NXOpen.CAE mesh")` |
| Result extraction | `siemens_docs_search("CAE result OP2")` |
| Geometry access | `siemens_docs_search("NXOpen.Features body")` |
| Material properties | `siemens_docs_search("Nastran MAT1 material")` |
| Load application | `siemens_docs_search("CAE load force")` |
| Constraint setup | `siemens_docs_search("CAE boundary condition")` |
| Expressions/Parameters | `siemens_docs_search("NXOpen Expression")` |
| Part manipulation | `siemens_docs_search("NXOpen.Part")` |

## Key NX Open Namespaces

| Namespace | Domain |
|-----------|--------|
| `NXOpen.CAE` | FEA, meshing, results |
| `NXOpen.Features` | Parametric features |
| `NXOpen.Assemblies` | Assembly operations |
| `NXOpen.Part` | Part-level operations |
| `NXOpen.UF` | User Function (legacy) |
| `NXOpen.GeometricUtilities` | Geometry helpers |

## Integration with Extractors

All extractors in `optimization_engine/extractors/` should:

1. **Search before coding**: Use `siemens_docs_search` to find correct APIs
2. **Document API usage**: List NX Open APIs in docstring
3. **Handle NX exceptions**: Catch `NXOpen.NXException` appropriately
4. **Follow 20-line rule**: If extraction is complex, check if existing extractor handles it

## Troubleshooting

| Issue | Solution |
|-------|----------|
| Auth errors | Run `siemens_auth_status`, then `siemens_login` if needed |
| No results | Try broader search terms, check namespace spelling |
| Incomplete docs | Fetch the parent class for full context |
| Network errors | Verify Dalidou is accessible: `ping dalidou.local` |

---

*Module Version: 1.0*
*MCP Server: dalidou.local:5000*
