Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / ShHealOper / ShHealOper_EdgeDivide.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      ShHealOper_EdgeDivide.cxx
24 // Created:   30.04.04 16:44:47
25 // Author:    Galina KULIKOVA
26 //
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>
38 #include <TopoDS.hxx>
39 #include <Geom_Curve.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <Geom2d_Curve.hxx>
42 #include <GCPnts_AbscissaPoint.hxx>
43 #include <TopExp.hxx>
44 #include <Precision.hxx>
45 //#include <.hxx>
46 //#include <.hxx>
47 //=======================================================================
48 //function : ShHealOper_EdgeDivide()
49 //purpose  : Constructor
50 //=======================================================================
51
52 ShHealOper_EdgeDivide::ShHealOper_EdgeDivide (const TopoDS_Shape& theShape) 
53 {
54   Init(theShape);
55 }
56 //=======================================================================
57 //function : Init
58 //purpose  : 
59 //=======================================================================
60
61 void ShHealOper_EdgeDivide::Init(const TopoDS_Shape& theShape)
62 {
63   ShHealOper_Tool::Init(theShape);
64   myDivideParamMode = Standard_True;
65   myMapEdgesFace.Clear();
66   TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgesFace);
67   
68 }
69
70 //=======================================================================
71 //function : Perform
72 //purpose  : 
73 //=======================================================================
74 Standard_Boolean ShHealOper_EdgeDivide::Perform(const TopoDS_Shape& theEdge, 
75                                                 const TColStd_SequenceOfReal& theValues,
76                                                 const Standard_Boolean theDivideParamMode)
77
78   myDone = Standard_False;
79   myDivideParamMode = theDivideParamMode;
80   if(theEdge.ShapeType() != TopAbs_EDGE) {
81     myErrorStatus = ShHealOper_InvalidParameters;
82     return myDone;
83   }
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);
90   return myDone;
91   
92 }
93 //=======================================================================
94 //function : Perform
95 //purpose  : 
96 //=======================================================================
97
98 Standard_Boolean ShHealOper_EdgeDivide::Perform(const TopoDS_Shape& theEdge, 
99                                                 const Standard_Real theValue,
100                                                 const Standard_Boolean theDivideParamMode)
101 {
102   myDone = Standard_False;
103   myErrorStatus = ShHealOper_NotError;
104   if(theEdge.ShapeType() != TopAbs_EDGE) {
105     myErrorStatus = ShHealOper_InvalidParameters;
106     return myDone;
107   }
108   myDivideParamMode = theDivideParamMode;
109   myEdge = TopoDS::Edge(theEdge);
110   Handle(TColStd_HSequenceOfReal) aSeqValues = new TColStd_HSequenceOfReal;
111   aSeqValues->Append(theValue);
112   myDone = build(aSeqValues);
113   return myDone;
114 }
115 //=======================================================================
116 //function : build
117 //purpose  : 
118 //=======================================================================
119
120 Standard_Boolean ShHealOper_EdgeDivide::build(const Handle(TColStd_HSequenceOfReal)& theValues)
121 {
122   if(myEdge.IsNull() || !theValues->Length()) {
123     myErrorStatus = ShHealOper_InvalidParameters;
124     return Standard_False;
125   }
126
127   Standard_Boolean has3d = Standard_False, 
128   has2d = Standard_False, 
129   hasPCurves = Standard_False;
130   
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;
135   }
136   
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);
141   if(has3d) {
142     Handle(ShHealOper_SplitCurve3d) aSplitCurve3d = new ShHealOper_SplitCurve3d;
143     aSplitCurve3d->SetValues(theValues);
144     aSplitTool->SetSplitCurve3dTool(aSplitCurve3d);
145   }
146   else if(has2d) {
147     Handle(ShHealOper_SplitCurve2d) aSplitCurve2d = new ShHealOper_SplitCurve2d;
148     aSplitCurve2d->SetValues(theValues);
149     aSplitTool->SetSplitCurve2dTool(aSplitCurve2d);
150   }
151   else {
152     myErrorStatus = ShHealOper_InvalidParameters;
153     return Standard_False;
154   }
155   
156   //split 3d curve and pcurve for each face reffering to edge.
157   Standard_Boolean isDone = Standard_True;
158   if(hasPCurves) {
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;
168     }
169   }
170   else {
171      aSplitTool->Perform();
172      isDone = aSplitTool->Status( ShapeExtend_DONE );
173      if( aSplitTool->Status( ShapeExtend_FAIL ))
174         myErrorStatus = ShHealOper_ErrorExecution;
175   }
176   if(isDone)
177     myResultShape = myContext->Apply(myInitShape);
178   return isDone;
179   
180 }
181 //=======================================================================
182 //function : computeValues
183 //purpose  : 
184 //=======================================================================
185
186 Standard_Boolean ShHealOper_EdgeDivide::computeValues(const Handle(TColStd_HSequenceOfReal)& theValues,
187                                                       Standard_Boolean& theHas3d,
188                                                       Standard_Boolean& theHas2d,
189                                                       Standard_Boolean& hasPCurves)
190 {
191   hasPCurves = (myMapEdgesFace.Contains(myEdge) && 
192                 myMapEdgesFace.FindFromKey(myEdge).Extent());
193   if(hasPCurves && (!BRep_Tool::SameRange(myEdge) || !BRep_Tool::SameParameter(myEdge))) {
194     ShapeFix_Edge sfe;
195     sfe.FixSameParameter(myEdge);
196   }
197   
198   Standard_Real aFirst =0.,aLast=0.;
199
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);
204     
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);
211     }
212   }
213   else {
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;
224     }
225     else {
226       if(hasPCurves) {
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;
233         }
234           
235       }
236     }
237     if(!theHas3d && !theHas2d)
238       return Standard_False;
239
240     Standard_Integer i = 1;
241     for( ; i <= theValues->Length();i++) {
242       Standard_Real aLen = theValues->Value(i)*aCurLen;
243       if(theHas3d) {
244         GCPnts_AbscissaPoint anAbsc(aAdC,aLen,aFirst);
245         if(anAbsc.IsDone()) 
246           theValues->ChangeValue(i) = anAbsc.Parameter();
247         else
248           theValues->Remove(i--);
249       }
250       else if(theHas2d) {
251         GCPnts_AbscissaPoint anAbsc(aAdC2d,aLen,aFirst);
252         if(anAbsc.IsDone()) 
253           theValues->ChangeValue(i) = anAbsc.Parameter();
254         else
255           theValues->Remove(i--);
256       }
257     }
258   }
259   return (theValues->Length());
260 }