-
Notifications
You must be signed in to change notification settings - Fork 1
/
fritzing-schematic.py
1552 lines (1204 loc) · 64.8 KB
/
fritzing-schematic.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
fritzing-schematic.py v1.0
-for Inkscape v1.2+
-version history at end of code
"""
"""
Copyright (c) 2024 Randy Blose
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import sys
#sys.path.append('/usr/share/inkscape/templates') # or another path, as necessary
import inkex
#import simplestyle
from lxml import etree
import tkinter as tk
from tkinter import messagebox
from tkinter import Scrollbar
from tkinter import Canvas
from tkinter import Frame
from inkex.elements import load_svg # is this even used?
class FritzingSchematic(inkex.EffectExtension):
# global variables
inch_scale_factor = 1000 # inch scaling factor - the value used to scale inch values to
# match the documents scale units
pixel_scale_factor = 10.41667 # pixel scaling factor - the value used to scale pixel values
# to match the document's scale units
# globals are frowned upon, but these were added in during dev work and just not removed,
# they are used in very few places and could be hard coded instead
# user inputs - from fritzing-schematic.inx
def add_arguments(self, pars):
#inkex.Effect.__init__(self)
# x size of schematic symbol
pars.add_argument('--x_size',
type = float,
default = '0.3',
help = 'x size')
# y size of schematic symbol
pars.add_argument('--y_size',
type = float,
default = '0.3',
help = 'y size')
# Define string option - for schematic label
pars.add_argument('--sch_label',
type = str,
default = 'label',
help = 'Schematic symbol label')
# number of left side connectors
pars.add_argument('--left_connectors',
type = int,
default = '0',
help = 'left connectors')
# number of bottom side connectors
pars.add_argument('--bottom_connectors',
type = int,
default = '0',
help = 'bottom connectors')
# number of right side connectors
pars.add_argument('--right_connectors',
type = int,
default = '0',
help = 'right connectors')
# number of top side connectors
pars.add_argument('--top_connectors',
type = int,
default = '0',
help = 'top connectors')
# pin numbering
pars.add_argument('--pin_num',
type = int,
default = '0',
help = 'some help')
# pin labelling
pars.add_argument('--pin_label',
type = int,
default = '0',
help = 'some help')
# connector terminalIDs
pars.add_argument('--terminal_id',
type=inkex.Boolean,
default = 'false',
help = 'some help')
# here so we can have tabs - but we do not use it directly - else error
self.arg_parser.add_argument("-none", "--active-tab",
type= str, dest="active_tab", default='symbol', # use a legitmate default
help="Active tab.")
# end of - def add_arguments(self, pars):
def effect(self):
# actual effect starts here - this executes when extension is 'applied'
# define variables/lists needed
connector_number = 1 # connector number, used for naming connector pins
result = True # error flag
schematic_pin_num = [] # stores schematic pin numbers
schematic_pin_label = [] # stores schematic pin labels
x_offset = 0 # x offset of schematic symbol from the left of page
y_offset = 0 # y offset of schematic symbol from top of page
connector_pin_attrib = [] # connector pin attribute list - stores values for pin creation
connector_term_attrib = [] # connector terminalID attribute list - stores values for terminalID creation
svg = self.document.getroot()
# Again, there are two ways to get the attibutes:
org_doc_width = self.svg.get('width') # not sure what units - they are px
org_doc_height = self.svg.attrib['height'] # size is in pixels
#sys.stderr.write(str(org_doc_width) + " " + str(org_doc_height) + "\n\n" )
org_doc_viewbox = self.svg.get('viewBox')
#sys.stderr.write(org_doc_viewbox + "\n\n")
# get user input values
schematic_width = round(self.options.x_size, 2) # width of schematic box symbol - trim off extra decimal places
schematic_height = round(self.options.y_size, 2) # height of schematic box symbol - trim off extra decimal places
label_text = self.options.sch_label # text for symbol label
num_left_pins = self.options.left_connectors # number of connectors on left side of schematic symbol
num_bottom_pins = self.options.bottom_connectors # number of connectors on top of schematic symbol
num_right_pins = self.options.right_connectors # number of connectors on right of schematic symbol
num_top_pins = self.options.top_connectors # number of connectors on top of schematic symbol
# check if too many pins for size of symbol = user input error
result = error_check_params(schematic_width, schematic_height, num_left_pins, num_bottom_pins, num_right_pins, num_top_pins)
if result is False:
return # exit extension
# delete current layer and create a new group named - schematic
layer = self.svg.get_current_layer()
layer.delete()
group = self.svg.get_current_layer().add(inkex.Group(id="schematic"))
# set up document
x_offset, y_offset = prep_doc(self, schematic_width, schematic_height, num_left_pins, num_bottom_pins, num_right_pins, num_top_pins) # set document settings
# create symbol
create_rect_symbol(self, group, schematic_width, schematic_height, x_offset, y_offset) # create rectangular symbol
# create symbol label
create_symbol_label(self, group, schematic_width, schematic_height, x_offset, y_offset, label_text) # create the label
# create left connector pins
if num_left_pins > 0:
# create left pins, pin numbers and pin labels
result = create_left_pins(self,
group,
num_left_pins,
schematic_width,
schematic_height,
x_offset,
y_offset,
connector_number,
schematic_pin_num,
schematic_pin_label,
connector_pin_attrib,
connector_term_attrib)
if result is False:
sys.stderr.write("\n Invalid pin number/label entry - left side\n\n")
sys.stderr.write(" extension failed!\n")
error_cleanup(self, group, org_doc_width, org_doc_height, org_doc_viewbox) # restore inkscape settings changed
return # exit extension
connector_number = num_left_pins + 1 # increment connector_number counter
# create bottom connector pins
if num_bottom_pins > 0:
# create bottom pins, pin numbers and pin labels
result = create_bottom_pins(self,
group,
num_bottom_pins,
schematic_width,
schematic_height,
x_offset,
y_offset,
connector_number,
schematic_pin_num,
schematic_pin_label,
connector_pin_attrib,
connector_term_attrib)
if result is False:
sys.stderr.write("\n Invalid pin number/label entry - bottom side\n\n")
sys.stderr.write(" extension failed!\n")
error_cleanup(self, group, org_doc_width, org_doc_height, org_doc_viewbox) # restore inkscape settings changed
return # exit extension
connector_number = num_left_pins + num_bottom_pins + 1 # increment connector_number counter
# create right connector pins
if num_right_pins > 0:
# create right pins, pin numbers and pin labels
result = create_right_pins(self,
group,
num_right_pins,
schematic_width,
schematic_height,
x_offset,
y_offset,
connector_number,
schematic_pin_num,
schematic_pin_label,
connector_pin_attrib,
connector_term_attrib)
if result is False:
sys.stderr.write("\n Invalid pin number/label entry - right side\n\n")
sys.stderr.write(" extension failed!\n")
error_cleanup(self, group, org_doc_width, org_doc_height, org_doc_viewbox) # restore inkscape settings changed
return # exit extension
connector_number = num_left_pins + num_bottom_pins + num_right_pins + 1 # increment connector_number counter
# create top connector pins
if num_top_pins > 0:
# create top pins, pin numbers and pin labels
result = create_top_pins(self,
group,
num_top_pins,
schematic_width,
schematic_height,
x_offset,
y_offset,
connector_number,
schematic_pin_num,
schematic_pin_label,
connector_pin_attrib,
connector_term_attrib)
if result is False:
sys.stderr.write("\n Invalid pin number/label entry - top side\n\n")
sys.stderr.write(" extension failed!\n")
error_cleanup(self, group, org_doc_width, org_doc_height, org_doc_viewbox) # restore inkscape settings changed
return # exit extension
# now create the actual pins in the .svg document
for index in range(0, len(connector_pin_attrib)):
create_pin(self, group, connector_pin_attrib[index])
if self.options.terminal_id is True:
create_terminalID(self, group, connector_term_attrib[index])
return # exit extension
# end of - def effect(self):
# end of - class FritzingSchematic(inkex.EffectExtension):
''' functions start here: '''
def prep_doc(self, x_size, y_size, num_left_pins, num_bottom_pins, num_right_pins, num_top_pins):
# adjusts some inkscape settings to match what is needed for fritzing
# inputs:
# x_size - width of schematic symbol
# y_size - height of schematic symbol
# num_left_pins - number of pins on left side of symbol
# num_bottom_pins - number of pins on bottom side of symbol
# num_right_pins - number of pins on right side of symbol
# num_top_pins - number of pins on top side of symbol
# returns:
# x_symbol_offset - location of symbol from left side of document
# y_symbol_offset - location of symbol from top side of document
#
svg = self.document.getroot()
# calculate document width
if num_left_pins > 0: # check for left pins
x_size = x_size + .105 # add to the value for space for left pins
x_symbol_offset = 105 # space to offset the symbol from the left side of document
else: # no left pins
x_size = x_size + .005 # just add space for symbol stroke
x_symbol_offset = 5 # offset for symbol stroke
if num_right_pins > 0: # check for right pins
x_size = x_size + .105 # add to the value for space for right pins
else:
x_size = x_size + .005 # add space for symbol stroke
viewbox_width = x_size * FritzingSchematic.inch_scale_factor # get viewBox value
#x_size = x_size / FritzingSchematic.pixel_scale_factor # scale down in size to get doc width
# calculate document height
if num_bottom_pins > 0: # check for bottom pins
y_size = y_size + .105 # add to this value for space for the bottom pins
else:
y_size = y_size + .005 # no bottom pins, so just add space for stroke of symbol
if num_top_pins > 0: # check for top pins
y_size = y_size + .105 # add to the value for space for the top pins
y_symbol_offset = 105 # offset from top of document to place symbol
else: # no top pins
y_size = y_size + .005 # add a little bit of space for pin stroke
y_symbol_offset = 5 # offset from top of document to place symbol
viewbox_height = y_size * FritzingSchematic.inch_scale_factor # get viewBox value
svg.set('width', str(x_size)+'in') # set doc width
svg.set('height', str(y_size)+'in') # set doc height
viewbox_value = "0 0 " + str(viewbox_width) + " " + str(viewbox_height) # conver viewBox value to string
svg.set('viewBox', viewbox_value) # set viewbox value
return x_symbol_offset, y_symbol_offset
# end of - def prep_doc(self, x_size, y_size, num_left_pins, num_bottom_pins, num_right_pins, num_top_pins):
def create_rect_symbol(self, group, x_size, y_size, x_offset, y_offset):
# creates a rectanglar box for a schematic symbol
# inputs:
# group - layer (or group) to add symbol to
# x_size - width of schematic symbol
# y_size - height of schematic symbol
# x_offset - space from left edge of document to place symbol
# y_offset - space from top of document to place symbol
# returns:
# nothing
x_size = x_size * FritzingSchematic.inch_scale_factor # user input in inches, so scale in size
symbol_width = str(x_size) # x size of box
y_size = y_size * FritzingSchematic.inch_scale_factor # user input in inches, so scale in size
symbol_height = str(y_size) # y size of box
svg = self.document.getroot()
# get offset of where to place symbol
x_loc = str(x_offset)
y_loc = str(y_offset)
# define rect attributes
attribs = {
'fill' : 'none',
'height' : symbol_height,
'width' : symbol_width,
'stroke-width' : '10',
'stroke' : '#000000',
'stroke-linejoin' : 'round',
'stroke-linecap' : 'round',
'id' : 'symbol',
'x' : x_loc,
'y' : y_loc}
newObj = etree.SubElement(group, inkex.addNS('rect','svg'), attribs )
return
# end of - def create_rect_symbol(self, group, x_size, y_size, x_offset, y_offset):
def create_symbol_label(self, group, x_size, y_size, x_offset, y_offset, label_name):
# create a text label for schematic symbol
# inputs:
# group - the group to add the label to
# x_size - width of schematic symbol
# y_size - height of schematic symbol
# x_offset - space from left edge of document to the symbol left edge
# y_offset - space from top of document to the top of the symbol
# label_name - name for the symbol label - user input
# returns:
# nothing
#
svg = self.document.getroot()
# Create text element
text = etree.Element(inkex.addNS('text','svg'))
text.text = label_name
# Set text position
# center width is x_offset + (x_size / 2)
x_size = x_size * FritzingSchematic.inch_scale_factor # user input in inches, so scale in size
center_width = str( ( ( x_size / 2 ) + x_offset) )
text.set('x', center_width)
# center height is y_offset + (y_size / 2)
y_size = y_size * FritzingSchematic.inch_scale_factor # user input in inches, so scale in size
center_height = str( ( y_size / 2 ) + y_offset )
text.set('y', center_height)
# Set text properties
text.set('font-family', 'Droid Sans')
text.set('font-size', '60')
text.set('fill', '#000000')
text.set('id', 'label')
text.set('text-anchor', 'middle')
# Connect elements together.
group.append(text)
return
# end of - def create_symbol_label(self, label_name):
def create_left_pins(self, group, num_pins, width, height, x_offset, y_offset, connect_num, schematic_pin_num, schematic_pin_label, connector_pin_attrib, connector_term_attrib):
# create left side pins for schematic symbol
# inputs:
# group - the group to add the pins to
# num_pins - number of pins to create
# width - size of schematic width in inches - user input
# height - size of shcematic height in inches - user input
# x_offset - x location of schematic symbol's left side
# y_offset - y location of schematic symbol's top side
# connect_num - the number to start using to name connectors created
# schematic_pin_num - user defined pin number list
# schematic_pin_label - user defined pin label list
# connector_pin_attrib - connector pin attributes - used later to create pins
# connector_term_attrib - connector terminalID attributes - used to create terminalIDs
# returns:
# True if successful user input
# False if failed user input
#
# x location of pin
x_pos1 = x_offset # left side of schematic symbol box
# length of pin...
x_pos2 = x_pos1 - 100 # should be set to 100, or 0.1 inch
# y location of pin
# get y location of top most possible pin position
y_pos = y_offset # top of schematic symbol
y_pos = y_pos + 100 # add 0.10 inch to position so pin isn't at top of symbol
# attempt to center pin vertically - works so-so
# determine offset from top most pin position of
# where the pins should start at
temp_height = (height * 10) # multiply by ten to go from decimal input to whole intger
if num_pins < temp_height:
temp_offset = temp_height - num_pins
temp_offset = int((temp_offset / 2))
else:
temp_offset = (temp_height)
y_pos = y_pos + ((temp_offset * 100 ) )
# check for user input
if ((self.options.pin_num == 1) or (self.options.pin_label == 1)): # user defined pin numbers or labels
user_pin_number_label(self, num_pins, connect_num, schematic_pin_num, schematic_pin_label, "Left")
if (self.options.pin_num == 1): # user defined pin numbers
if len(schematic_pin_num) < (connect_num - 1 + num_pins): # check that all pin numbers were entered by user
return bool(False) # return False - an error on input
else: # sequential pin numbers
connect_number = connect_num
for index in range(num_pins): # loop thru and number the pins
schematic_pin_num.append(connect_number)
connect_number = connect_number + 1
if (self.options.pin_label == 1): # user defined pin labels
if len(schematic_pin_label) < (connect_num - 1 + num_pins): # check that all pin labels were entered by user
sys.stderr.write("length of schematic_pin_label = " + str(len( schematic_pin_label)) + "\n")
sys.stderr.write("schematic_pin_label = " + str(schematic_pin_label) + "\n\n")
return bool(False) # return False - an error on input
else: # sequential pin labels
connect_number = connect_num
for index in range(num_pins): # loop thru and label the pins
schematic_pin_label.append("pin" + str(schematic_pin_num[connect_number - 1] )) # pin label should be text 'pin' + connector number of the pin
connect_number = connect_number + 1 # and not this sequentially counting number
# end of check user input
# create the pins, pin numbers, pin labels, and terminalIDs
for index in range(connect_num, connect_num + num_pins):
connect_num = schematic_pin_num[index - 1] - 1
connect_name = "connector" + str(connect_num) + "pin"
pin_attribs = set_pin_attribs(connect_name, x_pos1, x_pos2, y_pos, y_pos)
# add pin_attribs dict to connector_pin_attrib list for pin creation later on
connector_pin_attrib.append(pin_attribs)
# create pin number/label
create_pin_number(self, group, (connect_num + 1), x_pos1, x_pos2, y_pos, y_pos)
create_pin_label(self, group, (schematic_pin_label[index - 1]), (connect_num + 1), x_pos1, x_pos2, y_pos, y_pos)
# terminal ID creation
if self.options.terminal_id is True:
terminal_id_name = "connector" + str(connect_num) + "terminal"
id_attribs = set_term_attribs(terminal_id_name, x_pos2 - 5, y_pos - 5)
connector_term_attrib.append(id_attribs)
# first pin created at starting position for pins
# now figure out location for next pin
y_pos = y_pos + 100
# end of create pins, pin numbers, and pin labels
return bool(True)
# end of - def create_left_pins(self, group, num_pins, width, height, x_offset, y_offset, connect_num, schematic_pin_num, schematic_pin_label, connector_pin_attrib, connector_term_attrib):
def create_bottom_pins(self, group, num_pins, width, height, x_offset, y_offset, connect_num, schematic_pin_num, schematic_pin_label, connector_pin_attrib, connector_term_attrib):
# create bottom pins for schematic symbol
# inputs:
# group - the group to add the pins to
# num_pins - number of pins to create
# width - size of schematic width in inches - user input
# height - size of shcematic height in inches - user input
# x_offset - x location of schematic symbol's left side
# y_offset - y location of schematic symbol's top side
# connect_num - the number to start using to name connectors created
# schematic_pin_num - user defined pin numbering list
# schematic_pin_label - user defined pin label list
# connector_pin_attrib - connector pin attibutes - used later to create pins
# connector_term_attrib - connector terminalID attributes - used to create terminalIDs
# returns:
# True if successful user input
# False if failed user input
#
# get x location of left most pin position
x_pos = x_offset # left side of schematic symbol
x_pos = x_pos + 100 # add 0.10 inch to position
# determine offset from left most pin position of
# where the pins should start at
temp_width = (width * 10) # multiply by ten to go from decimal input to whole intger
if num_pins <= temp_width: # schematic symbol wider than # of pins
temp_offset = temp_width - num_pins # calculate offset
temp_offset = int( ( temp_offset / 2 ) )
else:
temp_offset = temp_width
x_pos = x_pos + (temp_offset * 100) # scale offset
# y location of pin
y_pos1 = y_offset + (height * 1000)
# length of pin...
y_pos2 = y_pos1 + 100 # should be set to 100, or 0.1 inch
# check for user input
if ((self.options.pin_num == 1) or (self.options.pin_label == 1)): # user defined pin numbers or labels
user_pin_number_label(self, num_pins, connect_num, schematic_pin_num, schematic_pin_label, "Bottom")
#sys.stderr.write(" user_pin_number_label result = " + str(result) + "\n\n")
if (self.options.pin_num == 1): # user defined pin numbers
if len(schematic_pin_num) < (connect_num - 1 + num_pins): # check that all pin numbers were entered by user
return bool(False) # return False - an error on input
else: # sequential pin numbers
connect_number = connect_num
for index in range(num_pins): # loop thru and number the pins
schematic_pin_num.append(connect_number)
connect_number = connect_number + 1
if (self.options.pin_label == 1): # user defined pin labels
if len(schematic_pin_label) < (connect_num - 1 + num_pins): # check that all pin labels were entered by user
return bool(False) # return False - an error on input
else: # sequential pin labels
connect_number = connect_num
for index in range(num_pins): # loop thru and label the pins
schematic_pin_label.append("pin" + str(schematic_pin_num[connect_number - 1] )) # pin label should be text 'pin' + connector number of the pin
connect_number = connect_number + 1 # and not this sequentially counting number
# end of check user input
# create the pins, pin numbers, and pin labels
for index in range(connect_num, connect_num + num_pins):
connect_num = schematic_pin_num[index-1] - 1
connect_name = "connector" + str(connect_num) + "pin"
pin_attribs = set_pin_attribs(connect_name, x_pos, x_pos, y_pos1, y_pos2)
# add pin_attribs dict to connector_pin_attrib list for pin creation later on
connector_pin_attrib.append(pin_attribs)
# create pin number/label
create_pin_number(self, group, (connect_num + 1), x_pos, x_pos, y_pos1, y_pos2)
create_pin_label(self, group, (schematic_pin_label[index - 1]), (connect_num + 1), x_pos, x_pos, y_pos1, y_pos2)
# terminal ID creation
if self.options.terminal_id is True:
terminal_id_name = "connector" + str(connect_num) + "terminal"
id_attribs = set_term_attribs(terminal_id_name, x_pos - 5, y_pos2 - 5)
connector_term_attrib.append(id_attribs)
# first pin created at starting position for pins
# now figure out location for next pin
x_pos = x_pos + 100
# end of create pins, pin numbers, and pin labels
return True # return true, all pins created
# end of - def create_bottom_pins(self, group, num_pins, width, height, x_offset, y_offset, connect_num, schematic_pin_num, schematic_pin_label, connector_pin_attrib, connector_term_attrib):
def create_right_pins(self, group, num_pins, width, height, x_offset, y_offset, connect_num, schematic_pin_num, schematic_pin_label, connector_pin_attrib, connector_term_attrib):
# create right side pins for schematic symbol
# inputs:
# group - the group to add the pins to
# num_pins - number of pins to create
# width - size of schematic width in inches - user input
# height - size of shcematic height in inches - user input
# x_offset - x location of schematic symbol's left side
# y_offset - y location of schematic symbol's top side
# connect_num - the number to start using to name connectors created
# schematic_pin_num - user defined pin numbering list
# schematic_pin_label - user defined pin label list
# connector_pin_attrib - connector pin attibutes - used later to create pins
# connector_term_attrib - connector terminalID attibutes - used later to create terminalIDs
# returns:
# True if successful user input
# False if failed user input
# #
svg = self.document.getroot()
# x location of pin
x_pos1 = x_offset + (width * 1000) # find right side of schematic symbol
# length of pin...
x_pos2 = x_pos1 + 100 # should be set to 100, or 0.1 inch
# y location of pin
# get y location of right most possible pin position
y_pos = y_offset + (height * 1000) # right side of schematic symbol
y_pos = y_pos - 100 # subtract 0.10 inch to position
# attempt to center pin vertically - works so-so
# determine offset from left most pin position of
# where the pins should start at
temp_height = (height * 10) # multiply by ten to go from decimal input to whole intger
if num_pins < temp_height:
temp_offset = temp_height - num_pins
temp_offset = int((temp_offset / 2))
else:
temp_offset = (temp_height)
y_pos = y_pos - (temp_offset * 100)
#y1_value = str(y_pos)
#
if ((self.options.pin_num == 1) or (self.options.pin_label == 1)): # user defined pin numbers or labels
result = user_pin_number_label(self, num_pins, connect_num, schematic_pin_num, schematic_pin_label, "Right")
#sys.stderr.write(" user_pin_number_label result = " + str(result) + "\n\n")
if (self.options.pin_num == 1): # user defined pin numbers
if len(schematic_pin_num) < (connect_num - 1 + num_pins): # check that all pin numbers were entered by user
return bool(False)
else: # sequential pin numbers
connect_number = connect_num
for index in range(num_pins):
schematic_pin_num.append(connect_number)
connect_number = connect_number + 1
if (self.options.pin_label == 1): # user defined pin labels
if len(schematic_pin_label) < (connect_num - 1 + num_pins): # check that all pin labels were entered by user
return bool(False)
else:
connect_number = connect_num
for index in range(num_pins):
schematic_pin_label.append("pin" + str(schematic_pin_num[connect_number - 1] )) # pin label should be text 'pin' + connector number of the pin
connect_number = connect_number + 1 # and not this sequentially counting number
#
# create the pins, pin numbers, and pin labels
for index in range(connect_num, connect_num + num_pins):
connect_num = schematic_pin_num[index-1] - 1
connect_name = "connector" + str(connect_num) + "pin"
pin_attribs = set_pin_attribs(connect_name, x_pos1, x_pos2, y_pos, y_pos)
# add pin_attribs dict to connector_pin_attrib list for pin creation later on
connector_pin_attrib.append(pin_attribs)
# create pin number/label
create_pin_number(self, group, (connect_num + 1), x_pos1, x_pos2, y_pos, y_pos)
create_pin_label(self, group, (schematic_pin_label[index - 1]), (connect_num + 1), x_pos1, x_pos2, y_pos, y_pos)
# terminal ID creation
if self.options.terminal_id is True:
terminal_id_name = "connector" + str(connect_num) + "terminal"
id_attribs = set_term_attribs(terminal_id_name, x_pos2 - 5, y_pos - 5)
connector_term_attrib.append(id_attribs)
# first pin created at starting position for pins
# now figure out location for next pin
y_pos = y_pos - 100
y1_value = str(y_pos)
# end of create pins, pin numbers, and pin labels
return
# end of - def create_right_pins(self, group, num_pins, width, height, x_offset, y_offset, connect_num, schematic_pin_num, schematic_pin_label, connector_pin_attrib, connector_term_attrib):
def create_top_pins(self, group, num_pins, width, height, x_offset, y_offset, connect_num, schematic_pin_num, schematic_pin_label, connector_pin_attrib, connector_term_attrib):
# create top pins for schematic symbol
# inputs:
# group - the group to add the pins to
# num_pins - number of pins to create
# width - size of schematic width in inches - user input
# height - size of shcematic height in inches - user input
# x_offset - x loc of schematic symbol's left side
# y_offset - y location of schematic symbol's top side
# connect_num - the number to start using to name connectors created
# schematic_pin_num - user defined pin numbering list
# schematic_pin_label - user defined pin label list
# connector_pin_attrib - connector pin attibutes - used later to create pins
# connector_term_attrib - connector terminalID attributes - used to create terminalIDs
# returns:
# True if successful user input
# False if failed user input
#
svg = self.document.getroot()
# x location of pin
# get x location of right most pin position
x_pos = x_offset + (width * 1000)
x_pos = x_pos - 100 # subtract 2.54mm (0.10 inch) to position so pin isn't on edge of symbol
# determine offset from right most pin position of
# where the pins should start at
temp_width = (width * 10) # multiply by ten to go from decimal input to whole intger
if num_pins <= temp_width: # schematic symbol wider than # of pins
temp_offset = temp_width - num_pins # calculate offset
temp_offset = int( ( temp_offset / 2 ) )
else:
temp_offset = temp_width
x_pos = x_pos - (temp_offset * 100) # scale offset
#x1_value = str(x_pos)
# y location of pin
y_pos1 = y_offset # find top of schematic symbol
#y1_value = str(y_pos1)
# length of pin...
y_pos2 = y_pos1 - 100
#y2_value = str(y_pos2)
#
if ((self.options.pin_num == 1) or (self.options.pin_label == 1)): # user defined pin numbers or labels
result = user_pin_number_label(self, num_pins, connect_num, schematic_pin_num, schematic_pin_label, "Top")
#sys.stderr.write(" user_pin_number_label result = " + str(result) + "\n\n")
if (self.options.pin_num == 1): # user defined pin numbers
if len(schematic_pin_num) < (connect_num - 1 + num_pins): # check that all pin numbers were entered by user
return bool(False)
else: # sequential pin numbers
connect_number = connect_num
for index in range(num_pins):
schematic_pin_num.append(connect_number)
connect_number = connect_number + 1
if (self.options.pin_label == 1): # user defined pin labels
if len(schematic_pin_label) < (connect_num - 1 + num_pins): # check that all pin labels were entered by user
return bool(False)
else:
connect_number = connect_num
for index in range(num_pins):
schematic_pin_label.append("pin" + str(schematic_pin_num[connect_number - 1] )) # pin label should be text 'pin' + connector number of the pin
connect_number = connect_number + 1 # and not this sequentially counting number
#
# create the pins, pin numbers, and pin labels
for index in range(connect_num, connect_num + num_pins):
connect_num = schematic_pin_num[index-1] - 1
connect_name = "connector" + str(connect_num) + "pin"
pin_attribs = set_pin_attribs(connect_name, x_pos, x_pos, y_pos1, y_pos2)
# add pin_attribs dict to connector_pin_attrib list for pin creation later on
connector_pin_attrib.append(pin_attribs)
# create pin number/label
create_pin_number(self, group, (connect_num + 1), x_pos, x_pos, y_pos1, y_pos2)
create_pin_label(self, group, (schematic_pin_label[index - 1]), (connect_num + 1), x_pos, x_pos, y_pos1, y_pos2)
# terminal ID creation
if self.options.terminal_id is True:
terminal_id_name = "connector" + str(connect_num) + "terminal"
id_attribs = set_term_attribs(terminal_id_name, x_pos - 5, y_pos2 - 5)
connector_term_attrib.append(id_attribs)
# first pin created at starting position for pins
# now figure out location for next pin
x_pos = x_pos - 100
x1_value = str(x_pos)
# end of create pins, pin numbers, and pin labels
return
# end of - def create_top_pins(self, group, num_pins, width, height, x_offset, y_offset, connect_num, schematic_pin_num, schematic_pin_label, connector_pin_attrib, connector_term_attrib):
def set_pin_attribs(pin_name, x1_loc, x2_loc, y1_loc, y2_loc):
# set the attributes for the graphic element that will be the connector pin
# inputs:
# pin_name - name used for the connector pin
# x1_loc - x location of the pin at schematic symbol
# x2_loc - x location of other end of the pin
# y1_loc - y location of one end of the pin
# y2_loc - y location of other end of the pin
# returns:
# pin_attribs{} - a dict of strings that conatins the info inkscape needs to create the graphic
#
# x location of pin
x1_value = str(x1_loc)
x2_value = str(x2_loc)
# y location of pin
y1_value = str(y1_loc)
y2_value = str(y2_loc)
# define pin attributes
pin_attribs = {
'fill' : 'none',
'id' : pin_name,
'stroke' : '#555555',
'stroke-linecap' : 'round',
'stroke-width' : '10',
'x1' : x1_value,
'x2' : x2_value,
'y1' : y1_value,
'y2' : y2_value
}
return pin_attribs
# end of - def set_pin_attribs(pin_name, x1_loc, x2_loc, y1_loc, y2_loc)
def set_term_attribs(term_name, x_loc, y_loc):
# set the terminal IDs attributes for the grahic element that will be created
# inputs:
# term_name - name of the terminal ID
x_value = str(x_loc)
y_value = str(y_loc)
# define connector terminal ID attibutes
id_attribs = {
'fill' : 'none',
'id' : term_name,
'height' : '10',
'width' : '10',
'style' : 'fill:none;stroke:none;stroke-width:0',
'x' : x_value,
'y' : y_value
}
return id_attribs
# end of - def set_term_attribs(term_name, x_loc, y_loc):
def create_pin_number(self, group, connect_num, x_pos1, x_pos2, y_pos1, y_pos2):
# create pin number for connector
# inputs:
# group - the group to add the pin number to
# connect_num - number to use for pin
# x_pos1 - the x location of the end of the pin by the schematic symbol
# x_pos2 - the x location of the other end of the pin
# y_pos1 - the y location of the end of the pin by the schematic symbol
# y_pos2 - the y location the the other end of the pin
# returns:
# nothing
#
# Create text element
text = etree.Element(inkex.addNS('text','svg'))
pin_number = str(connect_num)
text.text = pin_number
# check pin orientation to decide
# where and how to place pin number
if y_pos1 == y_pos2:
# we have a horizontal pin
if x_pos1 > x_pos2:
# we have a left side pin
# calculate pin number location
x_loc = ( x_pos1 - 30 ) # x location
y_loc = ( y_pos1 - 20 ) # y location
text.set('x', str(x_loc))
text.set('y', str(y_loc))
text.set('text-anchor', 'end')
else:
# we have a right side pin
# calculate pin number location
x_loc = ( x_pos1 + 30 ) # x location
y_loc = ( y_pos1 - 20 ) # y location
text.set('x', str(x_loc))
text.set('y', str(y_loc))
text.set('text-anchor', 'start')
else:
# we have a vertical pin
if y_pos1 < y_pos2:
# we have a bottom pin
# calculate pin number location
y_loc = ( y_pos1 + 50 ) # y location
x_loc = ( x_pos1 - 20 ) # x location
# Set text position
text.set('x', str(x_loc))
text.set('y', str(y_loc))
text.set('text-anchor', 'end')
else:
# we have a top pin
# calculate pin number location
y_loc = ( y_pos1 - 30 ) # y location
x_loc = ( x_pos1 - 20 ) # x location
# Set text position
text.set('x', str(x_loc))
text.set('y', str(y_loc))
text.set('text-anchor', 'end')
# create id for the pin number
pin_num_id = 'pin' + str(connect_num - 1) + 'num'
# Set text properties
text.set('font-family', 'Droid Sans')
text.set('font-size', '35')
text.set('fill', '#555555')
text.set('id', pin_num_id)
# Connect elements together.