An algorithm to generate toroidal or helical cage structures has been developed. Any toroidal or helical structure can be generated following four stages. In the first stage a Fonseca type unit cell and its symmetrical counterpart is formed which represents one-fifth of a toroid. In the second stage one-fifth fragment of the torus is fully obtained by applying geometry optimization to the structure obtained in the first stage. In the third stage the torus fragment obtained in the second stage is reproduced five times and connected to each other to generate either toroidal or helical structure. In the last stage a final optimization process is reapplied to get the complete structure desired.