{"id":2999,"date":"2022-03-29T15:09:14","date_gmt":"2022-03-29T15:09:14","guid":{"rendered":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/?p=2999"},"modified":"2023-09-28T12:25:53","modified_gmt":"2023-09-28T12:25:53","slug":"code-to-save-a-gcode-file","status":"publish","type":"post","link":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/2022\/03\/29\/code-to-save-a-gcode-file\/","title":{"rendered":"File handling in Python &#038; Grasshopper"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Import .csv file<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import rhinoscriptsyntax as rs\n\nfilter = \"CSV file (*.csv)|*.csv|*.txt|All Files (*.*)|*.*||\"\nfile = rs.OpenFileName(\"Open Point File\", filter)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Reload Python Library in Grasshopper<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This will allow you to edit your Python library while you work in Grasshopper and Rhino. You do not have to quit and restart Rhino. Example code below for the exturder_turtle library. The same code will work for any external Python file you want to use in your GH code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># code required so that library reloads appropriately\n# must be above other import statements\nimport sys\nto_delete = list()\nfor module in sys.modules:\n    if \"extruder_turtle\" in module:\n      to_delete.append(module)  \n        \nfor module in to_delete: #IMPORTANT, delete old version of library code\n    sys.modules.pop(module)\n    \nfrom extruder_turtle import * #import the library\n\nreload(extruder_turtle) #IMPORTANT, reload the libary<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create .gcode file<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import rhinoscriptsyntax as rs\n\nfilter = \"GCode (*.gcode)|*.gcode|All Files (*.*)|*.*||\"\nfile = rs.SaveFileName(\"\", filter)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">More information<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These functions are part of the user interface module in rhinoscript. For more information about them and related functionality see: <a href=\"https:\/\/developer.rhino3d.com\/api\/rhinoscript\/user_interface_methods\/user_interface_methods.htm \">https:\/\/developer.rhino3d.com\/api\/rhinoscript\/user_interface_methods\/user_interface_methods.htm <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Import .csv file Reload Python Library in Grasshopper This will allow you to edit your Python library while you work in Grasshopper and Rhino. You do not have to quit and restart Rhino. Example code below for the exturder_turtle library. The same code will work for any external Python file you want to use in your GH code. Create .gcode [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-2999","post","type-post","status-publish","format-standard","hentry","category-references-and-tutorials"],"_links":{"self":[{"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/posts\/2999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/comments?post=2999"}],"version-history":[{"count":8,"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/posts\/2999\/revisions"}],"predecessor-version":[{"id":6472,"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/posts\/2999\/revisions\/6472"}],"wp:attachment":[{"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/media?parent=2999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/categories?post=2999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/handandmachine.org\/classes\/computational_fabrication\/wp-json\/wp\/v2\/tags?post=2999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}