creating a model with a program or script

I want to write a program or script that creates random models of castles, using some programming language I am familiar with like C++ or VB - i.e. type in a random number and get a 3D castle. To do that I need to be able to create a blend file in text form from scratch. Can anyone recommend a good beginner’s explanation or tutorial on how blend files are constructed, what the headers they use, how the data for vertex locations etc. is formatted?

Creating .blend files from scratch seems like quite a long and difficult solution. You could write an addon with python that, or you could take a modular approach and have sets of different models that can be combined randomly based on your set rules to create a castle.

I’m sure theres a page on the blender wiki about the file format itself if you are definitely looking to go down that way. But that seems like a very long and treacherous road to head down for what your looking for. But I cant see the benefit of constructing the file and having to deal with setting the data manually in the file, when blender can do this for you with one line of python. You would just need to get a script that deals with the random creation of the castles.