|
| 1 | +# Example 01 - C# |
| 2 | + |
| 3 | +```csharp |
| 4 | +using System; |
| 5 | +using ZOSAPI; |
| 6 | +using ZOSAPI.Editors; |
| 7 | +using ZOSAPI.Editors.LDE; |
| 8 | +using ZOSAPI.SystemData; |
| 9 | +using ZOSAPI.Tools.General; |
| 10 | + |
| 11 | +namespace CSharpStandaloneApplication |
| 12 | +{ |
| 13 | + class Program |
| 14 | + { |
| 15 | + static void Main(string[] args) |
| 16 | + { |
| 17 | + // Find the installed version of OpticStudio |
| 18 | + bool isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize(); |
| 19 | + // Note -- uncomment the following line to use a custom initialization path |
| 20 | + //bool isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize(@"C:\Program Files\OpticStudio\"); |
| 21 | + if (isInitialized) |
| 22 | + { |
| 23 | + LogInfo("Found OpticStudio at: " + ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory()); |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + HandleError("Failed to locate OpticStudio!"); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + BeginStandaloneApplication(); |
| 32 | + } |
| 33 | + |
| 34 | + static void BeginStandaloneApplication() |
| 35 | + { |
| 36 | + // Create the initial connection class |
| 37 | + ZOSAPI_Connection TheConnection = new ZOSAPI_Connection(); |
| 38 | + |
| 39 | + // Attempt to create a Standalone connection |
| 40 | + IZOSAPI_Application TheApplication = TheConnection.CreateNewApplication(); |
| 41 | + if (TheApplication == null) |
| 42 | + { |
| 43 | + HandleError("An unknown connection error occurred!"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // Check the connection status |
| 48 | + if (!TheApplication.IsValidLicenseForAPI) |
| 49 | + { |
| 50 | + HandleError("Failed to connect to OpticStudio: " + TheApplication.LicenseStatus); |
| 51 | + return; |
| 52 | + } |
| 53 | + if (TheApplication.Mode != ZOSAPI_Mode.Server) |
| 54 | + { |
| 55 | + HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.Mode.ToString()); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + IOpticalSystem TheSystem = TheApplication.PrimarySystem; |
| 60 | + |
| 61 | + // Add your custom code here... |
| 62 | +
|
| 63 | + // creates a new API directory |
| 64 | + string strPath = System.IO.Path.Combine(TheApplication.SamplesDir, @"API\\CS"); |
| 65 | + System.IO.Directory.CreateDirectory(strPath); |
| 66 | + |
| 67 | + // Set up primary optical system |
| 68 | + string sampleDir = TheApplication.SamplesDir; |
| 69 | + string testFile = sampleDir + "\\API\\CS\\e01_new_file_and_quickfocus.zos"; |
| 70 | + |
| 71 | + //! [e01s01_cs] |
| 72 | + // Make new file |
| 73 | + TheSystem.New(false); |
| 74 | + TheSystem.SaveAs(testFile); |
| 75 | + //! [e01s01_cs] |
| 76 | +
|
| 77 | + TheSystem.SystemData.MaterialCatalogs.AddCatalog("SCHOTT"); |
| 78 | + |
| 79 | + //! [e01s02_cs] |
| 80 | + // Aperture |
| 81 | + ISystemData TheSystemData = TheSystem.SystemData; |
| 82 | + TheSystemData.Aperture.ApertureValue = 40; |
| 83 | + //! [e01s02_cs] |
| 84 | +
|
| 85 | + //! [e01s03_cs] |
| 86 | + // Fields |
| 87 | + IField Field_1 = TheSystemData.Fields.GetField(1); |
| 88 | + IField NewField_2 = TheSystemData.Fields.AddField(0, 5.0, 1.0); |
| 89 | + //! [e01s03_cs] |
| 90 | +
|
| 91 | + //! [e01s04_cs] |
| 92 | + // Wavelength preset |
| 93 | + bool slPreset = TheSystemData.Wavelengths.SelectWavelengthPreset(WavelengthPreset.d_0p587); |
| 94 | + //! [e01s04_cs] |
| 95 | +
|
| 96 | + //! [e01s05_cs] |
| 97 | + // Lens data |
| 98 | + ILensDataEditor TheLDE = TheSystem.LDE; |
| 99 | + TheLDE.InsertNewSurfaceAt(2); |
| 100 | + TheLDE.InsertNewSurfaceAt(2); |
| 101 | + ILDERow Surface_1 = TheLDE.GetSurfaceAt(1); |
| 102 | + ILDERow Surface_2 = TheLDE.GetSurfaceAt(2); |
| 103 | + ILDERow Surface_3 = TheLDE.GetSurfaceAt(3); |
| 104 | + //! [e01s05_cs] |
| 105 | +
|
| 106 | + //! [e01s06_cs] |
| 107 | + // Changes surface cells in LDE |
| 108 | + Surface_1.Thickness = 50.0; |
| 109 | + Surface_1.Comment = "Stop is free to move"; |
| 110 | + Surface_2.Radius = 100.0; |
| 111 | + Surface_2.Thickness = 10.0; |
| 112 | + Surface_2.Comment = "front of lens"; |
| 113 | + Surface_2.Material = "N-BK7"; |
| 114 | + Surface_3.Comment = "rear of lens"; |
| 115 | + //! [e01s06_cs] |
| 116 | +
|
| 117 | + //! [e01s07_cs] |
| 118 | + // Solver |
| 119 | + ISolveData Solver = Surface_3.RadiusCell.CreateSolveType(SolveType.FNumber); |
| 120 | + Solver._S_FNumber.FNumber = 10; |
| 121 | + Surface_3.RadiusCell.SetSolveData(Solver); |
| 122 | + //! [e01s07_cs] |
| 123 | +
|
| 124 | + //! [e01s08_cs] |
| 125 | + // QuickFocus |
| 126 | + IQuickFocus quickFocus = TheSystem.Tools.OpenQuickFocus(); |
| 127 | + quickFocus.Criterion = QuickFocusCriterion.SpotSizeRadial; |
| 128 | + quickFocus.UseCentroid = true; |
| 129 | + quickFocus.RunAndWaitForCompletion(); |
| 130 | + quickFocus.Close(); |
| 131 | + //! [e01s08_cs] |
| 132 | +
|
| 133 | + //! [e01s09_cs] |
| 134 | + // Save and close |
| 135 | + TheSystem.Save(); |
| 136 | + //! [e01s09_cs] |
| 137 | +
|
| 138 | + // Clean up |
| 139 | + FinishStandaloneApplication(TheApplication); |
| 140 | + } |
| 141 | + |
| 142 | + static void FinishStandaloneApplication(IZOSAPI_Application TheApplication) |
| 143 | + { |
| 144 | + // Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases |
| 145 | + if (TheApplication != null) |
| 146 | + { |
| 147 | + TheApplication.CloseApplication(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + static void LogInfo(string message) |
| 152 | + { |
| 153 | + // TODO - add custom logging |
| 154 | + Console.WriteLine(message); |
| 155 | + } |
| 156 | + |
| 157 | + static void HandleError(string errorMessage) |
| 158 | + { |
| 159 | + // TODO - add custom error handling |
| 160 | + throw new Exception(errorMessage); |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +``` |
0 commit comments