Repository navigation
Draw optics for CAD: layers, units, vertex frames, substrates and sparse rays in DXF - #303
jacobdparker wants to merge 3 commits into
Conversation
… DXF Write a full DXF document instead of an R12 stream, so the header gives the units and each surface is drawn on its own layers: its clear and mechanical apertures as closed 3D polylines, the back of its substrate a thickness behind the vertex with edges joining it to the surface, and the vertex of its sag with its axes, so a part can be placed and clocked against it in a CAD program. Lift the edges of the apertures onto the sag by intercepting it along z. Using the height the sag gives in its own coordinates put the edges off the sag whenever the sag was pistoned or tilted with respect to the surface. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A surface which stands for several, like the channels of an instrument, now draws each on layers named after its index, so each can be picked out on its own in a CAD program. `to_dxf` passes its keyword arguments on, so `rays=False` leaves the rays out of the drawing of a sequential system, which takes a drawing of the ESIS-II optics from 24 s and 50 000 polylines to under a second and 172 entities. Plot the apertures and the substrate on the sag the same way the DXF drawing does: the plot had the same error when the sag was pistoned or tilted with respect to the surface. The depth and the back of the substrate are now shared between the two. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ch channel its own `to_dxf(..., rays="sparse")` draws, at each wavelength and through each index of the system along its own axes, the rays through the middle and the four edges of the field of view, each at the middle of the beam and at eight points around its edge. The edges are found from where the rays that pass the whole system cross the field stop and the pupil stop, in each stop's own coordinates, so the choice does not depend on how the grid is laid out (normalized, absolute or polar) or on the shape of the stops. For the ESIS-II design that is 480 rays instead of 49,556, which span 92-100% of the full beam across every surface. The rays through each channel are now drawn on a layer of their own, like `rays_channel0`, as the surfaces are, and are traced without their intensity, which a drawing does not need. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #303 +/- ##
===========================================
- Coverage 100.00% 99.94% -0.06%
===========================================
Files 142 142
Lines 14059 14436 +377
===========================================
+ Hits 14059 14428 +369
- Misses 0 8 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
roytsmart
left a comment
There was a problem hiding this comment.
Thanks Jake, this makes the drawing far more usable for laying out mounts. The switch from sag(position) to sag.intercept along +z is also a real bug fix for Aperture.plot, not just the DXF.
I read the whole diff and checked a few things against the code:
- Nothing else in the workspace overrides
_write_to_dxf(only the legacy kgpy and FURST code user12writer, independently of optika), so changing the writer fromR12FastStreamWriterto aModelspacebreaks nothing downstream. sag.normalandsag.interceptboth take positions in the surface's frame, so_liftand the sign in_depth_substrateare right. The spherical intercept is closed form, so points that start behind z = 0 are handled.
Comments are inline. The first (axes in _where_rays_sparse) is a quick fix and the second (which frame the substrate is in) is a question; the rest are minor. None of it is a problem with the approach.
One more minor point, not on a changed line: the existing AbstractTestDxfWritable.test_to_dxf still writes test_dwg.dxf into the working directory, so it races with itself under pytest -n. The new tests already use tmp_path, so this would be a good time to move that one too.
| # The axes along which the field and the pupil vary, which do not | ||
| # include the axes they share with the wavelength or with the system. | ||
| axes_other = set(na.shape(grid.wavelength)) | set(system.shape) | ||
| axes_field = [a for a in na.shape(grid.field) if a not in axes_other] |
There was a problem hiding this comment.
This works out the field and pupil axes from the shapes of the grid, but the system already declares them: system.axis_wavelength_, system.axis_field_ and system.axis_pupil_ respect an explicit axis_field / axis_pupil. When those are set explicitly and the field grid carries one more axis, this treats that axis as part of the field and picks five field points across the combined axes, where the rest of the system treats it as an axis of its own.
Using the normalized properties would keep the two in step, e.g.
axes_field = [a for a in system.axis_field_ if a in shape]
axes_pupil = [a for a in system.axis_pupil_ if a in shape]with everything else falling into axes_rest as now.
| return -np.sign(normal.z) * thickness | ||
|
|
||
|
|
||
| def _behind( |
There was a problem hiding this comment.
This moves the back of the substrate from the surface's frame to the sag's frame, and plot changes with the DXF. On main, _plot_substrate put the back thickness behind the surface origin, perpendicular to the surface's z axis. Now it is thickness behind the sag's vertex, perpendicular to the sag's axis.
For a small piston or tilt the two agree, but _surface_dxf in the tests describes its transformed sag as "an off-axis segment of a parent surface". For a real off-axis segment the sag's vertex is the parent's vertex, which can be well off the part. The back would then be drawn behind a point that is not on the optic, square to the parent's axis rather than the part's. No ESIS sag has a transformation, so the SolidWorks check doesn't exercise this.
Which frame is the substrate thickness meant to be in? If it is the part's, keeping the back in the surface's frame (and only lifting the front onto the sag) would cover both cases.
| length_axes: u.Quantity = 10 * u.mm, | ||
| **kwargs, |
There was a problem hiding this comment.
Since every _write_to_dxf takes **kwargs, a misspelled option is dropped without a word: system.to_dxf(file, u.mm, ray="sparse") draws all ~50k rays, and surface.to_dxf(..., rays=False) does nothing. length_axes likewise sits on the to_dxf of apertures and rays, where it has no effect.
Would it be clearer to give the sequential system (and the surfaces) their own to_dxf with rays and length_axes as explicit arguments, so that each shows up in the signature and docs of the objects it applies to?
| close=True, | ||
| ) | ||
|
|
||
| vertices = getattr(aperture, "vertices", None) |
There was a problem hiding this comment.
Minor: an aperture without vertices (a circle) gets the back face here but no edge lines, while _plot_substrate walls a circle from the samples of its wire. Probably fine for CAD, since a circle extrudes on its own; just noting that the two differ.
| ) | ||
|
|
||
|
|
||
| _DXF_UNITS = { |
There was a problem hiding this comment.
Minor: $INSUNITS also has codes for Å (11), decameters (15), hectometers (16), gigameters (17), AU (18), light years (19) and parsecs (20), which currently fall back to unitless. ezdxf.enums.InsertUnits lists them all, if you'd rather not keep the table by hand.
| without_rays = test_mixins.dxf_entities(without_rays) | ||
| sparse = test_mixins.dxf_entities(sparse) | ||
|
|
||
| def is_ray(e) -> bool: |
There was a problem hiding this comment.
Nit: e is unannotated here, as is file in _dxf_layer in test_surfaces.py.
Makes
to_dxfproduce a drawing that can be used in CAD to lay out mounts for the optics: each optic is drawn on its own layers, the drawing carries its units, and the frame of each optic and the back of its substrate are drawn too. We tested it by importing the ESIS-II design into the full SolidWorks assembly.What changed
File format
r12writer.$INSUNITSis set from theunitargument, so CAD places the drawing at true size without being told the units.Layers
<name>_clear,<name>_mechanical,<name>_substrateand<name>_vertex.grating_channel0_clear. The rays through each channel do the same, likerays_channel0.Apertures
sag.interceptalong +z, not bysag(position). The old way put the edges at the wrong height whenever the sag had its own piston or tilt (sag.transformation).Aperture.plothad the same bug and now uses the same lift.Substrate
thicknessbehind the vertex along the sag's axis, on the side away from the light.Vertex frame
length_axeslong.Rays
rays=True(the default) draws every ray that passes the system, as before.rays=Falseleaves the rays out.rays="sparse"draws just enough rays to show the beam path. At each wavelength and channel, those are the rays through the middle and the four edges of the field of view, each at the middle of the beam and at eight points around its edge.Behaviour changes
rays_<axis><i>; before, they all went onrays. A system without such axes still usesrays.Testing
plot;raysset toTrue,Falseand"sparse"on every sequential test system;sag(position)lift.rays="sparse"it has 480 rays. Every one is in the full trace, and together they span 92–100% of the full beam on every surface.🤖 Generated with Claude Code