Research on reverse engineering reconstruction of parts CAD model for additive manufacturing

Taking additive manufacturing as the goal, taking the gear parts commonly used in mechanical engineering as an example, the bitmap of gear parts is processed and the pixel coordinate information of cross-section contour is extracted based on MATLAB software. The extracted pixel coordinates are saved in the external text file, and then combined with UG software platform, visual is used Studio 2012 carries on the deep secondary development, through programming to realize the reading, creating and curve fitting of data points in UG software, and finally realizes the reconstruction of 3D CAD model of parts.


Introduction
Additive manufacturing technology is a new manufacturing technology based on the principle of material accumulation forming. Reverse engineering is an important work content of additive manufacturing. It is a process of reconstructing CAD model of parts by using various digital technology and CAD technology according to the physical model of existing parts (or Parts prototype) without design drawings or incomplete design drawings and no CAD model [1] [2] [3] .
This paper focuses on three-dimensional reverse engineering reconstruction of gear. Because the process of reverse gear 3D model based on image recognition is usually not for all shaped parts, certain conditions must be met, such as the contour of the section must be consistent in a certain direction, or the revolving body formed by the generatrix around the central axis, the parts formed by the section contour scanning along a certain direction, and so on. This paper uses MATLAB and VC + + 2012 to carry out the secondary development of UG, and complete the reverse of three-dimensional CAD model of gear. This CAD model can be used as the front-end data model of additive manufacturing.

Gaussian filtering
Gaussian filtering is a kind of linear and smooth filtering method, which is widely used in image processing. Generally speaking, the implementation method of Gaussian filtering is a weighted process. In the picture, the pixel value of each point is no longer isolated; it uses a convolution to scan all the pixels in the screen. For a pixel point, firstly, according to the convolution of the neighborhood pixels and its own pixel value weighted, so that the completion of the image Gaussian filtering. As far as the Gaussian filter is concerned, the Gaussian filter is usually used to smooth the image as follows: In equation (1), is the width of the filter. The realization of Gaussian filtering in MATLAB needs fsspecial and imfilter functions. Fsspecial function is used to establish pre-defined filter operator, while imfilter can filter multi-dimensional image. As shown in Fig.1 is the comparison before and after filtering. After Gaussian filtering, it can be seen that when sigma = 1.6, there is no big change before and after filtering. Through experiments, it can be proved that the greater the Sigma value, the better the smoothness of the image, otherwise, the smaller.

Edge detection
In the process of reverse engineering, the most important thing is to extract the contour section curve of the part. Generally, the image must be binarized first. After processing, the pixel value can only be 0 or 1, 1 represents black, 0 represents white, and then the coordinates of the points with pixel value of 1 on the curve are extracted. The commonly used edge detection methods include Roberts's operator, Sobel and Prewitt operator, Gaussian Laplacian operator and Canny operator [4] .Considering the three evaluation criteria of edge detection, the algorithm is simple and has good detection accuracy, which has been widely used. So Canny operator is used for edge detection.
The basic principle of Canny operator is to denoise the original image by using the first-order partial derivative of two-dimensional Gaussian function, and then calculate the amplitude and direction of gradient by using the finite difference of the first-order partial derivative. Finally, the maximum value of the amplitude is suppressed, and the double domain algorithm is used to detect and link edges. The process of edge detection using Canny operator is shown in Fig.2.
After edge detection, it is still necessary to refine the extracted edge. The function used is bwmorph function. The main purpose is to reduce the target line. After actual calculation, the interference of noise points can be effectively reduced by filtering and thinning, and the amount of data can be significantly reduced by 20% -25%. Finally, the coordinates of the points with the pixel value of 1 on the contour are extracted, and the coordinate values are written into the named .TXT format file according to the line, so as to facilitate the subsequent secondary development work. As shown in Fig.3 is the boundary obtained after edge detection. To achieve Canny operator and coordinate point extraction in MATLAB。  Fig. 4. Gear and standard block.

Calculate the scale and gear height
Because the contour coordinate points extracted by image processing are only the coordinates of pixel points, it is difficult to obtain the height parameters of the part, and it is impossible to accurately establish the 3D model of the part and truly reflect the geometric characteristics of the 3D model by means of UG secondary development. Therefore, a standard sample block is used as a reference when shooting, as shown in Fig.4. Assuming that the height of the standard sample block is L, the standard sample block and the gear are placed at the same position and photographed from the side. The edge detection is carried out according to the above steps in 2.1 and 2.2, and the contour coordinate points are extracted. Finally, secondary development is carried out in UG platform to quickly fit the profile of standard sample block and gear side view, and the view height of standard sample block and part can be measured by "analysis" function in UG software. Let them be H and h respectively, and compare the image height of the sample block with the actual height. If the height of the gear is M, then the height of the gear can be measured according to the scale conversion as shown in equation (2).
In the same way, the standard block can be used as a reference when photographing the gear profile. According to the above method, a scaling ratio can be obtained to restore the shape of the part to the actual size of the part.

Configuration of UG secondary development environment
Although the function of UG is very powerful, it can still be redeveloped. In order to use the function of UG in VC, the first step is to obtain the permission of UG entrance, which is to build a bridge between VC and UG [5] . Usually, the way to build this kind of bridge is to find UGOPEN in the installation directory of UG, and then replace the three folders with the same name under the installation directory of vs with VB, VC and VC#.In this paper, it use UG / open API secondary development requires the creation of application and startup two folders, and then write a script language to create a toolbar and drop-down menu in UG. The file generated as .men is saved in the startup directory. The .dlx, .dlg, .ubm files developed by UIStyler is stored in the application. According to the above method, the .DLL files that have been debugged and produced are also placed in the startup directory, development completed.

Reading data points in VS
In section 2.2 of this paper, all the data points on the gear part contour have been read into the specified storage location. As the intermediate platform of MATLAB and UG, VS needs to read data points in UG, so it is necessary to count and read the data of points, and create them with functions. The purpose of data point statistics is to check whether the amount of data has been effectively controlled after filtering, thinning and extraction. In the .TXT file, each coordinate point is used as a line of text. To count the number of line breaks, you only need to count the number of line breaks. If you read a symbol, the number of lines will be increased by 1 until you get to the last line. Then the last line number is the number of points. The results are written directly to the list window. Similarly, read the data points of the .TXT file line by line according to the above method, and UF in the UGOPEN function library_ CURVE_ create_ point(double point_coords [3],tag_T*point) function can be used to create points, where point_Coords [3] respectively represents the three coordinates of a point_ T * point represents the identifier of a point. Through the while statement, all points can be quickly created in UG software platform, and the data conversion from MATLAB platform to UG platform is completed.

Fitting of gear profile
When fitting the data of gear profile, the starting point and end point of the profile should be selected. Here, the point selection function in function library is used, which is UF_ UI_ select_ point_ collection(char * message, logical coincident_ points, UF_ UI_ chained_ points_ P_ T * points, int * count, int * response); char * message is the prompt to define the point selector, logical coincidence_ Points is to judge whether the consistency point needs to be returned. The default value here is false, UF_ UI_ chained_ points_ P_ T * points is a structure containing an array, and UF must be used finally_ Free release, int * count is the number of returned start and end points, int * response is the return value of confirm, cancel, return, etc., set to 0 As shown in Fig.5, it is the result of contour fitting. The basic idea of fitting outer contour is to use non-uniform rational B-spline curve to fit the data points of the profile. Cubic rational B-spline has the advantages of high degree of approximation, strong adaptability, simple calculation and easy control. The function used to realize the fitting is UF_ MODL_ create_ fitted_ Spline, then UF_ MODL_ ask_ curve_ Closed function is used to check the closure of the fitted profile to prevent it from becoming a sheet in the later drawing process. In order to ensure the smoothness of the curve, UF is needed_ CURVE_ smooth_ spline_ Data smoothes the fitted curve once, and stretches based on it. The function used in stretching is UF_MODL_create_extruded.

Internal profile fitting
The cubic rational B-spline curve can also be used to fit the internal profile. Considering that the gear inner hole is a circle, the spline curve fitting directly will produce a large error. Because the least square circle fitting method is a statistical detection method, that is, the circular target in the image is edge missing due to the influence of uneven light intensity It will affect the center location and radius detection. If the edge positioning is accurate and the contour is clear, the least square method can realize the sub-pixel level of accurate fitting positioning [6] , especially for the processing of circle fitting in image recognition has a very obvious advantage. Fitting idea: first of all, the point selection function is used to select all the points to be fitted, and the center of the circle center is calculated by the least square method_ x、 center_ Y and radius of the circle, combined with UF_ CURVE_ create_ Arc function creates the contour of the inner hole circle, and finally extrudes the entity stretched out from the outer section contour to complete the reconstruction of the model. As shown in Fig.6 is the result of reverse modeling.

Sample analysis
After the above operations, the 3D geometric model of the part can be obtained, but the model cannot accurately reflect the size of the original model. Therefore, the image is analyzed according to the method in Section 2.3 of this paper. The standard block used in this paper is 5 * 5 * 10 mm, the actual size of the gear is 22.5 mm, the inner diameter is 10 mm, the thickness is 8 mm, and the 13 teeth are 1.5 molds. After edge detection, the data points in Fig.3 are imported into UG, and the height of the standard block image is 158mm and the gear image height is 127mm, therefore, the actual height of the gear is calculated to be about 8.038 mm. The width of the standard block model is 109mm, and the scale is 109 / 5, which is 21.8. As shown in Fig.7, the dimension after reduction is compared with that of direct fitting. After measurement, the internal control of the gear is 9.8249mm directly and 8.038mm behind the tooth. The relative error is less than 5% compared with the actual size of the gear, which is still within the acceptable range.

Conclusion
To sum up, this paper combines the MATLAB and UG software platforms, combines the image recognition process with the secondary development of UG, and provides a method to quickly measure some simple unknown dimensions and physical model dimensions.
With the powerful analysis function of UG platform, it can also calculate the parameters such as volume and quality, and obviously improve improves the efficiency of reverse modeling technology.
Due to the accuracy of shooting process, it is impossible to measure complex parts. However, the method described in this paper is still representative, which can provide a feasible way for the generation of CAD model in additive manufacturing technology, which can be used as an effective reference for the conversion of 2D images into 3D CAD models.