Salome HOME
5e04c891e2ce15c5fbaa4389f9ff39a1bfb6df6c
[modules/geom.git] / src / SKETCHER / Sketcher_Profile.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  GEOM SKETCHER : basic sketcher
24 //  File   : Sketcher_Profile.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header:
28 //
29 #include <Standard_Stream.hxx>
30
31 #include <Sketcher_Profile.hxx>
32
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>
40
41 #include <GeomAPI.hxx>
42 #include <Geom2d_Line.hxx>
43 #include <Geom2d_Circle.hxx>
44 #include <Geom_Surface.hxx>
45
46 #include <Precision.hxx>
47 #include <gp_Pln.hxx>
48 #include <gp_Ax2.hxx>
49
50 #include <TCollection_AsciiString.hxx>
51 #include <TColStd_Array1OfAsciiString.hxx>
52
53 #include "utilities.h"
54
55 //=======================================================================
56 // profile
57 // command to build a profile
58 //=======================================================================
59 Sketcher_Profile::Sketcher_Profile()
60 {
61 }
62
63
64 //=======================================================================
65 // profile
66 // command to build a profile
67 //=======================================================================
68 Sketcher_Profile::Sketcher_Profile(const char* aCmd)
69 {
70   enum {line, circle, point, none} move;
71
72   Standard_Integer i = 1;
73   Standard_Real x0, y0, x, y, dx, dy;
74   x0 = y0 = x = y = dy = 0;
75   dx = 1;
76
77   Standard_Boolean first, stayfirst, face, close;
78   first = Standard_True;
79   stayfirst = face = close = Standard_False;
80
81   TopoDS_Shape S;
82   TopoDS_Vertex MP;
83   BRepBuilderAPI_MakeWire MW;
84   gp_Ax3 DummyHP(gp::XOY());
85   gp_Pln P(DummyHP);
86   TopLoc_Location TheLocation;
87   Handle(Geom_Surface) Surface;
88
89   myOK = Standard_False;
90
91   //TCollection_AsciiString aCommand(CORBA::string_dup(aCmd));
92   TCollection_AsciiString aCommand ((char*)aCmd);
93   TCollection_AsciiString aToken = aCommand.Token(":", 1);
94   int n = 0;
95   // porting to WNT
96   TColStd_Array1OfAsciiString aTab (0, aCommand.Length() - 1);
97   if ( aCommand.Length() )
98   {
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);
103     }
104     n = n - 1;
105   }
106   if ( aTab.Length() && aTab(0).Length() )
107     while(i < n) {
108       Standard_Real length = 0, radius = 0, angle = 0;
109       move = point;
110
111       int n1 = 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);
118       }
119       n1 = n1 - 1;
120
121       switch(a(0).Value(1))
122       {
123       case 'F':
124         {
125           if (n1 != 3) goto badargs;
126           if (!first) {
127             MESSAGE("profile : The F instruction must precede all moves");
128             return;
129           }
130           x0 = x = a(1).RealValue();
131           y0 = y = a(2).RealValue();
132           stayfirst = Standard_True;
133           break;
134         }
135       case 'O':
136         {
137           if (n1 != 4) goto badargs;
138           P.SetLocation(gp_Pnt(a(1).RealValue(), a(2).RealValue(), a(3).RealValue()));
139           stayfirst = Standard_True;
140           break;
141         }
142       case 'P':
143         {
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");
149             return;
150           }
151           gp_Ax2 ax(P.Location(), vn, vx);
152           P.SetPosition(ax);
153           stayfirst = Standard_True;
154           break;
155         }
156       case 'X':
157         {
158           if (n1 != 2) goto badargs;
159           length = a(1).RealValue();
160           if (a(0) == "XX")
161             length -= x;
162           dx = 1; dy = 0;
163           move = line;
164           break;
165         }
166       case 'Y':
167         {
168           if (n1 != 2) goto badargs;
169           length = a(1).RealValue();
170           if (a(0) == "YY")
171             length -= y;
172           dx = 0; dy = 1;
173           move = line;
174           break;
175         }
176       case 'L':
177         {
178           if (n1 != 2) goto badargs;
179           length = a(1).RealValue();
180           if (Abs(length) > Precision::Confusion())
181             move = line;
182           else
183             move = none;
184           break;
185         }
186       case 'T':
187         {
188           if (n1 != 3) goto badargs;
189           Standard_Real vx = a(1).RealValue();
190           Standard_Real vy = a(2).RealValue();
191           if (a(0) == "TT") {
192             vx -= x;
193             vy -= y;
194           }
195           length = Sqrt(vx * vx + vy * vy);
196           if (length > Precision::Confusion()) {
197             move = line;
198             dx = vx / length;
199             dy = vy / length;
200           }
201           else
202             move = none;
203           break;
204         }
205       case 'R':
206         {
207           if (n1 != 2) goto badargs;
208           angle = a(1).RealValue() * PI180;
209           if (a(0) == "RR") {
210             dx = Cos(angle);
211             dy = Sin(angle);
212           }
213           else {
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;
218             dx = t;
219           }
220           break;
221         }
222       case 'D':
223         {
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()) {
229             dx = vx / length;
230             dy = vy / length;
231           }
232           else
233             move = none;
234           break;
235         }
236       case 'C':
237         {
238           if (n1 != 3) goto badargs;
239           radius = a(1).RealValue();
240           if (Abs(radius) > Precision::Confusion()) {
241             angle = a(2).RealValue() * PI180;
242             move = circle;
243           }
244           else
245             move = none;
246           break;
247         }    
248       case 'A':
249         { 
250           if (n1 != 3) goto badargs;
251           Standard_Real vx = a(1).RealValue();
252           Standard_Real vy = a(2).RealValue(); 
253           if (a(0) == "AA") {
254             vx -= x;
255             vy -= y;
256           }
257           Standard_Real det = (dx * vy - dy * vx);
258           if ( Abs(det) > Precision::Confusion()) {
259             Standard_Real c = (dx * vx + dy * vy)   // Cosine of alpha = arc angle / 2
260                               / Sqrt((dx * dx + dy * dy)    
261                                    * (vx * vx + vy * vy));  
262             radius = (vx * vx + vy * vy)       // radius = distance between start  
263                      * Sqrt(dx * dx + dy * dy) // and end point / 2 * sin(alpha)
264                      / (2.0 * det);            // radius is > 0 or < 0
265             if (Abs(radius) > Precision::Confusion()) {
266               angle = 2.0 * acos(c);           // angle in [0,Pi]  
267               move = circle;
268             }
269             else
270               move = none;
271             break;
272           } 
273           else
274             move = none;
275           break;
276         }        
277       case 'I':
278         {
279           if (n1 != 2) goto badargs;
280           length = a(1).RealValue();
281           if (a(0) == "IX") {
282             if (Abs(dx) < Precision::Confusion()) {
283               MESSAGE("profile : cannot intersect, arg "<<i-1);
284               return;
285             }
286             length = (length - x) / dx;
287           }
288           else if (a(0) == "IY") {
289             if (Abs(dy) < Precision::Confusion()) {
290               MESSAGE("profile : cannot intersect, arg "<<i-1);
291               return;
292             }
293             length = (length - y) / dy;
294           }
295           if (Abs(length) > Precision::Confusion())
296             move = line;
297           else
298             move = none;
299           break;
300         }
301       case 'W':
302         {
303           if (a(0) == "WW")
304             close = Standard_True;
305           else if(a(0) == "WF") {
306             close = Standard_True;
307             face = Standard_True;
308           }
309           i = n - 1;
310           break;
311         }
312       default:
313         {
314           MESSAGE("profile : unknown code " << a(i));
315           return;
316         }
317     }
318
319 again :
320     switch (move)
321     {
322     case line :
323       {
324         if (length < 0) {
325           length = -length;
326           dx = -dx;
327           dy = -dy;
328         }
329         Handle(Geom2d_Line) l = new Geom2d_Line(gp_Pnt2d(x,y),gp_Dir2d(dx,dy));
330         BRepBuilderAPI_MakeEdge ME (GeomAPI::To3d(l,P),0,length);
331         if (!ME.IsDone())
332           return;
333         MW.Add(ME);
334         x += length*dx;
335         y += length*dy;
336         break;
337       }
338     case circle :
339       {
340         Standard_Boolean sense = Standard_True;
341         if (radius < 0) {
342           radius = -radius;
343           sense = !sense;
344           dx = -dx;
345           dy = -dy;
346         }
347         gp_Ax2d ax(gp_Pnt2d(x-radius*dy,y+radius*dx),gp_Dir2d(dy,-dx));
348         if (angle < 0) {
349           angle = -angle;
350           sense = !sense;
351         }
352         Handle(Geom2d_Circle) c = new Geom2d_Circle(ax,radius,sense);
353         BRepBuilderAPI_MakeEdge ME (GeomAPI::To3d(c,P),0,angle);
354         if (!ME.IsDone())
355           return;
356         MW.Add(ME);
357         gp_Pnt2d p;
358         gp_Vec2d v;
359         c->D1(angle,p,v);
360         x = p.X();
361         y = p.Y();
362         dx = v.X() / radius;
363         dy = v.Y() / radius;
364         break;
365       }
366     case point:
367       {
368         MP = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, 0.0));
369         break;
370       }
371     case none:
372       {
373         i = n - 1;
374         break;
375       }
376     }
377
378     // update first
379     first = stayfirst;
380     stayfirst = Standard_False;
381
382     if(!(dx == 0 && dy == 0))
383       myLastDir.SetCoord(dx, dy, 0.0);
384     else
385       return;
386     myLastPoint.SetX(x);
387     myLastPoint.SetY(y);
388
389     // next segment....
390     i++;
391     if ((i == n) && close) {
392       // the closing segment
393       dx = x0 - x;
394       dy = y0 - y;
395       length = Sqrt(dx * dx + dy * dy);
396       move = line;
397       if (length > Precision::Confusion()) {
398         dx = dx / length;
399         dy = dy / length;
400         goto again;
401       }
402     }
403   }
404
405   // get the result, face or wire
406   if (move == none) {
407     return;
408   } else if (move == point) {
409     S = MP;
410   } else if (face) {
411     if (!MW.IsDone()) {
412       return;
413     }
414     BRepBuilderAPI_MakeFace MF (P, MW.Wire());
415     if (!MF.IsDone()) {
416       return;
417     }
418     S = MF;
419   } else {
420     if (!MW.IsDone()) {
421       return;
422     }
423     S = MW;
424   }
425
426   if(!TheLocation.IsIdentity())
427     S.Move(TheLocation);
428
429   myShape = S;
430   myOK = true;
431   return;
432
433   badargs :
434     MESSAGE("profile : bad number of arguments");
435     return;
436 }