How to save all tolerancing data: MonteCarlo, Sensitivity and Summary in ZOSAPI

Userlevel 5
Badge +3

The summary is in the text file. The way to save text files from the API is to use the “Output to File” entry that lives in the Classic tab of the Tolerancing tool in the OS GUI. This will save the tolerancing output by the name specified in the same folder that the system is in (be sure to include .TXT to the end). The corresponding API code for this is below:

tol.OutputFile = "OutputName.TXT"

To store the MonteCarlo and sensitivity data, we need to use the tolerance data viewer to open the saved ztd file and then store the dataset.

In order to save the .ZTD files from the ZOS-API, you will need to use the SaveTolDataFile property, and you may set the filename via TolDataFile under ZOSAPI.Tools.Tolerancing.ITolerancing Interface Reference:

tol.SaveTolDataFile = True

tol.TolDataFile = "myFile.ztd"

Then, once the tolerancing is done, from the Tolerance Data Viewer, you may read the created .ZTD files, by defining the file name and then running the tool. The corresponding python code is as follows:


TolReader.FileName= "\Documents\Zemax\Samples\Non-sequential\Miscellaneous\Digital_projector_flys_eye_homogenizer.ZTD"


To pull the Monte Carlo data for example, you may use the following lines:

mcData = TolReader.MonteCarloData()

mcDataVals = mcData.Values

mcDataTable = mcDataVals.Data

mcDataTable_reshape = reshape(mcDataTable, mcDataVals.Cols, mcDataVals.Rows)

(You can read the first row by typing: mcDataTable_reshape[0])


Be careful:

Regarding the file name definitions for the saved .TXT and .ZTD files, the files are only saved if a valid file name is provided. The filename should include the specified name together with the file extension, however, it should not include the path. The path is always the same as the current lens file. So you should only provide a file name without a path, both for the TolDataFile and for the OutputFile, such as below:

tol.TolDataFile = "myFile.ztd"

tol.OutputFile = "OutputName.TXT"

The Sensitivity Data contains different types data structures than the Monte Carlo Data.

If we take a look at the Monte Carlo data structure under the ZOSAPI.Tools.Tolerancing.IMonteCarloData Interface Reference, we can see that there is a Values property, which returns the results in a matrix format. However, for the Sensitivity Data, there is no Values property in the OSAPI.Tools.Tolerancing.ISensitivityData Interface Reference, so we cannot call it.

The sample code to read sensitivity data:

TolReader = TheSystem.Tools.OpenToleranceDataViewer()

# Choose the file we are viewing in the TDV

TolReader.FileName = 'C:\Users\...\Simple_singlet_Tol.ZTD'


# Sensitivity data

sdata = TolReader.SensitivityData

# Access criterion, compensator, operand data

criterion = sdata.GetCriterion(0)

criterionName = str(criterion.Name)

nomVal = criterion.NominalValue

# Compensator data – access the first compensator in the list

compData = sdata.GetCompensator(0)

compType = str(compData.OperandType)

compMin = compData.Minimum

compMax = compData.Maximum

compMean = compData.Mean

# Operand data – access the first operand in the list

sdOp = sdata.GetOperand(0)

sdOpType = str(sdOp.OperandType)

sdOpMin = sdOp.Minimum

sdOpMax = sdOp.Maximum

sdOpComment = str(sdOp.Comment)

sdEffect = sdOp.GetEffectOnCriterion(0)

sdEffectMax = sdEffect.EstimatedChangeMaximum

sdEffectMin = sdEffect.EstimatedChangeMinimum

# Grab some worst offender data as it applies to the first (and only) criterion

numCriteria = sdOp.NumberOfCriteria

effect = sdOp.GetEffectOnCriterion(0)

estChangeMin = effect.EstimatedChangeMinimum

estChangeMax = effect.EstimatedChangeMaximum

# Access the summary

summary = str(tdv.Summary)



If you want to load the tolerance script via ZOSAPI:

tool= TheSystem.Tools.OpenTolerancing


By using the GetCriterionScriptAt() member function, you may double-check the name of the file, however, it will not set the script.


0 replies

Be the first to reply!