Tutorial 2 - Lens ExportΒΆ
This notebook demonstrates how to create and export a lens geometry using the diffinytrace package.
The workflow includes:
Setting up a B-spline surface for the lens geometry.
Converting the lens to a CAD solid.
Exporting the lens as an STL file for use in CAD software
[1]:
import sys
import os
import gc
import tqdm
sys.path.insert(0, os.path.abspath(".."))
device = "cuda:0"
results_folder = "results/"
results_folder += "lens_export/"
try:
os.mkdir(results_folder)
except:
pass
Diffiny trace uses an older version of cadquery with which it is pretty easy to directly talk to open cascade. With this approach it is possible to export bspline lenses without fitting optical lens surfaces
the function dit.export.cad.lens_to_solid returns an object that is understood by open cascade
[2]:
import diffinytrace as dit
import torch
aperture_radius_detector = 12.
bspline_surface1 = dit.Bspline(aperture_radius_detector,[3,3],[10,10])
with torch.no_grad():
bspline_surface1.coeff.data = torch.randn_like(bspline_surface1.coeff.data)*7.
lens_transform = dit.transforms.Identity()
lens_thickness = 20.
surface1 = dit.Plane()
lens1 = dit.Lens(lens_transform,lens_thickness,surface1,bspline_surface1,dit.materials["PMMA"],aperture_radius_detector,is_square=True)
solid = dit.export.cad.lens_to_solid(lens1,20)
solid
c:\Users\marti\anaconda3\envs\working\Lib\site-packages\torch\functional.py:505: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\pytorch\aten\src\ATen\native\TensorShape.cpp:4383.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
[2]:
Although below a resolution and tol is defined bspline surfaces are always exported exactly with no fitting of nurbs surface to geometry. However other lens surfaces will be approximated
[3]:
dit.export.cad.export_lens(results_folder+"lens.stl",lens1,resolution = 200,tol=0.001)
Itβs also possible to export lenses with round apertures.
[4]:
aperture_radius_detector = 12.
bspline_surface1 = dit.Bspline(aperture_radius_detector,[3,3],[10,10])
with torch.no_grad():
bspline_surface1.coeff.data = torch.randn_like(bspline_surface1.coeff.data)*7.
lens_transform = dit.transforms.Identity()
lens_thickness = 20.
surface1 = dit.Plane()
lens1 = dit.Lens(lens_transform,lens_thickness,surface1,bspline_surface1,dit.materials["PMMA"],aperture_radius_detector,is_square=False)
solid = dit.export.cad.lens_to_solid(lens1,20)
solid
[4]:
[5]:
dit.export.cad.export_lens(results_folder+"lens_round.stl",lens1,resolution = 200,tol=0.001)
[ ]: