-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlayer2svg.jl
65 lines (59 loc) · 2.01 KB
/
layer2svg.jl
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
#! /usr/local/bin/julia
using EzXML
function splitFile(procString::String)
rootPath=splitdir(procString)[1]
origDoc=readxml(procString)
origRoot=root(origDoc)
origNamespace=namespaces(origRoot)
origAttribute=attributes(origRoot)
origElement=elements(origRoot)
# Sort out layers
isLayer=Vector{Bool}(undef,length(origElement))
isBackground=Vector{Bool}(undef,length(origElement))
layerNames=Vector{String}(undef,length(origElement))
fill!(isLayer,false)
fill!(isBackground,false)
fill!(layerNames,"")
for ii in 1:length(origElement) # Find the various layers
tempAtt=attributes(origElement[ii])
for jj in 1:length(tempAtt)
# println(tempAtt[jj].name," => ",tempAtt[jj].content)
if tempAtt[jj].name=="groupmode" && tempAtt[jj].content == "layer"
isLayer[ii]=true;
end
if tempAtt[jj].name=="label"
layerNames[ii]=tempAtt[jj].content;
end
if tempAtt[jj].name=="label" && lowercase(tempAtt[jj].content) == "background"
isBackground[ii]=true;
end
end
end
isLayer=isLayer.&.~isBackground #Work out which ones to keep
layerIndex=(1:length(isLayer))[isLayer]
permIndex=(1:length(isLayer))[.~isLayer.|isBackground]
for layerNo in layerIndex #Create new files
# Reassemble file
currDoc=XMLDocument()
currRoot=setroot!(currDoc,ElementNode(origRoot.name))
unlink!.(origAttribute)
link!.(Ref(currRoot),origAttribute)
# Preseve namespace hack
for tt in 1:length(origNamespace)
if length(origNamespace[tt][1])>1
link!(currRoot,AttributeNode(string("xmlns:",origNamespace[tt][1]),origNamespace[tt][2]))
else
link!(currRoot,AttributeNode(string("xmlns",origNamespace[tt][1]),origNamespace[tt][2]))
end
end
currIndex=cat(permIndex,layerNo,dims=1)
unlink!.(origElement)
for ll in currIndex
link!(currRoot,origElement[ll])
end
println(layerNames[layerNo])
write(rootPath*"/"*layerNames[layerNo]*".svg",currDoc)
end
end
absP=abspath.(ARGS)
splitFile.(absP)