1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM SKETCHER : basic sketcher
24 // File : Sketcher_Profile.cxx
25 // Author : Damien COQUERET
29 #include <Standard_Stream.hxx>
31 #include <Sketcher_Profile.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <BRepLib.hxx>
36 #include <BRepBuilderAPI_MakeVertex.hxx>
37 #include <BRepBuilderAPI_MakeEdge.hxx>
38 #include <BRepBuilderAPI_MakeWire.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
41 #include <GeomAPI.hxx>
42 #include <Geom2d_Line.hxx>
43 #include <Geom2d_Circle.hxx>
44 #include <Geom_Surface.hxx>
46 #include <Precision.hxx>
50 #include <TCollection_AsciiString.hxx>
51 #include <TColStd_Array1OfAsciiString.hxx>
53 #include "utilities.h"
55 //=======================================================================
57 // command to build a profile
58 //=======================================================================
59 Sketcher_Profile::Sketcher_Profile()
64 //=======================================================================
66 // command to build a profile
67 //=======================================================================
68 Sketcher_Profile::Sketcher_Profile(const char* aCmd)
70 enum {line, circle, point, none} move;
72 Standard_Integer i = 1;
73 Standard_Real x0, y0, x, y, dx, dy;
74 x0 = y0 = x = y = dy = 0;
77 Standard_Boolean first, stayfirst, face, close;
78 first = Standard_True;
79 stayfirst = face = close = Standard_False;
83 BRepBuilderAPI_MakeWire MW;
84 gp_Ax3 DummyHP(gp::XOY());
86 TopLoc_Location TheLocation;
87 Handle(Geom_Surface) Surface;
89 myOK = Standard_False;
91 //TCollection_AsciiString aCommand(CORBA::string_dup(aCmd));
92 TCollection_AsciiString aCommand ((char*)aCmd);
93 TCollection_AsciiString aToken = aCommand.Token(":", 1);
96 TColStd_Array1OfAsciiString aTab (0, aCommand.Length() - 1);
97 if ( aCommand.Length() )
99 while(aToken.Length() != 0) {
100 if(aCommand.Token(":", n + 1).Length() > 0)
101 aTab(n) = aCommand.Token(":", n + 1);
102 aToken = aCommand.Token(":", ++n);
106 if ( aTab.Length() && aTab(0).Length() )
108 Standard_Real length = 0, radius = 0, angle = 0;
112 TColStd_Array1OfAsciiString a (0, aTab(0).Length());
113 aToken = aTab(i).Token(" ", 1);
114 while (aToken.Length() != 0) {
115 if (aTab(i).Token(" ", n1 + 1).Length() > 0)
116 a(n1) = aTab(i).Token(" ", n1 + 1);
117 aToken = aTab(i).Token(" ", ++n1);
121 switch(a(0).Value(1))
125 if (n1 != 3) goto badargs;
127 MESSAGE("profile : The F instruction must precede all moves");
130 x0 = x = a(1).RealValue();
131 y0 = y = a(2).RealValue();
132 stayfirst = Standard_True;
137 if (n1 != 4) goto badargs;
138 P.SetLocation(gp_Pnt(a(1).RealValue(), a(2).RealValue(), a(3).RealValue()));
139 stayfirst = Standard_True;
144 if (n1 != 7) goto badargs;
145 gp_Vec vn(a(1).RealValue(), a(2).RealValue(), a(3).RealValue());
146 gp_Vec vx(a(4).RealValue(), a(5).RealValue(), a(6).RealValue());
147 if (vn.Magnitude() <= Precision::Confusion() || vx.Magnitude() <= Precision::Confusion()) {
148 MESSAGE("profile : null direction");
151 gp_Ax2 ax(P.Location(), vn, vx);
153 stayfirst = Standard_True;
158 if (n1 != 2) goto badargs;
159 length = a(1).RealValue();
168 if (n1 != 2) goto badargs;
169 length = a(1).RealValue();
178 if (n1 != 2) goto badargs;
179 length = a(1).RealValue();
180 if (Abs(length) > Precision::Confusion())
188 if (n1 != 3) goto badargs;
189 Standard_Real vx = a(1).RealValue();
190 Standard_Real vy = a(2).RealValue();
195 length = Sqrt(vx * vx + vy * vy);
196 if (length > Precision::Confusion()) {
207 if (n1 != 2) goto badargs;
208 angle = a(1).RealValue() * PI180;
214 Standard_Real c = Cos(angle);
215 Standard_Real s = Sin(angle);
216 Standard_Real t = c * dx - s * dy;
217 dy = s * dx + c * dy;
224 if (n1 != 3) goto badargs;
225 Standard_Real vx = a(1).RealValue();
226 Standard_Real vy = a(2).RealValue();
227 length = Sqrt(vx * vx + vy * vy);
228 if (length > Precision::Confusion()) {
238 if (n1 != 3) goto badargs;
239 radius = a(1).RealValue();
240 if (Abs(radius) > Precision::Confusion()) {
241 angle = a(2).RealValue() * PI180;
250 if (n1 != 2) goto badargs;
251 length = a(1).RealValue();
253 if (Abs(dx) < Precision::Confusion()) {
254 MESSAGE("profile : cannot intersect, arg "<<i-1);
257 length = (length - x) / dx;
259 else if (a(0) == "IY") {
260 if (Abs(dy) < Precision::Confusion()) {
261 MESSAGE("profile : cannot intersect, arg "<<i-1);
264 length = (length - y) / dy;
266 if (Abs(length) > Precision::Confusion())
275 close = Standard_True;
276 else if(a(0) == "WF") {
277 close = Standard_True;
278 face = Standard_True;
285 MESSAGE("profile : unknown code " << a(i));
300 Handle(Geom2d_Line) l = new Geom2d_Line(gp_Pnt2d(x,y),gp_Dir2d(dx,dy));
301 BRepBuilderAPI_MakeEdge ME (GeomAPI::To3d(l,P),0,length);
311 Standard_Boolean sense = Standard_True;
318 gp_Ax2d ax(gp_Pnt2d(x-radius*dy,y+radius*dx),gp_Dir2d(dy,-dx));
323 Handle(Geom2d_Circle) c = new Geom2d_Circle(ax,radius,sense);
324 BRepBuilderAPI_MakeEdge ME (GeomAPI::To3d(c,P),0,angle);
339 MP = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, 0.0));
351 stayfirst = Standard_False;
353 if(!(dx == 0 && dy == 0))
354 myLastDir.SetCoord(dx, dy, 0.0);
362 if ((i == n) && close) {
363 // the closing segment
366 length = Sqrt(dx * dx + dy * dy);
368 if (length > Precision::Confusion()) {
376 // get the result, face or wire
379 } else if (move == point) {
385 BRepBuilderAPI_MakeFace MF (P, MW.Wire());
397 if(!TheLocation.IsIdentity())
405 MESSAGE("profile : bad number of arguments");