Salome HOME
47210959a9ed98fac83ec0a08eb171ce6a50663f
[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     import geompy
42     geompy.addToStudy( wire, 'mysketch' )
43
44 Additionnal examples can be found as unit tests in the source code.
45 """
46
47 geompyEnable = True
48 try:
49     import geompy
50 except:
51     geompyEnable = False
52     
53
54 class Sketcher:
55
56     __sketch = None
57     __xstart = 0
58     __ystart = 0
59
60     def __init__(self):
61         self.__sketch = "Sketcher:"
62
63     def startAtPoint(self,x=0,y=0):
64         self.__sketch = "Sketcher:F "+str(x)+" "+str(y)
65         # the data x and y are registered to process the ending closure.
66         self.__xstart = x
67         self.__ystart = y
68
69     def translateToPoint(self,x=10,y=0):
70         '''segment->point->absolute'''
71         self.__sketch = self.__sketch + ":TT "+str(x)+" "+str(y) 
72
73     def translateWithVector(self,deltax=10,deltay=0):
74         '''segment->point->relative'''
75         self.__sketch = self.__sketch + ":T "+str(deltax)+" "+str(deltay) 
76
77     #def translateToSelection(self,vertex):
78     #    '''segment->point->selection'''
79     #    self.__sketch = self.__sketch + ":T "+str(deltax)+" "+str(deltay) 
80
81     def translateWithAngleAndLength(self,angle=30,length=100):
82         '''segment->direction->angle+length'''
83         self.__sketch = self.__sketch + ":R "+str(angle)+":L "+str(length)
84
85     def intersectXWithAngle(self,angle=30,x=100):
86         '''segment->direction->angle+length'''
87         self.__sketch = self.__sketch + ":R "+str(angle)+":IX "+str(x)
88
89     def intersectYWithAngle(self,angle=30,y=100):
90         '''segment->direction->angle+length'''
91         self.__sketch = self.__sketch + ":R "+str(angle)+":IY "+str(y)
92
93     def close(self):
94         '''
95         add a segment to reach the starting point and get a closed wire.
96         '''
97         self.__sketch = self.__sketch + ":WW"
98
99     def toString(self):
100         return self.__sketch
101
102     def getGeomWire(self):
103         if not geompyEnable:
104             return None
105         wire = geompy.MakeSketcher(self.__sketch, [0, 0, 0, 0, 0, 1, 1, 0, -0])
106         return wire
107 #
108 # ===============================================================
109 # Use cases and unit tests
110 # ===============================================================
111 #
112 def TEST_toString():
113     mysketcher = Sketcher()
114
115     mysketcher.startAtPoint(1.234,4.321)
116     mysketcher.translateToPoint(2.234,5.321)
117
118     expectedResult = "Sketcher:F 1.234 4.321:TT 2.234 5.321"
119     result = mysketcher.toString()
120     print "sketcher=",mysketcher.toString()
121     if result == expectedResult:
122         print "OK"
123     else:
124         print "Not OK"
125
126 def TEST_usingGeom():
127     mysketcher = Sketcher()
128     mysketcher.startAtPoint(0,0)
129     mysketcher.translateToPoint(100,0)
130     mysketcher.translateWithAngleAndLength(30,100)
131     mysketcher.intersectYWithAngle(15,100)
132     mysketcher.close()
133     wire = mysketcher.getGeomWire()
134     if geompyEnable:
135         geompy.addToStudy( wire, "Sketch" )
136         
137
138 if __name__ == "__main__":
139     #TEST_toString()
140     TEST_usingGeom()