brlcad2gcode.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/bin/bash
  2. #
  3. # Convenient BRL-CAD to G-Code (RS274NGC) converter script
  4. #
  5. # Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
  6. # Licensed under the GNU/GPL version 2 or later.
  7. #
  8. # Below you can define alternative paths to the conversion tools
  9. # Default is to use the tool found in $PATH
  10. # BRL-CAD G-to-VRML conversion tool
  11. G2VRML="g-vrml"
  12. # VRML to PGM conversion tool
  13. MESH2HMAP="mesh2hmap"
  14. # PGM to PNG conversion tool
  15. PGM2PNG="pnmtopng"
  16. # Image to GCODE conversion tool
  17. IMAGE2GCODE="image-to-gcode"
  18. # GNU bc calculator
  19. BC="bc"
  20. tmp_prefix="/tmp/brlcad2gcode-$RANDOM"
  21. log_file="$tmp_prefix.log"
  22. vrml_file="$tmp_prefix.vrml"
  23. pgm_file="$tmp_prefix.pgm"
  24. png_file="$tmp_prefix.png"
  25. function cleanup
  26. {
  27. rm -f ${log_file}
  28. rm -f ${vrml_file}
  29. rm -f ${pgm_file}
  30. rm -f ${png_file}
  31. }
  32. # cleanup_and_exit(exit_code)
  33. function cleanup_and_exit
  34. {
  35. cleanup
  36. exit $1
  37. }
  38. function sigterm
  39. {
  40. echo "Terminating."
  41. cleanup_and_exit 1
  42. }
  43. trap sigterm TERM INT
  44. cleanup
  45. # check_tool(tool, args, expected_return_code, install_hint, debian_package)
  46. function check_tool
  47. {
  48. local tool="$1"
  49. local args="$2"
  50. local expected_return="$3"
  51. local install_hint="$4"
  52. local debian_package="$5"
  53. $tool $args > ${log_file} 2>&1
  54. res=$?
  55. if [ $res -ne $expected_return ]; then
  56. cat ${log_file}
  57. echo
  58. echo "ERROR: Failed to probe $tool"
  59. echo "Please install $install_hint"
  60. if [ -n "$debian_package" ]; then
  61. echo
  62. echo "If you are running Debian Linux, please run the"
  63. echo "following command (as root) to install the package:"
  64. echo " apt-get install $debian_package"
  65. fi
  66. cleanup_and_exit 1
  67. fi
  68. }
  69. check_tool $G2VRML --help 1 "'BRL-CAD' (http://brlcad.org/)" ""
  70. check_tool $MESH2HMAP --help 0 "'mesh2hmap' (http://mesh2hmap.sf.net/)" ""
  71. check_tool $PGM2PNG --help 1 "'netpbm' (http://netpbm.sf.net/)" "netpbm"
  72. check_tool $IMAGE2GCODE /dev/zero 1 "'EMC2' (http://linuxcnc.org/)" ""
  73. check_tool $BC --help 0 "'GNU bc' (http://www.gnu.org/software/bc/)" "bc"
  74. function usage
  75. {
  76. echo "BRL-CAD to GCODE (RS274NGC) converter"
  77. echo
  78. echo "Usage: $0 [options] brlcad_db.g brlcad-db-object gcode.ngc"
  79. echo
  80. echo "Valid options are:"
  81. echo " --proj PLANE The projection axis plane in the 3D data used"
  82. echo " for the height in the heightmap."
  83. echo " PLANE may be either of:"
  84. echo " +z -z +y -y +x -x"
  85. echo " +z is default"
  86. echo " --imperial If set, use imperial units (inch) instead of metric"
  87. echo " units (mm)."
  88. echo " --meshres RES Resolution (in units; see --imperial) for the mesh."
  89. echo " You also need to specify this value in the"
  90. echo " image-to-gcode dialog 'Pixel size' field!"
  91. }
  92. METRIC=1
  93. projection="+z"
  94. meshres=
  95. brlcad_g_file=
  96. brlcad_g_object=
  97. ngc_file=
  98. # Parse commandline args
  99. while [ $# -gt 0 ]; do
  100. case "$1" in
  101. --proj)
  102. shift
  103. if [ $# -lt 1 ]; then
  104. echo "Missing argument to --proj"
  105. cleanup_and_exit 1
  106. fi
  107. projection="$1"
  108. case "$projection" in
  109. "+z" | "-z" | "+y" | "-y" | "+x" | "-x" )
  110. ;;
  111. *)
  112. echo "Invalid --proj plane. Must be one of +z -z +y -y +x -x"
  113. cleanup_and_exit 1
  114. ;;
  115. esac
  116. ;;
  117. --imperial)
  118. METRIC=0
  119. ;;
  120. --meshres)
  121. shift
  122. if [ $# -lt 1 ]; then
  123. echo "Missing argument to --meshres"
  124. cleanup_and_exit 1
  125. fi
  126. meshres="$1"
  127. ;;
  128. *)
  129. if [ $# -ne 3 ]; then
  130. usage
  131. cleanup_and_exit 1
  132. fi
  133. brlcad_g_file="$1"
  134. brlcad_g_object="$2"
  135. ngc_file="$3"
  136. break
  137. ;;
  138. esac
  139. shift
  140. done
  141. if [ -z "$brlcad_g_file" -o -z "$brlcad_g_object" -o -z "$ngc_file" ]; then
  142. usage
  143. cleanup_and_exit 1
  144. fi
  145. if ! [ -r "${brlcad_g_file}" ]; then
  146. echo "ERROR: Could not read input .g file ${brlcad_g_file}"
  147. cleanup_and_exit 1
  148. fi
  149. if [ $METRIC -eq 0 ]; then
  150. UNITS="inch"
  151. else
  152. UNITS="mm"
  153. fi
  154. if [ -z "$meshres" ]; then
  155. if [ $METRIC -eq 0 ]; then
  156. meshres="0.004"
  157. else
  158. meshres="0.100"
  159. fi
  160. fi
  161. # Convert the BRL-CAD database to VRML
  162. echo "Converting BRL-CAD database object to VRML..."
  163. $G2VRML ${brlcad_g_file} ${brlcad_g_object} > ${vrml_file}
  164. res=$?
  165. if [ $res -ne 0 ]; then
  166. echo "BRL-CAD to VRML conversion failed: $res"
  167. cleanup_and_exit 1
  168. fi
  169. # Convert the VRML file to a PGM image
  170. r="$meshres"
  171. if [ $METRIC -eq 0 ]; then
  172. r=$(echo "scale=6; $r * 25.4" | bc)
  173. fi
  174. echo "Converting VRML to PGM image (Mesh resolution = $r mm)..."
  175. $MESH2HMAP "--normal=$projection" "--m-pix=$r" ${vrml_file} ${pgm_file} > ${log_file} 2>&1
  176. res=$?
  177. if [ $res -ne 0 ]; then
  178. cat ${log_file}
  179. echo "VRML to PGM conversion failed: $res"
  180. cleanup_and_exit 1
  181. fi
  182. # Convert the PGM image to a PNG image
  183. echo "Converting PGM image to PNG image..."
  184. $PGM2PNG ${pgm_file} > ${png_file}
  185. res=$?
  186. if [ $res -ne 0 ]; then
  187. echo "PGM to PNG conversion failed: $res"
  188. cleanup_and_exit 1
  189. fi
  190. # Convert PNG image to GCODE
  191. echo "Converting PNG image to GCODE..."
  192. echo; echo
  193. echo "--- IMPORTANT ---"
  194. echo "Please use $meshres $UNITS as 'Pixel size' inside of the following dialog box."
  195. echo "--- IMPORTANT ---"
  196. echo; echo
  197. $IMAGE2GCODE ${png_file} > ${ngc_file}
  198. res=$?
  199. if [ $res -ne 0 ]; then
  200. echo "PNG to GCODE conversion failed: $res"
  201. rm -f ${ngc_file}
  202. cleanup_and_exit 1
  203. fi
  204. # Done! :)
  205. cleanup_and_exit 0