ModraUp Ruby API Documentation Ruby API Reference

Class: ModraUp::Entity

Inherits:
Object
  • Object
show all

Overview

Base class for model objects. Subclasses that can be drawn are ModraUp::Drawingelement. ModraUp::Vertex, ModraUp::Loop, ModraUp::Curve, ModraUp::Material, and ModraUp::Layer are also Entity objects, but not Drawingelements.

Examples:

e = ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0])
e.typename  #=> "Edge"
e.is_a?(ModraUp::Entity)  #=> true

Instance Method Summary # collapse

Instance Method Details

entity.typename ⇒ String

Type name (e.g., Face / Edge / Group).

Returns:

  • (String)

Examples:

ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0]).typename  #=> "Edge"

entity.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.

Returns:

  • (Integer)

Examples:

edge = ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0])
edge.entityID  #=> e.g. 57
ModraUp.active_model.find_entity_by_id(edge.entityID) == edge  #=> true

entity.add_observer(observer) ⇒ Boolean

Register a ModraUp::EntityObserver. Adding the same object a second time returns false. Locking, property, and material changes will dispatch an onChangeEntity event.

Parameters:

  • observer (ModraUp::EntityObserver)

Returns:

  • (Boolean)

Examples:

class MyEnt < ModraUp::EntityObserver
  def onChangeEntity(entity)
    entity.typename
  end
end
edge = ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0])
edge.add_observer(MyEnt.new)

entity.remove_observer(observer) ⇒ Boolean

Remove the entity observer.

Parameters:

  • observer (ModraUp::EntityObserver)

Returns:

  • (Boolean)

Examples:

edge = ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0])
obs = ModraUp::EntityObserver.new
edge.add_observer(obs)
edge.remove_observer(obs)

entity.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.

Returns:

  • (Integer)

Examples:

pid = ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0]).persistent_id
ModraUp.active_model.find_entity_by_persistent_id(pid)

entity.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.

Returns:

  • (ModraUp::Model, ModraUp::ComponentDefinition)

Examples:

ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0]).parent

entity.model ⇒ ModraUp::Model

The model it belongs to.

Returns:

  • (ModraUp::Model)

Examples:

ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0]).model

entity.to_s ⇒ String

String form. Returns "#<Deleted Entity>” when the entity has been deleted.

Returns:

  • (String)

Examples:

ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0]).to_s

entity.inspect ⇒ String

Same as #to_s.

Returns:

  • (String)

Examples:

ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0]).inspect

entity.attribute_dictionaries ⇒ ModraUp::AttributeDictionaries, nil

Collection of attribute dictionaries. Returns nil (evaluates to false) if there are no dictionaries. ModraUp::Loop / ModraUp::EdgeUse / ModraUp::Curve have no dictionaries.

Returns:

  • (ModraUp::AttributeDictionaries, nil)

Examples:

face = ModraUp.active_model.entities.add_face([0, 0, 0], [1, 0, 0], [1, 1, 0])
face.attribute_dictionaries  #=> nil

entity.attribute_dictionary(name) ⇒ ModraUp::AttributeDictionary, nilentity.attribute_dictionary(name, create) ⇒ 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.

Parameters:

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

Returns:

  • (ModraUp::AttributeDictionary, nil)

Examples:

defn = ModraUp.active_model.definitions.add("DC")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.attribute_dictionary("dynamic_attributes")  #=> nil
inst.set_attribute("dynamic_attributes", "lenx", "10")
inst.attribute_dictionary("dynamic_attributes")["lenx"]  #=> "10"

entity.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.

Returns:

  • (Boolean)

Examples:

e = ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0])
e.valid?

entity.deleted? ⇒ Boolean

Opposite of #valid?.

Returns:

  • (Boolean)

Examples:

ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0]).deleted?

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

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.

For structural data, generate a string using JSON.generate before passing it in.

Parameters:

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

Returns:

  • (Object)

Raises:

Examples:

defn = ModraUp.active_model.definitions.add("DC")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.set_attribute("dynamic_attributes", "lenx", "10")

Edge

edge = ModraUp.active_model.entities.add_line([0, 0, 0], [1, 0, 0])
edge.set_attribute("SPIRIT_ELEMENT_DICT", "index", 2)
edge.get_attribute("SPIRIT_ELEMENT_DICT", :index)  #=> 2

entity.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.

Parameters:

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

Returns:

  • (Object)

Examples:

defn = ModraUp.active_model.definitions.add("DC")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.set_attribute("dynamic_attributes", "lenx", "10")
inst.get_attribute("dynamic_attributes", "lenx")  #=> "10"
inst.get_attribute("dynamic_attributes", :lenx)  #=> "10"

entity.delete_attribute(dict_name) ⇒ Booleanentity.delete_attribute(dict_name, key) ⇒ Boolean

Delete a key, or delete the entire dictionary when the key is omitted.

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("DC")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.set_attribute("dynamic_attributes", "lenx", "10")
inst.delete_attribute("dynamic_attributes", "lenx")