Salome HOME
Merge from V6_main (04/10/2012)
[modules/geom.git] / src / GEOM_SWIG / gsketcher.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 #  File   : gsketcher.py
21 #  Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
22 #  Module : GEOM_SWIG
23
24 """
25     \namespace geompy
26     \brief 3D Sketcher interface
27 """
28
29 # This method is used by 3D Sketcher functionality
30 def printVar (var):
31     if isinstance(var, str):
32         return "\'%s\'"%var
33     else:
34         return "%.7f"%var
35
36 ## An interface to build a 3D Sketcher step-by-step.
37 #  Use geompy.Sketcher3D() method to obtain an instance of this class.
38 #
39 #  @ref tui_3dsketcher_page "Example"
40 class Sketcher3D:
41     """
42     3D sketcher interface.
43
44     Example of usage:
45         sk = geompy.Sketcher3D()
46         sk.addPointsAbsolute(0,0,0, 70,0,0)
47         sk.addPointsRelative(0, 0, 130)
48         sk.addPointAnglesLength("OXY", 50, 0, 100)
49         sk.addPointAnglesLength("OXZ", 30, 80, 130)
50         sk.close()
51         a3D_Sketcher_1 = sk.wire()
52     """
53
54     def __init__(self, geompyD):
55         self.geompyD = geompyD
56         self.myCommand = "3DSketcher"
57         pass
58
59     ## Add one or more points, sequentially connected with straight segments.
60     #  Coordinates are considered as absolute.
61     #  If the first point of sketcher is not yet defined, the first point
62     #  from the listCoords will become the first sketcher point.
63     #  @param X1, Y1, Z1, X2, Y2, Z2, ... Coordinates of points
64     def addPointsAbsolute (self, *listCoords):
65         """
66         Add one or more points, sequentially connected with straight segments.
67         Coordinates are considered as absolute.
68         If the first point of sketcher is not yet defined, the first point
69         from the listCoords will become the first sketcher point.
70
71         Parameters:
72             X1, Y1, Z1, X2, Y2, Z2, ... Coordinates of points
73
74         Example of usage:
75             sk = geompy.Sketcher3D()
76             sk.addPointsAbsolute(0,0,0, 70,0,0)
77             a3D_Sketcher_1 = sk.wire()
78         """
79         ii = 1
80         for cc in listCoords:
81             if ii == 1:
82                 self.myCommand = self.myCommand + ":TT"
83             self.myCommand = self.myCommand + " %s"%printVar(cc)
84             if ii == 3:
85                 ii = 1
86             else:
87                 ii = ii + 1
88         pass
89
90     ## Add one or more points, sequentially connected with straight segments.
91     #  Coordinates are considered relative to the previous point.
92     #  If the first point of sketcher is not yet defined, the
93     #  origin (0, 0, 0) will become the first sketcher point.
94     #  @param X1, Y1, Z1, X2, Y2, Z2, ... Coordinates of points
95     def addPointsRelative (self, *listCoords):
96         """
97         Add one or more points, sequentially connected with straight segments.
98         Coordinates are considered relative to the previous point.
99         If the first point of sketcher is not yet defined, the
100         origin (0, 0, 0) will become the first sketcher point.
101
102         Parameters:
103             X1, Y1, Z1, X2, Y2, Z2, ... Relative coordinates of points
104
105         Example of usage:
106             sk = geompy.Sketcher3D()
107             sk.addPointsRelative(0,0,130, 70,0,-130)
108             a3D_Sketcher_1 = sk.wire()
109         """
110         ii = 1
111         for cc in listCoords:
112             if ii == 1:
113                 self.myCommand = self.myCommand + ":T"
114             self.myCommand = self.myCommand + " %s"%printVar(cc)
115             if ii == 3:
116                 ii = 1
117             else:
118                 ii = ii + 1
119         pass
120
121     ## Add one straight segment, defined by two angles and length.
122     #  If the first point of sketcher is not yet defined, the
123     #  origin (0, 0, 0) will become the first sketcher point.
124     #  @param axes can be: "OXY", "OYZ" or "OXZ"
125     #  @param angle1 angle in a plane, defined by the \a axes
126     #  @param angle2 angle from the plane, defined by the \a axes
127     #  @param length length of the segment
128     def addPointAnglesLength (self, axes, angle1, angle2, length):
129         """
130         Add one straight segment, defined by two angles and length.
131         If the first point of sketcher is not yet defined, the
132         origin (0, 0, 0) will become the first sketcher point.
133
134         Parameters:
135             axes can be: "OXY", "OYZ" or "OXZ"
136             angle1 angle in a plane, defined by the \a axes
137             angle2 angle from the plane, defined by the \a axes
138             length length of the segment
139
140         Example of usage:
141             sk = geompy.Sketcher3D()
142             sk.addPointAnglesLength("OXY", 50, 0, 100)
143             a3D_Sketcher_1 = sk.wire()
144         """
145         self.myCommand = self.myCommand + ":%s %s %s %s" % (axes, printVar(angle1), printVar(angle2), printVar(length))
146         pass
147
148     ## Set to close the wire
149     def close (self):
150         """
151         Set to close the wire
152
153         Example of usage:
154             sk = geompy.Sketcher3D()
155             sk.addPointsRelative(0,0,130, 70,0,-130)
156             sk.close()
157             a3D_Sketcher_1 = sk.wire()
158         """
159         self.myCommand = self.myCommand + ":WW"
160         pass
161
162     ## Obtain the sketcher result.
163     #  @return New GEOM_Object, containing the created wire
164     def wire (self):
165         """
166         Obtain the sketcher result.
167
168         Returns:
169             New GEOM_Object, containing the created wire.
170
171         Example of usage:
172             sk = geompy.Sketcher3D()
173             sk.addPointsRelative(0,0,130, 70,0,-130)
174             a3D_Sketcher_1 = sk.wire()
175         """
176         from geompyDC import ParseSketcherCommand, RaiseIfFailed
177         Command,Parameters = ParseSketcherCommand(self.myCommand)
178         wire = self.geompyD.CurvesOp.Make3DSketcherCommand(Command)
179         self.myCommand = "3DSketcher"
180         RaiseIfFailed("Sketcher3D", self.geompyD.CurvesOp)
181         wire.SetParameters(Parameters)
182         return wire