Python-based drawing field language design method and system
Through the Python-based drawing field language design method, the textX library is used to define DSL and call the Turtle library for drawing, which solves the problems of existing DSL functionality limitations and insufficient ease of use, and achieves an efficient, flexible and personalized drawing experience.
Patent Information
- Application Number
- CN202411615771.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-13
- Publication Date
- 2025-04-15
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Existing independent drawing domain-specific language (DSL) functionality limitations, steep learning curve, poor integration, insufficient ease of use and insufficient documentation and support, resulting in inefficient users, learning difficulties and integration difficulties when dealing with complex graphics.
The drawing domain language design method developed based on Python, uses the textX library to define the domain-specific language (DSL), generates an abstract syntax tree (AST), instantiate the syntax and metamodels through an interpreter, call Python's Turtle graphics library for drawing, and provides a language parser, drawing processor, geometry transformation tool and extension interface.
It realizes a low threshold, high flexibility and personalized drawing experience, improves the automation and intelligence of graphics operations, enhances the efficiency of graphics drawing and dynamic operation capabilities, and reduces the time cost of users' learning and use.
Smart Images

Figure CN119829029A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to but is not limited to the field of specific language technology in the field of drawing, and in particular relates to a Python-based drawing field language design method and system. Background Art
[0002] In the field of modern engineering design and scientific research, professional drawing and simulation tools are indispensable. These tools, such as AutoCAD and Ansys CFX, are widely used in many fields such as architecture, machinery, fluid power, etc. They provide powerful drawing functions and advanced data processing capabilities, allowing engineers to design, analyze and optimize efficiently. At the same time, many mature tools are equipped with domain-specific languages (DSL) to enhance the user's drawing experience and simplify the operation steps. However, with the advancement of technology and the diversification of needs, the market demand for more flexible and personalized tools has gradually increased, prompting the development of DSL independent of specific tools.
[0003] Independent graphics domain-specific languages are designed to provide users with flexibility and personalized design capabilities that are different from existing tools. Such languages hope to enable users to create graphics and visualize data more efficiently by abstracting and simplifying the operation process. However, existing independent DSLs are not yet mature, and many projects still face many technical challenges.
[0004] In view of the above analysis, the prior art has the following technical problems that need to be solved urgently:
[0005] (1) Functional limitations: Existing independent DSLs usually cannot cover all complex drawing requirements and cannot compete with mature tools, which makes users face cumbersome operations and low efficiency when processing complex graphics;
[0006] (2) Steep learning curve: For non-professional users, the custom languages of existing technologies are often difficult to use, which affects their wide applicability. This high learning threshold hinders user adoption;
[0007] (3) Poor integration: Existing independent DSLs have weak integration capabilities with other mature tools and cannot be seamlessly integrated with existing workflows, resulting in low efficiency;
[0008] (4) Insufficient usability: The interface design lacks friendliness and the user interaction experience is generally poor, which affects drawing efficiency and accuracy;
[0009] (5) Insufficient documentation and support: Due to the lack of detailed documentation and support, users often have no way to seek help when faced with problems, causing confusion in operations. Summary of the invention
[0010] In view of the problems existing in the prior art, the present invention provides a drawing field language design method and system based on Python.
[0011] The present invention is implemented as follows: a drawing domain language design method based on Python, characterized in that the drawing domain language design method based on Python uses the textX library published in Python to define a domain specific language (DSL) and generate a corresponding abstract syntax tree (AST), instantiates the syntax and metamodel through an interpreter to obtain the entire drawing scene object, and the interpreter parses the instructions in the drawing language syntax according to the metamodel as a rule, and calls Python's Turtle graphics library to draw an image.
[0012] Furthermore, the method for parsing the drawing language syntax includes the following steps:
[0013] S1: Write the required drawing syntax according to the user documentation;
[0014] S2: Run the code and pass the grammar file into the interpreter;
[0015] S3: pass the metamodel file into the interpreter;
[0016] S4: The interpreter instantiates the drawing scene object;
[0017] S5: Analytically defined graph;
[0018] S6: Parse the drawing execution instruction, and store the operation graphic object of the execution instruction into the shape list. The attributes of each element in the shape list include the name, the starting drawing point and the set of all vertices, and call Turtle drawing;
[0019] S7: Parse the graphics conversion execution instruction, read the vertex set of the graphics object and perform the graphics transformation algorithm conversion of translation, rotation and scaling, and then call Turtle drawing;
[0020] S8: Determine whether there is a new execution instruction, if yes, return to S6, if no, proceed to the next step;
[0021] S9: Determine whether there is a new graphic definition, if yes, return to S5, if no, end.
[0022] Furthermore, the graphics transformation algorithm of S7 is specifically as follows:
[0023] (1) Input the transformation instruction attributes of the scene object, the transformed graphic object, and the transformation method (translation, rotation, and scaling);
[0024] (2) Parsing the translation, rotation and scaling parameters of the transformation instructions;
[0025] (3) passing the transformation parameters and the vertex set of the graphics object into the transformation function;
[0026] (4) Normalize the transformation parameters into a 3×3 homogeneous transformation matrix and normalize the vertex set into an n×3 homogeneous transformation matrix;
[0027] (5) Multiply the transformation parameter matrix and the vertex set matrix to obtain the transformed matrix, and return the transformed new vertex set;
[0028] (6) If the graphic object has both straight lines and curves, the vertex set of the graphic object is a three-dimensional list, that is, it contains two vertex lists, each of which performs vertex transformation;
[0029] (7) Call the Turtle library to traverse the vertex set to draw the transformed graphics. If there are both straight lines and curves, traverse and draw them separately.
[0030] Further, the following steps are included:
[0031] (1) Define the scene object name "scene";
[0032] (2) Declare the instructions to be executed in the scene object and add the instructions and their corresponding code files to the scene's attribute list;
[0033] (3) define attribute values for each instruction so that each instruction can be regarded as an object and contains its attributes;
[0034] (4) The interpreter calls the attributes and generates a metamodel file according to the structure defined by the scenario and uses it for graphics processing.
[0035] Further, the method comprises the following steps:
[0036] (1) Parse the syntax tree to obtain the drawing instruction list of the metamodel instantiation object;
[0037] (2) Parse the drawing instructions and generate the drawn graphics object and its vertex set according to the attributes of the instructions;
[0038] (3) Generate graphics that conform to the instructions using the line size, color, and fill color attributes of the graphic object;
[0039] (4) According to the vertex data structure, traverse the vertex sets of straight lines and curves, and call the drawing algorithm to generate the target graphics respectively.
[0040] Further, the method comprises the following steps:
[0041] (1) The drawing point obtained by command analysis is used as the center point O of the regular polygon, and the size of each internal angle is calculated as θ = 360° / pointnum;
[0042] (2) Use trigonometric formulas to obtain the coordinates of the first vertex;
[0043] (3) Generate the remaining vertices based on the center point and the first vertex, and complete the generation of the regular polygon vertex set through iteration;
[0044] (4) Use the generated vertex set to draw a regular polygon.
[0045] Further, the algorithm comprises the following steps:
[0046] (1) Inputting the conversion instruction of the scene object, the graphic object to be converted and the conversion method;
[0047] (2) Analyze the transformation parameters and normalize the translation, rotation, and scaling transformations into a 3×3 homogeneous transformation matrix;
[0048] (3) Multiply the vertex set matrix by the homogeneous transformation matrix to obtain a new vertex set;
[0049] (4) Call the drawing function and use the new vertex set to generate the transformed graphics.
[0050] Another object of the present invention is to provide a Python-based drawing domain language design system, which packages a drawing domain specific language (DSL) into a Python package, including a language parser, a drawing processor, a geometric transformation tool and an extension interface. The functional units of each module are specifically:
[0051] Language parsing module: responsible for parsing the DSL script entered by the user and converting it into executable Python objects or instructions;
[0052] Graphics drawing module: Based on Python's Turtle library, it converts parsed instructions into graphics operations and displays them on the screen;
[0053] Geometric transformation module: supports geometric transformation operations such as translation, rotation, and scaling, and processes the vertex set of the graphics;
[0054] Extension interface module: provides an API that allows users to extend the functionality of the DSL through Python code, adding custom graphics, special drawing algorithms, and other functions.
[0055] Furthermore, the specific implementation process of the Python-based drawing domain language design system is as follows:
[0056] (1) The user inputs the grammar file, which can be input through interactive programming or scripted programming entry;
[0057] (2) The input syntax file enters the code interpreter instantiation module, which parses the input syntax file through the interpreter file and creates an internally identifiable syntax object. The syntax object contains information such as the definition of the graphic object and image attributes;
[0058] (3) The created grammar object enters the graphics drawing module, and calls the turtle image library to draw graphics according to the information contained in the grammar object;
[0059] (4) The drawn graphic object enters the graphic vertex generation module, calculates the vertex information of the graphic according to the graphic object, stores the graphic vertices in the graphic vertex set, and then puts all the image information into the graphic saving module, and finally saves the image into the image set.
[0060] In combination with the above technical solutions and the technical problems solved, the advantages and positive effects of the technical solutions to be protected by the present invention are as follows:
[0061] First, low threshold: Developed based on Python, the entry threshold is low and the language is easy to read.
[0062] Diversity: It has two programming methods: scripted and interactive programming to adapt to users' diverse programming scenarios.
[0063] Personalized experience: Provide users with a more personalized drawing experience. It has the function of drawing straight lines, curves and complex graphics combined with their combinations. The syntax is similar to that of the native language.
[0064] Flexibility: Introducing a machine learning translation model to improve the flexibility of the drawing language.
[0065] Expansibility: The present invention has high integration and open interfaces, and can be integrated into other large-scale software for use as a scripting language.
[0066] Rich parsing corpus: Supports users to input through natural language, and the system can load relevant graphics from the corpus and parse the drawings.
[0067] New drawing domain language: A new language oriented to the drawing domain was designed.
[0068] Enhanced functions: The Turtle module is used for drawing. Since Turtle cannot draw curves, support for complex graphics such as arcs is added, and the angles of different units of measurement can be handled.
[0069] User experience optimization: In interactive programming, it provides functions such as code completion, theme switching, and automatic addition of closed lines. At the same time, graphics can be modified and transformed in real time, which greatly improves the user experience. The Turtle graphics library can be used to reproduce the drawing process.
[0070] Second, the expected benefits and commercial value of the technical solution of the present invention after transformation are as follows: by providing a more intuitive and easy-to-use drawing language, the time cost of user learning and use is reduced, and the design and development efficiency is improved. Personalized drawing experience and graphic transformation functions can attract more users and increase market share. Python-based open source tools reduce the cost of software development and maintenance, allowing more small and medium-sized enterprises and individual developers to use this technology to innovate. Open interfaces allow seamless integration with other large software, increase the flexibility and applicability of the system, and provide users with more value-added services. Due to the wide popularity and low threshold of the Python language, the present invention can be used as an educational tool to help students quickly master complex drawing skills and open up new education and training markets.
[0071] The technical solution of the present invention fills the technical gap in the industry at home and abroad: the existing specific languages in the field of drawing mostly rely on specific tools or platforms, while the present invention provides an independent solution that can be flexibly applied in different environments. By introducing a machine learning translation model, the present invention allows users to operate through more natural language input, which is relatively rare in existing drawing tools. The present invention not only supports basic graphics drawing, but also provides a unified geometric transformation framework, including translation, rotation, scaling, etc., which is not common in existing independent DSLs. The design of the present invention takes into account compatibility issues under different operating systems to ensure that users can use this technology in a variety of environments.
[0072] The technical solution of the present invention solves a technical problem that people have been eager to solve but have never succeeded in solving: traditional drawing tools often require cumbersome operating steps when processing complex graphics, and the present invention simplifies this process by defining DSL, allowing users to achieve complex graphics drawing with less code. Many users want to have highly customized drawing tools, but existing tools often lack sufficient flexibility. The present invention meets the personalized needs of users through customized DSL. Existing drawing tools are usually not intuitive and inefficient when processing graphic transformations. The present invention efficiently realizes the translation, rotation and scaling of graphics through matrix operations, thereby improving drawing efficiency. For non-professional users, learning to use professional drawing tools is a challenge. The present invention is developed based on Python, has a low entry threshold, is easy to use, and solves the problem that it is difficult for non-professionals to master drawing skills.
[0073] The technical solution of the present invention overcomes technical prejudice: some people believe that DSL is only suitable for niche applications in specific fields, but the present invention demonstrates the strong potential of DSL in the field of general drawing, breaking this cognitive limitation. Some people believe that Python is not suitable for high-performance drawing applications, but the present invention proves that Python can also achieve excellent drawing performance through optimized algorithms and reasonable design. Traditional drawing tools often ignore the importance of interactive programming, and the present invention greatly improves the user experience by supporting interactive programming, emphasizing the importance of real-time feedback and instant modification. The Turtle graphics library originally did not support functions such as complex curve drawing, but the present invention overcomes this limitation by expanding the functions of Turtle, demonstrating how to make up for the shortcomings of existing tools through technological innovation.
[0074] Third, the technical solution of the present invention solves the problems existing in the prior art that the complex drawing system is difficult to adapt to diversified drawing needs, the graphics operation is difficult to flexibly expand, and the graphics processing efficiency is low through a Python-based drawing field language design method.
[0075] First, the present invention uses the textX library to define a domain-specific language (DSL), and parses the user-defined drawing language syntax through the abstract syntax tree (AST), thereby achieving highly flexible drawing scene configuration. Compared with the traditional hard-coded drawing method, the present invention allows users to define complex graphic objects and conversion operations through concise syntax, greatly simplifying the graphic design process. This design method based on domain language solves the problem that the drawing syntax in the prior art is not flexible enough and difficult to expand, so that users can easily adapt to different scene requirements.
[0076] Secondly, the present invention significantly improves the automation and intelligence of graphics operations. By introducing an interpreter, the definition, parsing, conversion and drawing processes of graphics can be automatically processed. In particular, the automatic parsing and execution of geometric transformations such as translation, rotation, and scaling allow users to focus more on logical operations when designing graphics without having to deeply understand the underlying drawing implementation. This is difficult to achieve in the prior art, and users usually need to manually write cumbersome codes to complete similar operations.
[0077] In addition, the present invention solves the limitations of existing drawing systems in terms of execution efficiency and visualization effects by combining with Python's Turtle graphics library. The Turtle library has rich drawing functions, can quickly draw user-defined graphics, and implement dynamic graphics operations through an interpreter, greatly improving drawing efficiency. In contrast, traditional graphics processing systems are often inefficient when facing complex graphics transformations, and it is difficult to present the transformed effects in real time.
[0078] Therefore, the present invention has not only achieved significant technical progress in terms of flexibility in graphic design and automation of operation, but also achieved results that are difficult to match with the prior art in terms of improving the efficiency of graphic drawing and dynamic operation. These technical advances provide a strong advantage for the application of the present invention in the industry, and are particularly suitable for application scenarios that require efficient drawing and dynamic operation of graphics, such as education, graphic design, data visualization, etc. BRIEF DESCRIPTION OF THE DRAWINGS
[0079] Figure 1 is a flow chart of a method for parsing a drawing language grammar provided by an embodiment of the present invention;
[0080] Figure 2 It is a specific implementation flow chart of the Python-based drawing domain language design system provided by an embodiment of the present invention;
[0081] Figure 3 is a software flow chart provided by an embodiment of the present invention; Figure 4 It is a schematic diagram of obtaining coordinates of the remaining vertices of a polygon through iterative calculation provided by an embodiment of the present invention; Figure 5 A schematic diagram of generating a new polygon by transforming instructions provided by an embodiment of the present invention; Figure 6 It is a curve drawing effect diagram of a custom graphic provided by an embodiment of the present invention; Figure 7 is a comparison chart of Turtle and PyDL learning curves provided by an embodiment of the present invention; Figure 8 This is a comparison chart of the number of lines of code and time consumption of Turtle and PyDL provided by an embodiment of the present invention; Fig. 9 This is a comparison chart of the mastery changes of Turtle and PyDL after they have not been used for a long time, provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0082] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0083] An embodiment of the present invention provides a Python-based drawing domain language design method, including a domain-specific language metamodel module and an interpreter module. The domain-specific language (DSL) is defined by using textX published in Python and a corresponding abstract syntax tree (AST) is generated. The syntax and metamodel are instantiated by the interpreter to obtain the entire drawing scene object. The interpreter parses the instructions in the drawing language syntax according to the metamodel as a rule, that is, parses the attributes of the scene object, and calls the Python Turtle graphics library to draw an image.
[0084] The Python-based drawing domain language design method mainly uses the textX framework to define a domain-specific language (DSL) and generate the corresponding abstract syntax tree (AST). With this method, users can write drawing instructions with simple custom syntax, and the interpreter will be responsible for parsing these instructions and converting them into graphics. This method particularly relies on Python's Turtle library for graphics drawing. The entire process is centered on the parsing of the domain language, and combines steps such as graphics conversion and storage to achieve complex drawing functions.
[0085] First, users write DSL codes that define graphics and execute instructions based on the provided user documentation. These DSL codes contain specific graphics elements and their attributes, such as names, vertex coordinates, etc. Users do not need to care about the underlying implementation details. Next, the interpreter parses these DSL codes into abstract syntax trees by loading syntax files and metamodel files. Through the parser, syntax instructions and metamodels are instantiated into drawing scene objects, which will serve as the basis for drawing operations.
[0086] Next, the interpreter starts processing specific drawing instructions. It parses the user-defined graphics and generates corresponding graphics objects. These graphics objects not only contain names and starting points for drawing, but also a collection of all vertices. All objects in this parsing process are stored in a shape list as the basis for subsequent drawing and conversion operations. By calling Python's Turtle library, these graphics objects are drawn to the screen one by one according to the instructions.
[0087] After the graphics are defined, the interpreter will also process the graphics transformation operations. These transformation operations include geometric transformations such as translation, rotation, and scaling of the graphics. The interpreter calculates the vertex set of the graphics, completes these transformations, and then calls the Turtle library again to present the new graphics. The whole process realizes the dynamic adjustment of the graphics, which can be reflected in the final drawing result.
[0088] To ensure the continuity of the drawing process, the interpreter will loop to determine whether there are new execution instructions and graphics definitions. Once new instructions are detected, the interpreter will continue to parse and execute these instructions to ensure that all operations can be executed smoothly. If there are no new instructions, the entire drawing process is nearing its end.
[0089] Finally, when the interpreter completes the parsing and execution of all instructions, the entire drawing process ends. Through this design, users can complete complex graphics drawing through concise domain language syntax, while the interpreter is responsible for converting high-level instructions into specific graphics operations, achieving a combination of ease of use and scalability.
[0090] like Figure 1 As shown, the method for parsing the drawing language syntax includes the following steps:
[0091] S1: Write the required drawing syntax according to the user documentation;
[0092] S2: Run the code and pass the grammar file into the interpreter;
[0093] S3: pass the designed metamodel file into the interpreter;
[0094] S4: The interpreter instantiates the drawing scene object;
[0095] S5: Analytically defined graph;
[0096] S6: Parse the drawing execution instruction, call the graphics drawing algorithm such as the basic geometric drawing algorithm, curve drawing algorithm and regular polygon drawing algorithm, and store the graphics object of the execution instruction into the shape list. The attributes of each element in the shape list include the name, the starting drawing point and the set of all vertices [(x1, y1), (x2, y2), ...], and call Turtle drawing;
[0097] S7: Parse the graphics conversion execution instruction, read the vertex set of the graphics object and perform the graphics transformation algorithm conversion of translation, rotation and scaling, and then call Turtle drawing;
[0098] S8: Determine whether there is a new execution instruction, if yes, return to S6, if no, proceed to the next step;
[0099] S9: Determine whether there is a new graphic definition, if yes, return to S5, if no, end.
[0100] 1. Grammar writing and file input
[0101] The first step of the method is to write the required drawing syntax according to the user's needs and refer to the user documentation. The user defines the graphics to be drawn through custom syntax rules and saves these rules in the syntax file. In step S1, the user explicitly specifies the grammatical structure of the drawing, such as the name, parameters and drawing instructions of the graphics. Next, in steps S2 and S3, the syntax file and metamodel file will be passed to the interpreter. These files contain the definition of the specific syntax and metadata of the drawing, which are used to describe the model and execution instructions of the graphics.
[0102] 2. Instantiation of scene objects
[0103] In step S4, the interpreter instantiates the drawing scene object according to the passed-in grammar file and metamodel file. The instantiation of the scene object includes creating a logical workspace for drawing graphics. This process is equivalent to building the basic data structure for the subsequent drawing process, ensuring that the definition, attributes and behavior of the graphics can be managed and executed in a unified environment. This step provides the required environment and context for all subsequent graphics operations (such as parsing, drawing, transformation, etc.).
[0104] 3. Graph definition analysis
[0105] In step S5, the interpreter parses the user-defined graph. Each graph has a unique structure and attributes, such as vertex coordinates, starting points, names, etc. of geometric shapes such as line segments, rectangles, and circles. The parser reads the information in the definition file, parses the attributes of the graph into specific objects, and saves them in the system's memory for operation in subsequent steps. Each parsed graph generates a complete geometric object, which can contain multiple vertices and geometric attributes.
[0106] 4. Execution instruction parsing and Turtle drawing calls
[0107] The core work of step S6 is to parse the user-defined drawing execution instructions. Each execution instruction is associated with a graphic object (such as move, draw, etc.), and these operations will store the target graphic object in the shape list. Each graphic object in the shape list contains its name, starting drawing point, and a collection of all vertices. After the parsing is completed, the system will call the Turtle drawing function to draw the actual graphics based on the vertices of the graphic object.
[0108] 5. Graphics conversion instruction analysis and processing
[0109] In step S7, parsing and executing graphics transformation instructions is one of the key steps. The system reads the graphics objects stored in the shape list and uses the vertex sets in them to perform geometric transformations, including translation, rotation, scaling and other operations. After the transformation of each graphics object is completed, the system will call Turtle drawing again to draw the transformed graphics. The geometric transformation in this process performs mathematical operations based on the vertex set of the graphics and dynamically adjusts the position and shape of the graphics in the drawing space.
[0110] 6. Loop judgment and end processing
[0111] Steps S8 and S9 are used to determine whether there are new execution instructions or graphic definitions. If there are new execution instructions, the system will return to S6 and continue to execute the next step; if there are new graphic definitions, the system will return to S5 and continue to parse the newly defined graphics. If there are no new instructions or graphics, the system ends the operation. This mechanism ensures that users can flexibly perform multiple graphic definitions and instruction executions until all drawing operations are completed.
[0112] The S3 metamodel file design method is specifically as follows:
[0113] (1) Define the name of the entire scene, such as "scene".
[0114] (2) Define the instructions to be executed in the scene, such as "shapefine*=ShapeDefine", which means that in the code file of the invented language, all ShapeDefine statements that appear will be added to the shapefine variable to form a list, where shapefine can also be considered as an attribute of the scene, and ShapeDefine is its attribute value.
[0115] (3) After declaring all instructions in the scene object scene, write the attribute values of each instruction. For example, ShapeDefine can also be defined as an object, so in the design of the metamodel, ShapeDefine can also have its attributes and attribute values. Attribute values can also be defined as an object. For example, define a "shape" attribute inside it: ShapeDefine: shape* = Shape.
[0116] (4) The attributes finally defined will be used in the interpreter to process the graphics. For example, the definition of Shape:
[0117] Shape:
[0118] 'shape'name=ID extends=Extends?
[0119] line_color=LineColor?
[0120] line_size=LineSize?
[0121] fill_color=FillColor?
[0122] lines+=Line
[0123] curves*=Curve
[0124] 'end';
[0126] Among them, "shape" and "end" are the keywords of the invented language, name, extends and line_color are all attributes of Shape, and their attribute values can be defined further. When the name attribute is called in the interpreter to obtain the shape name, scene.shapedefine.shape.name is called.
[0127] The design of the entire metamodel and the attribute calls in the interpreter follow object-oriented thinking.
[0128] The graphics drawing algorithm of S6 is specifically as follows:
[0129] (1) Parse the syntax tree to obtain the object instantiated by the code text file according to the metamodel, and traverse the content DrawInstructions list, where each attribute represents a drawing instruction.
[0130] (2) Parse the drawing instruction and obtain the drawing graphic object and drawing point according to the shape attribute of the instruction.
[0131] (3) Parse the graphic object to obtain the line size, color, and fill color of the graphic.
[0132] (4) Parse other structural attribute values in the graphic object, such as the line and curve attributes of the custom graphic, the width and height attributes of the rectangle, the pointnum and radius attributes of the regular polygon, etc., and obtain the vertex set of the graphic by combining the drawing points. Since the custom graphic contains curve and line attributes, the vertex set contains two lists, list 0 is the line vertex set, and list 1 is the curve control point set. The data structure for storing vertices is a Python list, and the elements are two-dimensional coordinate tuples.
[0133] (5) Add all the attributes of the graph extracted in (2) and the corresponding vertex set obtained in (4) to the graph list Shapelist.
[0134] (6) Traverse the shapelist. Each list element contains a vertex set. Read the list length. If the list length is greater than or equal to 2, it means that the element is a custom shape. Otherwise, it is a rectangle, circle, or regular polygon.
[0135] (7) When drawing a custom shape, traverse its vertex set elements in sequence. Vertex set element 0 is the line vertex set, and vertex set element 1 is the curve control point set. The vertices of the line point set are drawn using the Bersenham algorithm, and the vertices of the curve point set are drawn using the quadratic B-spline curve method. The method for drawing rectangles, circles, and regular polygons is the same as that for the lines of custom shapes.
[0136] The specific algorithm of S6 for obtaining a regular polygon through the vertex number pointnum and the radius radius is:
[0137] (1) The drawing point obtained by the parsing instruction is used as the center point O(x, y), and the size of each internal angle is obtained as θ = 360° / pointnum.
[0138] (2) Use the trigonometric function formula to obtain the coordinates of the first vertex P1 (x+radius*sin(θ / 2), y-radius*cos(θ / 2)).
[0139] (3) P2 is obtained through O and P1. Since the radius of the regular polygon is the same, that is, the distance from the center point O to each vertex is the same, P2 is obtained by using the vector rotation method. That is, O is transformed into the origin of the Cartesian coordinate system through linear transformation through coordinate translation, and the OP1 vector is calculated. (1) The internal angle θ is calculated, and the coordinates of P2 are (X(OP1)*cosθ-Y(OP1)*sinθ+x, Y(OP1)*sinθ-Y(OP1)*cosθ+y).
[0140] (4) Point P3 is obtained based on points O and P2, and then P4, P5, P6, etc. are obtained by iteration.
[0141] The graphics transformation algorithm of S7 is specifically as follows:
[0142] (1) Input the transformation instruction attributes of the scene object, the transformed graphic object, and the transformation method (translation, rotation, and scaling);
[0143] (2) Parsing the translation, rotation and scaling parameters of the transformation instructions;
[0144] (3) passing the transformation parameters and the vertex set of the graphics object into the transformation function;
[0145] (4) Normalize the transformation parameters into a 3×3 homogeneous transformation matrix, such as the translation matrix T = [[1,0,ΔX], [0,1,ΔY], [0,0,1]], and normalize the vertex set into an n×3 homogeneous transformation matrix P = [[x1,y1,1], [x2,y2,1], ...];
[0146] (5) Multiply the transformation parameter matrix and the vertex set matrix to obtain the transformed matrix P'=P x T, and return the transformed new vertex set [(x1', y1'), (x2', y2'), ...];
[0147] (6) If the graphic object has both straight lines and curves, the vertex set of the graphic object is a three-dimensional list, that is, it contains two vertex lists, each of which performs vertex transformation;
[0148] (7) Call the Turtle library to traverse the vertex set to draw the transformed graphics. If there are both straight lines and curves, traverse and draw them separately.
[0149] The embodiment of the present invention provides a Python-based drawing domain language design system, which realizes the integration of languages in the drawing field. The drawing domain specific language (DSL) is packaged as a Python module, which is convenient for users to integrate and use in other projects, and provides versatility and scalability. The specific technical solution is as follows:
[0150] 1. Modular design:
[0151] The core functionality of the DSL is modularized and made available as an installable Python package (such as a PyPI package). The package will include a language parser, a drawing processor, geometric transformation tools, and an extension interface.
[0152] The functional units of each module are as follows:
[0153] Language parsing module: responsible for parsing the DSL script entered by the user and converting it into executable Python objects or instructions.
[0154] Graphics drawing module: Based on Python's Turtle library, it converts parsed instructions into graphics operations and displays them on the screen.
[0155] Geometric transformation module: supports geometric transformation operations such as translation, rotation, and scaling, and processes the vertex set of the graphics.
[0156] Extension interface module: provides an API that allows users to extend the functionality of the DSL through Python code, adding custom graphics, special drawing algorithms, and other functions.
[0157] The Python-based drawing domain language (DSL) design system provided by the embodiment of the present invention realizes efficient integration of drawing languages through modular design. First, the system packages DSL as a Python module, so that it can be easily integrated and used in the Python environment, thereby improving versatility and scalability. By designing DSL as an independent Python package, such as a PyPI package, users can directly call drawing functions in different projects, which facilitates cross-project graphics rendering and geometric operations. The modular design structure makes the function of each module clear, which is convenient for users to selectively call or expand according to specific needs.
[0158] In terms of module functions, the language parsing module undertakes the core parsing task of DSL scripts. This module parses the DSL scripts written by users and parses their syntax trees into Python objects or instruction lists, providing a basis for subsequent drawing and transformation operations. Through the parser, the user's graphics drawing requirements are converted into standardized instructions, enabling the drawing system to uniformly handle different graphics objects and operations. The parsed instructions are expressed through Python data structures to ensure compatibility with the Python environment and facilitate further processing and expansion.
[0159] The graphics drawing module uses Python's Turtle library to convert parsed instructions into graphics operations and display them on the screen. The Turtle library provides a rich drawing API, making the graphics drawing process intuitive and concise, while supporting rendering of different shapes, colors, and line styles. This module allows users to easily meet their drawing needs through the DSL language and ensures the real-time and accuracy of graphics rendering. With the powerful functions of the Turtle library, the system can easily cope with a variety of graphics requirements and provide users with flexible drawing capabilities.
[0160] In addition, the system includes a geometric transformation module and an extended interface module, which further improves the functionality and flexibility of DSL. The geometric transformation module supports operations such as translation, rotation, and scaling. Through mathematical processing of the vertex set of the graphics, users can freely transform the graphics. The extended interface module provides an API interface, allowing users to customize the functions of DSL through Python code, such as adding new graphics, adjusting drawing algorithms, etc. Through these extensions, users can flexibly expand the drawing capabilities of DSL according to their own needs, enhancing the versatility and adaptability of the system in the field of drawing.
[0161] 2.Python module packaging:
[0162] Module packaging: The core components of the project will be packaged as independent modules and packaged using the setuptools tool of the Python standard library. The directory structure of the module is as follows:
[0163] pydrawcli /
[0164] ├──__init__.py
[0165] ├──parser.py#Module for parsing DSL
[0166] ├──PyDrawCLI_Interpreter.py#Interpreter module
[0167] ├──Basic_trans.py#Geometric transformation module
[0168] ├──Draw_Curve.py#Turtle drawing module
[0169] ├──Draw_New_After_Trans.py
[0170] ├──Find_Center_points.py#Tool function
[0171] ├──polygon_make.py
[0172] ├──Print_Table.py
[0173] ├──String_Matching.py
[0174] └──setup.py#File used for packaging
[0175] Installation and usage: Users install the DSL module package through the pip command:
[0176] pip install pydrawcli
[0177] 3.DSL script integration and calling:
[0178] After installing the Python module, users can import and use the DSL in their own Python projects. For example, users can write DSL script files and then use the module for parsing and plotting:
[0179] importpydrawcli
[0180] #Import DSL script and perform drawing operations
[0181] pydrawcli.drawturtle(meta="yourmetafile.tx",tfile="yourdslfile.turtle")
[0182] 4. Expansion interface design:
[0183] In order to support users' secondary development, the module will provide an API interface, allowing users to extend DSL functions through Python. For example, users can customize new graphic objects or operation instructions:
[0184] frompydrawcliimport DSLInterpreter
[0185] #Define a custom drawing function
[0186] defdraw_custom_shape():
[0187] #Custom drawing logic
[0188] pass
[0189] #Register custom graphics to the DSL interpreter
[0190] interpreter=DSLInterpreter()
[0191] interpreter.register_custom_shape("custom_shape",draw_custom_shape)
[0192] 5.Module testing and compatibility:
[0193] The DSL module package will support mainstream Python versions (3.x) and be compatible with different operating systems. Testing frameworks such as pytest will be used to test each module to ensure the stability and compatibility of the module.
[0194] At the same time, detailed documentation and tutorials are provided to help users integrate the module in different development environments.
[0195] The specific implementation process is as follows Figure 2As shown, the user can input instructions through the interactive programming entrance 1 or the script programming entrance 2. The input file or instruction enters the code interpreter instantiation module 3, which parses the input script file or instruction through the interpreter file and creates an internally recognizable syntax object. The syntax object contains information such as the definition of the graphic object and image attributes. Then the created syntax object enters the graphics drawing module 4, and calls the turtle image library to draw the graphics according to the information contained in the syntax object. For example, draw basic graphics such as rectangles and circles, as well as the line color of the graphics according to the coordinates and size parameters in the syntax file. The drawn graphic object enters the graphics vertex generation module 5, calculates the vertex information of the graphic according to the graphic object, stores the graphic vertices in the graphics vertex set 6, and then puts all the image information into the graphics saving module 7, and finally saves the image in the image set 8.
[0196] Software flow chart as Figure 3 For example, if it is interactive programming, enter an instruction to enter the instruction processing flow. First, the instruction determines whether it is an exit instruction. If it is not, the instruction is passed to the parser to interpret the file code. The corresponding part is Figure 1 3 interpreter instantiation module. Judge the parsed instruction. If the parsed instruction is a graphics definition instruction, instantiate the graphics object according to the definition instruction, corresponding to 4 graphics definition module. If the parsed instruction is a graphics drawing instruction, draw a new graphic, generate and save the graphics vertex set, corresponding to 5 graphics drawing module. If the parsed instruction is a graphics transformation instruction, transform the graphics according to the graphics vertex set data using the method described in S7, corresponding to 6 graphics transformation module. If the instruction entered in the processing instruction sub-process is an exit instruction, save the graphics and the process ends. If it is script programming, the interpreter parses each instruction in the script line by line, corresponding to 3 interpreter instantiation module, and judges whether it is the last instruction. If it is judged as no, enter the instruction processing flow. If it is judged as yes, save the graphics and the process ends.
[0197] The present invention is based on the Python drawing domain language design method, and introduces in detail the specific implementation of graphic transformation operations such as translation, rotation and scaling. It uses the domain specific language (DSL) and the Turtle graphics library to realize the operation and drawing of graphic objects. The method can accurately complete the transformation of graphics and update the drawing results in real time by parsing the user-defined drawing instructions through the interpreter.
[0198] First, the implementation of the translation operation includes three steps: reading the translation vector in the translation instruction, then adding the translation vector to all vertices of the graphic object, and then obtaining the new vertex position after translation. The vertex list of the graphic object will be updated according to the calculation results, and the new vertex position will be stored in the shape list for subsequent operations to continue processing or drawing. This operation ensures that the graphic object can complete the specified translation in two-dimensional space according to the user's instructions.
[0199] Secondly, the rotation operation involves reading the rotation angle and rotation axis in the rotation instruction. Based on these parameters, the system calculates the corresponding rotation matrix. Then, the rotation matrix is matrix multiplied with the vertex set of the graphics object to obtain the rotated vertex position. The vertex list of the graphics object is updated according to the new rotation result, and the result is stored in the shape list. This process ensures that the graphics object can be accurately rotated on the given rotation axis according to the rotation angle specified by the user, which is suitable for a variety of graphics transformation scenarios.
[0200] The scaling operation is processed according to the scaling ratio in the scaling instruction. First, the system reads the scaling ratio, and then performs a scaling operation on all vertices of the graphic object. Specifically, the coordinate values of each vertex are multiplied to obtain the new vertex position after scaling. The vertex list of the graphic object is then updated, and the scaled vertex positions are stored in the shape list. This scaling operation ensures that the graphic is scaled proportionally at the specified ratio, which is suitable for scenes that need to expand or reduce the graphic.
[0201] In the transformation operation of graphics, whether it is translation, rotation or scaling, the updating and storage of shape lists is a key step. The shape list stores all graphics objects and their vertex data. Any transformation operation will ensure the latest state of the graphics by updating the data in the list. After each transformation, the system saves the new vertex information in the shape list to ensure that the graphics can display the transformed state as required when drawing the next step.
[0202] All these transformation operations are ultimately completed by calling the Turtle library to complete the actual drawing of the graphics. The Turtle library traverses the vertex list of the graphics object and draws the line segments between the vertices one by one. If the graphics object includes two elements, straight lines and curves, the system will traverse the vertex lists of straight lines and curves separately to ensure that each element can be accurately presented. Throughout the entire process, the drawing function of the Turtle library is closely integrated with the parsing function of the interpreter to achieve accurate drawing of graphics objects.
[0203] In summary, the present invention realizes the efficient execution of transformation operations (including translation, rotation and scaling) of graphic objects and ensures the real-time drawing and display of graphics by designing a Python-based graphics domain language, combining a domain-specific language (DSL), an interpreter and the Turtle graphics library. These functions make the method suitable for a variety of scenarios that require dynamic adjustment of graphics, such as education, design and data visualization.
[0204] The specific application fields or related products of the present invention.
[0205] Education and Training: Since the language of the invention has the characteristics of low threshold and easy learning, the invention can be used as a teaching tool to help students quickly master graphics drawing skills, such as in computer science, engineering drawing, art design and other courses.
[0206] Research and Engineering Design: Researchers and engineers can use this invention tool for rapid prototyping and visual data expression.
[0207] Software development: For software products such as CAD software or data analysis platforms that require integrated drawing functions, the DSL can be integrated into the workflow through the API provided by the present invention, providing users with more flexible drawing options.
[0208] Personal creation: Amateurs or professional designers can use the system to create personalized visual works, providing an intuitive and powerful interface that allows non-professionals to easily get started and produce professional graphic content.
[0209] Relevant evidence of the technical effects achieved by the embodiments of the present invention.
[0210] The main feature of the present invention is that it is easy for non-professionals to get started, and it has the characteristics of being easy to read, easy to learn, and easy to understand. Compared with the solution based on the Turtle library and Python language programming commonly used by users in the market, the main advantage of the present invention is that for ordinary users without programming experience, the time required from preliminary understanding to achieving drawing is greatly reduced. Through a survey of ordinary users, Table 1 shows the comparative effect of the Turtle drawing method and the PyDL drawing method (the language of the present invention).
[0211] Table 1
[0212]
[0213] When the present invention draws gear parts, only the definition and drawing of graphics are required when the code is defined, such as "polygon s pointnum 8radius 100end" is used to draw the gear core, while Turtle involves a lot of Python syntax, and each structure definition needs to be repeatedly drawn and involves complex data structures. For non-professionals, it takes an average of 10 minutes to understand Python syntax and an average of 10 minutes for creation and debugging to draw a gear from scratch. However, since the present invention is close to the native language habits, users can adapt to it in a very short time, and when the present invention draws graphics, only the shape parameters of the basic graphics need to be defined, so the average total time spent on drawing gears using the present invention is 5 minutes.
[0214] In the case that the ordinary users surveyed do not know Turtle mapping and the language of the present invention, the above table reflects that the present invention has the characteristics of simplicity and efficiency. At the same time, in some occasional cases such as the drawing of birds with complex multiple graphics combinations, the amount of code of the present invention is relatively increased, but from the time invested by users, it can be seen that the present invention is still easier to learn and create.
[0215] At the same time, the relative time used to master the two drawing methods is compared. By investigating the learning time of users (without programming experience) for the two drawing methods and checking the users' mastery of the two languages through prescribed graphics, the data shown in Table 2 are obtained.
[0216] Table 2
[0217]
[0218] Among them, turtle drawing is relatively simple to start with. The basic commands mastered in the early stage include drawing lines, turning, looping, etc., but as the complexity of the content increases (such as recursion, fractal graphs, etc.), the learning progress slows down in the later stage. In addition, when both methods are first learned, the mastery level increases rapidly, but as time goes by, the progress rate will gradually slow down.
[0219] It should be noted that the embodiments of the present invention can be implemented by hardware, software, or a combination of software and hardware. The hardware part can be implemented using dedicated logic; the software part can be stored in a memory and executed by an appropriate instruction execution system, such as a microprocessor or dedicated design hardware. It can be understood by a person of ordinary skill in the art that the above-mentioned devices and methods can be implemented using computer executable instructions and / or contained in a processor control code, such as a carrier medium such as a disk, CD or DVD-ROM, a programmable memory such as a read-only memory (firmware), or a data carrier such as an optical or electronic signal carrier. Such code is provided on the carrier medium. The device and its modules of the present invention can be implemented by hardware circuits such as very large-scale integrated circuits or gate arrays, semiconductors such as logic chips, transistors, etc., or programmable hardware devices such as field programmable gate arrays, programmable logic devices, etc., can also be implemented by software executed by various types of processors, and can also be implemented by a combination of the above-mentioned hardware circuits and software, such as firmware.
[0220] The above description is only a specific implementation mode of the present invention, but the protection scope of the present invention is not limited thereto. Any modifications, equivalent substitutions and improvements made by any technician familiar with the technical field within the technical scope disclosed by the present invention and within the spirit and principle of the present invention should be covered by the protection scope of the present invention.
Claims
1. A Python-based drawing domain language design method, characterized in that: A drawing domain language design method based on Python uses textx released in Python to define a domain specific language (DSL) and generate a corresponding abstract syntax tree (AST). The syntax and metamodel are instantiated through the interpreter to obtain the entire drawing scene object. The interpreter parses the instructions in the drawing language syntax according to the metamodel as the rules, and calls Python's Turtle graphics library to draw images.
2. The Python-based drawing domain language design method according to claim 1, characterized in that: The method for parsing the drawing language syntax comprises the following steps: S1: Write the required drawing syntax according to the user documentation; S2: Run the code and pass the grammar file into the interpreter; S3: pass the metamodel file into the interpreter; S4: The interpreter instantiates the drawing scene object; S5: Analytically defined graph; S6: Parse the drawing execution instruction, and store the operation graphic object of the execution instruction into the shape list. The attributes of each element in the shape list include the name, the starting drawing point and the set of all vertices, and call Turtle drawing; S7: Parse the graphics conversion execution instruction, read the vertex set of the graphics object and perform the graphics transformation algorithm conversion of translation, rotation and scaling, and then call Turtle drawing; S8: Determine whether there is a new execution instruction, if yes, return to S6, if no, proceed to the next step; S9: Determine whether there is a new graphic definition, if yes, return to S5, if no, end.
3. The Python-based drawing domain language design method according to claim 1, characterized in that: The graphics transformation algorithm of S7 is specifically as follows: (1) Input the transformation instruction attributes of the scene object, the transformed graphic object, and the transformation method (translation, rotation, and scaling); (2) Parsing the translation, rotation and scaling parameters of the transformation instructions; (3) passing the transformation parameters and the vertex set of the graphics object into the transformation function; (4) Normalize the transformation parameters into a 3×3 homogeneous transformation matrix and normalize the vertex set into an n×3 homogeneous transformation matrix; (5) Multiply the transformation parameter matrix and the vertex set matrix to obtain the transformed matrix, and return the transformed new vertex set; (6) If the graphic object has both straight lines and curves, the vertex set of the graphic object is a three-dimensional list, that is, it contains two vertex lists, each of which performs vertex transformation; (7) Call the Turtle library to traverse the vertex set to draw the transformed graphics. If there are both straight lines and curves, traverse and draw them separately.
4. The Python-based drawing domain language design method according to claim 3, characterized in that: The translation operation comprises the following steps: (1) Read the translation vector in the translation instruction; (2) performing an addition operation of the translation vectors on all vertices of the graphic object to obtain the new vertex positions after translation; (3) Update the vertex list of the graphics object, return the new vertex position and store it in the shape list.
5. The Python-based drawing domain language design method according to claim 3, characterized in that: The rotation operation comprises the following steps: (1) Read the rotation angle and rotation axis in the rotation instruction; (2) Calculate the rotation matrix and multiply the rotation matrix by the vertex set of the graphics object to obtain the position of the rotated vertices; (3) Update the vertex list of the graphics object, and return and store the rotated vertex positions in the shape list.
6. The Python-based drawing domain language design method according to claim 3, characterized in that: The scaling operation includes the following steps: (1) Read the scaling ratio in the scaling command; (2) performing a multiplication operation of the scaling ratio on all vertices of the graphic object to obtain the scaled vertex positions; (3) Update the vertex list of the graphics object, return the scaled vertex position and store it in the shape list.
7. The Python-based drawing domain language design method according to claim 1, characterized in that: The following steps are also included: (1) Define the scene object name "scene"; (2) Declare the instructions to be executed in the scene object and add the instructions and their corresponding code files to the scene's attribute list; (3) define attribute values for each instruction so that each instruction can be regarded as an object and contains its attributes; (4) The interpreter calls the attributes and generates a metamodel file according to the structure defined by the scenario and uses it for graphics processing.
8. The Python-based drawing domain language design method according to claim 7, characterized in that: The method comprises the following steps: (1) Parse the syntax tree to obtain the drawing instruction list of the metamodel instantiation object; (2) Parse the drawing instructions and generate the drawn graphics object and its vertex set according to the attributes of the instructions; (3) Generate graphics that conform to the instructions using the line size, color, and fill color attributes of the graphic object; (4) According to the vertex data structure, traverse the vertex sets of straight lines and curves, and call the drawing algorithm to generate the target graphics respectively.
9. A Python-based drawing domain language design system as claimed in claims 1-6, characterized in that: The system packages the drawing domain specific language (DSL) into a Python package, including a language parser, a drawing processor, a geometric transformation tool, and an extension interface. The functional units of each module are as follows: Language parsing module: responsible for parsing the DSL script entered by the user and converting it into executable Python objects or instructions; Graphics drawing module: Based on Python's Turtle library, it converts parsed instructions into graphics operations and displays them on the screen; Geometric transformation module: supports geometric transformation operations such as translation, rotation, and scaling, and processes the vertex set of the graphics; Extension interface module: provides an API that allows users to extend the functionality of the DSL through Python code, adding custom graphics, special drawing algorithms, and other functions.
10. The Python-based drawing domain language design system as claimed in claim 9, characterized in that: The specific implementation process of the system is as follows: (1) The user inputs the syntax file, which can be input through interactive programming or scripted programming entry; (2) The input syntax file enters the code interpreter instantiation module, which parses the input syntax file through the interpreter file and creates an internally identifiable syntax object. The syntax object contains information such as the definition of the graphic object and image attributes; (3) The created grammar object enters the graphics drawing module, and calls the turtle image library to draw graphics according to the information contained in the grammar object; (4) The drawn graphic object enters the graphic vertex generation module, calculates the vertex information of the graphic according to the graphic object, stores the graphic vertices in the graphic vertex set, and then puts all the image information into the graphic saving module, and finally saves the image into the image set.
Citation Information
Patent Citations
System and method for creating domain specific languages for digital environment simulations
US20220004683A1