# rotate all polygons in certain region

Hello everyone,

I am new to Klayout and I am currently learning draw Layout using python script. I have one problem that cannot fix and cannot find solution in forum. I want to rotate some polygons with respect to certain point. I want to use a region BOX to define those polygons and apply the transformation, below is my code:

def Rotation(layout,sp,box,degree,mirror=0):
r=pya.Region(box)
cp=r.bbox().center()
x=(cp.x-sp.x)-(cp.y-sp.y)
if degree==1:
y=cp.y+(cp.x-sp.x)
elif degree == 3:
y=cp.y-(cp.x-sp.x)
else:
y=cp.y
#t=pya.ICplxTrans(mag,degree,mirror,x,y)
t=pya.Trans(degree,mirror,x,y)
for polygons in r.each():
polygons.transform(t)

The code did not do anything? Appreciate if anyone could take a look and point out what wrong with the codes or is there any better way to do it?

• I could achieve the transform, but i cannot delete the original object.

the code i am using is:

``````def Rotation(layout,lmap,sp,box,degree,mirror=0):
shape_iter=layout.begin_shapes_overlapping(layout,vars(layout)[lmap],box)
t=pya.ICplxTrans(1,degree,mirror,0,0)
while not shape_iter.at_end():
contact = shape_iter.shape().polygon.transform(pya.Trans(-sp.x,-sp.y))
contact = contact.transform(t)
contact = contact.transform(pya.Trans(sp.x,sp.y))
layout.shapes(vars(layout)[lmap]).insert(contact)
shape_iter.next()
``````
• Hi,

as you discovered, the angle representation is not in degree for "Trans". "Trans" is limited to rotations which are multiples of 90 degree and accepts the angle as an integer which gives multiples of 90 degree. "ICplxTrans" is the version capable of doing all-angle transformations and the angle parameter is degree.

To transform the shapes, simple use the transformation directly on the Shape object (which is a reference into the database) rather than the working objects (Polygon etc.):

``````  while not shape_iter.at_end():
shape_iter.shape().transform(pya.Trans(-sp.x,-sp.y))
shape_iter.shape().transform(t)
shape_iter.shape().transform(pya.Trans(sp.x,sp.y))
``````

This can be simplified by precomputing the transformation:

``````  d = pya.Vector(sp.x, sp.y)   # can be skipped if sp is already a vector
t=pya.ICplxTrans(d) * pya.ICplxTrans(1,degree,mirror,0,0) * pya.ICplxTrans(-d)
while not shape_iter.at_end():
shape_iter.shape().transform(t)
``````

Matthias