Back to index

salome-geom  6.5.0
GEOMAlgo_Gluer2_3.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:   GEOMAlgo_Gluer2_3.cxx
00024 // Author: Peter KURNEV
00025 
00026 #include <GEOMAlgo_Gluer2.hxx>
00027 
00028 #include <TopAbs_ShapeEnum.hxx>
00029 
00030 #include <TopoDS_Shape.hxx>
00031 #include <TopoDS_Edge.hxx>
00032 
00033 #include <BRep_Tool.hxx>
00034 
00035 #include <TopExp.hxx>
00036 #include <TopExp_Explorer.hxx>
00037 
00038 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
00039 #include <TopTools_DataMapOfShapeListOfShape.hxx>
00040 #include <TopTools_ListOfShape.hxx>
00041 #include <TopTools_MapOfShape.hxx>
00042 #include <TopTools_MapIteratorOfMapOfShape.hxx>
00043 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
00044 #include <TopTools_ListIteratorOfListOfShape.hxx>
00045 #include <TopTools_IndexedMapOfShape.hxx>
00046 
00047 #include <NMTTools_CoupleOfShape.hxx>
00048 #include <NMTTools_ListOfCoupleOfShape.hxx>
00049 #include <NMTTools_ListIteratorOfListOfCoupleOfShape.hxx>
00050 #include <NMTTools_IndexedDataMapOfShapeIndexedMapOfShape.hxx>
00051 #include <NMTTools_Tools.hxx>
00052 
00053 #include <GEOMAlgo_GlueDetector.hxx>
00054 
00055 
00056 static
00057   void MapShapes1(const TopoDS_Shape& aS,
00058                   const TopAbs_ShapeEnum aType,
00059                   TopTools_IndexedMapOfShape& aM);
00060 
00061 //=======================================================================
00062 //function : Detect
00063 //purpose  :
00064 //=======================================================================
00065 void GEOMAlgo_Gluer2::Detect()
00066 {
00067   Standard_Boolean bCheckGeometry;
00068   Standard_Integer iErr, aNbSD;
00069   TopTools_ListIteratorOfListOfShape aItLS;
00070   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
00071   //GEOMAlgo_GlueDetector aDetector;
00072   //
00073   myErrorStatus=0;
00074   myWarningStatus=0;
00075   //
00076   myImagesDetected.Clear();
00077   myOriginsDetected.Clear();
00078   //
00079   bCheckGeometry=Standard_True;
00080   //
00081   //modified by NIZNHY-PKV Tue Mar 13 13:33:35 2012f
00082   myDetector.Clear();
00083   myDetector.SetContext(myContext);
00084   //modified by NIZNHY-PKV Tue Mar 13 13:33:38 2012t
00085   myDetector.SetArgument(myArgument);
00086   myDetector.SetTolerance(myTolerance);
00087   myDetector.SetCheckGeometry(bCheckGeometry);
00088   //
00089   myDetector.Perform();
00090   iErr=myDetector.ErrorStatus();
00091   if (iErr) {
00092     // Detector is failed
00093     myErrorStatus=11;
00094     return;
00095   }
00096   //modified by NIZNHY-PKV Tue Mar 13 13:40:36 2012f
00097   iErr=myDetector.WarningStatus();
00098   if (iErr) {
00099     // Sticked shapes are detected
00100     myWarningStatus=2;
00101   }
00102   //modified by NIZNHY-PKV Tue Mar 13 13:40:39 2012t
00103   //
00104   const TopTools_DataMapOfShapeListOfShape& aImages=myDetector.Images();
00105   aItDMSLS.Initialize(aImages);
00106   for (; aItDMSLS.More(); aItDMSLS.Next()) {
00107     const TopoDS_Shape& aSkey=aItDMSLS.Key();
00108     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
00109     aNbSD=aLSD.Extent();
00110     myImagesDetected.Bind(aSkey, aLSD);
00111   }
00112   //
00113   aItDMSLS.Initialize(myImagesDetected);
00114   for (; aItDMSLS.More(); aItDMSLS.Next()) {
00115     const TopoDS_Shape& aSkey=aItDMSLS.Key();
00116     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
00117     aItLS.Initialize(aLSD);
00118     for (; aItLS.More(); aItLS.Next()) {
00119       const TopoDS_Shape& aSx=aItLS.Value();
00120       myOriginsDetected.Bind(aSx, aSkey);
00121     }
00122   }
00123 }
00124 //=======================================================================
00125 //function : PerformShapesToWork
00126 //purpose  :
00127 //=======================================================================
00128 void GEOMAlgo_Gluer2::PerformShapesToWork()
00129 {
00130   Standard_Integer aNbSG, i, j, k, aNbC, aNb, aNbSD;
00131   TopTools_ListIteratorOfListOfShape aItLS1, aItLS2;
00132   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
00133   NMTTools_CoupleOfShape aCS;
00134   NMTTools_ListOfCoupleOfShape aLCS;
00135   NMTTools_ListIteratorOfListOfCoupleOfShape aItCS;
00136   //
00137   myErrorStatus=0;
00138   myWarningStatus=0;
00139   //
00140   myImagesToWork.Clear();
00141   myOriginsToWork.Clear();
00142   //
00143   aNbSD=myImagesDetected.Extent();
00144   if (!aNbSD) {// no shapes to glue detected
00145     myWarningStatus=1;
00146     return;
00147   }
00148   //
00149   aNbSG=myShapesToGlue.Extent();
00150   if (!aNbSG) {
00151     // glue all possible
00152     myImagesToWork=myImagesDetected;
00153     //
00154     aItDMSLS.Initialize(myImagesToWork);
00155     for (; aItDMSLS.More(); aItDMSLS.Next()) {
00156       const TopoDS_Shape& aSkey=aItDMSLS.Key();
00157       const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
00158       aItLS1.Initialize(aLSD);
00159       for (; aItLS1.More(); aItLS1.Next()) {
00160         const TopoDS_Shape& aSx=aItLS1.Value();
00161         myOriginsToWork.Bind(aSx, aSkey);
00162       }
00163     }
00164     return;
00165   }// if (!aNbSG) {
00166   //
00167   // 1. Make pairs
00168   aItDMSLS.Initialize(myShapesToGlue);
00169   for (k=0; aItDMSLS.More(); aItDMSLS.Next(), ++k) {
00170     //const TopoDS_Shape& aSkey=aItDMSLS.Key();
00171     const TopTools_ListOfShape& aLSG=aItDMSLS.Value();
00172     aItLS1.Initialize(aLSG);
00173     for (i=0; aItLS1.More(); aItLS1.Next(), ++i) {
00174       aItLS2.Initialize(aLSG);
00175       for (j=0; aItLS2.More(); aItLS2.Next(), ++j) {
00176         if (j>i) {
00177           const TopoDS_Shape& aSG1=aItLS1.Value();
00178           const TopoDS_Shape& aSG2=aItLS2.Value();
00179           aCS.SetShape1(aSG1);
00180           aCS.SetShape2(aSG2);
00181           TreatPair(aCS, aLCS);
00182         }
00183       }
00184     }
00185   }
00186   //
00187   // 2. Find Chains
00188   TopTools_ListOfShape aLSX;
00189   NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMC;
00190   //
00191   NMTTools_Tools::FindChains(aLCS, aMC);
00192   //
00193   // 3. myImagesToWork, myOriginsToWork
00194   aNbC=aMC.Extent();
00195   for (i=1; i<=aNbC; ++i) {
00196     const TopoDS_Shape& aSkey=aMC.FindKey(i);
00197     const TopTools_IndexedMapOfShape& aM=aMC(i);
00198     aLSX.Clear();
00199     aNb=aM.Extent();
00200     for (j=1; j<=aNb; ++j) {
00201       const TopoDS_Shape& aS=aM(j);
00202       aLSX.Append(aS);
00203       myOriginsToWork.Bind(aS, aSkey);
00204     }
00205     myImagesToWork.Bind(aSkey, aLSX);
00206   }
00207 }
00208 //=======================================================================
00209 //function : TreatPair
00210 //purpose  :
00211 //=======================================================================
00212 void GEOMAlgo_Gluer2::TreatPair(const NMTTools_CoupleOfShape& aCS,
00213                                 NMTTools_ListOfCoupleOfShape& aLCS)
00214 {
00215   if (myErrorStatus) {
00216     return;
00217   }
00218   //
00219   Standard_Integer i, aNbS1, aNbS2, aNbS;
00220   TopAbs_ShapeEnum aType, aTypeS;
00221   TopTools_ListIteratorOfListOfShape aItLS;
00222   TopTools_IndexedMapOfShape aMS1, aMS2;
00223   TopTools_DataMapOfShapeListOfShape aDMSLS;
00224   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
00225   NMTTools_CoupleOfShape aCSS;
00226   //
00227   // 1. Checking the pair on whether it can be glued at all
00228   // 1.1
00229   const TopoDS_Shape& aS1=aCS.Shape1();
00230   if (!myOriginsDetected.IsBound(aS1)) {
00231     myErrorStatus=30;
00232     return;
00233   }
00234   const TopoDS_Shape& aSkey1=myOriginsDetected.Find(aS1);
00235   // 1.2
00236   const TopoDS_Shape& aS2=aCS.Shape2();
00237   if (!myOriginsDetected.IsBound(aS2)) {
00238     myErrorStatus=30;
00239     return;
00240   }
00241   const TopoDS_Shape& aSkey2=myOriginsDetected.Find(aS2);
00242   // 1.3
00243   if (!aSkey1.IsSame(aSkey2)) {
00244     myErrorStatus=33;
00245     return;
00246   }
00247   //
00248   // 2. Append the pair to the aLCS
00249   aLCS.Append(aCS);
00250   //
00251   // 3. Treatment the sub-shapes of the pair
00252   aType=aS1.ShapeType();
00253   if (aType==TopAbs_VERTEX) {
00254     return;
00255   }
00256   aTypeS=TopAbs_EDGE;
00257   if (aType==aTypeS) {
00258     aTypeS=TopAbs_VERTEX;
00259   }
00260   //
00261   MapShapes1(aS1, aTypeS, aMS1);
00262   MapShapes1(aS2, aTypeS, aMS2);
00263   //
00264   aNbS1=aMS1.Extent();
00265   aNbS2=aMS2.Extent();
00266   if (aNbS1!=aNbS2) {
00267     myErrorStatus=31;
00268     return;
00269   }
00270   //
00271   // 1.
00272   for (i=1; i<=aNbS1; ++i) {
00273     const TopoDS_Shape& aSS1=aMS1(i);
00274     if (aMS2.Contains(aSS1)) {
00275       continue;
00276     }
00277     //
00278     if (!myOriginsDetected.IsBound(aSS1)) {
00279       myErrorStatus=30;
00280       return;
00281     }
00282     //
00283     const TopoDS_Shape& aSkey=myOriginsDetected.Find(aSS1);
00284     if (aDMSLS.IsBound(aSkey)) {
00285       TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aSkey);
00286       aLS.Append(aSS1);
00287     }
00288     else {
00289       TopTools_ListOfShape aLS;
00290       //
00291       aLS.Append(aSS1);
00292       aDMSLS.Bind(aSkey, aLS);
00293     }
00294   }
00295   //
00296   // 2.
00297   for (i=1; i<=aNbS2; ++i) {
00298     const TopoDS_Shape& aSS2=aMS2(i);
00299     if (aMS1.Contains(aSS2)) {
00300       continue;
00301     }
00302     //
00303     if (!myOriginsDetected.IsBound(aSS2)) {
00304       myErrorStatus=30;
00305       return;
00306     }
00307     //
00308     const TopoDS_Shape& aSkey=myOriginsDetected.Find(aSS2);
00309     if (aDMSLS.IsBound(aSkey)) {
00310       TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aSkey);
00311       aLS.Append(aSS2);
00312     }
00313     else {
00314       TopTools_ListOfShape aLS;
00315       //
00316       aLS.Append(aSS2);
00317       aDMSLS.Bind(aSkey, aLS);
00318     }
00319   }
00320   //
00321   // 3.
00322   aItDMSLS.Initialize(aDMSLS);
00323   for (; aItDMSLS.More(); aItDMSLS.Next()) {
00324     //const TopoDS_Shape& aSkey=aItDMSLS.Key();
00325     const TopTools_ListOfShape& aLS=aItDMSLS.Value();
00326     aNbS=aLS.Extent();
00327     if (aNbS!=2) {
00328       myErrorStatus=32;
00329       return;
00330     }
00331     //
00332     const TopoDS_Shape& aSS1=aLS.First();
00333     const TopoDS_Shape& aSS2=aLS.Last();
00334     aCSS.SetShape1(aSS1);
00335     aCSS.SetShape2(aSS2);
00336     TreatPair(aCSS, aLCS);
00337   }
00338 }
00339 //=======================================================================
00340 //function : MapShapes1
00341 //purpose  :
00342 //=======================================================================
00343 void MapShapes1(const TopoDS_Shape& aS,
00344                const TopAbs_ShapeEnum aType,
00345                TopTools_IndexedMapOfShape& aM)
00346 {
00347   TopExp_Explorer aExp;
00348 
00349   aExp.Init (aS, aType);
00350   for ( ;aExp.More(); aExp.Next()) {
00351     const TopoDS_Shape aSx=aExp.Current();
00352     if (aType==TopAbs_EDGE) {
00353       const TopoDS_Edge& aEx=*((TopoDS_Edge*)&aSx);
00354       if (BRep_Tool::Degenerated(aEx)) {
00355         continue;
00356       }
00357     }
00358     aM.Add(aSx);
00359   }
00360 }