Layout API "Hello World"?

Hello All. Could anyone direct me to a Layout API example that explains basic ruby script workflow? I am familiar with Sketchup API but just having trouble getting started. For instance, there does not appear to be a “Plugins” folder for Layout where I could place my scripts and start testing? Where should I put my scripts or how can I tell Layout where to look for them?

Thanks!

Because currently the LayOut API is non-live. This means it must run within SketchUp’s Ruby process.

So extension(s) that modify or create .layout files are (currently) SketchUp extensions.

Since there are no “live” LayOut application extensions, there can be no interface that displays messages to users such as “Hello World”.

1 Like

As Dan says the LayOut Ruby API is only available inside of SketchUp. The most trivial Hello word that just writes to the console would essentially be identical toa SketchUp Hello World.

A slightly more complex LayOut Hello World that creates a LO document and lets you save it out could look something like this.

leader_type = Layout::Label::LEADER_LINE_TYPE_BEZIER
target_point = Geom::Point2d.new(1, 1)
bounds = Geom::Bounds2d.new(2, 1, 1, 1)
label = Layout::Label.new("Hello World", leader_type, target_point, bounds)
doc = Layout::Document.new
doc.add_entity(label, doc.layers.first, doc.pages.first)
doc.save(UI.savepanel("Save Document", "", "test.layout"))

OK weird. Are there plans for for Layout API to go “live” or whatever where you could write scripts that worked without Sketchup?

And so I may be in “workaround” territory it sounds. OK, fine. Here is what I would like to do:

When my firm uses Layout to annotate drawings, there are probably 50 typical annotations we use I would like to create some sort of UI in Layout that could cycle through them.

I suppose I could l use the Label Tool’s ability to draw element data like Face Area into Layout. Seems super hacky so any thoughts?

We “suspect” so. But it hasn’t happened yet. Trimble’s SketchUp division does not publish roadmaps or future plans.

But because the LayOut APIs are currently file APIs that read and write .layout files only, you cannot author extensions that run within LayOut or modify it’s interface.

There is another topic thread around here about this very thing. Currently (despite requests) the LayOut Label tool is hard-coded to only access certain Attribute Dictionaries.

So you can use the SketchUp API to add attributes to the "dynamic_attributes" dictionary, and they’ll be available in LayOut to it’s Label tool. The other special dictionaries ("SU_DefinitionSet" and "SU_InstanceSet") are hard-coded to only access certain attributes, so any custom attributes will be ignored.

LayOut also has AutoText that can be added to your firm’s template.

Another option may be to bring in notes via tables linked to external spreadsheet files.

However, any UI that chooses these options would need to be done in a SketchUp extension, usually just prior to the LayOut document creation.

1 Like

Thanks Dan. OK, perhaps it’s not going to happen. What I envisioned was a text box/drop-down where you start typing and the text is matched a dataset. I think that would require something like the HtmlDialog in SU and could not be done through a something as blunt as a label hack. Spreadsheets are what we use now and it’s not the greatest because you have to cycle through so much to find what you need. Oh well! Thanks again.

Hi @eneroth3 Just re-visiting this thread.

Trying to understand… would the code you provided be run in a typical Sketchup Ruby Script like hellow_world.rb?

Also, it seems like doc.save(UI.savepanel... is acting on a Layout window panel. So in other words I’d have to have Sketchup AND Layout open when this script is running?

Thanks!

I don’t think so.
The Module:UI is for Sketchup only. Therefor The savepanel method is used to display the Save dialog box. The path that is returned can then be used inside code to save out a e.g. a layout file.

However you can open a layout file for manipulation or get information’s using the Layout API (Module: Layout)
E.g.:
A Document is the 2D drawing that the user is working with, and it serves as the “entry point” for most Ruby API interactions. The Document.open method gives you a handle to a Document, and from there you can use the document-level methods to start getting information and making changes.

The #save method saves the Layout::Document to a file at the given path. Passing an empty path string will save the Layout::Document at its current path. Or using the above mentioned savepanel method you can get a path form user interaction.

So overall, the Layout API is for manipulation or get information’s of the Layout document, but your extension is still “in SketchUp”.
The Layout program itself not necessarily need to be run at all. (I guess even it will “disturb” your script in most cases.)


Edit
More thought…
For example you can not create an interactive tool to mimic the Rectangle tool imagein layout. But you can add a rectangle to the layout document by code. ( see example here: #add_entity method )

1 Like

Aha @dezmo! Thanks that makes much more sense. I did not understand the relationship between the applications.

So presumably, I can go into the Sketchup Ruby Console and type something like…

doc = Layout::Document.open("/Users/hank/Desktop/test.layout")

And be able to get the number of pages in the a test.layout file on my Desktop with…

doc.pages.length

and so forth?

(BTW I failed the above, probably because I don’t understand the path system on Mac very well.)

Me too… :blush:

Why don’t you brose in: :wink:

doc = Layout::Document.open(UI.openpanel("Open Layout File", "", "*Layoutyout Files|*.layout;||"))
doc.pages.length

Ok, thanks. I tried that and literally selected the friggin file and still got…

Error: #<ArgumentError: File does not exist>

weird right? Maybe you CAN’T just use the Sketchup Ruby Console?

olf

I know… its strange!

Sep-15-2022 13-23-42

Strange. I just tried it on my Mac and got a result same as what @dezmo showed. Note that so far as I know, the file type patterns don’t work on Mac SketchUp so you have to navigate to where the file lives using the Finder-like interface that UI.openpanel pops up.

2 Likes

I even restarted and still the same thing.

Try something like:

dir = File.expand_path("~/Desktop")
path = UI.openpanel("Open Layout File", dir, "*.layout")
puts "#{path.inspect} exists? #{File.exist?(path)}"
begin
  doc = Layout::Document.open(path)
rescue => err
  puts "Error opening LayOut doc: #{path.inspect}"
  puts err.inspect
else
  puts "Pages in doc: #{doc.pages.length}"
end

The ~ at the beginning of a Mac path expands to ENV["HOME"] (which is the user’s home directory.)

same.

Sep-15-2022 19-57-06

It is very difficult to pause the video to see the output.
Generally, please post output not videos or screen shots in the forum.

What version of SketchUp are you seeing this with?
What version was the "test.layout" file made with?

Perhaps post the test file here so a Mac guru can try it on their machine?

Oh, and whenever things act goofy, I suggest rebooting the computer to clear memory and test again.

Also you might try someone else’s file as a test. (Perhaps that file is corrupted?)

I tested now on Windows. If the layout file version is newer than the SU version I get the same error.
Eg. if the layout file created in version 2021 of Layout and you are running a script in SU 2020, the Layout::Document.open(path) will raises an #<ArgumentError: File does not exist>

(That might be reported to GitHub… because the documentation not mentioning it… or need to be raise an other error like “Wrong file version” or so.)

2 Likes

I agree. It could be a Layout specific exception (as this API has Layout::LockedEntityError and Layout::LockedLayerError defined.) It could be Layout::Document::VersionError or Layout::Document::ReadError for example. (At the least, a Ruby IOError.)

However, the .layout file is really a zip archive wrapping up XML files. The XML specification requires file readers to ignore elements and attributes it does not know how to handle, and read the rest of the file. So really '.layout` files should be more version agnostic than this issue shows.

Either way, … it deserves a tracker report if one has not yet been opened.

2 Likes