How to clamp parts of any size to a grid

Currently having problems with my grid building system where unusually shaped parts are not properly being clamped to the grid meaning that sometimes half the part is going out of bounds, here is my code for clamping:

Halves['HalfX'] = Grid.Size.X / 2 
Halves['HalfZ'] = Grid.Size.Z / 2
Halves['HalfY'] = Grid.Size.Y / 2

Clamps['XMin'] = Grid.Position.X - Halves['HalfX'] + ObjectPositioningData['HalfX']
Clamps['XMax'] = Grid.Position.X + Halves['HalfX'] - ObjectPositioningData['HalfX']
Clamps['ZMin'] = Grid.Position.Z - Halves['HalfZ'] + ObjectPositioningData['HalfZ']
Clamps['ZMax'] = Grid.Position.Z + Halves['HalfZ'] - ObjectPositioningData['HalfZ']

Where the grid is the part you are placing on
and object position data contains half the size of the part you are trying to place, for example

If a part is (6,4,2) then
ObjectPositioningData[‘HalfX’] will be 3
ObjectPositioningData[‘HalfZ’] will be 1

Any help will be appreciated, tearing my hair out over this