module Autocad class Filter attr_reader :types, :values, :clauses def initialize(clauses: {}) @clauses = clauses @types = [] @values = [] end def new_filter(clause, value) new_clauses = clauses.dup new_clauses[clause] = value Filter.new(clauses: new_clauses) end def has_filters? true end # convert the clauses to the values and types needed for autocad filter # rbs return Array[Array,Array] -- the types and values array def convert_clauses types = [] values = [] case clauses.keys.first when :type types << 0 values << clauses[:type] when :layer types << 8 values << clauses[:layer] when :color types << 62 values << clauses[:color] when :block_reference types << 0 values << 'INSERT' if clauses[:block_reference] types << 1 values << clauses[:block_reference] end when :paper_space types << 67 values << 1 when :model_space types << 67 values << 0 when :text_content types << 1 # DXF type code 1 for text content values << clauses[:text_content] when :and, :or, :xor operator = clauses.keys.first.to_s.upcase types << -4 values << "<#{operator}" clauses[clauses.keys.first].each do |condition| sub_types, sub_values = condition.convert_clauses types.concat(sub_types) values.concat(sub_values) end types << -4 values << "#{operator}>" when :not types << -4 values << '' when :gt types.concat([-4, 40]) values.concat(['>=', clauses[:gt]]) when :lt types.concat([-4, 40]) values.concat(['<=', clauses[:lt]]) when :eq types.concat([-4, 40]) values.concat(['=', clauses[:eq]]) when :neq types.concat([-4, 40]) values.concat(['<>', clauses[:neq]]) end [types, values] end # Logical Operators def and(*conditions) new_filter(:and, conditions) end def merge_conditions(existing, new_condition) end def or(*conditions) new_filter(:or, conditions) end def xor(condition1, condition2) new_filter(:xor, [condition1, condition2]) end def not(condition) new_filter(:not, condition) end # Relational Operators # f.type("Circle").greater_than(5) def greater_than(value) new_filter(:gt, value) end def less_than(value) new_filter(:lt, value) end def equal_to(value) new_filter(:eq, value) end def not_equal_to(value) new_filter(:neq, value) end def block_reference(name = nil) new_filter(:block_reference, name) end def name(value) new_filter(:name, value) end def type(kind) new_filter(:type, kind) end def layer(name) new_filter(:layer, name) end def visible(vis = true) new_filter(:visible, vis) end def color(num) new_filter(:color, num) end def paper_space new_filter(:paper_space, nil) end def model_space new_filter(:model_space, nil) end def has_text(str) new_filter(:text_content, str) end end end