Salome HOME
Copyright update 2022
[modules/geom.git] / src / GEOMImpl / GEOMImpl_3DSketcherDriver.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Standard_Stream.hxx>
21
22 #include <GEOMImpl_3DSketcherDriver.hxx>
23 #include <GEOMImpl_I3DSketcher.hxx>
24 #include <GEOMImpl_Types.hxx>
25 #include <GEOM_Function.hxx>
26
27 #include <GEOMImpl_IMeasureOperations.hxx>
28
29 #include <Basics_Utils.hxx>
30
31 // OCCT Includes
32 #include <BRepBuilderAPI_MakePolygon.hxx>
33 #include <BRepBuilderAPI_MakeVertex.hxx>
34 #include <BRepBuilderAPI_MakeEdge.hxx>
35 #include <BRepBuilderAPI_MakeWire.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Wire.hxx>
39 #include <gp_Pnt.hxx>
40
41 #include <Standard_ConstructionError.hxx>
42
43 #include <utilities.h>
44
45 //=======================================================================
46 //function : GetID
47 //purpose  :
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_3DSketcherDriver::GetID()
50 {
51   static Standard_GUID a3DSketcherDriver("FF2BBB54-5D24-4df3-210B-3A678263EA26");
52   return a3DSketcherDriver;
53 }
54
55
56 //=======================================================================
57 //function : GEOMImpl_3DSketcherDriver
58 //purpose  :
59 //=======================================================================
60 GEOMImpl_3DSketcherDriver::GEOMImpl_3DSketcherDriver()
61 {
62 }
63
64 //=======================================================================
65 //function : Execute
66 //purpose  :
67 //=======================================================================
68 Standard_Integer GEOMImpl_3DSketcherDriver::Execute(Handle(TFunction_Logbook)& log) const
69 {
70   if (Label().IsNull()) return 0;
71   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
72
73   GEOMImpl_I3DSketcher aCI (aFunction);
74
75   TopoDS_Shape aShape;
76
77   if (aFunction->GetType() == SKETCHER3D_COORDS) {
78     Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
79     int anArrayLength = aCoordsArray->Length();
80
81     std::list<gp_Pnt> points;
82
83     for (int i = 0; i <= (anArrayLength-3); i += 3) {
84       gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
85       if (points.empty() || aPnt.Distance(points.back()) > gp::Resolution())
86         points.push_back(aPnt);
87     }
88
89     if ( points.size() == 1) { // Only Start Point
90       BRepBuilderAPI_MakeVertex mkVertex (points.back());
91       aShape = mkVertex.Shape();
92     }
93     else if ( points.size() > 1) { // Make Wire
94       BRepBuilderAPI_MakePolygon aMakePoly;
95       std::list<gp_Pnt>::iterator it;
96       for (it = points.begin(); it != points.end(); ++it) {
97         aMakePoly.Add(*it);
98       }
99
100       if (points.size() > 2 && 
101           points.back().X() == points.front().X() && 
102           points.back().Y() == points.front().Y() && 
103           points.back().Z() == points.front().Z())
104         aMakePoly.Close();
105
106       if (aMakePoly.IsDone())
107         aShape = aMakePoly.Wire();
108     }
109   }
110   else if (aFunction->GetType() == SKETCHER3D_COMMAND) {
111     Kernel_Utils::Localizer loc;
112
113     TCollection_AsciiString aCommand = aCI.GetCommand();
114     // "3DSketcher:CMD[:CMD[:CMD...]]"
115
116     // Split the command string to separate CMDs
117     int icmd = 2;
118     TColStd_SequenceOfAsciiString aSequence;
119     if (aCommand.Length()) {
120       TCollection_AsciiString aToken = aCommand.Token(":", icmd);
121       while (aToken.Length() > 0) {
122         aSequence.Append(aToken);
123         aToken = aCommand.Token(":", ++icmd);
124       }
125     }
126
127     int nbEdges = 0;
128     bool isFirstPointSet = false;
129     gp_XYZ p = gp::Origin().XYZ();
130     BRepBuilderAPI_MakeVertex MV0 (p);
131     TopoDS_Vertex V = TopoDS::Vertex(MV0.Shape());
132
133     gp_XYZ p0 = p;
134     TopoDS_Vertex V0 = V;
135
136     bool doClose = false;
137     BRepBuilderAPI_MakeWire MW;
138
139     int nbCMDs = aSequence.Length();
140     for (icmd = 1; icmd <= nbCMDs; icmd++) {
141       TCollection_AsciiString aCMD = aSequence.Value(icmd);
142
143       // Split the CMD into string values
144       TColStd_SequenceOfAsciiString aStrVals;
145       int ival = 1;
146       TCollection_AsciiString aToken = aCMD.Token(" ", ival);
147       while (aToken.Length() > 0) {
148         aStrVals.Append(aToken);
149         aToken = aCMD.Token(" ", ++ival);
150       }
151
152       // "TT x y z" : Create segment by point at X & Y or set the first point
153       // "T dx dy dz" : Create segment by point with DX & DY
154       //
155       // "OXY angleX angle2 length" : Create segment by two angles and length
156       // "OYZ angleY angle2 length" : Create segment by two angles and length
157       // "OXZ angleX angle2 length" : Create segment by two angles and length
158       //
159       // "WW" : Close Wire (to finish)
160
161       switch (aStrVals.Value(1).Value(1))
162       {
163       case 'T':
164         {
165           if (aStrVals.Length() != 4)
166             Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
167
168           gp_XYZ vp;
169           vp.SetX(aStrVals.Value(2).RealValue());
170           vp.SetY(aStrVals.Value(3).RealValue());
171           vp.SetZ(aStrVals.Value(4).RealValue());
172
173           if (aStrVals.Value(1) == "TT") { // absolute coordinates
174             if (!isFirstPointSet) {
175               p = vp;
176               BRepBuilderAPI_MakeVertex MV (p);
177               V = TopoDS::Vertex(MV.Shape());
178
179               p0 = p;
180               V0 = V;
181
182               isFirstPointSet = true;
183             }
184             else {
185               if ((vp - p).SquareModulus() > Precision::Confusion()) {
186                 BRepBuilderAPI_MakeVertex MV (vp);
187                 TopoDS_Vertex VV = TopoDS::Vertex(MV.Shape());
188                 BRepBuilderAPI_MakeEdge ME (V, VV);
189                 MW.Add(ME);
190                 nbEdges++;
191
192                 p = vp;
193                 V = VV;
194               }
195             }
196           }
197           else if (aStrVals.Value(1) == "T") { // relative coordinates
198             if (vp.SquareModulus() > Precision::Confusion()) {
199               vp = p + vp;
200
201               BRepBuilderAPI_MakeVertex MV (vp);
202               TopoDS_Vertex VV = TopoDS::Vertex(MV.Shape());
203               BRepBuilderAPI_MakeEdge ME (V, VV);
204               MW.Add(ME);
205               nbEdges++;
206
207               p = vp;
208               V = VV;
209             }
210           }
211           else
212             Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
213         }
214         break;
215       case 'O':
216         {
217           if (aStrVals.Length() != 4)
218             Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
219
220           char type = aStrVals.Value(1).Value(4);
221           char mode = aStrVals.Value(1).Value(5);
222           
223           double anAngle  = aStrVals.Value(2).RealValue() * M_PI/180.0;
224           double anAngle2 = aStrVals.Value(3).RealValue() * M_PI/180.0; 
225           double aLength  = aStrVals.Value(4).RealValue();
226           
227           double aHeight  = aLength * sin(anAngle2);
228           if (type == 'C')
229           {
230             aHeight = aStrVals.Value(3).RealValue();
231             anAngle2 = 0.0;
232           }
233
234           double aProjectedLength = aLength * cos(anAngle2);
235
236           gp_XYZ vp;
237           vp.SetX(aStrVals.Value(2).RealValue());
238           vp.SetY(aStrVals.Value(3).RealValue());
239           vp.SetZ(aStrVals.Value(4).RealValue());
240           
241           gp_XYZ pref = gp::Origin().XYZ();                 
242           if(mode == 'R')
243             pref = p;
244           
245           TCollection_AsciiString aTruncatedCommand = aStrVals.Value(1);
246           aTruncatedCommand.Trunc(3);
247           
248           if (aTruncatedCommand == "OXY") {
249             vp.SetX(pref.X() + aProjectedLength * cos(anAngle));
250             vp.SetY(pref.Y() + aProjectedLength * sin(anAngle));
251             vp.SetZ(pref.Z() + aHeight);
252           }
253           else if (aTruncatedCommand == "OYZ") {
254             vp.SetX(pref.X() + aHeight);
255             vp.SetY(pref.Y() + aProjectedLength * cos(anAngle));
256             vp.SetZ(pref.Z() + aProjectedLength * sin(anAngle));
257           }
258           else if (aTruncatedCommand == "OXZ") {
259             vp.SetX(pref.X() + aProjectedLength * cos(anAngle));
260             vp.SetY(pref.Y() + aHeight);
261             vp.SetZ(pref.Z() + aProjectedLength * sin(anAngle));
262           }
263           else
264             Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
265           
266           if ((vp - p).SquareModulus() > Precision::Confusion()) {
267             BRepBuilderAPI_MakeVertex MV (vp);
268             TopoDS_Vertex VV = TopoDS::Vertex(MV.Shape());
269             BRepBuilderAPI_MakeEdge ME (V, VV);
270             MW.Add(ME);
271             nbEdges++;
272
273             p = vp;
274             V = VV;
275           }
276         }
277         break;
278       case 'W':
279         {
280           if (aStrVals.Length() != 1)
281             Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
282
283           if (aStrVals.Value(1) == "WW")
284             doClose = true;
285           else
286             Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
287         }
288         break;
289       default:
290         {
291           Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
292         }
293       }
294     }
295
296     if (doClose &&
297         nbEdges > 1 && // as 3D sketcher has only straight edges
298         (p - p0).SquareModulus() > Precision::Confusion()) {
299       BRepBuilderAPI_MakeEdge ME (V, V0);
300       MW.Add(ME);
301       nbEdges++;
302     }
303
304     if (nbEdges > 0) {
305       if (!MW.IsDone())
306         Standard_ConstructionError::Raise("3D Sketcher error: Wire construction failed.");
307
308       aShape = MW;
309     }
310     else {
311       if (isFirstPointSet) {
312         aShape = V0;
313       }
314     }
315   }
316   else {
317   }
318  
319   if (aShape.IsNull()) return 0;
320
321   aFunction->SetValue(aShape);
322   log->SetTouched(Label());
323   return 1;
324 }
325
326 //================================================================================
327 /*!
328  * \brief Returns a name of creation operation and names and values of creation parameters
329  */
330 //================================================================================
331
332 bool GEOMImpl_3DSketcherDriver::
333 GetCreationInformation(std::string&             theOperationName,
334                        std::vector<GEOM_Param>& theParams)
335 {
336   if (Label().IsNull()) return 0;
337   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
338
339   GEOMImpl_I3DSketcher aCI( function );
340   Standard_Integer aType = function->GetType();
341
342   theOperationName = "3DSKETCH";
343
344   if (aType == SKETCHER3D_COORDS)
345   {
346     theParams.resize( 1 );
347     theParams[0].Set( "(X,Y,Z)" );
348
349     Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
350     int anArrayLength = aCoordsArray->Length();
351     for (int i = 0; i <= (anArrayLength-3); i += 3)
352       theParams[0] << "( "
353                    << aCoordsArray->Value(i+1) << ", "
354                    << aCoordsArray->Value(i+2) << ", "
355                    << aCoordsArray->Value(i+3) << " ) ";
356   }
357   else if (aType == SKETCHER3D_COMMAND) 
358   {
359     AddParam( theParams, "Command", aCI.GetCommand() );
360   }
361   else {
362     return false;
363   }
364   
365   return true;
366 }
367
368 IMPLEMENT_STANDARD_RTTIEXT(GEOMImpl_3DSketcherDriver,GEOM_BaseDriver)