1 // File: ShHealOper_EdgeDivide.cxx
2 // Created: 30.04.04 16:44:47
3 // Author: Galina KULIKOVA
4 // < MODULE = KERNEL> <PACKAGE = ShHealOper> : <Shape Healing Operations>
5 // Copyright (C) 2003 CEA
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2.1 of the License.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <ShHealOper_EdgeDivide.hxx>
24 #include <ShapeUpgrade_WireDivide.hxx>
25 #include <ShHealOper_SplitCurve3d.hxx>
26 #include <ShHealOper_SplitCurve2d.hxx>
27 #include <TopTools_ListOfShape.hxx>
28 #include <TopTools_ListIteratorOfListOfShape.hxx>
29 #include <BRep_Tool.hxx>
30 #include <ShapeFix_Edge.hxx>
31 #include <ShapeAnalysis_Edge.hxx>
32 #include <GeomAdaptor_Curve.hxx>
33 #include <Geom2dAdaptor_Curve.hxx>
35 #include <Geom_Curve.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <Geom2d_Curve.hxx>
38 #include <GCPnts_AbscissaPoint.hxx>
40 #include <Precision.hxx>
43 //=======================================================================
44 //function : ShHealOper_EdgeDivide()
45 //purpose : Constructor
46 //=======================================================================
48 ShHealOper_EdgeDivide::ShHealOper_EdgeDivide (const TopoDS_Shape& theShape)
52 //=======================================================================
55 //=======================================================================
57 void ShHealOper_EdgeDivide::Init(const TopoDS_Shape& theShape)
59 ShHealOper_Tool::Init(theShape);
60 myDivideParamMode = Standard_True;
61 myMapEdgesFace.Clear();
62 TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgesFace);
66 //=======================================================================
69 //=======================================================================
70 Standard_Boolean ShHealOper_EdgeDivide::Perform(const TopoDS_Shape& theEdge,
71 const TColStd_SequenceOfReal& theValues,
72 const Standard_Boolean theDivideParamMode)
74 myDone = Standard_False;
75 myDivideParamMode = theDivideParamMode;
76 if(theEdge.ShapeType() != TopAbs_EDGE) {
77 myErrorStatus = ShHealOper_InvalidParameters;
80 myEdge = TopoDS::Edge(theEdge);
81 Standard_Integer i =1;
82 Handle(TColStd_HSequenceOfReal) aSeqValues = new TColStd_HSequenceOfReal;
83 for( ; i <= theValues.Length(); i++)
84 aSeqValues->Append(theValues.Value(i));
85 myDone = build(aSeqValues);
89 //=======================================================================
92 //=======================================================================
94 Standard_Boolean ShHealOper_EdgeDivide::Perform(const TopoDS_Shape& theEdge,
95 const Standard_Real theValue,
96 const Standard_Boolean theDivideParamMode)
98 myDone = Standard_False;
99 myErrorStatus = ShHealOper_NotError;
100 if(theEdge.ShapeType() != TopAbs_EDGE) {
101 myErrorStatus = ShHealOper_InvalidParameters;
104 myDivideParamMode = theDivideParamMode;
105 myEdge = TopoDS::Edge(theEdge);
106 Handle(TColStd_HSequenceOfReal) aSeqValues = new TColStd_HSequenceOfReal;
107 aSeqValues->Append(theValue);
108 myDone = build(aSeqValues);
111 //=======================================================================
114 //=======================================================================
116 Standard_Boolean ShHealOper_EdgeDivide::build(const Handle(TColStd_HSequenceOfReal)& theValues)
118 if(myEdge.IsNull() || !theValues->Length()) {
119 myErrorStatus = ShHealOper_InvalidParameters;
120 return Standard_False;
123 Standard_Boolean has3d = Standard_False,
124 has2d = Standard_False,
125 hasPCurves = Standard_False;
127 //computation of the split values in dependance from specified mode and values.
128 if(!computeValues(theValues, has3d,has2d,hasPCurves)) {
129 myErrorStatus = ShHealOper_InvalidParameters;
130 return Standard_False;
133 //setting split values in the splitting curve tools.
134 Handle(ShapeUpgrade_WireDivide) aSplitTool = new ShapeUpgrade_WireDivide;
135 aSplitTool->Load(myEdge);
136 aSplitTool->SetContext(myContext);
138 Handle(ShHealOper_SplitCurve3d) aSplitCurve3d = new ShHealOper_SplitCurve3d;
139 aSplitCurve3d->SetValues(theValues);
140 aSplitTool->SetSplitCurve3dTool(aSplitCurve3d);
143 Handle(ShHealOper_SplitCurve2d) aSplitCurve2d = new ShHealOper_SplitCurve2d;
144 aSplitCurve2d->SetValues(theValues);
145 aSplitTool->SetSplitCurve2dTool(aSplitCurve2d);
148 myErrorStatus = ShHealOper_InvalidParameters;
149 return Standard_False;
152 //split 3d curve and pcurve for each face reffering to edge.
153 Standard_Boolean isDone = Standard_True;
155 const TopTools_ListOfShape& lfaces = myMapEdgesFace.FindFromKey(myEdge);
156 TopTools_ListIteratorOfListOfShape aItf(lfaces);
157 for( ; aItf.More() && isDone; aItf.Next()) {
158 TopoDS_Face aFace = TopoDS::Face(aItf.Value());
159 aSplitTool->SetFace(aFace);
160 aSplitTool->Perform();
161 isDone = aSplitTool->Status( ShapeExtend_DONE );
162 if( aSplitTool->Status( ShapeExtend_FAIL ))
163 myErrorStatus = ShHealOper_ErrorExecution;
167 aSplitTool->Perform();
168 isDone = aSplitTool->Status( ShapeExtend_DONE );
169 if( aSplitTool->Status( ShapeExtend_FAIL ))
170 myErrorStatus = ShHealOper_ErrorExecution;
173 myResultShape = myContext->Apply(myInitShape);
177 //=======================================================================
178 //function : computeValues
180 //=======================================================================
182 Standard_Boolean ShHealOper_EdgeDivide::computeValues(const Handle(TColStd_HSequenceOfReal)& theValues,
183 Standard_Boolean& theHas3d,
184 Standard_Boolean& theHas2d,
185 Standard_Boolean& hasPCurves)
187 hasPCurves = (myMapEdgesFace.Contains(myEdge) &&
188 myMapEdgesFace.FindFromKey(myEdge).Extent());
189 if(hasPCurves && (!BRep_Tool::SameRange(myEdge) || !BRep_Tool::SameParameter(myEdge))) {
191 sfe.FixSameParameter(myEdge);
194 Standard_Real aFirst =0.,aLast=0.;
196 //computation of the split values if edge should be splitted by parameter.
197 if(myDivideParamMode) {
198 BRep_Tool::Range(myEdge,aFirst,aLast);
199 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(myEdge,aFirst,aLast);
201 theHas3d = (!aCurve.IsNull());
202 theHas2d = (aCurve.IsNull() && (fabs(aLast-aFirst) > Precision::PConfusion() ));
203 Standard_Integer i = 1;
204 for( ; i <= theValues->Length();i++) {
205 Standard_Real aVal = theValues->Value(i);
206 theValues->ChangeValue(i) = aFirst+ aVal*fabs(aLast - aFirst);
210 //computation of the split values if edge should be splitted by length.
211 ShapeAnalysis_Edge sae;
212 Handle(Geom_Curve) aCurve;
213 Standard_Real aCurLen =0.;
214 GeomAdaptor_Curve aAdC;
215 Geom2dAdaptor_Curve aAdC2d;
216 if(sae.Curve3d(myEdge,aCurve,aFirst,aLast,Standard_False)) {
217 aAdC.Load(aCurve,aFirst,aLast);
218 aCurLen = GCPnts_AbscissaPoint::Length(aAdC,aFirst,aLast);
219 theHas3d = Standard_True;
223 TopoDS_Face aFace = TopoDS::Face(myMapEdgesFace.FindFromKey(myEdge).First());
224 Handle(Geom2d_Curve) aCurve2d;
225 if(sae.PCurve(myEdge,aFace,aCurve2d,aFirst,aLast)) {
226 aAdC2d.Load(aCurve2d,aFirst,aLast);
227 aCurLen = GCPnts_AbscissaPoint::Length(aAdC,aFirst,aLast);
228 theHas2d = Standard_True;
233 if(!theHas3d && !theHas2d)
234 return Standard_False;
236 Standard_Integer i = 1;
237 for( ; i <= theValues->Length();i++) {
238 Standard_Real aLen = theValues->Value(i)*aCurLen;
240 GCPnts_AbscissaPoint anAbsc(aAdC,aLen,aFirst);
242 theValues->ChangeValue(i) = anAbsc.Parameter();
244 theValues->Remove(i--);
247 GCPnts_AbscissaPoint anAbsc(aAdC2d,aLen,aFirst);
249 theValues->ChangeValue(i) = anAbsc.Parameter();
251 theValues->Remove(i--);
255 return (theValues->Length());