How to align an object by one or two edges to xyz axes?
I think you can do it by tricks like parenting, constraint, snapping, addon, and scripting.



Method 1 (parenting):


Select the edge you want to align;
ShiftNumpad 7 to align view to selected edge;
ShiftS > Cursor to Selected;
Switch to Object mode, ShiftA to add an empty, F6, toggle Align to View, set it as parent to that object;
Select the empty, AltR;
Select your object, AltP > Clear and Keep Transformation. Then delete the empty.




Method 2 (constraint):


(as method 1)
(as method 1)
(as method 1)
(as method 1)
Add another empty, add a Copy Rotation constraint to it, select the first empty as Target, leave other settings as default;
CtrlA > Apply visual Transform, then delete the other two empties.




Method 3 (snapping):


Select one vertex on the edge you want to align;
ShiftS > Cursor to Selected;
Switch to Object mode, ShiftA to add a new empty, then move it for a certain distance along the expected axis;
Select all vertices of your object in Edit mode, with another vertex on that edge active (NOT the one in Step 1). Enable Snap mode on 3D View header, set Snap Element to Vertex, and make sure the Snap Target is set to Active;
CtrlAltQ to toggle Quad View, switch Pivot Point to 3D Cursor, snap the active vertex to the empty in two of the three Ortho views individually (it may depends on the actual case). Then delete that empty.




Method 4 (addon):

Luckily there is an addon called Precise Align (the latest version is 1.1), which can well handle this:


Select one face that includes the edge you want to align;
Find Precise Align tool panel in the toolbar, click Create Empty button, click Swap Empty Origin if it is not aligned with the expected edge. Make sure Parent to Object is toggled;
Back to Object mode, select that empty, AltR;
Select your object, AltP > Clear and Keep Transformation. Then delete the empty.




Method 5 (scripting):

Actually, I'm not good at scripting, but I'm sure it can be done. Welcome anyone that can complement with the scripting solution here. :)