Class: ChemScanner::ChemDraw::BaseNode

Inherits:
Object
  • Object
show all
Includes:
BaseValue
Defined in:
lib/chem_scanner/chem_draw/node/base_node.rb

Overview

ChemDraw basic Node

Constant Summary

Constants included from BaseValue

ChemScanner::ChemDraw::BaseValue::ARROW_NOGO_CROSS, ChemScanner::ChemDraw::BaseValue::CDXML_ARROW_TYPE, ChemScanner::ChemDraw::BaseValue::CDXML_ATOM_EXTERNAL_CONNECTION_TYPE, ChemScanner::ChemDraw::BaseValue::CDXML_CDX_POINT, ChemScanner::ChemDraw::BaseValue::CDXML_GRAPHIC_TYPE, ChemScanner::ChemDraw::BaseValue::CDXML_LINE_TYPE, ChemScanner::ChemDraw::BaseValue::CDXML_NODE_TYPE, ChemScanner::ChemDraw::BaseValue::CDXML_ORBITAL_TYPE, ChemScanner::ChemDraw::BaseValue::CDXML_OVAL_TYPE, ChemScanner::ChemDraw::BaseValue::TEXT_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseValue

#binary_chunks, #cdx_styles, #cdx_text, #cdxml_text, #do_unhandled, #point_2d, #point_3d, #polygon_from_bb, #read_bounding_box, #read_ids, #read_int, #read_type, #read_value

Constructor Details

#initialize(parser, parser_type, id) ⇒ BaseNode

Returns a new instance of BaseNode.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 12

def initialize(parser, parser_type, id)
  @parser = parser
  @parser_type = parser_type

  check_id = id.nil? || id.zero?
  @id = check_id ? @parser.get_tempid : id

  case parser_type
  when "cdx" then set_cdx
  when "cdxml" then set_cdxml
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 10

def id
  @id
end

#parserObject (readonly)

Returns the value of attribute parser.



10
11
12
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 10

def parser
  @parser
end

#parser_typeObject (readonly)

Returns the value of attribute parser_type.



10
11
12
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 10

def parser_type
  @parser_type
end

#polygonObject (readonly)

Returns the value of attribute polygon.



10
11
12
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 10

def polygon
  @polygon
end

Instance Method Details

#assign_centerObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 92

def assign_center
  return if @polygon.nil?
  return unless @center.nil?

  box = bounding_box
  lb = box.leftbottom
  rt = box.righttop

  @center = Geometry::Point.new(
    (lb.x + rt.x) / 2,
    (lb.y + rt.y) / 2,
  )
end

#bounding_boxObject



88
89
90
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 88

def bounding_box
  @polygon.bounding_box
end

#cdx_readObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 41

def cdx_read
  pre_parse_node

  reader = @parser.reader
  tag = reader.read_next

  while tag.positive?
    cid = reader.current_id
    parse_node(tag, cid, reader.data)

    tag = reader.read_next(false)
  end

  post_parse_node
end

#cdxml_readObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 57

def cdxml_read
  pre_parse_node

  nid = @parser.reader.attr("id").to_i
  nid = nil if nid.zero?

  @parser.reader.attributes.each_value do |attr|
    parse_node(attr.name, nid, attr)
  end
  # end

  children = @parser.reader.element_children
  children.each do |child|
    @parser.reader = child
    nid = @parser.reader.attr("id").to_i
    nid = nid.to_i unless nid.nil?

    parse_node(child.name, nid, child)
  end

  post_parse_node
end

#center_xObject



106
107
108
109
110
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 106

def center_x
  return nil if @center.nil?

  center.x
end

#center_yObject



112
113
114
115
116
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 112

def center_y
  return nil if @center.nil?

  center.y
end

#get_tempidObject



118
119
120
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 118

def get_tempid
  parser.get_tempid
end

#parse_nodeObject

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 82

def parse_node
  raise NotImplementedError, "You must implement the parse method"
end

#post_parse_nodeObject



86
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 86

def post_parse_node; end

#pre_parse_nodeObject



80
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 80

def pre_parse_node; end

#readObject



35
36
37
38
39
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 35

def read
  return cdx_read if @parser_type == "cdx"

  cdxml_read
end

#set_cdxObject



25
26
27
28
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 25

def set_cdx
  @obj_ref = ChemDraw::CDX_OBJ
  @props_ref = ChemDraw::CDX_PROPS
end

#set_cdxmlObject



30
31
32
33
# File 'lib/chem_scanner/chem_draw/node/base_node.rb', line 30

def set_cdxml
  @obj_ref = ChemDraw::CDXML_OBJ
  @props_ref = ChemDraw::CDXML_PROPS
end