ModraUp Ruby API Documentation Ruby API Reference

Class: ModraUp::ComponentDefinition

Inherits:
ModraUp::Drawingelement show all

Overview

Component Definition: A dictionary of name and definition-level properties. Inherits from ModraUp::Drawingelement.

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.name

Instance Method Summary # collapse

Inherited from ModraUp::Entity

Instance Method Details

definition.name ⇒ String

Name.

Returns:

  • (String)

Examples:

ModraUp.active_model.definitions.add("Box").name  #=> "Box"

definition.name = name ⇒ String

Set the definition name. An empty string will raise an error; if it conflicts with other definitions, an automatic suffix of #2 will be added.

Parameters:

  • name (String)

Returns:

  • (String)

Raises:

  • [ArgumentError] Name is empty

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.name = "Crate"
defn.name  #=> "Crate"

definition.description ⇒ String

Definition description. Will be an empty string if not filled.

Returns:

  • (String)

Examples:

ModraUp.active_model.definitions.add("Box").description

definition.description = text ⇒ String

Set the definition description.

Parameters:

  • text (String)

Returns:

  • (String)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.description = "crate"

definition.typename ⇒ String

Fixes to ComponentDefinition.

Returns:

  • (String)

Examples:

ModraUp.active_model.definitions.add("Box").typename  #=> "ComponentDefinition"

definition.entityID ⇒ Integer

The definition ID within the current session, see ModraUp::Entity#entityID.

Returns:

  • (Integer)

Examples:

ModraUp.active_model.definitions.add("Box").entityID

definition.guid ⇒ String

Define the GUID. Leave it as an empty string if it is not present when imported.

Returns:

  • (String)

Examples:

ModraUp.active_model.definitions.add("Box").guid

definition.valid? ⇒ Boolean

Returns true if the definition is still in the model.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").valid?

definition.deleted? ⇒ Boolean

Opposite of #valid?.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").deleted?

definition.entities ⇒ ModraUp::Entities

Define the internal drawing context.

Returns:

  • (ModraUp::Entities)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.entities.length  #=> 0

definition.instances ⇒ Array

All instances of ModraUp::ComponentInstance / ModraUp::Group that reference this definition, including those embedded in other definitions.

Returns:

  • (Array<ModraUp::ComponentInstance, ModraUp::Group>)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
defn.instances[0] == inst  #=> true

definition.count_instances ⇒ Integer

Returns the number of placed instances referencing this definition, equal to #instances.length. Includes instances that use the definition but are not currently placed.

Returns:

  • (Integer)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
ModraUp.active_model.entities.add_instance(defn)
defn.count_instances  #=> 1

definition.count_used_instances ⇒ Integer

Counts the actual occurrences from the model root. When a child definition is placed multiple times under a parent definition, the counts are multiplied by level.

Returns:

  • (Integer)

Examples:

outer = ModraUp.active_model.definitions.add("Outer")
inner = ModraUp.active_model.definitions.add("Inner")
outer.entities.add_instance(inner)
ModraUp.active_model.entities.add_instance(outer)
ModraUp.active_model.entities.add_instance(outer)
inner.count_instances        #=> 1
inner.count_used_instances   #=> 2

definition.attribute_dictionaries ⇒ ModraUp::AttributeDictionaries, nil

Attribute dictionary at the definition level. Returns nil if none is defined.

Returns:

  • (ModraUp::AttributeDictionaries, nil)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.attribute_dictionaries  #=> nil

definition.attribute_dictionary(name) ⇒ ModraUp::AttributeDictionary, nildefinition.attribute_dictionary(name, create) ⇒ ModraUp::AttributeDictionary, nil

Retrieve the definition-level attribute dictionary by name. Returns nil if the attribute dictionary does not exist; creates an empty dictionary if create is true.

Parameters:

  • name (String)
  • create (Boolean) Default false

Returns:

  • (ModraUp::AttributeDictionary, nil)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.attribute_dictionary("dynamic_attributes")  #=> nil
defn.attribute_dictionary("dynamic_attributes", true)
defn.set_attribute("dynamic_attributes", "_lenx_formula", "5")
defn.attribute_dictionary("dynamic_attributes")["_lenx_formula"]  #=> "5"

definition.set_attribute(dict_name, key, value) ⇒ value

Write a definition-level attribute. The type written is the type read back.

Structural data should be passed as a string after being generated with JSON.generate.

Parameters:

  • dict_name (String)
  • key (String)
  • value (Boolean, Integer, Float, Length, String, Time, Array, Geom::Point3d, Geom::Vector3d, nil)

Returns:

  • (Object)

Raises:

  • [TypeError] Hash, Symbol values, entities, etc., are unsupported types

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.set_attribute("dynamic_attributes", "_lenx_formula", "5")

definition.get_attribute(dict_name, key, default = nil) ⇒ Object

Read the definition-level attribute. When dict_name and key are Symbols, convert them to strings.

Parameters:

  • dict_name (String, Symbol)
  • key (String, Symbol)
  • default (Object)

Returns:

  • (Object)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.set_attribute("dynamic_attributes", "_lenx_formula", "5")
defn.get_attribute("dynamic_attributes", "_lenx_formula")  #=> "5"

definition.delete_attribute(dict_name) ⇒ Booleandefinition.delete_attribute(dict_name, key) ⇒ Boolean

Delete attribute keys or an entire dictionary at the definition level.

Parameters:

  • dict_name (String)
  • key (String)

Returns:

  • (Boolean) Returns true if the attribute is found and deleted, false if the attribute is not found.

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.set_attribute("d", "k", "v")
defn.delete_attribute("dynamic_attributes")

definition.save_thumbnail(path) ⇒ Boolean

Write the definition thumbnail to path (PNG). Render with the same set of faces and edges coloring as #thumbnail_camera (if not set, bound the definition bounding box); bake with no geometry, no UI, or if writing to disk fails, return false.

Parameters:

  • path (String)

Returns:

  • (Boolean)
Note: Returns false if there is no UI host.

Examples:

defn = ModraUp.active_model.definitions.add("Box")
face = defn.entities.add_face([0,0,0], [10,0,0], [10,10,0], [0,10,0])
face.pushpull(5)
defn.save_thumbnail("/tmp/box.png")  #=> true

definition.refresh_thumbnail ⇒ nil

Request to refresh the component browser thumbnail (invalidate dirty memory cache; re-bake next time it is displayed).

Returns:

  • (nil)

Examples:

ModraUp.active_model.definitions.add("Box").refresh_thumbnail

definition.bounds ⇒ Geom::BoundingBox

Define the axis-aligned bounding box in local coordinates (excluding instance transformations).

Returns:

  • (Geom::BoundingBox)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.entities.add_line([0, 0, 0], [2, 0, 0])
defn.bounds.width  #=> 2.0

definition.invalidate_bounds ⇒ true

Drop the definition bounds cache. This implementation recalculates #bounds on every call, invalidating the viewport tessellation.

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.invalidate_bounds

definition.behavior ⇒ ModraUp::Behavior

The definition's ModraUp::Behavior.

Returns:

  • (ModraUp::Behavior)

Examples:

ModraUp.active_model.definitions.add("Box").behavior.is2d?

definition.group? ⇒ Boolean

Returns true if the component definition is a group (as opposed to a regular component).

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").group?  #=> false

definition.hidden? ⇒ Boolean

Hides the component in the component browser.

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.hidden = true
defn.hidden?  #=> true

definition.image? ⇒ Boolean

Returns whether this is an image definition. Currently always returns false.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").image?  #=> false

definition.internal? ⇒ Boolean

Is an internal definition within the model (not loaded from a file). Currently, this is always true.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").internal?

definition.live_component? ⇒ Boolean

Returns whether the component is a Live Component. Currently, this always returns false.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").live_component?  #=> false

definition.manifold? ⇒ Boolean

Is a closed solid: each live edge is exactly shared by two live faces, equivalent to the manifold determination within ModraUp::Entities.

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
face = defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0])
face.pushpull(10)
defn.manifold?  #=> true

definition.path ⇒ String

Returns the file path of the loaded definition. An in-memory definition is an empty string.

Returns:

  • (String)

Examples:

ModraUp.active_model.definitions.add("Box").path  #=> ""

definition.insertion_point ⇒ Geom::Point3d

Insertion point. Fixed at the origin since SketchUp 2020.

Returns:

  • (Geom::Point3d)

Examples:

ModraUp.active_model.definitions.add("Box").insertion_point.to_a
  #=> [0.0, 0.0, 0.0]

definition.insertion_point = point ⇒ point

Set the insertion point. No operation in SketchUp 2020 and later.

Parameters:

  • point (Geom::Point3d, Array<Numeric>)

Returns:

  • (Object)

Examples:

ModraUp.active_model.definitions.add("Box").insertion_point = [1, 0, 0]

definition.load_time ⇒ Time

Define load time (in Unix seconds). New definitions in memory have an epoch time.

Returns:

  • (Time)

Examples:

ModraUp.active_model.definitions.add("Box").load_time.to_i  #=> 0

definition.save_as(path) ⇒ Booleandefinition.save_as(path, version) ⇒ Boolean

Save the definition as a SketchUp .skp file. The root entity is the geometry of this definition; nested definitions are included. After success, #path becomes path. version is currently ignored (always writes legacy v17).

Parameters:

  • path (String)
  • version (Integer) Ignore

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add "Box"
defn.entities.add_face [0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]
defn.save_as "/tmp/box.skp"

definition.save_copy(path) ⇒ Booleandefinition.save_copy(path, version) ⇒ Boolean

Save a copy .skp, without modifying the current #path. Write the same content as #save_as.

Parameters:

  • path (String)
  • version (Integer) Ignore

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add "Box"
defn.entities.add_face [0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]
defn.save_copy "/tmp/box-copy.skp"

definition.add_classification(schema_name, schema_type) ⇒ Boolean

Add a schema/type classification to the definition. An empty name returns false; adding the same schema will replace the type.

Parameters:

  • schema_name (String)
  • schema_type (String)

Returns:

  • (Boolean)
Note: ModraUp extension. Does not require the model's Classifications table to have the schema beforehand.

Examples:

ModraUp.active_model.definitions.add("Box").add_classification("ifc", "Wall")
  #=> true

definition.remove_classification(schema_name, schema_type) ⇒ Boolean

Remove the added schema/type. If not found, return false, and clear the classification values under that schema.

Parameters:

  • schema_name (String)
  • schema_type (String)

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.add_classification("ifc", "Wall")
defn.remove_classification("ifc", "Wall")  #=> true

definition.get_classification_value(path) ⇒ Object, nil

Read classification attributes along a path. The path's first item is the schema name; otherwise, it returns nil.

Parameters:

  • path (Array<String>, String)

Returns:

  • (String, nil)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.add_classification("ifc", "Wall")
defn.set_classification_value(["ifc", "Name"], "Wall-1")
defn.get_classification_value(["ifc", "Name"])  #=> "Wall-1"

definition.set_classification_value(path, value) ⇒ Boolean

Write the classification attribute along the path. The schema must be #add_classification; otherwise, it returns false.

Parameters:

  • path (Array<String>, String)
  • value (Object) would to_s

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.add_classification("ifc", "Wall")
defn.set_classification_value(["ifc", "Name"], "Wall-1")  #=> true

definition.add_observer(observer) ⇒ Boolean

Register a ModraUp::DefinitionObserver. Adding the same object a second time returns false. ModraUp::Entities#add_instance dispatches an onComponentInstanceAdded event.

Parameters:

  • observer (ModraUp::DefinitionObserver)

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.add_observer(ModraUp::DefinitionObserver.new)

definition.remove_observer(observer) ⇒ true

Remove the observer from the definition.

Parameters:

  • observer (Object)

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
obs = ModraUp::DefinitionObserver.new
defn.add_observer(obs)
defn.remove_observer(obs)

definition.model ⇒ ModraUp::Model

The model it belongs to.

Returns:

  • (ModraUp::Model)

Examples:

ModraUp.active_model.definitions.add("Box").model == ModraUp.active_model

definition.parent ⇒ ModraUp::Model

The parent object of the definition is the model.

Returns:

  • (ModraUp::Model)

Examples:

ModraUp.active_model.definitions.add("Box").parent

definition.persistent_id ⇒ Integer

The definition ID that persists with the model, see ModraUp::Entity#persistent_id.

Returns:

  • (Integer)

Examples:

ModraUp.active_model.definitions.add("Box").persistent_id

definition.thumbnail_camera ⇒ ModraUp::Camera

Thumbnail camera. Unset when it bounds the definition bounding box.

Returns:

  • (ModraUp::Camera)

Examples:

ModraUp.active_model.definitions.add("Box").thumbnail_camera.perspective?

definition.thumbnail_camera = camera ⇒ camera

Set the thumbnail camera. Afterward, #thumbnail_camera read back the same viewpoint.

Parameters:

  • camera (ModraUp::Camera)

Returns:

  • (ModraUp::Camera)

Raises:

Examples:

defn = ModraUp.active_model.definitions.add("Box")
cam = defn.thumbnail_camera
cam.fov = 28
defn.thumbnail_camera = cam
defn.thumbnail_camera.fov  #=> 28

definition == other ⇒ Boolean

The same slot in the same model returns true.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Examples:

a = ModraUp.active_model.definitions.add("EqA")
b = ModraUp.active_model.definitions.add("EqB")
a == b  #=> false

definition <=> other ⇒ Integer

Compare by #name for use with sort.

Parameters:

  • other (ModraUp::ComponentDefinition)

Returns:

  • (Integer)

Examples:

a = ModraUp.active_model.definitions.add("A")
b = ModraUp.active_model.definitions.add("B")
(a <=> b) < 0

definition.casts_shadows? ⇒ Boolean

The definition does not cast shadows. Always returns false.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").casts_shadows?  #=> false

definition.casts_shadows = value ⇒ value

Defines the projection switch for the definition level. Current storage is not supported.

Parameters:

  • value (Boolean)

Returns:

  • (Object)

Examples:

ModraUp.active_model.definitions.add("Box").casts_shadows = false

definition.receives_shadows? ⇒ Boolean

The definition does not receive shadows. Always returns false.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").receives_shadows?  #=> false

definition.receives_shadows = value ⇒ value

Defines the shadow reception switch for the component definition. The setting is not currently stored.

Parameters:

  • value (Boolean)

Returns:

  • (Object)

Examples:

ModraUp.active_model.definitions.add("Box").receives_shadows = false

definition.visible? ⇒ Boolean

Determines if the definition is visible in the component browser. Opposite of #hidden?.

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.visible?  #=> true
defn.hidden = true
defn.visible?  #=> false

definition.visible = value ⇒ value

Set the component browser visibility to true. Equivalent to #hidden= with a value of false.

Parameters:

  • value (Boolean)

Returns:

  • (Object)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.visible = false
defn.hidden?  #=> true

definition.hidden = value ⇒ value

Sets the component browser hidden flag.

Parameters:

  • value (Boolean)

Returns:

  • (Object)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.hidden = true
defn.hidden?  #=> true

definition.erase! ⇒ nil

Delete this definition from the definition table. Throws an error if there are instances remaining.

Returns:

  • (nil)

Raises:

  • [RuntimeError] There are instances, or the definition has been deleted.

Examples:

ModraUp.active_model.definitions.add("Box").erase!

definition.layer ⇒ ModraUp::Layer

Returns the default Layer0 if the definition has no layer.

Returns:

  • (ModraUp::Layer)

Examples:

ModraUp.active_model.definitions.add("Box").layer.name

definition.layer = layer ⇒ layer

Define without a layer, write it as ignored.

Parameters:

  • layer (ModraUp::Layer, String)

Returns:

  • (Object)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.layer = defn.layer

definition.material ⇒ nil

Inherits from Drawingelement#material. The definition itself does not define a drawing material and is always nil. The instance material is defined in ModraUp::ComponentInstance#material / ModraUp::Group#material.

Returns:

  • (nil)

Examples:

ModraUp.active_model.definitions.add("Box").material  #=> nil

definition.material=(material) ⇒ material

Inherits from Drawingelement#material=. The definition itself cannot be colored; the assignment is ignored.

Parameters:

  • material (ModraUp::Material, String, ModraUp::Color, nil)

Returns:

  • (Object)

Examples:

ModraUp.active_model.definitions.add("Box").material = nil

definition.locked? ⇒ false

Definitions cannot be locked.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add("Box").locked?  #=> false

definition.locked = setting ⇒ setting

Definitions cannot be locked, writes are ignored.

Parameters:

  • setting (Boolean)

Returns:

  • (Object)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
defn.locked = true
defn.locked?  #=> false

definition.attach_procedure(procedure, parameters) ⇒ nil

Converts the definition to a procedural component. Existing entities become control entities in a hidden inputs group, and #entities exposes the output. Specify a registered procedure or its ID in procedure. Keys in parameters must be declared by the procedure. Omitted parameters use their declared default. If validation or execution fails, the operation is rolled back and raises ArgumentError.

Parameters:

  • procedure (ModraUp::Procedure, String)
  • parameters (Hash)

Returns:

  • (nil)

Raises:

  • [TypeError] ComponentDefinition is already a procedure, parameter is not a Hash, value type not supported
  • [ArgumentError] The ID is not registered, native procedure not declared, parameters not defined, validated, or failed to run.

Examples:

defn = ModraUp.active_model.definitions.add('Fence')
defn.attach_procedure(fence, length: 120)

definition.procedural? ⇒ Boolean

Added Ruby procedures, or generated by native process tools. Parameters components (node graphs) do not count.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.definitions.add('Box').procedural?  #=> false

definition.control_entities ⇒ ModraUp::Entities

Input entities for a process component.

Returns:

  • (ModraUp::Entities)

Raises:

  • [TypeError] Not a process component

Examples:

defn.control_entities.length

definition.get_procedure_parameters ⇒ Hash

Current parameters (Symbol keys). An empty Hash when the process is not registered in this session.

Returns:

  • (Hash{Symbol => Integer, Float, String, Boolean})

Raises:

  • [TypeError] Not a process component

Examples:

defn.get_procedure_parameters  #=> {:length=>120}

definition.set_procedure_parameters(parameters) ⇒ ModraUp::ComponentDefinition

Merge parameters and regenerate the output in one step. Undo on failure, rolling back the entire step and raising an ArgumentError.

Parameters:

  • parameters (Hash)

Returns:

  • (ModraUp::ComponentDefinition)

Raises:

  • [TypeError] The component is not a process component, or the value type is not supported.
  • [ArgumentError] Parameter not declared, process not registered, failure in native process validation or execution.

Examples:

defn.set_procedure_parameters(length: 200)

definition.run_procedures(mode = :if_modified) ⇒ nil

:if_modified runs the procedure only if the control entity has been modified;:force runs the procedure regardless. If no procedures are registered, nothing happens.

Parameters:

  • mode (Symbol)

Returns:

  • (nil)

Raises:

  • [TypeError] Not a process component
  • [ArgumentError] mode is not :if_modified / :force, or the operation failed.

Examples:

defn.run_procedures(:force)

definition.make_non_procedural(retained_geometry) ⇒ nil

Remove Procedural: :keep_output Keep Output (keep input when output is empty), :keep_input Keep Control Entity. One undo step.

Parameters:

  • retained_geometry (Symbol)

Returns:

  • (nil)

Raises:

  • [TypeError] The component is not procedural, or the parameter is not a Symbol.
  • [ArgumentError] not :keep_output / :keep_input

Examples:

defn.make_non_procedural(:keep_output)