0026493: BRepProj_Projection failed to project a wire on a shell
[occt.git] / src / BRepProj / BRepProj_Projection.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
b311480e 14
7fd59977 15
42cf5bc1 16#include <Bnd_Box.hxx>
17#include <BRep_Builder.hxx>
18#include <BRep_Tool.hxx>
7dfac274 19#include <BRepAlgoAPI_Section.hxx>
7fd59977 20#include <BRepBndLib.hxx>
42cf5bc1 21#include <BRepFill_Generator.hxx>
7fd59977 22#include <BRepLib_MakeEdge.hxx>
42cf5bc1 23#include <BRepLib_MakeVertex.hxx>
7fd59977 24#include <BRepLib_MakeWire.hxx>
42cf5bc1 25#include <BRepProj_Projection.hxx>
7fd59977 26#include <BRepSweep_Prism.hxx>
42cf5bc1 27#include <BRepTools_Modifier.hxx>
28#include <BRepTools_TrsfModification.hxx>
7fd59977 29#include <gp_Dir.hxx>
30#include <gp_Pnt.hxx>
7fd59977 31#include <gp_Trsf.hxx>
42cf5bc1 32#include <gp_Vec.hxx>
33#include <Precision.hxx>
34#include <ShapeAnalysis_FreeBounds.hxx>
35#include <Standard_ConstructionError.hxx>
36#include <Standard_NoSuchObject.hxx>
37#include <Standard_NullObject.hxx>
38#include <TopExp.hxx>
39#include <TopExp_Explorer.hxx>
40#include <TopLoc_Location.hxx>
7fd59977 41#include <TopoDS.hxx>
42#include <TopoDS_Iterator.hxx>
43#include <TopoDS_Shape.hxx>
7fd59977 44#include <TopTools_ListIteratorOfListOfShape.hxx>
42cf5bc1 45#include <TopTools_ListOfShape.hxx>
7fd59977 46
47//=======================================================================
48//function : DistanceOut
49//purpose : Compute the minimum distance between input shapes
50// (using Bounding Boxes of each Shape)
51//=======================================================================
7fd59977 52static Standard_Real DistanceOut (const TopoDS_Shape& S1, const TopoDS_Shape& S2)
53{
54 Bnd_Box BBox1, BBox2;
55 BRepBndLib::Add(S1,BBox1);
56 BRepBndLib::Add(S2,BBox2);
57 return BBox1.Distance(BBox2);
58}
59
60//=======================================================================
61//function : DistanceIn
62//purpose : Compute the maximum distance between input Shapes
63// we compute the maximum dimension of each Bounding Box and then
64// add each other with the minimum distance of shapes.
65//=======================================================================
66
67static Standard_Real DistanceIn (const TopoDS_Shape& S1, const TopoDS_Shape& S2)
68{
69 Bnd_Box LBBox,SBBox;
70 BRepBndLib::Add(S1,SBBox);
71 BRepBndLib::Add(S2,LBBox);
72
73 Standard_Real LXmin, LYmin, LZmin, LXmax, LYmax, LZmax,
74 SXmin, SYmin, SZmin, SXmax, SYmax, SZmax;
75 SBBox.Get(SXmin, SYmin, SZmin,
76 SXmax, SYmax, SZmax);
77 LBBox.Get(LXmin, LYmin, LZmin,
78 LXmax, LYmax, LZmax);
79
80 //Compute the max distance between input shapes------------//
81 gp_XYZ Lmin(LXmin, LYmin, LZmin),
82 Lmax(LXmax, LYmax, LZmax);
83 gp_XYZ Smin(SXmin, SYmin, SZmin),
84 Smax(SXmax, SYmax, SZmax);
85 Lmax.Subtract(Lmin);
86 Smax.Subtract(Smin);
87 return Lmax.Modulus() + Smax.Modulus() + DistanceOut(S1, S2);
88}
89
90//=======================================================================
91//function : BuildSection
92//purpose : Cuts theShape by theTool using BRepAlgoAPI_Section and
93// stores result as set of connected wires and compound
94//=======================================================================
95
96void BRepProj_Projection::BuildSection (const TopoDS_Shape& theShape,
97 const TopoDS_Shape& theTool)
98{
99 myIsDone = Standard_False;
100 mySection.Nullify();
101 myShape.Nullify();
102 myItr = 0;
103
104 // if theShape is compound, extract only faces -- section algorithm
105 // may refuse to work if e.g. vertex is present
106 TopoDS_Shape aShape;
107 if (theShape.ShapeType() == TopAbs_FACE ||
108 theShape.ShapeType() == TopAbs_SHELL ||
109 theShape.ShapeType() == TopAbs_SOLID ||
110 theShape.ShapeType() == TopAbs_COMPSOLID)
111 aShape = theShape;
112 else if (theShape.ShapeType() == TopAbs_COMPOUND)
113 {
114 TopoDS_Compound C;
115 BRep_Builder B;
116 TopExp_Explorer exp (theShape, TopAbs_FACE);
117 for (; exp.More(); exp.Next())
118 {
119 if ( C.IsNull() )
120 B.MakeCompound (C);
121 B.Add (C, exp.Current());
122 }
123 aShape = C;
124 }
125 if ( aShape.IsNull() )
126 Standard_ConstructionError::Raise(__FILE__": target shape has no faces");
127
128 // build section computing pcurves on the shape
7dfac274 129 BRepAlgoAPI_Section aSectionTool (aShape, theTool, Standard_False);
7fd59977 130 aSectionTool.Approximation (Standard_True);
131 aSectionTool.ComputePCurveOn1 (Standard_True);
132 aSectionTool.Build();
133
134 // check for successful work of the section tool
135 if (! aSectionTool.IsDone())
136 return;
137
138 // get edges of the result
139 Handle(TopTools_HSequenceOfShape) anEdges = new TopTools_HSequenceOfShape;
140 TopExp_Explorer exp(aSectionTool.Shape(), TopAbs_EDGE);
141 for (; exp.More(); exp.Next())
142 anEdges->Append (exp.Current());
143
144 // if no edges are found, this means that this section yields no result
145 if (anEdges->Length() <= 0)
146 return;
147
148 // connect edges to wires using ShapeAnalysis functionality
149 ShapeAnalysis_FreeBounds::ConnectEdgesToWires (anEdges, Precision::Confusion(),
150 Standard_True, mySection);
151 myIsDone = (! mySection.IsNull() && mySection->Length() > 0);
152
153 // collect all resulting wires to compound
154 if ( myIsDone )
155 {
156 BRep_Builder B;
157 B.MakeCompound (myShape);
158 for (Standard_Integer i=1; i <= mySection->Length(); i++)
159 B.Add (myShape, mySection->Value(i));
160
161 // initialize iteration (for compatibility with previous versions)
162 myItr = 1;
163 }
164}
165
166//=======================================================================
167//function : BRepProj_Projection
168//purpose : Cylindrical Projection
169//=======================================================================
170
171BRepProj_Projection::BRepProj_Projection(const TopoDS_Shape& Wire,
172 const TopoDS_Shape& Shape,
173 const gp_Dir& D)
174: myIsDone(Standard_False), myItr(0)
175{
176 // Check the input
177 Standard_NullObject_Raise_if((Wire.IsNull() || Shape.IsNull()),__FILE__": null input shape");
178 if (Wire.ShapeType() != TopAbs_EDGE &&
179 Wire.ShapeType() != TopAbs_WIRE )
180 Standard_ConstructionError::Raise(__FILE__": projected shape is neither wire nor edge");
181
182 // compute the "length" of the cylindrical surface to build
183 Standard_Real mdis = DistanceIn(Wire, Shape);
184 gp_Vec Vsup (D.XYZ() * 2 * mdis);
185 gp_Vec Vinf (D.XYZ() * -mdis);
186
187 // move the base of the cylindrical surface by translating it by -mdis
188 gp_Trsf T;
189 T.SetTranslation(Vinf);
190 // Note: it is necessary to create copy of wire to avoid adding new pcurves into it
191 Handle(BRepTools_TrsfModification) Trsf = new BRepTools_TrsfModification(T);
192 BRepTools_Modifier Modif (Wire, Trsf);
193 TopoDS_Shape WireBase = Modif.ModifiedShape(Wire);
194
195 // Creation of a cylindrical surface
196 BRepSweep_Prism CylSurf (WireBase, Vsup, Standard_False);
197
198 // Perform section
199 BuildSection (Shape, CylSurf.Shape());
200}
201
202//=======================================================================
203//function : BRepProj_Projection
204//purpose : Conical projection
205//=======================================================================
206
207BRepProj_Projection::BRepProj_Projection (const TopoDS_Shape& Wire,
208 const TopoDS_Shape& Shape,
209 const gp_Pnt& P)
210: myIsDone(Standard_False), myItr(0)
211{
212 // Check the input
213 Standard_NullObject_Raise_if((Wire.IsNull() || Shape.IsNull()),__FILE__": null input shape");
214 if (Wire.ShapeType() != TopAbs_EDGE &&
215 Wire.ShapeType() != TopAbs_WIRE )
216 Standard_ConstructionError::Raise(__FILE__": projected shape is neither wire nor edge");
217
218 // if Wire is only an edge, transform it into a Wire
219 TopoDS_Wire aWire;
220 if (Wire.ShapeType() == TopAbs_EDGE)
221 {
222 BRep_Builder BB;
223 BB.MakeWire(aWire);
224 BB.Add(aWire, Wire);
225 }
226 else
227 aWire = TopoDS::Wire(Wire);
228
229 // compute the "length" of the conical surface to build
230 Standard_Real mdis = DistanceIn(Wire, Shape);
231
232 // Initialize iterator to get first sub-shape of Wire
233 TopExp_Explorer ExpWire;
234 ExpWire.Init (aWire, TopAbs_VERTEX);
235
236 // get the first Point of the first sub-shape os the Wire
237 gp_Pnt PC = BRep_Tool::Pnt(TopoDS::Vertex(ExpWire.Current()));
238
239 // compute the ratio of the scale transformation
240 Standard_Real Scale = PC.Distance(P);
241 if ( Abs (Scale) < Precision::Confusion() )
242 Standard_ConstructionError::Raise("Projection");
243 Scale = 1. + mdis / Scale;
244
245 // move the base of the conical surface by scaling it with ratio Scale
7fd59977 246 gp_Trsf T;
247 T.SetScale(P, Scale);
248 Handle(BRepTools_TrsfModification) Tsca = new BRepTools_TrsfModification(T);
249 BRepTools_Modifier ModifScale(aWire,Tsca);
250 TopoDS_Shape ShapeGen1 = ModifScale.ModifiedShape(aWire);
251
1f205411 252 TopoDS_Vertex aVertex = BRepLib_MakeVertex(P);
253 TopoDS_Edge DegEdge;
254 BRep_Builder BB;
255 BB.MakeEdge( DegEdge );
256 BB.Add( DegEdge, aVertex.Oriented(TopAbs_FORWARD) );
257 BB.Add( DegEdge, aVertex.Oriented(TopAbs_REVERSED) );
258 BB.Degenerated( DegEdge, Standard_True );
da72a17c 259
1f205411 260 TopoDS_Wire DegWire;
261 BB.MakeWire( DegWire );
262 BB.Add( DegWire, DegEdge );
263 DegWire.Closed( Standard_True );
7fd59977 264
265 // Build the Ruled surface based shape
266 BRepFill_Generator RuledSurf;
1f205411 267 RuledSurf.AddWire(DegWire);
7fd59977 268 RuledSurf.AddWire(TopoDS::Wire(ShapeGen1));
7fd59977 269 RuledSurf.Perform();
270 TopoDS_Shell SurfShell = RuledSurf.Shell();
271
272 // Perform section
273 BuildSection (Shape, SurfShell);
274}