Method and system for slicing STL (Standard Template Library) file into GCode file based on Dijkstra algorithm and related equipment
By introducing the Dijkstra algorithm in the STL file slicing process, optimizing the printing path, the problem of insufficient redundant path calculation and path optimization in traditional slicing algorithms is solved, and more efficient and high-quality 3D printing is achieved.
Patent Information
- Application Number
- CN202411895580.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-22
- Publication Date
- 2025-03-28
AI Technical Summary
The traditional STL file to GCode file slicing algorithm has problems with insufficient redundant path calculation and path optimization, resulting in inefficient printing efficiency and poor quality.
The STL file slicing method based on Dijkstra algorithm is adopted to generate optimized printing paths by constructing graph structure and shortest path optimization, thereby improving the efficiency and print quality of GCode generation.
It effectively reduces the empty path of the print head, reduces the non-productive time, improves printing efficiency and accuracy, and solves the problems of path connection discontinuous and redundant path calculation in traditional algorithms.
Smart Images

Figure CN119723002A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to 3D printing technology, and in particular to a technology of slicing an STL file into a GCode file. Background Art
[0002] With the rapid development of 3D printing technology, STL (Standard Three-Dimensional Graphics Language) file format has become the most commonly used model format in 3D printing. In daily use, STL file format needs to be converted to GCode file frequently. In the conventional conversion process, the corresponding slicing algorithm is used to convert STL file format to GCode file; however, in the process of converting STL file to GCode (3D printer control language), the efficiency and accuracy of the slicing algorithm directly affect the printing quality.
[0003] Although traditional slicing algorithms such as linear interpolation and Bezier curves can complete slicing tasks, they have the following problems when processing complex models:
[0004] 1. Additional calculation of redundant paths;
[0005] Existing algorithms are usually based on simple point-by-point processing logic and fail to effectively eliminate redundant paths. For example, when two paths cross or overlap during printing, traditional algorithms often directly generate these redundant paths without optimization, resulting in unnecessary additional calculations and extended printing time.
[0006] 2. The shortest path planning is not fully considered;
[0007] The paths generated by traditional slicing methods are usually connected according to the geometric order of the contours, without introducing a path optimization algorithm. When the print head moves from one path segment to another, it fails to find the shortest movement path, which leads to the following problems:
[0008] The print head movement path is too long: For example, for multiple isolated printing areas, traditional algorithms lack path planning capabilities, and the print head movement path between areas may be long, resulting in a waste of time.
[0009] Discontinuous path connection: The path sequence is not optimized, and the print head may start and stop frequently or jump, increasing printing instability.
[0010] Based on the above problems, traditional solutions suffer from low computational efficiency and insufficient path optimization during application. Summary of the invention
[0011] In view of the problems existing in the slicing scheme between existing STL files and GCode files, the purpose of the present invention is to provide a conversion scheme for slicing STL files into GCode files based on the Dijkstra algorithm. The scheme optimizes the slicing process by introducing the shortest path algorithm, improves the efficiency of GCode generation and the printing quality, thereby effectively overcoming the problems existing in the prior art.
[0012] In order to achieve the above object, the present invention provides a method for converting STL file slices into GCode files based on Dijkstra algorithm, the method comprising:
[0013] Step (1): STL model parsing: Use the STL file parsing library to read the STL model, extract the vertex coordinates and normal vectors of the triangular face, and construct the geometric structure of the three-dimensional model based on them;
[0014] Step (2): constructing a two-dimensional slice plane: according to the set layer height, the three-dimensional model constructed in step (1) is sliced along the Z axis to generate the slice contour of each layer;
[0015] Step (3): Constructing a graph structure: configuring the slice outlines of each layer generated in step (2) into nodes of the graph, configuring the edges between nodes into movement paths from one node to another, configuring the weight of the edge into the length of the path, and storing the relationship between the nodes and edges of each layer in the form of a graph structure;
[0016] Step (4): Optimizing the path based on the Dijkstra algorithm: For the graph structure constructed in step (3), the Dijkstra algorithm is used to calculate the shortest path from the starting point to each node to generate an optimized printing path;
[0017] Step (5): Generate GCode: Generate GCode according to the optimized path to control the operating parameters of the 3D printer.
[0018] In some embodiments of the present invention, when performing STL model parsing in step (1), the following steps are included:
[0019] Step (1.1): Read the STL file, use the STL file parsing library to read the 3D model file, and parse the data structure of the model;
[0020] Step (1.2): Extract the vertex coordinates of each triangle in the STL file;
[0021] Step (1.3): Based on the vertex coordinates extracted in step (1.2), calculate the normal vector of each triangle;
[0022] Step (1.4): extract the normal vector of the triangle calculated in step (1.3);
[0023] Step (1.5): Construct the complete geometric structure of the three-dimensional model based on the extracted triangular patch information.
[0024] In some embodiments of the present invention, when constructing the two-dimensional slice plane in step (2), the steps include:
[0025] Step (2.1): First, set the slice height according to the printing requirements and model accuracy requirements;
[0026] Step (2.2): Slice the three-dimensional model along the Z-axis direction, and generate multiple parallel two-dimensional slice planes from the bottom to the top of the three-dimensional model;
[0027] Step (2.3): Generate a layer of slice contour according to the intersection state between each slice plane generated in step (2.2) and the corresponding triangular facet in the three-dimensional model;
[0028] Step (2.4): storing the contour of each slice generated in step (2.3) in a structured manner;
[0029] Step (2.5): Process the continuity and closure of the contour so that the contour segments of each layer can form a closed curve.
[0030] In some embodiments of the present invention, when constructing the graph structure in step (3), the following steps are included:
[0031] Step (3.1): For the two-dimensional slice plane constructed in step (2), in each slice layer, configure the key points in the slice contour into nodes of the graph structure;
[0032] Step (3.2): configure the movement paths between nodes in each layer of slices into edges of the graph;
[0033] Step (3.3): Set a weight for each edge configured in step (3.2) and configure it into the path length between nodes accordingly;
[0034] Step (3.4): After completing the setting of nodes and edges, the relationship between the nodes and edges of this layer is stored in a graph structure, thereby establishing a graph structure data model.
[0035] In some embodiments of the present invention, in step (4), when optimizing the path based on the Dijkstra algorithm, a starting point setting is added to the Dijkstra algorithm model, the starting point of the printing layer is set as the source point, and the dynamic calculation is performed according to the initial position of the printer.
[0036] In order to achieve the above object, the present invention provides a conversion system for STL file slices to GCode files, the conversion system comprising:
[0037] An STL model parsing module, wherein the STL model parsing module is configured to read the STL model using an STL file parsing library, extract vertex coordinates and normal vectors of triangular facets, and construct a geometric structure of a three-dimensional model based on the vertex coordinates and normal vectors of the triangular facets;
[0038] A two-dimensional slice plane construction module, wherein the two-dimensional slice plane construction module is configured to interact with the STL model parsing module data, and slice the three-dimensional model constructed by the STL model parsing module along the Z axis by setting layer height parameters to generate a slice outline of each layer;
[0039] A graph structure construction module, wherein the graph structure construction module is configured to interact with the data of the two-dimensional slice plane construction module, and is capable of configuring the slice contours of each layer generated by the two-dimensional slice plane construction module into nodes of a graph, configuring the edges between the nodes into a moving path from one node to another node, configuring the weight of the edge into the length of the path, and storing the relationship between the nodes and the edges of each layer in a graph structure;
[0040] A path optimization module, wherein the path optimization module is configured to interact with the graph structure construction module data, and can calculate the shortest path from the starting point to each node using the Dijkstra algorithm for the graph structure constructed by the graph structure construction module, and generate an optimized printing path;
[0041] A GCode generation module is configured to interact with the path optimization module data and can generate GCode according to the optimized path generated by the path optimization module to control the operating parameters of the 3D printer.
[0042] In some embodiments of the present invention, the graph structure building module configures weights between nodes that each printer can move continuously on the graph structure to prepare for subsequent path optimization calculations.
[0043] In some embodiments of the present invention, the path optimization module adds a starting point setting for the Dijkstra algorithm model, sets the starting point of the printing layer as the source point, and dynamically calculates according to the initial position of the printer.
[0044] In order to achieve the above-mentioned object, the present invention also provides a computer-readable storage medium on which a program is stored, and when the program is executed by a processor, the steps of the above-mentioned method for converting STL file slices into GCode files are implemented.
[0045] In order to achieve the above object, the present invention also provides a processor, which is used to run a program, and when the program is running, the steps of the above-mentioned method for converting STL file slices into GCode files are executed.
[0046] In order to achieve the above-mentioned purpose, the present invention also provides a terminal device, which includes a processor, a memory, and a program stored in the memory and executable on the processor, wherein the program code is loaded and executed by the processor to implement the steps of the above-mentioned method for converting STL file slices into GCode files.
[0047] In order to achieve the above object, the present invention also provides a computer program product, which, when executed on a data processing device, is suitable for executing the steps of the above-mentioned method for converting STL file slices into GCode files.
[0048] The conversion solution of slicing an STL file into a GCode file provided by the present invention optimizes the slicing process by introducing a shortest path algorithm, thereby improving the efficiency of GCode generation and the printing quality.
[0049] The conversion solution of STL file slicing to GCode file provided by the present invention utilizes Dijkstra algorithm to optimize the shortest path of the printer's printing path, thereby optimizing the printing speed and efficiency, and reducing problems such as too long idle movement path of the print head and discontinuous path connection. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments.
[0051] Figure 1 It is a flow chart of the conversion method of STL file slices to GCode files based on Dijkstra algorithm in the present invention;
[0052] Figure 2 This is a system schematic diagram of a conversion system for slicing STL files into GCode files based on the Dijkstra algorithm in the present invention. DETAILED DESCRIPTION
[0053] In order to make the technical means, creative features, objectives and effects achieved by the present invention easy to understand, the present invention is further explained below with reference to specific diagrams.
[0054] When converting STL files in 3D printing control solutions to GCode, in order to ensure efficiency and accuracy, it is necessary to effectively overcome the problem of multi-contour path connection and avoid path intersection and duplication.
[0055] For the problem of connecting multiple contour paths, in complex models, there may be multiple unconnected contour areas in the same layer. How to effectively plan the movement path of the print head between the areas is a technical difficulty and also directly affects the printing efficiency.
[0056] For path crossing and repetition problems, unoptimized path planning may cause the print head to frequently cross or repeat paths, increasing time and resource consumption.
[0057] To this end, the present invention introduces the shortest path algorithm and optimizes the slicing process, thereby improving the efficiency of GCode generation and printing quality.
[0058] Specifically, the present invention optimizes the printing path by using the Dijkstra algorithm, that is, by using the Dijkstra algorithm to globally optimize the path of the slice layer, the shortest path from the starting point to each node is calculated, thereby avoiding the problems of redundant connections and long-distance empty shifts in traditional path generation.
[0059] Therefore, through the shortest path planning, the idle movement path of the print head is reduced, the non-productive time is reduced, and the printing efficiency is effectively improved; at the same time, the number of starts and stops of the print head is reduced through the optimized path, and the possibility of path intersection and error accumulation is reduced, thereby simultaneously improving the printing accuracy.
[0060] On this basis, the present invention further constructs a graph structure adapted for the Dijkstra algorithm, specifically by abstracting the slice layer contour into an undirected weighted graph, where the nodes are the key points of the contour and the edge weights are the Euclidean distances between the nodes, thereby providing an adaptive input structure for the Dijkstra algorithm.
[0061] The graph structure thus constructed for the adaptive Dijkstra algorithm can abstract complex contour layers, facilitate calculation and optimization, and thus improve the adaptability of complex models; at the same time, based on this graph structure, the shortest path algorithm (i.e., Dijkstra algorithm) can be flexibly applied to geometric features of different layers.
[0062] As a further optimization setting, the present invention further optimizes and improves the Dijkstra algorithm model so that the optimized Dijkstra algorithm model can better meet the needs of 3D printing.
[0063] First, the present invention adds a starting point setting to the Dijkstra algorithm model, sets the starting point of the printing layer as the source point, and dynamically calculates according to the initial position of the printer. This setting can effectively solve the problem of the singleness of the default source point of the traditional Dijkstra algorithm, thereby being able to adapt to the diversity of starting points of the printing path.
[0064] Furthermore, the present invention optimizes the algorithm calculation process for the Dijkstra algorithm model and reduces the calculation complexity of the Dijkstra algorithm.
[0065] The present invention adopts block optimization in the Dijkstra algorithm model in a large-scale graph, first calculating the local shortest path and then merging it into a global path. On this basis, heuristic search (such as combining an improved version of the A* algorithm) can be further used to improve the efficiency of the shortest path calculation. This can solve the problem that the standard Dijkstra algorithm may be slow to calculate due to the large number of nodes in the slice layer in a complex model.
[0066] Based on the above solution, the present invention provides a method for converting STL file slices into GCode files based on Dijkstra algorithm. Figure 1 The conversion method of this STL file slice to GCode file is specifically composed of the following steps:
[0067] Step (1): STL model parsing: Use the STL file parsing library to read the STL model, extract the vertex coordinates and normal vectors of the triangular face, and construct the geometric structure of the three-dimensional model based on them;
[0068] Step (2): constructing a two-dimensional slice plane: according to the set layer height, the three-dimensional model constructed in step (1) is sliced along the Z axis to generate the slice contour of each layer;
[0069] Step (3): Constructing a graph structure: configuring the slice outlines of each layer generated in step (2) into nodes of the graph, configuring the edges between nodes into movement paths from one node to another, configuring the weight of the edge into the length of the path, and storing the relationship between the nodes and edges of each layer in the form of a graph structure;
[0070] Step (4): Optimizing the path based on the Dijkstra algorithm: For the graph structure constructed in step (3), the Dijkstra algorithm is used to calculate the shortest path from the starting point to each node to generate an optimized printing path;
[0071] Step (5): Generate GCode: Generate GCode according to the optimized path to control the operating parameters of the 3D printer.
[0072] The following specifically describes the implementation process and corresponding technical features of the method for converting STL file slices into GCode files provided by the present invention.
[0073] When performing STL model parsing in step (1) of the present invention, the STL model parsing can be implemented by the following steps:
[0074] Step 1.1: Read the STL file, use the STL file parsing library to read the 3D model file, and parse the data structure of the model.
[0075] Among them, the STL file stores geometric information with triangular patches as the basic unit, and each patch contains three vertices and a surface normal vector.
[0076] In this regard, the STL file parsing library will gradually read these data and store them in a structured manner. The set of triangle faces is F = {f 1 ,f 2 ,...,f m}, where f i Represents the i-th triangle. Each triangle consists of three vertices and a normal vector.
[0077] Further, the definition is as follows:
[0078] Vertex coordinates: Let the triangle face f i The coordinates of the three vertices are v i1 =(x i1 ,y i1 ,z i1 ),v i2 =(x i2 ,y i2 ,z i2 ) and v i3 =(x i3 ,y i3 ,z i3 ).
[0079] Normal vector: The normal vector of each triangle is denoted by n i =(n ix ,n iy ,n iz ).
[0080] Therefore, the i-th triangle f i It can be represented as a combination of vertex and normal vector: i = {v i1 ,v i2 ,v i3 ,n i}.
[0081] Step 1.2: Extract the vertex coordinates of the triangle patch.
[0082] In the STL file, each triangle is defined by three vertices, and each vertex has a unique coordinate in three-dimensional space. The coordinate information of these vertices is read from the file through the parsing library and stored in the order of each triangle. This process can build a set containing the vertex information of all triangles, read the triangles one by one, and obtain the vertex coordinates.
[0083] As an example, let the three vertices of a triangle be A, B, and C, and define the vertex set as A = (x 1 ,y1 ,z 1 ), B=(x 2 ,y 2 ,z 2 ) and C=(x 3 ,y 3 ,z 3 );
[0084] In the subsequent slicing and path planning process, the vertex coordinates will be used to calculate the intersection of the model and the slicing plane.
[0085] Step 1.3: Normal vector calculation. The normal vector n of the triangle is usually stored directly in the STL file. If the normal vector is not provided, it can be calculated from the vertex coordinates.
[0086] As an example, for a given triangle with vertices A, B, and C, the steps to calculate the normal vector n are as follows:
[0087] 1. Calculate two edge vectors:
[0088] AB=(x 2 -x 1 ,y 2 -y 1 ,z 2 -z 1 );
[0089] AC=(x 3 -x 1 ,y 3 -y 1 ,z 3 -z 1 );
[0090] 2. Use the vector product (cross multiplication) to calculate the normal vector n:
[0091]
[0092] 3. Normalized normal vector:
[0093] In order to ensure the unit length of the normal vector, normalization is performed. The normal vector is expressed as:
[0094]
[0095] Where |n| is the modulus of the normal vector, and the calculation formula is:
[0096]
[0097] Step 1.4: Extract the normal vector of the triangle determined in step 1.3.
[0098] Each triangle also contains a normal vector, which indicates the orientation of the triangle. The normal vector determined here can be used to determine the orientation of the face and the geometric features of the model in the subsequent slicing process. The parsing library automatically extracts the normal vector information from the STL file and associates it with the corresponding triangle vertex data for storage.
[0099] Step 1.5: Construct the geometric structure of the three-dimensional model. According to the triangular face information extracted in the previous steps, including vertex coordinates and normal vectors, gradually construct a complete three-dimensional model structure.
[0100] Furthermore, the three-dimensional model structure formed here is represented in the form of a vertex set and an edge set, so as to clarify the connection relationship of each triangle face in the model; at the same time, the completed geometric structure provides support for subsequent slicing and path planning.
[0101] Step 1.6: Data storage and preparation, after completing the extraction of vertices and normal vectors, store this information in a data structure to facilitate subsequent slicing, path optimization and other operations. The vertex coordinates and normal vectors of each triangle will be stored in association for quick access and processing during the slicing process.
[0102] When constructing a two-dimensional slice plane in step (2) of the solution of the present invention, the two-dimensional slice plane construction can be implemented by the following steps:
[0103] Step 2.1: Set the slice layer height. First, set the slice layer height according to the printing requirements and model accuracy requirements.
[0104] The layer height set in this step is used to determine the interval between slices, that is, a layer of slices is generated at a certain height on the Z axis.
[0105] Specifically, the slice layer height here is preferably set according to factors such as the resolution of the printing device and material properties to ensure printing accuracy and surface smoothness.
[0106] Step 2.2: Slice along the Z axis. Slice the three-dimensional model along the Z axis to generate multiple parallel two-dimensional slice planes from the bottom to the top of the model.
[0107] On each slice plane, the intersection of the model's triangular facets and the plane will form one or more closed contours. Here, the three-dimensional model is gradually converted into a series of two-dimensional contours through this layer-by-layer slicing method.
[0108] Step 2.3: Calculate the slice contour. For each slice plane generated in step 2.2, determine which triangular faces in the model intersect with the current plane. On this basis, for the line segments generated by the intersecting faces on the slice plane, further connect these line segments to form a closed contour of a layer.
[0109] The contour of each layer formed here represents the path of the print head during the printing process, and the shape and complexity of the contour depends on the geometric characteristics of the model.
[0110] Step 2.4: Generate contour data and store the slice contour of each layer in a structured manner to facilitate subsequent path optimization and GCode generation.
[0111] These contour data contain the path shape and coordinate information of each layer, ensuring that the shape of the model can be accurately reproduced during the printing process. At the same time, the contour data also provides the basis for the Dijkstra algorithm in path planning.
[0112] Step 2.5: Process the continuity and closure of the contour. In the process of generating the slice contour, ensure that the contour line segments of each layer can form a closed curve, so as to ensure that there is no incomplete path when printing. Merge and close the discontinuous line segments to ensure the integrity of the printing and the structural stability of the layer.
[0113] In step (3) of the scheme of the present invention, in the construction of the graph structure, the key points of each layer of slice contour are abstractly configured as nodes of the graph, and the Euclidean distance between the nodes is used as the weight of the edge, thereby realizing a graphical representation of the slice data.
[0114] As a further illustration, in the construction of the graph structure, the scheme of the present invention extracts the key points of the contour (such as inflection points and endpoints) for node abstraction, effectively reducing the amount of redundant data and reducing the computational complexity. When setting the weight, the Euclidean distance is used as the edge weight, and combined with the optimization requirements of the printing path (such as directionality), it ensures that the path planning is highly matched with the actual printing performance.
[0115] At the same time, in the construction of the graph structure, the solution of the present invention maintains the continuity of adjacent slice layers while constructing within the plane, thereby ensuring the coherence between layers and providing support for subsequent path optimization.
[0116] The graph structure constructed in this way efficiently expresses the geometric information of the slice contour, reduces the complexity of path calculation, and improves the efficiency and accuracy of shortest path planning, thereby optimizing the printing process.
[0117] As a further explanation, the scheme of the present invention can be implemented by the following steps to build the graph structure:
[0118] Step 3.1: Construction of slice contour nodes. In each slice layer, the key points in the slice contour are configured as nodes of the graph structure.
[0119] Each node is configured as the starting or ending point of a printing path, and the coordinate information of the node in the current plane is recorded. In this way, each layer of slices can be converted into a graph composed of several nodes, providing a basis for path planning.
[0120] Step 3.2: Establish edges between nodes. To describe the printing path, the moving paths between nodes in each layer of slices are configured as edges of the graph.
[0121] The edges represent the paths that the print head moves from one node to another. The directions of the edges are preferably determined by path planning to ensure that the print head passes through all nodes in the optimal order during printing, thereby reducing non-printing movements.
[0122] Step 3.3: Set the edge weight. For each edge established in step 3.2, set a weight to represent the path length between the nodes.
[0123] The weight here not only incorporates the physical distance between the two points, but can also be adjusted according to the printing process or needs. For example, a smaller weight can be given to an area that needs to speed up printing, or a larger weight can be given to a detailed area, thereby controlling printing accuracy and efficiency.
[0124] Step 3.4: Build a graph structure data model. After completing the setting of nodes and edges, store the relationship between nodes and edges of this layer in the form of a graph structure. Each graph node contains information about the edges connected to other nodes. This graph structure provides basic data for the path optimization algorithm, enabling the algorithm to find the optimal path on the graph structure, thereby guiding the generation of the printing path.
[0125] In this step, when the relationship between nodes and edges of each layer is stored in a graph structure, the relationship between nodes and edges is specifically stored in the form of an adjacency matrix. Preferably, a sparse matrix is used for storage, which can reduce the storage redundancy of large-scale node data, improve computing efficiency and optimize storage efficiency.
[0126] Furthermore, in the storage method of this graph structure, not only the Euclidean distance is stored for the edge weight, but also dynamic parameters such as printing direction and speed constraints are added, thereby realizing dynamic storage of weights and improving the adaptability of path planning.
[0127] Furthermore, in the storage method of the present graph structure, cross-layer connection information is added to the graph structure to ensure a smooth transition of adjacent slice paths, thereby supporting inter-layer coherence and thus enabling inter-layer topology expansion.
[0128] The storage solution formed based on this takes into account both efficiency and flexibility, adapts to the path planning requirements of complex models, and can significantly improve storage and computing performance.
[0129] Step 3.5: Optimization and integrity check of graph structure,After building the graph structure, it is necessary to check the integrity of the graph,to ensure that all contour nodes are included in the graph and,connected correctly to avoid missing or disconnected paths.
[0130] In addition, the graph structure is optimized by adjusting weights and connection relationships to reduce redundant movement paths, further improving printing efficiency and accuracy.
[0131] The connection relationship adjustments here mainly focus on removing redundant edges and adding cross-layer connections.
[0132] For redundant edge removal, specifically, when there are multiple edges between nodes, the edge with the smallest weight is retained, and redundant paths are removed to reduce computational complexity.
[0133] For,cross-layer connection addition, specifically, between nodes of adjacent slice layers, edges are added according to,the spatial position and path smoothness requirements to ensure that the,printing path is continuous and smooth when transitioning between layers.
[0134] When optimizing and adjusting the connection relationship, the printer's motion characteristics are combined to give priority to connecting edges that conform to the printing direction (such as reducing sharp turns and reverse paths) to optimize the print head motion trajectory.
[0135] Here, when adjusting the dynamic weight, the edge weight is updated in real time based on actual requirements such as path length, printing speed, and material extrusion amount, so that the generated path is more in line with the printing process requirements.
[0136] In this step, after optimizing and adjusting the weights and connection relationships of the graph structure, the optimized connection relationships can reduce unnecessary empty movement paths, improve path planning efficiency and printing quality, and meet the printing needs of complex models.
[0137] In step (4) of the scheme of the present invention, when optimizing the path based on the Dijkstra algorithm, the improved and optimized Dijkstra algorithm is used to optimize the shortest path of the printing path of the printer, thereby optimizing the printing speed and efficiency, and reducing the problems of too long idle movement path of the print head and discontinuous path connection. The implementation scheme of the improved and optimized Dijkstra algorithm is as mentioned above and will not be described in detail here.
[0138] As a further illustration, the solution of the present invention can be implemented by the following steps to optimize the path based on the Dijkstra algorithm:
[0139] Step 4.1: Define the starting node and distance function.
[0140] First, select a starting node S in the slice graph structure. Here, the starting point of the current layer path or the nearest node to the current position of the nozzle is preferred. Let G = (V, E) be the graph structure of the slice layer, where V is the node set and E is the edge set d (S, v), which represents the shortest path length from the starting node S to the node v. Initially defined:
[0141]
[0142] This means that the distance from the starting point to itself is zero, while the distances to other nodes are initially infinite, indicating that the path is unknown.
[0143] Step 4.2: Update the distance formula. For each node u, find the edge e from u to its adjacent node v. uv The weight w(u,v), that is, the path length from u to v. If there is a shorter path through node u to reach v, then update d(S,v) to:
[0144] d(S,v)=min{d(S,v),d(S,u)+w(u,v)};
[0145] Among them, d(S,u) is the currently known shortest path length from S to u, and w(u,v) represents the path weight from u to v; this update formula ensures that the shortest path is selected each time, thereby gradually reducing the total length of the overall path.
[0146] Step 4.3: Visit nodes layer by layer, select the node u with the smallest current path length in turn and mark it as visited, then update the path length of its adjacent nodes through node u.
[0147] Specifically, if the set of marked nodes is U, for each adjacent node v∈VU, apply the formula:
[0148] d(S,v)=min{d(S,v),d(S,u)+w(u,v)};
[0149] This process stops when all nodes are marked as visited. This ensures that the shortest path to each node is determined when it is visited, so no modification is required.
[0150] Step 4.4: Calculate the global shortest path set, and through the iterative update of the Dijkstra algorithm, finally obtain the shortest path set {P(S,v)} from the starting node S to all other nodes v∈V, where:
[0151] P(S,v)={S,u 1 ,u 12 ,....,v};
[0152] The path set {P(S,v)} contains the shortest path sequence from the starting node S to all nodes v, ensuring that the nodes in each layer of slices are traversed and the total path length is minimized.
[0153] Step 4.5: Optimize the total path length, which is defined as the cumulative distance from the starting node S to all other nodes:
[0154] L total =∑ v∈V d(S,v);
[0155] The total length of the optimized path is the smallest, ensuring the best printing path sequence. The final path set {P(S,v)} is converted into the printing path in the GCode command, guiding the nozzle to pass through all nodes in sequence along the shortest path, reducing unnecessary empty moves.
[0156] When generating GCode, step (5) in the solution of the present invention can be achieved by the following steps:
[0157] Step 5.1: Set initial parameters. Before generating GCode, first set the initial parameters for printing. This includes: the coordinates of the printing start position, nozzle temperature and bed temperature, extruder initial state, printing layer height, nozzle diameter and other basic printing parameters. These initial parameters are set through specific GCode instructions.
[0158] Step 5.2: Path conversion to GCode instructions. Utilize the optimized shortest path sequence to gradually convert the path nodes of each layer into GCode instructions.
[0159] The main steps to achieve this step include:
[0160] Movement command: Use G1 command to control the linear movement of the nozzle. For each path node, generate the corresponding G1 command according to its coordinates;
[0161] Extrusion control: Add extrusion control instructions on the printing path to control the extrusion amount of the printing material. The extrusion amount is calculated based on the distance the nozzle moves and is controlled by the E parameter;
[0162] Step 5.3: Adjust the printing speed and extrusion amount. According to the geometric characteristics of the path, dynamically adjust the movement speed and extrusion amount in the GCode; for complex paths or corner positions, reduce the speed to increase the printing accuracy; for straight lines or continuous paths, increase the speed to speed up the printing process. This can be achieved by setting different F (moving speed) and E (extrusion amount) parameters to ensure that the extrusion amount is increased in the high-speed area and the fine extrusion is maintained in the low-speed area;
[0163] Step 5.4: Interlayer transition control, after completing the generation of GCode for one layer, add transition layer movement instructions to guide the nozzle to move from the current layer to the next layer. This usually includes instructions to increase the nozzle height (through Z-axis adjustment) and fast transition of non-printing movement (no extrusion);
[0164] Step 5.5: Output the final GCode, summarize all generated GCode instructions, and output them as a standard GCode file in the printing order. The file contains all instructions from the starting point, printing of each path node, transition between layers, to the end of printing, forming a complete printing path control process.
[0165] The conversion method of STL file slices to GCode files based on Dijkstra algorithm given in this example solution can constitute a corresponding software program in specific application to form a corresponding STL file slice to GCode file conversion system. When the software program is running, it will execute the above-mentioned conversion method of STL file slices to GCode files, and store it in the corresponding storage medium for the processor to call and execute.
[0166] See also Figure 2 The STL file slice to GCode file conversion system 100 mainly includes an STL model parsing module 110, a two-dimensional slice plane construction module 120, a graph structure construction module 130, a path optimization module 140 and a GCode generation module 150.
[0167] The STL model parsing module 110 in the system is configured to use the STL file parsing library to read the STL model, extract the vertex coordinates and normal vectors of the triangular facets, and construct the geometric structure of the three-dimensional model accordingly.
[0168] The two-dimensional slice plane construction module 120 in the present system is configured to interact with the STL model analysis module 110 in data, and by setting layer height parameters, slices the three-dimensional model constructed by the STL model analysis module along the Z axis to generate a slice contour for each layer.
[0169] The graph structure construction module 130 in the present system is configured to interact with the two-dimensional slice plane construction module 120 in data, and is capable of configuring the slice contours of each layer generated by the two-dimensional slice plane construction module 120 into nodes of a graph, configuring the edges between the nodes into a moving path from one node to another, configuring the weight of the edge into the length of the path, and storing the relationship between the nodes and edges of each layer in the form of a graph structure.
[0170] The path optimization module 140 in the system is configured to interact with the graph structure construction module 130 , and can calculate the shortest path from the starting point to each node using the Dijkstra algorithm for the graph structure constructed by the graph structure construction module 130 , and generate an optimized printing path.
[0171] The GCode generation module 150 in the present system is configured to interact with the path optimization module 140 in data, and can generate GCode according to the optimized path generated by the path optimization module 140 to control the operating parameters of the 3D printer.
[0172] As a further explanation, the STL model parsing module 110 in the present system solution is specifically configured to be able to implement the solution constructed by the aforementioned steps (1.1) to (1.6).
[0173] As a further explanation, the two-dimensional slice plane construction module 120 in the present system solution is specifically configured to be able to implement the solution constructed by the aforementioned steps (2.1) to (2.5).
[0174] As a further explanation, the graph structure construction module 130 in the present system solution is specifically configured to be able to implement the solutions constructed in the aforementioned steps (3.1) to (3.5).
[0175] As a further explanation, the path optimization module 140 in the present system solution is specifically configured to be able to implement the solution constructed by the aforementioned steps (4.1) to (4.5).
[0176] As a further explanation, the GCode generation module 150 in the present system solution is specifically configured to be able to implement the solution constructed by the aforementioned steps (5.1) to (5.5).
[0177] Based on the above-mentioned conversion scheme of STL file slices based on Dijkstra algorithm to GCode files, an embodiment of the present invention also provides a computer-readable storage medium, on which a program is stored, and when the program is executed by a processor, the steps of the conversion method of STL file slices based on Dijkstra algorithm to GCode files are implemented.
[0178] An embodiment of the present invention further provides a processor, which is used to run a program, wherein when the program is run, the steps of the above-mentioned method for converting STL file slices into GCode files based on the Dijkstra algorithm are executed.
[0179] An embodiment of the present invention also provides a terminal device, which includes a processor, a memory, and a program stored in the memory and executable on the processor, wherein the program code is loaded and executed by the processor to implement the steps of the above-mentioned method for converting STL file slices into GCode files based on the Dijkstra algorithm.
[0180] The present invention also provides a computer program product, which, when executed on a data processing device, is suitable for executing the steps of the above-mentioned method for converting STL file slices into GCode files based on the Dijkstra algorithm.
[0181] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0182] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and modules described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0183] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0184] The present invention is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products of the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0185] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0186] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0187] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0188] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0189] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device.
[0190] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0191] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, systems or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0192] The above-mentioned method of the present invention, or a specific system unit, or a part of the same, is a pure software architecture, which can be arranged on a physical medium, such as a hard disk, an optical disk, or any electronic device (such as a smart phone, a computer-readable storage medium) through program code. When the machine loads the program code and executes it (such as a smart phone loading and executing it), the machine becomes a device for implementing the present invention. The above-mentioned method and device of the present invention can also be transmitted in the form of program code through some transmission media, such as cables, optical fibers, or any transmission mode. When the program code is received, loaded and executed by a machine (such as a smart phone), the machine becomes a device for implementing the present invention.
[0193] The above shows and describes the basic principles, main features and advantages of the present invention. It should be understood by those skilled in the art that the present invention is not limited to the above embodiments, and the above embodiments and descriptions are only for explaining the principles of the present invention. Without departing from the spirit and scope of the present invention, the present invention may have various changes and improvements, which fall within the scope of the present invention to be protected. The scope of protection of the present invention is defined by the attached claims and their equivalents.
Claims
1. A method for converting STL file slices into GCode files based on Dijkstra algorithm, characterized in that: The method comprises: Step (1): STL model parsing: Use the STL file parsing library to read the STL model, extract the vertex coordinates and normal vectors of the triangular face, and construct the geometric structure of the three-dimensional model based on them; Step (2): constructing a two-dimensional slice plane: according to the set layer height, the three-dimensional model constructed in step (1) is sliced along the Z axis to generate the slice contour of each layer; Step (3): Constructing a graph structure: configuring the slice outlines of each layer generated in step (2) into nodes of the graph, configuring the edges between nodes into movement paths from one node to another, configuring the weight of the edge into the length of the path, and storing the relationship between the nodes and edges of each layer in the form of a graph structure; Step (4): Optimizing the path based on the Dijkstra algorithm: For the graph structure constructed in step (3), the Dijkstra algorithm is used to calculate the shortest path from the starting point to each node to generate an optimized printing path; Step (5): Generate GCode: Generate GCode according to the optimized path to control the operating parameters of the 3D printer.
2. The method for converting STL file slices into GCode files according to claim 1, characterized in that: When performing STL model parsing in step (1), the following steps are included: Step (1.1): Read the STL file, use the STL file parsing library to read the 3D model file, and parse the data structure of the model; Step (1.2): Extract the vertex coordinates of each triangle in the STL file; Step (1.3): Based on the vertex coordinates extracted in step (1.2), calculate the normal vector of each triangle; Step (1.4): extract the normal vector of the triangle calculated in step (1.3); Step (1.5): Construct the complete geometric structure of the three-dimensional model based on the extracted triangular patch information.
3. The method for converting STL file slices into GCode files according to claim 1, characterized in that: When constructing the two-dimensional slice plane in step (2), the following steps are included: Step (2.1): First, set the slice height according to the printing requirements and model accuracy requirements; Step (2.2): Slice the three-dimensional model along the Z-axis direction, and generate multiple parallel two-dimensional slice planes from the bottom to the top of the three-dimensional model; Step (2.3): Generate a layer of slice contour according to the intersection state between each slice plane generated in step (2.2) and the corresponding triangular facet in the three-dimensional model; Step (2.4): storing the contour of each slice generated in step (2.3) in a structured manner; Step (2.5): Process the continuity and closure of the contour so that the contour segments of each layer can form a closed curve.
4. The method for converting STL file slices into GCode files according to claim 1, characterized in that: When constructing the graph structure in step (3), the following steps are included: Step (3.1): For the two-dimensional slice plane constructed in step (2), in each slice layer, configure the key points in the slice contour into nodes of the graph structure; Step (3.2): configure the movement paths between nodes in each layer of slices into edges of the graph; Step (3.3): Set a weight for each edge configured in step (3.2) and configure it into the path length between nodes accordingly; Step (3.4): After completing the setting of nodes and edges, the relationship between the nodes and edges of this layer is stored in a graph structure, thereby establishing a graph structure data model.
5. The method for converting STL file slices into GCode files according to claim 1, characterized in that: In the step (4), when optimizing the path based on the Dijkstra algorithm, a starting point setting is added to the Dijkstra algorithm model, the starting point of the printing layer is set as the source point, and is dynamically calculated according to the initial position of the printer.
6. A system for converting STL file slices into GCode files, characterized in that: The conversion system comprises: An STL model parsing module, wherein the STL model parsing module is configured to read the STL model using an STL file parsing library, extract vertex coordinates and normal vectors of triangular facets, and construct a geometric structure of a three-dimensional model based on the vertex coordinates and normal vectors of the triangular facets; A two-dimensional slice plane construction module, wherein the two-dimensional slice plane construction module is configured to interact with the STL model parsing module data, and slice the three-dimensional model constructed by the STL model parsing module along the Z axis by setting layer height parameters to generate a slice outline of each layer; A graph structure construction module, wherein the graph structure construction module is configured to interact with the data of the two-dimensional slice plane construction module, and is capable of configuring the slice contours of each layer generated by the two-dimensional slice plane construction module into nodes of a graph, configuring the edges between the nodes into a moving path from one node to another node, configuring the weight of the edge into the length of the path, and storing the relationship between the nodes and the edges of each layer in a graph structure; A path optimization module, wherein the path optimization module is configured to interact with the graph structure construction module data, and can calculate the shortest path from the starting point to each node using the Dijkstra algorithm for the graph structure constructed by the graph structure construction module, and generate an optimized printing path; A GCode generation module is configured to interact with the path optimization module data and can generate GCode according to the optimized path generated by the path optimization module to control the operating parameters of the 3D printer.
7. The system for converting STL file slices into GCode files according to claim 6, characterized in that: The graph structure building module configures weights between nodes on the graph structure to which each printer can move continuously.
8. The system for converting STL file slices into GCode files according to claim 6, characterized in that: The path optimization module adds a starting point setting for the Dijkstra algorithm model, sets the starting point of the printing layer as the source point, and dynamically calculates according to the initial position of the printer.
9. A computer-readable storage medium having a program stored thereon, characterized in that: When the program is executed by a processor, the steps of the method for converting STL file slices into GCode files described in any one of claims 1 to 5 are implemented.
10. A computer program product, characterized in that When executed on a data processing device, it is suitable for executing the steps of the method for converting STL file slices into GCode files as described in any one of claims 1 to 5.