index.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <html>
  2. <head>
  3. <style>
  4. </style>
  5. </head>
  6. <body>
  7. <?php
  8. $file = "C:\\Users\\Idleman\\Desktop\\test.fzz";
  9. $zip = new ZipArchive;
  10. $res = $zip->open($file);
  11. $properties = array();
  12. if ($res !== TRUE) throw new Exception('Impossible d\'ouvrir le ZIP, code:' . $res);
  13. $xmlString = $zip->getFromName(str_replace('.fzz','',basename($file)).'.fz');
  14. $zip->close();
  15. $xml = new SimpleXMLElement($xmlString);
  16. $parts = array();
  17. $minY = 0;
  18. foreach($xml->instances->instance as $instance){
  19. $part = array();
  20. $part['sigle'] = (string) $instance->title;
  21. $attributes = $instance->attributes();
  22. $part['id'] = (string) $attributes->moduleIdRef;
  23. $part['properties'] = array();
  24. foreach($instance->property as $property){
  25. $attribute = $property->attributes();
  26. $part['properties'][(string)$attribute['name']] = (string)$attribute['value'];
  27. }
  28. $part['type'] = 'component';
  29. if(isset($instance->views->breadboardView->geometry)){
  30. $geometry = $instance->views->breadboardView->geometry->attributes();
  31. $part['geometry'] = array(
  32. 'x' => array(),
  33. 'z' => 0,
  34. 'y' => array()
  35. );
  36. if(isset($geometry['wireFlags']))
  37. $part['type'] = 'wire';
  38. foreach ($geometry as $key => $value) {
  39. if(substr($key, 0,1) == 'x') $part['geometry']['x'][] =(float)$value;
  40. if(substr($key, 0,1) == 'y') $part['geometry']['y'][] =(float)$value;
  41. }
  42. $part['geometry']['z'] =(float)$geometry['z'];
  43. if($minY>$part['geometry']['y'][0])$minY = $part['geometry']['y'][0];
  44. if( $part['type']== 'wire'){
  45. $wireAttributes = $instance->views->breadboardView->wireExtras->attributes();
  46. $part['color'] = (string)$wireAttributes['color'];
  47. //var_dump($part['geometry']);
  48. }
  49. }
  50. $path = explode('/fritzing-parts/', $attributes->path);
  51. if(count($path)>1){
  52. $path = $path[1];
  53. //echo $path.'<br>';
  54. $path = 'fritzing-parts/'.$path;
  55. $part['fzp'] = $path;
  56. $xmlPart = new SimpleXMLElement(file_get_contents($path));
  57. $part['component'] = array(
  58. 'title' => (string) $xmlPart->title,
  59. 'description' => (string)$xmlPart->description,
  60. );
  61. $part['component']['name'] = (string)$xmlPart->title;
  62. $part['component']['description'] = (string)$xmlPart->description;
  63. $partAttributes = $xmlPart->views->iconView->layers->attributes();
  64. $icon = (string)$partAttributes['image'];
  65. $part['component']['icon'] = 'fritzing-parts/svg/core/'.$icon;
  66. $partAttributes = $xmlPart->views->breadboardView->layers->attributes();
  67. $icon = (string)$partAttributes['image'];
  68. $part['component']['breadboard'] = 'fritzing-parts/svg/core/'.$icon;
  69. $partAttributes = $xmlPart->views->schematicView->layers->attributes();
  70. $icon = (string)$partAttributes['image'];
  71. $part['component']['schema'] = 'fritzing-parts/svg/core/'.$icon;
  72. }
  73. $parts[] = $part;
  74. }
  75. $minY = abs($minY);
  76. ?>
  77. <svg id="vis"></svg>
  78. <script src="https://d3js.org/d3.v4.min.js"></script>
  79. <script>
  80. var vis = d3.select("#vis")
  81. .attr("width", 500)
  82. .attr("x", 500)
  83. .attr("y", 500)
  84. .attr("height", 500);
  85. <?php foreach ($parts as $part) {
  86. if( $part['type']!='wire' ) continue;
  87. echo 'var line = vis.append("polyline")'."\n\t\t".'.attr("stroke-linejoin","round").style("stroke", \''.$part['color'].'\').attr("stroke-width", 5)'."\n\t\t";
  88. $x = 0;
  89. $y = 0;
  90. echo '.attr("points","';
  91. for ($i=0;$i<count($part['geometry']['x']);$i++) {
  92. $x += $part['geometry']['x'][$i];
  93. $y += $part['geometry']['y'][$i];
  94. if($i==0) $y+= $minY;
  95. echo "$x,$y,";
  96. }
  97. echo '");';
  98. } ?>
  99. <?php foreach ($parts as $part) {
  100. if(!isset($part['component']) || $part['type']!='component' ) continue;
  101. ?>
  102. d3.xml('<?php echo $part['component']['breadboard']?>').mimeType("image/svg+xml").get(function(error, xml) {
  103. if (error) throw error;
  104. xml.documentElement.id = '<?php echo $part['sigle']?>';
  105. document.getElementById('vis').appendChild(xml.documentElement);
  106. d3.select("#<?php echo $part['sigle'];?>").attr("y", <?php echo $part['geometry']['y'][0]+$minY ?>)
  107. .attr("x", <?php echo $part['geometry']['x'][0] ?>);
  108. });
  109. <?php } ?>
  110. </script>
  111. </body>
  112. </html>