summaryrefslogtreecommitdiff
path: root/src/converter/schematic_converters/lib/PythonLib/design.py
blob: 75899869cdb6c910b01a64bdecb82dab2d46c627 (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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#The MIT License (MIT)

#PSpice to Oscad Schematic Converter
#This code is written by Suryavamshi Tenneti, FOSSEE, IIT Bombay
#The code is modified by Sumanto Kar and Gloria Nandihal, FOSSEE, IIT Bombay


from header import *
import math


# In the constructors of Line, Arc, Circle and Rectangle, the input parameters shiftx and shifty have already been scaled.
class Line: #Constructor of Line.
	npoints = 0
	x = []
	y = []
	def __init__(self, input_stream, shiftx, shifty): #This gets called when the first character of a line is "v".This function assumes "v" and the next character(usually 0) have already been read and are NOT in the stream.
		t = 0
		temp = input_stream.readline().strip()
		self.npoints = 0
		self.x = []
		self.y = []
		while(temp!=';'):
			#t = temp
			#print('Line->',temp)
			t = temp.split()
			self.x.append(int(t[0]))
			#t = input_stream.read(1)
			self.y.append(int(t[1]))
			#tmp = input_stream.readline().strip() .The first line, i.e.the one that contains the 'v'
			temp = input_stream.readline().strip()

			self.x[self.npoints]*= MULT
			self.y[self.npoints]*= -1*MULT

			self.x[self.npoints]-=shiftx
			self.y[self.npoints]-= -1*shifty

			self.npoints+=1

		if temp != ';':
			print('Error! \";\" not found\n')# The last character in the description of a line is ";"


	def print(self, output_stream):
		output_stream.write('P '+str(self.npoints)+' 0 1 0  ')
		for i in range(self.npoints):
			output_stream.write(str(self.x[i])+' '+str(self.y[i])+' ')
		output_stream.write('N\n')

class Rectangle:  ## Constructor of Rectangle.
	x1 = 0
	y1 = 0
	x2 = 0
	y2 = 0
	def __init__(self, input_stream, shiftx, shifty):
		input_line = input_stream.readline().strip()
		#print('Rect->',input_line)
		self.x1, self.y1,self.x2,self.y2 = input_line.split()[:4]  # The line that contains the 'r'

		self.x1 = (int(self.x1) * MULT) - shiftx
		self.x2 = (int(self.x2) * MULT) - shiftx
		self.y1 = (int(self.y1) * -1 * MULT) - (-1*shifty)
		self.y2 = (int(self.y2) * -1 * MULT) - (-1*shifty)

	def print(self, output_stream):
		output_stream.write('S '+str(self.x1)+' '+str(self.y1)+' '+str(self.x2)+' '+str(self.y2)+' 0 1 0 N\n')


class Circle:  # Constructor of Circle.
	x = 0
	y = 0
	r = 0
	def __init__(self, input_stream, shiftx, shifty):
		self.x, self.y, self.r = map(int,input_stream.readline().strip().split())
		#tmp = input_stream.readline().strip()
		#print('Circle->','x=',self.x,'y=',self.y,'r=',self.r)
		self.x*= MULT
		self.x-= shiftx
		self.y*=-1*MULT
		self.y-=-1*shifty
		self.r*= MULT

	def print(self, output_stream):
		output_stream.write('C '+str(self.x)+' '+str(self.y)+' '+str(self.r)+' 0 1 0 N\n')

  
class Arc:   # Constructor of Arc.
	x = 0
	y = 0
	r = 0
	sa = 0
	ea = 0
	x1 = 0
	y1 = 0
	x2 = 0
	y2 = 0   ## See Line::Line(istream & in , int shiftx, int shifty) above.#From pspice library, get the 3 points that describe the arc.

	def __init__(self, input_stream, shiftx, shifty):  # Midpoints of the arcs:
		xA = 0.0
		yA = 0.0
		xB = 0.0
		yB = 0.0
		xC = 0.0
		yC = 0.0
		xmAB = 0
		xmBC = 0
		ymAB = 0
		ymBC = 0
		input_line = input_stream.readline().strip()
		#print('Arc->',input_line)
		xA,yA,xB,yB,xC,yC = map(float, input_line.split())
		#tmp = input_stream.readline().strip()
		yA*= -1
		yB*= -1
		yC*= -1

		xmAB = (xA+xB)/2   # The perpendicular bisectors of any two chords of a circle meet at the centre
		ymAB = (yA+yB)/2
		xmBC = (xC+xB)/2
		ymBC = (yC+yB)/2

		mperpAB = -(xB - xA)/(yB - yA)# Get x and y by solving the two lines(perpendicular bisectors)
		mperpBC = -(xB - xC)/(yB - yC)
		
		try:
			self.x = math.trunc((ymBC - ymAB - mperpBC * xmBC + mperpAB * xmAB)/(-mperpBC + mperpAB))  
		except ZeroDivisionError:
			self.x = float('inf')
		try:	
			self.y = math.trunc((xmBC - xmAB + (ymAB/mperpAB) - (ymBC/mperpBC))/((1.0/mperpAB)-(1.0/mperpBC)))
		except ZeroDivisionError:
			if mperpBC == 0.0:
				self.y = -float('inf')
			else:
				self.y = float('inf')
		

		if not math.isinf(self.y) and not math.isinf(self.x) and not math.isnan(self.x) and not math.isnan(self.y):  # Get the radius:
			self.r = math.trunc(((self.x-xA) * (self.x-xA) + (self.y-yA) * (self.y-yA))**0.5)
		else:
			self.r = 0

		a = math.atan2(yA-self.y, xA-self.x)/math.pi*10.0*180.0 # Following code is used to decide which among A and C is the starting point(and thus determines "sa")
		b = math.atan2(yB-self.y, xB-self.x)/math.pi*10.0*180.0
		c = math.atan2(yC-self.y, xC-self.x)/math.pi*10.0*180.0
		
		if b < max(a,c) and b > min(a,c):  #b is between a and c# print('*1')
			#print('*1')
			self.sa = math.trunc(min(a,c))
			self.ea = math.trunc(max(a,c))

		if b > max(a,c):   #b is largest# print('*2')
			#print('*2')
			self.sa = math.trunc(max(a,c))
			self.ea = math.trunc(min(a,c) + 3600.0)

		if b < min(a,c):    #b is smallest# print('*3')
			#print('*3')
			self.sa = math.trunc(max(a,c) - 3600.0)
			self.ea = math.trunc(min(a,c))

		flag_x_inf = False
		flag_y_inf = False

		if math.isinf(self.x) or math.isnan(self.x):
			#self.x = shiftx
			flag_x_inf = True

		if math.isinf(self.y) or math.isnan(self.y):
			#self.y = shifty
			flag_y_inf = True

		
		xA = self.x + self.r*math.cos(self.sa*math.pi/1800.0)
		yA = self.y + self.r*math.sin(self.sa*math.pi/1800.0)
		xC = self.x + self.r*math.cos(self.ea*math.pi/1800.0)
		yC = self.y + self.r*math.sin(self.ea*math.pi/1800.0)
		
		self.sa+=1
		self.ea-=1

		self.r*=MULT

		self.x1 = (xA*MULT)-shiftx
		self.y1 = ((yA*MULT)-(-1)*shifty)
		self.x2 = ((xC*MULT)-shiftx)
		self.y2 = ((yC*MULT)-(-1)*shifty)

		if not flag_x_inf:
			self.x*=MULT
			self.x-=shiftx
		
		else:
			self.x = shiftx

		if not flag_y_inf: 
			self.y*=MULT
			self.y-=(-1)*shifty
		
		else:
			self.y = shifty
		 # scale and shift: #startx, starty, endx, endy are redundant.May not even be in use.Haven 't been fixed.
		
		if math.isinf(self.x1) or math.isnan(self.x1) or math.isinf(self.y1) or math.isnan(self.y1) or math.isinf(self.x2) or math.isnan(self.x2) or math.isinf(self.y2) or math.isnan(self.y2):
			self.x1 = -(2**31)
			self.x2 = -(2**31)
			self.y1 = -(2**31)
			self.y2 = -(2**31)
		
		else:
			self.x1 = math.trunc(self.x1)
			self.y1 = math.trunc(self.y1)
			self.x2 = math.trunc(self.x2)
			self.y2 = math.trunc(self.y2)

		'''
		if mperpAB == 0 or mperpBC == 0:
			print('mperpAB=',mperpAB, 'mperpBC=', mperpBC)
			print('X=',self.x,'Y=',self.y)
			print('Radius=',self.r)
			print('a=',a,'b=',b,'c=',c)
			print('xA=',xA,'yA=',yA,'xC=',xC,'yC=',yC)
			print('x=',self.x,'y=',self.y)
			print('x1=',self.x1,'x2=',self.x2,'y1=',self.y1,'y2=',self.y2)
			print('sa=',self.sa,'ea=',self.ea)
		'''	
	def print(self,output_stream):
		output_stream.write('A '+str(math.trunc(self.x))+' '+str(math.trunc(self.y))+' '+str(int(self.r))+' '+str(int(self.sa))+' '+str(int(self.ea))+' '+' 0 1 0 N '+str(int(self.x1))+' '+str(int(self.y1))+' '+str(int(self.x2))+' '+str(int(self.y2))+' '+'\n')


class Text:  #Constructor of Circle.
	x = 0
	y = 0
	text = ''
	orient = ''

	def __init__(self,input_stream, shiftx, shifty):
		input_line = input_stream.readline().strip().split()
		self.x,self.y,self.orient = input_line[:3]
		self.x = int(self.x)
		self.y = int(self.y)
		#tmp = input_stream.readline().strip()

		self.text = input_stream.readline().strip() 
		self.x*=MULT
		self.y*=-1*MULT
		self.x-=shiftx
		self.y-=-1*shifty

	def print(self,output_stream):
		output_stream.write('T ') # The line that contains the 't'
		if self.orient[0] == 'h':
			output_stream.write('0 ')
		elif self.orient[0] == 'v':
			output_stream.write('900 ')
		output_stream.write(str(self.x)+' '+str(self.y)+' '+str(30)+' 0 0 0 '+self.text+'\n')


class Design:  #Constructor of Design.
	shiftx = 0
	shifty = 0
	lines = []
	rects = []
	circles = []
	arcs = []
	texts = []  # Reads the whole design, makes Line, Circle, etc.objects and stores them in the appropriate container(appropriate vector)
	def __init__(self, input_stream):
		g = 0
		tmp = ''
		t = 0
		tint = ''
		tmp = input_stream.readline().strip()
		#print('Design->', tmp)
		if('@graphics' not in tmp):
			print('Design not found!')
			return
		#print('Graphics->',tmp.split())
		self.lines = []
		self.rects = []
		self.circles = []
		self.arcs = []
		self.texts = []
		tmp = tmp.split()
		tint,tint,tint,self.shiftx,self.shifty= map(str,tmp[:5])
		if len(tmp) > 5:
			tmp = ''.join(tmp[5:])
		self.shiftx = int(self.shiftx)*MULT  # print('shiftx->', self.shiftx, 'shifty->', self.shifty)
		self.shifty = int(self.shifty)*MULT
		#print('shiftx->',self.shiftx,'shifty->',self.shifty)
		while(t!= '*'):  #As long as we haven 't reached the description of the next Component continue reading the lib file.
			g  = input_stream.tell()  # Get the position of the read head, so that we can go back to this position
			t = input_stream.read(1) # Get the first character of the description, store in "t".This character gives what shape it is.
			if not t:
				break

			if t == 'v':
				#print('Line')
				input_stream.read(1)
				input_stream.read(1)
				input_stream.read(1)
				l = Line(input_stream, self.shiftx, self.shifty)
				self.lines.append(l)

			elif t == 'r':
				#print('Rect')
				input_stream.read(1)
				input_stream.read(1)
				input_stream.read(1)
				r = Rectangle(input_stream, self.shiftx, self.shifty)
				self.rects.append(r)

			elif t == 'c':
				#print('Circle')
				input_stream.read(1)
				input_stream.read(1)
				input_stream.read(1)
				c = Circle(input_stream, self.shiftx, self.shifty)
				self.circles.append(c)

			elif t == 'a':
				#print('Arc')
				input_stream.read(1)
				input_stream.read(1)
				input_stream.read(1)
				a = Arc(input_stream, self.shiftx, self.shifty)
				self.arcs.append(a)

			elif t == 'z':
				#print('Text')
				input_stream.read(1)
				input_stream.read(1)
				input_stream.read(1)
				z = Text(input_stream, self.shiftx, self.shifty)
				self.texts.append(z)

			else:   #If t is neither 'v', 'r', 'c'
				tmp = input_stream.readline().strip()
				g = input_stream.tell()

		input_stream.seek(g)


	def print(self, output_stream):
		for i in range(len(self.lines)):
			self.lines[i].print(output_stream)
		for i in range(len(self.rects)):
			self.rects[i].print(output_stream)
		for i in range(len(self.circles)):
			self.circles[i].print(output_stream)
		for i in range(len(self.arcs)):
			self.arcs[i].print(output_stream)
		for i in range(len(self.texts)):
			self.texts[i].print(output_stream)