The Seventh Circuit (in U.S. v. Jackson) upholds this against a Second Amendment challenge, in an opinion by Judge Frank Easterbrook.
The Court said in Heller that the Constitution entitles citizens to keep and bear arms for the purpose of lawful self-protection, not for all self-protection. Jackson was distributing illegal drugs (cocaine and unlicensed dextromethorphan hydrobromide tablets) out of his home. The Constitution does not give anyone the right to be armed while committing a felony, or even to have guns in the next room for emergency use should suppliers, customers, or the police threaten a dealer's stash. Jackson says that he lived in a dangerous neighborhood and wanted to protect himself from burglars and other marauders. That may be so, but his decision to operate an illegal home business also matters. Suppose a federal statute said: "Anyone who chooses to possess a firearm in the home for self-protection is forbidden to keep or distribute illegal drugs there." Such a statute would be valid, as Jackson's lawyer conceded. And if Congress may forbid people who possess guns to deal drugs, it may forbid people who deal drugs to possess guns. The statements "if you have a gun, you can't sell cocaine" and "if you sell cocaine, you can't have a gun" are identical.
I think this analysis is ultimately right, but the hypothetical involving the "Anyone who chooses to possess a firearm ..." statute doesn't advance matters much. Statutes that say "anyone who chooses to [exercise a constitutional right] is forbidden to [commit a certain crime]" — and the penalty is higher than the penalty for committing the crime while one is not exercising the constitutional right — might well not be constitutional.
Consider a different hypothetical: Say a federal statute (which had a penalty higher than that for simple fraud) said "Anyone who chooses to possess a firearm in the home for self-protection is forbidden to commit computer frauds at the office." Of course the government could and does forbid frauds. But it doesn't follow that it can impose extra punishment for committing fraud while exercising a Second Amendment right in a way unrelated to the fraud.
What's doing the work, I think, are the earlier sentences: "the Constitution entitles citizens to keep and bear arms for the purpose of lawful self-protection, not for all self-protection," plus "The Constitution does not give anyone the right to be armed while committing a felony, or even to have guns in the next room for emergency use should suppliers, customers, or the police threaten a dealer's stash." The Constitution doesn't protect the use of guns in crimes, and even having the gun around when it materially facilitates the crime (as it does for the drug sale in the next room but not for the computer fraud at the office) is itself a form of use of guns.
Here's what I say related to this in a draft article on Implementing the Right To Keep and Bear Arms, which is forthcoming in several months in the UCLA Law Review; among other things, I focus on the doctrine developed by some state courts applying their state right-to-bear-arms provisions, including by courts that have indeed struck down some other government actions on state right-to-bear-arms grounds:
Many states ban possession of guns while possessing drugs or committing a crime....The right to keep and bear arms in lawful self-defense doesn't include the right to use those arms in a crime. [Footnote: See, e.g., D.C. v. Heller, 554 U.S. at __ (describing the Second Amendment right as being a right to possess guns for "traditionally lawful purposes"); United States v. Bowers, 2008 WL 5396630, *2 (D. Neb. Dec. 23); Cockrum v. State, 24 Tex. 394, 401-03 (1859); State v. Daniel, 391 S.E.2d 90, 97 (W. Va. 1990).] And this would include using the guns in ways short of firing or even brandishing them (for instance, by carrying them in case one wants to fire or brandish them, which might well embolden the criminal and deter others who know that this criminal is armed).
On the other extreme, keeping a gun for self-defense in a way that's unconnected to the crime should generally be seen as the exercise of one's constitutional right: Consider, for instance, a person who possesses a gun for home defense while engaged in consensual sex with someone under the age of consent, or while committing a fraud at work. [Footnote: See, e.g., Biddinger v. State, 846 N.E.2d 271, 278 (Ind. Ct. App. 2006) (holding that mere possession of a firearm may not be used as an aggravating factor at sentencing).]
One can hypothesize ways in which even this sort of gun possession could help one commit a crime, for instance to resist arrest in the event that one is caught, or to threaten witnesses or coconspirators should such a threat be necessary. But so long as such possible misuse of a gun is entirely speculative, and not part of either the defendant's past behavior during the crime or clearly planned future behavior, those hypotheses shouldn't suffice to turn constitutionally protected behavior into criminal behavior. And the exercise of constitutionally protected rights in ways that are unconnected with criminal conduct generally can't be used to enhance the sentence for such criminal conduct. [Footnote: See, e.g., Dawson v. Delaware, 503 U.S. 159 (1992).]
This in fact is how many courts have analyzed this, in the "nexus" line of cases: When a gun is not possessed on the person, gun possession can only be treated as criminal or used to enhance a sentence if there is an adequate connection between the possession and the crime. [Footnote: State v. Gurske, 118 P.3d 333, 335 (Wash. 2005) (one in a long line of Washington state cases on the subject); State v. Blanchard, 776 So. 2d 1165 (La. 2001); People v. Atencio, 878 P.2d 147, 150 (Colo. Ct. App. 1994). See also Brewer v. Commonwealth, 206 S.W.3d 343, 347-48 (Ky. 2006) (relying partly on right to bear arms in holding that firearm may not be forfeited based on the owner's conviction of a crime unless there's a nexus between the firearm and the crime).]
In particular, "mere proximity or mere constructive possession is insufficient to establish that a defendant was armed at the time the crime was committed": "[T]he weapon must be easily accessible and readily available for use," "whether to facilitate the commission of the crime, escape from the scene of the crime, protect contraband or the like, or prevent investigation, discovery, or apprehension by the police." [Footnote: Gurske, 118 P.3d at 335--36.] This test is far from perfectly clear, and needs more scholarly attention. But it seems like a reasonable first cut aimed at making sure that criminals are punished for their criminal behavior and not for their constitutionally protected behavior.