stepanal.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /* tool to analyze and recursively dump STEP files. Also reading STEP files is a real pain in the ass. */
  3. function step_dump($id, $l = 0, $p = "", $pi = -1)
  4. {
  5. global $entities;
  6. global $children;
  7. global $parents;
  8. global $needed;
  9. global $cmd;
  10. global $maxlevel;
  11. if($l >= 254) {echo("php max level reached\n"); return; }
  12. if($l >= $maxlevel) return;
  13. if($cmd == 1) {
  14. $n = trim(explode("(", $e[0]=="("?substr($e,1):$e)[0]);
  15. if($p && $p != $n) $parents[$p."\t".$n] = 1;
  16. $p = $n;
  17. } else if($cmd == 2) {
  18. if($pi != -1) $needed[$id] = $needed[$pi];
  19. } else echo(str_repeat("| ",$l)."#".$id." ".$entities[$id]."\n");
  20. foreach($children[$id] as $d)
  21. step_dump($d, $l+1, $p, $id);
  22. }
  23. function step_needed($id)
  24. {
  25. global $entities;
  26. global $children;
  27. global $needed;
  28. $needed[$id] = 1;
  29. foreach($children as $i=>$e)
  30. if(in_array($id, $e)) {
  31. step_needed($i);
  32. foreach($e as $d)
  33. if(substr($entities[$d],0,5)=="PLANE" || substr($entities[$d],0,19)=="CYLINDRICAL_SURFACE" ||
  34. substr($entities[$d],0,17)=="SPHERICAL_SURFACE" || substr($entities[$d],0,15)=="CONICAL_SURFACE" ||
  35. substr($entities[$d],0,4)=="AXIS" || substr($entities[$d],0,10)=="(GEOMETRIC") {
  36. $needed[$d] = 1;
  37. step_dump($d, 0, "");
  38. }
  39. }
  40. }
  41. if(!isset($_SERVER['argv'][2])) die(
  42. "php stepanal.php <step file> <entity id | entity type> [maxlevel]\n".
  43. "php stepanal.php -p <step file1> [step file2] [step file3...]\n".
  44. "php stepanal.php -e <step file> <entity id>\n");
  45. if($_SERVER['argv'][1] == "-p") {
  46. $files = array_slice($_SERVER['argv'], 2);
  47. $root = "CLOSED_SHELL";
  48. $cmd = 1;
  49. $maxlevel = 128;
  50. } else if($_SERVER['argv'][1] == "-e") {
  51. $files[] = $_SERVER['argv'][2];
  52. $root = $_SERVER['argv'][3];
  53. $cmd = 2;
  54. $maxlevel = 128;
  55. } else {
  56. $files[] = $_SERVER['argv'][1];
  57. $root = $_SERVER['argv'][2];
  58. $cmd = 0;
  59. $maxlevel = @intval($_SERVER['argv'][3]) ? intval($_SERVER['argv'][3]) : 128;
  60. }
  61. $parents = [];
  62. foreach($files as $fn) {
  63. $entities = []; $children = []; $needed = [];
  64. $f=fopen($fn, "r");
  65. if($f) {
  66. $line = "";
  67. while(!feof($f) && trim($line) != "DATA;") {
  68. $line = fgets($f);
  69. if($cmd == 2) echo($line);
  70. }
  71. $data = explode(";",preg_replace("|[\ \t]+#|", "#", preg_replace("|\([\ \t]+|", "(", preg_replace("|/\*.*?\*/|U","",
  72. str_replace("\r","", str_replace("\n","", fread($f, filesize($fn))))))));
  73. fclose($f);
  74. }
  75. foreach($data as $d)
  76. if(@trim($d)[0] == "#") {
  77. $l = explode("=", $d);
  78. $e = @trim(substr($d, strlen($l[0])+1));
  79. $entities[intval(substr(trim($l[0]),1))] = $e;
  80. $needed[intval(substr(trim($l[0]),1))] = 1;
  81. $a = explode(",", str_replace("(", ",", str_replace(")", ",", $e)));
  82. $children[intval(substr(trim($l[0]),1))] = [];
  83. foreach($a as $d)
  84. if(@trim($d)[0] == "#")
  85. $children[intval(substr(trim($l[0]),1))][] = intval(substr(trim($d),1));
  86. }
  87. if($cmd == 2) {
  88. foreach(["ADVANCED_BREP_SHAPE_REPRESENTATION", "MANIFOLD_SURFACE_SHAPE_REPRESENTATION",
  89. "FACETED_BREP_SHAPE_REPRESENTATION"] as $r)
  90. foreach($entities as $i=>$e)
  91. if(substr($e, 0, strlen($r)) == $r) {
  92. $needed[$i] = 0;
  93. step_dump($i, 0, "");
  94. }
  95. }
  96. if(intval($root)."" == $root) {
  97. $needed[intval($root)] = 1;
  98. step_dump(intval($root), 0, "");
  99. if($cmd == 2) step_needed(intval($root));
  100. } else
  101. foreach($entities as $i=>$e)
  102. if(substr($e, 0, strlen($root)) == strtoupper($root)) {
  103. $needed[$i] = 1;
  104. step_dump($i, 0, "");
  105. if($cmd == 2) step_needed($i);
  106. }
  107. }
  108. if($cmd == 1) {
  109. ksort($parents);
  110. foreach($parents as $k=>$v) {
  111. $l = explode("\t",$k);
  112. echo(sprintf("%32s %s",$l[0],$l[1])."\n");
  113. }
  114. }
  115. if($cmd == 2) {
  116. foreach($entities as $i=>$e)
  117. if($needed[$i]) {
  118. foreach($children[$i] as $d)
  119. if($d != $i && !$needed[$d]) $e = str_replace("(,", "(", str_replace(",)", ")", preg_replace("|[,]+|", ",",
  120. preg_replace("|#".$d."([^0-9])|", "$1", $e))));
  121. echo("#".$i."=".$e.";\n");
  122. }
  123. echo("ENDSEC;\nEND_ISO-10303-21;\n");
  124. }