Salome HOME
cc6404bd85f9b1fd82ffd945e6a8249055d3baf9
[modules/geom.git] / src / GEOM_PY / sketcher.py
1 # -*- coding: iso-8859-1 -*-
2 #
3 # Copyright (C) 2007-2013  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.
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 This module provides the user with a simple python API to realize
23 various sketches from the GEOM text user interface.
24
25 Example::
26
27     from salome.geom.sketcher import Sketcher
28
29     # Create the sketch
30     mysketcher = Sketcher()
31     mysketcher.startAtPoint(0,0)
32     mysketcher.translateToPoint(100,0)
33     mysketcher.translateWithAngleAndLength(30,100)
34     mysketcher.intersectYWithAngle(15,100)
35     mysketcher.close()
36
37     # Get the geom object
38     wire = mysketcher.getGeomWire()
39
40     # Put it in the study
41     from salome.geom import geomBuilder
42     geompy = geomBuilder.New(salome.myStudy)
43     geompy.addToStudy( wire, 'mysketch' )
44
45 Additionnal examples can be found as unit tests in the source code.
46 """
47
48 geompyEnable = True
49 try:
50     from salome.kernel.deprecation import is_called_by_sphinx
51     if not is_called_by_sphinx():
52         import salome
53         salome.salome_init()
54         import GEOM
55         from salome.geom import geomBuilder
56         geompy = geomBuilder.New(salome.myStudy)
57         pass
58     pass
59 except:
60     geompyEnable = False
61     pass
62
63 class Sketcher:
64
65     __sketch = None
66     __xstart = 0
67     __ystart = 0
68
69     def __init__(self):
70         self.__sketch = "Sketcher:"
71
72     def startAtPoint(self,x=0,y=0):
73         self.__sketch = "Sketcher:F "+str(x)+" "+str(y)
74         # the data x and y are registered to process the ending closure.
75         self.__xstart = x
76         self.__ystart = y
77
78     def translateToPoint(self,x=10,y=0):
79         '''segment->point->absolute'''
80         self.__sketch = self.__sketch + ":TT "+str(x)+" "+str(y) 
81
82     def translateWithVector(self,deltax=10,deltay=0):
83         '''segment->point->relative'''
84         self.__sketch = self.__sketch + ":T "+str(deltax)+" "+str(deltay) 
85
86     #def translateToSelection(self,vertex):
87     #    '''segment->point->selection'''
88     #    self.__sketch = self.__sketch + ":T "+str(deltax)+" "+str(deltay) 
89
90     def translateWithAngleAndLength(self,angle=30,length=100):
91         '''segment->direction->angle+length'''
92         self.__sketch = self.__sketch + ":R "+str(angle)+":L "+str(length)
93
94     def intersectXWithAngle(self,angle=30,x=100):
95         '''segment->direction->angle+length'''
96         self.__sketch = self.__sketch + ":R "+str(angle)+":IX "+str(x)
97
98     def intersectYWithAngle(self,angle=30,y=100):
99         '''segment->direction->angle+length'''
100         self.__sketch = self.__sketch + ":R "+str(angle)+":IY "+str(y)
101
102     def close(self):
103         '''
104         add a segment to reach the starting point and get a closed wire.
105         '''
106         self.__sketch = self.__sketch + ":WW"
107
108     def toString(self):
109         return self.__sketch
110
111     def getGeomWire(self):
112         if not geompyEnable:
113             return None
114         wire = geompy.MakeSketcher(self.__sketch, [0, 0, 0, 0, 0, 1, 1, 0, -0])
115         return wire
116 #
117 # ===============================================================
118 # Use cases and unit tests
119 # ===============================================================
120 #
121 def TEST_toString():
122     mysketcher = Sketcher()
123
124     mysketcher.startAtPoint(1.234,4.321)
125     mysketcher.translateToPoint(2.234,5.321)
126
127     expectedResult = "Sketcher:F 1.234 4.321:TT 2.234 5.321"
128     result = mysketcher.toString()
129     print "sketcher=",mysketcher.toString()
130     if result == expectedResult:
131         print "OK"
132     else:
133         print "Not OK"
134
135 def TEST_usingGeom():
136     mysketcher = Sketcher()
137     mysketcher.startAtPoint(0,0)
138     mysketcher.translateToPoint(100,0)
139     mysketcher.translateWithAngleAndLength(30,100)
140     mysketcher.intersectYWithAngle(15,100)
141     mysketcher.close()
142     wire = mysketcher.getGeomWire()
143     if geompyEnable:
144         geompy.addToStudy( wire, "Sketch" )
145         
146
147 if __name__ == "__main__":
148     #TEST_toString()
149     TEST_usingGeom()