Salome HOME
afc6344443c00466e1b27df8100c874119988401
[modules/geom.git] / src / GEOM_PY / sketcher.py
1 # -*- coding: iso-8859-1 -*-
2 #
3 # Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 ## \defgroup simplesketcher sketcher - Simplified sketcher API
23 #  \{
24 #  \details
25 #  This module provides simplified access to the 2D sketcher functionality of Geometry module.
26 #  
27 #  \note This module is deprecated, refer to \ref gsketcher for more details
28 #  \}
29
30 """
31 This module provides the user with a simple python API to realize
32 various sketches from the GEOM text user interface.
33
34 Example::
35
36     from salome.geom.sketcher import Sketcher
37
38     # Create the sketch
39     mysketcher = Sketcher()
40     mysketcher.startAtPoint(0,0)
41     mysketcher.translateToPoint(100,0)
42     mysketcher.translateWithAngleAndLength(30,100)
43     mysketcher.intersectYWithAngle(15,100)
44     mysketcher.close()
45
46     # Get the geom object
47     wire = mysketcher.getGeomWire()
48
49     # Put it in the study
50     from salome.geom import geomBuilder
51     geompy = geomBuilder.New()
52     geompy.addToStudy( wire, 'mysketch' )
53
54 Additional examples can be found as unit tests in the source code.
55 """
56
57 geompyEnable = True
58 try:
59     from salome.kernel.deprecation import is_called_by_sphinx
60     if not is_called_by_sphinx():
61         import salome
62         salome.salome_init()
63         import GEOM
64         from salome.geom import geomBuilder
65         geompy = geomBuilder.New()
66         pass
67     pass
68 except:
69     geompyEnable = False
70     pass
71
72 ##
73 #  \ingroup simplesketcher
74 class Sketcher:
75
76     __sketch = None
77     __xstart = 0
78     __ystart = 0
79
80     def __init__(self):
81         self.__sketch = "Sketcher:"
82
83     def startAtPoint(self,x=0,y=0):
84         self.__sketch = "Sketcher:F "+str(x)+" "+str(y)
85         # the data x and y are registered to process the ending closure.
86         self.__xstart = x
87         self.__ystart = y
88
89     def translateToPoint(self,x=10,y=0):
90         '''segment->point->absolute'''
91         self.__sketch = self.__sketch + ":TT "+str(x)+" "+str(y) 
92
93     def translateWithVector(self,deltax=10,deltay=0):
94         '''segment->point->relative'''
95         self.__sketch = self.__sketch + ":T "+str(deltax)+" "+str(deltay) 
96
97     #def translateToSelection(self,vertex):
98     #    '''segment->point->selection'''
99     #    self.__sketch = self.__sketch + ":T "+str(deltax)+" "+str(deltay) 
100
101     def translateWithAngleAndLength(self,angle=30,length=100):
102         '''segment->direction->angle+length'''
103         self.__sketch = self.__sketch + ":R "+str(angle)+":L "+str(length)
104
105     def intersectXWithAngle(self,angle=30,x=100):
106         '''segment->direction->angle+length'''
107         self.__sketch = self.__sketch + ":R "+str(angle)+":IX "+str(x)
108
109     def intersectYWithAngle(self,angle=30,y=100):
110         '''segment->direction->angle+length'''
111         self.__sketch = self.__sketch + ":R "+str(angle)+":IY "+str(y)
112
113     def close(self):
114         '''
115         add a segment to reach the starting point and get a closed wire.
116         '''
117         self.__sketch = self.__sketch + ":WW"
118
119     def toString(self):
120         return self.__sketch
121
122     def getGeomWire(self):
123         if not geompyEnable:
124             return None
125         wire = geompy.MakeSketcher(self.__sketch, [0, 0, 0, 0, 0, 1, 1, 0, -0])
126         return wire
127 #
128 # ===============================================================
129 # Use cases and unit tests
130 # ===============================================================
131 #
132 def TEST_toString():
133     mysketcher = Sketcher()
134
135     mysketcher.startAtPoint(1.234,4.321)
136     mysketcher.translateToPoint(2.234,5.321)
137
138     expectedResult = "Sketcher:F 1.234 4.321:TT 2.234 5.321"
139     result = mysketcher.toString()
140     print("sketcher=",mysketcher.toString())
141     if result == expectedResult:
142         print("OK")
143     else:
144         print("Not OK")
145
146 def TEST_usingGeom():
147     mysketcher = Sketcher()
148     mysketcher.startAtPoint(0,0)
149     mysketcher.translateToPoint(100,0)
150     mysketcher.translateWithAngleAndLength(30,100)
151     mysketcher.intersectYWithAngle(15,100)
152     mysketcher.close()
153     wire = mysketcher.getGeomWire()
154     if geompyEnable:
155         geompy.addToStudy( wire, "Sketch" )
156         
157
158 if __name__ == "__main__":
159     #TEST_toString()
160     TEST_usingGeom()