As another noted – a counselor or medical professional can assist one on the disorder your struggling with (as you likely know -we note this for other readers too --one site I saw was catholictherapists) We are not permitted to give medical advice here.
Morally speaking - all sorts of thoughts can come out of the blue to anyone.
…and there not be any sin.
When such happens one would not consent but calmly turn to some good thing- like the work one is doing etc (as opposed to trying hard not to think the thing…which can be counterproductive).
A person with OCD is going to have a more intense experience with such thoughts (to say the least) and as noted can obtain professional assistance with such. A Priest /confessor can assist - not only with his direction but with advising the person with the OCD regarding what the medical professional proposes to assist them with (so they know what is moral in treating such).