Pyqt5-based G code hierarchical analysis and model visualization system
By designing a G code hierarchical analysis and model visualization system based on pyqt5, the problem of product quality difficulty in ensuring G code generation in CNC machining is solved, effective simulation and optimization of G code trajectory is realized, and the quality of processing products and precise positioning capabilities of trajectory optimization are improved.
Patent Information
- Application Number
- CN202311297461.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-09
- Publication Date
- 2025-04-11
AI Technical Summary
In CNC machining, since the generation of G codes depends on software systems or manual labor, it is difficult to guarantee the quality of the processed product and it is difficult to optimize the trajectory.
A G code hierarchical analysis and model visualization system based on pyqt5 was designed, including G code analysis layer and G code visualization layer. Through layered analysis and visualization of three different modes (point cloud mode, line segment mode and plane mode), the simulation and optimization of the G code trajectory is achieved.
Effective simulation and optimization of G code trajectory is realized, the G code file information can be layered and integrated and classified in a short time, and the three-dimensional visualization allows researchers to intuitively observe the generation of coordinate trajectories after G code import and the error point, which facilitates subsequent precise positioning and optimization.
Smart Images

Figure CN119806503A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of numerical control machining trajectory simulation, and in particular to the design of a G code hierarchical analysis and model visualization system based on pyqt5 technology. Background Art
[0002] In the field of CNC machining centers, G-code is the most widely used computer numerical control (CNC) programming language, mainly used for computer-aided manufacturing to control automated machine tools, and there are many variant versions. G-code is an instruction in a CNC program. Using G-code can achieve rapid positioning, reverse circular interpolation, radius programming and other actions. CNC machine tools usually use G-code to describe the processing information of the machine tool, such as tool path, coordinate selection, coolant opening, etc. G-code functions such as G00 (or G0) for rapid positioning, G01 (or G1) for linear interpolation, G02 (or G2) for clockwise circular interpolation, etc., and G-code coordinates are represented by traditional X, Y, and Z three-dimensional coordinate axes, such as G00 X01 Y02 Z12 for rapid movement to the position with coordinate point (01,02,12). G-code is generally generated by software such as CAD / CAM, or typed by CNC field staff, and then entered into the CNC system to guide CNC machine tools to process products. However, the G code generated by software is overly dependent on the reliability of the software system, and the G code generated manually is even more difficult to accurately express the processed products. This also gives rise to the problem of optimizing CNC machining trajectories controlled by G code. Summary of the invention
[0003] In order to help solve the problem that the quality of processed products is difficult to be effectively guaranteed in CNC machining, realize the trajectory simulation before G code pre-processing, and facilitate the subsequent simulation of the effects before and after trajectory optimization, the present invention proposes the design of a G code hierarchical analysis and model visualization system based on pyqt5.
[0004] In order to achieve the above object, the technical solution adopted by the present invention is: a G code hierarchical analysis and model visualization system based on pyqt5, comprising:
[0005] The G code parsing layer is used to segment each type of instruction according to the Gcode code, and then extract the coordinate points according to the set keywords and store them in an array for calling;
[0006] The G-code visualization layer is used to obtain the array and simulate the running trajectory of the G-code in the CNC machine tool to observe the state characteristics of the G-code instructions in real time.
[0007] The G-code visualization layer includes:
[0008] The analysis result display window module is used to display the results of G code analysis, including the number of code layers, track points and three-dimensional coordinate points;
[0009] The trajectory information display window module is used to display the trajectory points in the G code and observe the dynamic operation process of the G code trajectory generation in real time in multiple dimensions;
[0010] Auxiliary module, including trajectory drawing progress bar and save file button, used to display the trajectory generation progress of G code and save the G code parsing file.
[0011] The trajectory information display window module includes:
[0012] Track point cloud mode, used to display the coordinate point track of G code in point cloud mode;
[0013] The trajectory line segment mode is used to connect the trajectory point cloud of the point cloud mode into a line segment according to the G code instruction sequence, that is, to determine whether the current point and the next point form a line segment based on the Z-axis value: if the difference between the Z-axis values of the current point and the next point is less than the threshold, the current point and the next point are connected into a line segment; otherwise, no line is connected;
[0014] The track plane mode is used to connect each line segment of the line segment mode into a layer according to the Z-axis value. That is, if the difference between the average Z-axis value of all points of the current line segment and the average Z-axis value of all points of the next line segment is less than the threshold, the current line segment and the next line segment are connected into a layer; otherwise, they are not connected into a layer.
[0015] A G code layered parsing and model visualization method based on pyqt5, comprising the following steps:
[0016] Step 1: Get the G-code file;
[0017] Step 2: Use the string matching method to perform instruction segmentation on the read G code and store it layer by layer into the operation array;
[0018] Step 3: Perform corresponding drawing operations and G-code information display according to the selected mode, count the drawing process and display the counting results.
[0019] In step 1:
[0020] Only when the selected G-code file format is correct, the G-code file is parsed and the character strings outside the matching text range are ignored.
[0021] The step 2 comprises:
[0022] Step 2.1: Divide the G code according to the matching keyword text set by the G code characteristics, read the code layer by layer through the pointer, and match the first character string in each layer of the code with the keyword text; if the first character string matches the matching keyword text successfully, execute step 2.2 for the layer of code, otherwise automatically skip the line of code, and the pointer points to the first letter of the next line of code, and execute the step of matching the first character string with the keyword text;
[0023] Step 2.2: After the first string of the current line of code is matched successfully, the pointer continues to match the string forward. When encountering a string whose first letter is X, Y or Z and whose subsequent letters are in the form of numbers, the number is copied into the coordinate point array; if the X, Y or Z coordinate is missing after the matching of the line of code is completed, 0 is automatically added to the corresponding position in the first array used to store the coordinate points;
[0024] Step 2.3: Allocate two two-dimensional arrays in the memory at the same time. The second array is used to store the coordinate points and connection relationships of each layer, and the third array is used to store the relationship between the coordinate points and the relationship between layers.
[0025] The step 3 comprises:
[0026] Convert the first array, the second array and the third array generated in step 2 into the required data format for subsequent calls;
[0027] The build mode is as follows:
[0028] Track point cloud mode displays each 3D coordinate point and the parsed information of the code instruction in the window;
[0029] Track segment mode, create a multi-threaded form, put the specified drawing operation in the sub-thread, and the rest of the operations in the main thread; after extracting the second array, draw each coordinate point one by one, and display the information of each coordinate point;
[0030] In the trajectory plane mode, a multi-threaded form is created, and after extracting the third array, each coordinate point is drawn one by one, and the information of each coordinate point is displayed.
[0031] A G code hierarchical parsing and model visualization method based on pyqt5 also includes step 4, which is as follows:
[0032] The G code analysis data displayed in different modes obtained through steps 2 and 3 are saved in the local computer, and the G code analysis data is saved by linking to the local computer disk to select a location.
[0033] A pyqt5-based G code hierarchical parsing and model visualization device comprises a memory and a processor; the memory is used to store a computer program; the processor is used to implement a pyqt5-based G code hierarchical parsing and model visualization method when executing the computer program.
[0034] A computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the pyqt5-based G code layered analysis and model visualization method is implemented.
[0035] The present invention has the following beneficial effects and advantages:
[0036] 1. The G code is analyzed in layers, which facilitates the integration and classification of the information contained in the G code file. Although there are hundreds or thousands of lines of code in the G code file, and theoretically there are at most 100 types of G code instructions in the code, the keyword matching technology is used to perform specific keyword matching operations based on the characteristics of the G code instructions, and the computer's relatively fast running speed, it is finally possible to analyze the code file information in layers, integrate and classify it, and display it intuitively in a short time.
[0037] 2. Use three different simulation forms to visualize the G-code instruction information in three dimensions. During CNC machining, since the G-code is generated manually or automatically by the software system and then directly enters the CNC machine tool to guide the machining trajectory of the machine tool tool, it is difficult to see whether the generated G-code can accurately express the physical and mathematical properties of the machined product. As a result, the quality of the processed products is difficult to be effectively guaranteed, and it is also difficult to accurately locate and accurately compare the trajectory optimization in the field of CNC machining. This system uses three different simulation forms to visualize the generated machining trajectory of G-code instructions, so that relevant researchers can more intuitively observe the generation of the coordinate trajectory and the error points after the G-code is imported, which is convenient for subsequent researchers to accurately locate and optimize the error points of the machining trajectory driven by the G-code. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1 It is the operation logic diagram of the system of the present invention;
[0039] Figure 2 This is a G code analysis flow chart of the system of the present invention;
[0040] Figure 3 This is the G code visualization flow chart of the system of the present invention
[0041] Figure 4a This is the G code information display diagram on the left side of the system trajectory point cloud mode;
[0042] Figure 4bThis is the G code visualization diagram on the right side of the system trajectory point cloud mode;
[0043] Figure 5a This is the G code information diagram on the left side of the system trajectory segment mode display interface;
[0044] Figure 5b The real-time status diagram on the right side of the system trajectory segment mode display interface;
[0045] Figure 6a This is the G code information diagram on the left side of the second display interface of the system trajectory segment mode;
[0046] Figure 6b It is the real-time status diagram on the right side of the second interface of the system trajectory segment mode display;
[0047] Figure 7a Draw a progress display diagram for the left model of the system trajectory plane mode display interface three;
[0048] Figure 7b It is the real-time status diagram on the right side of the third interface of the system trajectory plane mode display;
[0049] Figure 8a Draw a progress display diagram for the model on the left side of the system trajectory plane mode display interface;
[0050] Figure 8b It is the real-time status diagram on the right side of the system trajectory plane mode display interface. DETAILED DESCRIPTION
[0051] The following is combined with Figure 1 The method of the present invention is described in further detail.
[0052] The present invention makes use of Python, the most popular cross-platform, object-oriented, high-level dynamic programming language, which has the characteristics of quick learning, less code and high development efficiency; at the same time, it makes use of the PyQt framework, which is powerful, efficient and easy to develop and copy beautiful interfaces. The framework is a binding of the QT library under the Python language and inherits all the advantages of the QT library; and the visualization function of the G code uses a high-level API of the Python Visualization Toolkit (VTK) - PyVista, which is a 3D high-level library under Python and can more conveniently assist developers in developing 3D visualization applications than traditional VTK and pyside2.
[0053] After the file storing the G code is read into the system, during the parsing process of the G code, the fixed prefixes of the G code instructions such as G0 (G00), G1 (G01), etc. and the XYZ axis are written into the keyword dictionary as keywords. The system background begins to match the code line by line. During this period, the keyword dictionary is often called, and the G code instruction feature, that is, the space between each string in each line of G code, is used to determine whether the pointer matching is completed. At the same time, a signal mechanism is introduced to improve the Z-dimensional axis value. Finally, the relevant information of the matched string is written into the created corresponding array for subsequent calls.
[0054] Based on Python, a popular high-level programming language, and the development combination of PyQt5+PyVista, three different forms of expression are used in the process of 3D visualization of G code, namely point cloud mode, line segment mode and plane mode. These three modes need to call the three arrays stored before respectively. These three arrays correspond to three modes respectively. The point cloud mode array only stores the coordinates of three-dimensional points. In addition, the line segment mode array and the plane mode array also store the coordinate points and connection relationships of each layer according to the different Z-axis values, and the relationship between layers between coordinate points. Then PyVista is used to integrate the data and 3D modeling. The front-end visualization uses PyQt5 to display the G code 3D model and analysis information that has been modeled. At the same time, PyQt5 can be used to realize the file storage of G code analysis information.
[0055] A G-code hierarchical parsing and model visualization system based on pyqt5, including:
[0056] G-code parsing, splits each type of instruction such as G0 (G00) according to the characteristics of the G-code, and then extracts the coordinate points according to the XYZ axis keywords, temporarily stores them in an array for later use.
[0057] G code visualization is used to simulate the running trajectory of G code in CNC machine tools. The running speed, trajectory connection points, and trajectory characteristics (roughness, completeness, etc.) of G code instructions can be observed in real time.
[0058] The G-code visualization layer includes:
[0059] The parsing result display window is used to display the results of G code parsing, including the number of layers, pointnumbers of trajectory points and three-dimensional coordinate points of the code;
[0060] The trajectory information display window is used to display the trajectory points in the G code one by one, and the dynamic operation process of the G code trajectory generation can be observed in real time in multiple dimensions.
[0061] Other auxiliary modules, including trajectory drawing progress bar and save file button, are used to understand the progress of G-code trajectory generation and saving of G-code parsing files.
[0062] The trajectory information display window includes:
[0063] Track point cloud mode, display the coordinate point track of G code in point cloud mode;
[0064] Track segment mode: connect the track point cloud of point cloud mode into line segments according to the command sequence;
[0065] In the track plane mode, each line segment in the line segment mode is connected into layers according to the command sequence.
[0066] like Figure 1 As shown, a design method of a G code hierarchical parsing and model visualization system based on pyqt5 includes the following steps:
[0067] Step 1: The user selects the correct G code file on the local disk of the computer through this system, and the system automatically reads the file information;
[0068] Step 2: The system background uses the string matching method to segment the G code that is read and store it into the operation array layer by layer.
[0069] Step 3: After the background divides the G code information and stores it into the operation array, the three trajectory display modes under the Mesh button can draw the trajectory points in the array; when the system background receives the display instruction operation, the corresponding drawing operation and G code information display are performed on the system foreground interface according to the three different modes selected, and the system counts the drawing process and displays the counting result with a progress bar;
[0070] Step 4: The system background receives the save file operation and can link to the local disk of the computer, allowing the user to select a suitable location to save the G code analysis results in txt file format.
[0071] The step 1 comprises:
[0072] When the G-code file format selected by the user is incorrect, the system will not perform any subsequent operations. Only when the G-code file format selected by the user is correct will the system parse it and ignore the character strings outside the matching text range, such as code comments.
[0073] The step 2 comprises:
[0074] Step 2.1: Divide the G code according to the matching keyword text written according to the characteristics of the G code, and read the code layer by layer. Pair the first character string in a layer of code with the keyword text. If the character string successfully matches the matching keyword, the subsequent reading and matching of the layer of code will continue. Otherwise, the system automatically skips the line of code, and the pointer points to the first letter of the next line of code; the keyword text includes the instruction character string G0, G1...G100 and the three-dimensional coordinates X, Y, Z.
[0075] Step 2.2: After the first string of the line of code is matched successfully, the pointer continues to match the string forward. When encountering a string with the first letter X, Y or Z and the subsequent letters in digital form, the system copies the number into the coordinate point array. If the X, Y or Z coordinate is missing after the line of code is matched, the system will automatically add the previous identical X, Y or Z coordinate value to the corresponding position in the storage coordinate point array;
[0076] Step 2.3: Based on steps 2.1 and 2.2, the system will allocate two more two-dimensional arrays in the memory, which are used to store the coordinate points and connection relationships of each layer, as well as the relationships between coordinate points and between layers.
[0077] The step 3 comprises:
[0078] Use the pv.PolyData() command to convert the three arrays generated in step 2 into a data format supported by pyvista for subsequent calls;
[0079] The trajectory point cloud model displays each 3D coordinate point in the window, as well as the information after the code instruction is parsed (including the number of layers and trajectory points);
[0080] The track segment mode creates a multi-threaded form, places time-consuming drawing operations in the child thread, and the rest of the operations in the main thread to prevent a single thread from causing system lag or unresponsiveness. After extracting the array that stores the relationship between coordinate points and lines, draw operations one by one and display the information of each coordinate point.
[0081] The track plane mode, like the previous mode, creates a multi-threaded form, extracts the array that stores the relationship between the layers of the coordinate points, draws them one by one, and displays each coordinate point.
[0082] The step 4 comprises:
[0083] The data in the G-code parsing data display interface (including the number of layers, number of trajectory points and coordinates) can be saved by the user in the local computer. The response feedback mechanism of pyqt5 can be linked to the local computer disk, making it convenient for users to choose the storage location.
[0084] G code analysis process is as follows Figure 2 As shown in the figure, the process is executed by a multi-thread mechanism and a real-time feedback mechanism. The multi-thread mechanism is that the main thread (process) creates a new sub-thread, which can be defined as a daemon process, which is mainly responsible for the matching of the keyword dictionary and the G-code instructions by the guard system. This operation is mainly to enable the matching process and the storage process to proceed at the same time, so that the subsequent model visualization process has the ability of real-time drawing, so that researchers can observe the dynamic trajectory generation process of the G-code simulation operation.
[0085] G-code information 3D visualization process Figure 3 As shown in the figure, the process is also executed by a multi-thread mechanism. The main thread (process) creates a new sub-thread, which is defined by the Worker class (inherited from the QThread class of PyQt5). The subsequent visualization of the point cloud mode, line segment mode and plane mode, as well as the model simulation progress calculation operation and the display operation of the G code analysis information in the data display window. The main thread is responsible for real-time response to the reception and feedback of the save file button signal. This multi-threaded isolation operation enables researchers to save the required analysis information in the form of files in the local computer at the same time during the model simulation process, preventing the system from failing to respond to multiple operations in time when a single thread is used, causing the process to freeze, and then causing the system to crash or data loss.
[0086] In order to complete the design and implementation of this system, the design environment adopted by this system is Windows 10 operating system, and the development software is PyCharm Community Edition 2022.2.2. The PyQt5 and PyVista libraries are imported in the Python3.6 development environment. With the powerful functions and efficient development efficiency of PyQt5 and PyVista tool libraries, the front-end interface is developed using QtDesigner, and the back-end logical operations are mainly processed using toolkits such as PyQt5.QtWidgets, pyvistaqt and PyQt5.QtCore.
[0087] like Figure 4a , 4b As shown in the figure, this is the G code model trajectory simulation point cloud mode of this system. This system mainly includes six modules, namely the file operation button and Mesh grid mode selection button on the top layer, the G code parsing information display window on the left of the middle layer and the gray model trajectory simulation display window on the right, the save file button on the bottom left and the trajectory simulation drawing progress bar. In this point cloud mode, Figure 4a As shown in the figure, the parsing information display window on the left shows the number of layers and coordinate points of the G code file. Figure 4bAs shown, the simulation window on the right displays the point cloud form of the model. From the point cloud form of the model, we can intuitively see that the G code has a lot of room for optimization. Most positions of the five-pointed star have problems of incomplete coordinate points and rough coordinate trajectories. Relevant researchers are urgently needed to optimize the relevant points.
[0088] like Figure 5a to Figure 6b As shown in the figure, it is the G code model trajectory simulation line segment mode. Figure 5a , Figure 6a As shown, the parsing information on the left shows the coordinate points contained in different line segments, such as Figure 5b and Figure 6b As shown, the right window shows that the points belonging to the same line segment are connected according to the code running order based on the above point cloud mode. At this time, you can see the real-time trajectory motion process of the G code model simulation and the model drawing progress displayed in real time by the percentage progress bar.
[0089] like Figure 7a to Figure 8b As shown in the figure, it is the plane mode of G code model trajectory simulation. Figure 7a , Figure 8a As shown, the parsing information displayed on the left is essentially the same as the information displayed in the previous line segment mode, but this time it expresses the coordinate point information contained in different layers, such as Figure 7b and Figure 8b As shown, the right window displays in real time the process of connecting line segments belonging to the same plane, as well as the model drawing progress bar displayed in the lower left corner.
Claims
1. A G code hierarchical analysis and model visualization system based on pyqt5, characterized in that: include: The G code parsing layer is used to segment each type of instruction according to the Gcode code, and then extract the coordinate points according to the set keywords and store them in an array for calling; The G-code visualization layer is used to obtain the array and simulate the running trajectory of the G-code in the CNC machine tool to observe the state characteristics of the G-code instructions in real time.
2. A pyqt5-based G code hierarchical parsing and model visualization system according to claim 1, characterized in that: The G-code visualization layer includes: The analysis result display window module is used to display the results of G code analysis, including the number of code layers, track points and three-dimensional coordinate points; The trajectory information display window module is used to display the trajectory points in the G code and observe the dynamic operation process of the G code trajectory generation in real time in multiple dimensions; Auxiliary module, including trajectory drawing progress bar and save file button, used to display the trajectory generation progress of G code and save the G code parsing file.
3. The pyqt5-based G code hierarchical analysis and model visualization system according to claim 1, characterized in that: The trajectory information display window module includes: Track point cloud mode, used to display the coordinate point track of G code in point cloud mode; The trajectory line segment mode is used to connect the trajectory point cloud of the point cloud mode into a line segment according to the G code instruction sequence, that is, to determine whether the current point and the next point form a line segment based on the Z-axis value: if the difference between the Z-axis values of the current point and the next point is less than the threshold, the current point and the next point are connected into a line segment; otherwise, no line is connected; The track plane mode is used to connect each line segment of the line segment mode into a layer according to the Z-axis value. That is, if the difference between the average Z-axis value of all points of the current line segment and the average Z-axis value of all points of the next line segment is less than the threshold, the current line segment and the next line segment are connected into a layer; otherwise, they are not connected into a layer.
4. A G code hierarchical analysis and model visualization method based on pyqt5, characterized in that: The following steps are involved: Step 1: Get the G-code file; Step 2: Use the string matching method to perform instruction segmentation on the read G code and store it layer by layer into the operation array; Step 3: Perform corresponding drawing operations and G-code information display according to the selected mode, count the drawing process and display the counting results.
5. The method for hierarchical analysis and model visualization of G codes based on pyqt5 according to claim 4, characterized in that: In step 1: Only when the selected G-code file format is correct, the G-code file is parsed and the character strings outside the matching text range are ignored.
6. The method for hierarchical analysis and model visualization of G codes based on pyqt5 according to claim 4, characterized in that: The step 2 comprises: Step 2.1: Divide the G code according to the matching keyword text set by the G code characteristics, read the code layer by layer through the pointer, and match the first character string in each layer of the code with the keyword text; if the first character string matches the matching keyword text successfully, execute step 2.2 for the layer of code, otherwise automatically skip the line of code, and the pointer points to the first letter of the next line of code, and execute the step of matching the first character string with the keyword text; Step 2.2: After the first string of the current line of code is matched successfully, the pointer continues to match the string forward. When encountering a string whose first letter is X, Y or Z and whose subsequent letters are in the form of numbers, the number is copied into the coordinate point array; if the X, Y or Z coordinate is missing after the matching of the line of code is completed, 0 is automatically added to the corresponding position in the first array used to store the coordinate points; Step 2.3: Allocate two two-dimensional arrays in the memory at the same time. The second array is used to store the coordinate points and connection relationships of each layer, and the third array is used to store the relationship between the coordinate points and the relationship between layers.
7. The method for hierarchical analysis and model visualization of G codes based on pyqt5 according to claim 4, characterized in that: The step 3 comprises: Convert the first array, the second array and the third array generated in step 2 into the required data format for subsequent calls; The build mode is as follows: Track point cloud mode displays each 3D coordinate point and the parsed information of the code instruction in the window; Track segment mode, create a multi-threaded form, put the specified drawing operation in the sub-thread, and the rest of the operations in the main thread; after extracting the second array, draw each coordinate point one by one, and display the information of each coordinate point; In the trajectory plane mode, a multi-threaded form is created, and after extracting the third array, each coordinate point is drawn one by one, and the information of each coordinate point is displayed.
8. The method for hierarchical analysis and model visualization of G codes based on pyqt5 according to claim 4, characterized in that: It also includes step 4, which is as follows: The G code analysis data displayed in different modes obtained through steps 2 and 3 are saved in the local computer, and the G code analysis data is saved by linking to the local computer disk to select a location.
9. A G code hierarchical analysis and model visualization device based on pyqt5, characterized in that: It comprises a memory and a processor; the memory is used to store a computer program; the processor is used to implement a pyqt5-based G code hierarchical parsing and model visualization method as described in any one of claims 4 to 8 when executing the computer program.
10. A computer-readable storage medium, characterized in that: A computer program is stored on the storage medium. When the computer program is executed by the processor, a pyqt5-based G code hierarchical analysis and model visualization method as described in any one of claims 4 to 8 is implemented.
Citation Information
Cited By
Simulation control method and equipment for die-free turning and storage medium
CN120491558A