A method for polyline coordinate extraction and associated text matching based on DXF files
By combining DXF file parsing and Euclidean distance matching algorithms, the coordinates of polylines are automatically extracted and associated text is accurately matched, solving the problems of low efficiency and poor accuracy in DXF file processing, and realizing structured storage and efficient data processing of polyline data.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- POWERCHINA ZHONGNAN ENG
- Filing Date
- 2026-07-06
- Publication Date
- 2026-07-31
AI Technical Summary
Existing methods for extracting polyline coordinates and matching associated text in DXF files rely on manual operation, which is inefficient and inaccurate. Furthermore, existing tools have low matching accuracy and lack structured attributes, making them unsuitable for direct integration with subsequent engineering data analysis systems.
By combining DXF file parsing technology with bounding box algorithms and Euclidean distance matching algorithms, polyline coordinates are automatically extracted and associated text is accurately matched. Through bounding box center point calculation and Euclidean distance filtering rules, structured storage of polyline data is achieved.
It enables automated processing of polyline data, improving processing efficiency and accuracy, reducing labor costs, is compatible with mainstream CAD software, supports structured data output, and is suitable for engineering data analysis and modeling.
Smart Images

Figure CN122490126A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer data processing technology, and in particular to a method for extracting polyline coordinates and matching associated text based on DXF files. Background Technology
[0002] With the digital development of engineering design, CAD software has become a core design tool in fields such as road engineering, municipal engineering, and building engineering. DXF, short for Drawing Exchange Format, is a common data exchange format for CAD software. Due to its strong compatibility and ease of transmission, it is widely used in the interaction and storage of engineering design documents. In DXF files, polylines are the core entities describing road outlines, pipeline routes, platform boundaries, and building outlines. Their vertex coordinate information is the basic data for engineering data processing, on-site construction, and BIM modeling. The text labels corresponding to polylines, such as road names, pipeline numbers, and point names, are key attributes for data classification and association.
[0003] Because polyline data processing in DXF files involves multiple stages such as file parsing, entity filtering, spatial matching, and data structuring, there is still significant room for research and application in automating polyline coordinate extraction and associated text matching. Currently, the industry primarily relies on manual operation for polyline coordinate extraction and associated text matching in DXF files. Designers must open CAD software, query the coordinates of each polyline one by one, manually record the X, Y, and Z 3D coordinates of each vertex, and manually match the corresponding TEXT / MTEXT text annotations of the polylines through visual judgment. Existing CAD data extraction tools can only perform simple batch extraction of polyline coordinates, lacking automatic text association functionality. Some tools that support text matching use coarse region matching rules without setting distance thresholds for filtering, easily associating irrelevant text incorrectly with polylines, resulting in low matching accuracy. Furthermore, the extracted coordinate data is mostly in plain text format, lacking standardized structured attributes, making it impossible to directly interface with subsequent engineering data analysis systems. Summary of the Invention
[0004] To address the technical challenges of low efficiency, poor accuracy, and insufficient compatibility in existing DXF file polyline coordinate extraction methods, this invention provides a method for polyline coordinate extraction and associated text matching based on DXF files. By combining DXF file parsing technology with bounding box algorithms and Euclidean distance matching algorithms, this method is used for the automated extraction of polyline coordinates and accurate matching of associated text annotations in DXF files in engineering design. This significantly improves the efficiency of DXF file data processing, ensures the accuracy of coordinate extraction and text matching, and simultaneously achieves structured storage of polyline data, reducing the manual cost of engineering data processing. To achieve the above objectives, this invention is implemented through the following technical solutions: A method for polyline coordinate extraction and associated text matching based on DXF files includes the following steps: Step S1: Read and parse the DXF file to be processed, traverse and filter out all polyline entities and text entities, extract the insertion point coordinates and text content of the text entities and store them, and extract and store the vertex coordinate list of the polyline entities. Step S2: Perform null value detection on the extracted vertex coordinate list. If it is null, skip it; if it is not null, calculate the coordinates of the bounding box center point of each polyline entity using the bounding box center point formula. Step S3: Calculate the spatial distance between the insertion point coordinates of each text entity and the center point coordinates of each bounding box based on the Euclidean distance formula, and match the unique associated text annotations of each polyline entity using threshold filtering rules; Step S4: Perform structured processing on each polyline entity, obtain the ID, handle attribute identifier, point number, and vertex coordinates of each polyline entity, and integrate the ID, handle attribute, unique associated text label, point number, and vertex coordinates of each polyline entity into an independent structured data record. Step S5: Summarize the structured data records of all polyline entities and save them to an Excel file in a standardized format; Step S6: Output the generated Excel file, perform statistical analysis on the Excel file to obtain the total number of polyline entities and the total number of extracted vertex coordinates, summarize the processing results, and issue warnings for output anomalies.
[0005] Preferably, the implementation of step S1 includes: Step S11: Read and parse the DXF file to be processed using the DXF file parsing library to obtain all entities in the DXF file model space; Step S12: Traverse all entities and filter out all polyline entities and text entities; Step S13: Read the native insert attribute of each text entity to obtain the insertion point coordinates, read the text attribute of each text entity to obtain the text content, and store the insertion point coordinates and text content respectively. Step S14: Call the get_points() method to extract the vertex coordinates of each polyline entity, process the list of all vertex coordinates, and store it as a vertex coordinate list.
[0006] Preferably, the polyline entity includes LWPOLYLINE lightweight polyline and POLYLINE3D polyline, and the text entity includes TEXT single-line text and MTEXT multi-line text.
[0007] Preferably, the implementation of step S2 includes: Step S21: Perform null value detection on the extracted vertex coordinate list. If the vertex coordinate list is null, skip the polyline entity. If the vertex coordinate list is not null, extract the X and Y coordinate values of all vertices and form X coordinate set and Y coordinate set respectively. Step S22: Calculate the minimum value min of the X-coordinate set. x and maximum value max x and the minimum value of the set of Y coordinates. y and maximum value max y Substituting the values into the bounding box center point formula, calculate the coordinates of the bounding box center point for each polyline entity:
[0008] In the formula, center x center y These are the X and Y coordinates of the center point of the bounding box of the polyline entity.
[0009] Preferably, the implementation of step S3 includes: Step S31: Extract the insertion point coordinates and text content of the stored text entities, as well as the coordinates of the bounding box center point; Step S32: Calculate the spatial distance d between the center point coordinates of the bounding box of each polyline entity and the insertion point coordinates of each text entity using the Euclidean distance formula;
[0010] In the formula, d is the spatial distance between the text entity insertion point coordinates and the center point coordinates of the bounding box; center x center y The coordinates of the center point of the bounding box; text x text y The coordinates of the insertion point for the text entity; Step S33: Iterate through and calculate all spatial distances d, filter out text entities with spatial distances d ≤ distance threshold, minimum spatial distance d and non-empty text content, and use the text content of the text entity as the unique associated text label of the polyline entity.
[0011] Preferably, the distance threshold is 200 length units; the method for identifying that the text content is not empty is to determine whether the length of the text attribute value of the text entity is greater than 0.
[0012] Preferably, the implementation of step S4 includes: Step S41: Assign a unique and continuous polyline entity ID to each polyline entity in traversal order; extract the native handle attribute identifier of each polyline entity; traverse all vertex coordinates of each polyline entity and generate point numbers starting from 1 in the order of vertex coordinates. Step S42: Extract the X, Y, and Z coordinates of each vertex and round them to 3 decimal places; if it is a two-dimensional polyline entity, assign the Z coordinate 0.0 and round it to 3 decimal places. Step S43: Integrate the polyline entity ID, handle attribute identifier, unique associated text label, point number, X coordinate, Y coordinate, and Z coordinate of each polyline entity into an independent structured data record.
[0013] Preferably, step S41 further includes: Step S44: If the polyline entity has no handle attribute identifier, mark the currently read polyline entity as having no handle attribute identifier, and adjust the handle attribute identifier in step S43 to a null placeholder.
[0014] Preferably, the implementation of step S5 includes: Step S51: Summarize the structured data records of all polyline entities to form a unified polyline entity coordinate dataset; Step S52: Convert the polyline entity coordinate dataset into a structured data table, and save it as an Excel file with the same name as the original DXF file and the .xlsx extension to the directory where the original DXF file is located, without including the row index column when saving.
[0015] Preferably, the column names of the structured data table are as follows: polyline ID, handle, polyline name, point number, X coordinate, Y coordinate, and Z coordinate.
[0016] The present invention has the following advantages and beneficial effects compared with the prior art: This invention automates the manual process of extracting polyline coordinates and matching text in DXF files. It combines bounding box algorithms and Euclidean distance threshold filtering rules to achieve accurate text labeling and constructs a standardized structured data output format. This significantly reduces the workload and error rate of manual operations, improves the efficiency of DXF file data processing, and enables structured storage of polyline data, adapting to subsequent engineering data analysis, modeling, and other application needs. It is also compatible with DXF files generated by mainstream CAD software, supporting LWPOLYLINE / POLYLINE polyline entities and TEXT / MTEXT text entities, demonstrating strong versatility. Furthermore, the user interface is user-friendly, requiring no professional programming or advanced CAD skills, allowing engineering designers to use it directly. Attached Figure Description
[0017] Figure 1 This is a flowchart illustrating the execution process of the method of the present invention. Detailed Implementation
[0018] The present invention will now be further described with reference to the accompanying drawings and specific embodiments: To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of the embodiments of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0019] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. The terminology used herein in the description of the invention is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention.
[0020] Example 1: like Figure 1 As shown, this invention discloses a method for polyline coordinate extraction and associated text matching based on DXF files. The method includes the following steps: Step S1: Read and parse the DXF file to be processed, traverse and filter out all polyline entities and text entities, extract the insertion point coordinates and text content of the text entities and store them, extract and store the vertex coordinate list of the polyline entities, and output error information immediately if an error occurs during file parsing.
[0021] To enable technicians to better understand the specific implementation methods of the above steps, this step specifically includes: Step S11: Read and parse the DXF file to be processed using the DXF file parsing library to obtain all entities in the DXF file model space; Step S12: Traverse all entities and filter out all polyline entities and text entities; the polyline entities here include LWPOLYLINE lightweight polyline and POLYLINE3D polyline, and the text entities include TEXT single-line text and MTEXT multi-line text.
[0022] Step S13: Read the native insert attribute of each text entity to obtain the insertion point coordinates [textx, texty], read the text attribute of each text entity to obtain the text content, and store the insertion point coordinates [textx, texty] and the text content respectively; Step S14: Call the `get_points()` method to extract the vertex coordinates of each polyline entity, process the list of all vertex coordinates, and store it as a vertex coordinate list. It is known that `get_points()` is an AutoCAD-embedded AutoLISP programming language function that pauses program execution, allowing the user to input the X and Y coordinates or X, Y, and Z coordinates of a point. Therefore, `get_points()` here is a parameterless method that can directly extract the vertex coordinate list from the current polyline entity's DXF data without relying on additional parameters; hence, the parentheses should be left blank.
[0023] Step S2: Perform null value detection on the extracted vertex coordinate list. If a value is null, skip it; if a value is not null, calculate the bounding box center point coordinates of each polyline entity using the bounding box center point formula. The specific implementation of this step includes: Step S21: Perform null value detection on the extracted vertex coordinate list. If the vertex coordinate list is null, it means that the polyline entity has no valid vertex data, so skip the polyline entity. If the vertex coordinate list is not null, extract the X coordinate value and Y coordinate value of all vertex coordinates and form the X coordinate set and Y coordinate set respectively. Step S22: Calculate the minimum value min of the X-coordinate set. x and maximum value max x and the minimum value of the set of Y coordinates. y and maximum value max y Substituting the values into the bounding box center point formula, calculate the coordinates of the bounding box center point for each polyline entity:
[0024] In the formula, center x center yThese are the X and Y coordinates of the center point of the bounding box of the polyline entity.
[0025] Step S3: Calculate the spatial distance between the insertion point coordinates of each text entity and the center point coordinates of each bounding box based on the Euclidean distance formula, and match the unique associated text annotations for each polyline entity using threshold filtering rules. For better understanding, the specific implementation of step S3 includes: Step S31: Extract the insertion point coordinates and text content of the stored text entities, as well as the coordinates of the bounding box center point; Step S32: The Euclidean distance formula is defined as the square root of the sum of the squares of the coordinate differences between two points; that is, the Euclidean distance formula is used to calculate the spatial distance d between the center point coordinates of the bounding box of each polyline entity and the insertion point coordinates of each text entity.
[0026] In the formula, d is the spatial distance between the text entity insertion point coordinates and the center point coordinates of the bounding box; center x center y The coordinates of the center point of the bounding box; text x text y The coordinates of the insertion point for the text entity; Step S33: Initialize the minimum distance to positive infinity, and default associated text to "unassociated text". As an initialization step, positive infinity ensures that the first comparison will always have a smaller value; unassociated text prevents immediate return of an empty value when the match fails, ensuring logical completeness. Further, by traversing and calculating all spatial distances d, text entities with spatial distances d ≤ distance threshold, the smallest spatial distance d, and non-empty text content are selected. The text content of these text entities is used as the unique associated text label for the polyline entity. Preferably, the distance threshold is 200 length units, and the method for determining whether the text content is non-empty is by judging whether the length of the text attribute value of the text entity is greater than 0. When the length of the text attribute value of the text entity is greater than 0, it is determined to be in a non-empty state.
[0027] Step S4: Structure each polyline entity by obtaining its ID, handle attribute identifier, point number, and vertex coordinates. Then, integrate the ID, handle attribute, unique associated text label, point number, and vertex coordinates of each polyline entity into a single, independent structured data record. In other words, this step specifically includes: Step S41: Assign a unique and continuous polyline entity ID to each polyline entity in traversal order; extract the native handle attribute identifier of each polyline entity; traverse all vertex coordinates of each polyline entity and generate point numbers starting from 1 in the order of vertex coordinates. Step S42: Extract the X, Y, and Z coordinates of each vertex and round them to 3 decimal places. Note that if it is a two-dimensional polyline entity, the Z coordinate is assigned 0.0 and rounded to 3 decimal places. Step S43: Integrate the ID, handle attribute identifier, unique associated text label, point number, X coordinate, Y coordinate, and Z coordinate of each polyline entity into an independent structured data record.
[0028] Step S41 further includes: In step S44, if the polyline entity has no handle attribute identifier, then the currently read polyline entity is marked as having no handle attribute identifier, and the handle attribute identifier in step S43 is adjusted to a null placeholder. That is, if the polyline entity has a handle attribute identifier, it is used directly; if it does not have a handle attribute identifier, it is marked as having no handle attribute identifier, and the control placeholder is achieved by marking it as having no handle attribute identifier.
[0029] Step S5: Summarize the structured data records of all polyline entities and save them to an Excel file in a standardized format; specifically, to more clearly illustrate this step, it includes: Step S51: Summarize the structured data records of all polyline entities to form a unified polyline entity coordinate dataset; each record here contains information such as vertex coordinates, polyline ID, handle, and associated text.
[0030] Step S52: Convert the polyline entity coordinate dataset into a structured data table, and save it as an Excel file with the same name as the original DXF file but with the .xlsx extension to the same directory as the original DXF file. Do not include the row index column when saving to ensure data structure and readability. The column names of the structured data table are as follows: Polyline ID, Handle, Polyline Name, Point Number, X Coordinate, Y Coordinate, and Z Coordinate.
[0031] Step S6: Output the generated Excel file. Perform statistical parsing on the Excel file to obtain the total number of polyline entities and the total number of extracted vertex coordinates. Summarize the processing results and issue warnings for output anomalies. During the output process, the processing status can be fed back twice via a graphical interface pop-up window, indicating that the DXF file processing is complete and the coordinate information has been saved, respectively. The pop-up window will also display the specific save path of the Excel file. If file reading failure, entity parsing error, or data saving anomaly occurs during processing, the anomaly information will be captured immediately, a detailed error log will be output to the console, and an error warning will be issued through a graphical interface pop-up window, indicating the cause of the anomaly to the user.
[0032] Based on the aforementioned technologies, the core approach combines DXF file parsing technology with spatial algorithms to transform polyline coordinate extraction and text association matching into an automated data analysis process. The goal is to achieve rapid extraction, accurate matching, and structured storage of polyline data while meeting engineering data accuracy requirements, strictly adhering to DXF file entity attribute specifications and engineering data accuracy requirements. This automation process has become a key technological link in improving engineering design data processing efficiency, reducing labor costs, and ensuring data accuracy, and it has significant practical implications for promoting the digitalization and intelligentization of the engineering design field.
[0033] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A DXF file-based multi-segment line coordinate extraction and associated text matching method, characterized in that, Includes the following steps: Step S1: Read and parse the DXF file to be processed, traverse and filter out all polyline entities and text entities, extract the insertion point coordinates and text content of the text entities and store them, and extract and store the vertex coordinate list of the polyline entities. Step S2: Perform null value detection on the extracted vertex coordinate list. If it is null, skip it; if it is not null, calculate the coordinates of the bounding box center point of each polyline entity using the bounding box center point formula. Step S3: Calculate the spatial distance between the insertion point coordinates of each text entity and the center point coordinates of each bounding box based on the Euclidean distance formula, and match the unique associated text annotations of each polyline entity using threshold filtering rules; Step S4: Perform structured processing on each polyline entity, obtain the ID, handle attribute identifier, point number, and vertex coordinates of each polyline entity, and integrate the ID, handle attribute, unique associated text label, point number, and vertex coordinates of each polyline entity into an independent structured data record. Step S5: Summarize the structured data records of all polyline entities and save them to an Excel file in a standardized format; Step S6: Output the generated Excel file, perform statistical analysis on the Excel file to obtain the total number of polyline entities and the total number of extracted vertex coordinates, summarize the processing results, and issue warnings for output anomalies.
2. The method of claim 1, wherein, The implementation of step S1 includes: Step S11: Read and parse the DXF file to be processed using the DXF file parsing library to obtain all entities in the DXF file model space; Step S12: Traverse all entities and filter out all polyline entities and text entities; Step S13: Read the native insert attribute of each text entity to obtain the insertion point coordinates, read the text attribute of each text entity to obtain the text content, and store the insertion point coordinates and text content respectively. Step S14: Call the get_points() method to extract the vertex coordinates of each polyline entity, process the list of all vertex coordinates, and store it as a vertex coordinate list.
3. The method of claim 2, wherein the method further comprises: The polyline entities include LWPOLYLINE lightweight polyline and POLYLINE3D polyline, and the text entities include TEXT single-line text and MTEXT multi-line text.
4. The method of claim 1, wherein the method is based on a DXF file. The implementation of step S2 includes: Step S21: Perform null value detection on the extracted vertex coordinate list. If the vertex coordinate list is null, skip the polyline entity. If the vertex coordinate list is not null, extract the X and Y coordinate values of all vertices and form X coordinate set and Y coordinate set respectively. Step S22, calculate the minimum value min of the X coordinate set x and the maximum value max x , and the minimum value min of the Y coordinate set y and the maximum value max y , substitute the bounding box center point formula, calculate the bounding box center point coordinates of each polyline entity: In the formula, center x , center y are the X, Y coordinates of the center point of the bounding box of the polyline entity, respectively.
5. The method of claim 1, wherein, The implementation of step S3 includes: Step S31: Extract the insertion point coordinates and text content of the stored text entities, as well as the coordinates of the bounding box center point; Step S32: Calculate the spatial distance d between the center point coordinates of the bounding box of each polyline entity and the insertion point coordinates of each text entity using the Euclidean distance formula; In the formula, d is the spatial distance between the text entity insertion point and the center point of the bounding box; center x center y The coordinates of the center point of the bounding box; text x text y The coordinates of the insertion point for the text entity; Step S33: Iterate through and calculate all spatial distances d, filter out text entities with spatial distances d ≤ distance threshold, minimum spatial distance d and non-empty text content, and use the text content of the text entity as the unique associated text label of the polyline entity.
6. The method of claim 5, wherein the method further comprises: The distance threshold is 200 length units; The method for determining whether the text content is not empty is to determine whether the length of the text attribute value of the text entity is greater than 0.
7. The method of claim 1, wherein the method is based on a DXF file. The implementation of step S4 includes: Step S41: Assign a unique and continuous polyline entity ID to each polyline entity in traversal order; extract the native handle attribute identifier of each polyline entity; traverse all vertex coordinates of each polyline entity and generate point numbers starting from 1 in the order of vertex coordinates. Step S42: Extract the X, Y, and Z coordinates of each vertex and round them to 3 decimal places; if it is a two-dimensional polyline entity, assign the Z coordinate 0.0 and round it to 3 decimal places. Step S43: Integrate the polyline entity ID, handle attribute identifier, unique associated text label, point number, X coordinate, Y coordinate, and Z coordinate of each polyline entity into an independent structured data record.
8. The method for polyline coordinate extraction and associated text matching based on DXF files according to claim 7, characterized in that, Step S41 further includes: Step S44: If the polyline entity has no handle attribute identifier, mark the currently read polyline entity as having no handle attribute identifier, and adjust the handle attribute identifier in step S43 to a null placeholder.
9. The method of claim 1, wherein, The implementation of step S5 includes: Step S51: Summarize the structured data records of all polyline entities to form a unified polyline entity coordinate dataset; Step S52: Convert the polyline entity coordinate dataset into a structured data table, and save it as an Excel file with the same name as the original DXF file and the .xlsx extension to the directory where the original DXF file is located, without including the row index column when saving.
10. The method of claim 9, wherein the method further comprises: The column names of the structured data table are as follows: polyline ID, handle, polyline name, point number, X coordinate, Y coordinate, and Z coordinate.