I do it exactly like aziator, but I use a white grease pencil to outline what I want to cut.
Then I take one of those 'craft knives' - the ones where you click the blade out and snap off segment about 1/4" at a time - and extend it enough that I cut through the entire foam all at once. Like a jigsaw.
When done, I flip the foam over, so any white is on the underside.
Just go slow and make a lot of up and down motions (again, like a jig saw). The more up and down motion, the less it'll want to tear.
I trace with a black marker on the side of the foam that will not be visible (goes down into the case). This will be obvious to most but: make sure you figure out which way you want the rifle pointing inside the case, then when you are tracing the rifle on the foam flip it to the wrong direction and trace, when you are done tracing and cutting, put the foam into the case with the marked side down and your rifle should be pointing in the preferred direction.
Then I take my handy Cold Steel Recon 1 and it's like a hot knife through butter. Of course any large razor sharp knife will do the trick. Definitely the slow up and down motions when cutting being careful to stay true to the black line.
Make sure to cut on the inside of the black line, because you can always enlarge some parts but once you cut off too much there is no going back and it's expensive to have to order new foam.
Good luck. This does take some talent (like anything artsy-fartsy) but with practice you will attain the skill.
I know, we all did this in kindergarten but you tend to forget stuff after a few decades... and I went to kindergarten in the soviet union, we didn't have paper, much less foam, so I have an excuse. We did play with broken mercury thermometers, ah, those were the days. Mercury can be a lot of fun if you're only a toddler (or young at heart and like to see little blobs of silvery stuff disappear when you put your finger on it and reappear somewhere else...)
1. Go to Wal-Mart
2. Buy Black & Decker electric carving knife for $9.96 (yes, like for carving turkeys)
3. Go to school section and buy $1 box of white chalk
4. Go home, place gun(s) in locations desired, break chalk in half so you have a sharp edge to work with
5. Look straight down onto weapon and trace exactly the outline, but go with smaller trace rather than larger trace
6. Take carving knife and cut to the INSIDE of the chalk line, which gives it that nice "snug" fit
7. Wipe off chalk
8. Be envy of all the other operators!