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