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