Class: Autocad::SelectionSetAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/autocad/selection_set_adapter.rb

Overview

Manages AutoCAD selection set operations and OLE integration

Executes selections using stored criteria and provides access to selected entities. Bridges between Ruby selection criteria and AutoCAD’s native selection mechanisms.

Key Features:

  • Multiple selection methods (window, crossing, fence, point)

  • Filter-based entity selection

  • Enumeration of selected entities

  • OLE integration with AutoCAD

Examples:

Create and use a selection set

ss = drawing.create_selection_set("walls")
ss.filter { |f| f.layer("WALLS") }
ss.select(mode: :all)
ss.each { |wall| wall.color = 1 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(drawing, selection_set, ole = nil) ⇒ SelectionSetAdapter

Initialize new adapter

Parameters:

  • drawing (Drawing)

    Parent drawing document

  • selection_set (SelectionSet)

    Selection criteria container

  • ole (WIN32OLE, nil) (defaults to: nil)

    Optional existing OLE selection set



44
45
46
47
48
# File 'lib/autocad/selection_set_adapter.rb', line 44

def initialize(drawing, selection_set, ole = nil)
  @drawing = drawing
  @selection_set = selection_set
  @ole_obj = ole || create_selection_set
end

Instance Attribute Details

#drawingObject (readonly)

Returns the value of attribute drawing.



34
35
36
# File 'lib/autocad/selection_set_adapter.rb', line 34

def drawing
  @drawing
end

#ole_objObject (readonly)

Returns the value of attribute ole_obj.



34
35
36
# File 'lib/autocad/selection_set_adapter.rb', line 34

def ole_obj
  @ole_obj
end

#selection_setObject (readonly)

Returns the value of attribute selection_set.



34
35
36
# File 'lib/autocad/selection_set_adapter.rb', line 34

def selection_set
  @selection_set
end

Class Method Details

.from_ole_obj(drawing, ole) ⇒ SelectionSetAdapter

Initialize from OLE object

Parameters:

  • drawing (Drawing)

    Parent drawing document

  • ole (WIN32OLE)

    AutoCAD OLE selection set object

Returns:



26
27
28
29
# File 'lib/autocad/selection_set_adapter.rb', line 26

def self.from_ole_obj(drawing, ole)
  ss = SelectionSet.new(ole.Name)
  new(drawing, ss, ole)
end

Instance Method Details

#appAutocad::App

Get the application instance

Returns:



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

def app
  @drawing.app
end

#clearvoid

This method returns an undefined value.

Clear selection set contents

Examples:

Clear before new selection

ss.clear
ss.select_on_screen


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

def clear
  ole_obj.Clear
end

#clear_filtervoid

This method returns an undefined value.

Clear filter criteria

Examples:

Reset filters for new selection

ss.clear_filter
ss.filter { |f| f.type("LINE") }


94
95
96
# File 'lib/autocad/selection_set_adapter.rb', line 94

def clear_filter
  selection_set.clear_filter
end

#countInteger

Get item count in selection set

Examples:

Report selection count

puts "Selected #{ss.count} entities"

Returns:

  • (Integer)

    Number of selected entities



74
75
76
# File 'lib/autocad/selection_set_adapter.rb', line 74

def count
  ole_obj.Count
end

#deletevoid

This method returns an undefined value.

Delete the selection set from AutoCAD

Examples:

Remove selection set when no longer needed

ss.delete if ss.count == 0


55
56
57
58
# File 'lib/autocad/selection_set_adapter.rb', line 55

def delete
  @ole_obj.Delete
  @selection_set = nil
end

#each {|Element| ... } ⇒ Enumerator<Element>

Iterate through selected entities

Examples:

Process each selected entity

ss.each { |entity| entity.color = 1 }

Convert to array

entities = ss.each.to_a

Yields:

  • (Element)

    Block to process each entity

Returns:



136
137
138
139
140
# File 'lib/autocad/selection_set_adapter.rb', line 136

def each
  return to_enum(__callee__) unless block_given?

  ole_obj.each { |o| yield app.wrap(o) }
end

#filter {|Filter| ... } ⇒ void

This method returns an undefined value.

Configure filter through block

Examples:

Create complex filter

ss.filter do |f|
  f.and(f.layer("WALLS"), f.or(f.type("LINE"), f.type("POLYLINE")))
end

Yields:

  • (Filter)

    Block for building filter criteria



290
291
292
# File 'lib/autocad/selection_set_adapter.rb', line 290

def filter(&)
  @selection_set.filter(&)
end

#filter_text(str) ⇒ void

This method returns an undefined value.

Set text content filter

Examples:

Filter for text with specific content

ss.filter_text("Revision")

Parameters:

  • str (String)

    Text pattern to match



105
106
107
# File 'lib/autocad/selection_set_adapter.rb', line 105

def filter_text(str)
  @selection_set.filter_text(str)
end

#filter_text_containing(str) ⇒ void

This method returns an undefined value.

Filter text containing substring

Examples:

Find text containing “NOTE”

ss.filter_text_containing("NOTE")

Parameters:

  • str (String)

    Substring to match



116
117
118
# File 'lib/autocad/selection_set_adapter.rb', line 116

def filter_text_containing(str)
  @selection_set.filter_text_containing(str)
end

#filter_typesArray<Integer>

Get filter types from selection set

Examples:

Access raw filter data

puts "Using filter types: #{ss.filter_types.join(', ')}"

Returns:

  • (Array<Integer>)

    Array of DXF group codes



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

def filter_types
  @selection_set.filter_types
end

#filter_valuesArray<Object>

Get filter values from selection set

Examples:

Access raw filter data

puts "Using filter values: #{ss.filter_values.inspect}"

Returns:

  • (Array<Object>)

    Array of filter values



165
166
167
# File 'lib/autocad/selection_set_adapter.rb', line 165

def filter_values
  @selection_set.filter_values
end

#has_items?Boolean

Checks if the selection set has any items

Examples:

Skip processing if selection is empty

next unless ss.has_items?

Returns:

  • (Boolean)

    True if selection contains entities



65
66
67
# File 'lib/autocad/selection_set_adapter.rb', line 65

def has_items?
  ole_obj.Count > 0
end

#nameString

Get the name of the selection set

Examples:

Get selection set identifier

puts "Working with selection set: #{ss.name}"

Returns:

  • (String)

    The selection set name



147
148
149
# File 'lib/autocad/selection_set_adapter.rb', line 147

def name
  @ole_obj.Name
end

#select(mode: :all, pt1: nil, pt2: nil, ft: ole_filter_types, fv: ole_filter_values) ⇒ void

This method returns an undefined value.

Select entities using various methods

Examples:

Select all entities matching filter

ss.select(mode: :all)

Window selection

ss.select(mode: :window, pt1: [0,0,0], pt2: [10,10,0])

Parameters:

  • mode (Symbol) (defaults to: :all)

    Selection mode (:all, :window, :crossing, :previous, :last)

  • pt1 (Point3d, nil) (defaults to: nil)

    First point for window selection

  • pt2 (Point3d, nil) (defaults to: nil)

    Second point for window selection

  • ft (WIN32OLE::Variant, nil) (defaults to: ole_filter_types)

    Optional filter types variant

  • fv (WIN32OLE::Variant, nil) (defaults to: ole_filter_values)

    Optional filter values variant



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/autocad/selection_set_adapter.rb', line 206

def select(mode: :all, pt1: nil, pt2: nil, ft: ole_filter_types, fv: ole_filter_values)
  acad_mode = case mode
  when :all
    ACAD::AcSelectionSetAll
  when :window
    ACAD::AcSelectionSetWindow
  when :crossing
    ACAD::AcSelectionSetCrossing
  when :previous
    ACAD::AcSelectionSetPrevious
  when :last
    ACAD::AcSelectionSetLast
  else
    Acad::AcSelectionSetAll
  end
  @ole_obj.Select(acad_mode, nil, nil, ft, fv)
rescue => ex
  binding.irb
end

#select_at_point(x, y) ⇒ void

This method returns an undefined value.

Select entities at specific point

Examples:

Select at specific point

ss.select_at_point(10, 20)

Select at point object

ss.select_at_point(Point3d.new(10, 20, 0))

Parameters:

  • x (Numeric, Point3d, Array<Numeric>)

    X coordinate or point object

  • y (Numeric, nil)

    Y coordinate (if using separate coordinates)



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/autocad/selection_set_adapter.rb', line 237

def select_at_point(x, y)
  point = if x.respond_to?(:x) && x.respond_to?(:y)
    # Handle Point3d object
    [x.x, x.y]
  elsif x.is_a?(Array)
    # Handle array input
    x
  else
    # Handle separate x,y coordinates
    [x, y]
  end

  @ole_obj.SelectAtPoint(point, filter_types, filter_values)
end

#select_by_polygon(points: [], mode: :fence) ⇒ void

This method returns an undefined value.

Select entities using polygonal fence

Examples:

Select using fence

points = [[0,0], [10,0], [10,10], [0,10]]
ss.select_by_polygon(points: points, mode: :fence)

Parameters:

  • points (Array<Point3d>) (defaults to: [])

    Polygon vertices

  • mode (Symbol) (defaults to: :fence)

    Selection mode (:fence, :window, :crossing)

Raises:

  • (ArgumentError)

    For invalid mode

  • (ArgumentError)

    For invalid mode



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/autocad/selection_set_adapter.rb', line 264

def select_by_polygon(points: [], mode: :fence)
  # Convert points to arrays of coordinates
  point_coords = points.map { |p| Point3d(p) }.map { |p| [p.x, p.y, p.z] }.flatten

  mode_code = case mode
  when :fence then 5
  when :window then 3
  when :crossing then 4
  else
    raise ArgumentError, "Invalid selection mode: #{mode}. Must be :fence, :window, or :crossing"
  end
  ole_point_coords = WIN32OLE::Variant.new(point_coords, WIN32OLE::VARIANT::VT_ARRAY | WIN32OLE::VARIANT::VT_R8)

  binding.irb
  @ole_obj.SelectByPolygon(mode_code, ole_point_coords, filter_types, filter_values)
end

#select_on_screen(ft = ole_filter_types, fv = ole_filter_values) ⇒ void

This method returns an undefined value.

Select entities interactively on screen

Examples:

Select entities with user interaction

ss.select_on_screen

Select with predefined filters

ss.filter { |f| f.type("CIRCLE") }
ss.select_on_screen

Parameters:

  • ft (WIN32OLE::Variant, nil) (defaults to: ole_filter_types)

    Optional filter types variant

  • fv (WIN32OLE::Variant, nil) (defaults to: ole_filter_values)

    Optional filter values variant



181
182
183
184
185
186
187
# File 'lib/autocad/selection_set_adapter.rb', line 181

def select_on_screen(ft = ole_filter_types, fv = ole_filter_values)
  if filter_types.empty? && filter_values.empty?
    @ole_obj.SelectOnScreen(nil, nil)
  else
    @ole_obj.SelectOnScreen(ft, fv)
  end
end