The trick is to work through sets. It's screwy. Rather than post code into this window, I'll refer you to my Mel tools for CgFX, which you can get from http://developer.nvidia.com/view.asp?IO=cgfx\_mel
Look in the script "cgfxFindShaders.mel" -- the function facet_sg() right at the top.
Basically, you have to iterate through ALL of the shadingEngines in the scene, and one after another ask Maya if the facet is part of that shading group.
If you have a range that crosses shading-group boundaries, you're doubly hosed (a case I didn't bother handling) -- then you have to break-down the range of facets into individual facets and iterate all shading groups on THOSE.
In the last case, you could probably accelerate things by capitalizing on facet coherence -- if you know facet "N" is in shadingGroup "G," then there's a good chance that facet "N+1" is also in shadingGroup "G" so if you check "G" first, chances are your script will run more quickly. Make sense?