In manufacturing industry, the automation of robot trajectory planning and robot code generation lowers the development and assessment times of new products and models, increasing the flexibility of a robotic production line. This is of increasing importance in a number of industries where new products and new models are constantly being developed. This paper presents a method that automatically generates robot code for collision free trajectories. Starting with a collision free piecewise linear joint space trajectory, a direct transcription optimal control method is used, iteratively, to improve the trajectory while maintaining a desired clearance. The final solution is a collision free robot trajectory defined by a set of parameters, e.g. joint space via points and TCP-zone radii, that can be used to automatically generate functional RAPID code for an industrial robot. The algorithm is designed to be easily extendable. For example, by changing the constraints on the trajectory or by including energy consumption as an objective. A successful test of the algorithm has been carried out on an industrial stud welding station using the ABB virtual controller, indicating a 10% improvement in motion time.