Class: DXF::SplineParser

Inherits:
EntityParser show all
Defined in:
lib/dxf/parser.rb

Instance Attribute Summary collapse

Attributes inherited from EntityParser

#handle, #layer

Instance Method Summary collapse

Constructor Details

#initializeSplineParser

Returns a new instance of SplineParser.



191
192
193
194
195
196
197
198
# File 'lib/dxf/parser.rb', line 191

def initialize
    super 'SPLINE'
    @fit_points = []
    @knots = []
    @weights = []

    @fit_point_index = Hash.new {|h,k| h[k] = 0}
end

Instance Attribute Details

#closedObject (readonly)

Returns the value of attribute closed.



187
188
189
# File 'lib/dxf/parser.rb', line 187

def closed
  @closed
end

#degreeObject (readonly)

Returns the value of attribute degree.



188
189
190
# File 'lib/dxf/parser.rb', line 188

def degree
  @degree
end

#knotsObject (readonly)

Returns the value of attribute knots.



189
190
191
# File 'lib/dxf/parser.rb', line 189

def knots
  @knots
end

#linearObject (readonly)

Returns the value of attribute linear.



187
188
189
# File 'lib/dxf/parser.rb', line 187

def linear
  @linear
end

#periodicObject (readonly)

Returns the value of attribute periodic.



187
188
189
# File 'lib/dxf/parser.rb', line 187

def periodic
  @periodic
end

#planarObject (readonly)

Returns the value of attribute planar.



187
188
189
# File 'lib/dxf/parser.rb', line 187

def planar
  @planar
end

#pointsArray

Returns points.

Returns:

  • (Array)

    points



185
186
187
# File 'lib/dxf/parser.rb', line 185

def points
  @points
end

#rationalObject (readonly)

Returns the value of attribute rational.



187
188
189
# File 'lib/dxf/parser.rb', line 187

def rational
  @rational
end

Instance Method Details

#parse_pair(code, value) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dxf/parser.rb', line 200

def parse_pair(code, value)
    case code
	when 11, 21, 31
	    k = Parser.code_to_symbol(code)
	    i = @fit_point_index[k]
	    @fit_points[i] = Parser.update_point(@fit_points[i], k => value)
	    @fit_point_index[k] += 1
	when 12, 22, 32 then    @start_tangent = update_point(@start_tangent, Parser.code_to_symbol(code) => value)
	when 13, 23, 33 then    @end_tangent = update_point(@end_tangent, Parser.code_to_symbol(code) => value)
	when 40 then    knots.push value.to_f
	when 41 then    @weights.push value
	when 42 then    @knot_tolerance = value
	when 43 then    @control_tolerance = value
	when 44 then    @fit_tolerance = value
	when 70
	    value = value.to_i
	    @closed = value[0].zero? ? nil : true
	    @periodic = value[1].zero? ? nil : true
	    @rational = value[2].zero? ? nil : true
	    @planar = value[3].zero? ? nil : true
	    @linear = value[4].zero? ? nil : true
	when 71 then    @degree = value
	when 72 then    @num_knots = value
	when 73 then    @num_control_points = value
	when 74 then    @num_fit_points = value
	else
	    super
    end
end

#to_entityObject

Raises:

  • (ParseError)


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/dxf/parser.rb', line 230

def to_entity
    raise ParseError, "Wrong number of control points" unless points.size == @num_control_points

    # If all of the points lie in the XY plane, remove the Z component from each point
    if planar && points.all? {|a| a.z.zero?}
	@points.map! {|a| Geometry::Point[a[0, 2]]}
    end

    if knots.size == 2*points.size
	# Bezier?
	if knots[0,points.size].all?(&:zero?) && (knots[-points.size,points.size].uniq.size==1)
	    Bezier.new *points
	end
    else
	Spline.new degree:degree, knots:knots, points:points
    end
end