Help Developing an extension

Good day everyone. I am currently working on an extension that allows you to procedurally generate groups/components and need a little help. I got the generation part to work (works great) but I only can generate things on flat faces. I want to be able to generate stuff on curved faces like terrain. Below I have attached images of what the code does, can someone help please? Thanks.

Heres what the code does for now after launching the script:

I also added a line of code that allows the user to change the number of groups generated (in this case 100 groups)

You would first need a surface to sample. You would then define a point on the surface and calculate the normal vector at that point. Finally, place the group/ component using a transform that related to that position and normal.