Rotate a line around 360 degrees using API

Hi everyone, @c.poupin @Mike.Buttery

I’m trying to rotate a vertical line around 360 degrees using API, my code works as shown in the image below, but I’m looking to enhance it by by redefining the CreateTransformed method using list comprehension in a single line…a task I’m currently struggling with!?

here my parameters and the rotation code:

# path for calculation of bars count
out_V_rebar_path = Arc.Create(c, out_rebar_radius, 0, 2*pi, XYZ(1, 0, 0), XYZ(0, 1, 0)).Length

# function for calculating angle rotation
def rebar_rotation(path, spacing):
    sweepAngle = 2 * pi * spacing / path
    return sweepAngle
# rotation angle for the vertical rebars
out_V_angle = rebar_rotation(out_V_rebar_path, V_s)

# rotated rebars count
out_V_rebar_count = int(ceil(2*pi/out_V_angle))

# getting all rotated rebars
out_V_rebar = []
for i in range(out_V_rebar_count+1):   
    a= i*out_V_angle
    rot = Transform.CreateRotation(XYZ.BasisZ, a)
    out_Vert_bar = out_vert_bar.CreateTransformed(rot)
    out_V_rebar.append(out_Vert_bar)

OUT = [i.ToProtoType() for i in out_V_rebar]

Any help would be apreciated

Thanks.

Just work backwards. You have all the variables and methods defined individually, so just start at the last line and begin replacing the inline variable with its definition from the previous line, one by one. Also, what’s the reason for using list comprehension if you’ve already got working code that is easy to follow?

1 Like

@Nick_Boyts

Just for brevity and to avoid writing lengthy code. :wink:
I tried this code, and it works as shown in the image below, however, I don’t know if it’s the optimized one. :thinking:

out_V_rebar = [out_vert_bar.CreateTransformed(Transform.CreateRotation(XYZ.BasisZ, n*out_V_angle)) for n in range(out_V_rebar_count+1)]

Thanks.

In my opinion, list comprehension is actually less “efficient” here as you now have a whole bunch of stuff defined and called in a single line. It is technically shorter, but it’s harder to read and understand on the fly.

As for optimized, you can’t get any more optimized than list comprehension in terms of comparable code length. You’re also not doing anything computationally heavy here, so optimization isn’t a huge concern compared to clarity.

Still always good to practice though.