summaryrefslogtreecommitdiff
path: root/views/script/netlist.js
blob: 72766c20626a89ab5038ea97818ad7e497db87f8 (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
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
var netlistcreator={
  matrixxform:function(point,matrix){
    var pin=webtronics.circuit.svgRoot.createSVGPoint();
    pin.x=point.x;
    pin.y=point.y;
    pin=pin.matrixTransform(matrix);
    return {x:Math.round(pin.x),y:Math.round(pin.y)};
  },

  /*tests if 2 point are within 3 pixels of each other*/
  ispoint:function(point1,point2){
    return (Math.abs(point2.x-point1.x)<3)&&(Math.abs(point2.y-point1.y)<3); 
  },

  sortnetlist:function(list){
    var G=[];
    var S=[];
    var A=[];
    var B=[];
    var C=[];
    var D=[];
    var I=[];
    var J=[];
    var K=[];
    var L=[];
    var M=[];
    var N=[];
    var P=[];
    var Q=[];
    var R=[];
    var U=[];
    var V=[];
    var wire=[];
    var other=[]
    for(var i=0;i<list.length;i++){
      if(list[i].type=='gnd'){
        G.push(list[i]);
      }
      else if(list[i].type=='v'){
        V.push(list[i]);
      }
      else if(list[i].type=='wire'){
        wire.push(list[i]);			
      }		
      else if(list[i].type=='b'){
        B.push(list[i]);
      }
      else if(list[i].type=='c'){
        C.push(list[i]);	
      }
      else if(list[i].type=='d'){
        D.push(list[i]);
      }
      else if(list[i].type=='i'){	
        J.push(list[i]);
      }
      else if(list[i].type=='j'){	
        J.push(list[i]);
      }
      else if(list[i].type=='k'){
        K.push(list[i]);
      }
      else if(list[i].type=='l'){
        L.push(list[i]);
      }
      else if(list[i].type=='m'){
        M.push(list[i]);
      }
      else if(list[i].type=='n'){
        N.push(list[i]);
      }
      else if(list[i].type=='plot'){
        P.push(list[i]);
      }
      else if(list[i].type=='q'){
        Q.push(list[i]);
      }
      else if(list[i].type=='r'){
        R.push(list[i]);
      }
      else if(list[i].type=='u'){	
        U.push(list[i]);
      }
      /* this is the best way I could think to tell if a part i digital */
      else if(list[i].category=="digital"){	
        A.push(list[i]);
      }
      else {
        list[i].error='unknown device';
        other.push(list[i]);
      }
    }

    var sortfunction=function(a,b){
      var apart=a.id.replace(a.type,"");
      var bpart=b.id.replace(b.type,"");
      if(!apart)apart=0;
      if(!bpart)bpart=0;
      return (apart>bpart);
    };
    V.sort(sortfunction);
    wire.sort(sortfunction);
    B.sort(sortfunction);
    C.sort(sortfunction);
    D.sort(sortfunction);
    I.sort(sortfunction);
    J.sort(sortfunction);
    K.sort(sortfunction);
    L.sort(sortfunction);
    M.sort(sortfunction);
    N.sort(sortfunction);
    P.sort(sortfunction);
    Q.sort(sortfunction);
    R.sort(sortfunction);
    U.sort(sortfunction);
    A.sort(sortfunction);

    var newlist=[];
    G.each(function(item){newlist.push(item)});		
    G.reverse();
    V.each(function(item){newlist.push(item)});		
    wire.each(function(item){newlist.push(item)});		
    B.each(function(item){newlist.push(item)});		
    C.each(function(item){newlist.push(item)});		
    D.each(function(item){newlist.push(item)});		
    I.each(function(item){newlist.push(item)});		
    J.each(function(item){newlist.push(item)});		
    K.each(function(item){newlist.push(item)});		
    L.each(function(item){newlist.push(item)});		
    M.each(function(item){newlist.push(item)});		
    N.each(function(item){newlist.push(item)});		
    Q.each(function(item){newlist.push(item)});		
    R.each(function(item){newlist.push(item)});
    U.each(function(item){newlist.push(item)});		
    A.each(function(item){newlist.push(item)});		
    other.each(function(item){newlist.push(item)});		

    /*plots go last*/
    P.each(function(item){newlist.push(item)});		


    return newlist;
  },

  /* draws wires to namewire ports with the same id*/
  connectnamewires:function(list){

    for(var i=0;i<list.length;i++){
      if((list[i].type=="wire") || (list[i].type=="gnd")){
        for(var j=i;j<list.length;j++){
         if( (list[i]!=list[j]) && ((list[i].id==list[j].id) || (list[i].type=="gnd" && list[j].type=="gnd"))   ){
           var line= webtronics.circuit.createline('yellow',1,list[i]['analogpins'][0]['x'],list[i]['analogpins'][0]['y'],list[j]['analogpins'][0]['x'],list[j]['analogpins'][0]['y']);
           line.setAttributeNS(null,'class','webtronics_namewire_connector');
           webtronics.circuit.info.appendChild(line);
	  //console.log(line);            
	  break; 
	}
}    
}
}
},

/*check for vectors and convert them*/
tovector:function(pin,nodenumber){
  var v ="";   
  if(pin.parentNode.tagName=="wtx:vector"){
    var vector=Element.descendants(pin.parentNode);
    if(pin==vector[0]){v+="["}
      v+="a"+nodenumber;
    if(pin==vector[vector.length-1]){v+="]";}
  }
  else{
    v+="a"+nodenumber;
  }
  
  return v;
},

/*
 *    <wtx:pins>
 *		<wtx:analog>
 *		  <wtx:node index="1" x="0" y="10"></wtx:node>
 *		  <wtx:node index="2" x="40" y="10"></wtx:node>
 *		</wtx:analog>
 *    </wtx:pins>
 *    <wtx:id>r</wtx:id>
 *    <wtx:type>r</wtx:type>
 *    <wtx:name>testresistor</wtx:name>
 *    <wtx:category>resistors</wtx:category>
 *    <wtx:value></wtx:value>
 *    <wtx:label></wtx:label>
 *    <wtx:spice></wtx:spice>
 *    <wtx:flip></wtx:flip>
 *    <wtx:model></wtx:model>
 */


 getwtxdata:function(parts){
  list=[];
  for(var i=0;i<parts.length;i++){
    var part={error:"", elem:{}, analogpins:[],digitalpins:[],amplitude:"",phase:"",offsetvoltage:"",voltageamplitude:"",frequency:"",delaytime:"",dampingfactor:"",type:"", name:"", category:"", value:"", spice:"", model:"",measure:"",
    pulval1:"",pulval2:"",pulval3:"",pulval4:"",pulval5:"",pulval6:"",pulval7:"",eval1:"",eval2:"",eval3:"",eval4:"",eval5:"",eval6:"", pwlval1:"",pwlval2:"",pwlval3:"",pwlval4:"",pwlval5:"",pwlval6:"",pwlval7:"",pwlval8:""
  }
    /*
     *        try{
     *            part.nodes=this.getwtxpins(part[i]);        
  }
  catch{part.error="wtx:pins not found"}
  */
  part.elem=parts[i];

  try{
    var category=webtronics.circuit.getwtxtagname(parts[i],"analog")[0];
    var nodes = webtronics.circuit.getwtxtagname(category,"node");
    for(var j=0;j<nodes.length;j++){
     var point = this.matrixxform( {x:webtronics.circuit.getwtxattribute(nodes[j],"x"),y:webtronics.circuit.getwtxattribute(nodes[j],"y")},webtronics.circuit.parseMatrix(part.elem));
     part.analogpins.push({index:webtronics.circuit.getwtxattribute(nodes[j],"index"),x:point.x,y:point.y,node:undefined}) ;
   }
      //sort nodes int correct order
      part.analogpins.sort(function(a,b){if (a.name > b.name)return 1;if (a.name < b.name)return -1;return 0;});
    }
    catch(e){console.log("no analog pins found");}
    
    try{
      var category=webtronics.circuit.getwtxtagname(parts[i],"digital")[0];
      var nodes = webtronics.circuit.getwtxtagname(category,"node");
      for(var j=0;j<nodes.length;j++){
       var point = this.matrixxform( {x:webtronics.circuit.getwtxattribute(nodes[j],"x"),y:webtronics.circuit.getwtxattribute(nodes[j],"y")},webtronics.circuit.parseMatrix(part.elem));
       part.digitalpins.push({index:webtronics.circuit.getwtxattribute(nodes[j],"index"),x:point.x,y:point.y,node:undefined}) ;
     }
     part.digitalpins.sort(function(a,b){if (a.name > b.name)return 1;if (a.name < b.name)return -1;return 0;});
   }
   catch(e){console.log("no digital pins found");}
   try{
    part.id=this.readwtx(parts[i],'id');
 if(part.type=="gnd"){part.id=part.type;this.writewtx(parts[i],'id',part.id);console.log(this.readwtx(parts[i],'id')+" sfd ");}
  }
  catch(e){part.error="wtx:id not found";}    
  try{
    part.type=this.readwtx(parts[i],'type');
  }
  catch(e){
    part.error="wtx:type not found";
  }
  try{
    part.name=this.readwtx(parts[i],'name');
  }
  catch(e){part.error="wtx:name not found";}
  try{
    part.category=this.readwtx(parts[i],'category');
  }
  catch(e){part.error="wtx:category not found";}    
  try{
    part.value=this.readwtx(parts[i],'value');  

  }
  catch(e){part.error="wtx:value not found";}    
  try{
    part.spice=this.readwtx(parts[i],'spice');
  }
  catch(e){part.error="wtx:spice not found";}    
  try{        
    part.model=this.readwtx(parts[i],'model');
  }
  catch(e){part.error="wtx:model not found";} 
    //for ac voltage source
    try{        
      part.amplitude=this.readwtx(parts[i],'amplitude');


    }

    catch(e){part.error="wtx:amplitude not found";} 
    try{        
      part.phase=this.readwtx(parts[i],'phase');
    }
    catch(e){part.error="wtx:phase not found";} 

    //for sinusoidal voltage source

    try{        
      part.offsetvoltage=this.readwtx(parts[i],'offsetvoltage');
    }
    catch(e){part.error="wtx:offsetvoltage not found";} 

    try{        
      part.voltageamplitude=this.readwtx(parts[i],'voltageamplitude');
    }
    catch(e){part.error="wtx:voltageamplitude not found";} 
    try{        
      part.frequency=this.readwtx(parts[i],'frequency');
    }
    catch(e){part.error="wtx:frequency not found";} 
    try{        
      part.delaytime=this.readwtx(parts[i],'delaytime');
    }
    catch(e){part.error="wtx:delaytime not found";} 
    try{        
      part.dampingfactor=this.readwtx(parts[i],'dampingfactor');
    }
    catch(e){part.error="wtx:dampingfactor not found";} 

    // FOR pulse volatge source  
    try{        
      part.pulval1=this.readwtx(parts[i],'pulval1');
    }
    catch(e){part.error="wtx:Initial Value not found";} 

    try{        
      part.pulval2=this.readwtx(parts[i],'pulval2');
    }
    catch(e){part.error="wtx:Pulse Value not found";} 
    try{        
      part.pulval3=this.readwtx(parts[i],'pulval3');
    }
    catch(e){part.error="wtx:Delay Time not found";} 
    try{        
      part.pulval4=this.readwtx(parts[i],'pulval4');
    }
    catch(e){part.error="wtx:Rise Time not found";} 

    try{        
      part.pulval5=this.readwtx(parts[i],'pulval5');
    }
    catch(e){part.error="wtx:Fall Time not found";} 
    try{        
      part.pulval6=this.readwtx(parts[i],'pulval6');
    }
    catch(e){part.error="wtx:Pulse Period not found";} 
    try{        
      part.pulval7=this.readwtx(parts[i],'pulval7');
    }
    catch(e){part.error="wtx:Pulse Width not found";} 

    //FOR pwl volatge source
    try{        
      part.pwlval1=this.readwtx(parts[i],'pwlval1');
    }
    catch(e){part.error="wtx:Time T1 Value not found";} 

    try{        
      part.pwlval2=this.readwtx(parts[i],'pwlval2');
    }
    catch(e){part.error="wtx:Voltage V1 Value not found";} 
    try{        
      part.pwlval3=this.readwtx(parts[i],'pwlval3');
    }
    catch(e){part.error="wtx:Time T2 Value not found";} 
    try{        
      part.pwlval4=this.readwtx(parts[i],'pwlval4');
    }
    catch(e){part.error="wtx:Voltage V2 value not found";} 

    try{        
      part.pwlval5=this.readwtx(parts[i],'pwlval5');
    }
    catch(e){part.error="wtx:Time T3 value not found";} 
    try{        
      part.pwlval6=this.readwtx(parts[i],'pwlval6');
    }
    catch(e){part.error="wtx:Voltage V3 value not found";} 
    try{        
      part.pwlval7=this.readwtx(parts[i],'pwlval7');
    }
    catch(e){part.error="wtx:Time T4 Value not found";} 
    try{        
      part.pwlval8=this.readwtx(parts[i],'pwlval8');
    }
    catch(e){part.error="wtx:Voltage V4 Value not found";} 

//FOR exponential volatge source
try{        
  part.eval1=this.readwtx(parts[i],'eval1');
}
catch(e){part.error="wtx:Initial Value not found";} 

try{        
  part.eval2=this.readwtx(parts[i],'eval2');
}
catch(e){part.error="wtx:Pulse Value not found";} 
try{        
  part.eval3=this.readwtx(parts[i],'eval3');
}
catch(e){part.error="wtx:Rise Time Delay value not found";} 
try{        
  part.eval4=this.readwtx(parts[i],'eval4');
}
catch(e){part.error="wtx:Rise Time Constant value not found";} 

try{        
  part.eval5=this.readwtx(parts[i],'eval5');
}
catch(e){part.error="wtx:Fall Time Delay value not found";} 
try{        
  part.eval6=this.readwtx(parts[i],'eval6');
}
catch(e){part.error="wtx:Fall Time Constan value not found";} 

    //special tag for parts that do simulation
    try{        
      part.measure=this.readwtx(parts[i],'measure');
    }
    catch(e){}    

    list.push(part);
  }
  return list;
  
},
/*detect analog and digital mix*/
mixedsignals:function(analogwires,digitalwires){

  for(var j=1;j<analogwires.length;j++){
    var crossed=this.getconnected(digitalwires,analogwires[j]);
    if(crossed>-1){
      return true;  
    }
  }
  return false;
},

/* test if wires are connected anywhere*/
getconnected:function(wirelist,wire){
  for(var i=0;i<wirelist.length;i++){
    for(var j=0;j<wirelist[i].length;j++){
      for(var k=0;k<wire.length;k++){
       if(this.ispoint(wirelist[i][j],wire[k])){
         return i;
       }
     }
   }
 }
 return -1;
},

//returns points connected by lines
//it is recursive and should be called with NULL for wires
followwires:function(wires,pin){
  if(wires==null)wires=[];
  var points=[];
  points.push(pin);	
  var lines =webtronics.circuit.getwithselector('#webtronics_drawing > line, #information > .webtronics_namewire_connector');
  for(var i =0 ;i<lines.length;i++){
    var point1={x:lines[i].getAttribute('x1')-0,y:lines[i].getAttribute('y1')-0};
    var point2={x:lines[i].getAttribute('x2')-0,y:lines[i].getAttribute('y2')-0};
    if(wires.indexOf(lines[i])<0){		
      if(this.ispoint(point1,pin)){
       wires.push(lines[i]);
       var p=this.followwires(wires,point2);
       for(var j=0;j<p.length;j++)points.push(p[j]);				
     }
   else if(this.ispoint(point2,pin)){
     wires.push(lines[i]);
     var p=this.followwires(wires,point1);
     for(var j=0;j<p.length;j++)points.push(p[j]);				
   }
}
}
return points;
},



//sets the node numbers for parts
numberwires:function(parts){
  var analogpoints=[];
  var digitalpoints=[];
  for(var i=0;i<parts.length; i++){
    //analog node numbering loop
    if(parts[i].type=="wire")continue;

    if( parts[i].type=="gnd"){
      if (analogpoints.length==0 ){
       var wire=this.followwires(null,{x:parts[i].analogpins[0]['x'],y:parts[i].analogpins[0]['y']});
       analogpoints.push(wire);
//add this node to thelist of digital wires
digitalpoints.push(wire);
}
parts[i].analogpins[0]["node"]=0;
//      parts[i].digitalpins[0]["node"]=0;
continue;
}
if(parts[i].analogpins!=undefined){
  for(var j=0;j<parts[i].analogpins.length;j++){
   var wire=this.followwires(null,{x:parts[i].analogpins[j]['x'],y:parts[i].analogpins[j]['y']});
   var found=this.getconnected(analogpoints,wire);
   if(found<0){
     analogpoints.push(wire);
     parts[i].analogpins[j]["node"]=analogpoints.length-1;
   }
   else{
     parts[i].analogpins[j]["node"]=found;
   }
 }
}
    //digital node numbering loop
    
    if(parts[i].digitalpins!=undefined){
      for(var j=0;j<parts[i].digitalpins.length;j++){
       var wire=this.followwires(null,{x:parts[i].digitalpins[j]['x'],y:parts[i].digitalpins[j]['y']});
       var found=this.getconnected(digitalpoints,wire);
       if(found<0){
         digitalpoints.push(wire);
         parts[i].digitalpins[j]["node"]=digitalpoints.length-1;
       }
       else{
         parts[i].digitalpins[j]["node"]=found;
       }
     }	
   }
 }
  //returns true if digital and analog are mixed
  return this.mixedsignals(analogpoints,digitalpoints);
} , 



/* creates all netlist data from parts data*/
getnodes:function(parts){
  var sections={netlist:[],coupling:[],firstdir:[],simulation:[],lastdir:[]};    
  
  //if(this.numberwires(parts))return {firstdir:[],netlist:[{error:"pin is both analog and digital"}],lastdir:[],plot:[]};
  this.numberwires(parts);
  for(var i=0;i<parts.length; i++){
    //    if(parts[i].type=="wire")continue;
    // check what type of simulation to use
    if(parts[i].type=='gnd' || parts[i].type=='wire')continue;
    if(parts[i].type=="plot"){
     if(sections.simulation.length==0){
       sections.simulation.push(".op");
       sections.simulation.push(".print tran");
     }
     if(sections.simulation[1] !=undefined && sections.simulation[1].match(/\.print\sac/g)==null){
       sections.simulation[1]+=" v("+parts[i].analogpins[0]["node"]+")";
       sections.simulation[1]+=" "+parts[i].measure;
       if(parts[i].model)sections.simulation.push(parts[i].model);
     }


   }
   else{
    if(parts[i].type=="v"){
     if(sections.simulation.length==0 && parts[i].model.length){
       sections.simulation.push(".op");
       sections.simulation.push(".print ac "+parts[i].measure);
       sections.simulation.push(parts[i].model);
     }
   }
   else if(parts[i].type=="l"){
     if(parts[i].model.length){
       sections.coupling.push(parts[i].model);  
     }
   }
   else{
     if(parts[i].model.match(/\.mod/i) && !parts[i].id.match(/^x/))parts[i].id="x"+parts[i].id;
     if(parts[i].model.length)sections.firstdir.push(parts[i].model);

   }
      //create pins array
      var net={error:parts[i].error,pwlval1:parts[i].pwlval1,pwlval2:parts[i].pwlval2,pwlval3:parts[i].pwlval3,pwlval4:parts[i].pwlval4,pwlval5:parts[i].pwlval5,pwlval6:parts[i].pwlval6,pwlval7:parts[i].pwlval7,pwlval8:parts[i].pwlval8,pulval1:parts[i].pulval1,pulval2:parts[i].pulval2,pulval3:parts[i].pulval3,pulval4:parts[i].pulval4,pulval5:parts[i].pulval5,pulval6:parts[i].pulval6,pulval7:parts[i].pulval7,name:parts[i].name,
        partid:parts[i].id,pins:{analog:parts[i].analogpins,digital:parts[i].digitalpins},model:parts[i].value,amplitude:parts[i].amplitude,
      phase:parts[i].phase,offsetvoltage:parts[i].offsetvoltage,voltageamplitude:parts[i].voltageamplitude,frequency:parts[i].frequency,
      delaytime:parts[i].delaytime,dampingfactor:parts[i].dampingfactor,eval1:parts[i].eval1,eval2:parts[i].eval2,eval3:parts[i].eval3,eval4:parts[i].eval4,eval5:parts[i].eval5,eval6:parts[i].eval6};
      if(net!=null)sections.netlist.push(net);
    }
    
  }
  
  return sections;
},

/* organizes data into netlist*/
createnetlist:function(responsefunc){

  var parts=webtronics.circuit.getwithselector('#webtronics_drawing > g');

  if(parts.length<1){
    responsefunc("no parts found \n");
    return;
  }



  var partswtx=this.sortnetlist(this.getwtxdata(parts));


  if(partswtx[0].type.toLowerCase()!='gnd'){
    responsefunc('no ground node');
    return;
  }
  this.connectnamewires(partswtx);
  
  var spice="*ngspice netlist * \n";
  var sections=this.getnodes(partswtx);
  
  //dump models into spice	

  if(sections.netlist.length){
    var command="";
    for(var i=0;i<sections.netlist.length;i++){
      if(sections.netlist[i].error!=""){
       spice+=sections.netlist[i].error+'\n';
       continue;
     }
     command=sections.netlist[i].partid;
     var pins=[];
     for(var j=0;j<sections.netlist[i].pins['analog'].length;j++)pins.push(sections.netlist[i].pins['analog'][j]);
      for(var j=0;j<sections.netlist[i].pins['digital'].length;j++)pins.push(sections.netlist[i].pins['digital'][j]);
        pins.sort(function(a,b){return a.index > b.index? 1:a.index < b.index?-1:0;})
//      console.log(pins);
for(var j=0;j<pins.length;j++){command += " "+pins[j].node;}

  if(sections.netlist[i].name=="ac"){
   command+=" "+"AC "+sections.netlist[i].amplitude+" "+sections.netlist[i].phase;
 }else if(sections.netlist[i].name=="sinvoltagesource"){
   command+=" "+"SIN ("+sections.netlist[i].offsetvoltage+" "+sections.netlist[i].voltageamplitude+" "+sections.netlist[i].frequency+" "+sections.netlist[i].delaytime+" "+sections.netlist[i].dampingfactor+")";
 }else if(sections.netlist[i].name=="battery"){
  command+=" "+"DC "+sections.netlist[i].model;
}else if(sections.netlist[i].name=="current"){
 command+=" "+"dc "+sections.netlist[i].model;
}else if(sections.netlist[i].name=="pulse"){
  command+=" "+"PULSE ("+sections.netlist[i].pulval1+" "+sections.netlist[i].pulval2+" "+sections.netlist[i].pulval3+" "+sections.netlist[i].pulval4+" "+sections.netlist[i].pulval5+" "+sections.netlist[i].pulval6+" "+sections.netlist[i].pulval7+")";
}
else if(sections.netlist[i].name=="pwl"){
  command+=" "+"PWL ("+sections.netlist[i].pwlval1+" "+sections.netlist[i].pwlval2+" "+sections.netlist[i].pwlval3+" "+sections.netlist[i].pwlval4+" "+sections.netlist[i].pwlval5+" "+sections.netlist[i].pwlval6+" "+sections.netlist[i].pwlval7+" "+sections.netlist[i].pwlval8+")";
}
else if(sections.netlist[i].name=="exponential"){
  command+=" "+"EXP ("+sections.netlist[i].eval1+" "+sections.netlist[i].eval2+" "+sections.netlist[i].eval3+" "+sections.netlist[i].eval4+" "+sections.netlist[i].eval5+" "+sections.netlist[i].eval6+")";
}
else{
  command+=" "+sections.netlist[i].model;
}
if(command!="")spice+=command+'\n';
}
}

if(sections.coupling.length){
  for(var i=0;i<sections.coupling.length;i++){
    spice+=sections.coupling[i]+'\n';
  }
}


var modelloader={
  modeltext:"",
  modelcount:0,
  download:function(name){
   var found=false;
   for( var i=0;i<webtronics.partslists.length;i++){

    if(JSON.stringify(webtronics.partslists[i]).indexOf(name)!=-1){
     found=true;
					if(webtronics.partslists[i].url.indexOf("http://")==-1){//see if path is local

            openfile( webtronics.partslists[i].url+"/spice/"+ name,this.responder);
          }
          else{
            server.requestfile(list.url,this.responder);
          }
          break;
          this.modelcount++;
        }

      }
      if(!found)console.log("model not found");
    },
    finish:function(){
      spice+=modelloader.modeltext; 
      if(sections.simulation.length){
        for(var i=0;i<sections.simulation.length;i++){
         if(sections.simulation[i]!="")spice+=sections.simulation[i]+"\n";
       }
     }
     if(sections.lastdir.length){
      sections.lastdir=sections.lastdir.uniq();
      for(var i=0;i<sections.lastdir.length;i++){
        if(sections.lastdir[i]!="")spice+=sections.lastdir[i]+"\n";
      }
    }
    responsefunc(spice);
  },

  responder:function(text){
    console.log("reponded");
    modelloader.modeltext+=text;
    if(!modelloader.modelcount){
      modelloader.finish();
      spice=spice.concat(".end \n");	

    }       
  }
}


if(sections.firstdir.length){
  sections.firstdir=sections.firstdir.uniq();

  for(var i=0;i<sections.firstdir.length;i++){
//      console.log(sections.firstdir[i]);

if(sections.firstdir[i].length){



 modelloader.download(sections.firstdir[i],sections,webtronics.partslists);
}
}
}
else modelloader.finish();
var connector=webtronics.circuit.getwithselector('#information > .webtronics_namewire_connector')
for(var i=0;i<connector.length;i++)connector[i].parentNode.removeChild(connector[i]);



},





writeconnects:function(pins){

  var str=[];
  
  for(var i=0;i<pins.length;i++){
    str[i] = pins[i].x +','+pins[i].y;
  }
  return str.join(';'); 
},


getconnects:function(elem){
  var pins=[];    
  var nodes = this.getwtxtagname(elem,"node");
  for(var j=0;j<nodes.length;j++){
//	console.log(nodes[j]);
//	console.log(this.parseMatrix(elem));
var point = this.matrixxform( {x:this.getwtxattribute(nodes[j],"x"),y:this.getwtxattribute(nodes[j],"y")},this.parseMatrix(elem));
pins.push({x:point.x,y:point.y}) ;
}
      //sort nodes int correct order
      return pins;
    },

    isconnect:function(pin,radius,x,y){
      return (Math.abs(pin.x-x)<3)&&(Math.abs(pin.y-y)<3); 
    },

    isconnects:function(parts,radius,x,y){

      for(var i=0; i<parts.length; i++){
        if(parts[i].tagName =='g'){
         var pins=this.getconnects(parts[i]);
         if(pins){
           for(var j=0;j<pins.length;j++){
             if(this.isconnect(pins[j],radius,x,y)){
               return pins[j];
             }
           }
         }
       }
     }
     return null;
   },

//get the number by part id and leg
getnodenumber:function(name, leg){
  //get part by id
  var part=webtronics.circuit.getwithselector("#webtronics_drawing wtx:id "+name )[0];
  var nodes=part.getwtxtagname("node");
  for(var i=0;i<nodes.length;i++){
    if(nodes[i].getAttribute("index")==leg){
      var wire = this.followwires(null,{ x:this.getwtxattribute(node,"x"),y:this.getwtxattribute(node,"y")});
      return this.getconnected(analogwires,wire);
    }
  }
  return -1
},
getwtxtagname:function(elem,tagname){


  var tag=elem.getElementsByTagName("wtx:"+tagname);
  if(!tag.length){
    tag=elem.getElementsByTagName(tagname);
  }
  if(!tag.length){
    tag=elem.getElementsByTagNameNS(this.wtxNs,tagname);
  }
  if(!tag.length){
    tag=elem.getElementsByTagNameNS("*",tagname);
  }
  return tag;
  
},

getwtxattribute:function(elem,attrib){
  var value=elem.getAttribute(attrib);
  if(value==undefined)value=elem.getAttributeNS(this.wtxNs,attrib);
  if(value==undefined)value=elem.getAttributeNS("*",attrib);
  
  return value;
},

readwtx:function(elem,value){
  var tag=this.getwtxtagname(elem,value);
  if(tag[0])return tag[0].textContent;
  else return "";
},

writewtx:function(elem,value,text){
  var tag=this.getwtxtagname(elem,value);
  if(tag[0])tag[0].textContent=text;
},



}