Special Align and Orientate Script needed. - urgent.

I am very new to scripting and am trying to achieve some things doing pivot/normals/and face alignments, but cannot succeed.

I was wondering if you could help me.

The problem...

We want to align a rectangular plane to lots of other planes scattered in space. The scattered planes have 1 common pivot point at 0,0,0.

We,ve managed to reset the pivots, center them to the planes and align Z to the normal. But the problem is that the X and Y axes are not parallel/perpendicular to the edges of the plane. So when we want to align the new plane, we still would need to rotate in Z to get the planes to match. Sometimes the planes are also of different scale. But proportions are similar.

We thought something like.... "get plane info, search for longest edge and align pivot axis X to it..."

Or... " number vertices colckwise from 1-4 looking at the normal. then align incoming plane to the 4 vertices. kind of registering when you do a 3d scan.It could also start renumbering the vertices beginning from the lowest in Y (world space i.e.) so to have a better orientation.

I would appreciate If you could help us.

We can afford to spend a couple of U$D if your results are fast and your price reasonable.

Just contact me if you need anything else.

Best regards.