A method and system for intelligent generation of parametric drawing code for hydraulic engineering based on variable snapshots and spatial matching
By employing a two-layer matching architecture of a global engineering parameter information database and a runtime variable snapshot dictionary, combined with large language models and spatial matching technology, the problem of large language models being unable to generate parametric drawing code for water conservancy projects has been solved. This has enabled the automatic parametric representation of station number annotations, improving the accuracy and coverage of drawing code generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HOHAI UNIV
- Filing Date
- 2026-05-12
- Publication Date
- 2026-07-31
AI Technical Summary
Large language models cannot restore absolute coordinates to parametric variables in water conservancy projects. Existing CAD macro recording and BIM schemes cannot automatically infer variable semantics. Prior knowledge of water conservancy projects is scattered and difficult to manage, resulting in difficulties in generating parametric drawing code, especially in the scenario of stationing, where parametric expressions cannot be automatically inferred.
By constructing a two-layer matching architecture of a global project parameter information database and a runtime variable snapshot dictionary, the variable names and parameterized expressions corresponding to the target coordinates are deduced, and parameterized plotting code is generated by combining a large language model. Memory reflection and spatial matching techniques are used to solve the variable name mapping problem.
It implements parameterized plotting code that generates references to actual variable names in the source program from a large language model, improving the coverage and matching accuracy of engineering parameters. It is suitable for scenarios with dense station number annotation in water conservancy projects and supports progressive self-learning improvement.
Smart Images

Figure CN122489053A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer-aided design (CAD) programming and artificial intelligence code generation technology for water conservancy projects, specifically involving an intelligent generation method and system for parametric drawing code of water conservancy projects based on variable snapshots and spatial matching. Background Technology
[0002] Water conservancy projects encompass various hydraulic structures such as reservoirs and dams, water diversion tunnels, irrigation canals, dikes and revetments, and sluice gates and pumping stations. Their design drawings are diverse, including overall layout plans, structural drawings, longitudinal sections, cross sections, and reinforcement drawings, all of which must adhere to industry standards. In water conservancy engineering design practice, designers widely use general-purpose CAD software such as AutoCAD and develop drawing programs (source code) using secondary development methods such as VisualLISP, ObjectARX, and VBA to achieve parametric automatic drawing of design drawings. These drawing programs extensively use parametric variables to express geometric relationships and engineering constraints. For example, "channel bottom line.start point + lining thickness" represents the offset position of the outer edge of the channel lining, or "dam axis.GetPointByRatio(0.3)" represents the coordinate point at a certain scale position on the dam axis. Parametric programming makes the design results reusable and modifiable, and is the core practice of parametric design in water conservancy projects.
[0003] Hydraulic engineering design drawings have a significant characteristic that distinguishes them from general architectural engineering drawings: linear hydraulic structures such as channels, tunnels, dikes, and diversion channels are generally marked with chainages (stations) along their axes. Chainage parameters are present in various types of drawings, including longitudinal profiles, cross-sections, site plans, and geological profiles, making it one of the most fundamental and densely packed annotation systems in hydraulic engineering design drawings. The standard format for chainages is usually "K+M" (K being the kilometer number and M being the remaining meter number), for example, "0+150.000" indicates a distance of 150 meters from the starting point of the route. In the drawing program, the chainage value is usually calculated by cumulatively adding the length expressions of the baseline chainage variable and the line element variables along the route. Its parameterized expression involves traversing geometric topological relationships and summing length attributes segment by segment, making the logic more complex than general coordinate parameterization.
[0004] With the development of large language model technology, designers hope to use large language models to assist in writing or completing drawing program code, thereby improving programming efficiency. However, the following technical obstacles are encountered in practical applications: First, large language models lack the ability to convert absolute coordinates into parameterized variables. When a designer selects a graphic element in the CAD interface and expects the large language model to generate the corresponding drawing code, the large language model can only obtain the absolute numerical coordinates of the graphic element in the drawing coordinate system, but cannot know the variable name corresponding to the coordinates in the drawing source program. Because the large language model does not have the ability to access the runtime memory state of the drawing program, it can only generate code containing hard-coded coordinate values, and cannot generate reusable code that references existing parameterized variables in the source program. This problem is particularly prominent in the stationing scenario: there is a non-trivial topological accumulation relationship between the stationing text (such as "0+234.500") and its parameterized calculation expression in the drawing program (such as "base stationing + seg_A.Length + seg_B.Length"), and the large language model cannot derive the corresponding parameterized expression from the stationing value alone.
[0005] Secondly, existing CAD macro recording and parametric secondary development technologies have inherent limitations. Macro recording mechanisms record the user's action sequence and absolute coordinate values; essentially, they replay the user interface interaction process. They do not involve capturing and parsing the variable states in memory during program runtime, therefore, they cannot reconstruct the parametric variable expression logic from the recording results. While traditional secondary development solutions for hydraulic engineering CAD based on VisualLISP or ObjectARX can achieve parametric drawing of specific drawings, they are fixed-function programming results and lack the ability to automatically deduce variable semantics from existing drawing code and generate new code.
[0006] Third, the parametric approach of Building Information Modeling (BIM) is not suitable for general CAD drawing scenarios in water conservancy projects. The parametric capabilities of BIM systems rely on users manually defining parameter relationships in advance during the modeling phase and storing parameter dependency diagrams in a proprietary data format. However, BIM applications in the water conservancy and hydropower industry are still in their early stages. Compared to building construction and railways, the BIM standard system and software ecosystem for water conservancy projects are not yet mature, and many design institutes still rely primarily on 2D CAD parametric drawing as their main production tool. BIM solutions require the planning and definition of the parameter system before modeling and are limited by specific BIM platforms and data formats, making them unsuitable for the mainstream engineering practice in water conservancy projects that uses general CAD software in conjunction with custom drawing programs for parametric design.
[0007] Fourth, prior knowledge in water conservancy projects is scattered and difficult to fully acquire in a single run. In water conservancy projects, key design parameters such as station sequence, design water level, canal bottom elevation, cross-sectional dimensions, and hydraulic gradient are stored in various design documents (such as feasibility study reports, preliminary design specifications, and construction drawings) and corresponding design parameter tables. These parameters are not all instantiated in every run of the drawing program; therefore, relying solely on a memory snapshot from a single program run is insufficient to cover all engineering parameters. The lack of unified management of these cross-drawing common parameters limits the coverage of parameterized code generation.
[0008] In summary, there is an urgent need for a method to intelligently generate parametric drawing code that can automatically establish a mapping relationship between absolute coordinates and source program variable names based on the results of the drawing program, and input this mapping as a constraint into a large language model. This would solve the technical problem that large language models cannot independently generate parametric code, especially the problem of automatic parametric inference of station expressions in dense station labeling scenarios in water conservancy projects. Summary of the Invention
[0009] To address the problems existing in the prior art, this invention provides an intelligent generation method and system for parametric drawing code of water conservancy projects based on variable snapshots and spatial matching. It maintains a global engineering parameter information database independent of runtime snapshots, and the two constitute a two-layer matching architecture, supporting station number topology path inference, thus solving the problem that large language models cannot independently generate parametric drawing code.
[0010] To achieve the above objectives, the present invention provides the following solution: A method for intelligently generating parametric drawing code for hydraulic engineering based on variable snapshots and spatial matching, the method comprising: S1: Build or update the global engineering parameter information database; obtain the geometric data of user-selected elements from CAD software, including control point coordinates, element type and associated text content; S2: Execute the drawing source program, establish the correspondence between coordinate feature values and variable names through reflection traversal of the program's runtime memory and static analysis of the source program code, and generate a runtime variable snapshot dictionary; S3: Spatial matching of control point coordinates with runtime variable snapshot dictionary to infer the variable name and parameterized expression corresponding to the target coordinates; S4: Input the parameterized expression, primitive type, and associated text content as context information into the large language model, and the large language model generates parameterized drawing code; the variable name mapping relationship in the runtime variable snapshot dictionary and the global project parameter information library is input as a constraint along with the context, so that the variable names in the code output by the large language model are consistent with the actual variable names in the drawing source program.
[0011] Preferably, in step S2, the method for executing the drawing source program and establishing the correspondence between coordinate feature values and variable names by reflecting on the program's runtime memory and performing static analysis of the source program code, and generating a runtime variable snapshot dictionary, includes: S21: After the drawing source program finishes execution, the geometric object instances in the program runtime memory are traversed by reflection, and the coordinate feature values of each geometric object are extracted according to its type; each coordinate value is associated with its corresponding geometric object instance. S22: Perform abstract syntax tree parsing on the source code of the drawing source program, traverse the assignment statements and function call statements in the syntax tree, and identify the variable names assigned to each geometric object instance in the source program; S23: Bind the results of S21 and S22 to the same object instance: The coordinate values obtained by memory reflection and the variable names obtained by AST analysis are associated with the same object instance identifier, thereby generating a runtime variable snapshot dictionary.
[0012] Preferably, in step S3, the method for spatially matching the control point coordinates with the runtime variable snapshot dictionary to deduce the variable name and parameterized expression corresponding to the target coordinates includes: Exact match: Calculate the Euclidean distance between the target coordinates and each entry in the snapshot dictionary. If the distance is less than the tolerance threshold, generate the expression "variable name". Offset matching: When an exact match fails, calculate the vector difference between the target coordinates and the coordinates of each reference variable, search the engineering parameter set for parameters with the same value as the vector difference, and generate the expression "reference variable + offset parameter". Scale matching: When neither exact match nor offset match is found, calculate the projection scale of the target coordinates onto the line segment variable in the snapshot dictionary, and generate a scaled point expression with the line segment variable and the projection scale coefficient as parameters.
[0013] Preferably, S3 further includes: Calculate a confidence score for each matching result; For results with a confidence level below a preset threshold, a prompt will be displayed in the user interface, allowing the user to manually confirm or correct them; After user confirmation, the mapping relationship of "coordinate value → parameter variable name" will be automatically written into the global engineering parameter information database, so that the global engineering parameter information database will continue to accumulate mapping entries as the number of times it is used increases.
[0014] Preferably, the method of inputting parametric expressions, primitive types, and associated text content as contextual information into a large language model, and generating parametric drawing code from the large language model, includes: Identify strings in the graphic element text that conform to the station number format, and locate the corresponding benchmark station coordinates in the global engineering parameter information database; Search for a continuous geometric path from the base station point to the current annotation anchor point in the geometric topology graph of the runtime variable snapshot; The length attributes of each line segment variable on the path are accumulated sequentially to generate a station number calculation expression code consisting of the sum of the base station number variable and the length of each line segment variable segment by segment.
[0015] The present invention also provides an intelligent generation system for parametric drawing code of water conservancy projects based on variable snapshots and spatial matching. The system is used to implement the aforementioned method and includes: a graphic element acquisition module, a snapshot generation module, a global engineering parameter library module, a spatial matching module, and an intelligent code generation module. The global engineering parameter library module is used to build or update the global engineering parameter information library; The graphic element acquisition module is used to acquire the geometric data of the graphic elements selected by the user from the CAD software, including control point coordinates, graphic element type and associated text content; The snapshot generation module is used to execute the drawing source program, and through reflection traversal of the program's runtime memory and static analysis of the source program code, establish the correspondence between coordinate feature values and variable names, and generate a runtime variable snapshot dictionary; The spatial matching module is used to perform spatial matching between the control point coordinates and the runtime variable snapshot dictionary, and to infer the variable name and parameterized expression corresponding to the target coordinates. The intelligent code generation module is used to input parameterized expressions, primitive types, and associated text content as context information into the large language model, and the large language model generates parameterized drawing code. The variable name mapping relationship in the runtime variable snapshot dictionary and the global project parameter information library is input as a constraint along with the context, so that the variable names in the code output by the large language model are consistent with the actual variable names in the drawing source program.
[0016] Preferably, the snapshot generation module includes: a memory reflection traversal unit, an AST static analysis unit, and a binding unit; The memory reflection traversal unit is used to perform reflection traversal on the geometric object instances in the program runtime memory after the drawing source program has finished executing, and extract the coordinate feature values of each geometric object according to its type; each coordinate value is associated with its corresponding geometric object instance. The AST static analysis unit is used to perform abstract syntax tree parsing on the source code of the drawing source program, traverse the assignment statements and function call statements in the syntax tree, and identify the variable names assigned to each geometric object instance in the source program. The binding unit is used to bind the results of the memory reflection traversal unit and the AST static analysis unit to the same object instance: the coordinate values obtained by memory reflection and the variable names obtained by AST analysis are associated with the same object instance identifier, thereby generating a runtime variable snapshot dictionary.
[0017] Preferably, the spatial matching module includes: a precise matching unit, an offset matching unit, and a proportional matching unit; The precise matching unit is used to calculate the Euclidean distance between the target coordinates and each entry in the snapshot dictionary. When the distance is less than the tolerance threshold, the expression "variable name" is generated. The offset matching unit is used to calculate the vector difference between the target coordinates and the coordinates of each reference variable when the exact match fails, retrieve the parameter with the same value as the vector difference in the engineering parameter set, and generate the expression "reference variable + offset parameter". The proportional matching unit is used to calculate the projection ratio of the target coordinates onto the line segment variable in the snapshot dictionary when neither the current exact match nor the offset match is found, and to generate a proportional point selection expression with the line segment variable and the projection ratio coefficient as parameters.
[0018] Preferably, the spatial matching module further includes: a confidence calculation unit, a comparison unit, and a writing unit; The confidence score calculation unit is used to calculate the confidence score for each matching result; The comparison unit is used to prompt users in the user interface and allow them to manually confirm or correct results with a confidence level lower than a preset threshold. The writing unit is used to automatically write the mapping relationship of "coordinate value → parameter variable name" into the global engineering parameter information database after user confirmation, so that the global engineering parameter information database continues to accumulate mapping entries as the number of uses increases.
[0019] Preferably, the intelligent code generation module includes: an identification and positioning unit, a search unit, and an accumulation unit; The identification and positioning unit is used to identify strings in the graphic element text that conform to the station number format and locate the corresponding benchmark station number coordinates in the global engineering parameter information database. The search unit is used to search for a continuous geometric path from the base station point to the current labeled anchor point in the geometric topology map of the runtime variable snapshot. The accumulation unit is used to sequentially accumulate the length attributes of each line segment variable on the path to generate a station number calculation expression code composed of the base station number variable and the length of each line segment variable summed segment by segment.
[0020] Compared with the prior art, the beneficial effects of the present invention are as follows: (1) The present invention establishes a mapping relationship of "coordinate value → variable name" based on the running result of the drawing source program, and inputs the mapping as an explicit constraint into the large language model, so that the large language model can generate parameterized drawing code that references the actual variable name in the source program, fundamentally solving the technical problem that the large language model cannot independently restore absolute coordinates to parameterized variables.
[0021] (2) The present invention adopts a two-layer matching architecture consisting of a global engineering parameter information library and a runtime variable snapshot dictionary. The global library persistently stores engineering prior knowledge that is universal across drawings, such as station number sequence and design parameters. The runtime snapshot captures dynamic variable instances generated during the current program execution. The two complement each other for querying, effectively improving the coverage of engineering parameters and making up for the shortcomings of relying solely on a single runtime snapshot, which may miss some parameters.
[0022] (3) This invention forms a progressive self-learning closed-loop mechanism by having users confirm the low-confidence matching results and automatically writing the mapping relationship after user confirmation into the global engineering parameter information database, so that the matching accuracy of the system continues to improve with the increase of the number of uses, which is different from the limitation of static rule system where the accuracy does not change after deployment.
[0023] (4) This invention proposes a stationing inference method based on geometric topology paths for stationing annotation primitives. By searching for continuous paths in the geometric topology map of runtime variable snapshots and accumulating the length expressions of each line segment variable, a calculation expression code conforming to the stationing definition rules is generated. In water conservancy projects, linear hydraulic structures such as channels, tunnels, and dikes commonly use a stationing system to annotate mileage along the axis. Stationing parameters are used in various design drawings such as longitudinal profiles, cross profiles, and plan layouts. This method is particularly suitable for parametric design scenarios of water conservancy projects with dense stationing annotations. Attached Figure Description
[0024] To more clearly illustrate the technical solution of the present invention, the drawings used in the embodiments are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0025] Figure 1 This is an overall flowchart of the intelligent generation method for parameterized drawing code based on variable snapshots and spatial matching in an embodiment of the present invention; Figure 2 This is a schematic diagram illustrating the process of generating the runtime variable snapshot dictionary in an embodiment of the present invention; Figure 3 This is a flowchart illustrating the workflow of tailrace branch pipe chainage topology inference and global engineering parameter information database collaboration in an embodiment of the present invention. Figure 4 This is a schematic diagram of the system working effect in an embodiment of the present invention, taking the marking of the chainage of the tailrace bifurcation longitudinal section as an example. (a) is the tailrace bifurcation longitudinal section (CAD), (b) is a schematic diagram of the variable snapshot dictionary (S2 output), (c) is a schematic diagram of the AI-generated parameterized code (S4 output), and (d) is a schematic diagram of the execution result. Detailed Implementation
[0026] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0027] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0028] Example 1 like Figure 1 As shown, this invention provides an intelligent generation method for parametric drawing code in water conservancy engineering based on variable snapshots and spatial matching. Specifically, it involves a method using runtime memory reflection and spatial coordinate matching technology to assist a large language model in generating parametric drawing code for water conservancy engineering. The core idea of this method is: first, execute the drawing source program once, recording the "variable → coordinate" mapping generated by the program forward as a runtime variable snapshot dictionary. Then, perform a reverse lookup on the coordinates of the primitives selected by the user in CAD, restoring the absolute coordinates to the variable names and parametric expressions in the source program. Finally, use this mapping relationship as the constraint context input to the large language model to generate usable parametric drawing code. Simultaneously, maintain a global engineering parameter information database independent of runtime snapshots, storing water conservancy engineering design parameters that are universal across drawings, such as station sequence, design water level, canal bottom elevation, and cross-sectional dimensions. This database, together with runtime snapshots, forms a two-layer matching architecture of "persistent prior + dynamic instance," compensating for the coverage limitations of a single runtime snapshot. Specifically, it includes the following steps: Step S1: Global library initialization and primitive geometric data acquisition.
[0029] Before executing a drawing task, the system first builds or updates a global engineering parameter information database. This database persistently stores prior engineering knowledge bound to the current project, including cross-drawing parameters such as station sequence and its design coordinates, structural design parameters, and project number mappings. Initially, the user imports engineering parameters to complete the database construction; in subsequent uses, when the user confirms a low-confidence matching result, the system automatically writes the new mapping entry into the global database to complete the update.
[0030] Subsequently, the system retrieves the geometric data of the user-selected elements from the CAD software. Through interaction with the CAD software's programming interface, the system obtains one or more element objects selected by the user in the CAD interface, extracting the control point coordinates, element type (e.g., line segment, arc, polyline, text, etc.), and associated text content for each element. The extracted coordinate features differ for different types of elements: line segment elements are extracted with start and end coordinates; arc elements with center coordinates and radius; polyline elements with the coordinate sequence of each control vertex; and text elements with the text content and its bounding box anchor point coordinates. This geometric data forms the input basis for subsequent matching and code generation.
[0031] Step S2: Generating a runtime variable snapshot dictionary.
[0032] like Figure 2 As shown, the drawing source program is executed. Through reflection traversal of the program's runtime memory and static analysis of the abstract syntax tree (AST) of the source program code, the correspondence between coordinate feature values and variable names is established, and a runtime variable snapshot dictionary is generated.
[0033] Specifically, this step includes two parallel analysis processes: First, memory reflection traversal. After the drawing source program finishes execution, the system performs reflection traversal on the geometric object instances in the runtime memory, extracting the coordinate feature values of each geometric object based on its type. The system enumerates all surviving geometric object instances in runtime memory through the reflection mechanism of the programming language, checking whether each instance belongs to the predefined geometric type set G={Line, Arc, Polyline, Text, ...}. Only instances belonging to G are included in the coordinate extraction process.
[0034] For each geometric object instance obj that passes type detection i The system determines the type T (obj) based on the type of the obj. i Extract coordinate feature values F(obj) according to the following rules. i For line segment objects, F(obj) i ) = {(x1, y1), (x2, y2)}, where x1 and y1 are the coordinates of the starting and ending points, respectively; for an arc object, F(obj ... i ) = {(c x c y ), r}, where r is the center coordinate and r is the radius, respectively; for a polyline object, F(obj) i )={(x1,y1),(x2,y2),...,(x n y n F(obj) is an ordered list of coordinates for each control vertex; for a text object, F(obj) is an ordered list of coordinates for each control vertex. i ) = {(a xa y `obj` contains the bounding box anchor point coordinates and the text string content. Each geometry object instance `obj`... i The extracted results are organized into triples R i =(ID(obj i ), T(obj i ), F(obj i ), where ID(obj) i This is a unique identifier for the instance in runtime memory. Each coordinate value is identified by its ID (obj). i It maintains an association with the instance of the object to which it belongs.
[0035] Second, static AST analysis. The system performs Abstract Syntax Tree (AST) parsing on the source code of the drawing program. The source code is converted into a tree-like syntax structure. The system traverses all nodes in the syntax tree, focusing on identifying two types of statements: assignment statements and function call statements, extracting the variable names assigned to each geometric object instance in the source program.
[0036] For an assignment statement node, its syntax can be represented as "varName = Expression". The system extracts the identifier varName from the left side of the assignment as the variable name and parses the type of the expression Expression on the right side of the assignment. When Expression is a geometry object constructor call (such as Line(...), Arc(...), Polyline(...)), the system associates varName with the object instance created by that constructor, recording it as a tuple A. j =(varName j ID(obj) j )), where ID(obj j () is the unique identifier of this object instance.
[0037] For function call statement nodes, the system recognizes method call expressions with a geometry object instance as the caller. When the right-hand side of an assignment statement is an object method call in the form "receiver.Method(args)", the system simultaneously records the variable name on the left-hand side of the assignment and the instance identifier of the called object. For example, in the source code "pile_top = center_line.GetPointByRatio(0.3)", the system recognizes the variable name "pile_top" on the left-hand side of the assignment, the called object "center_line", the method name "GetPointByRatio", and the parameter "0.3", associating the variable name "pile_top" with the geometry object instance returned by the method call.
[0038] The results of memory reflection traversal and AST static analysis are bound to the same object instance: memory reflection traversal records the object instance identifier and its coordinate feature values for each geometric object instance, while AST static analysis records the object instance identifier and variable name for each geometric object instance. The system performs a one-to-one matching using the object instance identifier as the association key, merging the coordinate feature values and variable names into a single snapshot dictionary entry. In the generated runtime variable snapshot dictionary, each entry contains coordinate values, coordinate types (start point, end point, center, etc.), and the corresponding code variable name, providing a query basis for subsequent spatial matching.
[0039] Step S3: Spatial matching and parameterized expression generation.
[0040] Spatial matching is performed between the primitive coordinates obtained in step S1 and the snapshot dictionary obtained in step S2 to deduce the variable names and parameterized expressions corresponding to the target coordinates.
[0041] Spatial matching is performed sequentially according to the following three-level strategy: Level 1, exact matching. Calculate the Euclidean distance between the target coordinates and the coordinates of each entry in the snapshot dictionary. Let the target coordinates be... The first variable in the runtime variable snapshot dictionary k The coordinates of each entry are The system calculates the Euclidean distance between the two: The system iterates through all entries in the snapshot dictionary and calculates the Euclidean distance between the target coordinates and the coordinates of each entry. d k And take the minimum distance d min = min{d1, d2, ..., d p} and its corresponding entries. When the minimum distance d min When the distance is less than a preset tolerance threshold (e.g., 0.01 mm), the target coordinates are considered to correspond to the same spatial location as the coordinates of the entry, and the parameterized expression "variable name" is directly generated. For example, if the target coordinates (1234.500, 5678.900) are 0.002 mm away from the variable "pile_top" in the snapshot dictionary, which is less than the tolerance threshold, then the expression "pile_top" is generated.
[0042] The second level is offset matching. When an exact match fails, the system switches to offset matching. Let the target coordinates be... The first snapshot in the dictionary The coordinates of the reference variables are The system calculates the vector difference. , The system then searches the engineering parameter set for parameters whose numerical values match the vector difference components. This set is derived from numerical variables in the runtime variable snapshot dictionary and design parameters registered in the global engineering parameter information database. When a matching offset parameter is found, the expression "baseline variable + offset parameter" is generated. For example, if the vector difference between the target coordinates and the coordinates of the variable "wall_base" is (300, 0), and the value of the engineering parameter "wall thickness" is 300 mm, then the expression "wall_base + wall thickness" is generated. If multiple candidate combinations exist, the combination with the fewest offset parameters is selected first. When no combination matches, the system proceeds to the third level of proportional matching.
[0043] The third level is proportional matching. If the first two levels of matching fail, the system enters proportional matching. Let the target coordinates be... The starting point of a line segment variable in the snapshot dictionary is The endpoint is The system calculates the projection scale factor of the target coordinates onto the line segment: The coefficient λ represents the normalized position of the target coordinates relative to the starting point along the line segment direction; λ = 0 corresponds to the starting point, and λ = 1 corresponds to the ending point. The coordinates of the projected point are calculated based on the projection scale factor. Then calculate the target coordinates. P t With projection point P' The distance between them is used as the projection error: When the projection error e is less than the tolerance threshold ε and the projection scale factor satisfies 0 ≤ λ ≤ 1, the target coordinates are considered to fall on the line segment variable, and the proportional point selection expression "line segment variable.GetPointByRatio(λ)" is generated. For example, if the projection scale factor λ = 0.3 on the line segment represented by the variable "center_line" and the projection error e < ε, then the expression "center_line.GetPointByRatio(0.3)" is generated.
[0044] Furthermore, before performing the aforementioned three-level matching, the system prioritizes querying the global engineering parameter information database. When the text content associated with a graphic element completely matches the string of a parameter value already registered in the global database, the text is directly replaced with the corresponding parameter variable name, skipping coordinate space matching. This priority query mechanism can quickly handle common scenarios such as design parameter value annotation.
[0045] Step S3 also calculates a confidence score for each matching result. The specific process is as follows: The confidence score for exact matches is the highest, determined by the Euclidean distance between the target coordinates and the coordinates of the matched entry; the smaller the distance, the higher the confidence score. The confidence score for offset matches is next, related to the number of offset parameters used; single-parameter offsets in one direction have a higher confidence score than two-parameter offsets in two directions. The confidence score for proportional matches is the lowest, determined by the projection error; the smaller the error, the higher the confidence score.
[0046] For results with a confidence level below a preset threshold, the system displays them as highlighted prompts in the user interface and allows users to manually confirm or correct the matching results.
[0047] Step S4: Intelligent generation of parameterized code.
[0048] After completing the spatial matching and parametric expression generation in step S3, the system inputs the matching results into the large language model to generate parametric drawing code. Specifically, the system uses the parametric expressions, primitive types, and associated text content corresponding to each primitive obtained in step S3 as context information, along with the mapping relationship between the runtime variable snapshot dictionary and the variable names in the global engineering parameter information library as constraints, and inputs them into the large language model. The large language model then generates parametric drawing code that references the actual variable names in the source program under the above constraints. The input context of the large language model includes the following: the parametric expressions corresponding to each primitive output in step S3; the geometric type (such as line segment, arc, text) and text content of the target primitive; the list of variable name and coordinate value mapping relationships extracted from the runtime variable snapshot dictionary; and the list of parameter name and parameter value mapping relationships extracted from the global engineering parameter information library. The latter two are input as parallel variable name constraints along with the context, jointly restricting the large language model to use only the variable names already existing in the drawing source program or the parameter names already registered in the global engineering parameters when generating code, and not to generate hard-coded coordinates or self-created variable names.
[0049] Once the user confirms the low-confidence matching result in step S3, the system automatically writes the mapping relationship of "coordinate value → parameter variable name" into the global engineering parameter information database, so that the global database continuously accumulates mapping entries as the number of uses increases, forming a progressive self-learning closed loop.
[0050] Example 2 Topological inference of station number and generation of parameterized code in the longitudinal section diagram of the tailrace branch pipe, such as Figure 3 , Figure 4 As shown, the working process of this invention is illustrated by taking the generation of parametric codes for station number markings in the longitudinal section diagram of the tailrace branch of a pumped storage power station as an example.
[0051] In hydraulic engineering, linear hydraulic structures such as canals, tunnels, and embankments are marked with station numbers along their axes, increasing by mileage. Station numbers are linear positioning markers that increment along the axis from the starting point. The standard format for station numbers is usually "K+M" (K is the kilometer number, M is the fractional meter number), for example, "0+035.000" indicates a position 35 meters from the starting point of the line. In drawing programs, station numbers are typically calculated by summing the length expressions of the base station variable and the line element variables along the line segment. Tailrace branch pipes are typical bifurcated structures in the tailrace system of hydropower stations. The main pipe leads the water flow from the turbine tailrace pipe and splits into two branches at the bifurcation, each leading to a different tailrace tunnel outlet. In drawing practice, the station number of the main pipe is shared by both branches, while each branch after the bifurcation is independently numbered and accumulated along its own axis. This topological feature of branching at station intersections means that the automatic inference of station parameterized expressions requires handling tree-like topological path search, rather than simple linear accumulation.
[0052] The designers have written a drawing source program to draw a longitudinal section of the tailrace branch pipe. The main variables defined in the source program include: the variable "base station" represents the station (0+000.000) of the main pipe inlet section, with coordinates (100, 300); the variable "main pipe section A" represents the first segment of the main pipe (starting from the base station), with a length of 20 meters and an end coordinate (120, 300); the variable "main pipe section B" represents the second segment of the main pipe (continuing from main pipe section A to the branch), with a length of 15 meters and an end coordinate (135, 300); at the branch, the main pipe splits into two branches: the variable "left branch pipe section C" represents the left branch pipe segment (starting from the branch), with a length of 12 meters and an end coordinate (147, 288); the variable "right branch pipe section D" represents the right branch pipe segment (starting from the branch), with a length of 18 meters and an end coordinate (153, 312). The benchmark station number "0+000.000" and its corresponding coordinates have been registered in the global engineering parameter information database.
[0053] In CAD, the designer sequentially selects two station label elements on the longitudinal section view: the first is located at the end of the left branch pipe, with text content "0+047.000" and anchor point coordinates (147, 288); the second is located at the end of the right branch pipe, with text content "0+053.000" and anchor point coordinates (153, 312). The designer wants the system to generate parametric drawing code for the two station labels respectively.
[0054] The operation process is as follows: (1) The system executes step S1. The system first confirms that the global engineering parameter information database is ready (in this embodiment, it has been constructed in the previous drawing task), and then obtains the information of the first selected graphic element: the graphic element type is "text", the text content is "0+047.000", and the anchor point coordinates are (147, 288).
[0055] (2) The system executes step S2, running the source program to generate a runtime variable snapshot dictionary. The dictionary records the coordinates of the reference station (100, 300), the starting point (100, 300) and ending point (120, 300) of the main pipe section A (length 20 meters), the starting point (120, 300) and ending point (135, 300) of the main pipe section B (length 15 meters), the starting point (135, 300) and ending point (147, 288) of the left branch pipe section C (length 12 meters), and the starting point (135, 300) and ending point (153, 312) of the right branch pipe section D (length 18 meters), and their corresponding variable names. Among them, the ending point of the main pipe section B, the starting point of the left branch pipe section C, and the starting point of the right branch pipe section D are all at the same coordinates (135, 300), forming a bifurcation node in the geometric topology diagram.
[0056] (3) The system executes step S3. First, the system recognizes that the graphic element text "0+047.000" conforms to the station number format, so it first queries the global engineering parameter information database. In the global database, it finds the variable name "base station number point" and coordinates (100, 300) corresponding to the base station number "0+000.000".
[0057] (4) In step S4, the system performs topology inference processing on the station number annotation elements. In the geometric topology map of the runtime variable snapshot, the system searches for a continuous geometric path to the current annotation anchor point coordinates (147, 288) starting from the reference station point coordinates (100, 300). At the fork node (135, 300), there are two branches in the topology map: the left branch segment C leads to (147, 288), and the right branch segment D leads to (153, 312). The system solves the endpoint coordinates for the two candidate paths respectively, determines that the path via the left branch segment C can reach the target anchor point (147, 288), and selects this path. The final path is: reference station point → main pipe segment A → main pipe segment B → left branch segment C. The system sequentially accumulates the length expressions of the line element variables of each segment on the path to generate the station number calculation expression: S L =S0+L A +L B +L C In the formula: S L S0 is the calculated value of the left branch pipe station number (m); S0 is the reference station number value (m); L A L B The lengths (m) of main section A and main section B are respectively; L C The length (m) of the left branch pipe segment C.
[0058] (5) The system inputs the context information into the large language model, and the large language model generates parametric drawing code for the left branch pipe station number annotation under variable name constraints: Chainage value = Base chainage point. Chainage + Main pipe section A. Length + Main pipe section B. Length + Left branch pipe section C. Length = chainage_text = str(int(chainage_value / 1000)) + " + " + format(chainage_value%1000,".3f") DrawText(station number text, position=left branch segment C.end point) (6) For the second station mark element "0+053.000" (anchor point coordinates (153,312)), the system repeats the above process. At the fork, select the path of the right branch pipe section D. The final path is: base station point → main pipe section A → main pipe section B → right branch pipe section D, and generate the station calculation expression: S R =S0+L A +L B +L D In the formula: S R The calculated value (m) for the right branch pipe station number; L D The length of the right branch pipe segment D is (m); the meanings of the other symbols are the same as those in the station number calculation expression in (5).
[0059] Comparing the station number calculation expression in (5) with that in (6), we can see that L A and L B The presence of this information in the chainage calculation for both branch pipes reflects the engineering semantics that the chainage of the main pipe section is shared by both branch pipes. The left and right branch pipes each reference different branch pipe section length variables (L). C With L D This allows for independent stationing of each branch pipe after the fork. When the length of the main pipe section or the reference station S0 is changed in the design, the stationing of both branch pipes is automatically updated accordingly; when the length of one branch pipe is adjusted, only the stationing of that branch pipe is affected, and the other branch pipe is unaffected.
[0060] In the above embodiment, since there are two candidate paths on the topology map at the junction, the system evaluates the confidence level of the path selection. If the Euclidean distance between the endpoint coordinates of the candidate path and the target anchor point coordinates is within the tolerance range (i.e., there are multiple valid paths), the system will prompt the user to confirm in the user interface. After the user confirms the correct path, the system automatically writes the "coordinate → variable name" mapping relationship on that path into the global engineering parameter information database, so that when encountering similar station markings in the same branch pipe structure in the future, the corresponding branch pipe path can be directly hit without repeated inference.
[0061] Example 3 Corresponding to the above method, the present invention also provides an intelligent generation system for parametric drawing codes of water conservancy projects based on variable snapshots and spatial matching, the system comprising: The graphic element acquisition module is used to acquire the geometric data of the graphic elements selected by the user from CAD software, including control point coordinates, graphic element type and associated text content; The snapshot generation module is used to execute the drawing source program in an isolated environment and collect a snapshot dictionary of runtime variables. The snapshot dictionary records the variable name and its corresponding geometric coordinate value of each variable during program execution. The spatial matching module is used to spatially match the coordinates of the graphic elements obtained by the graphic element acquisition module with the snapshot dictionary obtained by the snapshot generation module. It executes the three-level strategy of precise matching, offset matching and proportional matching in sequence, and reverse-engineers the variable name and parameterized expression corresponding to the target coordinates. The intelligent code generation module is used to input the parametric expressions, primitive types and text content obtained from the spatial matching module into the large language model, and the large language model generates parametric drawing code under variable name constraints. The global project parameter library module independently stores common project parameters in persistent storage, separate from runtime snapshots. These parameters are prioritized for querying by the space matching module and support external data import and progressive writing triggered by user confirmation.
[0062] The data flow and collaboration relationships between the above modules are as follows: Figure 1 and Figure 2 As shown.
[0063] In this embodiment, the snapshot generation module includes: a memory reflection traversal unit, an AST static analysis unit, and a binding unit; The memory reflection traversal unit is used to perform reflection traversal on the geometric object instances in the program's runtime memory after the drawing source program has finished executing, and extract the coordinate feature values of each geometric object according to its type; each coordinate value is associated with its corresponding geometric object instance. The AST static analysis unit is used to perform abstract syntax tree parsing on the source code of the drawing source program, traverse the assignment statements and function call statements in the syntax tree, and identify the variable names assigned to each geometric object instance in the source program. The binding unit is used to bind the results of the memory reflection traversal unit and the AST static analysis unit to the same object instance: the coordinate values obtained by memory reflection and the variable names obtained by AST analysis are associated with the same object instance identifier, thereby generating a runtime variable snapshot dictionary.
[0064] In this embodiment, the spatial matching module includes: a precise matching unit, an offset matching unit, and a proportional matching unit; The exact matching unit is used to calculate the Euclidean distance between the target coordinates and each entry in the snapshot dictionary. When the distance is less than the tolerance threshold, the expression "variable name" is generated. The offset matching unit is used to calculate the vector difference between the target coordinates and the coordinates of each reference variable when the exact match fails, retrieve the parameter with the same value as the vector difference in the engineering parameter set, and generate the expression "reference variable + offset parameter". The proportional matching unit is used to calculate the projection ratio of the target coordinates onto the snapshot dictionary line segment variable when neither exact matching nor offset matching is found, and to generate a proportional point selection expression with the line segment variable and the projection ratio coefficient as parameters.
[0065] In this embodiment, the spatial matching module further includes: a confidence calculation unit, a comparison unit, and a writing unit; The confidence score calculation unit is used to calculate the confidence score for each matching result. The comparison unit is used to prompt users in the user interface and allow them to manually confirm or correct results with a confidence level lower than a preset threshold. The write unit is used to automatically write the mapping relationship of "coordinate value → parameter variable name" into the global engineering parameter information database after user confirmation, so that the global engineering parameter information database can continuously accumulate mapping entries as the number of uses increases.
[0066] In this embodiment, the intelligent code generation module includes: an identification and positioning unit, a search unit, and an accumulation unit; The identification and positioning unit is used to identify strings in the graphic element text that conform to the station number format and locate the corresponding benchmark station number coordinates in the global engineering parameter information database. The search unit is used to search for a continuous geometric path from the base station point to the current annotation anchor point in the geometric topology map of the runtime variable snapshot; The accumulation unit is used to sequentially accumulate the length attributes of each line segment variable on the path, generating a station number calculation expression code composed of the base station number variable and the length of each line segment variable summed segment by segment.
[0067] The embodiments described above are merely preferred embodiments of the present invention and are not intended to limit the scope of the present invention. Various modifications and improvements made to the technical solutions of the present invention by those skilled in the art without departing from the spirit of the present invention should fall within the protection scope defined by the claims of the present invention.
Claims
1. A method for intelligently generating parametric drawing codes for hydraulic engineering based on variable snapshots and spatial matching, characterized in that, The method includes: S1: Build or update the global engineering parameter information database; obtain the geometric data of user-selected elements from CAD software, including control point coordinates, element type and associated text content; S2: Execute the drawing source program, establish the correspondence between coordinate feature values and variable names through reflection traversal of the program's runtime memory and static analysis of the source program code, and generate a runtime variable snapshot dictionary; S3: Spatial matching of control point coordinates with runtime variable snapshot dictionary to infer the variable name and parameterized expression corresponding to the target coordinates; S4: Input the parameterized expression, primitive type, and associated text content as context information into the large language model, and the large language model generates parameterized drawing code; the variable name mapping relationship in the runtime variable snapshot dictionary and the global project parameter information library is input as a constraint along with the context, so that the variable names in the code output by the large language model are consistent with the actual variable names in the drawing source program.
2. The method according to claim 1, characterized in that, In step S2, the method for executing the drawing source program, establishing the correspondence between coordinate feature values and variable names through reflection traversal of the program's runtime memory and static analysis of the source program code, and generating a runtime variable snapshot dictionary includes: S21: After the drawing source program finishes execution, the geometric object instances in the program runtime memory are traversed by reflection, and the coordinate feature values of each geometric object are extracted according to its type; each coordinate value is associated with its corresponding geometric object instance. S22: Perform abstract syntax tree parsing on the source code of the drawing source program, traverse the assignment statements and function call statements in the syntax tree, and identify the variable names assigned to each geometric object instance in the source program; S23: Bind the results of S21 and S22 to the same object instance: The coordinate values obtained by memory reflection and the variable names obtained by AST analysis are associated with the same object instance identifier, thereby generating a runtime variable snapshot dictionary.
3. The method according to claim 1, characterized in that, In step S3, the method for spatially matching the control point coordinates with the runtime variable snapshot dictionary to deduce the variable name and parameterized expression corresponding to the target coordinates includes: Exact match: Calculate the Euclidean distance between the target coordinates and each entry in the snapshot dictionary. If the distance is less than the tolerance threshold, generate the expression "variable name". Offset matching: When an exact match fails, calculate the vector difference between the target coordinates and the coordinates of each reference variable, search the engineering parameter set for parameters with the same value as the vector difference, and generate the expression "reference variable + offset parameter". Scale matching: When neither exact match nor offset match is found, calculate the projection scale of the target coordinates onto the line segment variable in the snapshot dictionary, and generate a scaled point expression with the line segment variable and the projection scale coefficient as parameters.
4. The method according to claim 1, characterized in that, S3 further includes: Calculate a confidence score for each matching result; For results with a confidence level below a preset threshold, a prompt will be displayed in the user interface, allowing the user to manually confirm or correct them; After user confirmation, the mapping relationship of "coordinate value → parameter variable name" will be automatically written into the global engineering parameter information database, so that the global engineering parameter information database will continue to accumulate mapping entries as the number of times it is used increases.
5. The method according to claim 1, characterized in that, Methods for inputting parametric expressions, primitive types, and associated text content as contextual information into a large language model, and then generating parametric drawing code from the large language model, include: Identify strings in the graphic element text that conform to the station number format, and locate the corresponding benchmark station coordinates in the global engineering parameter information database; Search for a continuous geometric path from the base station point to the current annotation anchor point in the geometric topology graph of the runtime variable snapshot; The length attributes of each line segment variable on the path are accumulated sequentially to generate a station number calculation expression code consisting of the sum of the base station number variable and the length of each line segment variable segment by segment.
6. A smart generation system for parametric drawing codes of hydraulic engineering based on variable snapshots and spatial matching, the system being used to implement the method described in any one of claims 1-5, characterized in that, The system includes: a graphic element acquisition module, a snapshot generation module, a global engineering parameter library module, a spatial matching module, and an intelligent code generation module; The global engineering parameter library module is used to build or update the global engineering parameter information library; The graphic element acquisition module is used to acquire the geometric data of the graphic elements selected by the user from the CAD software, including control point coordinates, graphic element type and associated text content; The snapshot generation module is used to execute the drawing source program, and through reflection traversal of the program's runtime memory and static analysis of the source program code, establish the correspondence between coordinate feature values and variable names, and generate a runtime variable snapshot dictionary; The spatial matching module is used to perform spatial matching between the control point coordinates and the runtime variable snapshot dictionary, and to infer the variable name and parameterized expression corresponding to the target coordinates. The intelligent code generation module is used to input parameterized expressions, primitive types, and associated text content as context information into the large language model, and the large language model generates parameterized drawing code. The variable name mapping relationship in the runtime variable snapshot dictionary and the global project parameter information library is input as a constraint along with the context, so that the variable names in the code output by the large language model are consistent with the actual variable names in the drawing source program.
7. The system according to claim 6, characterized in that, The snapshot generation module includes: a memory reflection traversal unit, an AST static analysis unit, and a binding unit; The memory reflection traversal unit is used to perform reflection traversal on the geometric object instances in the program runtime memory after the drawing source program has finished executing, and extract the coordinate feature values of each geometric object according to its type; each coordinate value is associated with its corresponding geometric object instance. The AST static analysis unit is used to perform abstract syntax tree parsing on the source code of the drawing source program, traverse the assignment statements and function call statements in the syntax tree, and identify the variable names assigned to each geometric object instance in the source program. The binding unit is used to bind the results of the memory reflection traversal unit and the AST static analysis unit to the same object instance: the coordinate values obtained by memory reflection and the variable names obtained by AST analysis are associated with the same object instance identifier, thereby generating a runtime variable snapshot dictionary.
8. The system according to claim 6, characterized in that, The spatial matching module includes: a precise matching unit, an offset matching unit, and a proportional matching unit; The precise matching unit is used to calculate the Euclidean distance between the target coordinates and each entry in the snapshot dictionary. When the distance is less than the tolerance threshold, the expression "variable name" is generated. The offset matching unit is used to calculate the vector difference between the target coordinates and the coordinates of each reference variable when the exact match fails, retrieve the parameter with the same value as the vector difference in the engineering parameter set, and generate the expression "reference variable + offset parameter". The proportional matching unit is used to calculate the projection ratio of the target coordinates onto the line segment variable in the snapshot dictionary when neither the current exact match nor the offset match is found, and to generate a proportional point selection expression with the line segment variable and the projection ratio coefficient as parameters.
9. The system according to claim 6, characterized in that, The spatial matching module further includes: a confidence calculation unit, a comparison unit, and a writing unit; The confidence score calculation unit is used to calculate the confidence score for each matching result; The comparison unit is used to prompt users in the user interface and allow them to manually confirm or correct results with a confidence level lower than a preset threshold. The writing unit is used to automatically write the mapping relationship of "coordinate value → parameter variable name" into the global engineering parameter information database after user confirmation, so that the global engineering parameter information database continues to accumulate mapping entries as the number of uses increases.
10. The system according to claim 6, characterized in that, The intelligent code generation module includes: an identification and positioning unit, a search unit, and an accumulation unit; The identification and positioning unit is used to identify strings in the graphic element text that conform to the station number format and locate the corresponding benchmark station number coordinates in the global engineering parameter information database. The search unit is used to search for a continuous geometric path from the base station point to the current labeled anchor point in the geometric topology map of the runtime variable snapshot. The accumulation unit is used to sequentially accumulate the length attributes of each line segment variable on the path to generate a station number calculation expression code composed of the base station number variable and the length of each line segment variable summed segment by segment.