Ben Wright recorded a career best 172 and James Allenby scored his first century for Glamorgan as the Welsh county put themselves in a strong position against Gloucestershire going into the final day at Cardiff.
Responding to the visitors' 417 all out Glamorgan amassed 583 for nine declared - giving Glamorgan a first-innings lead of 166.
And Gloucestershire then finished day three on 12 for one with Dean Cosker getting a vital breakthrough bowling Chris Dent with the final ball of the evening session.
Glamorgan were indebted to Wright, who scored the second century of his career. He dominated the fourth-wicket partnership of 157 with Michael Powell, who was only able to contribute 37.
After Wright went, Jim Allenby and veteran Robert Croft shared a record seventh-wicket stand for Glamorgan against Gloucestershire of 153 in 39 overs.
After a 30-minute delay to the third day because of rain Wright built on his fine overnight platform of 77 not out, reaching his second century in 140 balls with 14 fours and a six before overtaking his career-best 108 against Leicestershire in 2007. That was after Glamorgan had resumed the day on 257 for three - 160 behind Gloucestershire.
But he was not content with just a century as he hit Vikram Banerjee and Gemaal Hussain for sixes into the grandstand racing to 150 with the final ball before lunch - his third 50 coming up in only 49 balls.
The Gloucestershire bowlers did not have much to smile about until James Franklin bowled Powell with a ball which kept a little low.
Wright was in sight of becoming Glamorgan's first double-centurion since July 2006, but he edged a lifting Jon Lewis ball behind. His impressive innings came to an end having faced 219 balls with 23 fours and three sixes.
After Mark Wallace went for a duck caught at third slip off Andrew Ireland it was left to Allenby and Croft to take Glamorgan to maximum batting points and then take them past Gloucestershire.
And Croft produced a wonderful cover drive to take Glamorgan into the lead before Allenby secured his sixth 50 in six championship games this season. He reached the landmark from 78 balls including six boundaries.
After going to tea at 478 for six, a lead of 61, Croft went to a 104-ball half-century, and after he was out, Allenby reached his first Glamorgan century from 157 balls with eight fours and a six.
Allenby faced another 10 balls before he holed out to deep backward square off Ireland in the pursuit of quick runs.
Ireland was the pick of the bowlers with five for 114.