Class: Autocad::MText

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

Overview

Multi-line formatted text container in AutoCAD

Represents a rich text entity with multiple lines and formatting. Supports Liquid templates, table cell awareness, and line-by-line operations.

Examples:

Create and render a template

specs = drawing.model.add_mtext("Project: {{name}}")
specs.render(name: "Tower A")

Instance Attribute Summary collapse

Attributes inherited from Element

#acad_type, #app

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?, #bounds, #cell?, #def, #drawing, #explode, #graphical?, #has_tags?, #highlight, #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



167
168
169
170
171
172
173
174
175
176
# File 'lib/autocad/mtext.rb', line 167

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

Instance Attribute Details

#ole_objObject (readonly)

Returns the value of attribute ole_obj.



15
16
17
# File 'lib/autocad/mtext.rb', line 15

def ole_obj
  @ole_obj
end

#originalObject (readonly)

Returns the value of attribute original.



15
16
17
# File 'lib/autocad/mtext.rb', line 15

def original
  @original
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



132
133
134
# File 'lib/autocad/mtext.rb', line 132

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

#empty?Boolean

Check for empty content

Returns:

  • (Boolean)

    True if the MText has no content



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

def empty?
  ole_obj.TextLinesCount == 0
end

#mtext?Boolean

Type check for multi-line text (always true)

Returns:

  • (Boolean)

    Always returns true for MText objects



37
38
39
# File 'lib/autocad/mtext.rb', line 37

def mtext?
  true
end

#read_ole(ole) ⇒ String

Read content from OLE object

Parameters:

  • ole (WIN32OLE)

    The OLE object to read from

Returns:

  • (String)

    The text content



62
63
64
# File 'lib/autocad/mtext.rb', line 62

def read_ole(ole)
  ole.TextString
end

#render(h = {}) ⇒ self

Render Liquid template with provided context

Examples:

Render a template with variables

mtext.render(client: "ABC Corp", floors: 42)

Parameters:

  • h (Hash) (defaults to: {})

    The template variables

Returns:

  • (self)

    The MText object



151
152
153
154
155
156
157
158
# File 'lib/autocad/mtext.rb', line 151

def render(h = {})
  return self unless template?

  template = Liquid::Template.parse(to_s)
  result = template.render(h)
  update(result) unless result == @original
  self
end

#sizeInteger

Get number of text lines

Returns:

  • (Integer)

    The number of lines in the MText



53
54
55
# File 'lib/autocad/mtext.rb', line 53

def size
  ole_obj.TextLinesCount
end

#template?Boolean

Check for Liquid template placeholders

Returns:

  • (Boolean)

    True if the text contains template placeholders



140
141
142
# File 'lib/autocad/mtext.rb', line 140

def template?
  !!(@original =~ /{{.+}}/)
end

#text?Boolean

Override text type check (always false)

Returns:

  • (Boolean)

    Always returns false for MText objects



45
46
47
# File 'lib/autocad/mtext.rb', line 45

def text?
  false
end

#to_regexpRegexp

Convert content to regex pattern

Returns:

  • (Regexp)

    A regular expression based on the text content



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

def to_regexp
  Regexp.new(original.to_s)
end

#to_sString

Get content as string

Returns:

  • (String)

    The text content



123
124
125
# File 'lib/autocad/mtext.rb', line 123

def to_s
  @original.to_s
end

#update_ole!(text) ⇒ void

This method returns an undefined value.

Full content rewrite with line-by-line updates

Parameters:

  • text (String)

    The new text content



110
111
112
113
114
115
116
117
# File 'lib/autocad/mtext.rb', line 110

def update_ole!(text)
  ole_obj.DeleteAllTextLines
  text.each_line do |line|
    ole_obj.AddTextLine(line)
  end
  ole_obj.Redraw Autocad::MSD::MsdDrawingModeNormal
  ole_obj.Rewrite
end

#write_ole(text) ⇒ void

This method returns an undefined value.

Update text content with cell awareness

Parameters:

  • text (String)

    The new text content



71
72
73
74
75
76
77
# File 'lib/autocad/mtext.rb', line 71

def write_ole(text)
  if in_cell?
    write_ole_in_cell(text)
  else
    write_ole_regular(text)
  end
end

#write_ole_in_cell(text) ⇒ void

This method returns an undefined value.

Update text content for MText in table cell

Parameters:

  • text (String)

    The new text content



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/autocad/mtext.rb', line 93

def write_ole_in_cell(text)
  orig_ole = ole_obj
  new_text_ole = ole_obj.Clone
  new_text_ole.DeleteAllTextLines
  text.each_line do |line|
    new_text_ole.AddTextLine(line)
  end
  @ole_obj = new_text_ole
rescue
  @ole_obj = orig_ole
end

#write_ole_regular(text) ⇒ void

This method returns an undefined value.

Update text content for regular MText

Parameters:

  • text (String)

    The new text content



84
85
86
# File 'lib/autocad/mtext.rb', line 84

def write_ole_regular(text)
  ole_obj.TextString = text
end