How to join pieces automatically using interlocking or similar? I have a piece, which I need to cut into two different pieces, see image 1.

Image 1



Then I need to make interlocking joints or similar, see image 2. 

Image 2



How can I do that automatically, using Solidworks or another program?

It's an *.STL file so I can easily manipulate all parts without any errors.
I'm not qualified to advise regarding Solidworks, but can suggest a process and concept from which you may be able to adapt to your software.

As you have the two pieces currently in STL form, it makes things only slightly more difficult. Consider to align them parametrically so the faces contact and the edges are coincidental. The block STL will appear as a single cube, in the example you've provided.

Create and place a mating cylinder piece in the desired position as you've shown in the second image. It is a common feature of many 3D modeling programs to be able to perform a subtraction of one part from another. It is equally common to be able to merge two parts into a single manifold part.

Save the file containing the three parts.

Select either block and the cylinder. You can delete or otherwise hide or deselect the unused block. Perform a subtraction on the remaining pair. Save this file under a different name.

Reload the three parts file.

Select the other block and cylinder, perform a union of the pair. Save this file under a different name.

Rather than saving the three parts in a file, one can frequently save the joined or subtracted part and perform an undo until the three parts return. This is a faster more convenient method.

I have performed the above steps in Meshmixer and know that it can be done in Blender, if one is skilled in these programs. 

I've performed an internet search for "perform boolean subtraction solidworks" and found multiple results for this process. One link references the "cavity" feature for subtraction. I did not search for boolean union and leave this as an exercise for the reader.

Consider also to plan for this activity in the future by leaving the block intact, unsliced, place the cylinder, perform the boolean action, then slice it as desired. The sequence of saves and/or undo actions will be different from above, but may be easier than re-aligning a previously sliced "block" or item.