Salome HOME
f18427df9c1a607622e69a176b57d691ef107301
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PolylineDriver.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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_PolylineDriver.hxx>
25 #include <GEOMImpl_IPolyline.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <BRepBuilderAPI_MakePolygon.hxx>
30 #include <BRep_Tool.hxx>
31 #include <TopoDS.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <TopAbs.hxx>
36 #include <TopExp.hxx>
37
38 #include <Precision.hxx>
39 #include <gp_Pnt.hxx>
40
41 //=======================================================================
42 //function : GetID
43 //purpose  :
44 //======================================================================= 
45 const Standard_GUID& GEOMImpl_PolylineDriver::GetID()
46 {
47   static Standard_GUID aPolylineDriver("FF1BBB31-5D14-4df2-980B-3A668264EA16");
48   return aPolylineDriver; 
49 }
50
51
52 //=======================================================================
53 //function : GEOMImpl_PolylineDriver
54 //purpose  : 
55 //=======================================================================
56 GEOMImpl_PolylineDriver::GEOMImpl_PolylineDriver() 
57 {
58 }
59
60 //=======================================================================
61 //function : Execute
62 //purpose  :
63 //======================================================================= 
64 Standard_Integer GEOMImpl_PolylineDriver::Execute(TFunction_Logbook& log) const
65 {
66   if (Label().IsNull()) return 0;    
67   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
68
69   GEOMImpl_IPolyline aCI (aFunction);
70   Standard_Integer aType = aFunction->GetType();
71
72   TopoDS_Shape aShape;
73
74   if (aType == POLYLINE_POINTS) {
75     int aLen = aCI.GetLength();
76     int ind = 1;
77     BRepBuilderAPI_MakePolygon aMakePoly;
78     for (; ind <= aLen; ind++)
79     {
80       Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
81       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
82       if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
83         aMakePoly.Add(TopoDS::Vertex(aShapePnt));
84 //        if (!aMakePoly.Added()) return 0;
85       }
86     }
87     // Compare first and last point coordinates and close polyline if it's the same.
88     if ( aLen > 2 ) {
89       Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
90       TopoDS_Shape aFirstPnt = aFPoint->GetValue();
91       TopoDS_Vertex aV1 = TopoDS::Vertex(aFirstPnt);
92
93       Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
94       TopoDS_Shape aLastPnt = aLPoint->GetValue();
95       TopoDS_Vertex aV2 = TopoDS::Vertex(aLastPnt);
96
97       if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
98         aMakePoly.Close();
99     }
100
101     if (aMakePoly.IsDone()) {
102       aShape = aMakePoly.Wire();
103     }
104   }
105   else {
106   }
107
108   if (aShape.IsNull()) return 0;
109
110   aFunction->SetValue(aShape);
111
112   log.SetTouched(Label()); 
113
114   return 1;    
115 }
116
117
118 //=======================================================================
119 //function :  GEOMImpl_PolylineDriver_Type_
120 //purpose  :
121 //======================================================================= 
122 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PolylineDriver_Type_()
123 {
124
125   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
126   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
127   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
128   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
129   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
130   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
131  
132
133   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
134   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PolylineDriver",
135                                                          sizeof(GEOMImpl_PolylineDriver),
136                                                          1,
137                                                          (Standard_Address)_Ancestors,
138                                                          (Standard_Address)NULL);
139
140   return _aType;
141 }
142
143 //=======================================================================
144 //function : DownCast
145 //purpose  :
146 //======================================================================= 
147 const Handle(GEOMImpl_PolylineDriver) Handle(GEOMImpl_PolylineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
148 {
149   Handle(GEOMImpl_PolylineDriver) _anOtherObject;
150
151   if (!AnObject.IsNull()) {
152      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PolylineDriver))) {
153        _anOtherObject = Handle(GEOMImpl_PolylineDriver)((Handle(GEOMImpl_PolylineDriver)&)AnObject);
154      }
155   }
156
157   return _anOtherObject ;
158 }