Skip to content

Collimator example

Jiao edited this page Nov 19, 2018 · 5 revisions

The following code

import numpy as np
from instrument.geometry.pml import weave
from instrument.geometry import shapes, operations
# create a pyramid
pyramid = shapes.pyramid(thickness="20.*mm", width="20.*mm", height="110.*mm")
# make it along the beam
pyramid_along_beam = operations.rotate(pyramid, transversal="1", angle="90.*deg")
# make copies that are rotated to different directions 
angles = np.arange(-160, 0., 15.)
pyramids = [operations.rotate(pyramid_along_beam, vertical="1", angle='%s*deg' % a)
            for a in angles]
# these pyramids form the channels 
channels = operations.unite(*pyramids)
# create half of a hollow cylinder
hollow_cylinder = operations.subtract(
    shapes.cylinder(radius="55.*mm", height="50.*mm"),
    shapes.cylinder(radius="25.*mm", height="60.*mm"),
    )
half_cylinder = operations.subtract(
    hollow_cylinder,
    operations.translate(
        shapes.block(height="110*mm", width="110*mm", thickness="110*mm"),
        transversal="-55.*mm",
        )
    )
# take out channels to form collimator
collimator = operations.subtract(half_cylinder, channels)

# need a special xml renderer to just write out the shape, not the extra tags
from instrument.geometry.pml.Renderer import Renderer as base
class Renderer(base):
   def _renderDocument(self, body):
       self.onGeometry(body)
       return
   def header(self): return []
   footer = header
   def end(self): return
# write to a xml file
weave( collimator, open('collimator.xml', 'wt'), print_docs = False, renderer=Renderer(), author='')

# the following write a full xml with <geometry> tag
# weave( collimator, open('collimator-full.xml', 'wt'), print_docs = False)

generates a collimator.xml file that can be converted to openscad model using SCADgen:

image

Clone this wiki locally