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