ModraUp Ruby API Documentation Ruby API Reference

Class: Layout::Document

Inherits:
Object
  • Object
show all

Overview

A layout document embedded within a model. Each model has one, obtained via ModraUp::Model#layout_document; accessing it after the model is closed throws a TypeError. The layout is saved with the model and does not have a separate .layout file.

Note: ModraUp extension. A LayOut Document corresponds to a .layout file; here it corresponds to the model's separately saved drawing sheet data.

Examples:

doc = ModraUp.active_model.layout_document
doc.pages.length

Class Method Summary # collapse

  • .templates ⇒ Array<Hash>
    Lists system drawing templates and user .osklt files, excluding the built-in blank template. Each entry is a Hash with string keys "name" and "path". System and user templates are each sorted by filename. Use #apply_template to apply a template.

Instance Method Summary # collapse

  • #apply_template(name_or_path) ⇒ Layout::Document
    Apply a template to a document that has no drawings: import the page size, margins, border frames, grids, dynamic text, title block fields, layers, dimension styles, and page objects from the template; subsequently, Layout::Pages#add new pages will use the paper size and objects from the first interior page of the template. Templates without a cover page will use the first page.
  • #pages ⇒ Layout::Pages
    All drawing pages in the order of the drawings.
  • #layers ⇒ Layout::Layers
    The layer list is ordered with the first layer on top. A new document starts with a default layer and a shared layer named "Every Page Display".
  • #path ⇒ String
    The file path of the model's layout document, which is an empty string if the document is not saved.
  • #page_info ⇒ Layout::PageInfo
    The paper size and margins of the drawing sheet; all pages share a single set, applied to every page.
  • #add_entity(entity, layer, page) ⇒ Layout::Document
    Place the newly created object on the drawing sheet, with one-step undo. If no page is given, layer must be a shared layer. If the object extends beyond the sheet edges, it is clipped back within the sheet boundaries. If the object is larger than the sheet, an ArgumentError is raised. After placement, the object's bounds can be retrieved using Layout::Entity#bounds.
  • #remove_entity(entity) ⇒ Layout::Document
    Remove the entity, with one-step undo. When deleting a section or detail view, the section symbol or detail box on the source viewport is also removed, consistent with the interface.
  • #shared_entities ⇒ Layout::Entities
    Objects on shared layers (displayed on every page).
  • #title_block ⇒ Layout::TitleBlock
    Title block common fields (displayed in the title block by the template): project, company, designer, checker, approver, date.
  • #get_attribute(dictionary, key, default = nil) ⇒ Object
    Read document-level plugin data.
  • #set_attribute(dictionary, key, value) ⇒ Object
    Write document-level plugin data with a single undo step. The value can only be nil, true, false, a number, a string, an Array, or a Hash (with Hash keys as strings).
  • #delete_attribute(dictionary) ⇒ Boolean
    Delete a key or an entire dictionary.
  • #export(file_path, options = nil) ⇒ nil
    Exports sheets to PDF using the same process as the Export dialog. Missing or stale model views in the selected range are updated before the file is written. Failure, cancellation, or a model change during export preserves the original file. The call returns immediately. On completion, the block receives ok, indicating whether the file was written, and message, the result shown in the interface. On success the message includes vector and raster view counts. On failure it gives the reason. The block may call export again.
  • #== ⇒ Boolean
    Is the drawing sheet for the same model document.
  • #inspect ⇒ String
    A brief description.
  • #select_tool(tool) ⇒ Layout::Document
    Select the paper tool. tool must be an object (with the same name and parameters) that implements ModraUp::Tool callback methods (ModraUp::Tool.activate, ModraUp::Tool.deactivate, ModraUp::Tool.onMouseMove, ModraUp::Tool.onLButtonDown, ModraUp::Tool.onLButtonUp, ModraUp::Tool.onLButtonDoubleClick, ModraUp::Tool.onRButtonDown, ModraUp::Tool.onRButtonUp, ModraUp::Tool.onKeyDown, ModraUp::Tool.onKeyUp, ModraUp::Tool.onReturn, ModraUp::Tool.onCancel, ModraUp::Tool.onUserText, ModraUp::Tool.enableVCB?, ModraUp::Tool.draw). The view parameter in the callback is a Layout::PaperView, and x and y are logical pixels within the paper area, which can be converted to paper coordinates using Layout::PaperView#paper_point. Entering this tool will take you to the drawing workspace; passing nil will return to the document selection tool.
  • #tool ⇒ Object, nil
    The currently selected viewport tool; nil if none is selected.
  • #add_observer(observer) ⇒ Boolean
    Add a drawing observer. Observers need only implement the methods they are interested in, with the first parameter always being the document:
  • #remove_observer(observer) ⇒ Boolean
    Remove the document observer.
  • #selection ⇒ Array<Layout::Entity>
    The current selection in the drawing workspace consists of the primary paper objects. If the document is not the current document being displayed in the workspace, the selection is an empty array.
  • #selection= ⇒ Layout::Entity, Array<Layout::Entity>, nil
    Set the sheet selection. Accept an object, array of objects, or nil (to clear). The objects must be on the same drawing sheet (objects on shared layers are considered on each sheet); if not on the current sheet, switch to that sheet. This does not affect the undo history.
  • #active_page ⇒ Layout::Page, nil
    The drawing sheet currently displayed in the Layout::Document active_page. Returns nil if the document is not the active page or there is no drawing sheet.
  • #active_page= ⇒ Layout::Page
    Displays a different sheet in the drawing workspace and clears the paper-space selection. This change does not add an undo step.

Class Method Details

Layout::Document.templates ⇒ Array<Hash>

Lists system drawing templates and user .osklt files, excluding the built-in blank template. Each entry is a Hash with string keys "name" and "path". System and user templates are each sorted by filename. Use #apply_template to apply a template.

Returns:

  • (Array<Hash>)

Raises:

  • [NotImplementedError] No drawing host when
Note: ModraUp extension. LayOut selects the template when creating a document with Document.new(template_path); here, the document already exists with the model.

Examples:

Layout::Document.templates.map { |t| t["name"] }

Instance Method Details

document.apply_template(name_or_path) ⇒ document

Apply a template to a document that has no drawings: import the page size, margins, border frames, grids, dynamic text, title block fields, layers, dimension styles, and page objects from the template; subsequently, Layout::Pages#add new pages will use the paper size and objects from the first interior page of the template. Templates without a cover page will use the first page.

These settings are overall taken from the template: any layers, grids, and title block fields added to the document are replaced, except for their extended attributes (#set_attribute). One-step undo.

Parameters:

  • name_or_path (String) List the names or the .osklt file paths. Prioritize user templates with the same name as system templates by name.

Returns:

  • (Layout::Document) self

Raises:

  • [ArgumentError] The document already has a drawing sheet, or the template was not found or is invalid.
Note: ModraUp extension. Corresponds to LayOut Layout::Document.new({template_path}).

Examples:

doc = ModraUp.active_model.layout_document
doc.apply_template("A3 Architectural") if doc.pages.length == 0
doc.pages.add("Plan")

document.pages ⇒ Layout::Pages

All drawing pages in the order of the drawings.

Returns:

  • (Layout::Pages)

Examples:

ModraUp.active_model.layout_document.pages.each { |page| puts page.name }

document.layers ⇒ Layout::Layers

The layer list is ordered with the first layer on top. A new document starts with a default layer and a shared layer named "Every Page Display".

Returns:

  • (Layout::Layers)

Examples:

ModraUp.active_model.layout_document.layers.map(&:name)

document.path ⇒ String

The file path of the model's layout document, which is an empty string if the document is not saved.

Returns:

  • (String)

Examples:

ModraUp.active_model.layout_document.path

document.page_info ⇒ Layout::PageInfo

The paper size and margins of the drawing sheet; all pages share a single set, applied to every page.

Returns:

  • (Layout::PageInfo)

Raises:

  • [RuntimeError] Reading when the document has no pages.

Examples:

A3 Landscape

info = ModraUp.active_model.layout_document.page_info
info.width = 420.mm
info.height = 297.mm

document.add_entity(entity, layer, page) ⇒ Layout::Documentdocument.add_entity(entity, shared_layer) ⇒ Layout::Document

Place the newly created object on the drawing sheet, with one-step undo. If no page is given, layer must be a shared layer. If the object extends beyond the sheet edges, it is clipped back within the sheet boundaries. If the object is larger than the sheet, an ArgumentError is raised. After placement, the object's bounds can be retrieved using Layout::Entity#bounds.

Parameters:

  • entity (Layout::Entity) Objects not yet added to the document.
  • layer (Layout::Layer)
  • page (Layout::Page, nil)

Returns:

  • (Layout::Document) self

Raises:

  • [ArgumentError] The object is already in the document, larger than the sheet, or the layer is not a shared layer when added without specifying a page.
  • [Layout::LockedLayerError] Layer is locked or hidden on this page
  • [RuntimeError] The data for a single object exceeds 1 MB

Examples:

doc = ModraUp.active_model.layout_document
page = doc.pages.add
rect = Layout::Rectangle.new(Geom::Bounds2d.new(10.mm, 10.mm, 50.mm, 30.mm))
doc.add_entity(rect, doc.layers.active, page)

document.remove_entity(entity) ⇒ Layout::Document

Remove the entity, with one-step undo. When deleting a section or detail view, the section symbol or detail box on the source viewport is also removed, consistent with the interface.

Parameters:

  • entity (Layout::Entity)

Returns:

  • (Layout::Document) self

Raises:

  • [Layout::LockedLayerError] The layer containing the object is locked or hidden.

Examples:

doc = ModraUp.active_model.layout_document
page = doc.pages.add
rect = Layout::Rectangle.new(Geom::Bounds2d.new(10.mm, 10.mm, 50.mm, 30.mm))
doc.add_entity(rect, doc.layers.active, page)
doc.remove_entity(rect)

document.shared_entities ⇒ Layout::Entities

Objects on shared layers (displayed on every page).

Returns:

  • (Layout::Entities)

Examples:

ModraUp.active_model.layout_document.shared_entities.length

document.title_block ⇒ Layout::TitleBlock

Title block common fields (displayed in the title block by the template): project, company, designer, checker, approver, date.

Returns:

  • (Layout::TitleBlock)
Note: ModraUp extension.

Examples:

ModraUp.active_model.layout_document.title_block["project"] = "Example residence"

document.get_attribute(dictionary, key, default = nil) ⇒ Object

Read document-level plugin data.

Parameters:

  • dictionary (String)
  • key (String)
  • default (Object) The value to return when the item is missing

Returns:

  • (Object)

Examples:

ModraUp.active_model.layout_document.get_attribute("my_plugin", "run", 0)

document.set_attribute(dictionary, key, value) ⇒ Object

Write document-level plugin data with a single undo step. The value can only be nil, true, false, a number, a string, an Array, or a Hash (with Hash keys as strings).

Parameters:

  • dictionary (String)
  • key (String)
  • value (Object)

Returns:

  • (Object) value

Raises:

  • [ArgumentError] Value cannot be serialized as JSON
Note: ModraUp extension. LayOut supports additional value types. ModraUp stores values that JSON can represent.

Examples:

ModraUp.active_model.layout_document.set_attribute("my_plugin", "run", 1)

document.delete_attribute(dictionary) ⇒ Booleandocument.delete_attribute(dictionary, key) ⇒ Boolean

Delete a key or an entire dictionary.

Parameters:

  • dictionary (String)
  • key (String)

Returns:

  • (Boolean) Did the content delete?

Examples:

ModraUp.active_model.layout_document.delete_attribute("my_plugin")

document.export(file_path, options = nil) ⇒ nildocument.export(file_path, options = nil) { |ok, message| ... } ⇒ nil

Exports sheets to PDF using the same process as the Export dialog. Missing or stale model views in the selected range are updated before the file is written. Failure, cancellation, or a model change during export preserves the original file. The call returns immediately. On completion, the block receives ok, indicating whether the file was written, and message, the result shown in the interface. On success the message includes vector and raster view counts. On failure it gives the reason. The block may call export again.

Option keys may be strings or symbols. start_page and end_page select a 1-based inclusive range, defaulting to all sheets. update_views defaults to true. When false, the exporter reuses previews, except that it still renders views with size or camera mismatches. prefer_vector defaults to true and prioritizes vector lines and annotations. The exporter accepts output_resolution, compress_images, and compress_quality without applying them. update_views and prefer_vector are ModraUp extensions.

Parameters:

  • file_path (String) Ends with .pdf
  • options (Hash, nil)

Returns:

  • (nil)

Yields:

  • @yieldparam [Boolean] ok whether the file was written
  • @yieldparam [String] message Export result

Raises:

  • [ArgumentError] not a .pdf, page index out of bounds, document has no drawings or unknown option name
  • [RuntimeError] Not a active model's drawing, or an export is already in progress.
Note: ModraUp extension. LayOut's export runs synchronously and supports PNG and other formats. ModraUp exports PDF only.

Examples:

Export all drawings

doc = ModraUp.active_model.layout_document
doc.export(File.join(Dir.tmpdir, "drawings.pdf")) do |ok, message|
  puts ok ? "Exported" : "Export failed: #{message}"
end

Export only the 2nd drawing sheet.

doc = ModraUp.active_model.layout_document
doc.export(File.join(Dir.tmpdir, "sheet2.pdf"), { "start_page" => 2, "end_page" => 2 }) { |ok, _| p ok }

document == other ⇒ Boolean

Is the drawing sheet for the same model document.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Examples:

m = ModraUp.active_model
m.layout_document == m.layout_document  #=> true

document.inspect ⇒ String

A brief description.

Returns:

  • (String)

Examples:

ModraUp.active_model.layout_document.inspect

document.select_tool(tool) ⇒ Layout::Document

Select the paper tool. tool must be an object (with the same name and parameters) that implements ModraUp::Tool callback methods (ModraUp::Tool.activate, ModraUp::Tool.deactivate, ModraUp::Tool.onMouseMove, ModraUp::Tool.onLButtonDown, ModraUp::Tool.onLButtonUp, ModraUp::Tool.onLButtonDoubleClick, ModraUp::Tool.onRButtonDown, ModraUp::Tool.onRButtonUp, ModraUp::Tool.onKeyDown, ModraUp::Tool.onKeyUp, ModraUp::Tool.onReturn, ModraUp::Tool.onCancel, ModraUp::Tool.onUserText, ModraUp::Tool.enableVCB?, ModraUp::Tool.draw). The view parameter in the callback is a Layout::PaperView, and x and y are logical pixels within the paper area, which can be converted to paper coordinates using Layout::PaperView#paper_point. Entering this tool will take you to the drawing workspace; passing nil will return to the document selection tool.

The paper tool does not affect 3D tools in the model. The deactivate method is called when space is pressed, the built-in drawing tool is selected, the model is selected, or the document is switched. Middle mouse drag and scroll wheel zoom are still handled by the paper tool.

Parameters:

  • tool (Object, nil)

Returns:

  • (Layout::Document)

Raises:

  • [RuntimeError] The document is not for the active model, or there are unfinished edits in the viewport.
Note: ModraUp extension. LayOut's Ruby API does not have a tool.

Examples:

Click the viewport to print the paper coordinates at the click location.

class PickTool
  def onLButtonDown(flags, x, y, view)
    pt = view.paper_point(x, y, true)
    entity = view.pick_entity(x, y)
    puts "#{pt.x.to_mm.round(1)}, #{pt.y.to_mm.round(1)} mm: #{entity.class}"
  end
  def draw(view)
    view.drawing_color = "red"
    view.line_width = 2
    view.draw(GL_LINE_LOOP, [[10.mm, 10.mm], [60.mm, 10.mm], [60.mm, 40.mm], [10.mm, 40.mm]])
  end
end
ModraUp.active_model.layout_document.select_tool(PickTool.new)

document.tool ⇒ Object or nil

The currently selected viewport tool; nil if none is selected.

Returns:

  • (Object, nil)
Note: ModraUp extension.

Examples:

ModraUp.active_model.layout_document.tool

document.add_observer(observer) ⇒ Boolean

Add a drawing observer. Observers need only implement the methods they are interested in, with the first parameter always being the document:

- onDocumentChanged(document): The drawing definition is modified, including UI edits, Ruby writes, undo, and redo. - onSelectionChanged(document): The sheet selection changes, see #selection. - onActivePageChanged(document, page): The current drawing changes, page may be nil. - onWorkspaceChanged(document, active): Entering (true) or leaving (false) the drawing workspace. - onViewsUpdated(document): A batch of model views are updated in the background (excluding exports). - onExportComplete(document, path, ok, message): The PDF export is complete, including UI-initiated exports.

Notifications are delivered in the event loop at a later time, and multiple consecutive changes may be merged into one. Only the document currently displayed in the drawing workspace (the active model) receives notifications; switching documents does not send notifications. Observers are automatically removed when the model is closed.

Parameters:

  • observer (Object)

Returns:

  • (Boolean) Returns false if a new observer is not added (returns false if the same object is added again).
Note: ModraUp extension. LayOut's Ruby API does not support observers.

Examples:

class MyDrawingObserver
  def onDocumentChanged(doc)
    puts "Drawing contains #{doc.pages.length} pages"
  end
  def onExportComplete(doc, path, ok, message)
    puts ok ? "Exported #{path}" : message
  end
end
ModraUp.active_model.layout_document.add_observer(MyDrawingObserver.new)

document.remove_observer(observer) ⇒ Boolean

Remove the document observer.

Parameters:

  • observer (Object)

Returns:

  • (Boolean) Whether removal succeeded
Note: ModraUp extension.

Examples:

doc = ModraUp.active_model.layout_document
observer = Object.new
doc.add_observer(observer)
doc.remove_observer(observer)

document.selection ⇒ Array<Layout::Entity>

The current selection in the drawing workspace consists of the primary paper objects. If the document is not the current document being displayed in the workspace, the selection is an empty array.

Returns:

  • (Array<Layout::Entity>)
Note: ModraUp extension.

Examples:

ModraUp.active_model.layout_document.selection.grep(Layout::SketchUpModel)

document.selection = entities

Set the sheet selection. Accept an object, array of objects, or nil (to clear). The objects must be on the same drawing sheet (objects on shared layers are considered on each sheet); if not on the current sheet, switch to that sheet. This does not affect the undo history.

Parameters:

  • entities (Layout::Entity, Array<Layout::Entity>, nil)

Returns:

  • (Layout::Entity, Array<Layout::Entity>, nil)

Raises:

  • [ArgumentError] The objects are not on the same drawing sheet, or belong to another document.
  • [Layout::LockedLayerError] The object is on a locked or hidden layer in the document.
  • [RuntimeError] The document is not for the active model, or there are unfinished edits in the viewport.
Note: ModraUp extension.

Examples:

Select the newly generated dimension.

doc = ModraUp.active_model.layout_document
page = doc.active_page
doc.selection = page.entities.grep(Layout::LinearDimension) if page

document.active_page ⇒ Layout::Page or nil

The drawing sheet currently displayed in the Layout::Document active_page. Returns nil if the document is not the active page or there is no drawing sheet.

Returns:

  • (Layout::Page, nil)
Note: ModraUp extension.

Examples:

page = ModraUp.active_model.layout_document.active_page
puts page.name if page

document.active_page = page

Displays a different sheet in the drawing workspace and clears the paper-space selection. This change does not add an undo step.

Parameters:

  • page (Layout::Page)

Returns:

  • (Layout::Page)

Raises:

  • [RuntimeError] The document is not for the active model, or there are unfinished edits in the viewport.
Note: ModraUp extension.

Examples:

doc = ModraUp.active_model.layout_document
doc.active_page = doc.pages[0]