]> SALOME platform Git repositories - modules/geom.git/blob - src/SKETCHER/Sketcher_Profile.cxx
Salome HOME
Porting to OCCT development version: Standard_PI -> M_PI
[modules/geom.git] / src / SKETCHER / Sketcher_Profile.cxx
1 // Copyright (C) 2007-2011  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 //  GEOM SKETCHER : basic sketcher
23 //  File   : Sketcher_Profile.cxx
24 //  Author : Damien COQUERET
25 //  Module : GEOM
26
27 #include <Standard_Stream.hxx>
28
29 #include <Sketcher_Profile.hxx>
30
31 #include <TopoDS_Vertex.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <BRepLib.hxx>
34 #include <BRepBuilderAPI_MakeVertex.hxx>
35 #include <BRepBuilderAPI_MakeEdge.hxx>
36 #include <BRepBuilderAPI_MakeWire.hxx>
37 #include <BRepBuilderAPI_MakeFace.hxx>
38
39 #include <GeomAPI.hxx>
40 #include <Geom2d_Line.hxx>
41 #include <Geom2d_Circle.hxx>
42 #include <Geom_Surface.hxx>
43
44 #include <Precision.hxx>
45 #include <gp_Pln.hxx>
46 #include <gp_Ax2.hxx>
47
48 #include <TCollection_AsciiString.hxx>
49 #include <TColStd_Array1OfAsciiString.hxx>
50
51 #include "utilities.h"
52
53 //=======================================================================
54 // profile
55 // command to build a profile
56 //=======================================================================
57 Sketcher_Profile::Sketcher_Profile()
58 {
59 }
60
61
62 //=======================================================================
63 // profile
64 // command to build a profile
65 //=======================================================================
66 Sketcher_Profile::Sketcher_Profile(const char* aCmd)
67 {
68   enum {line, circle, point, none} move;
69
70   Standard_Integer i = 1;
71   Standard_Real x0, y0, x, y, dx, dy;
72   x0 = y0 = x = y = dy = 0;
73   dx = 1;
74
75   Standard_Boolean first, stayfirst, face, close;
76   first = Standard_True;
77   stayfirst = face = close = Standard_False;
78
79   Standard_Integer reversed = 0;
80   Standard_Integer control_Tolerance = 0;
81
82   TopoDS_Shape S;
83   TopoDS_Vertex MP;
84   BRepBuilderAPI_MakeWire MW;
85   gp_Ax3 DummyHP(gp::XOY());
86   gp_Pln P(DummyHP);
87   TopLoc_Location TheLocation;
88   Handle(Geom_Surface) Surface;
89
90   myOK = Standard_False;
91   myError = 0;
92
93   //TCollection_AsciiString aCommand(CORBA::string_dup(aCmd));
94   TCollection_AsciiString aCommand ((char*)aCmd);
95   TCollection_AsciiString aToken = aCommand.Token(":", 1);
96   int n = 0;
97   // porting to WNT
98   TColStd_Array1OfAsciiString aTab (0, aCommand.Length() - 1);
99   if ( aCommand.Length() )
100   {
101     while(aToken.Length() != 0) {
102       if(aCommand.Token(":", n + 1).Length() > 0)
103         aTab(n) = aCommand.Token(":", n + 1);
104       aToken = aCommand.Token(":", ++n);
105     }
106     n = n - 1;
107   }
108   if ( aTab.Length() && aTab(0).Length() )
109     while(i < n) {
110       Standard_Real length = 0, radius = 0, angle = 0;
111       move = point;
112
113       int n1 = 0;
114       TColStd_Array1OfAsciiString a (0, aTab(0).Length());
115       aToken = aTab(i).Token(" ", 1);
116       while (aToken.Length() != 0) {
117         if (aTab(i).Token(" ", n1 + 1).Length() > 0)
118           a(n1) = aTab(i).Token(" ", n1 + 1);
119         aToken = aTab(i).Token(" ", ++n1);
120       }
121       n1 = n1 - 1;
122
123       switch(a(0).Value(1))
124       {
125       case 'F':
126         {
127           if (n1 != 3) goto badargs;
128           if (!first) {
129             MESSAGE("profile : The F instruction must precede all moves");
130             return;
131           }
132           x0 = x = a(1).RealValue();
133           y0 = y = a(2).RealValue();
134           stayfirst = Standard_True;
135           break;
136         }
137       case 'O':
138         {
139           if (n1 != 4) goto badargs;
140           P.SetLocation(gp_Pnt(a(1).RealValue(), a(2).RealValue(), a(3).RealValue()));
141           stayfirst = Standard_True;
142           break;
143         }
144       case 'P':
145         {
146           if (n1 != 7) goto badargs;
147           gp_Vec vn(a(1).RealValue(), a(2).RealValue(), a(3).RealValue());
148           gp_Vec vx(a(4).RealValue(), a(5).RealValue(), a(6).RealValue());
149           if (vn.Magnitude() <= Precision::Confusion() || vx.Magnitude() <= Precision::Confusion()) {
150             MESSAGE("profile : null direction");
151             return;
152           }
153           gp_Ax2 ax(P.Location(), vn, vx);
154           P.SetPosition(ax);
155           stayfirst = Standard_True;
156           break;
157         }
158       case 'X':
159         {
160           if (n1 != 2) goto badargs;
161           length = a(1).RealValue();
162           if (a(0) == "XX")
163             length -= x;
164           dx = 1; dy = 0;
165           move = line;
166           break;
167         }
168       case 'Y':
169         {
170           if (n1 != 2) goto badargs;
171           length = a(1).RealValue();
172           if (a(0) == "YY")
173             length -= y;
174           dx = 0; dy = 1;
175           move = line;
176           break;
177         }
178       case 'L':
179         {
180           if (n1 != 2) goto badargs;
181           length = a(1).RealValue();
182           if (Abs(length) > Precision::Confusion())
183             move = line;
184           else
185             move = none;
186           break;
187         }
188       case 'T':
189         {
190           if (n1 != 3) goto badargs;
191           Standard_Real vx = a(1).RealValue();
192           Standard_Real vy = a(2).RealValue();
193           if (a(0) == "TT") {
194             vx -= x;
195             vy -= y;
196           }
197           length = Sqrt(vx * vx + vy * vy);
198           if (length > Precision::Confusion()) {
199             move = line;
200             dx = vx / length;
201             dy = vy / length;
202           }
203           else
204             move = none;
205           break;
206         }
207       case 'R':
208         {
209           if (n1 != 2) goto badargs;
210           angle = a(1).RealValue() * M_PI / 180.;
211           if (a(0) == "RR") {
212             dx = Cos(angle);
213             dy = Sin(angle);
214           }
215           else {
216             Standard_Real c = Cos(angle);
217             Standard_Real s = Sin(angle);
218             Standard_Real t = c * dx - s * dy;
219             dy = s * dx + c * dy;
220             dx = t;
221           }
222           break;
223         }
224       case 'D':
225         {
226           if (n1 != 3) goto badargs;
227           Standard_Real vx = a(1).RealValue();
228           Standard_Real vy = a(2).RealValue();
229           length = Sqrt(vx * vx + vy * vy);
230           if (length > Precision::Confusion()) {
231             dx = vx / length;
232             dy = vy / length;
233           }
234           else
235             move = none;
236           break;
237         }
238       case 'C':
239         {
240           if (n1 != 3) goto badargs;
241           radius = a(1).RealValue();
242           if (Abs(radius) > Precision::Confusion()) {
243             angle = a(2).RealValue() * M_PI / 180.;
244             move = circle;
245           }
246           else
247             move = none;
248           break;
249         }
250       case 'A':                                // TAngential arc by end point
251         {
252           if (n1 != 3) goto badargs;
253           Standard_Real vx = a(1).RealValue();
254           Standard_Real vy = a(2).RealValue();
255           if (a(0) == "AA") {
256             vx -= x;
257             vy -= y;
258           }
259           Standard_Real det = dx * vy - dy * vx;
260           if ( Abs(det) > Precision::Confusion()) {
261             // Cosine of alpha = arc of angle / 2 , alpha in [0,Pi]
262             Standard_Real c = (dx * vx + dy * vy) / Sqrt((dx * dx + dy * dy) * (vx * vx + vy * vy));
263             // radius = distance between start and end point / 2 * sin(alpha)
264             // radius is > 0 or < 0
265             radius = (vx * vx + vy * vy)* Sqrt(dx * dx + dy * dy) / (2.0 * det);
266             if (Abs(radius) > Precision::Confusion()) {
267               angle = 2.0 * acos(c); // angle in [0,2Pi]
268               move = circle;
269             }
270             else
271               move = none;
272             break;
273           }
274           else
275             move = none;
276           break;
277         }
278       case 'U':                                // Arc by end point and radiUs
279         {
280           if (n1 != 5) goto badargs;
281           Standard_Real vx = a(1).RealValue();
282           Standard_Real vy = a(2).RealValue();
283           radius  = a(3).RealValue();
284           reversed = a(4).IntegerValue();
285           if (a(0) == "UU") {                 // Absolute
286             vx -= x;
287             vy -= y;
288           }
289           Standard_Real length = Sqrt(vx * vx + vy * vy);
290           if ( (4.0 - (vx * vx + vy * vy) / (radius * radius) >= 0.0 ) && (length > Precision::Confusion()) ) {
291             // Cosine of alpha = arc angle / 2 , alpha in [0,Pi/2]
292             Standard_Real c = 0.5 * Sqrt(4.0 - (vx * vx + vy * vy) / (radius * radius));
293             angle = 2.0 * acos(c); // angle in [0,Pi]
294             if ( reversed == 2 )
295               angle = angle - 2 * M_PI;
296             dx =    0.5 * (  vy * 1.0/radius
297                            + vx * Sqrt(4.0  / (vx * vx + vy * vy) - 1.0 / (radius * radius)));
298             dy = -  0.5 * (  vx * 1.0/radius
299                            - vy * Sqrt(4.0  / (vx * vx + vy * vy) - 1.0 / (radius * radius)));
300             move = circle;
301           }
302           else{
303             move = none;
304           }
305           break;
306         }
307       case 'E':                                // Arc by end point and cEnter
308         {
309           if (n1 != 7) goto badargs;
310           Standard_Real vx = a(1).RealValue();
311           Standard_Real vy = a(2).RealValue();
312           Standard_Real vxc  = a(3).RealValue();
313           Standard_Real vyc  = a(4).RealValue();
314           reversed = a(5).IntegerValue();
315           control_Tolerance = a(6).IntegerValue();
316
317           if (a(0) == "EE") {                 // Absolute
318             vx -= x;
319             vy -= y;
320             vxc -= x;
321             vyc -= y;
322           }
323           radius = Sqrt( vxc * vxc + vyc * vyc );
324           Standard_Real det = vx * vyc - vy * vxc;
325           Standard_Real length = Sqrt(vx * vx + vy * vy);
326           Standard_Real length2 = Sqrt((vx-vxc) * (vx-vxc) + (vy-vyc) * (vy-vyc));
327           Standard_Real length3 = Sqrt(vxc * vxc + vyc * vyc);
328           Standard_Real error = Abs(length2 - radius);
329           myError = error;
330           if ( error > Precision::Confusion() ){
331             MESSAGE("Warning : The specified end point is not on the Arc, distance = "<<error);
332           }
333           if ( error > Precision::Confusion() && control_Tolerance == 1)                      // Don't create the arc if the end point
334             move = none;                                                                      // is too far from it
335           else if ( (length > Precision::Confusion()) &&
336                     (length2 > Precision::Confusion()) &&
337                     (length3 > Precision::Confusion()) ) {
338             Standard_Real c = ( radius * radius - (vx * vxc + vy * vyc) )
339                             / ( radius * Sqrt((vx-vxc) * (vx-vxc) + (vy-vyc) * (vy-vyc)) ) ;  // Cosine of arc angle
340             angle = acos(c);                                                                  // angle in [0,Pi]
341             if ( reversed == 2 )
342               angle = angle - 2 * M_PI;
343             if (det < 0)
344               angle = -angle;
345             dx =  vyc / radius;
346             dy = -vxc / radius;
347             move = circle;
348           }
349           else {
350             move = none;
351           }
352           break;
353         }
354       case 'I':
355         {
356           if (n1 != 2) goto badargs;
357           length = a(1).RealValue();
358           if (a(0) == "IX") {
359             if (Abs(dx) < Precision::Confusion()) {
360               MESSAGE("profile : cannot intersect, arg "<<i-1);
361               return;
362             }
363             length = (length - x) / dx;
364           }
365           else if (a(0) == "IY") {
366             if (Abs(dy) < Precision::Confusion()) {
367               MESSAGE("profile : cannot intersect, arg "<<i-1);
368               return;
369             }
370             length = (length - y) / dy;
371           }
372           if (Abs(length) > Precision::Confusion())
373             move = line;
374           else
375             move = none;
376           break;
377         }
378       case 'W':
379         {
380           if (a(0) == "WW")
381             close = Standard_True;
382           else if(a(0) == "WF") {
383             close = Standard_True;
384             face = Standard_True;
385           }
386           i = n - 1;
387           break;
388         }
389       default:
390         {
391           MESSAGE("profile : unknown code " << a(i));
392           return;
393         }
394     }
395
396 again :
397     switch (move)
398     {
399     case line :
400       {
401         if (length < 0) {
402           length = -length;
403           dx = -dx;
404           dy = -dy;
405         }
406         Handle(Geom2d_Line) l = new Geom2d_Line(gp_Pnt2d(x,y),gp_Dir2d(dx,dy));
407         BRepBuilderAPI_MakeEdge ME (GeomAPI::To3d(l,P),0,length);
408         if (!ME.IsDone())
409           return;
410         MW.Add(ME);
411         x += length*dx;
412         y += length*dy;
413         break;
414       }
415     case circle :
416       {
417         Standard_Boolean sense = Standard_True;
418         if (radius < 0) {
419           radius = -radius;
420           sense = !sense;
421           dx = -dx;
422           dy = -dy;
423         }
424         gp_Ax2d ax(gp_Pnt2d(x-radius*dy,y+radius*dx),gp_Dir2d(dy,-dx));
425         if (angle < 0) {
426           angle = -angle;
427           sense = !sense;
428         }
429         Handle(Geom2d_Circle) c = new Geom2d_Circle(ax,radius,sense);
430         BRepBuilderAPI_MakeEdge ME (GeomAPI::To3d(c,P),0,angle);
431         if (!ME.IsDone())
432           return;
433         MW.Add(ME);
434         gp_Pnt2d p;
435         gp_Vec2d v;
436         c->D1(angle,p,v);
437         x = p.X();
438         y = p.Y();
439         dx = v.X() / radius;
440         dy = v.Y() / radius;
441         break;
442       }
443     case point:
444       {
445         MP = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, 0.0));
446         break;
447       }
448     case none:
449       {
450         i = n - 1;
451         break;
452       }
453     }
454
455     // update first
456     first = stayfirst;
457     stayfirst = Standard_False;
458
459     if(!(dx == 0 && dy == 0))
460       myLastDir.SetCoord(dx, dy, 0.0);
461     else
462       return;
463     myLastPoint.SetX(x);
464     myLastPoint.SetY(y);
465
466     // next segment....
467     i++;
468     if ((i == n) && close) {
469       // the closing segment
470       dx = x0 - x;
471       dy = y0 - y;
472       length = Sqrt(dx * dx + dy * dy);
473       move = line;
474       if (length > Precision::Confusion()) {
475         dx = dx / length;
476         dy = dy / length;
477         goto again;
478       }
479     }
480   }
481
482   // get the result, face or wire
483   if (move == none) {
484     return;
485   } else if (move == point) {
486     S = MP;
487   } else if (face) {
488     if (!MW.IsDone()) {
489       return;
490     }
491     BRepBuilderAPI_MakeFace MF (P, MW.Wire());
492     if (!MF.IsDone()) {
493       return;
494     }
495     S = MF;
496   } else {
497     if (!MW.IsDone()) {
498       return;
499     }
500     S = MW;
501   }
502
503   if(!TheLocation.IsIdentity())
504     S.Move(TheLocation);
505
506   myShape = S;
507   myOK = true;
508   return;
509
510   badargs :
511     MESSAGE("profile : bad number of arguments");
512     return;
513 }