Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PositionDriver.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_PositionDriver.hxx>
24
25 #include <GEOMImpl_IPosition.hxx>
26 #include <GEOMImpl_Types.hxx>
27
28 #include <GEOM_Function.hxx>
29
30 #include <GEOMUtils.hxx>
31
32 // OCCT Includes
33 #include <BRepBuilderAPI_Transform.hxx>
34 #include <ShHealOper_EdgeDivide.hxx>
35 #include <BRep_Tool.hxx>
36 #include <BRepTools.hxx>
37 #include <BRepFill_LocationLaw.hxx>
38 #include <BRepFill_Edge3DLaw.hxx>
39 #include <BRepFill_SectionPlacement.hxx>
40 #include <BRepTools_WireExplorer.hxx>
41 #include <BRepBuilderAPI_MakeWire.hxx>
42 #include <BRepBuilderAPI_MakeVertex.hxx>
43 #include <TopoDS.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Wire.hxx>
48 #include <TopAbs.hxx>
49 #include <TopExp.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <gp_Pln.hxx>
52 #include <Geom_Plane.hxx>
53 #include <Geom_Curve.hxx>
54 #include <GProp_GProps.hxx>
55 #include <BRepGProp.hxx>
56 #include <ShapeAnalysis_Edge.hxx>
57 #include <GeomAdaptor_Curve.hxx>
58 #include <BRepGProp.hxx>
59 #include <ShapeFix_Wire.hxx>
60
61 #include <GeomFill_TrihedronLaw.hxx>
62 #include <GeomFill_CurveAndTrihedron.hxx>
63 #include <GeomFill_CorrectedFrenet.hxx>
64
65 #include <Precision.hxx>
66 #include <gp_Pnt.hxx>
67 #include <gp_Vec.hxx>
68 #include <TopExp.hxx>
69
70 //=======================================================================
71 //function : GetID
72 //purpose  :
73 //=======================================================================
74 const Standard_GUID& GEOMImpl_PositionDriver::GetID()
75 {
76   static Standard_GUID aPositionDriver("FF1BBB69-5D14-4df2-980B-3A668264EA16");
77   return aPositionDriver;
78 }
79
80
81 //=======================================================================
82 //function : GEOMImpl_PositionDriver
83 //purpose  :
84 //=======================================================================
85 GEOMImpl_PositionDriver::GEOMImpl_PositionDriver()
86 {
87 }
88
89 //=======================================================================
90 //function : Execute
91 //purpose  :
92 //=======================================================================
93 Standard_Integer GEOMImpl_PositionDriver::Execute(TFunction_Logbook& log) const
94 {
95   if (Label().IsNull()) return 0;
96   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
97
98   GEOMImpl_IPosition aCI (aFunction);
99   Standard_Integer aType = aFunction->GetType();
100
101   TopoDS_Shape aShape;
102
103   if (aType == POSITION_SHAPE || aType == POSITION_SHAPE_COPY) {
104     Handle(GEOM_Function) aRefShape = aCI.GetShape();
105     Handle(GEOM_Function) aRefStartLCS = aCI.GetStartLCS();
106     Handle(GEOM_Function) aRefEndLCS = aCI.GetEndLCS();
107
108     TopoDS_Shape aShapeBase = aRefShape->GetValue();
109     TopoDS_Shape aShapeStartLCS = aRefStartLCS->GetValue();
110     TopoDS_Shape aShapeEndLCS = aRefEndLCS->GetValue();
111
112     if (aShapeBase.IsNull() || aShapeStartLCS.IsNull() ||
113         aShapeEndLCS.IsNull() || aShapeEndLCS.ShapeType() != TopAbs_FACE)
114       return 0;
115
116     gp_Trsf aTrsf;
117     gp_Ax3 aStartAx3, aDestAx3;
118
119     // End LCS
120     aDestAx3 = GEOMUtils::GetPosition(aShapeEndLCS);
121
122     // Start LCS
123     aStartAx3 = GEOMUtils::GetPosition(aShapeStartLCS);
124
125     // Set transformation
126     aTrsf.SetDisplacement(aStartAx3, aDestAx3);
127
128     // Perform transformation
129     BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
130     aShape = aBRepTrsf.Shape();
131   }
132   else if (aType == POSITION_SHAPE_FROM_GLOBAL ||
133            aType == POSITION_SHAPE_FROM_GLOBAL_COPY) {
134     Handle(GEOM_Function) aRefShape = aCI.GetShape();
135     Handle(GEOM_Function) aRefEndLCS = aCI.GetEndLCS();
136
137     TopoDS_Shape aShapeBase = aRefShape->GetValue();
138     TopoDS_Shape aShapeEndLCS = aRefEndLCS->GetValue();
139
140     if (aShapeBase.IsNull() || aShapeEndLCS.IsNull() ||
141         aShapeEndLCS.ShapeType() != TopAbs_FACE)
142       return 0;
143
144     gp_Trsf aTrsf;
145     gp_Ax3 aStartAx3, aDestAx3;
146
147     // End LCS
148     aDestAx3 = GEOMUtils::GetPosition(aShapeEndLCS);
149
150     // Set transformation
151     aTrsf.SetDisplacement(aStartAx3, aDestAx3);
152
153     // Perform transformation
154     BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
155     aShape = aBRepTrsf.Shape();
156   }
157   else if (aType == POSITION_ALONG_PATH) {
158     Handle(GEOM_Function) aRefShape = aCI.GetShape();
159     Handle(GEOM_Function) aPathShape = aCI.GetPath();
160     Standard_Real aParameter = aCI.GetDistance();
161     bool aReversed = aCI.GetReverse();
162     if (aReversed)
163       aParameter = 1 - aParameter;
164
165     TopoDS_Shape aShapeBase = aRefShape->GetValue();
166     TopoDS_Shape aPath = aPathShape->GetValue();
167     TopoDS_Wire aWire;
168
169     if (aShapeBase.IsNull() || aPath.IsNull())
170       return 0;
171
172     if ( aPath.ShapeType() == TopAbs_EDGE ) {
173       TopoDS_Edge anEdge = TopoDS::Edge(aPath);
174       aWire = BRepBuilderAPI_MakeWire(anEdge); 
175     }
176     else if ( aPath.ShapeType() == TopAbs_WIRE)
177       aWire = TopoDS::Wire(aPath);
178     else
179       return 0;
180
181     Handle(GeomFill_TrihedronLaw) TLaw = new GeomFill_CorrectedFrenet();
182     Handle(GeomFill_CurveAndTrihedron) aLocationLaw = new GeomFill_CurveAndTrihedron( TLaw );
183     Handle(BRepFill_LocationLaw) aLocation = new BRepFill_Edge3DLaw(aWire, aLocationLaw);
184
185     aLocation->TransformInCompatibleLaw( 0.01 );
186
187     //Calculate a Parameter
188     Standard_Real aFirstParam1 = 0, aLastParam1 = 0; // Parameters of the First edge
189     Standard_Real aFirstParam2 = 0, aLastParam2 = 0; // Parameters of the Last edge
190     aLocation->CurvilinearBounds(aLocation->NbLaw(), aFirstParam2, aLastParam2);
191
192     if ( aLocation->NbLaw() > 1)
193       aLocation->CurvilinearBounds(1, aFirstParam1, aLastParam1);
194     else if ( aLocation->NbLaw() == 1 )
195       aFirstParam1 = aFirstParam2;
196     else
197       return 0;
198
199     Standard_Real aParam = (aFirstParam1 + (aLastParam2 - aFirstParam1)*aParameter );
200
201     TopoDS_Shape CopyShape = aShapeBase;
202     BRepFill_SectionPlacement Place( aLocation, aShapeBase );
203     TopLoc_Location Loc2(Place.Transformation()), Loc1;
204     Loc1 = CopyShape.Location();
205     CopyShape.Location(Loc2.Multiplied(Loc1));
206
207     aLocation->D0( aParam, CopyShape );
208     aShape = CopyShape;
209   }
210   else
211     return 0;
212
213   if (aShape.IsNull()) return 0;
214
215   aFunction->SetValue(aShape);
216
217   log.SetTouched(Label());
218
219   return 1;
220 }
221
222
223 //=======================================================================
224 //function :  GEOMImpl_PositionDriver_Type_
225 //purpose  :
226 //=======================================================================
227 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PositionDriver_Type_()
228 {
229
230   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
231   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
232   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
233   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
234   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
235   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
236
237
238   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
239   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PositionDriver",
240                                                          sizeof(GEOMImpl_PositionDriver),
241                                                          1,
242                                                          (Standard_Address)_Ancestors,
243                                                          (Standard_Address)NULL);
244
245   return _aType;
246 }
247
248 //=======================================================================
249 //function : DownCast
250 //purpose  :
251 //=======================================================================
252 const Handle(GEOMImpl_PositionDriver) Handle(GEOMImpl_PositionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
253 {
254   Handle(GEOMImpl_PositionDriver) _anOtherObject;
255
256   if (!AnObject.IsNull()) {
257      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PositionDriver))) {
258        _anOtherObject = Handle(GEOMImpl_PositionDriver)((Handle(GEOMImpl_PositionDriver)&)AnObject);
259      }
260   }
261
262   return _anOtherObject ;
263 }