ModraUp Ruby API Documentation Ruby API Reference

Class: ModraUp::DefinitionList

Inherits:
ModraUp::Entity show all
Includes:
Enumerable

Overview

The model's component definitions, available through ModraUp::Model#definitions. Use #add for an empty definition, #load for a local .skp or .osk3d file, #import for an .osk3d import, and #load_from_url to download and load a definition. Use #[] to look up an existing definition by name, GUID, or index.

Examples:

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

Instance Method Summary # collapse

Inherited from ModraUp::Entity

  • #typename ⇒ String
    Type name (e.g., Face / Edge / Group).
  • #entityID ⇒ Integer
    Entity ID within the current session: A small integer starting from 1 that increments and remains constant for the lifetime of the entity, unchanged by undo/redo operations. This ID is not preserved when saving the file; it will be renumbered upon reopening the model. For cross-session identification, use #persistent_id. Copied entities will receive new IDs.
  • #persistent_id ⇒ Integer
    Number assigned with the model: A unique, incrementing small integer starting from 1 that persists across model saves and reloads, independent of #entityID. Copied entities receive new numbers, and deleted entities do not reuse their numbers. Edges, faces, vertices, curves, groups, component instances/definitions, dimensions, leader lines/points, section planes, materials, layers, layer folders, and drawing sheets have assigned numbers; all others (rings, EdgeUses, attribute dictionaries, line types, sets, etc.) return 0.
  • #parent ⇒ ModraUp::Model, ModraUp::ComponentDefinition
    Returns the owner of this drawing context. At the root this is the model. Inside a definition it is the definition itself.
  • #model ⇒ ModraUp::Model
    The model it belongs to.
  • #to_s ⇒ String
    String form. Returns "#<Deleted Entity>” when the entity has been deleted.
  • #inspect ⇒ String
    Same as #to_s.
  • #attribute_dictionaries ⇒ ModraUp::AttributeDictionaries, nil
    Collection of attribute dictionaries. Returns nil (evaluates to false) if there are no dictionaries.
  • #attribute_dictionary(name) ⇒ ModraUp::AttributeDictionary, nil
    Retrieve an attribute dictionary by name. If none exists, return nil; if create is true, create an empty dictionary. ModraUp::Loop / ModraUp::EdgeUse / ModraUp::Curve always return nil.
  • #valid? ⇒ Boolean
    Returns true if the entity is valid, meaning it has not been deleted from the model. Revoked entities are false; even if new geometry is drawn occupying its original position, this object remains an invalid entity (still false). Repeating the action to restore it makes it valid again.
  • #deleted? ⇒ Boolean
    Opposite of #valid?.
  • #set_attribute(dict_name, key, value) ⇒ Object
    Write attribute values. Materials, groups, component instances, faces, and edges are supported. When dict_name and key are Symbols, they are converted to strings. The type of data written is the same type that will be read back.
  • #get_attribute(dict_name, key, default = nil) ⇒ Object
    Read the attribute. If dict_name and key are Symbols, convert them to strings (similar to SketchUp). If the dictionary or key does not exist, return default. The return value type is the same as what is written by #set_attribute.
  • #delete_attribute(dict_name) ⇒ Boolean
    Delete a key, or delete the entire dictionary when the key is omitted.

Instance Method Details

definitions[name] ⇒ ModraUp::ComponentDefinition, nildefinitions[guid] ⇒ ModraUp::ComponentDefinition, nildefinitions[index] ⇒ ModraUp::ComponentDefinition, nil

Retrieve a definition by name, GUID, or 0-based index. Negative indices count from the end. Strings match by name or GUID, with ASCII letters case-insensitive. Unmatched strings return nil.

Parameters:

  • arg (String, Numeric)

Returns:

  • (ModraUp::ComponentDefinition, nil)

Raises:

  • [ IndexError ] out-of-bounds index

Examples:

defs = ModraUp.active_model.definitions
defs.add("Box")
defs["Box"].name  #=> "Box"
defs[0].is_a?(ModraUp::ComponentDefinition)

definitions.at(name) ⇒ ModraUp::ComponentDefinition, nildefinitions.at(guid) ⇒ ModraUp::ComponentDefinition, nildefinitions.at(index) ⇒ ModraUp::ComponentDefinition, nil

Alias for #[].

Parameters:

  • arg (String, Numeric)

Returns:

  • (ModraUp::ComponentDefinition, nil)

Raises:

  • [ IndexError ] out-of-bounds index

Examples:

ModraUp.active_model.definitions.at("Box")

definitions.length ⇒ Integer

The number of defined entities (including group definitions) that have not been deleted.

Returns:

  • (Integer)

Examples:

ModraUp.active_model.definitions.length

definitions.size ⇒ Integer

Alias for #length.

Returns:

  • (Integer)

Examples:

ModraUp.active_model.definitions.size

definitions.each {|definition| } ⇒ ModraUp::DefinitionList

Iterate through the non-deleted definitions.

Returns:

  • (ModraUp::DefinitionList)

Yields:

  • @yieldparam definition [ModraUp::ComponentDefinition]

Raises:

  • [LocalJumpError] The list is not empty and no block was provided.

Examples:

ModraUp.active_model.definitions.each { |d| d.name }

definitions.add(name = "Component") ⇒ ModraUp::ComponentDefinition

Add a component definition.

Parameters:

  • name (String)

Returns:

  • (ModraUp::ComponentDefinition)

Raises:

  • [RuntimeError] The target document was closed or replaced during parameter conversion.

Examples:

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

definitions.unique_name(base_name) ⇒ String

Returns an unused definition name. ASCII letters are case-insensitive when compared, and if a duplicate name is found, it appends #1, #2, etc., without creating the definition.

Parameters:

  • base_name (String) Base name

Returns:

  • (String)

Examples:

ModraUp.active_model.definitions.unique_name("Box")

definitions.purge_unused ⇒ ModraUp::DefinitionList

Purge unused definitions by retaining only those definitions and their sub-definitions that still have instances placed in the model.

Returns:

  • (ModraUp::DefinitionList) This list

Examples:

ModraUp.active_model.definitions.purge_unused

definitions.remove(definition) ⇒ Boolean

Removes the definition of a specified component or group, as well as all instances of that definition from the model.

Parameters:

  • definition (ModraUp::ComponentDefinition) To delete the definition

Returns:

  • (Boolean) Returns true when the deletion is successful; returns false when the definition belongs to another model.

Raises:

  • [TypeError] The parameter is not a component definition or the definition has been deleted.
  • [ArgumentError] The current edit path uses this definition.

Examples:

Remove the definition just created.

definitions = ModraUp.active_model.definitions
definition = definitions.add('Temporary component')
definitions.remove(definition)

definitions.load(path) ⇒ ModraUp::ComponentDefinitiondefinitions.load(path, allow_newer: false) ⇒ ModraUp::ComponentDefinition

Load component definitions from .skp or native .osk3d files. ModraUp::ComponentDefinition#path is the file path, and ModraUp::ComponentDefinition#load_time is the current time. Changes loaded from OSK3D can be undone.

Parameters:

  • path (String) Path to a SKP or OSK3D file
  • allow_newer (Boolean) Compatible keywords. SKP files that can be decoded are not rejected based on the version number of the saving program.

Returns:

  • (ModraUp::ComponentDefinition)

Raises:

  • [IOError] File missing, invalid, or unsupported format.
  • [RuntimeError] The file is part of the current model (including any copies with the same content), the first loaded component is empty, or the target document is replaced before loading.
Note: ModraUp extension. Supports native .osk3d.

Examples:

path = "/tmp/box.skp"
definitions = ModraUp.active_model.definitions
box = definitions.add(definitions.unique_name("Box"))
box.entities.add_line(ORIGIN, [1, 0, 0])
box.save_as(path)
defn = definitions.load(path, allow_newer: true)
defn.path == path

definitions.import(path, options = {}) ⇒ ModraUp::ComponentDefinition

Import a native .osk3d file as an unplaced component definition. SKP files use ModraUp::DefinitionList#load.

Parameters:

  • path (String) Native model file path
  • options (Hash) To maintain compatibility with the calling signature, native imports use file-level settings.

Returns:

  • (ModraUp::ComponentDefinition)

Raises:

  • [IOError] File does not exist, import failed, or the target document was replaced during import.
  • [ArgumentError] File extension is not .osk3d
Note: ModraUp extension. Supports native .osk3d.

Examples:

Import an existing ModraUp file

path = UI.openpanel("Select model", "", "*.osk3d")
ModraUp.active_model.definitions.import(path) if path

definitions.load_from_url(url) ⇒ ModraUp::ComponentDefinitiondefinitions.load_from_url(url, load_handler) ⇒ ModraUp::ComponentDefinition, nil

Loads a .skp component definition from an HTTP or HTTPS URL. The call blocks until the download finishes. An optional handler implements the ModraUp::LoadHandler callbacks cancelled?, onPercentChange(percent), onSuccess, and onFailure(message). Without a handler, failures raise IOError. With a handler, failures call onFailure and return nil. Dynamic Components uses this path through DCDownloader.

Parameters:

  • url (String)
  • load_handler (ModraUp::LoadHandler) Optional; does not have to be a subclass of this class, but must implement the callback.

Returns:

  • (ModraUp::ComponentDefinition, nil)

Raises:

  • [ArgumentError] URL is empty
  • [IOError] No handler and URL invalid, download or parse failed

Examples:

handler = Class.new do
  def onFailure(message); puts(message); end
end.new
ModraUp.active_model.definitions.load_from_url("http://127.0.0.1:1/box.skp", handler)

definitions.add_observer(observer) ⇒ Boolean

Register a ModraUp::DefinitionsObserver. Re-registering the same object still returns true, but the event is dispatched only once. #add / #load will dispatch an onComponentAdded event.

Parameters:

  • observer (ModraUp::DefinitionsObserver)

Returns:

  • (Boolean)

Examples:

class MyDefs < ModraUp::DefinitionsObserver
  def onComponentAdded(definitions, definition)
    definition.name
  end
end
ModraUp.active_model.definitions.add_observer(MyDefs.new)

definitions.remove_observer(observer) ⇒ Boolean

Remove observers from the definition list.

Parameters:

  • observer (ModraUp::DefinitionsObserver)

Returns:

  • (Boolean)

Examples:

defs = ModraUp.active_model.definitions
obs = ModraUp::DefinitionsObserver.new
defs.add_observer(obs)
defs.remove_observer(obs)