Salome HOME
Mantis issue 0021532: EDF 2202 GEOM: Problem when restoring groups containing a singl...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PolylineDriver.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
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_PolylineDriver.hxx>
26 #include <GEOMImpl_IPolyline.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <TColgp_Array1OfPnt.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <BRepBuilderAPI_MakePolygon.hxx>
33 #include <BRep_Tool.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Wire.hxx>
38 #include <TopAbs.hxx>
39 #include <TopExp.hxx>
40
41 #include <Precision.hxx>
42 #include <gp_Pnt.hxx>
43
44 //=======================================================================
45 //function : GetID
46 //purpose  :
47 //======================================================================= 
48 const Standard_GUID& GEOMImpl_PolylineDriver::GetID()
49 {
50   static Standard_GUID aPolylineDriver("FF1BBB31-5D14-4df2-980B-3A668264EA16");
51   return aPolylineDriver; 
52 }
53
54
55 //=======================================================================
56 //function : GEOMImpl_PolylineDriver
57 //purpose  : 
58 //=======================================================================
59 GEOMImpl_PolylineDriver::GEOMImpl_PolylineDriver() 
60 {
61 }
62
63 //=======================================================================
64 //function : Execute
65 //purpose  :
66 //======================================================================= 
67 Standard_Integer GEOMImpl_PolylineDriver::Execute(TFunction_Logbook& log) const
68 {
69   if (Label().IsNull()) return 0;    
70   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
71
72   GEOMImpl_IPolyline aCI (aFunction);
73   Standard_Integer aType = aFunction->GetType();
74   
75   TopoDS_Shape aShape;
76
77   if (aType == POLYLINE_POINTS) {
78
79     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
80     TColgp_Array1OfPnt points(1, (useCoords ? aCI.GetLength() : 1) );
81     if(useCoords) {
82       Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
83       int anArrayLength = aCoordsArray->Length();
84       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
85         gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
86         points.SetValue(j,aPnt);
87         j++;
88       } 
89     }
90
91     int aLen = aCI.GetLength();
92     int ind = 1;
93     BRepBuilderAPI_MakePolygon aMakePoly;
94     for (; ind <= aLen; ind++)
95     {
96       if(useCoords) {
97         aMakePoly.Add(BRepBuilderAPI_MakeVertex(points.Value(ind)));
98       } else {
99         Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
100         TopoDS_Shape aShapePnt = aRefPoint->GetValue();
101         if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
102           Standard_TypeMismatch::Raise
103             ("Polyline creation aborted : arguments are not a vertexes");
104           return 0;
105         }
106         if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
107           aMakePoly.Add(TopoDS::Vertex(aShapePnt));
108           //if (!aMakePoly.Added()) return 0;
109         }
110       }
111     }
112     // Compare first and last point coordinates and close polyline if it's the same.
113     if ( aLen > 2 ) {
114       TopoDS_Vertex aV1;
115       if( useCoords ) {
116         aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
117       } else {
118         Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
119         TopoDS_Shape aFirstPnt = aFPoint->GetValue();
120         aV1 = TopoDS::Vertex(aFirstPnt);
121       }
122       
123       TopoDS_Vertex aV2;
124       if( useCoords ) {
125         aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
126       } else {
127         Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
128         TopoDS_Shape aLastPnt = aLPoint->GetValue();
129         aV2 = TopoDS::Vertex(aLastPnt);
130       }
131       
132       if ( (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2)) ||
133            aCI.GetIsClosed())
134         aMakePoly.Close();
135     }
136     
137     if (aMakePoly.IsDone()) {
138       aShape = aMakePoly.Wire();
139     }
140   }
141   else {
142   }
143
144   if (aShape.IsNull()) return 0;
145   
146   aFunction->SetValue(aShape);
147   
148   log.SetTouched(Label()); 
149   
150   return 1;    
151 }
152
153
154 //=======================================================================
155 //function :  GEOMImpl_PolylineDriver_Type_
156 //purpose  :
157 //======================================================================= 
158 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PolylineDriver_Type_()
159 {
160
161   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
162   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
163   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
164   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
165   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
166   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
167  
168
169   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
170   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PolylineDriver",
171                                                          sizeof(GEOMImpl_PolylineDriver),
172                                                          1,
173                                                          (Standard_Address)_Ancestors,
174                                                          (Standard_Address)NULL);
175
176   return _aType;
177 }
178
179 //=======================================================================
180 //function : DownCast
181 //purpose  :
182 //======================================================================= 
183 const Handle(GEOMImpl_PolylineDriver) Handle(GEOMImpl_PolylineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
184 {
185   Handle(GEOMImpl_PolylineDriver) _anOtherObject;
186
187   if (!AnObject.IsNull()) {
188      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PolylineDriver))) {
189        _anOtherObject = Handle(GEOMImpl_PolylineDriver)((Handle(GEOMImpl_PolylineDriver)&)AnObject);
190      }
191   }
192
193   return _anOtherObject ;
194 }