A British man who tried to have sex with a traffic cone inside a train station elevator steered clear of jail Thursday when a judge sentenced him to a drug rehab program, according to a report.

Two workers at the Wigan North Western station in greater Manchester observed Trevor Smith, 48, “thrusting his hips” as he sat on the elevator floor with his pants and underwear pulled down on April 14, the Manchester Evening News reported.

Smith told cops who discovered him atop the cone — with the residue of white powder on his face — that his trousers had simply “fallen down,” according to the news outlet.

“I believe he was trying to have sex with the cone,” one of the workers said in court. “I was in complete shock. I have not seen anything like that in all my time working on the railways.”

One of the men said Smith was “clearly” under the influence of drugs at the time.

“When the lift door opened, the defendant was now on top of the cone and appeared to be thrusting his hips as though he was having sex with it,” prosecutor Duncan Wilcox said. “It was clearly a lift to which members of the public had access.”

Smith told investigators that he spent the hours leading up to the incident boozing it up while watching soccer.

His friends thought he was getting “down,” so they gave him a substance he believed to be cocaine to perk him up, the court heard. Police found him with a bag of narcotics and arrested him on suspicion of a drug offense.

Smith told police he entered the elevator thinking it was a locked room and that he did not remember anything else.

He has eight previous convictions, including two for public sex acts.

In 2012, a female neighbor saw him performing a sex act naked while on all fours on the roof of a kitchen, according to the report.

He received a conditional discharge but was arrested again three years later after being found performing an indecent act in a car. When asked to stop by a passer-by, Smith refused and continued to pleasure himself for up to 40 minutes.

Smith was slapped with a three-month jail stint after he violated a three-year sentence of community service.

On Thursday, Judge Martin Walsh let Smith off with a suspended sentence and ordered him to take part in a drug rehab program of up to 30 weeks.

“Thankfully, no members of the public witnessed the disgusting behavior that was taking place,” the judge told him. “Any decent members of the community who might have witnessed the offense would have been utterly appalled.”