forked from DingJianhao/auto_flowchart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawer.py
185 lines (149 loc) · 5.64 KB
/
drawer.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import sys, win32com.client
from collections import deque
import copy
import re
import os
import analyze as az
G=az.get_graph("sample2.cpp")
# Visio constants
visCharacterColor = 1
visCharacterFont = 20
visSectionCharacter = 3
visCharacterSize = 7
visCharacterDblUnderline = 8
visSectionFirstComponent = 10
visSectionObject = 1
visRowPrintProperties = 25
visPrintPropertiesPageOrientation = 16
visRowPage = 10
visPageWidth = 0
visPageHeight = 1
# Visio must be open and I used a "Basic Diagram" template
visio = win32com.client.Dispatch("Visio.Application")
FlowchartTemplateName = "Basic Flowchart.vst"
docFlowTemplate = visio.Documents.Add(FlowchartTemplateName)
pg = docFlowTemplate.Pages.Item(1)
# Change page from landscape to portrait but this works sometimes
visio.Application.ActiveWindow.Page.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesPageOrientation).FormulaForceU = "1"
visio.Application.ActiveWindow.Page.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageWidth).FormulaU = "8.5 in"
visio.Application.ActiveWindow.Page.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageHeight).FormulaU = "11 in"
# Place a Visio shape on the Visio document
def dropShape (shapeType, posX, posY, theText):
print("Shape type = %s" % shapeType)
print("X = %i" % posX)
print("Y = %i" % posY)
vsoShape = pg.Drop(shapeType, posX, posY)
setDefaultShapeValues(vsoShape)
vsoShape.Text = theText
return vsoShape # Returns the shape that was created
# Draw connector from bottom of one shape to another shape with autoroute
def connectShapes(shape1, shape2, theText):
conn = visio.Application.ConnectorToolDataObject
shpConn = pg.Drop(conn, 0, 0)
shpConn.CellsU("BeginX").GlueTo(shape1.CellsU("PinX"))
shpConn.CellsU("EndX").GlueTo(shape2.CellsU("PinX"))
setDefaultShapeValues(shpConn)
shpConn.Text = theText
# Specify which part of a shape to draw a connector from
# one shape to another shape.
def connectShapes2(shape1, shape2, glueBegin, glueEnd, theText):
conn = visio.Application.ConnectorToolDataObject
shpConn = pg.Drop(conn, 0, 0)
shpConn.CellsU("BeginX").GlueTo(shape1.CellsU(glueBegin))
shpConn.CellsU("EndX").GlueTo(shape2.CellsU(glueEnd))
setDefaultShapeValues(shpConn)
shpConn.Text = theText
# Set the default color, font and ect for a shape
def setDefaultShapeValues(vsoShape):
vsoShape.Cells("LineColor").FormulaU = 0
vsoShape.Cells("LineWeight").FormulaU = "2.0 pt"
vsoShape.FillStyle = "None"
vsoShape.Cells("Char.size").FormulaU = "12 pt"
vsoShape.CellsSRC(visSectionCharacter, 0, visCharacterDblUnderline).FormulaU = False
vsoShape.CellsSRC(visSectionCharacter, 0, visCharacterColor).FormulaU = "THEMEGUARD(RGB(0,0,0))"
vsoShape.CellsSRC(visSectionCharacter, 0, visCharacterFont).FormulaU = 100
return vsoShape
# Get the stencil object
def getStencilName(): # Name of Visio stencil containing shapes
FlowchartStencilName = "BASFLO_U.vssx" # Basic Flow Chart
docFlowStencil = ""
for doc in visio.Documents:
print ("Doc name = %s" % doc)
if doc.Name == FlowchartStencilName or doc.Name == "BASFLO_M.vssx" :
docFlowStencil = doc
print ("docFlowStencil = %s" % docFlowStencil) # Print installed stencils
return docFlowStencil
MasterProcessName = "Prozess"
MasterDecisionName = "Entscheidung"
MasterStartEnd = "Start/Ende"
docFlowStencil = getStencilName()
# Get masters for Process and Decision:
mstProcess = docFlowStencil.Masters(MasterProcessName)
mstDecision = docFlowStencil.Masters(MasterDecisionName)
mstStartEnd = docFlowStencil.Masters(MasterStartEnd)
x = 1
y = 10
queue = deque()
queue.append(0)
shape = 0
visited=[0 for i in range(0,len(G))]
posed=[0 for i in range(0,len(G))]
layout_x=[0 for i in range(0,len(G))]
layout_y = [0 for i in range(0, len(G))]
tag={}
print(G)
while len(queue)!=0:
pos = queue[0]
print(G[pos].content)
queue.popleft()
if visited[pos] == 1:
continue
#choose shape
if G[pos].type==1:
shape=mstProcess
elif G[pos].type==2:
shape=mstDecision
elif G[pos].type==3:
shape = mstStartEnd
if pos==0:
layout_x[pos]=x
layout_y[pos]=y
posed[0]=1
tag[pos] = dropShape(shape, layout_x[pos], layout_y[pos], G[pos].content)
visited[pos] = 1
if G[pos].yes!=-1 and posed[G[pos].yes]==0:
queue.append(G[pos].yes)
posed[G[pos].yes] = 1
layout_x[G[pos].yes]=layout_x[pos]
layout_y[G[pos].yes] = layout_y[pos]-1.2
if G[pos].no!=-1 and posed[G[pos].no]==0:
queue.append(G[pos].no)
posed[G[pos].no] = 1
layout_x[G[pos].no] = layout_x[pos]+2
layout_y[G[pos].no] = layout_y[pos]
# Add connectors to the shapes
queue = deque()
queue.append(0)
shape = 0
visited=[0 for i in range(0,len(G))]
while len(queue)!=0:
pos = queue[0]
print(G[pos].content)
queue.popleft()
if visited[pos] == 1:
continue
if G[pos].type==2:
if G[pos].yes != -1:
connectShapes(tag[pos], tag[G[pos].yes], "YES")
if G[pos].no != -1:
connectShapes2(tag[pos], tag[G[pos].no], "Connections.X2", "PinX", "NO")
else:
if G[pos].yes != -1:
connectShapes(tag[pos], tag[G[pos].yes], "")
if G[pos].no != -1:
connectShapes(tag[pos], tag[G[pos].no], "")
visited[pos] = 1
if G[pos].yes!=-1:
queue.append(G[pos].yes)
if G[pos].no!=-1:
queue.append(G[pos].no)