1 // Copyright (C) 2007-2010 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 // File: ShHealOper_EdgeDivide.cxx
24 // Created: 30.04.04 16:44:47
25 // Author: Galina KULIKOVA
27 #include <ShHealOper_EdgeDivide.hxx>
28 #include <ShapeUpgrade_WireDivide.hxx>
29 #include <ShHealOper_SplitCurve3d.hxx>
30 #include <ShHealOper_SplitCurve2d.hxx>
31 #include <TopTools_ListOfShape.hxx>
32 #include <TopTools_ListIteratorOfListOfShape.hxx>
33 #include <BRep_Tool.hxx>
34 #include <ShapeFix_Edge.hxx>
35 #include <ShapeAnalysis_Edge.hxx>
36 #include <GeomAdaptor_Curve.hxx>
37 #include <Geom2dAdaptor_Curve.hxx>
39 #include <Geom_Curve.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <Geom2d_Curve.hxx>
42 #include <GCPnts_AbscissaPoint.hxx>
44 #include <Precision.hxx>
47 //=======================================================================
48 //function : ShHealOper_EdgeDivide()
49 //purpose : Constructor
50 //=======================================================================
52 ShHealOper_EdgeDivide::ShHealOper_EdgeDivide (const TopoDS_Shape& theShape)
56 //=======================================================================
59 //=======================================================================
61 void ShHealOper_EdgeDivide::Init(const TopoDS_Shape& theShape)
63 ShHealOper_Tool::Init(theShape);
64 myDivideParamMode = Standard_True;
65 myMapEdgesFace.Clear();
66 TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgesFace);
70 //=======================================================================
73 //=======================================================================
74 Standard_Boolean ShHealOper_EdgeDivide::Perform(const TopoDS_Shape& theEdge,
75 const TColStd_SequenceOfReal& theValues,
76 const Standard_Boolean theDivideParamMode)
78 myDone = Standard_False;
79 myDivideParamMode = theDivideParamMode;
80 if(theEdge.ShapeType() != TopAbs_EDGE) {
81 myErrorStatus = ShHealOper_InvalidParameters;
84 myEdge = TopoDS::Edge(theEdge);
85 Standard_Integer i =1;
86 Handle(TColStd_HSequenceOfReal) aSeqValues = new TColStd_HSequenceOfReal;
87 for( ; i <= theValues.Length(); i++)
88 aSeqValues->Append(theValues.Value(i));
89 myDone = build(aSeqValues);
93 //=======================================================================
96 //=======================================================================
98 Standard_Boolean ShHealOper_EdgeDivide::Perform(const TopoDS_Shape& theEdge,
99 const Standard_Real theValue,
100 const Standard_Boolean theDivideParamMode)
102 myDone = Standard_False;
103 myErrorStatus = ShHealOper_NotError;
104 if(theEdge.ShapeType() != TopAbs_EDGE) {
105 myErrorStatus = ShHealOper_InvalidParameters;
108 myDivideParamMode = theDivideParamMode;
109 myEdge = TopoDS::Edge(theEdge);
110 Handle(TColStd_HSequenceOfReal) aSeqValues = new TColStd_HSequenceOfReal;
111 aSeqValues->Append(theValue);
112 myDone = build(aSeqValues);
115 //=======================================================================
118 //=======================================================================
120 Standard_Boolean ShHealOper_EdgeDivide::build(const Handle(TColStd_HSequenceOfReal)& theValues)
122 if(myEdge.IsNull() || !theValues->Length()) {
123 myErrorStatus = ShHealOper_InvalidParameters;
124 return Standard_False;
127 Standard_Boolean has3d = Standard_False,
128 has2d = Standard_False,
129 hasPCurves = Standard_False;
131 //computation of the split values in dependance from specified mode and values.
132 if(!computeValues(theValues, has3d,has2d,hasPCurves)) {
133 myErrorStatus = ShHealOper_InvalidParameters;
134 return Standard_False;
137 //setting split values in the splitting curve tools.
138 Handle(ShapeUpgrade_WireDivide) aSplitTool = new ShapeUpgrade_WireDivide;
139 aSplitTool->Load(myEdge);
140 aSplitTool->SetContext(myContext);
142 Handle(ShHealOper_SplitCurve3d) aSplitCurve3d = new ShHealOper_SplitCurve3d;
143 aSplitCurve3d->SetValues(theValues);
144 aSplitTool->SetSplitCurve3dTool(aSplitCurve3d);
147 Handle(ShHealOper_SplitCurve2d) aSplitCurve2d = new ShHealOper_SplitCurve2d;
148 aSplitCurve2d->SetValues(theValues);
149 aSplitTool->SetSplitCurve2dTool(aSplitCurve2d);
152 myErrorStatus = ShHealOper_InvalidParameters;
153 return Standard_False;
156 //split 3d curve and pcurve for each face reffering to edge.
157 Standard_Boolean isDone = Standard_True;
159 const TopTools_ListOfShape& lfaces = myMapEdgesFace.FindFromKey(myEdge);
160 TopTools_ListIteratorOfListOfShape aItf(lfaces);
161 for( ; aItf.More() && isDone; aItf.Next()) {
162 TopoDS_Face aFace = TopoDS::Face(aItf.Value());
163 aSplitTool->SetFace(aFace);
164 aSplitTool->Perform();
165 isDone = aSplitTool->Status( ShapeExtend_DONE );
166 if( aSplitTool->Status( ShapeExtend_FAIL ))
167 myErrorStatus = ShHealOper_ErrorExecution;
171 aSplitTool->Perform();
172 isDone = aSplitTool->Status( ShapeExtend_DONE );
173 if( aSplitTool->Status( ShapeExtend_FAIL ))
174 myErrorStatus = ShHealOper_ErrorExecution;
177 myResultShape = myContext->Apply(myInitShape);
181 //=======================================================================
182 //function : computeValues
184 //=======================================================================
186 Standard_Boolean ShHealOper_EdgeDivide::computeValues(const Handle(TColStd_HSequenceOfReal)& theValues,
187 Standard_Boolean& theHas3d,
188 Standard_Boolean& theHas2d,
189 Standard_Boolean& hasPCurves)
191 hasPCurves = (myMapEdgesFace.Contains(myEdge) &&
192 myMapEdgesFace.FindFromKey(myEdge).Extent());
193 if(hasPCurves && (!BRep_Tool::SameRange(myEdge) || !BRep_Tool::SameParameter(myEdge))) {
195 sfe.FixSameParameter(myEdge);
198 Standard_Real aFirst =0.,aLast=0.;
200 //computation of the split values if edge should be splitted by parameter.
201 if(myDivideParamMode) {
202 BRep_Tool::Range(myEdge,aFirst,aLast);
203 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(myEdge,aFirst,aLast);
205 theHas3d = (!aCurve.IsNull());
206 theHas2d = (aCurve.IsNull() && (fabs(aLast-aFirst) > Precision::PConfusion() ));
207 Standard_Integer i = 1;
208 for( ; i <= theValues->Length();i++) {
209 Standard_Real aVal = theValues->Value(i);
210 theValues->ChangeValue(i) = aFirst+ aVal*fabs(aLast - aFirst);
214 //computation of the split values if edge should be splitted by length.
215 ShapeAnalysis_Edge sae;
216 Handle(Geom_Curve) aCurve;
217 Standard_Real aCurLen =0.;
218 GeomAdaptor_Curve aAdC;
219 Geom2dAdaptor_Curve aAdC2d;
220 if(sae.Curve3d(myEdge,aCurve,aFirst,aLast,Standard_False)) {
221 aAdC.Load(aCurve,aFirst,aLast);
222 aCurLen = GCPnts_AbscissaPoint::Length(aAdC,aFirst,aLast);
223 theHas3d = Standard_True;
227 TopoDS_Face aFace = TopoDS::Face(myMapEdgesFace.FindFromKey(myEdge).First());
228 Handle(Geom2d_Curve) aCurve2d;
229 if(sae.PCurve(myEdge,aFace,aCurve2d,aFirst,aLast)) {
230 aAdC2d.Load(aCurve2d,aFirst,aLast);
231 aCurLen = GCPnts_AbscissaPoint::Length(aAdC,aFirst,aLast);
232 theHas2d = Standard_True;
237 if(!theHas3d && !theHas2d)
238 return Standard_False;
240 Standard_Integer i = 1;
241 for( ; i <= theValues->Length();i++) {
242 Standard_Real aLen = theValues->Value(i)*aCurLen;
244 GCPnts_AbscissaPoint anAbsc(aAdC,aLen,aFirst);
246 theValues->ChangeValue(i) = anAbsc.Parameter();
248 theValues->Remove(i--);
251 GCPnts_AbscissaPoint anAbsc(aAdC2d,aLen,aFirst);
253 theValues->ChangeValue(i) = anAbsc.Parameter();
255 theValues->Remove(i--);
259 return (theValues->Length());