summaryrefslogtreecommitdiff
path: root/import_tools/heeks_coords.py
blob: e0a4cd17ba583d137f64e3f4f144d68245d0cf19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python
import yaml
import  sys
from xml.dom import minidom
''''cat foo | heeks_coords.py' or 'heeks_coords.py foo.heeks' where foo.heeks
    is a HeeksCAD file containing coordinate systems you wish to inspect.
    the idea is that you locate a coordinate system on some geometry and name
    it according to the interface type it represents.'''

class yaml_dummy(yaml.YAMLObject):
    yaml_tag='!interface'

def parse_file(file):
    coords = []
    doc = minidom.parse(file)
    for node1 in doc.childNodes:
        if node1.nodeName == 'HeeksCAD_Document':
            for node2 in node1.childNodes:
                if node2.nodeName == 'CoordinateSystem':
                    coords.append(parse_coord(node2))
    return coords

def parse_coord(node):
    tmp = {}
    attr_keys = ['ox', 'oy', 'oz', 'xx', 'xy', 'xz', 'yx', 'yy', 'yz']
    for key in attr_keys:
        attr = node.attributes[key]
        tmp[key] = float("%.13f" %(float(attr.value))) #round down to 0 if < +-1e-13
    
    dummy = yaml_dummy()
    for (k,v) in {'point':[tmp['ox'], tmp['oy'], tmp['oz']],
            'x_vec':[tmp['xx'], tmp['xy'], tmp['xz']],
            'y_vec':[tmp['yx'], tmp['yy'], tmp['yz']],
            'type':str(node.attributes['title'].value)}.iteritems():
        setattr(dummy, k, v)
    return dummy

def main():
    rval = {}
    if len(sys.argv)>1 and sys.argv[1] == '-':
        rval['stdin'] = parse_file(sys.stdin)
    else:
        for i in sys.argv[1:]:
            rval[i] = parse_file(open(i))
    print yaml.dump(rval)
    return rval

if __name__ == '__main__':
   main()