Class: Autocad::SelectionFilter

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

Overview

Builds AutoCAD selection filter criteria using group codes

Provides a fluent interface for creating complex entity filters based on AutoCAD DXF group codes and logical operations.

Key Features:

  • Logical operators (AND, OR, NOT, XOR)

  • Comparison operators (>, <, =, !=)

  • Entity property filters (type, layer, color)

  • Text content matching

  • Space-specific filtering (model/paper)

Examples:

Create a filter for red circles

filter = SelectionFilter.new
  .and(
    SelectionFilter.new.type("CIRCLE"),
    SelectionFilter.new.color(1)
  )

Complex filter with nested conditions

filter = SelectionFilter.new
  .and(
    SelectionFilter.new.layer("WALLS"),
    SelectionFilter.new.or(
      SelectionFilter.new.type("LINE"),
      SelectionFilter.new.type("POLYLINE")
    )
  )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

Initialize empty filter



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

def initialize
  @types = []
  @values = []
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



33
34
35
# File 'lib/autocad/selection_filter.rb', line 33

def types
  @types
end

#valuesObject (readonly)

Returns the value of attribute values.



33
34
35
# File 'lib/autocad/selection_filter.rb', line 33

def values
  @values
end

Instance Method Details

#and(*conditions) ⇒ self

Logical AND combination of filters

Examples:

Combine multiple conditions with AND

filter.and(
  SelectionFilter.new.type("LINE"),
  SelectionFilter.new.layer("WALLS")
)

Parameters:

Returns:

  • (self)

    The filter for chaining



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/autocad/selection_filter.rb', line 62

def and(*conditions)
  return self if conditions.empty?

  @types << -4
  @values << "<AND"

  conditions.each do |condition|
    @types.concat(condition.types)
    @values.concat(condition.values)
  end

  @types << -4
  @values << "AND>"

  self
end

#block_reference(name = nil) ⇒ self

Filter by block references

Examples:

Find any block reference

filter.block_reference

Find specific block

filter.block_reference("DOOR")

Parameters:

  • name (String, nil) (defaults to: nil)

    Optional block name pattern

Returns:

  • (self)

    The filter for chaining



225
226
227
228
229
230
231
# File 'lib/autocad/selection_filter.rb', line 225

def block_reference(name = nil)
  # return unless name

  @types << 0
  @values << "INSERT"
  self
end

#color(num) ⇒ self

Filter by color index

Examples:

Filter for red entities (color 1)

filter.color(1)

Parameters:

  • num (Integer)

    AutoCAD color number (0-256)

Returns:

  • (self)

    The filter for chaining



294
295
296
297
298
# File 'lib/autocad/selection_filter.rb', line 294

def color(num)
  @types << 62 # Color number filter
  @values << num
  self
end

#contains(str) ⇒ self

Filter text content using wildcards

Examples:

Find text containing “REVISION”

filter.contains("*REVISION*")

Parameters:

  • str (String)

    Search pattern (e.g., “REV”)

Returns:

  • (self)

    The filter for chaining



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/autocad/selection_filter.rb', line 329

def contains(str)
  @types << -4
  @values << "<OR"

  # Filter for TEXT
  @types << 1 # Text string group code for TEXT
  @values << "*#{str}*"

  # Filter for MTEXT
  @types << 1 # Text string group code for MTEXT
  @values << "*#{str}*"

  @types << -4
  @values << "OR>"

  self
end

#equal_to(value) ⇒ self

Equal to comparison

Examples:

Find circles with radius = 7.5

filter.type("CIRCLE").equal_to(7.5)

Parameters:

  • value (Numeric)

    Comparison value

Returns:

  • (self)

    The filter for chaining



193
194
195
196
197
198
199
# File 'lib/autocad/selection_filter.rb', line 193

def equal_to(value)
  @types << -4
  @values << "="
  @types << 40 # floating point
  @values << value
  self
end

#greater_than(value) ⇒ self

Greater than comparison

Examples:

Find circles with radius > 5

filter.type("CIRCLE").greater_than(5)

Parameters:

  • value (Numeric)

    Comparison value

Returns:

  • (self)

    The filter for chaining



163
164
165
166
167
168
169
# File 'lib/autocad/selection_filter.rb', line 163

def greater_than(value)
  @types << -4
  @values << ">="
  @types << 40 # floating point
  @values << value
  self
end

#has_filters?Boolean

Check if filter has any conditions

Examples:

Skip empty filter

next unless filter.has_filters?

Returns:

  • (Boolean)

    True if filter has conditions



48
49
50
# File 'lib/autocad/selection_filter.rb', line 48

def has_filters?
  @types.any?
end

#has_text(str) ⇒ self

Helper method for text content matching

Examples:

Find text containing specific content

filter.has_text("Revision")

Parameters:

  • str (String)

    Text to search for

Returns:

  • (self)

    The filter for chaining



354
355
356
# File 'lib/autocad/selection_filter.rb', line 354

def has_text(str)
  contains(str)
end

#layer(name) ⇒ self

Filter by layer name

Examples:

Filter for entities on a specific layer

filter.layer("WALLS")

Parameters:

  • name (String)

    Layer name

Returns:

  • (self)

    The filter for chaining



266
267
268
269
270
# File 'lib/autocad/selection_filter.rb', line 266

def layer(name)
  @types << 8
  @values << name
  self
end

#less_than(value) ⇒ self

Less than comparison

Examples:

Find circles with radius < 10

filter.type("CIRCLE").less_than(10)

Parameters:

  • value (Numeric)

    Comparison value

Returns:

  • (self)

    The filter for chaining



178
179
180
181
182
183
184
# File 'lib/autocad/selection_filter.rb', line 178

def less_than(value)
  @types << -4
  @values << "<="
  @types << 40 # floating point
  @values << value
  self
end

#model_spaceself

Filter for model space entities

Examples:

Filter for model space entities

filter.model_space

Returns:

  • (self)

    The filter for chaining



316
317
318
319
320
# File 'lib/autocad/selection_filter.rb', line 316

def model_space
  @types << 67  # Model space filter
  @values << 0
  self
end

#name(value) ⇒ self

Filter by entity name/type

Examples:

Filter by entity name

filter.name("CIRCLE")

Parameters:

  • value (String)

    Entity type name (e.g., “CIRCLE”)

Returns:

  • (self)

    The filter for chaining



240
241
242
243
244
# File 'lib/autocad/selection_filter.rb', line 240

def name(value)
  @types << [0, 2]
  @values << value
  self
end

#not(condition) ⇒ self

Logical NOT for a filter condition

Examples:

Select everything except circles

filter.not(SelectionFilter.new.type("CIRCLE"))

Parameters:

Returns:

  • (self)

    The filter for chaining



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

def not(condition)
  @types << -4
  @values << "<NOT"

  @types.concat(condition.types)
  @values.concat(condition.values)

  @types << -4
  @values << "NOT>"

  self
end

#not_equal_to(value) ⇒ self

Not equal to comparison

Examples:

Find circles with radius != 5

filter.type("CIRCLE").not_equal_to(5)

Parameters:

  • value (Numeric)

    Comparison value

Returns:

  • (self)

    The filter for chaining



208
209
210
211
212
213
214
# File 'lib/autocad/selection_filter.rb', line 208

def not_equal_to(value)
  @types << -4
  @values << "<>"
  @types << 40 # floating point
  @values << value
  self
end

#or(*conditions) ⇒ self

Logical OR combination of filters

Examples:

Match entities on multiple layers

filter.or(
  SelectionFilter.new.layer("WALLS"),
  SelectionFilter.new.layer("DOORS")
)

Parameters:

Returns:

  • (self)

    The filter for chaining



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/autocad/selection_filter.rb', line 89

def or(*conditions)
  return self if conditions.empty?

  @types << -4
  @values << "<OR"

  conditions.each do |condition|
    @types.concat(condition.types)
    @values.concat(condition.values)
  end

  @types << -4
  @values << "OR>"

  self
end

#paper_spaceself

Filter for paper space entities

Examples:

Filter for paper space entities

filter.paper_space

Returns:

  • (self)

    The filter for chaining



305
306
307
308
309
# File 'lib/autocad/selection_filter.rb', line 305

def paper_space
  @types << 67  # Paper space filter
  @values << 1
  self
end

#type(kind) ⇒ self

Filter by entity type

Examples:

Filter for lines

filter.type("LINE")

Parameters:

  • kind (String)

    Entity type name (e.g., “CIRCLE”)

Returns:

  • (self)

    The filter for chaining



253
254
255
256
257
# File 'lib/autocad/selection_filter.rb', line 253

def type(kind)
  @types << 0
  @values << kind
  self
end

#visible(vis = true) ⇒ self

Filter by visibility state

Examples:

Filter for visible entities

filter.visible(true)

Filter for hidden entities

filter.visible(false)

Parameters:

  • vis (Boolean) (defaults to: true)

    True for visible entities

Returns:

  • (self)

    The filter for chaining



281
282
283
284
285
# File 'lib/autocad/selection_filter.rb', line 281

def visible(vis = true)
  @types << 60
  @values << (vis ? 0 : 1)
  self
end

#xor(condition1, condition2) ⇒ self

Logical XOR combination of two filters

Examples:

Select entities that match exactly one condition

filter.xor(
  SelectionFilter.new.type("CIRCLE"),
  SelectionFilter.new.layer("SPECIAL")
)

Parameters:

Returns:

  • (self)

    The filter for chaining



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/autocad/selection_filter.rb', line 118

def xor(condition1, condition2)
  @types << -4
  @values << "<XOR"

  @types.concat(condition1.types)
  @values.concat(condition1.values)

  @types.concat(condition2.types)
  @values.concat(condition2.values)

  @types << -4
  @values << "XOR>"

  self
end