|
3 | 3 | ============================================= |
4 | 4 | Author(s): John P T Salvesen |
5 | 5 | |
6 | | -Date: 09-10-2025 |
| 6 | +Date: 18-11-2025 |
7 | 7 | """ |
8 | 8 |
|
9 | 9 | ################################################################################ |
@@ -1300,25 +1300,132 @@ def convert_apertures(parsed_elements, environment): |
1300 | 1300 | ######################################## |
1301 | 1301 | # Initialise parameters |
1302 | 1302 | ######################################## |
1303 | | - a = 1.0 |
1304 | | - b = 1.0 |
| 1303 | + offset_x = 0.0 |
| 1304 | + offset_y = 0.0 |
| 1305 | + a = None |
| 1306 | + b = None |
| 1307 | + dx1 = None |
| 1308 | + dx2 = None |
| 1309 | + dy1 = None |
| 1310 | + dy2 = None |
| 1311 | + aper_type = None |
1305 | 1312 |
|
1306 | 1313 | ######################################## |
1307 | 1314 | # Read values |
1308 | 1315 | ######################################## |
| 1316 | + if 'dx' in ele_vars: |
| 1317 | + offset_x = parse_expression(ele_vars['dx']) |
| 1318 | + if 'dy' in ele_vars: |
| 1319 | + offset_y = parse_expression(ele_vars['dy']) |
1309 | 1320 | if 'ax' in ele_vars: |
1310 | 1321 | a = parse_expression(ele_vars['ax']) |
1311 | 1322 | if 'ay' in ele_vars: |
1312 | 1323 | b = parse_expression(ele_vars['ay']) |
| 1324 | + if "dx1" in ele_vars: |
| 1325 | + dx1 = parse_expression(ele_vars['dx1']) |
| 1326 | + if "dx2" in ele_vars: |
| 1327 | + dx2 = parse_expression(ele_vars['dx2']) |
| 1328 | + if "dy1" in ele_vars: |
| 1329 | + dy1 = parse_expression(ele_vars['dy1']) |
| 1330 | + if "dy2" in ele_vars: |
| 1331 | + dy2 = parse_expression(ele_vars['dy2']) |
| 1332 | + |
| 1333 | + ######################################## |
| 1334 | + # Determine type of aperture |
| 1335 | + ######################################## |
| 1336 | + if any(v is not None for v in [dx1, dx2, dy1, dy2]) and \ |
| 1337 | + any(v is not None for v in [a, b]): |
| 1338 | + raise ValueError( |
| 1339 | + f"Error! Aperture {ele_name} has both rectangular and elliptical definitions. This is not supported.") |
| 1340 | + elif any(v is not None for v in [dx1, dx2, dy1, dy2]): |
| 1341 | + aper_type = 'LimitRect' |
| 1342 | + |
| 1343 | + if dx1 is None and dx2 is None: |
| 1344 | + dx1 = -1.0 |
| 1345 | + dx2 = +1.0 |
| 1346 | + elif dx1 is None and isinstance(dx2, float): |
| 1347 | + if dx2 < 0: |
| 1348 | + dx1 = dx2 |
| 1349 | + dx2 = +1.0 |
| 1350 | + else: |
| 1351 | + dx1 = -1.0 |
| 1352 | + elif dx2 is None and isinstance(dx1, float): |
| 1353 | + if dx1 < 0: |
| 1354 | + dx2 = +1.0 |
| 1355 | + else: |
| 1356 | + dx2 = dx1 |
| 1357 | + dx1 = -1.0 |
| 1358 | + elif isinstance(dx1, float) and isinstance(dx2, float): |
| 1359 | + if dx1 > dx2: |
| 1360 | + tmp = dx1 |
| 1361 | + dx1 = dx2 |
| 1362 | + dx2 = tmp |
| 1363 | + else: |
| 1364 | + # At least one is expression, cannot compare |
| 1365 | + # This might cause issues |
| 1366 | + pass |
| 1367 | + |
| 1368 | + if dy1 is None and dy2 is None: |
| 1369 | + dy1 = -1.0 |
| 1370 | + dy2 = +1.0 |
| 1371 | + elif dy1 is None and isinstance(dy2, float): |
| 1372 | + if dy2 < 0: |
| 1373 | + dy1 = dy2 |
| 1374 | + dy2 = +1.0 |
| 1375 | + else: |
| 1376 | + dy1 = -1.0 |
| 1377 | + elif dy2 is None and isinstance(dy1, float): |
| 1378 | + if dy1 < 0: |
| 1379 | + dy2 = +1.0 |
| 1380 | + else: |
| 1381 | + dy2 = dy1 |
| 1382 | + dy1 = -1.0 |
| 1383 | + elif isinstance(dy1, float) and isinstance(dy2, float): |
| 1384 | + if dy1 > dy2: |
| 1385 | + tmp = dy1 |
| 1386 | + dy1 = dy2 |
| 1387 | + dy2 = tmp |
| 1388 | + else: |
| 1389 | + # At least one is expression, cannot compare |
| 1390 | + # This might cause issues |
| 1391 | + pass |
| 1392 | + |
| 1393 | + |
| 1394 | + elif any(v is not None for v in [a, b]): |
| 1395 | + aper_type = 'LimitEllipse' |
| 1396 | + |
| 1397 | + if a is None: |
| 1398 | + a = 1.0 |
| 1399 | + if b is None: |
| 1400 | + b = 1.0 |
| 1401 | + |
| 1402 | + else: |
| 1403 | + raise ValueError(f"Error! Aperture {ele_name} has no valid definition.") |
1313 | 1404 |
|
1314 | 1405 | ######################################## |
1315 | 1406 | # Create Element |
1316 | 1407 | ######################################## |
1317 | | - environment.new( |
1318 | | - name = ele_name, |
1319 | | - parent = xt.LimitEllipse, |
1320 | | - a = a, |
1321 | | - b = b) |
| 1408 | + if aper_type == 'LimitRect': |
| 1409 | + environment.new( |
| 1410 | + name = ele_name, |
| 1411 | + parent = xt.LimitRect, |
| 1412 | + min_x = dx1, |
| 1413 | + max_x = dx2, |
| 1414 | + min_y = dy1, |
| 1415 | + max_y = dy2, |
| 1416 | + shift_x = offset_x, |
| 1417 | + shift_y = offset_y) |
| 1418 | + continue |
| 1419 | + elif aper_type == 'LimitEllipse': |
| 1420 | + environment.new( |
| 1421 | + name = ele_name, |
| 1422 | + parent = xt.LimitEllipse, |
| 1423 | + a = a, |
| 1424 | + b = b, |
| 1425 | + shift_x = offset_x, |
| 1426 | + shift_y = offset_y) |
| 1427 | + else: |
| 1428 | + raise ValueError(f"Error! Aperture {ele_name} has unsupported definition.") |
1322 | 1429 | continue |
1323 | 1430 |
|
1324 | 1431 | ################################################################################ |
|
0 commit comments