Extension Ruby help -> send to layout

I have recently dabbled rather successfully with creating a very specific extension for our company. It streamlines a whole bunch of steps that we usually need to take to create specific drawing details. I managed to create it with the help of Antigravity + Gemini / Claude. It’s been an amazing journey. Now I’ve run against a wall with a specific feature which seemed easy at first but it’s simply not working.

I have created a folder with Layout-Files
In the extension I point to that folder in the setting section
The extension reads the folder, finds all the Layout-files and builds a “send to ____” button per file.
When you click on the button it is supposed to check if the sketchup file is saved and then “send it to layout” → meaning the specific layout file the button was created for. So if I have DIN A1 sheet with our company logo and stuff all set up correctly there will be a button hat creates a new layout file based on the template and adds the current sketchup file / scene to it.

I know this is possible. :slight_smile:

But I keep getting versions of this error:

Here is the summarized code I am trying to use.

# 1. Open the template file
doc  = Layout::Document.open("/path/to/template.layout")
page = doc.pages.first

# 2. Define the viewport bounds (paper size minus 1-inch margin)
info   = doc.page_info
margin = 1.0
bounds = Geom::Bounds2d.new(margin, margin,
                             info.width  - 2.0 * margin,
                             info.height - 2.0 * margin)

# 3. Create the SketchUp model viewport
viewport = Layout::SketchUpModel.new("/path/to/model.skp", bounds)

# 4. Set the scene (1-based index into model.pages)
viewport.current_scene = 3  # e.g. third scene tab

# 5. Resolve a layer to place the entity on
#    page.layer_instances.first is nil for some documents → fallback
target_layer = page.layer_instances.first || doc.layers.first

# 6. Add viewport to the document, save, and open
doc.add_entity(viewport, target_layer, page)
doc.save("/path/to/output.layout")
UI.openURL("file:///path/to/output.layout")

Any pointers or (code) examples of how I could get this to work?

1 Like

If you are using a template file to base a new document upon, then you use Layout::Document::new not Layout::Document::open.

Otherwise you will be modifying the template file rather than a new file.

Also, the template file could have it’s file locked if you did this previously, which could be resulting in the “file does not exist” error.

  • Aside: I had reported that the Layout API does not have a close method for the Layout::Document class, therefore once you open a document file, it will remain open and locked for the session. (Hopefully, the file handle will be released when SketchUp is closed.)

In other news …

You can use Ruby core File class method to test if the user has permissions for your template folder path.

1 Like

Hi Dan - thank you so much for your input. I have changed the code to

doc = Layout::Document.new(template_path)

and cleaned up the code the best I can. I actually managed to get this to work now with SOME Layout files and not with others.

VERY interesting. So I did some testing. I opened the working Layout file, modded it a bit and changed saved it again (save as template - not sure if this makes a difference but better safe then sorry…)

Tested. All good. Modded a bit further. saved and tested. All good. Took the content of the layout file that DOESN’T work and copy pasted into the layout file that was working. Tested - ERROR! :slight_smile:

So there is something in that layout file that Sketchup doesn’t like. Mhhhh… Next try - copied element for element from one layout file to the other until I found the “bad boy” - it was the linked Excel Sheet.

Very, very interesting. Where does that leave us? :slight_smile: Is a layout template file not allowed to contain linked elements?

I might need to give up the “send to layout” idea. I was a bit to enthusiastic - or rather - not clear enough with my intentions to myself. I now realize what I want to do is to immitate my current workflow. We have layout files that we copy to a project. They are set up very deliberately . We open them, go to reference and update the reference of the Sketchup file with that of the current project and the reference of the Excel-sheet with that of the current project. This sets up everything rather nicely. Not sure if this can actually be achieved via Ruby script.

Unfortunately, the update method for a model viewport is still missing from the API, as well as the method to import external table files.

From the official API Issue Tracker at GitHub:

Ok - got it. Well - fingers crossed that the API will be expanded at one point or the other! :slight_smile: Crazier things have happened! I’ll use what I have now for a “simple” callout to Layout and shelf my idea of sending it of to our more complex files for now.

Thank you so much. I would have chased that rabbit for a long, fruitless time!

I never have issues when using more than one dialog object using the same template file, I only run into issues when the template file is still open in LayOut. Closing the template tab will release it, you don’t have to close the whole instance.

Jack, that was a brain fart on my part. (Heh, a rhyme.) I really meant …

… because this is LayOut API use from within SketchUp. (I’ve corrected the original post.)

Ohh man, thanks for this details (Dan and Napperkt) I have a similar proyect in my company that I’ve been trying to solve. Exporting the scenes into layout and putting X scenes in Y page, and not having to do it manually, and wasn’t being able to do it.

Now i can export correctly and it puts the scenes I want in the page I want! Now trying to get it to put the correct style in hidden line (in sketchup) or vectorial (in layout) havent decide what is better yet.

Again thanks a lot, been trying to do it for the past 3/4 months by my self.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.