The judge allowed an urgent application by the Great Western Hospitals NHS Foundation Trust for permission to carry out 'serious medical treatment' on the 25-year-old - known as 'AA' - who is pregnant with her first child and has a history of 'affective bipolar disorder'.
He also ruled it would be lawful and in the woman's 'best interests' for hospital staff to ensure she does not leave at any stage until it is 'clinically appropriate' for her to be discharged.
The judge directed that any physical restraint or force used on her should be the minimum necessary, and all reasonable steps should be taken to minimise her distress and ensure she retains 'the greatest dignity possible in all the circumstances'.
Mr Justice Hayden, sitting in the Family Division of the High Court in London, said 'AA' and her partner welcomed the pregnancy and they had the support of her parents.
In a ruling on Tuesday this week, the judge said AA was now 38 weeks pregnant but appeared unable 'to comprehend any aspect of her treatment'.
She had been prescribed 'a battery of anti-psychotic medication' for her bipolar condition, and also had a history of substance and alcohol abuse, most likely linked to efforts to self-medicate, particularly in the early days of her illness.
The judge said AA's father, who had been with her throughout her 'long journey' of mental ill-health, had described her present condition as 'the worst he has seen'.
The father told the court she was very agitated, 'violent and exhausted by lack of sleep'.
A few days ago, on January 29, AA appeared at the Great Western Hospital, Swindon, Wiltshire, confused and disorientated after a suspected seizure.
She was admitted to the hospital's labour suite, although - at the time of the High Court hearing - she had not gone into labour.
The day after arriving at the hospital, she was detained under the Mental Health Act 1983 and remained 'highly agitated and largely unco-operative with almost every aspect of her obstetric care'.
Because of her pregnancy, it was not possible to administer her full range of anti-psychotic medication or the required dose.
The judge said the dilemma for doctors was that membranes had ruptured, giving rise to a significantly increased risk of both mother and unborn baby suffering infection before delivery.
Ordinarily in such circumstances, labour would be induced, but her medical team were entirely clear that she would be unable to co-operate and that it would be in her best interests and the safest option to perform an planned caesarean under general anaesthetic.
Her father said the doctors and nurses had been 'fantastic' and he supported them '100%'.
He had told the court his daughter had already moved intravenous lines on more than one occasion, said the judge.
On the night before the court hearing, AA had become more distressed. She had run at a window and tried to get out, telling her father 'she wanted to go to heaven'.
The judge said AA's partner was in difficult circumstances and not always consistent or logical, but he believed he was saying that if AA 'were not suffering this profound psychotic episode... she would follow the recommendation of the doctors'.
The judge ruled: "In all the circumstances, therefore, it is perfectly clear that AA , at the moment, lacks capacity to take these crucial decisions, and that the case made by the Trust for elective caesarean is, as the official solicitor (appointed to safeguard AA's best interests) observed, compelling.
"For the avoidance of doubt the official solicitor has supported every aspect of the Trust's case".