首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的python程序来读取。gcode文件

简单的python程序来读取。gcode文件
EN

Stack Overflow用户
提问于 2016-01-06 16:51:07
回答 1查看 6.1K关注 0票数 3

我对Python完全陌生,我想创建一个简单的脚本,它将在文件中找到一个特定的值,而不是使用它执行一些计算。

所以我有一个.gcode文件(它是一个3D打印机有数千行的文件,但是它可以被任何简单的文本编辑器打开)。我想要创建一个简单的Python程序,当您启动它时,会打开simple,其中的按钮要求选择一个.gcode文件。然后在文件打开后,我想让程序找到特定的行。

;所用长丝= 22900.5mm (55.1cm3)

(上面是文件中行的确切格式)并从中提取值55.1。稍后,我想用这个值做一些简单的计算。到目前为止,我已经制作了一个简单的GUI和一个带有打开文件选项的按钮,但是我仍然无法从这个文件中获得这个值作为一个数字(以便以后可以在等式中使用)。

我希望我已经足够清楚地解释了我的问题,以便有人可以帮助我:)谢谢您的帮助!

到目前为止我的代码是:

代码语言:javascript
复制
from tkinter import *
import re




# Here, we are creating our class, Window, and inheriting from the Frame
# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__)
class Window(Frame):

    # Define settings upon initialization. Here you can specify
def __init__(self, master=None):

        # parameters that you want to send through the Frame class. 
Frame.__init__(self, master)   

        #reference to the master widget, which is the tk window                 
self.master = master

        #with that, we want to then run init_window, which doesn't yet exist
self.init_window()

    #Creation of init_window
def init_window(self):

        # changing the title of our master widget      
self.master.title("Used Filament Data")

        # allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)

        # creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)

        # create the file object)
file = Menu(menu)

        # adds a command to the menu option, calling it exit, and the
        # command it runs on event is client_exit
file.add_command(label="Exit", command=self.client_exit)

        #added "file" to our menu
menu.add_cascade(label="File", menu=file)

        #Creating the button
quitButton = Button(self, text="Load GCODE",command=self.read_gcode)
quitButton.place(x=0, y=0)

def get_filament_value(self, filename):
with open(filename, 'r') as f_gcode:
data = f_gcode.read()
re_value = re.search('filament used = .*? \(([0-9.]+)', data)

if re_value:
value = float(re_value.group(1))
else:
print 'filament not found in {}'.format(root.fileName)
value = 0.0
return value

print get_filament_value('test.gcode') 

def read_gcode(self):
root.fileName = filedialog.askopenfilename( filetypes = ( ("GCODE files", "*.gcode"),("All files", "*.*") ) )
self.value = self.get_filament_value(root.fileName)

def client_exit(self):
exit()





# root window created. Here, that would be the only window, but
# you can later have windows within windows.
root = Tk()

root.geometry("400x300")

#creation of an instance
app = Window(root)

#mainloop 
root.mainloop()  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-06 17:42:07

您可以使用正则表达式在gcode文件中找到匹配的行。下面的函数加载整个gcode文件并进行搜索。如果找到该值,则该值将作为浮点数返回。

代码语言:javascript
复制
import re

def get_filament_value(filename):
    with open(filename, 'r') as f_gcode:
        data = f_gcode.read()
        re_value = re.search('filament used = .*? \(([0-9.]+)', data)

        if re_value:
            value = float(re_value.group(1))
        else:
            print('filament not found in {}'.format(filename))
            value = 0.0
        return value

print(get_filament_value('test.gcode'))

您的文件应该显示:

代码语言:javascript
复制
55.1

所以您的原始代码应该如下所示:

代码语言:javascript
复制
from tkinter import *
import re

# Here, we are creating our class, Window, and inheriting from the Frame
# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__)
class Window(Frame):

    # Define settings upon initialization. Here you can specify
    def __init__(self, master=None):

        # parameters that you want to send through the Frame class. 
        Frame.__init__(self, master)   

        #reference to the master widget, which is the tk window                 
        self.master = master

        #with that, we want to then run init_window, which doesn't yet exist
        self.init_window()

    #Creation of init_window
    def init_window(self):

        # changing the title of our master widget      
        self.master.title("Used Filament Data")

        # allowing the widget to take the full space of the root window
        self.pack(fill=BOTH, expand=1)

        # creating a menu instance
        menu = Menu(self.master)
        self.master.config(menu=menu)

        # create the file object)
        file = Menu(menu)

        # adds a command to the menu option, calling it exit, and the
        # command it runs on event is client_exit
        file.add_command(label="Exit", command=self.client_exit)

        #added "file" to our menu
        menu.add_cascade(label="File", menu=file)

        #Creating the button
        quitButton = Button(self, text="Load GCODE",command=self.read_gcode)
        quitButton.place(x=0, y=0)

    # Load the gcode file in and extract the filament value
    def get_filament_value(self, fileName):
        with open(fileName, 'r') as f_gcode:
            data = f_gcode.read()
            re_value = re.search('filament used = .*? \(([0-9.]+)', data)

            if re_value:
                value = float(re_value.group(1))
                print('filament value is {}'.format(value))
            else:
                value = 0.0
                print('filament not found in {}'.format(fileName))
        return value

    def read_gcode(self):
        root.fileName = filedialog.askopenfilename(filetypes = (("GCODE files", "*.gcode"), ("All files", "*.*")))
        self.value = self.get_filament_value(root.fileName)

    def client_exit(self):
        exit()

# root window created. Here, that would be the only window, but
# you can later have windows within windows.
root = Tk()

root.geometry("400x300")

#creation of an instance
app = Window(root)

#mainloop 
root.mainloop() 

这将将结果作为浮动保存到一个名为value的类变量中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34638372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档