-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_graph_skeleton.jsx
153 lines (115 loc) · 3.87 KB
/
1_graph_skeleton.jsx
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "functions/json2.js";
#include "../plowright_file_paths.js";
// Open file: blank_skeleton.ai
// Graph the initial skeleton of the family tree based on "cleaned" data
// The next step is to manually reposition nodes within the AI file
// Read content
var data_file = File(data_path);
var open_res = data_file.open('r');
if(!data_file){alert("Data file is not found")}
var data = JSON.parse(data_file.read());
data_file.close();
// Get document
var doc = app.activeDocument;
var doc_w = doc.width;
var doc_h = doc.height;
var skel = doc.layers.getByName('Skeleton')
var skel_nodes = skel.layers.getByName('Nodes')
var skel_links = skel.layers.getByName('Links')
// Set variables
var space_x = 50
var space_y = 100
var offset_x = 100
var offset_y = 100
// Colors
var node_col_fill = new RGBColor();
node_col_fill.red = 245;
node_col_fill.green = 245;
node_col_fill.blue = 245;
var node_col_stroke = new RGBColor();
node_col_stroke.red = 15;
node_col_stroke.green = 15;
node_col_stroke.blue = 15;
function node_centre(position, gen){
x = (position - 1) * space_x + offset_x
y = (gen - 1) * -space_y - offset_y
return [x,y]
}
function node_centre_get(id){
try{
node = skel_nodes.groupItems.getByName(id)
bounds = node.geometricBounds
return [((bounds[2] - bounds[0]) / 2) + bounds[0], ((bounds[3] - bounds[1]) / 2) + bounds[1]]
}catch(e){
return null
}
}
function node_create(coords, radius, id){
left = coords[0] - radius
top = coords[1] + radius
diam = radius * 2
// Make group
grp = skel_nodes.groupItems.add()
grp.name = id
// Make text
txt = skel_nodes.textFrames.pointText(coords)
txt.contents = id
txt.textRange.justification = Justification.CENTER;
txt.moveToEnd(grp)
// Make ellipse
ell = skel_nodes.pathItems.ellipse(top, left, diam, diam)
ell.moveToEnd(grp)
ell.fillColor = node_col_fill
ell.strokeColor = node_col_stroke
}
function draw_links(fam_id){
fam = fams[fam_id]
if(fam.HUSB != null & fam.WIFE != null){
husb_centr = node_centre_get(fam.HUSB)
wife_centr = node_centre_get(fam.WIFE)
if(husb_centr != null & wife_centr != null){
// Determine midpoint between spouses
mid_pt = [(husb_centr[0] + wife_centr[0]) / 2, (husb_centr[1] + wife_centr[1]) / 2]
// Make group
fam_grp = skel_links.groupItems.add()
fam_grp.name = fam_id
// Make spouse line
var spouse_line = skel_links.pathItems.add();
spouse_line.setEntirePath([
husb_centr,
wife_centr
]);
spouse_line.strokeWidth = 2
spouse_line.moveToEnd(fam_grp)
// Make children lines
if(fam.CHIL != null){
for(var i = 0; i < fam.CHIL.length; i ++){
chil_id = fam.CHIL[i]
chil_pt = node_centre_get(chil_id)
if(chil_pt != null){
var chil_line = skel_links.pathItems.add();
chil_line.setEntirePath([ chil_pt,mid_pt]);
chil_line.strokeWidth = 2
chil_line.moveToEnd(fam_grp)
}
}
}
}
}
}
// Create nodes
var inds = data['individuals']
if(skel_nodes.pageItems.length == 0){
for (id in inds) {
ind = inds[id]
node_create(node_centre(ind.position, ind.gen), 20, id)
}
}
// Create links
var fams = data['families']
if(skel_links.pageItems.length == 0){
for (fam_id in fams) {
draw_links(fam_id)
}
}
//~ alert('skel_nodes: ' + skel_nodes.pageItems.length)