Salome HOME
77ab5dbf12e471cb5bdbc92f954fd0c17899b225
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PositionDriver.cxx
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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(Handle(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  * \brief Returns a name of creation operation and names and values of creation parameters
225  */
226 //================================================================================
227
228 bool GEOMImpl_PositionDriver::
229 GetCreationInformation(std::string&             theOperationName,
230                        std::vector<GEOM_Param>& theParams)
231 {
232   if (Label().IsNull()) return 0;
233   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
234
235   GEOMImpl_IPosition aCI( function );
236   Standard_Integer aType = function->GetType();
237
238   theOperationName = "MODIFY_LOCATION";
239
240   switch ( aType ) {
241   case POSITION_SHAPE:
242   case POSITION_SHAPE_COPY:
243     AddParam( theParams, "Object", aCI.GetShape() );
244     AddParam( theParams, "Start LCS", aCI.GetStartLCS() );
245     AddParam( theParams, "End LCS", aCI.GetEndLCS() );
246     break;
247   case POSITION_SHAPE_FROM_GLOBAL:
248   case POSITION_SHAPE_FROM_GLOBAL_COPY:
249     AddParam( theParams, "Object", aCI.GetShape() );
250     AddParam( theParams, "End LCS", aCI.GetEndLCS() );
251     break;
252   case POSITION_ALONG_PATH:
253     AddParam( theParams, "Object", aCI.GetShape() );
254     AddParam( theParams, "Path", aCI.GetPath() );
255     AddParam( theParams, "Distance", aCI.GetDistance() );
256     AddParam( theParams, "Reverse Direction", aCI.GetReverse() );
257     break;
258   default:
259     return false;
260   }
261   
262   return true;
263 }
264
265 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PositionDriver,GEOM_BaseDriver)