1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_ProjectionDriver.hxx>
27 #include <GEOMImpl_IMirror.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
31 #include <BRep_Tool.hxx>
32 #include <BRepBuilderAPI_Transform.hxx>
33 #include <BRepBuilderAPI_MakeVertex.hxx>
34 #include <BRepClass_FaceClassifier.hxx>
35 #include <BRepOffsetAPI_NormalProjection.hxx>
36 #include <BRepTools.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <GeomAPI_ProjectPointOnSurf.hxx>
48 #include <Geom_Plane.hxx>
50 #include <gp_Trsf.hxx>
54 //=======================================================================
57 //=======================================================================
58 const Standard_GUID& GEOMImpl_ProjectionDriver::GetID()
60 static Standard_GUID aProjectionDriver ("FF1BBB70-5D14-4df2-980B-3A668264EA16");
61 return aProjectionDriver;
65 //=======================================================================
66 //function : GEOMImpl_ProjectionDriver
68 //=======================================================================
70 GEOMImpl_ProjectionDriver::GEOMImpl_ProjectionDriver()
74 //=======================================================================
77 //=======================================================================
78 Standard_Integer GEOMImpl_ProjectionDriver::Execute(TFunction_Logbook& log) const
80 if (Label().IsNull()) return 0;
81 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
83 if (aFunction.IsNull()) return 0;
88 GEOMImpl_IMirror TI (aFunction);
89 Standard_Integer aType = aFunction->GetType();
91 Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
92 if (anOriginalFunction.IsNull()) return 0;
94 TopoDS_Shape anOriginal = anOriginalFunction->GetValue();
95 if (anOriginal.IsNull()) return 0;
98 if (aType == PROJECTION_COPY) {
99 // Source shape (point, edge or wire)
100 if (anOriginal.ShapeType() != TopAbs_VERTEX &&
101 anOriginal.ShapeType() != TopAbs_EDGE &&
102 anOriginal.ShapeType() != TopAbs_WIRE) {
103 Standard_ConstructionError::Raise
104 ("Projection aborted : the source shape is neither a vertex, nor an edge or a wire");
108 Handle(GEOM_Function) aTargetFunction = TI.GetPlane();
109 if (aTargetFunction.IsNull()) return 0;
110 TopoDS_Shape aFaceShape = aTargetFunction->GetValue();
111 //if (aFaceShape.IsNull() || aFaceShape.ShapeType() != TopAbs_FACE) {
112 // Standard_ConstructionError::Raise
113 // ("Projection aborted : the target shape is not a face");
116 Standard_Real tol = 1.e-4;
118 if (anOriginal.ShapeType() == TopAbs_VERTEX) {
119 if (aFaceShape.IsNull() || aFaceShape.ShapeType() != TopAbs_FACE) {
120 Standard_ConstructionError::Raise
121 ("Projection aborted : the target shape is not a face");
123 TopoDS_Face aFace = TopoDS::Face(aFaceShape);
124 Handle(Geom_Surface) surface = BRep_Tool::Surface(aFace);
125 double U1, U2, V1, V2;
126 //surface->Bounds(U1, U2, V1, V2);
127 BRepTools::UVBounds(aFace, U1, U2, V1, V2);
130 GeomAPI_ProjectPointOnSurf proj;
131 proj.Init(surface, U1, U2, V1, V2, tol);
133 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(anOriginal));
135 if (!proj.IsDone()) {
136 Standard_ConstructionError::Raise
137 ("Projection aborted : the algorithm failed");
139 int nbPoints = proj.NbPoints();
141 Standard_ConstructionError::Raise("No solution found");
144 Quantity_Parameter U, V;
145 proj.LowerDistanceParameters(U, V);
146 gp_Pnt2d aProjPnt (U, V);
149 BRepClass_FaceClassifier aClsf (aFace, aProjPnt, tol);
150 if (aClsf.State() != TopAbs_IN && aClsf.State() != TopAbs_ON) {
152 double minDist = RealLast();
153 for (int i = 1; i <= nbPoints; i++) {
154 Quantity_Parameter Ui, Vi;
155 proj.Parameters(i, Ui, Vi);
156 aProjPnt = gp_Pnt2d(Ui, Vi);
157 aClsf.Perform(aFace, aProjPnt, tol);
158 if (aClsf.State() == TopAbs_IN || aClsf.State() == TopAbs_ON) {
160 double dist = proj.Distance(i);
161 if (dist < minDist) {
169 Standard_ConstructionError::Raise("No solution found");
173 gp_Pnt surfPnt = surface->Value(U, V);
175 aShape = BRepBuilderAPI_MakeVertex(surfPnt).Shape();
178 //see BRepTest_BasicCommands.cxx for example of BRepOffsetAPI_NormalProjection
179 BRepOffsetAPI_NormalProjection OrtProj (aFaceShape);
180 OrtProj.Add(anOriginal);
182 //Standard_Real tol = 1.e-4;
183 //Standard_Real tol2d = Pow(tol, 2./3);
184 //GeomAbs_Shape Continuity = GeomAbs_C2;
185 //Standard_Integer MaxDeg = 14;
186 //Standard_Integer MaxSeg = 16;
187 //OrtProj.SetParams(tol, tol2d, Continuity, MaxDeg, MaxSeg);
190 } catch (Standard_Failure) {
191 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
192 TCollection_AsciiString aMsg (aFail->GetMessageString());
194 aMsg = "Projection aborted : possibly the source shape intersects the cylinder's axis";
195 Standard_ConstructionError::Raise(aMsg.ToCString());
197 if (!OrtProj.IsDone()) {
198 Standard_ConstructionError::Raise
199 ("Projection aborted : BRepOffsetAPI_NormalProjection failed");
202 aShape = OrtProj.Shape();
204 // check that the result shape is an empty compound
205 // (IPAL22905: TC650: Projection on face dialog problems)
206 if( !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND )
208 TopoDS_Iterator anIter( aShape );
210 Standard_ConstructionError::Raise("Projection aborted : empty compound produced");
214 if (aShape.IsNull()) return 0;
216 aFunction->SetValue(aShape);
217 log.SetTouched(Label());
224 //=======================================================================
225 //function : GEOMImpl_ProjectionDriver_Type_
227 //=======================================================================
228 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ProjectionDriver_Type_()
231 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
232 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
233 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
234 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
235 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
236 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
239 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
240 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ProjectionDriver",
241 sizeof(GEOMImpl_ProjectionDriver),
243 (Standard_Address)_Ancestors,
244 (Standard_Address)NULL);
249 //=======================================================================
250 //function : DownCast
252 //=======================================================================
254 const Handle(GEOMImpl_ProjectionDriver) Handle(GEOMImpl_ProjectionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
256 Handle(GEOMImpl_ProjectionDriver) _anOtherObject;
258 if (!AnObject.IsNull()) {
259 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ProjectionDriver))) {
260 _anOtherObject = Handle(GEOMImpl_ProjectionDriver)((Handle(GEOMImpl_ProjectionDriver)&)AnObject);
264 return _anOtherObject;