ModraUp Ruby API Documentation Ruby API Reference

Class: ModraUp::ComponentInstance

Inherits:
ModraUp::Drawingelement show all

Overview

Component Instance.

Examples:

inst = ModraUp.active_model.entities.add_instance(ModraUp.active_model.definitions.add("Box"))
inst.guid

Instance Method Summary # collapse

Inherited from ModraUp::Drawingelement

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

instance.definition ⇒ ModraUp::ComponentDefinition

The definition (or component) that this instance (or group) references.

Returns:

  • (ModraUp::ComponentDefinition)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.definition.name  #=> "Box"

instance.glued_to ⇒ ModraUp::Face, ModraUp::Group, ModraUp::ComponentInstance or nil

The face or group/component instance that the instance is glued to; nil if not glued or if the target has been deleted.

Returns:

  • (ModraUp::Face, ModraUp::Group, ModraUp::ComponentInstance, nil)

Examples:

inst.glued_to  #=> nil

instance.glued_to = face_or_instance ⇒ face_or_instance

Glues the instance to a face, group, or component instance in the same entities collection. Pass nil to detach it. Raises ArgumentError if the definition disallows gluing, the alignment is incompatible, the object is locked, or the attachment would create a cycle.

Parameters:

  • face_or_instance [ModraUp::Face, ModraUp::Group, ModraUp::ComponentInstance, nil]

Returns:

  • (ModraUp::Face, ModraUp::Group, ModraUp::ComponentInstance, nil)

Examples:

inst.glued_to = face

instance.name ⇒ String

Instance name. Returns an empty string if unnamed.

Returns:

  • (String)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.name  #=> ""

instance.name=(name) ⇒ String

Set the instance name.

Parameters:

  • name (String)

Returns:

  • (String)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.name = "Box#1"

instance.transformation ⇒ Geom::Transformation

Transformation of the instance in the parent drawing context.

Returns:

  • (Geom::Transformation)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.transformation.origin.to_a  #=> [0.0, 0.0, 0.0]

instance.transformation=(transformation) ⇒ Geom::Transformation

Set the transformation of the instance within its parent drawing context.

Parameters:

  • transformation (Geom::Transformation)

Returns:

  • (Geom::Transformation)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.transformation = Geom::Transformation.translation(1, 0, 0)
inst.transformation.origin.to_a  #=> [1.0, 0.0, 0.0]

instance.transform!(transformation) ⇒ self

Left multiply the transformation (apply transformation first, then apply the original transformation).

Parameters:

  • transformation (Geom::Transformation)

Returns:

  • (ModraUp::ComponentInstance)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.transform!(Geom::Transformation.translation(1, 0, 0))
inst.transformation.origin.to_a  #=> [1.0, 0.0, 0.0]

instance.explode ⇒ Array<ModraUp::Entity>, false

Explodes the instance into its parent drawing context. The parent can be an unopened group or definition. Returns false on failure. After success, the instance #deleted?.

Returns:

  • (Array<ModraUp::Entity>, false)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
bits = inst.explode
inst.deleted?  #=> true

instance.make_unique ⇒ ModraUp::ComponentInstance

If there are other instances sharing the same definition, make a copy of the definition and only point this instance to it. If it is already unique, perform no operation.

Returns:

  • (ModraUp::ComponentInstance)

Raises:

  • [RuntimeError] The instance has been deleted.

Examples:

ents = ModraUp.active_model.entities
defn = ModraUp.active_model.definitions.add("Box")
a = ents.add_instance(defn)
b = ents.add_instance(defn)
a.make_unique
a.definition == b.definition  #=> false

instance.scaled_size ⇒ Array<Numeric>

The scaled size of the definition bounding box of an applied instance (width, height, depth).

Returns:

  • (Array<Numeric>)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.scaled_size.length  #=> 3

instance.local_bounds ⇒ Geom::BoundingBox

Define the local bounding box, excluding the instance transformation.

Returns:

  • (Geom::BoundingBox)

Examples:

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

instance.entities ⇒ ModraUp::Entities

The drawing context within the instance definition. Changing this will affect all instances sharing the same definition, but will not #make_unique.

Returns:

  • (ModraUp::Entities)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.entities.add_line([0, 0, 0], [1, 0, 0])

instance.add_observer(observer) ⇒ Boolean

Register a ModraUp::InstanceObserver. Adding the same object a second time returns false.

Parameters:

  • observer (ModraUp::InstanceObserver)

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
inst.add_observer(ModraUp::InstanceObserver.new)

instance.remove_observer(observer) ⇒ Boolean

Remove the previously registered instance observer.

Parameters:

  • observer (ModraUp::InstanceObserver)

Returns:

  • (Boolean)

Examples:

defn = ModraUp.active_model.definitions.add("Box")
inst = ModraUp.active_model.entities.add_instance(defn)
obs = ModraUp::InstanceObserver.new
inst.add_observer(obs)
inst.remove_observer(obs)

instance.guid ⇒ String

The unique base64 id of the instance placement (SKP VFF 6819). Will be an empty string if not imported.

Returns:

  • (String)

Examples:

inst = ModraUp.active_model.entities.add_instance(ModraUp.active_model.definitions.add("Box"))
inst.guid.is_a?(String)  #=> true

instance.manifold? ⇒ Boolean

Deprecated: Same as ModraUp::ComponentDefinition#manifold?, only checks the definition not the transformation.

Returns:

  • (Boolean)

Examples:

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

instance.volume ⇒ Float

Returns the volume in cubic inches. The value is positive for a manifold solid and negative for non-manifold geometry.

Returns:

  • (Float)

Examples:

defn = ModraUp.active_model.definitions.add("Cube")
defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]).pushpull(10)
inst = ModraUp.active_model.entities.add_instance(defn)
inst.volume > 0  #=> true

instance.move!(transformation) ⇒ Boolean

Sets the transformation, similar to #transformation= but does not record it in the undo stack (for animations/scene transitions).

Parameters:

  • transformation (Geom::Transformation)

Returns:

  • (Boolean)

Examples:

inst = ModraUp.active_model.entities.add_instance(ModraUp.active_model.definitions.add("Box"))
inst.move!(Geom::Transformation.translation(1, 0, 0))  #=> true

instance.subtract(other) ⇒ ModraUp::Group, nil

Subtract this instance from other (other - this instance). The result is always a new ModraUp::Group (geometrically baked into the parent space). Both must be manifold shells in the same parent ModraUp::Entities. On success, both are deleted. Returns nil if either is not a manifold, the result is empty, locked, deleted, or not in the same parent context; the model remains unchanged.

Parameters:

  • other (ModraUp::Group, ModraUp::ComponentInstance)

Returns:

  • (ModraUp::Group, nil)

Raises:

  • [TypeError] other is not a group or component instance

Examples:

model = ModraUp.active_model
defn = model.definitions.add("Cube")
defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]).pushpull(10)
a = model.entities.add_instance(defn)
b = model.entities.add_instance(defn, Geom::Transformation.translation(5, 0, 0))
a.subtract(b).is_a?(ModraUp::Group)  #=> true

instance.trim(other) ⇒ ModraUp::Group, nil

Cut the instance with another instance. The current instance remains; another instance is replaced by another instance minus the current instance.

Failure conditions are the same as for ##subtract.

Parameters:

  • other (ModraUp::Group, ModraUp::ComponentInstance)

Returns:

  • (ModraUp::Group, nil)

Raises:

  • [TypeError] other is not a group or component instance

Examples:

model = ModraUp.active_model
defn = model.definitions.add("Cutter")
defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]).pushpull(10)
cutter = model.entities.add_instance(defn)
target = model.entities.add_instance(defn, Geom::Transformation.translation(5, 0, 0))
cutter.trim(target).is_a?(ModraUp::Group)  #=> true

instance.union(other) ⇒ ModraUp::Group, nil

The union of this instance with other results in a new ModraUp::Group. It succeeds only if both instances are valid and not nil. Upon success, both original instances are deleted. Failure conditions are the same as for #subtract.

Parameters:

  • other (ModraUp::Group, ModraUp::ComponentInstance)

Returns:

  • (ModraUp::Group, nil)

Raises:

  • [TypeError] other is not a group or component instance

Examples:

model = ModraUp.active_model
defn = model.definitions.add("Cube")
defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]).pushpull(10)
a = model.entities.add_instance(defn)
b = model.entities.add_instance(defn, Geom::Transformation.translation(5, 0, 0))
a.union(b).is_a?(ModraUp::Group)  #=> true

instance.intersect(other) ⇒ ModraUp::Group, nil

Returns the intersection volume between this instance and other. The result is a new group in the parent context with geometry baked into the parent space. Both sides must be manifold shells in the same parent ModraUp::Entities. Both sides are deleted on success. Returns nil if they do not intersect, are non-manifold, locked, deleted, or not in the same parent context; the model remains unchanged.

Parameters:

  • other (ModraUp::Group, ModraUp::ComponentInstance)

Returns:

  • (ModraUp::Group, nil)

Raises:

  • [TypeError] other is not a group or component instance

Examples:

model = ModraUp.active_model
defn = model.definitions.add("Cube")
defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]).pushpull(10)
a = model.entities.add_instance(defn)
b = model.entities.add_instance(defn, Geom::Transformation.translation(5, 0, 0))
a.intersect(b).is_a?(ModraUp::Group)  #=> true

instance.outer_shell(other) ⇒ ModraUp::Group, nil

The outer surface of two manifold shells (volume union, internal faces removed). The result is a new group in the parent context. Both are deleted if successful. Solids do not need to intersect: if they are completely separate, both shells remain in the resulting group; if one completely encloses the other, only the outer shell remains. Failure conditions are the same as for #intersect.

Parameters:

  • other (ModraUp::Group, ModraUp::ComponentInstance)

Returns:

  • (ModraUp::Group, nil)

Raises:

  • [TypeError] other is not a group or component instance

Examples:

model = ModraUp.active_model
defn = model.definitions.add("Cube")
defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]).pushpull(10)
a = model.entities.add_instance(defn)
b = model.entities.add_instance(defn, Geom::Transformation.translation(5, 0, 0))
a.outer_shell(b).is_a?(ModraUp::Group)  #=> true

instance.split(other) ⇒ Array(ModraUp::Group, ModraUp::Group, ModraUp::Group), nil

Splits two manifold solids at their intersection. Returns three new groups on Layer0 in this order: the part of other outside the receiver (Difference2), the part of the receiver outside other (Difference1), and their shared volume (Intersection). The operation removes the original objects. If the volumes do not overlap, the intersection group is empty and the difference groups contain the original solids. For a non-manifold input or a failed operation, returns nil and leaves the model unchanged.

Parameters:

  • other (ModraUp::Group, ModraUp::ComponentInstance)

Returns:

  • (Array(ModraUp::Group, ModraUp::Group, ModraUp::Group), nil)

Raises:

  • [TypeError] other is not a group or component instance

Examples:

model = ModraUp.active_model
defn = model.definitions.add("Cube")
defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]).pushpull(10)
a = model.entities.add_instance(defn)
b = model.entities.add_instance(defn, Geom::Transformation.translation(5, 0, 0))
diff2, diff1, inter = a.split(b)
inter.name  #=> "Intersection"

instance.show_differences(other) ⇒ Booleaninstance.show_differences(other, verbose) ⇒ Boolean

Compares the volumes of two manifold solids in their shared parent space. Returns true for equal volumes in the same location, otherwise false. Both containers move to Layer0. Coincident faces and edges go on a layer ending in _same and remaining geometry on a layer ending in _diff. The prefix is the group instance name (or its definition name if empty), or the component definition name. With verbose enabled, the console lists the centers of differing faces. A non-manifold, locked, deleted, or differently parented input returns false without changing the model. Shared definitions are made unique first, preserving other instances.

Parameters:

  • other (ModraUp::Group, ModraUp::ComponentInstance)
  • verbose (Boolean) Does printing the differences to the console default to false?

Returns:

  • (Boolean)

Raises:

  • [TypeError] other is not a group or component instance
Note: ModraUp extension. Shared definitions will be uniquified to avoid affecting other instances.

Examples:

model = ModraUp.active_model
defn = model.definitions.add("Cube")
defn.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]).pushpull(10)
a = model.entities.add_instance(defn)
b = model.entities.add_instance(defn)
a.show_differences(b, false)  #=> true

#hatch_pattern ⇒ ModraUp::HatchPattern, nil

Read the hatch pattern of this group/component itself. It also applies to nested groups and components, taking precedence over material patterns.

Returns:

  • (ModraUp::HatchPattern, nil)

Examples:

group = ModraUp.active_model.entities.grep(ModraUp::ComponentInstance).first
group.hatch_pattern if group

#hatch_pattern=(hatch_pattern) ⇒ ModraUp::HatchPattern, nil

Sets the hatch pattern for this group/component itself, no material assignment needed; nil removes the pattern.

Parameters:

  • hatch_pattern (ModraUp::HatchPattern, nil) The hatch pattern of this model.

Returns:

  • (ModraUp::HatchPattern, nil)

Raises:

  • [ArgumentError] The hatch pattern belongs to another model.

Examples:

model = ModraUp.active_model
group = model.entities.grep(ModraUp::ComponentInstance).first
group.hatch_pattern = model.hatch_patterns.first if group