Class: Autocad::Element

Inherits:
Object
  • Object
show all
Includes:
ElementTrait
Defined in:
lib/autocad/element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ElementTrait

#autocad_type, #block_reference?, #bounds, #cell?, #def, #drawing, #explode, #graphical?, #has_tags?, #highlight, #id_from_record, #inspect, #line?, #model, #parent, #pviewport?, #select, #text?, #to_ole, #visible?

Constructor Details

#initialize(ole, app, typ, cell = nil) ⇒ Element

Returns a new instance of Element.



419
420
421
422
423
424
425
# File 'lib/autocad/element.rb', line 419

def initialize(ole, app, typ, cell = nil)
  @ole_obj = ole
  @original = read_ole(ole)
  @app = app
  @cell = cell
  @acad_type = typ
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object



437
438
439
440
441
442
443
444
# File 'lib/autocad/element.rb', line 437

def method_missing(meth, *, &)
  if /^[A-Z]/.match?(meth.to_s)
    result = ole_obj.send(meth, *, &)
    Element.convert_item(result, app)
  else
    super
  end
end

Instance Attribute Details

#acad_typeObject (readonly)

Returns the value of attribute acad_type.



417
418
419
# File 'lib/autocad/element.rb', line 417

def acad_type
  @acad_type
end

#appObject (readonly)

Returns the value of attribute app.



417
418
419
# File 'lib/autocad/element.rb', line 417

def app
  @app
end

#ole_objObject (readonly)

Returns the value of attribute ole_obj.



417
418
419
# File 'lib/autocad/element.rb', line 417

def ole_obj
  @ole_obj
end

#originalObject (readonly)

Returns the value of attribute original.



417
418
419
# File 'lib/autocad/element.rb', line 417

def original
  @original
end

Class Method Details

.convert_item(ole, app, cell = nil) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/autocad/element.rb', line 318

def self.convert_item(ole, app, cell = nil)
  return Point3d.from_ole(ole) if ole.instance_of?(WIN32OLE_RECORD) && ole.typename == "Point3d"
  return ole unless ole.instance_of?(WIN32OLE)

  if ole.respond_to? :ObjectName
    typ = case ole.ObjectName
    when "AcDbModelSpace"
      ::Autocad::ModelSpace.new(ole, app, typ)
    when "AcDbPaperSpace"
      ::Autocad::PaperSpace.new(ole, app, typ)
    when "AcDbBlockTableRecord"
      ::Autocad::Block.new(ole, app, typ, cell)
    when "AcDbPolyline"
      ::Autocad::Polyline.new(ole, app, typ, cell)
    when "AcDbText"
      ::Autocad::Text.new(ole, app, typ, cell)
    when "AcDbMText"
      ::Autocad::TextNode.new(ole, app, typ, cell)
    when "AcDbArc"
      ::Autocad::Arc.new(ole, app, typ, cell)
    when "AcDbViewport"
      ::Autocad::Viewport.new(ole, app, typ)
    when "AcDbBlock"
      ::Autocad::Block.new(ole, app, typ)
    when "AcDbLayerTableRecord"
      ::Autocad::Layer.new(ole, app, typ)
    when AcDbLayerTableRecord
    end

    return typ if typ
    binding.irb

  else
    typ = ole.ole_type
    result = case typ.name
    when "IAcadSelectionSet"
      drawing = app.current_drawing
      ::Autocad::SelectionSetAdapter.from_ole_obj(drawing, ole)
    when "IAcadLayer"
      ::Autocad::Layer.new(ole, app, typ)
    when "IAcadLine"
      ::Autocad::Line.new(ole, app, typ)
    when "IAcadCircle"
      ::Autocad::Circle.new(ole, app, typ)
    when "IAcadLWPolyline"
      ::Autocad::Polyline.new(ole, app, typ)
    when "IAcadLineType"
      ::Autocad::Linetype.new(ole, app, typ)
    when "IAcadText"
      ::Autocad::Text.new(ole, app, typ)
    when "IAcadModelSpace"
      ::Autocad::ModelSpace.new(ole, app, typ)
    when "IAcadPaperSpace"
      ::Autocad::PaperSpace.new(ole, app, typ)
    when "IAcadMText"
      ::Autocad::MText.new(ole, app, typ)
    when "IAcadPViewport"
      ::Autocad::PViewport.new(ole, app, typ)
    when "IAcadBlockReference"
      ::Autocad::BlockReference.new(ole, app, typ)
    when "IAcadBlock"
      ::Autocad::Block.new(ole, app, typ)
    when "IAcadExternalReference"
      ::Autocad::ExternalReference.new(ole, app, typ)
    when "IAcadAttributeReference"
      ::Autocad::AttributeReference.new(ole, app, typ)
    when "IAcadLayout"
      ::Autocad::Layout.new(ole, app, typ)
    when "IAcadPlotConfiguration"
      ::Autocad::PlotConfiguration.new(ole, app, typ)
    when "IAcadDimStyle"
      ::Autocad::DimStyle.new(ole, app, typ)
    when "IAcadTextStyle"
      ::Autocad::TextStyle.new(ole, app, typ)
    when "IAcadArc"
      ::Autocad::Arc.new(ole, app, typ)
    when "IAcadPlot"
      ::Autocad::Plot.new(ole, app, typ)
    when "IAcadSpline"
      ::Autocad::Spline.new(ole, app, typ)
    when "IAcadViewport"
      ::Autocad::Viewport.new(ole, app, typ)
    when "IAcadPoint"
      ::Autocad::Point.new(ole, app, typ)

    else
      # binding.irb
      Element.new(ole, app, typ)
    end

    return result if result
    binding.irb
  end
end

.ole_object?Boolean

Returns:

  • (Boolean)


413
414
415
# File 'lib/autocad/element.rb', line 413

def self.ole_object?
  ole.instance_of?(WIN32OLE)
end

Instance Method Details

#[](name) ⇒ Object



455
456
457
# File 'lib/autocad/element.rb', line 455

def [](name)
  property_handler[name]
end

#app_ole_objObject



502
503
504
# File 'lib/autocad/element.rb', line 502

def app_ole_obj
  app.ole_obj
end

#clone(new_insertion_point) ⇒ Object



513
514
515
516
517
518
519
# File 'lib/autocad/element.rb', line 513

def clone(new_insertion_point)
  pt = Point3d.new(new_insertion_point)
  ole = ole_obj.Copy(pt.to_ole)
  app.wrap(ole)
rescue => ex
  raise Autocad::Error.new("Error cloning object #{ex}")
end

#delete(regen: true) ⇒ Object



506
507
508
509
510
511
# File 'lib/autocad/element.rb', line 506

def delete(regen: true)
  ole_obj.Delete
  drawing.regen if regen
rescue => ex
  raise Autocad::Error.new("Error deleting object #{self} #{ex}")
end

#do_update(value) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/autocad/element.rb', line 459

def do_update(value)
  return false if value == original

  saved_original = original
  begin
    write_ole(value)
    @original = read_ole(ole_obj)
    true
  rescue
    @original = saved_original
    false
  end
end

#each_complex(ole) ⇒ Object

if ole.IsCellElement

  each_cell(ole, &block)
else
  each_complex(ole, &block)
end

end



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/autocad/element.rb', line 582

def each_complex(ole, &)
  cell = nil
  components = ole.GetSubElements
  while components.MoveNext
    component = components.Current
    if component.IsTextNodeElement
      yield Autocad::Wrap.wrap(component.AsTextNodeElement, app, cell)
    elsif component.IsTextElement
      yield Autocad::Wrap.wrap(component.AsTextElement, app, cell)
    elsif component.IsComplexElement
      each(component, &)
    else
      yield Autocad::Wrap.wrap(component, app, cell)
    end
  end
end

#get_property_handlerObject



446
447
448
449
# File 'lib/autocad/element.rb', line 446

def get_property_handler
  ph_ole = app_ole_obj.CreatePropertyHandler(ole_obj)
  PropertyHandler.new(ph_ole)
end

#in_cell?Boolean

Returns:

  • (Boolean)


427
428
429
# File 'lib/autocad/element.rb', line 427

def in_cell?
  !!@cell
end

#move(x, y) ⇒ Object



533
534
535
536
537
# File 'lib/autocad/element.rb', line 533

def move(x, y)
  pt1 = Point3d(0, 0, 0)
  pt2 = Point3d(x, y, 0)
  move_ole(pt1.to_ole, pt2.to_ole)
end

#move_ole(pt1, pt2) ⇒ Object



539
540
541
542
543
544
# File 'lib/autocad/element.rb', line 539

def move_ole(pt1, pt2)
  ole_obj.Move(pt1, pt2)
  app.wrap(ole_obj)
rescue => ex
  raise Autocad::Error.new("Error moving object #{ex}")
end

#move_x(amt) ⇒ Object



521
522
523
524
525
# File 'lib/autocad/element.rb', line 521

def move_x(amt)
  pt1 = Point3d(0, 0, 0)
  pt2 = Point3d(amt, 0, 0)
  move_ole(pt1.to_ole, pt2.to_ole)
end

#move_y(amt) ⇒ Object



527
528
529
530
531
# File 'lib/autocad/element.rb', line 527

def move_y(amt)
  pt1 = Point3d(0, 0, 0)
  pt2 = Point3d(0, 1, 0)
  move_ole(pt1.to_ole, pt2.to_ole)
end

#ole_cell(ole) ⇒ Object



546
547
548
# File 'lib/autocad/element.rb', line 546

def ole_cell(ole)
  ole.IsCellElement || ole.IsSharedCellElement
end

#property_handlerObject



451
452
453
# File 'lib/autocad/element.rb', line 451

def property_handler
  @property_handler ||= get_property_handler
end

#read_ole(ole) ⇒ Object



431
432
# File 'lib/autocad/element.rb', line 431

def read_ole(ole)
end

#redraw(el = ole_obj) ⇒ Object



495
496
497
498
499
500
# File 'lib/autocad/element.rb', line 495

def redraw(el = ole_obj)
  # el.Redraw ::Autocad::MSD::MsdDrawingModeNormal if el.IsGraphical
  el.Update
rescue
  nil
end

#update(value) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/autocad/element.rb', line 473

def update(value)
  redraw_el = ole_obj
  if do_update(value)
    if in_cell?
      @cell.ReplaceCurrentElement @ole_obj
      redraw_el = @cell
    end
    redraw(redraw_el)
    @updated = true
    true
  else
    @updated = false
    false
  end
rescue => e
  app.error_proc.call(e, nil)
end

#updated?Boolean

Returns:

  • (Boolean)


491
492
493
# File 'lib/autocad/element.rb', line 491

def updated?
  @updated
end

#write_ole(value) ⇒ Object



434
435
# File 'lib/autocad/element.rb', line 434

def write_ole(value)
end