CN-122020760-A - Cross-paradigm automatic conversion method, system and medium for parameterized geometric modeling script
Abstract
The invention discloses a cross-paradigm automatic conversion method, a system and a medium of a parameterized geometric modeling script, and the method aims at a semantic gap between an implicit construction entity geometry (CSG) description language (such as OpenSCAD) and an explicit boundary representation (B-rep) programming language (such as CadQuery/Python) and provides a conversion framework based on double-layer Abstract Syntax Tree (AST) mapping and state stack management. The method comprises the steps of adopting a recursive structure mapping mechanism to deduce logic of an implicit function list in a source script, decoupling and reconstructing the logic into an explicit command type circulating control flow in a target language, solving the problem of isolation and recombination of a hierarchical scope by utilizing a statement container stack, identifying specific geometric combinations (such as cubes and spheres in Minkowski and operation) through semantic analysis, and reconstructing the specific geometric combinations into efficient function calls based on feature modeling (such as chamfering). The method not only reserves the parameterized design intent of the original script, but also remarkably improves the calculation efficiency of the generated model in the B-rep kernel.
Inventors
- ZHANG YOUBAO
- DONG ZUOREN
Assignees
- 中国科学院上海光学精密机械研究所
Dates
- Publication Date
- 20260512
- Application Date
- 20260414
Claims (11)
- 1. The cross-paradigm automatic conversion method of the parameterized geometric modeling script is characterized by comprising the following steps of: s1, multi-file recursion analysis: Identifying the type of the current reference instruction in response to an analysis request of the source script based on the implicit construction entity geometric paradigm and the external dependency file referenced by the source script; If the reference instruction is a full-quantity containing instruction, merging all abstract syntax tree nodes of the target file into a current abstract syntax tree; If the reference instruction is a definition import instruction, only screening and combining module definition nodes and function definition nodes in the target file, and filtering a top-level geometric instantiation statement in the target file; Based on the merging result, constructing a unified source abstract syntax tree containing multiple file dependency relationships; S2, initializing a state, namely initializing a module object of a target language, and establishing a statement container stack and a global result lookup table, wherein the statement container stack is used for storing target abstract syntax tree node sequences corresponding to different nesting levels, and the global result lookup table is used for storing mapping relations between source abstract syntax tree node identifiers and generated target language variable names; s3, a scope isolation and intermediate representation generation step: Traversing the nodes of the unified source abstract syntax tree, and performing the following operations: in response to traversing to the nested scope node, pushing a new target abstract syntax tree node sequence to the stack top of the statement container stack, and creating an independent code generation scope for the current nested level; Invoking a corresponding processing function according to the type of the current node, wherein the processing function instantiates an intermediate representation node object of the target language, and the intermediate representation node object is a package of a grammar element of the target language; Writing the intermediate representation node object generated by instantiation into a target abstract syntax tree node sequence at the current stack top, so as to realize code isolation among different-level acting domains; in response to the completion of the traversal of the current nested scope node, executing a pop operation, popping up a target abstract syntax tree node sequence corresponding to the nested scope node from the statement container stack, packaging the target abstract syntax tree node sequence into a code block node of a target language, and mounting the code block node to a parent level abstract syntax tree node; S4, geometric flow explicit tracking: Generating a globally unique target language variable name for a node in response to traversing to a source abstract syntax tree node generating a geometric entity, and storing a mapping relationship between a unique identifier of the node and the target language variable name into the global result lookup table; Responding to traversing to the Boolean operation node, traversing all the child nodes, inquiring the global result lookup table according to the unique identifier of each child node, and obtaining a corresponding target language variable name list; Generating an explicit Boolean operation function call expression or a binary operation expression taking the variable name list as a parameter, and assigning an evaluation result of the expression to a newly generated global unique variable name; Storing the mapping relation between the unique identifier of the Boolean operation node and the newly generated variable name into the global result lookup table; S5, code generation: And in response to the unified source abstract syntax tree traversing, recursively calling a serialization method of each intermediate representation node object to generate executable script codes of a target language.
- 2. The method of claim 1, wherein the multi-file recursive parsing step further comprises: maintaining a set of accessed file paths; Querying whether a path of a current target file exists in the accessed file path set before performing recursive analysis on the target file; if so, terminating the current recursion branch to avoid infinite recursion due to the loop reference; If not, adding the path of the current target file into the accessed file path set, and continuing to execute recursion analysis.
- 3. The method for automatic conversion of cross-paradigm of parameterized geometric modeling script according to claim 1, wherein, The statement container stack is a last-in first-out data structure, each element in the stack is a sequence container for storing intermediate representation node objects, the push operation is triggered when traversing to at least one of a module definition node, a Boolean operation node, a geometric transformation and modification node and a control flow structure node, the pop operation is triggered when exiting from the nested scope node of the type, in the packaging operation, a popped target abstract syntax tree node sequence is packaged into a target language code block structure matched with the type of the nested scope node, and the code block structure comprises at least one of a module definition body, a function definition body, a loop body, a condition judgment body and a general statement block.
- 4. The method of claim 1, wherein the Boolean operation nodes comprise at least one of a union operation node, a difference operation node and an intersection operation node.
- 5. The method for automatic conversion of cross-paradigms of parameterized geometric modeling scripts of claim 1, further comprising the steps of Minkowski and operational semantic optimization: responding to traversing to Minkowski and operation nodes, traversing all the child nodes in the scope, and extracting the geometric type and size parameters of each child node; Judging whether the number of the child nodes and the geometric type combination belong to a preset specific geometric pair mode library or not; if the judgment result is yes, setting a corresponding auxiliary function requirement zone bit, and injecting an intermediate representation node object for calling a preset optimized auxiliary function into the head of the target script in the code generation step; The preset optimization auxiliary function is configured to use a boundary to represent chamfering operation or offset operation of the B-rep kernel to equivalently replace general Minkowski Boolean convolution operation; If the result of the determination is negative, an intermediate representation node object calling the general Minkowski function is generated.
- 6. The method of claim 5, wherein the predetermined specific geometric pair mode library comprises at least one or more of a combination of geometric types of cubes and spheres, cubes and cylinders, cylinders and spheres.
- 7. The method of claim 1, further comprising the step of command expansion of the list of functions to derive a structure of the function list: generating an intermediate representation node object of the list initialization statement at the head of the current scope in response to traversing to the functional list derivation node; Traversing the nested substructures of the functional list deduction nodes by adopting a recursion descent method, and executing structured mapping according to the type of the current sub-node: If the current child node is an iteration node, generating a node object represented in the middle of a loop structure of the target language, and pressing into a new sentence container; If the current child node is a condition filtering node, generating a condition judgment structure intermediate representation node object of the target language, and pressing into a new statement container; if the current child node is a local variable binding node, generating an assignment statement intermediate representation node object of the target language; responding to the pure expression node traversed to the innermost layer, generating an intermediate expression node object for calling a list adding method, and adding the calculation result of the current expression as a parameter into a list object initialized by the list initialization statement; And in the return process of the recursion descent traversal, sequentially executing a pop operation, packaging each inner layer statement container into a loop body or a condition judgment body, and mounting the loop body or the condition judgment body to a corresponding parent control flow node.
- 8. The method for cross-paradigm automatic conversion of parameterized geometric modeling scripts of claim 1, further comprising a dynamic child node scope mapping step of: A definition side injection sub-step, responding to traversing to a module definition node, when a function definition middle representing node object of a target language is generated, forcedly injecting a list type shape parameter at the tail of a parameter list defined by the function, and setting a default value as an empty list for the shape parameter; The calling side packaging sub-step is that all geometric entity generating nodes in a sub-scope are recursively processed in response to traversing to a module calling node which comprises the sub-scope, target language variable names generated for all geometric entities through the geometric flow explicit tracking step of claim 1 are obtained, a list construction intermediate representation node object is generated, the obtained target language variable name list is packaged into a list object, and the list object is transmitted to a shape parameter of a list type as a keyword real parameter when the function call intermediate representation node object is generated.
- 9. A cross-paradigm automatic conversion system of parameterized geometric modeling scripts for converting source scripts based on a structured entity geometry CSG paradigm into executable target scripts based on a boundary representation B-rep paradigm, comprising: An abstract syntax tree AST parsing module configured to receive a Source script file written in a first programming language, perform lexical analysis and syntax analysis on the Source script, and generate a Source abstract syntax tree Source AST representing a program structure thereof; A conversion engine communicatively coupled to the abstract syntax tree AST parsing module configured to traverse the source abstract syntax tree and to convert geometric operation semantics in a source language into equivalent imperative statements in a target language based on predefined node mapping rules, thereby constructing a target abstract syntax tree TARGET AST, wherein the conversion engine comprises: -a statement container stack for dynamically maintaining the scope level of the current code generation during traversal; -a global result look-up table for storing the mapping relationship between each geometry generating node in the source abstract syntax tree and its corresponding variable identifier in the object code; an intermediate representation factory module configured to generate an intermediate representation node object conforming to a second programming language syntax specification according to instructions of the conversion engine, the intermediate representation node object including script module definitions, library importation instructions, function definitions, assignment statements, function calls, control flow structures, and base expression objects; a code serializer configured to serialize the target abstract syntax tree into an executable target script file in accordance with a format specification of a second programming language.
- 10. The system for cross-paradigm automatic conversion of parameterized geometric modeling scripts of claim 9, wherein the abstract syntax tree AST parsing module comprises: the full-quantity merging unit is configured to perform lexical marking and grammar analysis on the source script codes to construct a source abstract grammar tree node object with a hierarchical structure; A definition import unit configured to identify a full inclusion instruction, recursively parse the object file in response to the instruction and embed the generated abstract syntax tree nodes directly into the current syntax tree position; And the loop protection unit is configured to maintain the accessed file path set, check the target file path again before performing recursive analysis, and terminate the current recursive branch when the path is repeated.
- 11. A computer readable storage medium, on which a computer program is stored, characterized in that the computer program, when being executed by a processor, implements the method of any one of claims 1 to 8.
Description
Cross-paradigm automatic conversion method, system and medium for parameterized geometric modeling script Technical Field The invention belongs to the technical fields of Computer Aided Design (CAD), computer graphics and compiling, and particularly relates to a method, a system and a computer readable storage medium for converting a modeling script (such as OpenSCAD) based on implicit construction entity geometry (CSG) description into a programming modeling script (such as CadQuery/Python) based on an explicit boundary representation (B-rep) kernel. Background In the field of programming CAD modeling, openSCAD has a huge user base and open source model library due to the simple script grammar and modeling concept based on construction entity geometry (CSG). However, the geometric kernel of OpenSCAD has low efficiency when processing complex boolean operations, and the implicit geometric definition mode thereof causes that advanced operations based on topological features such as chamfering (Fillet), shell extraction (Shell) and the like are difficult to be performed on the model, and generally only discretized STL grids can be derived, thereby losing parameterized design intent. In contrast, the Python-based CadQuery framework employs an explicit boundary representation (B-rep) kernel (OpenCASCADE), supports powerful feature modeling and parameterized queries, and can be directly integrated into the Python-rich scientific computing ecology. However, there is a fundamental semantic gap between OpenSCAD and CadQuery, the former using implicit, functional, CSG tree-based modeling paradigm, and the latter using explicit, imperative, B-rep-based feature modeling paradigm. This difference makes the migration of the existing OpenSCAD model to CadQuery almost completely dependent on manual rewriting, and is inefficient, high in error rate, and poor in maintainability. Currently, there are few automatic changeover tools, but the following technical drawbacks exist: 1. Engineering level multi-file dependencies cannot be handled-existing tools typically do not support differential importation of include and use, and cannot handle complex library references. 2. The semantic mapping is incomplete, and complex universal matrix transformation cannot be correctly converted (multmatrix). 3. Performance bottleneck-blind transition boolean operations lead to B-rep kernel computation crashes. In summary, the prior art has not yet achieved fully automatic, semantically complete, performance optimized cross-paradigm conversion from OpenSCAD to CadQuery. Therefore, there is a need for a cross-paradigm conversion method that can understand geometry in depth, automatically process hierarchical scopes and file dependencies, and perform algorithmic optimization for B-rep kernels. Disclosure of Invention The invention aims to solve the problem of semantic gap in the prior art when OpenSCAD scripts are converted into CadQuery scripts, in particular to solve the problem of multi-file dependent differential analysis, hierarchical nested scope mapping, implicit geometric flow explicit, general matrix transformation and performance optimization of specific operations (such as Minkowski). In order to solve the technical problems, the invention provides a parameterized geometric modeling script cross-paradigm automatic conversion method based on double-layer Abstract Syntax Tree (AST) mapping and state stack management. The method achieves semantic level conversion from an implicit CSG paradigm (OpenSCAD) to an explicit B-rep paradigm (CadQuery/Python) by constructing a complete conversion pipeline that includes an Abstract Syntax Tree (AST) parsing module, a conversion engine, an intermediate representation factory module, and a code generator. The technical scheme of the invention is as follows: In one aspect, the invention provides a cross-paradigm automatic conversion method of a parameterized geometric modeling script, which is characterized by comprising the following steps: s1, multi-file recursion analysis: Identifying the type of the current reference instruction in response to an analysis request of the source script based on the implicit construction entity geometric paradigm and the external dependency file referenced by the source script; If the reference instruction is a full-quantity containing instruction, merging all abstract syntax tree nodes of the target file into a current abstract syntax tree; If the reference instruction is a definition import instruction, only screening and combining module definition nodes and function definition nodes in the target file, and filtering a top-level geometric instantiation statement in the target file; Based on the merging result, constructing a unified source abstract syntax tree containing multiple file dependency relationships; S2, initializing a state, namely initializing a module object of a target language, and establishing a statement container stack (STATEMENT CONTAINER STACK