0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / BOPAlgo / BOPAlgo_PaveFiller_10.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2014 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 //                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 //
7 // This file is part of Open CASCADE Technology software library.
8 //
9 // This library is free software; you can redistribute it and/or modify it under
10 // the terms of the GNU Lesser General Public License version 2.1 as published
11 // by the Free Software Foundation, with special exception defined in the file
12 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
13 // distribution for complete text of the license and disclaimer of any warranty.
14 //
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
17
18 #include <BOPAlgo_PaveFiller.hxx>
19
20 #include <Precision.hxx>
21
22 #include <gp_Pnt.hxx>
23 #include <Bnd_Box.hxx>
24
25 #include <TopoDS_Iterator.hxx>
26 #include <TopoDS_Vertex.hxx>
27 #include <TopoDS_Edge.hxx>
28
29 #include <BRep_Tool.hxx>
30 #include <BRep_Builder.hxx>
31 #include <BRepBndLib.hxx>
32
33 #include <BOPDS_ShapeInfo.hxx>
34 #include <BOPDS_VectorOfListOfPaveBlock.hxx>
35 #include <BOPDS_MapOfCommonBlock.hxx>
36 #include <BOPDS_ListOfPaveBlock.hxx>
37 #include <BOPDS_CommonBlock.hxx>
38 #include <BOPDS_DS.hxx>
39
40 //=======================================================================
41 //function : SetNonDestructive
42 //purpose  : 
43 //=======================================================================
44 void BOPAlgo_PaveFiller::SetNonDestructive() 
45 {
46   if (!myIsPrimary || myNonDestructive) {
47     return;
48   }
49   //
50   Standard_Boolean bFlag;
51   TopTools_ListIteratorOfListOfShape aItLS;
52   //
53   bFlag=Standard_False;
54   aItLS.Initialize(myArguments);
55   for(; aItLS.More() && (!bFlag); aItLS.Next()) {
56     const TopoDS_Shape& aS=aItLS.Value();
57     bFlag=aS.Locked();
58   }
59   myNonDestructive=bFlag;
60 }
61 //=======================================================================
62 //function : UpdateEdgeTolerance
63 //purpose  : 
64 //=======================================================================
65 void BOPAlgo_PaveFiller::UpdateEdgeTolerance (const Standard_Integer nE,
66                                               const Standard_Real theTol)
67 {
68   BOPDS_ShapeInfo& aSIE = myDS->ChangeShapeInfo(nE);
69   const TColStd_ListOfInteger& aLI = aSIE.SubShapes();
70
71   // For the safe input mode avoid modifying the input shapes
72   if (myNonDestructive)
73   {
74     if (!myDS->IsNewShape(nE))
75       return;
76
77     TColStd_ListIteratorOfListOfInteger itLI(aLI);
78     for (; itLI.More(); itLI.Next())
79     {
80       Standard_Integer nV = itLI.Value(), nVSD;
81       if (!myDS->IsNewShape(nV) &&
82           !myDS->HasShapeSD(nV, nVSD))
83         return;
84     }
85   }
86
87   // Update edge
88   const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(nE);
89   BRep_Builder().UpdateEdge(aE, theTol);
90   Bnd_Box& aBoxE = aSIE.ChangeBox();
91   BRepBndLib::Add(aE, aBoxE);
92   aBoxE.SetGap(aBoxE.GetGap() + Precision::Confusion());
93
94   // Update vertices
95   TColStd_ListIteratorOfListOfInteger itLI(aLI);
96   for (; itLI.More(); itLI.Next())
97   {
98     Standard_Integer nV = itLI.Value();
99     UpdateVertex(nV, theTol);
100   }
101 }
102 //=======================================================================
103 //function : UpdateVertex
104 //purpose  : 
105 //=======================================================================
106 Standard_Integer BOPAlgo_PaveFiller::UpdateVertex
107   (const Standard_Integer nV,
108    const Standard_Real aTolNew)
109 {
110   Standard_Integer nVNew;
111   Standard_Real aTolV;
112   BRep_Builder aBB;
113   
114   nVNew = nV;
115   if (myDS->IsNewShape(nVNew) || 
116       myDS->HasShapeSD(nV, nVNew) ||
117       !myNonDestructive) {
118     // nV is a new vertex, it has SD or non-destructive mode is not in force
119     const TopoDS_Vertex& aVSD = *(TopoDS_Vertex*)&myDS->Shape(nVNew);
120     aTolV = BRep_Tool::Tolerance(aVSD);
121     if (aTolV < aTolNew) {
122       aBB.UpdateVertex(aVSD, aTolNew);
123       BOPDS_ShapeInfo& aSIV = myDS->ChangeShapeInfo(nVNew);
124       Bnd_Box& aBoxV = aSIV.ChangeBox();
125       BRepBndLib::Add(aVSD, aBoxV);
126       aBoxV.SetGap(aBoxV.GetGap() + Precision::Confusion());
127       myIncreasedSS.Add(nV);
128     }
129     return nVNew;
130   }
131   //
132   // nV is old vertex
133   const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&myDS->Shape(nV);
134   aTolV = BRep_Tool::Tolerance(aV);
135   //
136   // create new vertex
137   TopoDS_Vertex aVNew;
138   gp_Pnt aPV = BRep_Tool::Pnt(aV);
139   aBB.MakeVertex(aVNew, aPV, Max(aTolV, aTolNew));
140   //
141   // append new vertex to DS
142   BOPDS_ShapeInfo aSIV;
143   aSIV.SetShapeType(TopAbs_VERTEX);
144   aSIV.SetShape(aVNew);
145   nVNew = myDS->Append(aSIV);
146   //
147   // bounding box for the new vertex
148   BOPDS_ShapeInfo& aSIDS = myDS->ChangeShapeInfo(nVNew);
149   Bnd_Box& aBoxDS = aSIDS.ChangeBox();
150   BRepBndLib::Add(aVNew, aBoxDS);
151   aBoxDS.SetGap(aBoxDS.GetGap() + Precision::Confusion());
152   //
153   // add vertex to SD map
154   myDS->AddShapeSD(nV, nVNew);
155
156   // Add new vertex to map of vertices to avoid further extension
157   myVertsToAvoidExtension.Add(nVNew);
158
159   if (aTolV < aTolNew)
160     myIncreasedSS.Add(nV);
161
162   return nVNew;
163 }
164 //=======================================================================
165 //function : UpdatePaveBlocksWithSDVertices
166 //purpose  : 
167 //=======================================================================
168 void BOPAlgo_PaveFiller::UpdatePaveBlocksWithSDVertices()
169 {
170   myDS->UpdatePaveBlocksWithSDVertices();
171 }
172 //=======================================================================
173 //function : UpdateCommonBlocksWithSDVertices
174 //purpose  : 
175 //=======================================================================
176 void BOPAlgo_PaveFiller::UpdateCommonBlocksWithSDVertices()
177 {
178   if (!myNonDestructive) {
179     UpdatePaveBlocksWithSDVertices();
180     return;
181   }
182   Standard_Integer aNbPBP;
183   //
184   BOPDS_VectorOfListOfPaveBlock& aPBP=myDS->ChangePaveBlocksPool();
185   aNbPBP=aPBP.Length();
186   if(!aNbPBP) {
187     return;
188   }
189   //
190   Standard_Integer i, nV1, nV2;
191   Standard_Real aTolV;
192   BOPDS_MapOfCommonBlock aMCB;
193   BOPDS_ListIteratorOfListOfPaveBlock aItPB;
194   Handle(BOPDS_PaveBlock) aPB;
195   // 
196   aTolV = Precision::Confusion();
197   //
198   for (i=0; i<aNbPBP; ++i) {
199     BOPDS_ListOfPaveBlock& aLPB=aPBP(i);
200     aItPB.Initialize(aLPB);
201     for (; aItPB.More(); aItPB.Next()) {
202       aPB=aItPB.Value();
203       const Handle(BOPDS_CommonBlock)& aCB=myDS->CommonBlock(aPB);
204       if (aCB.IsNull()) {
205         continue;
206       }
207       //
208       if (aMCB.Add(aCB)) {
209         aPB->Indices(nV1, nV2);
210         UpdateVertex(nV1, aTolV);
211         UpdateVertex(nV2, aTolV);
212         myDS->UpdateCommonBlockWithSDVertices(aCB);
213       }
214     }
215   }
216   UpdatePaveBlocksWithSDVertices();
217 }
218
219 namespace
220 {
221   //=======================================================================
222   //function : UpdateInterfsWithSDVertices
223   //purpose  : 
224   //=======================================================================
225   template <class InterfType>
226   void UpdateIntfsWithSDVertices(BOPDS_PDS theDS, NCollection_Vector<InterfType>& theInterfs)
227   {
228     for (Standard_Integer i = 0; i < theInterfs.Length(); i++)
229     {
230       InterfType& anIntf = theInterfs(i);
231       Standard_Integer anInd;
232       if (anIntf.HasIndexNew(anInd))
233       {
234         Standard_Integer anIndSD;
235         if (theDS->HasShapeSD(anInd, anIndSD))
236         {
237           anIntf.SetIndexNew(anIndSD);
238         }
239       }
240     }
241   }
242 }
243
244 //=======================================================================
245 //function : UpdateInterfsWithSDVertices
246 //purpose  : 
247 //=======================================================================
248 void BOPAlgo_PaveFiller::UpdateInterfsWithSDVertices()
249 {
250   UpdateIntfsWithSDVertices(myDS, myDS->InterfVV());
251   UpdateIntfsWithSDVertices(myDS, myDS->InterfVE());
252   UpdateIntfsWithSDVertices(myDS, myDS->InterfVF());
253   UpdateIntfsWithSDVertices(myDS, myDS->InterfEE());
254   UpdateIntfsWithSDVertices(myDS, myDS->InterfEF());
255 }