]> SALOME platform Git repositories - modules/geom.git/blob - src/ShHealOper/ShHealOper_EdgeDivide.cxx
Salome HOME
Fix for Bug IPAL9053( 3.0.0: "Check Geometry" and "Load script" functionalities from...
[modules/geom.git] / src / ShHealOper / ShHealOper_EdgeDivide.cxx
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
6 //
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.
11 //
12
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.
17 //
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
21 //
22
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>
34 #include <TopoDS.hxx>
35 #include <Geom_Curve.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <Geom2d_Curve.hxx>
38 #include <GCPnts_AbscissaPoint.hxx>
39 #include <TopExp.hxx>
40 #include <Precision.hxx>
41 //#include <.hxx>
42 //#include <.hxx>
43 //=======================================================================
44 //function : ShHealOper_EdgeDivide()
45 //purpose  : Constructor
46 //=======================================================================
47
48 ShHealOper_EdgeDivide::ShHealOper_EdgeDivide (const TopoDS_Shape& theShape) 
49 {
50   Init(theShape);
51 }
52 //=======================================================================
53 //function : Init
54 //purpose  : 
55 //=======================================================================
56
57 void ShHealOper_EdgeDivide::Init(const TopoDS_Shape& theShape)
58 {
59   ShHealOper_Tool::Init(theShape);
60   myDivideParamMode = Standard_True;
61   myMapEdgesFace.Clear();
62   TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgesFace);
63   
64 }
65
66 //=======================================================================
67 //function : Perform
68 //purpose  : 
69 //=======================================================================
70 Standard_Boolean ShHealOper_EdgeDivide::Perform(const TopoDS_Shape& theEdge, 
71                                                 const TColStd_SequenceOfReal& theValues,
72                                                 const Standard_Boolean theDivideParamMode)
73
74   myDone = Standard_False;
75   myDivideParamMode = theDivideParamMode;
76   if(theEdge.ShapeType() != TopAbs_EDGE) {
77     myErrorStatus = ShHealOper_InvalidParameters;
78     return myDone;
79   }
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);
86   return myDone;
87   
88 }
89 //=======================================================================
90 //function : Perform
91 //purpose  : 
92 //=======================================================================
93
94 Standard_Boolean ShHealOper_EdgeDivide::Perform(const TopoDS_Shape& theEdge, 
95                                                 const Standard_Real theValue,
96                                                 const Standard_Boolean theDivideParamMode)
97 {
98   myDone = Standard_False;
99   myErrorStatus = ShHealOper_NotError;
100   if(theEdge.ShapeType() != TopAbs_EDGE) {
101     myErrorStatus = ShHealOper_InvalidParameters;
102     return myDone;
103   }
104   myDivideParamMode = theDivideParamMode;
105   myEdge = TopoDS::Edge(theEdge);
106   Handle(TColStd_HSequenceOfReal) aSeqValues = new TColStd_HSequenceOfReal;
107   aSeqValues->Append(theValue);
108   myDone = build(aSeqValues);
109   return myDone;
110 }
111 //=======================================================================
112 //function : build
113 //purpose  : 
114 //=======================================================================
115
116 Standard_Boolean ShHealOper_EdgeDivide::build(const Handle(TColStd_HSequenceOfReal)& theValues)
117 {
118   if(myEdge.IsNull() || !theValues->Length()) {
119     myErrorStatus = ShHealOper_InvalidParameters;
120     return Standard_False;
121   }
122
123   Standard_Boolean has3d = Standard_False, 
124   has2d = Standard_False, 
125   hasPCurves = Standard_False;
126   
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;
131   }
132   
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);
137   if(has3d) {
138     Handle(ShHealOper_SplitCurve3d) aSplitCurve3d = new ShHealOper_SplitCurve3d;
139     aSplitCurve3d->SetValues(theValues);
140     aSplitTool->SetSplitCurve3dTool(aSplitCurve3d);
141   }
142   else if(has2d) {
143     Handle(ShHealOper_SplitCurve2d) aSplitCurve2d = new ShHealOper_SplitCurve2d;
144     aSplitCurve2d->SetValues(theValues);
145     aSplitTool->SetSplitCurve2dTool(aSplitCurve2d);
146   }
147   else {
148     myErrorStatus = ShHealOper_InvalidParameters;
149     return Standard_False;
150   }
151   
152   //split 3d curve and pcurve for each face reffering to edge.
153   Standard_Boolean isDone = Standard_True;
154   if(hasPCurves) {
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;
164     }
165   }
166   else {
167      aSplitTool->Perform();
168      isDone = aSplitTool->Status( ShapeExtend_DONE );
169      if( aSplitTool->Status( ShapeExtend_FAIL ))
170         myErrorStatus = ShHealOper_ErrorExecution;
171   }
172   if(isDone)
173     myResultShape = myContext->Apply(myInitShape);
174   return isDone;
175   
176 }
177 //=======================================================================
178 //function : computeValues
179 //purpose  : 
180 //=======================================================================
181
182 Standard_Boolean ShHealOper_EdgeDivide::computeValues(const Handle(TColStd_HSequenceOfReal)& theValues,
183                                                       Standard_Boolean& theHas3d,
184                                                       Standard_Boolean& theHas2d,
185                                                       Standard_Boolean& hasPCurves)
186 {
187   hasPCurves = (myMapEdgesFace.Contains(myEdge) && 
188                 myMapEdgesFace.FindFromKey(myEdge).Extent());
189   if(hasPCurves && (!BRep_Tool::SameRange(myEdge) || !BRep_Tool::SameParameter(myEdge))) {
190     ShapeFix_Edge sfe;
191     sfe.FixSameParameter(myEdge);
192   }
193   
194   Standard_Real aFirst =0.,aLast=0.;
195
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);
200     
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);
207     }
208   }
209   else {
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;
220     }
221     else {
222       if(hasPCurves) {
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;
229         }
230           
231       }
232     }
233     if(!theHas3d && !theHas2d)
234       return Standard_False;
235
236     Standard_Integer i = 1;
237     for( ; i <= theValues->Length();i++) {
238       Standard_Real aLen = theValues->Value(i)*aCurLen;
239       if(theHas3d) {
240         GCPnts_AbscissaPoint anAbsc(aAdC,aLen,aFirst);
241         if(anAbsc.IsDone()) 
242           theValues->ChangeValue(i) = anAbsc.Parameter();
243         else
244           theValues->Remove(i--);
245       }
246       else if(theHas2d) {
247         GCPnts_AbscissaPoint anAbsc(aAdC2d,aLen,aFirst);
248         if(anAbsc.IsDone()) 
249           theValues->ChangeValue(i) = anAbsc.Parameter();
250         else
251           theValues->Remove(i--);
252       }
253     }
254   }
255   return (theValues->Length());
256 }