Creating multiple groups from multiple surfaces

Good evening All,

I have a drawing with approx 2,000 small surfaces which I want to make in groups to be push pulled afterwards. The script below is working very well when the drawing is fresh after the surfaces have been created. When I make a second or third selection of only surfaces it run still fast, but when I make a selection and there is one previously created group inside the script runs only at half or even less of the speed and the script to become “not responding” but eventually it finishes after a while in the background. Even when I re-start sketchup or even the pc and re-run of the script it remains slow. When I explode all groups and make another selection the script is fast again. Perhaps somebody can tell me if I do something wrong. My laptop has 16 GB memory and the graphic card is and set to Nvidia

the “puts” command in the middle might slow the script down but I put it there to see the speed

Thanks in advance for your comments

model=Sketchup.active_model
active=model.active_entities
c = 0
model.selection.grep(Sketchup::Face).each{|f|a.add_group(f);puts "#{c}";c+= 1}
model.commit_operation
puts "Made #{c} face groups"

The smart quotes will cause issues when copying your code, so …
[How to] Post correctly formatted and colorized code on the forum?

Also please follow good coding style. (Put spaces before and after operators and properly indent code.)

You also commit an undo operation, but never start one.

Thanks for you comment, I took the time to read the procedures and hope that the correction is ok. The code works but slowing down of the script after 2-3 selections of processing the groups. (up to 30min to process 200 surfaces) Any reason for that in the coding or is there a way to cleanup the memory of the dwg?20.12.18 Formation level + progress 20.12.10 - 8.skp (15.4 MB)