Class: Autocad::Text

Inherits:
Element show all
Defined in:
lib/autocad/text.rb

Overview

Single-line text annotation element in AutoCAD

Represents a simple text entity with a single line of content. Provides methods for reading, writing, and manipulating text properties.

Examples:

Create and modify text

text = drawing.model.add_text("Label", [10,5,0])
text.height = 2.5
text.update("New Label")

Instance Attribute Summary

Attributes inherited from Element

#acad_type, #app, #ole_obj, #original

Instance Method Summary collapse

Methods inherited from Element

#[], #app_ole_obj, #clone, convert_item, #delete, #do_update, #each_complex, #get_property_handler, #in_cell?, #initialize, #move, #move_ole, #move_x, #move_y, #ole_cell, ole_object?, #property_handler, #redraw, #update, #updated?

Methods included from ElementTrait

#autocad_type, #block_reference?, #cell?, #def, #drawing, #explode, #graphical?, #has_tags?, #id_from_record, #inspect, #line?, #model, #parent, #pviewport?, #select, #to_ole, #visible?

Constructor Details

This class inherits a constructor from Autocad::Element

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object

Delegate uppercase methods to OLE object, lowercase to string methods

Parameters:

  • meth (Symbol)

    The method name

  • * (Array)

    Method arguments

  • & (Proc)

    Optional block

Returns:

  • (Object)

    Result of the method call



111
112
113
114
115
116
117
118
119
120
# File 'lib/autocad/text.rb', line 111

def method_missing(meth, *, &)
  if /^[A-Z]/.match?(meth)
    ole_obj.send(meth, *)
  else
    dup = @original.dup
    result = dup.send(meth, *, &)
    update(result)
    result
  end
end

Instance Method Details

#=~(reg) ⇒ Integer?

Regex pattern matching against text content

Parameters:

  • reg (Regexp)

    The regular expression to match against

Returns:

  • (Integer, nil)

    Match position or nil if no match



67
68
69
# File 'lib/autocad/text.rb', line 67

def =~(reg)
  @original =~ reg
end

#boundsBoundingBox

Note:

Contains debug code - use with caution

Experimental bounding box calculation

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/autocad/text.rb', line 84

def bounds
  binding.break
  rotation = ole_obj.Rotation
  begin
    app_ole_obj.Matrix3dInverse(rotation)
  rescue
    binding.irb
  end
  transform = app_ole_obj.Transform3dFromMatrix3dandFixedPoint3d(app_ole_obj.Matrix3dInverse(rotation),
    ole_obj.origin)
  ole_obj.transform transform

  0.upto(4) do |i|
    points[i] = ole_obj.Boundary.Low
  end
  points[2] = self.Boundary.High
  points[1].X = points[2].x
  points[3].y = points[2].Y
end

#highlight(flag = true) ⇒ void

This method returns an undefined value.

Toggle text highlighting in the drawing

Examples:

Highlight selected text

selected_text.each { |t| t.highlight }

Parameters:

  • flag (Boolean) (defaults to: true)

    Whether to highlight (true) or unhighlight (false)



57
58
59
60
# File 'lib/autocad/text.rb', line 57

def highlight(flag = true)
  ole_obj.Highlight(flag)
  ole_obj.Update
end

#read_ole(_ole) ⇒ String

Read text content from OLE object

Parameters:

  • _ole (WIN32OLE)

    The OLE object to read from (unused parameter)

Returns:

  • (String)

    The text content



21
22
23
# File 'lib/autocad/text.rb', line 21

def read_ole(_ole)
  ole_obj.TextString
end

#text?Boolean

Confirm this is a text element (always true)

Returns:

  • (Boolean)

    Always returns true for Text objects



29
30
31
# File 'lib/autocad/text.rb', line 29

def text?
  true
end

#to_regexpRegexp

Convert text content to regex pattern

Returns:

  • (Regexp)

    A regular expression based on the text content



46
47
48
# File 'lib/autocad/text.rb', line 46

def to_regexp
  Regexp.new(read_ole.to_s)
end

#to_sString

Get text content as string

Returns:

  • (String)

    The text content



75
76
77
# File 'lib/autocad/text.rb', line 75

def to_s
  original.to_s
end

#write_ole(text) ⇒ void

This method returns an undefined value.

Update text content in AutoCAD

Parameters:

  • text (String)

    The new text content



38
39
40
# File 'lib/autocad/text.rb', line 38

def write_ole(text)
  ole_obj.TextString = text
end