I wrote a script that creates sheets from an excel file. I found out that this is a pretty complicated task. I ran into the same problem that you describe and I chose to solve it by first creating and naming every necessary view first, and then using the Out-of-the-Box nodes (Sheet.ByNameNumberTitleBlockAndView, Sheet.ByNameNumberTitleBlockAndViews) to place them on the sheet.
In my case, any sheets that contain multiple views must be created by this node:
So, I had to separate sheets with only a single view and send that data to this node:
After the views are placed, the script then turns off all annotation categories in all the views, then moves all views to the origin. You must turn off all annotations because the viewports are placed by the bottom left of the bounding box of the viewport. The bounding box is drawn around any annotations that extend outside the view crop region, so any 2d elements that stick out of the view crop will affect the placement of the viewport. (If you don’t have any annotations in the view, then I suppose you don’t need to worry about this)
After annotation categories are hidden, I use Element.MoveByVector to move the viewport into the correct location on the sheet. When the viewports are placed correctly, I then show all annotations again. My script does not automatically center the viewport on sheet like you are trying to do, but it might help you get closer to your goal.
If you want to center the views, I would suggest that you find the viewport BoxCenter of all viewports after they are placed on the sheet. Are you trying to center a single viewport per sheet? If so, that would be much simpler. You could find the viewport box center like this:
Then just set the box center to be equal to the titleblock center.
If you are trying to center multiple viewports, you could find out which min point is the closest to the origin, and which max point is farthest. This would give you a bounding box around the group of viewports, from which you could find the center of the group. Then you could find the center of the titleblock, and move all views by the vector from the group center to the titleblock center.
This is just a rough outline of how you could solve the problem. I expect you will need some clarification to understand what I mean, so feel free to ask. Here is my sheet creation script if you want to inspect closer: SHEET CREATION_R2020 v5.dyn (386.3 KB)