Salome HOME
Fix bug in structural elements (PAL #2012)
[modules/geom.git] / src / NMTDS / NMTDS_Tools.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        NMTDS_Tools.cxx
21 // Created:     Tue Feb 20 14:57:28 2007
22 // Author:      Peter KURNEV
23 //              <pkv@irinox>
24 //
25 #include <NMTDS_Tools.ixx>
26 #include <TopoDS_Vertex.hxx>
27 #include <gp_Pnt.hxx>
28 #include <BRep_Tool.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
31 #include <TopAbs_ShapeEnum.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <BRep_Builder.hxx>
34
35 void CopySource(const TopoDS_Shape& aS, 
36                 TopTools_IndexedDataMapOfShapeShape& aMapSS, 
37                 TopoDS_Shape& aSC);
38
39 //=======================================================================
40 //function : CopyShape
41 //purpose  : 
42 //=======================================================================
43   void NMTDS_Tools::CopyShape(const TopoDS_Shape& aS, 
44                               TopoDS_Shape& aSC)
45 {
46   TopTools_IndexedDataMapOfShapeShape aMapSS;
47   //
48   CopySource(aS, aMapSS, aSC);
49 }
50 //=======================================================================
51 //function : CopyShape
52 //purpose  : 
53 //=======================================================================
54   void NMTDS_Tools::CopyShape(const TopoDS_Shape& aS, 
55                               TopoDS_Shape& aSC,
56                               TopTools_IndexedDataMapOfShapeShape& aMapSS)
57 {
58   CopySource(aS, aMapSS, aSC);
59 }
60 //=======================================================================
61 //function : CopySource
62 //purpose  : 
63 //=======================================================================
64 void CopySource(const TopoDS_Shape& aS, 
65                 TopTools_IndexedDataMapOfShapeShape& aMapSS, 
66                 TopoDS_Shape& aSC)
67 {
68   Standard_Boolean bFree;
69   TopAbs_ShapeEnum aT;
70   TopoDS_Iterator aIt;
71   BRep_Builder BB;
72   //
73   aT=aS.ShapeType();
74   //
75   if (aMapSS.Contains(aS)) {
76     aSC=aMapSS.ChangeFromKey(aS);
77     aSC.Orientation(aS.Orientation());
78     return;
79   }
80   else {
81     aSC=aS.EmptyCopied();
82     aMapSS.Add(aS, aSC);
83   }
84   //
85   bFree=aSC.Free();
86   aSC.Free(Standard_True);
87   aIt.Initialize(aS);
88   for (; aIt.More();  aIt.Next()) {
89     TopoDS_Shape aSCx;
90     //
91     const TopoDS_Shape& aSx=aIt.Value();
92     //
93     CopySource (aSx, aMapSS, aSCx);  
94     //
95     aSCx.Orientation(aSx.Orientation());
96     BB.Add(aSC, aSCx);
97   }
98   aSC.Free(bFree);
99 }
100 //=======================================================================
101 // function: ComputeVV
102 // purpose: 
103 //=======================================================================
104   Standard_Integer NMTDS_Tools::ComputeVV(const TopoDS_Vertex& aV1, 
105                                           const TopoDS_Vertex& aV2)
106 {
107   Standard_Real aTolV1, aTolV2, aTolSum, aTolSum2, aD2;
108   gp_Pnt aP1, aP2;
109   //
110   aTolV1=BRep_Tool::Tolerance(aV1);
111   aTolV2=BRep_Tool::Tolerance(aV2);
112   aTolSum=aTolV1+aTolV2;
113   aTolSum2=aTolSum*aTolSum;
114   //
115   aP1=BRep_Tool::Pnt(aV1);
116   aP2=BRep_Tool::Pnt(aV2);
117   //
118   aD2=aP1.SquareDistance(aP2);
119   if (aD2>aTolSum2) {
120     return -1;
121   }
122   return 0;
123 }
124 //=======================================================================
125 // function: HasBRep
126 // purpose: 
127 //=======================================================================
128   Standard_Boolean NMTDS_Tools::HasBRep(const TopAbs_ShapeEnum aTi)
129 {
130   return (aTi==TopAbs_VERTEX || aTi==TopAbs_EDGE || aTi==TopAbs_FACE);
131 }
132 //=======================================================================
133 //function : TypeToInteger
134 //purpose  : 
135 //=======================================================================
136   Standard_Integer NMTDS_Tools::TypeToInteger(const TopAbs_ShapeEnum aType1,
137                                             const TopAbs_ShapeEnum aType2)
138 {
139   Standard_Integer iRet, iT1, iT2, iX;
140   //
141   iRet=-1;
142   iT1=(Standard_Integer)aType1;
143   iT2=(Standard_Integer)aType2;
144   //
145   iX=iT2*10+iT1;
146   switch (iX) {
147     case 77:
148       iRet=5; // VV
149       break;
150     case 76:
151     case 67:
152       iRet=4; // VE
153       break;
154     case 74:
155     case 47:
156       iRet=2; // VF
157       break;
158     case 66:
159       iRet=3; // EE
160       break;
161     case 64:
162     case 46:
163       iRet=1; // EF
164       break;
165     case 44:
166       iRet=0; // FF
167       break;
168     default:
169       break;
170   }
171   return iRet; 
172 }