A material information recognition and statistics method and device based on CAD export, electronic equipment, medium and program product
By parsing CAD drawings and filtering target layers based on rules, combining dual scripts to extract text and cell boundaries, establishing row and column index mapping, and introducing a deep table structure recognition model to complete missing boundary lines, the problem of locating table objects in CAD drawings was solved, achieving efficient and accurate material information statistics.
Patent Information
- Application Number
- CN202511197552.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-26
- Publication Date
- 2025-11-07
- Estimated Expiration
- 2045-08-26
AI Technical Summary
Existing technologies struggle to reliably position table objects in drawings exported from CAD, especially in multi-layered environments where establishing reliable mappings of text cells is difficult. This results in low efficiency and high error rates in reconstructing material lists, making it impossible to generate usable structured Excel files.
By parsing DWG format engineering drawings, the target layer is filtered based on preset layer name matching rules. Text and cell boundaries are extracted using dual scripts, row and column index mapping is established, and a deep table structure recognition model is introduced to complete missing boundary lines, generating a formatted Excel file.
It enables efficient, accurate, and structured output of material information, significantly improving statistical efficiency and reducing human error rates.
Smart Images

Figure CN120764487B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application belongs to the technical field of data processing, and particularly relates to a material information recognition and statistics method and device based on CAD export, an electronic device, a medium and a program product. BACKGROUND
[0002] The engineering drawing material table is often embedded in the DWG drawing exported by CAD. The traditional statistics of the material table mostly rely on manual work or simple scripts / OCR, and it is difficult to stably locate the table object in multiple layers. The sources are disorderly. In some existing technologies, even if the text and boundary coordinates are obtained, it is also difficult to establish a reliable mapping of the text cells. In addition, when the table boundary line is missing or the coordinates are noisy, the table structure cannot be stably reconstructed for statistics. The above problems lead to the difficulty in effectively reconstructing the material table from the drawing exported by CAD and directly generating a usable structured Excel, resulting in low efficiency and high error rate. SUMMARY
[0003] To solve the above technical problems, the application provides a material information recognition and statistics method and device based on CAD export, an electronic device, a medium and a computer program product.
[0004] The application provides a material information recognition and statistics method based on CAD export, comprising the following method steps:
[0005] Step S101, parse the DWG format engineering drawing to obtain all layer sets in the drawing, traverse the layer sets based on a preset layer name matching rule to filter a target layer, identify a table object from the target layer, and record the number of tables and the global coordinate range of the tables for the table object, wherein the table object contains material information;
[0006] Step S102, for each identified table object, extract text elements and their spatial coordinate information in the table by a first script to generate a text data file, and extract cell boundary coordinates by a second script to generate a cell coordinate file;
[0007] Step S103, pair the text data file and the cell coordinate file based on the file name prefix and the table ID to obtain a paired file, and convert the cell boundary coordinates in the table into ordered row and column indexes based on the paired file;
[0008] Step S104, extract text data and cell data from the paired file, wherein the text data includes each text element and its spatial coordinate, and the cell data includes cell boundary coordinates and their corresponding row and column indexes, establish the association between the text and the cell based on the coordinates of the text elements and the boundary coordinates of the cells, and form the mapping of the row and column indexes of the cells and the text;
[0009] In step S105, based on the mapping between the cell row and column indexes and the text, the text coordinates are double-ordered, the ordered text is spliced to obtain the complete text content of the corresponding cell.
[0010] Another aspect of the present application also provides a device for identifying and counting material information based on CAD export, comprising:
[0011] A table analysis and identification module is configured to analyze a DWG format engineering drawing to obtain all layer sets in the drawing, filter a target layer based on a preset layer name matching rule, identify a table object from the target layer, and record the number of tables and the global coordinate range of the tables, wherein the table object contains material information.
[0012] A text and cell extraction module is configured to, for each identified table object, extract text elements and their spatial coordinate information in the table by a first script to generate a text data file, and extract cell boundary coordinates by a second script to generate a cell coordinate file.
[0013] A row and column index conversion module is configured to pair the text data file and the cell coordinate file based on a file name prefix and a table ID to obtain a paired file, and convert the cell boundary coordinates in the table into ordered row and column indexes based on the paired file.
[0014] A row and column index and text mapping module is configured to extract text data and cell data from the paired file, wherein the text data includes each text element and its spatial coordinate, the cell data includes cell boundary coordinates and their corresponding row and column indexes, the association between the text elements and the cells is established based on the coordinates of the text elements and the boundary coordinates of the cells to form a mapping between the row and column indexes of the cells and the text.
[0015] A cell text splicing module is configured to, based on the mapping between the row and column indexes of the cells and the text, double-order the text coordinates, and splice the ordered text to obtain the complete text content of the corresponding cell.
[0016] Another aspect of the present application also provides an electronic device, comprising at least one processor, and a memory connected with the at least one processor in communication; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the above-mentioned method for identifying and counting material information based on CAD export.
[0017] In another aspect of the present application, a computer readable storage medium is provided, which stores computer program instructions executable by a processor to implement the CAD-derived material information recognition and statistics method as described above.
[0018] In another aspect of the present application, a computer program product is provided, which comprises a computer program executable by a processor to implement the CAD-derived material information recognition and statistics method as described above.
[0019] The present application realizes automatic recognition and positioning of material tables by analyzing CAD drawings and screening target layers based on rules, extracts text and establishes row and column index mapping based on double-script, ensures accurate material information positioning, introduces a deep table structure recognition model to automatically detect and complete missing boundary lines, effectively deals with incomplete table structure and drawing errors, finally generates a formatted Excel file, realizes efficient, accurate and structured output of material information, and significantly improves statistical efficiency and reduces manual operation error rate. BRIEF DESCRIPTION OF DRAWINGS
[0020] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings needed to be used in the embodiments. It should be understood that the following drawings only show some embodiments of the present application, and therefore should not be regarded as a limitation on the scope. For those skilled in the art, other related drawings can also be obtained without creative labor.
[0021] Other features, objects and advantages of the present application will become more apparent through reading the detailed description of the non-limiting embodiments made by referring to the following drawings:
[0022] Figure 1 A CAD-derived material information recognition and statistics method flowchart is provided for the embodiments of the present application.
[0023] Figure 2 A row and column index conversion flowchart is provided for the embodiments of the present application.
[0024] Figure 3 A deep table structure recognition network model architecture diagram is provided for the embodiments of the present application.
[0025] Figure 4 A CAD-derived material information recognition and statistics device structure diagram is provided for the embodiments of the present application.
[0026] Figure 5 An electronic device structure diagram is provided for the embodiments of the present application. DETAILED DESCRIPTION
[0027] In order to make the purposes, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are some but not all of the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by a person of ordinary skill in the art without creative work fall within the protection scope of the present application.
[0028] The present application proposes a material information recognition and statistical method based on CAD export. The technical solutions of the present application will be described in detail below in combination with various embodiments.
[0029] As shown in the drawings, Figure 1 The embodiments of the present application disclose a material information recognition and statistical method based on CAD export 100, comprising the following method steps:
[0030] Step S101, parse the DWG format engineering drawing to obtain all layer sets in the drawing, traverse the layer sets based on a preset layer name matching rule to filter target layers, identify table objects from the target layers, record the number of tables and the global coordinate range of the tables for the table objects, wherein the tables are tables containing material information.
[0031] In some embodiments, the API interface (such as the ActiveX interface of AutoCAD) provided by the CAD software is used to load and parse the DWG file, establish an interactive channel with the drawing, and extract the set of all layers in the drawing through the interface, which contains the name, visibility, color and other attribute information of the layers. For example, an architectural engineering drawing may contain multiple layers such as "outline", "annotation", "standard drawing", "title bar", etc., each layer carrying different types of graphic data.
[0032] Then, according to the preset layer name matching rule, the layer set is traversed for name comparison. Exemplarily, the specific logic is that, for each layer, the name of the layer is obtained, and if the name completely matches the preset target layer name (such as "standard drawing", wherein the standard drawing contains the target table, such as the material table) or meets the fuzzy matching rule (such as containing the keyword "material table"), the layer is marked as the target layer. After the screening is completed, only the target layers are retained for subsequent processing, and irrelevant layers (such as "axis" and "annotation" layers) are excluded to reduce data interference.
[0033] Next, from the target layer selected, all entities contained in the target layer are traversed, and the table object is identified by the entity type attribute (EntityType). In CAD, a table object usually corresponds to a specific type identifier (for example, AcDbTable), so the table object can be filtered by judging EntityType == "AcDbTable".
[0034] Finally, the total number of identified table objects is counted, and for each table object, the global coordinate range is extracted from its geometry attribute. For example, the left lower corner and the right upper corner coordinates of the table boundary are obtained (usually through the GetBoundingBox method), which are used to locate the spatial position of the table in the drawing. For example, the global coordinate range of a table is recorded as bottomLeft=(100, 200, 0), topRight=(500, 400, 0), indicating the coverage area of the table in the two-dimensional plane.
[0035] In step S102, for each identified table object, the first script extracts the text elements and their spatial coordinate information in the table to generate a text data file; and the second script extracts the cell boundary coordinates to generate a cell coordinate file.
[0036] In some embodiments, after identifying the table object in step S101, data extraction is performed for each table, and two special scripts are used to extract text content and cell boundary information, respectively. Based on the number of tables recorded in step S101 and the global coordinate range, each table object is processed in order according to the table ID (such as the unique handle Handle of the CAD entity). An independent processing context is created for each table object, including the table ID, global coordinate range, and other metadata, which are used for subsequent file naming and association. The script execution environment is initialized, the basic library required for script running is loaded through the LISP interpreter of the CAD software (such as the vl-load-com function of AutoCAD), and the script can call the entity operation interface of CAD.
[0037] Specifically, the first script is loaded, and the unique identifier [TableID] of the current table is passed in. The script locates the table object by TableID, and traverses all text entities in the table (including AcDbText single-line text and AcDbMText multi-line text). For each text entity, key information is extracted. For example, the text content Content: the text content (such as "bolt" "M10x50") is obtained through the TextString attribute; the text spatial coordinates X / Y / Z: the three-dimensional coordinates of the text insertion point (i.e., the spatial position of the text) are obtained through the InsertionPoint attribute.
[0038] The extracted information is organized in JSON array format, with each record containing "Content", "X", "Y", "Z" fields. The file is saved with the naming rule [filename]_[TableID]_TableText.json (e.g., "Engineering Drawing A_12345_TableText.json") and uses an incremental writing mechanism (writing once every 10 extracted text data) to avoid memory overflow.
[0039] In this embodiment, a second script is loaded, with the [TableID] of the current table passed in. The script locates the table by TableID, identifies the intersection of horizontal and vertical lines of the table, and determines the original boundaries of each cell. The standardized boundary coordinates of each cell are recorded, including, for example, bottomLeft (x, y values of the lower left corner) and topRight (x, y values of the upper right corner).
[0040] Similarly, the extracted boundary information is organized in JSON format, with each record containing "bottomLeft" and "topRight" fields, each field containing "x" and "y" coordinate values. The file is saved with the naming rule [filename]_[TableID]_RepairedTableCells.json (e.g., "Engineering Drawing A_12345_RepairedTableCells.json"), and also uses an incremental writing mechanism to ensure data safety.
[0041] Optionally, basic verification is performed on the two generated JSON files, specifically, checking whether the file is empty, and whether the key fields (such as the "X" coordinate of the text and the "bottomLeft" of the cell) are complete. If there is file damage or field missing, an error log is recorded (such as "Text file for Table ID=67890 is missing Y coordinate field"), and the script is triggered to re-execute.
[0042] In step S103, the text data file and the cell coordinate file are paired based on the file name prefix and the table ID to obtain a paired file, and the cell boundary coordinates in the table are converted into ordered row and column indexes based on the paired file.
[0043] In some embodiments, in this step, the association between text and cells is established through file pairing, and the discrete boundary coordinates are converted into ordered row and column indexes, providing a structured reference for subsequent content integration.
[0044] Specifically, scan all JSON files under the storage directory, extract the name prefix (original DWG file name) and table ID of each file (e.g., parse the prefix "Engineering Drawing A" and table ID "12345" from "Engineering Drawing A_12345_TableText.json"). Group the literal data file (TableText.json) and the cell coordinate file (RepairedTableCells.json) that have the same prefix and table ID. For example, "Engineering Drawing A_12345_TableText.json" and "Engineering Drawing A_12345_RepairedTableCells.json" form a paired file.
[0045] Optionally, record the pairing relationship in the form of a dictionary, with the table ID as the key and the paths of the two files as the value (e.g., {"12345": ["path / text.json", "path / cell.json"]}).
[0046] In some embodiments, based on the paired files, convert the cell boundary coordinates in the table to an ordered row-column index, which specifically includes:
[0047] Extract the x and y coordinates of the lower left corner and the upper right corner of all cells from the cell coordinate file to generate XCoords set and YCoords set, where XCoords set contains the x values of the lower left corner and the upper right corner of all cells, and YCoords set contains the y values of the lower left corner and the upper right corner of all cells.
[0048] Sort XCoords set in ascending order and remove duplicates, calculate the difference between adjacent coordinates, and merge them into the same coordinate when the difference is ≤0.5 coordinate units to form the column boundary set ColBounds.
[0049] Sort YCoords set in descending order and remove duplicates, calculate the difference between adjacent coordinates, and merge them into the same coordinate when the difference is ≤0.5 coordinate units to form the row boundary set RowBounds.
[0050] Establish a mapping from coordinates to row-column indices. For x coordinates, find the two adjacent column boundary intervals in ColBounds that they fall into, and the corresponding column index is the index of the previous boundary in ColBounds. For y coordinates, find the two adjacent row boundary intervals in RowBounds that they fall into, and the corresponding row index is the index of the previous boundary in RowBounds.
[0051] Specifically, as shown in Figure 2 Step S201, the cell boundary coordinate set is extracted, and exemplarily, from the loaded cell coordinate file (RepairedTableCells.json), all cell objects are traversed, the x and y coordinates of the bottomLeft (lower left corner) and topRight (upper right corner) of each cell are extracted, and two basic coordinate sets are generated:
[0052] XCoords set: contains the x values of the lower left corners and the x values of the upper right corners of all cells, i.e. XCoords = {cell['bottomLeft']['x'], cell['topRight']['x'] for cell in cell list}. This set reflects all possible column boundary horizontal coordinates in the table.
[0053] YCoords set: contains the y values of the lower left corners and the y values of the upper right corners of all cells, i.e. YCoords = {cell['bottomLeft']['y'], cell['topRight']['y'] for cell in cell list}. This set reflects all possible row boundary vertical coordinates in the table.
[0054] The purpose of this step is to collect all potential boundary coordinate points in the table, providing original data for subsequent row and column boundary division.
[0055] Next, step S202, the generation of the column boundary set (ColBounds), specifically, the XCoords set is processed to form a standardized column boundary, which exemplarily includes:
[0056] First, arrange XCoords in ascending order (since the x-axis in the CAD coordinate system increases from left to right, ascending order conforms to the logic of columns from left to right), and remove duplicate values to obtain a preliminary ordered coordinate sequence;
[0057] Calculate the difference between adjacent coordinates after sorting. When the difference ≤0.5 coordinate units, it is determined as the boundary error of the same column (such as the slight offset when drawing in CAD), and it is merged into the same coordinate (usually taking the smaller value or the average value). For example, if the adjacent coordinates are 100.2 and 100.5, the difference is 0.3 (≤0.5), and they are merged into 100.2 (or 100.35);
[0058] The merged coordinate sequence is the column boundary set ColBounds, where each coordinate represents the left and right boundaries of a column in the table (e.g., ColBounds = [50.0, 100.2, 150.5] indicates that the table has 3 columns with column boundaries at 50.0, 100.2, and 150.5);
[0059] Next, step S203, generation of the row boundary set (RowBounds), which involves similar processing of the YCoords set to form normalized row boundaries, which exemplarily include:
[0060] Sort YCoords in descending order (since the y-axis in the CAD coordinate system increases from bottom to top, and descending order aligns with the logic of rows from top to bottom, e.g., the first row of the table is at the top with the largest y value), and remove duplicate values to obtain a preliminary sorted coordinate sequence.
[0061] Using the same merging logic as for column boundaries, calculate the difference between adjacent coordinates, and merge them into the same coordinate when the difference is ≤0.5 coordinate units. For example, adjacent coordinates 300.8 and 300.4 (difference 0.4) are merged into 300.8 (or 300.6).
[0062] The merged coordinate sequence is the row boundary set RowBounds, where each coordinate represents the top and bottom boundaries of a row in the table (e.g., RowBounds = [300.8, 250.3, 200.1] indicates that the table has 3 rows with row boundaries at 300.8, 250.3, and 200.1).
[0063] Next, step S204, establish a mapping relationship between coordinates and row-column indices, based on ColBounds and RowBounds, to convert any coordinate to the corresponding row-column index, which exemplarily includes:
[0064] Column index (ColIndex) calculation, specifically, for a certain x-coordinate (such as the X-coordinate of a text or the x-value of a cell boundary), determine its column by ColIndex = index(ColBounds, x). The specific logic is to find the two adjacent column boundary intervals that the x-coordinate falls into (e.g., ColBounds[i] ≤ x ≤ ColBounds[i+1]), and the column index corresponding to the x is i.
[0065] RowIndex calculation, specifically, for a certain y coordinate, determine its row by RowIndex = index(RowBounds, y). The specific logic is to find the two adjacent row boundary intervals where the y coordinate falls (such as RowBounds[j] ≥ y ≥ RowBounds[j+1], because RowBounds is in descending order), then the row index corresponding to the y is j.
[0066] For example, if ColBounds = [50.0, 100.2, 150.5], the X coordinate of a certain text is 70.0, which falls between 50.0 and 100.2, then its ColIndex = 0 (first column); if RowBounds = [300.8, 250.3, 200.1], the Y coordinate of a certain text is 280.0, which falls between 300.8 and 250.3, then its RowIndex = 0 (first row).
[0067] Thus, through the normalization processing of the original coordinates, the boundary offset caused by the CAD drawing error is eliminated, and the physical coordinates are converted into logical row and column indexes, providing a unified reference standard for subsequent spatial matching of text content and cells.
[0068] Step S104, respectively extracting text data and cell data from the paired file, wherein the text data includes each text element and its spatial coordinates, and the cell data includes cell boundary coordinates and their corresponding row and column indexes, establishing the association between text and cell based on the coordinates of text elements and the boundary coordinates of cells, forming the mapping of cell row and column indexes and text.
[0069] In some embodiments, text data and cell data are extracted from the paired loaded JSON file. For example, reading the text data file ([filename]_[TableID]_TableText.json) to obtain the detailed information of each text element, including, for example, Content: text content (such as "bolt" "M10"); X / Y: spatial coordinates of text in the CAD coordinate system (used for positioning).
[0070] Then, the cell coordinate file ([filename]_[TableID]_RepairedTableCells.json) is read, the row and column indexes generated in step S103 are combined, and the structured data of each cell is parsed: bottomLeft / topRight: the left lower corner and the right upper corner boundary coordinates (x, y values) of the cell; RowIndex / ColIndex: the row index and the column index corresponding to the cell (for example, (0, 1) indicates the first row and the second column).
[0071] It can be understood that the text in the table is usually located inside or on the edge of the corresponding cell in the CAD drawing, so the association between the two can be achieved by judging whether the text coordinates fall within the boundary range of the cell.
[0072] Then, whether the text belongs to the target cell is judged, and the steps are exemplarily implemented as follows:
[0073] For each text element, its X / Y coordinates are extracted; for each cell, its bottomLeft.x / bottomLeft.y (left, lower boundary) and topRight.x / topRight.y (right, upper boundary) are extracted.
[0074] The coordinate range is checked in the horizontal direction and the vertical direction, respectively. Exemplarily, the horizontal direction check includes checking whether the text X coordinate is within the left and right boundary range of the cell (including tolerance):
[0075] cell['bottomLeft']['x'] - ε ≤ text['X'] ≤ cell['topRight']['x'] + ε
[0076] Wherein ε is a tolerance coefficient (default 0.1 coordinate unit), which is compatible with the error that the text slightly exceeds the boundary when the CAD drawing is drawn (for example, the text edge overlaps the cell line), and it can be understood that the tolerance coefficient can be set according to the actual situation, and the present application does not limit it.
[0077] The vertical direction check includes checking whether the text Y coordinate is within the upper and lower boundary range of the cell (including tolerance):
[0078] cell['bottomLeft']['y'] - ε ≤ text['Y'] ≤ cell['topRight']['y'] + ε
[0079] When the horizontal and vertical directions both satisfy the condition, it is determined that the text belongs to the cell, and the corresponding relationship between the text and the cell is recorded.
[0080] Finally, the association results are converted into a structured mapping relationship. For example, the mapping structure is defined as a dictionary with (RowIndex, ColIndex) as the key and a list of text elements as the value, such as {(0, 1): [text1, text2], (1, 2): [text3]}. The mapping relationship is temporarily stored in JSON format, containing the text content and original coordinates corresponding to each key.
[0081] Optionally, when a text matches multiple cells or a cell matches multiple texts, regular processing is used to ensure the accuracy of the association. For example,
[0082] Text matching multiple cells: the smallest cell is preferred (the area is calculated by the boundary coordinates: (topRight.x - bottomLeft.x) × (topRight.y - bottomLeft.y)), as the text is usually located in the smallest enclosing cell.
[0083] Cell matching multiple texts: all matching texts are temporarily stored (later combined into complete content by sorting in step S105), without filtering to avoid missing split text fragments (such as "M10 × 50" split into "M10" and "× 50").
[0084] Step S105: Based on the mapping of cell row and column indexes and texts, the text coordinates are sorted twice, and the sorted texts are spliced to obtain the complete text content of the corresponding cell.
[0085] In some embodiments, based on the (RowIndex, ColIndex) -> [list of text elements] mapping relationship generated in step S104, the grouping processing is performed according to the row and column indexes of the cells:
[0086] Each key-value pair in the mapping dictionary is traversed, where the key is (RowIndex, ColIndex) (such as (2, 3) representing the 3rd row and 4th column), and the value is a list of all text elements in the cell.
[0087] From each text element, the core information required for sorting is extracted, including Content (text content), X (horizontal coordinate), and Y (vertical coordinate).
[0088] For multiple texts in the same cell (such as "M10" and "× 50"), the natural reading order of the texts is restored by "vertical first and then horizontal" double sorting:
[0089] First step: Y coordinate descending order sorting
[0090] Sort the text by its Y-coordinate from largest to smallest (sorted(key=lambda x: -x['Y'])). Since a larger Y-value indicates a higher position in the CAD coordinate system, this sorting ensures a vertical "top-to-bottom" text order. For example:
[0091] The cell contains two lines of text: the first line Y=280 (content "bolt"), and the second line Y=270 (content "GB / T5782"). After sorting, keep "bolt" first and "GB / T 5782" last.
[0092] Step 2: Sort by X coordinate in ascending order
[0093] When text has the same Y-coordinate (i.e., lies on the same horizontal line), it is sorted by X-coordinate from smallest to largest (sorted(key=lambda x: x['X'])) to ensure a horizontal text order "from left to right". For example:
[0094] The text “M10” (X=120) and “×50” (X=140) on the same line are sorted and then concatenated to form “M10×50”.
[0095] After sorting, for example, the contents of the cells are concatenated according to the following rules:
[0096] For the sorted list of text, concatenate the Contents sequentially. Horizontal text is directly concatenated (e.g., “M10” + “×50” → “M10×50”), while vertical text is separated by a newline character (\n) (e.g., “bolt” + “GB / T 5782” → “bolt\nGB / T 5782”).
[0097] Optionally, the processing of special characters includes removing redundant spaces (such as separator spaces automatically added in CAD), but retaining necessary format spaces (such as “5”→“5”, “Specification: M10”→“Specification: M10”).
[0098] Finally, the merged results are stored. For example, a final mapping dictionary of (RowIndex, ColIndex) → complete content is generated, such as {(2,3): "M10×50", (3,3): "bolt\nGB / T 5782"}.
[0099] Thus, through this step, the text in the CAD material list that was split due to length limitations or layout requirements is reorganized into readable, complete content, while maintaining the original layout logic (line breaks, order).
[0100] In some embodiments, after generating the complete content of the single cell in step S105, the process of converting the structured data into a standardized Excel file is implemented through workbook construction, data writing and format optimization to realize the visualization and ease of use of material information.
[0101] Preferably, an independent worksheet is generated for each table in the DWG format engineering drawing by establishing a workbook object; the complete text content of the corresponding cell is written into the corresponding cell in the independent worksheet according to the row and column index of the corresponding cell, and the cell is formatted, wherein the formatting includes cell style, column width, and header row formatting.
[0102] Specifically, the Workbook() method of the OpenPyXL library is called to create a blank workbook object as a container for all table data. The default generated blank worksheet (Sheet) will be overwritten by subsequent operations to avoid redundancy. All tables identified in the DWG drawing (based on the number and ID of tables recorded in step S101) are traversed to create an independent worksheet (Worksheet) for each table. The worksheet naming follows the “original file name prefix + table ID” rule, such as “engineering drawing A_12345”, while limiting the name length ≤ 31 characters (the upper limit of Excel worksheet name), and replacing the part exceeding the limit with an MD5 hash value (such as “long file name prefix..._8f3d7e”) to ensure the uniqueness and compliance of the name.
[0103] Next, the (RowIndex, ColIndex) -> complete content mapping dictionary generated in step S105 is loaded to locate the Excel cells in order of row and column index. The writing of the complete content of the cell specifically includes:
[0104] The row and column indices of Excel start from 1, so RowIndex and ColIndex in the mapping need to be added by 1 (for example, RowIndex=0 corresponds to Excel row number 1, and ColIndex=1 corresponds to Excel column number 2).
[0105] Next, the complete content is written into the corresponding cell. For example, the content “specification” of cell (0, 1) will be written into cell A2 of Excel.
[0106] For content containing line breaks (such as “bolt \n GB / T 5782”), it is directly written to preserve the original layout, and automatic line breaks are realized through subsequent format setting.
[0107] Preferably, the readability of the Excel table is improved through uniform style setting, specifically including:
[0108] The basic cell style is set, specifically, the automatic line break is enabled, and the content containing \ n (such as multi-line text) is ensured to be displayed completely; the vertical top alignment is set, which conforms to the conventional layout habit of table text. The style is applied to all cells by default to ensure the uniformity of the format.
[0109] The column width is automatically adjusted, specifically, the character length of the content in each column of all cells is calculated (Chinese is counted as 2 characters, and English / numbers are counted as 1 character). The column width is set based on the maximum character length in the column, and the limit range is 10-50 characters: if the maximum length < 10, set to 10 (to avoid being too narrow); if the maximum length > 50, set to 50 (to avoid being too wide); otherwise, set to the actual maximum length (for example, if the maximum length is 15, set the column width to 15).
[0110] The table header row is strengthened, specifically, the table header row (RowIndex=0, corresponding to Excel row number 1) is located, and a special style is applied to all cells in the row.
[0111] The font is bolded to highlight the distinction between the table header and the content.
[0112] The background color is filled with a light gray background to improve visual recognition.
[0113] Preferably, after completing the writing and formatting of all tables, the workbook object is saved as a local file, and the naming rule is [task ID]_output.xlsx ([task ID] is a unique identifier assigned by the system, which is convenient for tracing). The file is temporarily stored in the local temporary directory (such as / tmp / ), which is prepared for subsequent uploading to the cloud storage (such as OSS) to avoid occupying memory resources.
[0114] In some embodiments, there may be missing boundary lines in the material table in part of the drawing, which may cause disorder in boundary detection and coordinate mapping. To solve this problem, a deep table structure recognition network is introduced in this embodiment to identify the area with missing boundary lines and complete the missing boundary lines based on the cell boundary coordinates extracted in step S102.
[0115] Preferably, based on the global coordinate range of the table, the corresponding table region image is obtained, the table region image and the cell boundary coordinates are input into the pre-trained deep table structure recognition network model, and the missing probability map of the table boundary line is output; based on the probability map and the cell boundary coordinates, the missing boundary line is identified and completed, and the completed boundary coordinates are added to the cell coordinate file.
[0116] As Figure 3As shown, the deep table structure recognition network model includes an input layer, a feature extraction layer, a feature fusion layer, and an output layer. The input layer receives the table region image and the cell boundary coordinates, and outputs a preprocessed image. The preprocessing includes establishing a mapping relationship between the pixel coordinates of the table region image and the actual coordinates of the cell boundary, and removing noise points of the table region image. The feature extraction layer is a CNN layer, which receives the preprocessed image and outputs a feature map after convolution operation. The feature fusion layer is a feature pyramid network, which receives the feature map and outputs a fused second feature map. The output layer receives the second feature map, and outputs a missing probability map of the table boundary line through convolution operation and activation function.
[0117] Specifically, the specific network structure and implementation process include:
[0118] 1. Input layer and preprocessing
[0119] Input: Table region image of target layer in CAD drawing (size of 512x512 pixels grayscale image), containing identified table outline and part of boundary line.
[0120] Preprocessing: Remove noise points (such as residual drawing traces) in CAD drawing by Gaussian filtering, and retain edge features of table lines and cells; establish mapping between image pixel coordinates and cell boundary coordinates (such as bottomLeft and topRight) extracted in S102, to ensure that the network output is aligned with the actual coordinate system.
[0121] 2. Feature extraction network
[0122] Exemplarily, a lightweight convolutional neural network (CNN) is used to extract table edge features, which balances detection accuracy and efficiency. The network includes:
[0123] Convolution layer: Exemplarily, the first to third layers are 3x3 convolution kernels (channel numbers are 16→32→64 in turn), with a step of 1, and are combined with ReLU activation function to capture local edges of table lines (such as line segment endpoints and intersection points);
[0124] The fourth to fifth layers are 5x5 convolution kernels (channel numbers 128→256) with a step of 1, which capture global structural features of the table (such as row and column distribution rules and continuity of boundary lines);
[0125] Pooling operation: After every 2 layers of convolution, 2x2 max pooling (step 2) is performed to reduce the size of the feature map while retaining key features.
[0126] Output: Exemplarily, a feature map of 64x64x256, containing position, direction and integrity information of the table line.
[0127] 3. Multi-scale feature fusion layer
[0128] Exemplarily, different levels of features are fused through a feature pyramid network (FPN) to improve the positioning accuracy of the missing boundary lines. Specifically, the global features of the high layer (64x64) are fused from top to bottom by upsampling (bilinear interpolation) and spliced with the local features of the middle layer (128x128) and the low layer (256x256) to supplement the detail information.
[0129] 4. Output layer
[0130] The output layer is used to output the missing probability map of the table boundary line to locate the area to be completed. Exemplarily, it includes two layers of 1x1 convolution (channel number 128→2) to predict the missing probability of the row boundary line and the column boundary line, respectively. Through the Sigmoid activation function, two 512x512 probability maps (row missing probability map and column missing probability map) are output, and the pixel value range is [0, 1] (the higher the value, the greater the possibility of missing the boundary line at that position). The threshold is set to be when the pixel value is greater than or equal to 0.7, it is determined that the boundary line is missing. It can be understood that the threshold can be set according to the actual situation, and the present application does not make any limitation.
[0131] In some embodiments, identifying the missing boundary line and completing it includes extracting the pixel coordinates of the row / column missing area from the missing probability map, converting them into actual coordinates based on the mapping relationship, and automatically filling in the row / column missing area based on the average distance between adjacent rows / columns of the cell boundary coordinates.
[0132] Specifically, the pixel coordinates of the row / column missing area are extracted from the probability map output by the network and converted into CAD actual coordinates (based on the coordinate mapping relationship of the input layer).
[0133] The classification of the missing type includes, for example, the row missing, which is exemplarily specific to the discontinuity of the upper / lower boundary coordinates of a plurality of consecutive cells (such as the difference between topRight.y of a row and bottomLeft.y of the next row being far greater than the normal row spacing); and the column missing, which is exemplarily specific to the discontinuity of the left / right boundary coordinates of a plurality of consecutive cells (such as the difference between topRight.x of a column and bottomLeft.x of the next column being abnormal).
[0134] From the cell boundary coordinates extracted in step S102, the complete and continuous boundary is selected as the completion reference:
[0135] The YCoords set (y coordinates of the cells) is sorted in descending order, the difference (normal row spacing) between adjacent coordinates is calculated, and the outliers deviating from the mean value by more than twice are removed.
[0136] Sort the XCoords set (x coordinates of cells) in ascending order, calculate the difference of adjacent coordinates (normal column spacing), and keep the continuous 3 or more coordinate sequences that meet the average value.
[0137] In this embodiment, for the missing row area, take the average spacing of adjacent valid rows (for example, the average row height of the above 3 rows is 10 coordinate units); based on the starting y coordinate of the missing area, calculate the missing row boundary in sequence according to the average value (for example, the starting y=200, and the boundary lines of y=190 and y=180 are completed).
[0138] For the missing column area, take the average spacing of adjacent valid columns (for example, the average column width of the left 2 columns is 15 coordinate units); based on the starting x coordinate of the missing area, calculate the missing column boundary in sequence according to the average value (for example, the starting x=100, and the boundary lines of x=115 and x=130 are completed).
[0139] Finally, add the completed boundary coordinates to the XCoords / YCoords set, re-execute the sorting, de-duplication and merging operations to generate complete ColBounds and RowBounds. For details, refer to the above embodiment, which will not be repeated here.
[0140] Figure 4 A material information recognition and statistics device 400 based on CAD export is shown. The device embodiment corresponds to the method embodiment shown in Figure 1 The device can be applied to various electronic devices. Specifically, it includes:
[0141] A table analysis and recognition module 401 is configured to analyze a DWG format engineering drawing to obtain all layer sets in the drawing, filter target layers based on a preset layer name matching rule, recognize table objects from the target layers, record the number of tables and the global coordinate range of the tables, and identify the table objects, wherein the table objects contain material information.
[0142] A text and cell extraction module 402 is configured to extract text elements and their spatial coordinate information in a table by a first script for each recognized table object, generate a text data file, and extract cell boundary coordinates by a second script, and generate a cell coordinate file.
[0143] A row and column index conversion module 403 is configured to pair the text data file and the cell coordinate file based on the file name prefix and the table ID to obtain a paired file, and convert the cell boundary coordinates in the table to ordered row and column indexes based on the paired file.
[0144] The row and column index and character mapping module 404 is configured to extract character data and cell data from the matching file, wherein the character data comprises each character element and its spatial coordinates, and the cell data comprises cell boundary coordinates and corresponding row and column indexes thereof, to establish a correlation between the character and the cell based on the coordinates of the character element and the boundary coordinates of the cell, and to form a mapping between the row and column indexes of the cell and the character.
[0145] The cell character splicing module 405 is configured to perform double sorting on the character coordinates based on the mapping between the row and column indexes of the cell and the character, and to splice the sorted characters to obtain complete character content of the corresponding cell.
[0146] Based on the same inventive concept, the electronic device provided in the embodiments of the present application can correspond to the method in the foregoing embodiments, and the problem solving principle thereof is similar to that of the method. The electronic device provided in the embodiments of the present application comprises at least one processor, and a memory in communication connection with the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the method and / or technical solution of the foregoing embodiments of the present application.
[0147] The electronic device can be a user device, or a device integrated by a user device and a network device through a network, or can also be an application program running on the above device, the user device includes but is not limited to computers, mobile phones, tablet computers, smart watches, wristbands and various terminal devices, and the network device includes but is not limited to network hosts, single network servers, multiple network server sets or computer sets based on cloud computing, etc., which can be used to realize part of the processing function when setting an alarm. Here, the cloud is composed of a large number of hosts or network servers based on cloud computing, wherein the cloud computing is a kind of distributed computing, which is composed of a virtual computer formed by a group of loosely coupled computer sets.
[0148] Figure 5 The structure of a device suitable for implementing the method and / or technical solution in the embodiments of the present application is shown, the device 500 comprises a central processing unit (CPU) 501, which can perform various appropriate actions and processes according to programs stored in a read-only memory (ROM) 502 or programs loaded into a random access memory (RAM) 503 from a storage part 508. In the RAM 503, various programs and data required for system operation are also stored. The CPU 501, the ROM 502 and the RAM 503 are connected to each other through a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0149] The following components are connected to the I / O interface 505: an input section 506 including a keyboard, a mouse, a touch screen, a microphone, an infrared sensor, and the like; an output section 507 including a display such as a cathode ray tube (CRT), a liquid crystal display (LCD), an LED display, an OLED display, and the like, and a speaker, and the like; a storage section 508 including one or more computer readable medium such as a hard disk, an optical disk, a magnetic disk, a semiconductor memory, and the like; and a communication section 509 including a network interface card such as a LAN (Local Area Network) card, a modem, and the like. The communication section 509 performs communication processing via a network such as the Internet.
[0150] In particular, the methods and / or embodiments in the present application can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product comprising a computer program carried on a computer readable medium, the computer program containing program code for executing the methods shown in the flowcharts. When the computer program is executed by a central processing unit (CPU) 501, the above-mentioned functions defined in the methods of the present application are performed.
[0151] Another embodiment of the present application also provides a computer readable storage medium having stored thereon computer program instructions, which can be executed by a processor to implement the method and / or technical solutions of any one or more embodiments of the present application.
[0152] In particular, the embodiments can employ any combination of one or more computer readable media. The computer readable media can be a computer readable signal medium or a computer readable storage medium. The computer readable storage medium can be, for example but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the above. More specific examples (a non-exhaustive list) of the computer readable storage medium include an electrical connection having one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, the computer readable storage medium can be any tangible medium that contains or stores a program that can be used by an instruction execution system, apparatus, or device to execute the program.
[0153] A computer readable signal medium can include a propagated data signal with computer executable code embodied therein. Embodiments of the application can be implemented using software running on a general purpose computer or work station, program code being applied to input data to perform the functions of the application, or items of hardware that perform some of the functions of the application. Embodiments of the application also can be implemented by means of software modules that are executed on the computing device.
[0154] Program code embodied on a computer readable medium can be transmitted using any appropriate medium, including but not limited to wireless, wire line, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
[0155] The computer program instructions can also be loaded onto a computer, other programmable data processing apparatus, or other devices to cause a series of operational steps to be performed on the computer, other programmable apparatus or other devices to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide processes for implementing the functions of the
[0156] Furthermore, the term "comprising" does not exclude other elements or steps and the singular does not exclude the plural and vice versa. A single processor or other unit can fulfill the functions of several units or a unit can be implemented jointly with a few other units. The terms "first", "second" and the like do not denote any ordinal, but do denote different categories.
Claims
1. A CAD-based material information extraction and statistical method, characterized in that, The method comprises the following steps: Step S101, parsing the DWG format engineering drawing to obtain all layer sets in the drawing, traversing the layer sets based on a preset layer name matching rule to obtain a target layer, identifying a table object from the target layer, recording the number of tables and the global coordinate range of the tables in the table object, wherein the table object contains material information; Step S102, for each identified table object, extracting text elements and their spatial coordinate information in the table through a first script to generate a text data file; extracting cell boundary coordinates through a second script to generate a cell coordinate file; Step S103, pairing the text data file and the cell coordinate file based on the file name prefix and the table ID to obtain a paired file, and converting the cell boundary coordinates in the table into ordered row and column indexes based on the paired file; Step S104, extracting text data and cell data from the paired file, wherein the text data includes each text element and its spatial coordinates, the cell data includes cell boundary coordinates and their corresponding row and column indexes, establishing an association between the text elements and the cells based on their coordinates and boundary coordinates to form a mapping between the cell row and column indexes and the text; Step S105, based on the mapping between the cell row and column indexes and the text, double-sorting the text coordinates, and splicing the sorted text to obtain the complete text content of the corresponding cell; Further comprising, based on the global coordinate range of the table, obtaining a corresponding table region image, inputting the table region image and the cell boundary coordinates into a pre-trained deep table structure recognition network model to output a missing probability map of the table boundary line; Based on the probability map and the cell boundary coordinates, identifying and completing the missing boundary lines, and adding the completed boundary coordinates to the cell coordinate file; Wherein, the conversion of the cell boundary coordinates in the table into ordered row and column indexes based on the paired file comprises, extracting the x and y coordinates of the lower left corner and the upper right corner of all cells from the cell coordinate file to generate XCoords and YCoords sets, wherein the XCoords set contains the x values of the lower left corners and the x values of the upper right corners of all cells, and the YCoords set contains the y values of the lower left corners and the y values of the upper right corners of all cells; sorting the XCoords set in ascending order and removing duplicates, calculating the difference value of adjacent coordinates, merging them into the same coordinate when the difference value is ≤0.5 coordinate units to form a column boundary set ColBounds; sorting the YCoords set in descending order and removing duplicates, calculating the difference value of adjacent coordinates, merging them into the same coordinate when the difference value is ≤0.5 coordinate units to form a row boundary set RowBounds; A mapping of coordinates to row and column indexes is established, for an x coordinate, find two adjacent column boundary intervals in ColBounds that it falls in, the corresponding column index is the index of the first boundary of the interval in ColBounds; for a y coordinate, find two adjacent row boundary intervals in RowBounds that it falls in, the corresponding row index is the index of the first boundary of the interval in RowBounds.
2. The method according to claim 1, wherein, Further comprising, By establishing a workbook object, an independent worksheet is generated for each table in the DWG format engineering drawing; The complete text content of the corresponding cell is written into the corresponding cell in the independent worksheet according to the row and column indexes of the corresponding cell, and the cell is formatted, wherein the formatting includes cell style, column width, and header row formatting.
3. The CAD export-based material information identification and statistics method of claim 1, wherein, The deep table structure identification network model comprises an input layer, a feature extraction layer, a feature fusion layer, and an output layer, The input layer receives the table region image and the cell boundary coordinates, and outputs a preprocessed image, the preprocessing including establishing a mapping relationship between the table region image pixel coordinates and the cell boundary actual coordinates, and removing noise points of the table region image; The feature extraction layer is a CNN layer, which receives the preprocessed image and outputs a feature map after convolution operation; The feature fusion layer is a feature pyramid network, which receives the feature map and outputs a fused second feature map; The output layer receives the second feature map, and outputs a missing probability map of the table boundary line through convolution operation and an activation function.
4. The CAD export-based material information identification and statistics method of claim 3, wherein, The identification of missing boundary lines and completion includes extracting the pixel coordinates of the row / column missing area from the missing probability map, converting the pixel coordinates into actual coordinates based on the mapping relationship, and automatically filling the row / column missing area based on the average distance between adjacent rows / columns of the cell boundary coordinates.
5. A CAD export-based material information recognition and statistics device, characterized by, Comprise: A table analysis and identification module is configured to analyze a DWG format engineering drawing to obtain all layer sets in the drawing, filter target layers based on a preset layer name matching rule by traversing the layer sets, identify table objects from the target layers, and record the number of tables and the global coordinate range of the tables, wherein the table objects contain material information. A text and cell extraction module is configured to, for each identified table object, extract text elements and their spatial coordinate information in the table by a first script, and generate a text data file. A second script is used to extract cell boundary coordinates and generate a cell coordinate file. A row and column index conversion module is configured to pair the text data file and the cell coordinate file based on the file name prefix and the table ID to obtain a paired file, and convert the cell boundary coordinates in the table into ordered row and column indexes based on the paired file. A row-column index and text mapping module is configured to extract text data and cell data from the pairing file, wherein the text data includes each text element and its spatial coordinates, and the cell data includes cell boundary coordinates and its corresponding row-column index, to establish the association between the text and the cell based on the coordinates of the text element and the boundary coordinates of the cell, and to form the mapping between the cell row-column index and the text; A cell text splicing module is configured to perform double sorting on the text coordinates based on the mapping between the cell row-column index and the text, to splice the sorted text to obtain the complete text content of the corresponding cell; Further comprising a missing probability map acquisition module configured to acquire a corresponding table region image based on the global coordinate range of the table, to input the table region image and the cell boundary coordinates into a pre-trained deep table structure recognition network model, and to output a missing probability map of the table boundary line; Based on the probability map and the cell boundary coordinates, the missing boundary line is identified and completed, and the completed boundary coordinates are added to the cell coordinate file. The conversion of the cell boundary coordinates in the table into ordered row-column indexes based on the pairing file comprises: X and y coordinates of the lower left corner and the upper right corner of all cells are extracted from the cell coordinate file to generate XCoords and YCoords, wherein XCoords contains x values of the lower left corner and x values of the upper right corner of all cells, and YCoords contains y values of the lower left corner and y values of the upper right corner of all cells; XCoords is sorted in ascending order and de-duplicated, the difference between adjacent coordinates is calculated, and when the difference is ≤0.5 coordinate units, the adjacent coordinates are merged into the same coordinate to form a column boundary set ColBounds; YCoords is sorted in descending order and de-duplicated, the difference between adjacent coordinates is calculated, and when the difference is ≤0.5 coordinate units, the adjacent coordinates are merged into the same coordinate to form a row boundary set RowBounds; A mapping between coordinates and row-column indexes is established, for x coordinates, find the two adjacent column boundary intervals in ColBounds where the x coordinates fall, and the corresponding column index is the index of the previous boundary in ColBounds; for y coordinates, find the two adjacent row boundary intervals in RowBounds where the y coordinates fall, and the corresponding row index is the index of the previous boundary in RowBounds.
6. An electronic device, the electronic device comprising: It comprises: at least one processor; and a memory connected in communication with the processor; wherein the memory stores instructions executable by the processor, and the instructions are executed by the processor to enable the processor to perform the method of any one of claims 1-4.
7. A computer readable medium having stored thereon computer program instructions, characterized in that, The computer program instructions can be executed by the processor to implement the method of any one of claims 1-4.
8. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to implement the method of any one of claims 1-4.
Citation Information
Patent Citations
Spreadsheet structured recognition and extraction method based on CAD basic elements
CN112241411A
Method and system for adjusting CAD drawing segmentation parameters in combination with region recognition
CN120374991A