summaryrefslogtreecommitdiff
path: root/src/converter/schematic_converters/lib/PythonLib/component_instance.py
blob: 661cd04eee9ef1e66ba006e66b173ef4a02cc986 (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
#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 attribute import *
from random import randint
from header import *
import sys
import copy

class ComponentInstance:
	x = 0
	y = 0
	type_ = ''
	orient = ''
	attrs = []

	def __init__(self, input_stream):
		self.attrs = [Attribute() for _ in range(2)]
		tmp = ''
		compnum = 0
		x0 = 0
		y0 = 0
		line = input_stream.readline().strip().split()
		#print(line)
		tmp,compnum,self.type_,x0,y0,self.orient = line
		x0 = int(x0)
		y0 = int(y0)
		self.x = x0 * MULT
		self.y = y0 * MULT
		#input_stream.readline()
		g = input_stream.tell()
		#print('type ->',self.type_)
		line = input_stream.readline().strip()
#if the above cases like VALUE, DC, GAIN doesn 't exist then giving attributes of PKGREF to it#
		#print('component instance',line)
		input_stream.seek(g)
		while(line[0] == 'a'):
			#print('compinst',line)
			attr = Attribute(line)
			#print('compinst->attr.name',attr.name)
			if attr.name == 'PKGREF':
				#print('yes attr[0]')
				self.attrs[0] = attr
			if attr.name == 'VALUE' or attr.name == 'DC' or attr.name == 'GAIN':
				#print('yes attr[1]')
				self.attrs[1] = attr
			g = input_stream.tell()
			line = input_stream.readline().strip()
		input_stream.seek(g)
		#print('attrs[0].value-> '+self.attrs[0].value,'attrs[1].name-> '+self.attrs[1].value)
		if self.attrs[0].name == '':
			#print('null attr[0]')
			self.attrs[0].name = 'PKGREF'
			self.attrs[0].value = self.type_
			self.attrs[0].orient = 'h'
			self.attrs[0].x = self.x
			self.attrs[0].y = self.y
			self.attrs[0].isHidden = True
			self.attrs[0].hjust = 'l'
			self.attrs[0].vjust = 'n'

		if self.attrs[1].name == '':
			#print('null attr[1]')
			self.attrs[1] = copy.copy(self.attrs[0])
			
			self.attrs[1].value = self.type_
			#print(self.attrs[1].value)
			self.attrs[1].y+=80

		self.attrs[0].x+= self.x
		self.attrs[1].x+= self.x
		self.attrs[0].y+= self.y
		self.attrs[1].y+= self.y
		#print('component initiated', self.attrs[0].value)

	#print all the components in output schematic file as per KiCad format
	def print(self, output_stream):
		output_stream.write('$Comp\n'+'L '+self.type_+' '+self.attrs[0].value+'\n')
		output_stream.write('U 1 1 '+str(randint(0, sys.maxsize+1)%90000000+10000000)+'\n')
		output_stream.write('P '+str(self.x)+' '+str(self.y)+'\n') #printing the postion of component
		output_stream.write('F 0')# upto F0 printed
		self.attrs[0].print(output_stream)# print the attributes by calling attributes print
		output_stream.write('F 1')# upto F1 printed
		self.attrs[1].print(output_stream)# print the attributes by calling attributes print
		output_stream.write('\t1    '+str(self.x)+' '+str(self.y)+'\n')# printing the postions of the component again
		if self.orient == 'v':
			output_stream.write('\t0    -1    -1    0\n')# rotation matrix corresponding to KiCad
		if self.orient == 'V':
			output_stream.write('\t0    1    -1    0\n')# rotation matrix corresponding to KiCad
		if self.orient == 'h':
			output_stream.write('\t1    0    0    -1\n')
		if self.orient == 'H':
			output_stream.write('\t-1    0    0    -1\n')
		if self.orient == 'u':
			output_stream.write('\t-1    0    0    1\n')
		if self.orient == 'U':
			output_stream.write('\t1    0    0    1\n')
		if self.orient == 'd':
			output_stream.write('\t0    1    1    0\n')
		if self.orient == 'D':
			output_stream.write('\t0    -1    1    0\n')

		output_stream.write('$EndComp\n')#The MIT License (MIT)

#PSpice to Oscad Schematic Converter
#Copyright (c) 2014, Siddhant Ranade,Ashlesha Atrey and Suryavamshi Tenneti, FOSSEE, IIT Bombay
#The code is modified by Sumanto Kar and Gloria Nandihal



from attribute import *
from random import randint
from header import *
import sys
import copy

class ComponentInstance:
	x = 0
	y = 0
	type_ = ''
	orient = ''
	attrs = []

	def __init__(self, input_stream):
		self.attrs = [Attribute() for _ in range(2)]
		tmp = ''
		compnum = 0
		x0 = 0
		y0 = 0
		line = input_stream.readline().strip().split()
		#print(line)
		tmp,compnum,self.type_,x0,y0,self.orient = line
		x0 = int(x0)
		y0 = int(y0)
		self.x = x0 * MULT
		self.y = y0 * MULT
		#input_stream.readline()
		g = input_stream.tell()
		#print('type ->',self.type_)
		line = input_stream.readline().strip()
#if the above cases like VALUE, DC, GAIN doesn 't exist then giving attributes of PKGREF to it#
		#print('component instance',line)
		input_stream.seek(g)
		while(line[0] == 'a'):
			#print('compinst',line)
			attr = Attribute(line)
			#print('compinst->attr.name',attr.name)
			if attr.name == 'PKGREF':
				#print('yes attr[0]')
				self.attrs[0] = attr
			if attr.name == 'VALUE' or attr.name == 'DC' or attr.name == 'GAIN':
				#print('yes attr[1]')
				self.attrs[1] = attr
			g = input_stream.tell()
			line = input_stream.readline().strip()
		input_stream.seek(g)
		#print('attrs[0].value-> '+self.attrs[0].value,'attrs[1].name-> '+self.attrs[1].value)
		if self.attrs[0].name == '':
			#print('null attr[0]')
			self.attrs[0].name = 'PKGREF'
			self.attrs[0].value = self.type_
			self.attrs[0].orient = 'h'
			self.attrs[0].x = self.x
			self.attrs[0].y = self.y
			self.attrs[0].isHidden = True
			self.attrs[0].hjust = 'l'
			self.attrs[0].vjust = 'n'

		if self.attrs[1].name == '':
			#print('null attr[1]')
			self.attrs[1] = copy.copy(self.attrs[0])
			
			self.attrs[1].value = self.type_
			#print(self.attrs[1].value)
			self.attrs[1].y+=80

		self.attrs[0].x+= self.x
		self.attrs[1].x+= self.x
		self.attrs[0].y+= self.y
		self.attrs[1].y+= self.y
		#print('component initiated', self.attrs[0].value)

	#print all the components in output schematic file as per KiCad format
	def print(self, output_stream):
		output_stream.write('$Comp\n'+'L '+self.type_+' '+self.attrs[0].value+'\n')
		output_stream.write('U 1 1 '+str(randint(0, sys.maxsize+1)%90000000+10000000)+'\n')
		output_stream.write('P '+str(self.x)+' '+str(self.y)+'\n') #printing the postion of component
		output_stream.write('F 0')# upto F0 printed
		self.attrs[0].print(output_stream)# print the attributes by calling attributes print
		output_stream.write('F 1')# upto F1 printed
		self.attrs[1].print(output_stream)# print the attributes by calling attributes print
		output_stream.write('\t1    '+str(self.x)+' '+str(self.y)+'\n')# printing the postions of the component again
		if self.orient == 'v':
			output_stream.write('\t0    -1    -1    0\n')# rotation matrix corresponding to KiCad
		if self.orient == 'V':
			output_stream.write('\t0    1    -1    0\n')# rotation matrix corresponding to KiCad
		if self.orient == 'h':
			output_stream.write('\t1    0    0    -1\n')
		if self.orient == 'H':
			output_stream.write('\t-1    0    0    -1\n')
		if self.orient == 'u':
			output_stream.write('\t-1    0    0    1\n')
		if self.orient == 'U':
			output_stream.write('\t1    0    0    1\n')
		if self.orient == 'd':
			output_stream.write('\t0    1    1    0\n')
		if self.orient == 'D':
			output_stream.write('\t0    -1    1    0\n')

		output_stream.write('$EndComp\n')