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