Module: Autocad::ModelTrait

Included in:
ModelSpace, PaperSpace
Defined in:
lib/autocad/model.rb

Instance Method Summary collapse

Instance Method Details

#add_arc(center, radius, start_angle, end_angle) ⇒ Object

Add an arc to the model

Raises:



84
85
86
87
88
89
90
# File 'lib/autocad/model.rb', line 84

def add_arc(center, radius, start_angle, end_angle)
  pt = Point3d(center)
  ole_arc = ole_obj.AddArc(pt.to_ole, radius, start_angle, end_angle)
  app.wrap(ole_arc)
rescue StandardError => e
  raise Autocad::Error.new("Error adding arc #{e}")
end

#add_circle(center, radius) ⇒ Object

Add a circle to the model

Raises:



97
98
99
100
101
102
103
# File 'lib/autocad/model.rb', line 97

def add_circle(center, radius)
  pt = Point3d(center)
  ole_circle = ole_obj.AddCircle(pt.to_ole, radius)
  app.wrap(ole_circle)
rescue StandardError => e
  raise Autocad::Error.new("Error adding circle #{e.message}")
end

#add_ellipse(center, major_axis, radius_ratio) ⇒ Object

Add an ellipse to the model

Raises:



127
128
129
130
131
132
133
# File 'lib/autocad/model.rb', line 127

def add_ellipse(center, major_axis, radius_ratio)
  pt = Point3d(center)
  ole_ellipse = ole_obj.AddEllipse(pt.to_ole, major_axis, radius_ratio)
  app.wrap(ole_ellipse)
rescue StandardError => e
  raise Autocad::Error.new("Error adding ellipse #{e}")
end

#add_line(pt1, pt2, layer: nil) ⇒ Object

Add a line to the model



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/autocad/model.rb', line 56

def add_line(pt1, pt2, layer: nil)
  pt1 = Point3d.new(pt1)
  pt2 = Point3d.new(pt2)
  ole_line = ole_obj.AddLine(pt1.to_ole, pt2.to_ole)
  if layer
    layer_obj = create_layer(layer)
    ole_line.Layer = layer_obj.name
  end
  app.wrap(ole_line)
rescue StandardError => e
  puts e.message
  nil
end

#add_rectangle(lower_left, upper_right) ⇒ Object

Add a rectangular polyline to the model

Raises:



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

def add_rectangle(lower_left, upper_right)
  x1, y1 = Point3d(lower_left).to_xy
  x2, y2 = Point3d(upper_right).to_xy
  pts = [x1, y1, x2, y1, x2, y2, x1, y2, x1, y1]
  pts_variant = WIN32OLE::Variant.new(pts, WIN32OLE::VARIANT::VT_ARRAY | WIN32OLE::VARIANT::VT_R8)
  ole = ole_obj.AddLightweightPolyline(pts_variant)
  app.wrap(ole)
rescue StandardError => e
  raise Autocad::Error.new("Error adding rectangle #{e}")
end

#add_spline(points, start_tangent, end_tangent) ⇒ Object

Add a spline to the model

Raises:



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/autocad/model.rb', line 141

def add_spline(points, start_tangent, end_tangent)
  pts = Point3d.pts_to_array(points)
  pts_variant = Point3d.array_to_ole(pts)

  start_tangent_ole = Point3d.new(start_tangent).to_ole
  end_tangent_ole = Point3d.new(end_tangent).to_ole

  ole = ole_obj.AddSpline(pts_variant, start_tangent_ole, end_tangent_ole)
  app.wrap(ole)
rescue StandardError => e
  raise Autocad::Error.new("Error adding spline #{e.message}")
end

#attach_external_reference(path, name:, pt: [0, 0, 0], x_scale: 1.0, y_scale: 1.0, z_scale: 1.0, rotation: 0.0, overlay: false, password: nil) ⇒ Object

Attach an external reference to the model



40
41
42
43
44
45
46
47
48
49
# File 'lib/autocad/model.rb', line 40

def attach_external_reference(path, name:, pt: [0, 0, 0], x_scale: 1.0, y_scale: 1.0, z_scale: 1.0, rotation: 0.0,
                              overlay: false, password: nil)
  windows_path = app.windows_path(path)
  pt3d = Point3d.new(insertion_point)
  ole_reference = ole_obj.AttachExternalReference(windows_path, name, pt3d.to_ole, x_scale, y_scale, z_scale,
                                                  rotation, overlay, password)
  app.wrap(ole_reference)
rescue StandardError => e
  app.error_proc.call(e, self)
end

#drawingObject

Get the parent drawing document



156
157
158
# File 'lib/autocad/model.rb', line 156

def drawing
  @drawing ||= ::Autocad::Drawing.from_ole_obj(app, ole_obj.Document)
end

#eachObject

Iterate through all elements in the model space



13
14
15
16
17
18
19
# File 'lib/autocad/model.rb', line 13

def each
  return enum_for(:each) unless block_given?

  @ole_obj.each do |ole|
    yield app.wrap(ole)
  end
end

#layoutObject

Get the layout associated with this model



72
73
74
75
# File 'lib/autocad/model.rb', line 72

def layout
  ole_layout = ole_obj.Layout
  app.wrap(ole_layout)
end

#xref?Boolean

Check if this model is an external reference

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/autocad/model.rb', line 23

def xref?
  @ole_obj.IsXRef
rescue StandardError
  false
end