ModraUp Ruby API Documentation Ruby API Reference

Class: Layout::SketchUpModel

Inherits:
Layout::Entity show all

Overview

Model Viewport: Displays a scene, or a single component or group (independent viewport). Parallel projection viewports have a fixed scale and can project model points, associated dimensions, sections, and details. When the drawing workspace is open, new and modified viewports are automatically plotted in the background.

Note: ModraUp extension. The source must be from this model's scene or instance; it cannot reference an external .skp file. There is no synchronized rendering; attempting to use #render will raise a NotImplementedError.

Examples:

A scene viewport at 1:100 in line-drawing mode

doc = ModraUp.active_model.layout_document
scene = ModraUp.active_model.pages.add("Plan")
plan = Layout::SketchUpModel.new(scene, Geom::Bounds2d.new(20.mm, 20.mm, 250.mm, 180.mm))
plan.scale = 1.0 / 100
plan.render_mode = Layout::SketchUpModel::VECTOR_RENDER
doc.add_entity(plan, doc.layers.active, doc.pages.add)

Constants # collapse

Layout::SketchUpModel::RASTER_RENDER

Render Style: Bitmap.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::RASTER_RENDER
Layout::SketchUpModel::HYBRID_RENDER

Render Mode: Hybrid (vector lines on a colored background).

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::HYBRID_RENDER
Layout::SketchUpModel::VECTOR_RENDER

Render Style: Line Drawing (Black and White Vector).

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::VECTOR_RENDER
Layout::SketchUpModel::CUSTOM_VIEW

Custom camera (not a standard view).

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::CUSTOM_VIEW
Layout::SketchUpModel::TOP_VIEW

Standard View: Top View.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::TOP_VIEW
Layout::SketchUpModel::TOP_RELATIVE_VIEW

The relative top view in LayOut.

Returns:

  • (Integer)
Note: ModraUp extension. Does not support this; an ArgumentError is thrown when setting it.

Examples:

Layout::SketchUpModel::TOP_RELATIVE_VIEW
Layout::SketchUpModel::BOTTOM_VIEW

Standard View: Bottom View.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::BOTTOM_VIEW
Layout::SketchUpModel::BOTTOM_RELATIVE_VIEW

The LayOut relative bottom view.

Returns:

  • (Integer)
Note: ModraUp extension. Does not support this; an ArgumentError is thrown when setting it.

Examples:

Layout::SketchUpModel::BOTTOM_RELATIVE_VIEW
Layout::SketchUpModel::FRONT_VIEW

Standard View: Front View.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::FRONT_VIEW
Layout::SketchUpModel::BACK_VIEW

Standard View: Back View.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::BACK_VIEW
Layout::SketchUpModel::LEFT_VIEW

Standard View: Left View.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::LEFT_VIEW
Layout::SketchUpModel::RIGHT_VIEW

Standard View: Right View.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::RIGHT_VIEW
Layout::SketchUpModel::ISO_VIEW

Standard View: Isometric.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel::ISO_VIEW

Class Method Summary # collapse

  • .new(scene, bounds) ⇒ Layout::SketchUpModel
    Create a viewport and then add it to the drawing sheet using Layout::Document#add_entity. The scene viewport displays the scene's camera, style, and hidden elements. An instance viewport (independent viewport) only shows the component or group instance, defaulting to a top view with parallel projection, and uses integer scaling ratios. Nested instances are specified with ModraUp::InstancePath.

Instance Method Summary # collapse

Inherited from Layout::Entity

Class Method Details

Layout::SketchUpModel.new(scene, bounds) ⇒ Layout::SketchUpModelLayout::SketchUpModel.new(instance, bounds) ⇒ Layout::SketchUpModelLayout::SketchUpModel.new(instance_path, bounds) ⇒ Layout::SketchUpModel

Create a viewport and then add it to the drawing sheet using Layout::Document#add_entity. The scene viewport displays the scene's camera, style, and hidden elements. An instance viewport (independent viewport) only shows the component or group instance, defaulting to a top view with parallel projection, and uses integer scaling ratios. Nested instances are specified with ModraUp::InstancePath.

Parameters:

  • source (ModraUp::Page, ModraUp::ComponentInstance, ModraUp::Group, ModraUp::InstancePath)
  • bounds (Geom::Bounds2d) Position and size on the layout (in inches)

Returns:

  • (Layout::SketchUpModel)

Raises:

  • [NotImplementedError] source is a .skp path
  • [ArgumentError] The instance path contains a non-instance
Note: ModraUp extension. LayOut The first parameter is the .skp path; OpenSu is this model's scene or instance.

Examples:

The front view of a single component.

model = ModraUp.active_model
box = model.entities.add_group
box.entities.add_face([0, 0, 0], [20, 0, 0], [20, 10, 0], [0, 10, 0]).pushpull(-30)
front = Layout::SketchUpModel.new(box, Geom::Bounds2d.new(290.mm, 20.mm, 110.mm, 90.mm))
front.view = Layout::SketchUpModel::FRONT_VIEW
front.scale = 1.0 / 20
doc = model.layout_document
doc.add_entity(front, doc.layers.active, doc.pages.add)

Instance Method Details

viewport.scale ⇒ Float, nil

The scale is a ratio (e.g., 1:50 corresponds to 0.02). The instance viewport in the layout has not been documented and is nil if the scale is not applied (the scale is calculated based on size when applied).

Returns:

  • (Float, nil)

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.scale = 1.0 / 20
v.scale  #=> 0.05

viewport.scale = ratio

Set the scale, one step undo. The camera height for parallel projection is derived from "viewport height × scale denominator".

Parameters:

  • ratio (Numeric) 1:1~1:10000

Returns:

  • (Numeric)

Raises:

  • [ArgumentError] Out of range

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.scale = 1.0 / 100

viewport.render_mode ⇒ Integer

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).render_mode

viewport.render_mode = mode

Set the rendering mode, with one-step undo. Wireframe is black and white; switch to colored background when mixed in black and white.

Parameters:

  • mode (Integer)

Returns:

  • (Integer)

Raises:

  • [ArgumentError] Unknown constant

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.render_mode = Layout::SketchUpModel::HYBRID_RENDER

viewport.view ⇒ Integer

Standard views (e.g., TOP_VIEW);custom camera as CUSTOM_VIEW.

Returns:

  • (Integer)

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).view

viewport.view = view

Switch to the standard view, undo one step. The scale remains unchanged in parallel projection.

Parameters:

  • view (Integer) TOP_VIEW、BOTTOM_VIEW、FRONT_VIEW、BACK_VIEW、LEFT_VIEW、RIGHT_VIEW or ISO_VIEW

Returns:

  • (Integer)

Raises:

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.view = Layout::SketchUpModel::FRONT_VIEW

viewport.perspective? ⇒ Boolean

Returns whether the projection is perspective.

Returns:

  • (Boolean)

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).perspective?

viewport.perspective = perspective

Switch between perspective or parallel projection, with one step undo. When switching to parallel projection, the ratio is calculated based on the current viewport.

Parameters:

  • perspective (Boolean)

Returns:

  • (Boolean)

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.perspective = false

viewport.line_weight ⇒ Float

Model line weight in points; 0 for automatic.

Returns:

  • (Float)

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).line_weight

viewport.line_weight = points

Sets the model line weight, undoable in one step.

Parameters:

  • points (Numeric) 0 (Automatic)~10

Returns:

  • (Numeric)

Raises:

  • [ArgumentError] Out of range

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.line_weight = 0.5

viewport.scenes ⇒ Array<String>

The model's scene names, in the order of ModraUp::Pages.

Returns:

  • (Array<String>)
Note: ModraUp extension. The first item in LayOut's list is the "Last Saved View";here, only scenes are listed.

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add("A"), Geom::Bounds2d.new(1, 1, 4, 3)).scenes

viewport.current_scene ⇒ Integer, nil

The index of the displayed scene in #scenes; nil if the instance viewport or scene has been deleted.

Returns:

  • (Integer, nil)
Note: ModraUp extension. Unlike LayOut, which includes a "Last Saved View" option in its index baseline.

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).current_scene

viewport.current_scene = scene

Change to display another scene, undo one step; camera returns to the camera of that scene.

Parameters:

  • scene (Integer, ModraUp::Page)

Returns:

  • (Integer, ModraUp::Page)

Raises:

  • [ IndexError ] Scene does not exist

Examples:

pages = ModraUp.active_model.pages
v = Layout::SketchUpModel.new(pages.add("A"), Geom::Bounds2d.new(1, 1, 4, 3))
v.current_scene = pages.add("B")

viewport.camera_modified? ⇒ Boolean

The camera is different from the scene (true after the standard view, navigation, or #camera=; instance viewports always return true).

Returns:

  • (Boolean)

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).camera_modified?

viewport.reset_camera ⇒ Layout::SketchUpModel

Reset the camera to the scene's initial view, one step undo.

Returns:

  • (Layout::SketchUpModel) self

Raises:

  • [ArgumentError] The instance viewport has not been added to the document.

Examples:

doc = ModraUp.active_model.layout_document
v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
doc.add_entity(v, doc.layers.active, doc.pages.add)
v.view = Layout::SketchUpModel::TOP_VIEW
v.reset_camera

viewport.preserve_scale_on_resize? ⇒ Boolean

Does the viewport maintain scale when its size is changed (default is true).

Returns:

  • (Boolean)

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).preserve_scale_on_resize?

viewport.preserve_scale_on_resize = keep

Sets whether to preserve scale on resizing, with one step for undo.

Parameters:

  • keep (Boolean)

Returns:

  • (Boolean)

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.preserve_scale_on_resize = false

viewport.render_needed? ⇒ Boolean

Does the preview need to be updated (not the latest, not generated, model has changed, or updating)? The drawing workspace updates in the background automatically when open.

Returns:

  • (Boolean)

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).render_needed?  #=> true

viewport.render ⇒ raises

Does not support synchronous rendering: rendering occurs in a separate process, and the results are returned via the interface event loop.

Raises:

  • [NotImplementedError]
Note: ModraUp extension. Unlike LayOut, rendering starts automatically when opening the drawing workspace. You can check if rendering is needed using #render_needed?.

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).render rescue puts $!.message

viewport.model_to_paper_point(point) ⇒ Geom::Point2d

The position of a model point on the paper in inches, including viewport translation, rotation, and scale.

Parameters:

  • point (Geom::Point3d) Model point

Returns:

  • (Geom::Point2d)

Raises:

  • [ArgumentError] Perspective viewport (without a fixed ratio) or one that has not been added to the document.

Examples:

model = ModraUp.active_model
box = model.entities.add_group
box.entities.add_face([0, 0, 0], [20, 0, 0], [20, 10, 0], [0, 10, 0])
doc = model.layout_document
v = Layout::SketchUpModel.new(box, Geom::Bounds2d.new(1, 1, 4, 3))
doc.add_entity(v, doc.layers.active, doc.pages.add)
v.model_to_paper_point(box.bounds.corner(0))

viewport.paper_to_model_point(point) ⇒ Geom::Point3d

The paper point corresponding to the model point, lying on the plane that passes through the camera's target and is perpendicular to the camera's view.

Parameters:

  • point (Geom::Point2d) inch on the drawing sheet

Returns:

  • (Geom::Point3d)

Raises:

  • [ArgumentError] Perspective viewport or not yet added to the document.
Note: ModraUp extension.

Examples:

doc = ModraUp.active_model.layout_document
v = Layout::SketchUpModel.new(ModraUp.active_model.entities.add_group.tap { |g| g.entities.add_cpoint([0, 0, 0]) }, Geom::Bounds2d.new(1, 1, 4, 3))
doc.add_entity(v, doc.layers.active, doc.pages.add)
v.paper_to_model_point(Geom::Point2d.new(3, 2.5))

viewport.camera ⇒ ModraUp::Camera

The camera copy actually used by the viewport (scene camera or custom camera).

Returns:

  • (ModraUp::Camera)

Raises:

  • [ArgumentError] No document has been added yet.
Note: ModraUp extension.

Examples:

doc = ModraUp.active_model.layout_document
v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
doc.add_entity(v, doc.layers.active, doc.pages.add)
v.camera.direction

viewport.camera = camera

Set custom camera, with one-step undo. For parallel projection, the scale is inferred from the camera height.

Parameters:

  • camera (ModraUp::Camera)

Returns:

  • (ModraUp::Camera)

Raises:

  • [ArgumentError] The camera is invalid or has not been added to the document.
Note: ModraUp extension.

Examples:

doc = ModraUp.active_model.layout_document
v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
doc.add_entity(v, doc.layers.active, doc.pages.add)
v.camera = ModraUp::Camera.new([0, -100, 50], [0, 0, 0], [0, 0, 1], false)

viewport.source ⇒ ModraUp::Page, ModraUp::InstancePath, nil

The source of the viewport: the scene, or the instance path of an independent viewport; it becomes nil when the source is deleted.

Returns:

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

Examples:

All views of a component

doc = ModraUp.active_model.layout_document
doc.pages.flat_map { |p| p.entities.grep(Layout::SketchUpModel) }
   .select { |v| v.source.is_a?(ModraUp::InstancePath) }

viewport.color_mode ⇒ Symbol

Color: black_and_white, color, or grayscale.

Returns:

  • (Symbol)
Note: ModraUp extension.

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).color_mode

viewport.color_mode = mode

Set the color mode, undoing any changes. After selecting color or grayscale in the sketch viewport, the model switches to mixed representation.

Parameters:

  • mode (Symbol)

Returns:

  • (Symbol)

Raises:

  • [ArgumentError] Unknown mode
Note: ModraUp extension.

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.color_mode = :black_and_white

viewport.pan ⇒ Array<Length>

Pan the viewport by [x, y] (in paper inches).

Returns:

  • (Array<Length>)
Note: ModraUp extension.

Examples:

Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3)).pan

viewport.pan = [x, y]

Set the viewport pan, one step in the undo.

Parameters:

  • offset (Array<Length>, Geom::Point2d) inch on the drawing sheet

Returns:

  • (Array)
Note: ModraUp extension.

Examples:

v = Layout::SketchUpModel.new(ModraUp.active_model.pages.add, Geom::Bounds2d.new(1, 1, 4, 3))
v.pan = [5.mm, 0]

viewport.create_section(first, second, look, depth: nil, position: nil, page: nil) ⇒ Layout::SketchUpModel

Generate a section view from the current viewport, with one undo step, resulting in the same outcome as the section tool: a new viewport with section numbers, and the original viewport with a section cut symbol. The points first and second are the model points at the ends of the section line, which should project as a line in the current viewport (at least 0.1 mm long); the section plane contains this line and the viewing direction of the current viewport. The look parameter specifies the viewing direction of the section, pointing to the retained side.

If position is not provided, the tool will arrange the section views according to its rules: first try right, left, down, and up, then move the original viewport overall, and if it still cannot fit, create a new sheet of the same size.

Parameters:

  • first (Geom::Point3d)
  • second (Geom::Point3d)
  • look (Geom::Vector3d) The observation direction forms an angle of at least approximately 1° with the section plane.
  • depth (Length, nil) Retained depth (model inches); nil for unlimited
  • position (Geom::Point2d, nil) Center of the new view (in sketch units); raise an error if it extends beyond the paper.
  • page (Layout::Page, nil) Place it on which drawing sheet; default is the page of the current viewport.

Returns:

  • (Layout::SketchUpModel) Create new section plane

Raises:

  • [ArgumentError] The perspective viewport is too short, the section plane is too short, the view direction is within the section plane, or the position does not fit.
Note: ModraUp extension.

Examples:

model = ModraUp.active_model
box = model.entities.add_group
box.entities.add_face([0, 0, 0], [40, 0, 0], [40, 20, 0], [0, 20, 0]).pushpull(-30)
doc = model.layout_document
top = Layout::SketchUpModel.new(box, Geom::Bounds2d.new(20.mm, 20.mm, 110.mm, 70.mm))
doc.add_entity(top, doc.layers.active, doc.pages.add)
bb = box.bounds
c = bb.center
section = top.create_section(Geom::Point3d.new(bb.min.x - 2, c.y, c.z),
                             Geom::Point3d.new(bb.max.x + 2, c.y, c.z),
                             Geom::Vector3d.new(0, 1, 0), depth: bb.height / 2 + 2)
section.scale = 1.0 / 20

viewport.create_detail(corner1, corner2, scale: nil, position:, page: nil) ⇒ Layout::SketchUpModel

Generate a detail drawing from this viewport, with one undo step, similar to the detail tool: create a region bounded by two model points in the current viewport and scale it to a larger proportion at position, adding the detail frame to the current viewport.

Parameters:

  • corner1 (Geom::Point3d)
  • corner2 (Geom::Point3d)
  • scale (Numeric, nil) Ratio; nil for the smallest common scale that is at least doubled.
  • position (Geom::Point2d) Center new view (on drawing sheet inches)
  • page (Layout::Page, nil) The default is the drawing sheet containing the current viewport.

Returns:

  • (Layout::SketchUpModel) Create a new detail drawing

Raises:

  • [ArgumentError] The area is too small to fit the paper or perspective viewport.
Note: ModraUp extension.

Examples:

model = ModraUp.active_model
box = model.entities.add_group
box.entities.add_face([0, 0, 0], [40, 0, 0], [40, 20, 0], [0, 20, 0])
doc = model.layout_document
top = Layout::SketchUpModel.new(box, Geom::Bounds2d.new(20.mm, 20.mm, 110.mm, 70.mm))
doc.add_entity(top, doc.layers.active, doc.pages.add)
top.create_detail([0, 0, 0], [10, 10, 0], position: Geom::Point2d.new(250.mm, 60.mm))

viewport.section_marks ⇒ Array<Hash>

Section marks in the current viewport: each {first:, second:, normal:, target:}, where target is the section plane (set to nil if removed).

Returns:

  • (Array<Hash>)
Note: ModraUp extension.

Examples:

doc = ModraUp.active_model.layout_document
doc.pages.flat_map { |p| p.entities.grep(Layout::SketchUpModel) }.map(&:section_marks)

viewport.visible_lines(kinds: [:model]) ⇒ Array<Layout::VisibleLine>

Hidden visible edges in the sketch plane, in inches, used for outlining, generating continuous dimensions, etc. Prioritizes reusing exported and snap-captured vector caches; if not found, it is computed synchronously on the main thread, causing a brief interface unresponsiveness, with duration increasing with model size; plugins should call this only for necessary views.

Parameters:

  • kinds (Array<Symbol>) :model (model lines), :section (section planes), :dimension (model dimensions)

Returns:

  • (Array<Layout::VisibleLine>)

Raises:

  • [ArgumentError] Perspective viewport or unknown type
  • [RangeError] Exceeds the limit of 40,000 segments per operation.
  • [RuntimeError] Unable to generate vectors (e.g., no renderer)
Note: ModraUp extension.

Examples:

doc = ModraUp.active_model.layout_document
view = doc.pages.flat_map { |p| p.entities.grep(Layout::SketchUpModel) }.first
xs = view.visible_lines(kinds: [:model, :section]).flat_map { |l| [l.start.x, l.end.x] }.uniq.sort if view