# 创建光学系统

已在中创建的 {py:class} `OpticStudioSystem <zospy.zpcore.OpticStudioSystem>`对象`oss`[](01_connection.md)可用于从 Python 控制连接的 OpticStudio 实例。此对象将 ZOS-API 命名空间公开为属性。记录这些命名空间不是本文档的目的，因为它们已在 OpticStudio 帮助文件中进行了详细记录。

要创建和修改序列光学系统，可以通过 {py:attr} `oss.LDE <zospy.zpcore.OpticStudioSystem.LDE>`属性访问镜头数据编辑器。

## 例子

```python
import zospy as zp

# Connect to OpticStudio
zos = zp.ZOS()
oss = zos.connect()

# Create a new, empty system
oss.new()

# Insert a new surface at index 1 and adjust its radius and thickness
surface_1 = oss.LDE.InsertNewSurfaceAt(1)
surface_1.Radius = 10
surface_1.Thickness = 5

# Get the STOP surface and adjust its semi-diameter
surface_stop = oss.LDE.GetSurfaceAt(oss.LDE.StopSurface)
surface_stop.SemiDiameter = 2
```

## 使用求解器

OpticStudio 允许使用求解器动态设置表面属性。ZOSPy 通过 {py:mod} `zospy.solvers`模块公开这些求解器。以下示例展示了如何使用 {py:func} `position <zospy.solvers.position>`求解器设置表面的厚度，以及如何使用 {py:func} `material_model <zospy.solvers.material_model>`求解器设置材料的折射率。

```python
import zospy as zp

# Connect to OpticStudio
zos = zp.ZOS()
oss = zos.connect()

surface = oss.LDE.GetSurfaceAt(2)
zp.solvers.position(surface.ThicknessCell, from_surface=oss.LDE.GetSurfaceAt(1), length=10)
zp.solvers.material_model(surface.MaterialCell, refractive_index=1.5)
```

## 贡献求解器

OpticStudio 有许多求解器，但并非所有求解器都已在 ZOSPy 中实现（目前）。如果您想使用尚未实现的求解器，请考虑将其[贡献](../contributing/contributing.md)给 ZOSPy。所有求解器都遵循相同的结构，编写新的求解器非常简单。以下是设置元素功率的求解器的示例：

```python
def element_power(radius_cell: _ZOSAPI.Editors.IEditorCell, power: float) -> _ZOSAPI.Editors.ISolveElementPower:
    """Solver for element power.

    Adjusts the value of `radius_cell` to create an element with the specified `power`. This solver should be set on
    the last surface of the element.

    Parameters
    ----------
    radius_cell : ZOSAPI.Editors.IEditorCell
        Radius cell of the last element surface
    power : float
        Element power in diopters

    Returns
    -------
    solve_data : ZOSAPI.Editors.ISolveElementPower
        The solve data for the element power
    """
    
    # Create an OpticStudio solve data object
    solve_data = radius_cell.CreateSolveType(zp.constants.Editors.SolveType.ElementPower)._S_ElementPower

    # Apply the settings to the solve data
    solve_data.Power = power

    # Add the solve data to the Lens Data Editor cell
    radius_cell.SetSolveData(solve_data)

    return solve_data
```
