Salome HOME
Merge from BR_V7_main_Field branch (02/09/2013)
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PolylineDriver.cxx
1 // Copyright (C) 2007-2013  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 #include "GEOMImpl_PolylineDriver.hxx"
24
25 #include "GEOMImpl_ICurveParametric.hxx"
26 #include "GEOMImpl_IPolyline.hxx"
27 #include "GEOMImpl_Types.hxx"
28 #include "GEOM_Function.hxx"
29
30 #include <BRepBuilderAPI_MakePolygon.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <BRep_Tool.hxx>
33 #include <Precision.hxx>
34 #include <TColgp_Array1OfPnt.hxx>
35 #include <TopAbs.hxx>
36 #include <TopExp.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS_Wire.hxx>
41 #include <gp_Pnt.hxx>
42
43 //=======================================================================
44 //function : GetID
45 //purpose  :
46 //======================================================================= 
47 const Standard_GUID& GEOMImpl_PolylineDriver::GetID()
48 {
49   static Standard_GUID aPolylineDriver("FF1BBB31-5D14-4df2-980B-3A668264EA16");
50   return aPolylineDriver; 
51 }
52
53
54 //=======================================================================
55 //function : GEOMImpl_PolylineDriver
56 //purpose  : 
57 //=======================================================================
58 GEOMImpl_PolylineDriver::GEOMImpl_PolylineDriver() 
59 {
60 }
61
62 //=======================================================================
63 //function : Execute
64 //purpose  :
65 //======================================================================= 
66 Standard_Integer GEOMImpl_PolylineDriver::Execute(TFunction_Logbook& log) const
67 {
68   if (Label().IsNull()) return 0;    
69   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
70
71   GEOMImpl_IPolyline aCI (aFunction);
72   Standard_Integer aType = aFunction->GetType();
73   
74   TopoDS_Shape aShape;
75
76   if (aType == POLYLINE_POINTS) {
77
78     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
79     TColgp_Array1OfPnt points(1, (useCoords ? aCI.GetLength() : 1) );
80     if(useCoords) {
81       Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
82       int anArrayLength = aCoordsArray->Length();
83       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
84         gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
85         points.SetValue(j,aPnt);
86         j++;
87       } 
88     }
89
90     int aLen = aCI.GetLength();
91     int ind = 1;
92     BRepBuilderAPI_MakePolygon aMakePoly;
93     for (; ind <= aLen; ind++)
94     {
95       if(useCoords) {
96         aMakePoly.Add(BRepBuilderAPI_MakeVertex(points.Value(ind)));
97       } else {
98         Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
99         TopoDS_Shape aShapePnt = aRefPoint->GetValue();
100         if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
101           Standard_TypeMismatch::Raise
102             ("Polyline creation aborted : arguments are not a vertexes");
103           return 0;
104         }
105         if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
106           aMakePoly.Add(TopoDS::Vertex(aShapePnt));
107           //if (!aMakePoly.Added()) return 0;
108         }
109       }
110     }
111     // Compare first and last point coordinates and close polyline if it's the same.
112     if ( aLen > 2 ) {
113       TopoDS_Vertex aV1;
114       if( useCoords ) {
115         aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
116       } else {
117         Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
118         TopoDS_Shape aFirstPnt = aFPoint->GetValue();
119         aV1 = TopoDS::Vertex(aFirstPnt);
120       }
121       
122       TopoDS_Vertex aV2;
123       if( useCoords ) {
124         aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
125       } else {
126         Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
127         TopoDS_Shape aLastPnt = aLPoint->GetValue();
128         aV2 = TopoDS::Vertex(aLastPnt);
129       }
130       
131       if ( (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2)) ||
132            aCI.GetIsClosed())
133         aMakePoly.Close();
134     }
135     
136     if (aMakePoly.IsDone()) {
137       aShape = aMakePoly.Wire();
138     }
139   }
140   else {
141   }
142
143   if (aShape.IsNull()) return 0;
144   
145   aFunction->SetValue(aShape);
146   
147   log.SetTouched(Label()); 
148   
149   return 1;    
150 }
151
152 //================================================================================
153 /*!
154  * \brief Returns a name of creation operation and names and values of creation parameters
155  */
156 //================================================================================
157
158 bool GEOMImpl_PolylineDriver::
159 GetCreationInformation(std::string&             theOperationName,
160                        std::vector<GEOM_Param>& theParams)
161 {
162   if (Label().IsNull()) return 0;
163   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
164
165   GEOMImpl_IPolyline        aCI( function );
166   GEOMImpl_ICurveParametric aIP( function );
167   Standard_Integer aType = function->GetType();
168
169   theOperationName = "CURVE";
170
171   switch ( aType ) {
172   case POLYLINE_POINTS:
173     AddParam( theParams, "Type", "Polyline");
174     if ( aIP.HasData() )
175     {
176       AddParam( theParams, "X(t) equation", aIP.GetExprX() );
177       AddParam( theParams, "Y(t) equation", aIP.GetExprY() );
178       AddParam( theParams, "Z(t) equation", aIP.GetExprZ() );
179       AddParam( theParams, "Min t", aIP.GetParamMin() );
180       AddParam( theParams, "Max t", aIP.GetParamMax() );
181       if ( aIP.GetParamNbStep() )
182         AddParam( theParams, "Number of steps", aIP.GetParamNbStep() );
183       else
184         AddParam( theParams, "t step", aIP.GetParamStep() );
185     }
186     else
187     {
188       GEOM_Param& pntParam = AddParam( theParams, "Points");
189       if ( aCI.GetConstructorType() == COORD_CONSTRUCTOR )
190       {
191         Handle(TColStd_HArray1OfReal) coords = aCI.GetCoordinates();
192         if ( coords->Length() > 3 )
193           pntParam << ( coords->Length() ) / 3 << " points: ";
194         for ( int i = coords->Lower(), nb = coords->Upper(); i <= nb; )
195           pntParam << "( " << coords->Value( i++ )
196                    << ", " << coords->Value( i++ )
197                    << ", " << coords->Value( i++ ) << " ) ";
198       }
199       else
200       {
201         if ( aCI.GetLength() > 1 )
202           pntParam << aCI.GetLength() << " points: ";
203         for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
204           pntParam << aCI.GetPoint( i ) << " ";
205       }
206       AddParam( theParams, "Is closed", aCI.GetIsClosed() );
207     }
208     break;
209   default:
210     return false;
211   }
212
213   return true;
214 }
215
216 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_PolylineDriver,GEOM_BaseDriver);
217 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PolylineDriver,GEOM_BaseDriver);