forked from fsmMLK/inkscapeCircuitSymbols
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawArrows.py
220 lines (185 loc) · 9.09 KB
/
drawArrows.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/python
import math
import inkscapeMadeEasy_Base as inkBase
import inkscapeMadeEasy_Draw as inkDraw
class arrow(inkBase.inkscapeMadeEasy):
# ---------------------------------------------
def drawVoltArrowSimple(self, parent, position, label='arrowV', name='v', color=inkDraw.color.defined('black'),
angleDeg=0, invertArrows=False, size=20.0, invertCurvatureDirection=False,
extraAngleText=0.0):
""" draws a voltage drop arrow
parent: parent object
position: position [x,y]
label: label of the object (it can be repeated)
name: string with namee. (default 'v')
color: color of the arrow and text
angleDeg: rotation angle in degrees counter-clockwise (default 0)
invertArrows: invert current direction
size: size of the arrow
invertCurvatureDirection: invert curvature direction of the arrow
extraAngleText: extra angle (in degrees) added to the text. (default: 0.0)
"""
if invertCurvatureDirection:
arrow_elem = self.drawVoltArrow(parent, position, label, name, color, angleDeg + 180, invertArrows, size,
extraAngleText)
self.rotateElement(arrow_elem, position, angleDeg + 180)
else:
arrow_elem = self.drawVoltArrow(parent, position, label, name, color, angleDeg, not invertArrows, size,
extraAngleText)
self.rotateElement(arrow_elem, position, angleDeg)
# ---------------------------------------------
def drawVoltArrow(self, parent, position, label='name', name='v', color=inkDraw.color.defined('black'), angleDeg=0,
invertArrows=False, size=20.0, extraAngleText=0.0):
""" draws a voltage drop arrow
parent: parent object
position: position [x,y]
label: label of the object (it can be repeated)
name: string with namee. (default 'v')
color: color of the arrow and text
invertArrows: invert current direction
size: size of the arrow
extraAngleText: extra angle (in degrees) added to the text. (default: 0.0)
"""
group = self.createGroup(parent, label)
scale = size / 20.0 # the default size was 20 height
renameMode = 0 # 0: do not modify 1: overwrite 2:rename
# make linestyle
[arrowStartVolt, arrowEndVolt] = inkDraw.marker.createArrow1Marker(self, 'arrowVoltage', RenameMode=renameMode,
scale=0.25, strokeColor=color,
fillColor=color)
if invertArrows:
lineStyle = inkDraw.lineStyle.set(lineColor=color, markerStart=arrowStartVolt)
else:
lineStyle = inkDraw.lineStyle.set(lineColor=color, markerEnd=arrowEndVolt)
radius = 30.0 * scale
h = 10.0 * scale
halfTheta = math.asin(h / radius)
inkDraw.arc.startEndRadius(group, [h, 0], [-h, 0], radius, position, lineStyle=lineStyle, flagRightOf=False)
# get appropriate refPoint based on the angle
theta = angleDeg + extraAngleText
while theta < 0:
theta = theta + 360
while theta > 360:
theta = theta - 360
if theta <= 20:
justif = 'tc'
else:
if theta <= 60:
justif = 'tl'
else:
if theta <= 100:
justif = 'cl'
else:
if theta <= 150:
justif = 'bl'
else:
if theta <= 200:
justif = 'bc'
else:
if theta <= 250:
justif = 'br'
else:
if theta <= 290:
justif = 'cr'
else:
if theta <= 340:
justif = 'tr'
else:
justif = 'tc'
centerY = -radius * math.cos(halfTheta)
posY = centerY + radius
if not name == '':
if inkDraw.useLatex:
name = '$' + name + '$'
dist = 4.0
else:
dist = 3.0
inkDraw.text.latex(self, group, name, [position[0], position[1] + posY + dist], fontSize=self.fontSize,
refPoint=justif, textColor=color, angleDeg=-theta, preambleFile=self.preambleFile)
return group
# ---------------------------------------------
def drawCurrArrowSimple(self, parent, position, label='arrowI', name='', color=inkDraw.color.defined('black'),
angleDeg=0, invertArrows=False, size=20.0, invertTextSide=False, extraAngleText=0.0):
""" draws a current arrow
parent: parent object
position: position [x,y]
label: label of the object (it can be repeated)
name: string with namee. (default none)
color: color of the arrow and text
angleDeg: rotation angle in degrees counter-clockwise (default 0)
invertArrows: invert current direction
size: size of the arrow
invertTextSide: invert side of the text in relation to the arrow
extraAngleText: extra angle (in degrees) added to the text. (default: 0.0)
"""
# control signal
if invertTextSide:
temp1 = self.drawCurrArrow(parent, position, label, name, color=self.currentColor, angleDeg=angleDeg + 180,
invertArrows=invertArrows, size=size, extraAngleText=extraAngleText)
self.rotateElement(temp1, position, angleDeg + 180)
else:
temp1 = self.drawCurrArrow(parent, position, label, name, color=self.currentColor, angleDeg=angleDeg,
invertArrows=not invertArrows, size=size, extraAngleText=extraAngleText)
self.rotateElement(temp1, position, angleDeg)
# ---------------------------------------------
def drawCurrArrow(self, parent, position, label='name', name='i', color=inkDraw.color.defined('black'), angleDeg=0,
invertArrows=False, size=10.0, extraAngleText=0.0):
""" draws a current arrow
parent: parent object
position: position [x,y]
label: label of the object (it can be repeated)
name: string with namee. (default 'v')
color: color of the arrow and text
angleDeg: rotation angle in degrees
invertArrows: invert current direction
size: size of the arrow
extraAngleText: extra angle (in degrees) added to the text. (default: 0.0)
"""
scale = size / 10.0
group = self.createGroup(parent, label)
renameMode = 0 # 0: do not modify 1: overwrite 2:rename
# make linestyle
[arrowStartCurr, arrowEndCurr] = inkDraw.marker.createArrow1Marker(self, 'arrowCurrent', RenameMode=renameMode,
scale=0.25, strokeColor=color,
fillColor=color)
lineStyle = inkDraw.lineStyle.set(lineColor=color, markerEnd=arrowEndCurr)
if invertArrows:
inkDraw.line.relCoords(group, [[10 * scale, 0]], [position[0] - 5 * scale, position[1]], label='none',
lineStyle=lineStyle)
else:
inkDraw.line.relCoords(group, [[-10 * scale, 0]], [position[0] + 5 * scale, position[1]], label='none',
lineStyle=lineStyle)
# get appropriate refPoint based on the angle
theta = angleDeg + extraAngleText
while theta < 0:
theta = theta + 360
while theta > 360:
theta = theta - 360
if theta <= 20:
justif = 'bc'
else:
if theta <= 60:
justif = 'br'
else:
if theta <= 100:
justif = 'cr'
else:
if theta <= 150:
justif = 'tr'
else:
if theta <= 200:
justif = 'tc'
else:
if theta <= 250:
justif = 'tl'
else:
if theta <= 290:
justif = 'cl'
else:
justif = 'bl'
if inkDraw.useLatex:
name = '$' + name + '$'
inkDraw.text.latex(self, group, name, [position[0], position[1] - self.textOffset * 0.8],
fontSize=self.fontSize, refPoint=justif, textColor=color, angleDeg=-theta,
preambleFile=self.preambleFile)
return group