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?
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.
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 .layoutfiles 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.
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.
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?
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 in layout. But you can add a rectangle to the layout document by code. ( see example here: #add_entity method )
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.
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.)
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.