Class: ChemScanner::ChemDraw::Fragment

Inherits:
BaseNode
  • Object
show all
Defined in:
lib/chem_scanner/chem_draw/node/fragment.rb

Overview

CDX Fragment parser

Constant Summary

Constants included from BaseValue

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

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#id, #parser, #parser_type

Instance Method Summary collapse

Methods inherited from BaseNode

#assign_center, #bounding_box, #cdx_read, #cdxml_read, #center_x, #center_y, #get_tempid, #pre_parse_node, #read, #set_cdx, #set_cdxml

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) ⇒ Fragment

Returns a new instance of Fragment.



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

def initialize(parser, parser_type, id)
  super(parser, parser_type, id)
  @boxed = false

  @node_map = {}
  @bond_map = {}

  @graphic_map = {}
end

Instance Attribute Details

#bond_mapObject

indicate if fragment is boxed within an rectangle



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

def bond_map
  @bond_map
end

#boxedObject

indicate if fragment is boxed within an rectangle



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

def boxed
  @boxed
end

#graphic_mapObject

indicate if fragment is boxed within an rectangle



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

def graphic_map
  @graphic_map
end

#node_mapObject

indicate if fragment is boxed within an rectangle



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

def node_map
  @node_map
end

#polygonObject

indicate if fragment is boxed within an rectangle



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

def polygon
  @polygon
end

Instance Method Details

#bond_has_endpoint(endpoint) ⇒ Object



111
112
113
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 111

def bond_has_endpoint(endpoint)
  @bond_map.detect { |_, b| b.end_points.include?(endpoint) }
end

#cloneObject



115
116
117
118
119
120
121
122
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 115

def clone
  cloned = self.class.new(@parser, @parser_type, @id)
  cloned.boxed = @boxed
  cloned.clone_node_map(@node_map)
  cloned.clone_bond_map(@bond_map)

  cloned
end

#clone_bond_map(bond_map) ⇒ Object



131
132
133
134
135
136
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 131

def clone_bond_map(bond_map)
  @bond_map = {}
  bond_map.each do |k, v|
    @bond_map[k] = v
  end
end

#clone_node_map(node_map) ⇒ Object



124
125
126
127
128
129
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 124

def clone_node_map(node_map)
  @node_map = {}
  node_map.each do |k, v|
    @node_map[k] = v
  end
end

#create_bond(id) ⇒ Object



80
81
82
83
84
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 80

def create_bond(id)
  bond = Bond.new(@parser, @parser_type, id)
  bond.read
  @bond_map[id] = bond
end

#create_node(id) ⇒ Object



74
75
76
77
78
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 74

def create_node(id)
  node = FragmentNode.new(@parser, @parser_type, id)
  node.read
  @node_map[id] = node
end

#get_external_pointObject

Check if fragment has ExternalConnectionPoint node



91
92
93
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 91

def get_external_point
  get_node_with_type(12)
end

#get_internal_nidsObject

Get the internal id for Fragment node



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 96

def get_internal_nids
  ext_node = get_external_point
  return [] if ext_node.count.zero?

  ext_ids = ext_node.keys
  internal_ids = []
  ext_ids.each do |ext_id|
    hbond = bond_has_endpoint(ext_id)
    _, bond = hbond

    internal_ids.push(bond.other_endpoint(ext_id))
  end
  [ext_ids, internal_ids]
end

#get_node_with_type(type) ⇒ Object



86
87
88
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 86

def get_node_with_type(type)
  @node_map.select { |_, v| v.type == type }
end

#parse_node(tag, nid, _data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 23

def parse_node(tag, nid, _data)
  case @props_ref.key?(tag) || @obj_ref[tag]
  when "Node" then create_node(nid)
  when "Bond" then create_bond(nid)
  when "Graphic"
    graphic = Graphic.new(@parser, @parser_type, id)
    graphic.read
    @graphic_map[id] = graphic
  # when "BoundingBox" then @polygon = read_value(tag, data)

  # NOTE: Indicates that this object represents some properties
  # in some other objects.
  # when "RepresentsProperty" then @represent = true
  else do_unhandled(tag)
  end
end

#post_parse_nodeObject



40
41
42
43
44
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 40

def post_parse_node
  return if !@polygon.nil? || @node_map.count.zero?

  rebuild_polygon
end

#rebuild_polygonObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 46

def rebuild_polygon
  fn = @node_map.first[1]
  lb = Geometry::Point.new(fn.x, fn.y)
  rt = Geometry::Point.new(fn.x, fn.y)

  @node_map.each_value do |node|
    # next if node.x.nil? || node.y.nil?
    next if node.has_nil_coord?

    nlb = node.leftbottom
    nrt = node.righttop

    lb.x = nlb.x if nlb.x < lb.x
    lb.y = nlb.y if nlb.y < lb.y

    rt.x = nrt.x if nrt.x > rt.x
    rt.y = nrt.y if nrt.y > rt.y
  end

  points = [
    Geometry::Point.new(lb.x, lb.y),
    Geometry::Point.new(lb.x, rt.y),
    Geometry::Point.new(rt.x, rt.y),
    Geometry::Point.new(rt.x, lb.y),
  ]
  @polygon = Geometry::Polygon.new(points)
end

#set_id(new_id) ⇒ Object



144
145
146
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 144

def set_id(new_id)
  @id = new_id
end

#set_new_idObject



138
139
140
141
142
# File 'lib/chem_scanner/chem_draw/node/fragment.rb', line 138

def set_new_id
  new_id = @parser.get_tempid
  set_id(new_id)
  new_id
end