Not sure if cutting your legs will not in fact harm your unborn baby. It has been proven however that your mental state alone has affect, so it would make sense that physical pain of the mother would indeed cause harm as well. I do honor you for at least trying not to cause the baby any harm.
I haven't ever spoken with any "professionals" about self harm, but from what I know, like all actions we continue, it meets our needs on some level. It is a way of dealing with mental pain because the act alone takes the focus off the "depression", but so would playing 'ping-pong' or anything that gets the body moving differently and directs our focus elsewhere.
Mainly it is likely motivated by a feeling of loss of power, which is really a loss of significance, perhaps intensified by a feeling of loss or lack of love/connection and certainty. So it would seem to meet these three needs. By consciously harming ourselves we gain a sense of power and connection with self.
It is easy for any person to say, "don't do that", but they fail to understand this act 'feels', as both of you have mentioned, to help deal with pain. Of course I would suggest otherwise, but I can understand why you do it. My advice would be try and consider another way to meet these needs. For instance, your situation, that is, about to have a child. This will by default add a sense of significance, love and connection, certainty, and variety, unlike probably any other outlet. It will meet all primal needs by default. You will have the opportunity to shape someone, and give to them all the things you may not have had, or improve on what you did have.
Please understand I am not judging you, I know everything a person does is only an attempt to 'protect' themselves on some level, even if the action is not acceptable. Also, I have dealt with some who practice this act, and I can say with total certainty, they are some of the most caring and loving people you could possibly have the fortune to meet. Completely unlike those who harm someone else to meet a need, none I have known would ever consider such an action, it violates their rules. I would just emphasize you are just as important as anyone around you. So in this case it might be acceptable to say, "Do unto your self, as you would do unto others!"
I wish you the very best.