    

 ## On this page

  

 

 # PythonStandalone\_21\_White\_LED\_Phosphor

 Last update: 17.07.2025 

 1import clr, os, winreg, sys

 2from itertools import islice

 3 

 4import time

 5 

 6class PythonStandaloneApplication(object):

 7 class LicenseException(Exception):

 8 pass

 9 class ConnectionException(Exception):

 10 pass

 11 class InitializationException(Exception):

 12 pass

 13 class SystemNotPresentException(Exception):

 14 pass

 15 

 16 def __init__(self, path=None):

 17 \# determine location of ZOSAPI\_NetHelper.dll &amp; add as reference

 18 aKey = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER), r"Software\\Zemax", 0, winreg.KEY_READ)

 19 zemaxData = winreg.QueryValueEx(aKey, 'ZemaxRoot')

 20 NetHelper = os.path.join(os.sep, zemaxData[0], r'ZOS-API\\Libraries\\ZOSAPI\_NetHelper.dll')

 21 winreg.CloseKey(aKey)

 22 clr.AddReference(NetHelper)

 23 import ZOSAPI_NetHelper

 24 

 25 \# Find the installed version of OpticStudio

 26 if path is None:

 27 isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize()

 28 else:

 29 \# Note -- uncomment the following line to use a custom initialization path

 30 isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize(path)

 31 

 32 \# determine the ZOS root directory

 33 if isInitialized:

 34 dir = ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory()

 35 else:

 36 raise PythonStandaloneApplication.InitializationException("Unable to locate Zemax OpticStudio. Try using a hard-coded path.")

 37 

 38 \# add ZOS-API referencecs

 39 clr.AddReference(os.path.join(os.sep, dir, "ZOSAPI.dll"))

 40 clr.AddReference(os.path.join(os.sep, dir, "ZOSAPI\_Interfaces.dll"))

 41 import ZOSAPI

 42 

 43 \# create a reference to the API namespace

 44 self.ZOSAPI = ZOSAPI

 45 

 46 \# create a reference to the API namespace

 47 self.ZOSAPI = ZOSAPI

 48 

 49 \# Create the initial connection class

 50 self.TheConnection = [ZOSAPI.ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)()

 51 

 52 if self.TheConnection is None:

 53 raise PythonStandaloneApplication.ConnectionException("Unable to initialize .NET connection to ZOSAPI")

 54 

 55 self.TheApplication = self.TheConnection.CreateNewApplication()

 56 if self.TheApplication is None:

 57 raise PythonStandaloneApplication.InitializationException("Unable to acquire ZOSAPI application")

 58 

 59 if self.TheApplication.IsValidLicenseForAPI == False:

 60 raise PythonStandaloneApplication.LicenseException("License is not valid for ZOSAPI use")

 61 

 62 self.TheSystem = self.TheApplication.PrimarySystem

 63 if self.TheSystem is None:

 64 raise PythonStandaloneApplication.SystemNotPresentException("Unable to acquire Primary system")

 65 

 66 def __del__(self):

 67 if self.TheApplication is not None:

 68 self.TheApplication.CloseApplication()

 69 self.TheApplication = None

 70 

 71 self.TheConnection = None

 72 

 73 def OpenFile(self, filepath, saveIfNeeded):

 74 if self.TheSystem is None:

 75 raise PythonStandaloneApplication.SystemNotPresentException("Unable to acquire Primary system")

 76 self.TheSystem.LoadFile(filepath, saveIfNeeded)

 77 

 78 def CloseFile(self, save):

 79 if self.TheSystem is None:

 80 raise PythonStandaloneApplication.SystemNotPresentException("Unable to acquire Primary system")

 81 self.TheSystem.Close(save)

 82 

 83 def SamplesDir(self):

 84 if self.TheApplication is None:

 85 raise PythonStandaloneApplication.InitializationException("Unable to acquire ZOSAPI application")

 86 

 87 return self.TheApplication.SamplesDir

 88 

 89 def ExampleConstants(self):

 90 if self.TheApplication.LicenseStatus == self.ZOSAPI.LicenseStatusType.PremiumEdition:

 91 return "Premium"

 92 elif self.TheApplication.LicenseStatus == self.ZOSAPI.LicenseStatusTypeProfessionalEdition:

 93 return "Professional"

 94 elif self.TheApplication.LicenseStatus == self.ZOSAPI.LicenseStatusTypeStandardEdition:

 95 return "Standard"

 96 else:

 97 return "Invalid"

 98 

 99 def reshape(self, data, x, y, transpose = False):

 100 """Converts a System.Double\[,\] to a 2D list for plotting or post processing

 101 

 102 Parameters

 103 ----------

 104 data : System.Double\[,\] data directly from ZOS-API 

 105 x : x width of new 2D list [use var.GetLength(0) for dimension]

 106 y : y width of new 2D list [use var.GetLength(1) for dimension]

 107 transpose : transposes data; needed for some multi-dimensional line series data

 108 

 109 Returns

 110 -------

 111 res : 2D list; can be directly used with Matplotlib or converted to

 112 a numpy array using numpy.asarray(res)

 113 """

 114 if type(data) is not list:

 115 data = list(data)

 116 var_lst = [y] * x;

 117 it = iter(data)

 118 res = [list(islice(it, i)) for i in var_lst]

 119 if transpose:

 120 return self.transpose(res);

 121 return res

 122 

 123 def transpose(self, data):

 124 """Transposes a 2D list (Python3.x or greater). 

 125 

 126 Useful for converting mutli-dimensional line series (i.e. FFT PSF)

 127 

 128 Parameters

 129 ----------

 130 data : Python native list (if using System.Data[,] object reshape first) 

 131 

 132 Returns

 133 -------

 134 res : transposed 2D list

 135 """

 136 if type(data) is not list:

 137 data = list(data)

 138 return list(map(list, zip(*data)))

 139 

 140if __name__ == '\_\_main\_\_':

 141 zos = PythonStandaloneApplication()

 142 

 143 \# load local variables

 144 ZOSAPI = zos.ZOSAPI

 145 TheApplication = zos.TheApplication

 146 TheSystem = zos.TheSystem

 147 if not os.path.exists(TheApplication.SamplesDir + "\\\\API\\\\Python"):

 148 os.makedirs(TheApplication.SamplesDir + "\\\\API\\\\Python")

 149 

 150 \#! \[e21s01\_py\]

 151 \# Create a new non-sequential file

 152 TheSystem.New(False)

 153 TheSystem.MakeNonSequential()

 154 \# Add new catalog MISC

 155 TheSystem.SystemData.MaterialCatalogs.AddCatalog('MISC')

 156 \# Set Wave #1 to 0.47 micron

 157 TheSystem.SystemData.Wavelengths.GetWavelength(1).Wavelength = 0.47

 158 \# Use lumens as the source unit

 159 TheSystem.SystemData.Units.SourceUnits = ZOSAPI.SystemData.ZemaxSourceUnits.Lumens

 160 \#! \[e21s01\_py\]

 161 

 162 \#! \[e21s02\_py\]

 163 \# Add 4 more objects

 164 TheNCE = TheSystem.NCE

 165 TheNCE.AddObject()

 166 TheNCE.AddObject()

 167 TheNCE.AddObject()

 168 TheNCE.AddObject()

 169 \#! \[e21s02\_py\]

 170 

 171 \#! \[e21s03\_py\]

 172 \# Set 1st object as a Source File

 173 Object_1 = TheNCE.GetObjectAt(1)

 174 Typeset_SourceFile = Object_1.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.SourceFile)

 175 Typeset_SourceFile.FileName1 = 'RAYFILE\_LB\_T67C\_100K\_190608\_ZEMAX.DAT'

 176 Object_1.ChangeType(Typeset_SourceFile)

 177 Object_1.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par1).IntegerValue = 5

 178 Object_1.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par2).IntegerValue = 1000

 179 Object_1.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par3).DoubleValue = 2.485572

 180 Object_1.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par8).DoubleValue = 0.47

 181 Object_1.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par9).DoubleValue = 0.47

 182 \#! \[e21s03\_py\]

 183 

 184 \#! \[e21s04\_py\]

 185 \# Edit source data of object 1

 186 \# SourcesData includes all the settings in Object Properties &gt; Sources

 187 Object_1.SourcesData.PrePropagation = -0.2

 188 Object_1.SourcesData.ArrayType = ZOSAPI.Editors.NCE.ArrayMode.Rectangular

 189 Object_1.SourcesData.ArrayNumberX = 5

 190 Object_1.SourcesData.ArrayNumberY = 5

 191 \#! \[e21s04\_py\]

 192 

 193 \#! \[e21s05\_py\]

 194 \# Set 2nd object as CAD Part: STEP/IGES/SAT

 195 Object_2 = TheNCE.GetObjectAt(2)

 196 Typeset_CADPartSTEPIGESSAT = Object_1.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.CADPartSTEPIGESSAT)

 197 Typeset_CADPartSTEPIGESSAT.FileName1 = 'LB\_T67C\_190608\_GEOMETRY.STEP'

 198 Object_2.ChangeType(Typeset_CADPartSTEPIGESSAT)

 199 \#! \[e21s05\_py\]

 200 

 201 \#! \[e21s06\_py\]

 202 \# Set Rays Ignore Object = Always for object 2

 203 \# TypeData includes all settings in Object Properties &gt; Type

 204 Object_2.TypeData.RaysIgnoreObject = ZOSAPI.Editors.NCE.RaysIgnoreObjectType.Always

 205 \#! \[e21s06\_py\]

 206 

 207 \#! \[e21s07\_py\]

 208 \# Set 3rd object as Cylinder Volume

 209 Object_3 = TheNCE.GetObjectAt(3)

 210 Typeset_CylinderVolume = Object_3.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.CylinderVolume)

 211 Object_3.ChangeType(Typeset_CylinderVolume)

 212 \# Set positions, material and parameters

 213 Object_3.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.ZPosition).DoubleValue = 0.8

 214 Object_3.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Material).Value = 'PMMA'

 215 Object_3.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par1).DoubleValue = 1.2

 216 Object_3.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par2).DoubleValue = 0.1

 217 Object_3.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par3).DoubleValue = 1.2

 218 \#! \[e21s07\_py\]

 219 

 220 \#! \[e21s08\_py\]

 221 \# Make Face 1 of object 3 has Lambertian scattering properties

 222 \# To set scatter properties, you need to first create "ScatteringSettings" by "CreateScatterModelSettings()" method.

 223 \# And then assign is to object 3 by ChangeScatterModelSettings().

 224 ScatType_Lam = Object_3.CoatScatterData.GetFaceData(1).CreateScatterModelSettings(ZOSAPI.Editors.NCE.ObjectScatteringTypes.Lambertian)

 225 ScatType_Lam._S_Lambertian.ScatterFraction = 1

 226 Object_3.CoatScatterData.GetFaceData(1).ChangeScatterModelSettings(ScatType_Lam)

 227 Object_3.CoatScatterData.GetFaceData(1).NumberOfRays = 1

 228 \#! \[e21s08\_py\]

 229 

 230 \#! \[e21s09\_py\]

 231 \# Make object 3 a volume scattering material

 232 \# VolumePhysicsData includes all settings in Object Properties &gt; VolumePhysics.

 233 \# Use Photoluminescence model

 234 Object_3.VolumePhysicsData.Model = ZOSAPI.Editors.NCE.VolumePhysicsModelType.PhotoluminescenceModel

 235 Photo_setting = Object_3.VolumePhysicsData.ModelSettings._S_PhotoluminescenceModel

 236 \# Use Standard Algorithm

 237 Photo_setting.BasicAlgorithm = False

 238 \# Set absorption, emission and quantum yield files

 239 Photo_setting.AbsorptionFile = '\_sample\_3.ZAS'

 240 Photo_setting.EmissionFile = '\_sample\_3.ZES'

 241 Photo_setting.QuantumYield = '\_sample\_3.ZQE'

 242 \# set efficiency spectrum to quantum yield

 243 Photo_setting.EfficiencySpectrum = ZOSAPI.Editors.NCE.EfficiencySpectrumType.QuantumYield

 244 \# set photoluminescence parameters

 245 Photo_setting.ExtinctionCoefficient = 1E+05

 246 Photo_setting.ExtinctionWavelength = 0.47

 247 Photo_setting.PLDensity = 3.1E+017

 248 \# Set Model to Ignore Mie Scattering

 249 Photo_setting.ConsiderMieScattering = False

 250 \#! \[e21s09\_py\]

 251 

 252 \#! \[e21s10\_py\]

 253 \# Set 4th object as Standard Lens

 254 Object_4 = TheNCE.GetObjectAt(4)

 255 Typeset_StandardLens = Object_4.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.StandardLens)

 256 Object_4.ChangeType(Typeset_StandardLens)

 257 \# Set positions

 258 Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.ZPosition).DoubleValue = 0.9

 259 \# To set solve for any cell, you need to first create a "ISolveData" by "CreateSolveType()" method.

 260 \# And then assign it to the cell.

 261 Solve_ObjPick = Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Material).CreateSolveType(ZOSAPI.Editors.SolveType.ObjectPickup)

 262 Solve_ObjPick._S_ObjectPickup.Object = 3

 263 \# Set parameters

 264 Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Material).SetSolveData(Solve_ObjPick)

 265 Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par3).DoubleValue = 1.2

 266 Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par4).DoubleValue = 1.2

 267 Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par5).DoubleValue = 1.2

 268 Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par6).DoubleValue = -1.2

 269 Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par8).DoubleValue = 1.2

 270 Object_4.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par9).DoubleValue = 1.2

 271 \#! \[e21s10\_py\]

 272 

 273 \#! \[e21s11\_py\]

 274 \# Set 5th object as Detector Color

 275 Object_5 = TheNCE.GetObjectAt(5)

 276 Typeset_DetectorColor = Object_5.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.DetectorColor)

 277 Object_5.ChangeType(Typeset_DetectorColor)

 278 \# Set positions, material and parameters

 279 Object_5.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.ZPosition).DoubleValue = 7

 280 Object_5.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Material).Value = 'ABSORB'

 281 Object_5.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par1).DoubleValue = 5

 282 Object_5.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par2).DoubleValue = 5

 283 Object_5.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par3).IntegerValue = 150

 284 Object_5.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par4).IntegerValue = 150

 285 Object_5.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par6).IntegerValue = 4

 286 Object_5.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par7).IntegerValue = 3

 287 \#! \[e21s11\_py\]

 288 

 289 \#! \[e21s12\_py\]

 290 \# Open NSC Ray Trace tool and turn on Scatter NSC Rays and Ignore Errors

 291 RayTraceControl = TheSystem.Tools.OpenNSCRayTrace()

 292 RayTraceControl.SplitNSCRays = False

 293 RayTraceControl.ScatterNSCRays = True

 294 RayTraceControl.UsePolarization = False

 295 RayTraceControl.IgnoreErrors = True

 296 RayTraceControl.SaveRays = False

 297 

 298 \# Trace rays and report the progress when it's running.

 299 \# Note that, instead an RunAndWaitCompletion(), Run() is used so that

 300 \# the code will just go on without waiting the tracing finishs.

 301 \# We will check the progress of tracing by a while loop.

 302 \# You can check the properties "Progress", which is percentage integer data (1-100)

 303 

 304 print('Starting Tracing...')

 305 RayTraceControl.ClearDetectors(0)

 306 RayTraceControl.Run()

 307 while (RayTraceControl.Progress &lt; 100):

 308 sys.stdout.write("\\r" + str(RayTraceControl.Progress) + '%')

 309 sys.stdout.flush()

 310 time.sleep(1)

 311 

 312 RayTraceControl.Close()

 313 print('\\nFinished!')

 314 \#! \[e21s12\_py\]

 315 

 316 \#! \[e21s13\_py\]

 317 \# Open two detector viewers for showing results in angle space and position space

 318 \# Detector Viewer has its own settings interface: IAS\_DetectorViewer.

 319 \# Note that not all analyses have a specific settings interface.

 320 TheAnalysis = TheSystem.Analyses

 321 Det1 = TheAnalysis.New_DetectorViewer()

 322 Det_Set1 = Det1.GetSettings()

 323 

 324 Det_Set1.ShowAs = ZOSAPI.Analysis.DetectorViewerShowAsTypes.TrueColor

 325 Det_Set1.Smoothing = 3

 326 Det1.ApplyAndWaitForCompletion()

 327 

 328 Det2 = TheAnalysis.New_DetectorViewer()

 329 Det_Set2 = Det2.GetSettings()

 330 

 331 Det_Set2.ShowAs = ZOSAPI.Analysis.DetectorViewerShowAsTypes.TrueColor

 332 Det_Set2.Smoothing = 3

 333 Det_Set2.DataType = ZOSAPI.Analysis.DetectorViewerShowDataTypes.AngleSpace

 334 Det2.ApplyAndWaitForCompletion()

 335 \#! \[e21s13\_py\]

 336 

 337 TheSystem.SaveAs(TheApplication.SamplesDir + '\\\\API\\\\Python\\\\e21\_White\_LED\_Phosphor.zos')

 338 

 339 

 340 

 341 \# This will clean up the connection to OpticStudio.

 342 \# Note that it closes down the server instance of OpticStudio, so you for maximum performance do not do

 343 \# this until you need to.

 344 del zos

 345 zos = None

[ZOSAPI.ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)

**Definition:** ZemaxService.cs:198