ff18adbf22dea5db85de4c05904be34a988496a6
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_ShapeDivide.cxx
1 // Created on: 1999-04-27
2 // Created by: Pavel DURANDIN
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 //    abv 16.06.99 returning ReShape context; processing shared subshapes in compounds
18 //    sln 29.11.01 Bug21: in method Perform(..) nullify location of compound's faces only if mode myConsiderLocation is on
19
20 #include <BRep_Builder.hxx>
21 #include <Message_Msg.hxx>
22 #include <Precision.hxx>
23 #include <ShapeBuild_ReShape.hxx>
24 #include <ShapeExtend.hxx>
25 #include <ShapeExtend_BasicMsgRegistrator.hxx>
26 #include <ShapeUpgrade_FaceDivide.hxx>
27 #include <ShapeUpgrade_ShapeDivide.hxx>
28 #include <ShapeUpgrade_WireDivide.hxx>
29 #include <Standard_ErrorHandler.hxx>
30 #include <Standard_Failure.hxx>
31 #include <TopExp.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Compound.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Wire.hxx>
40
41 //=======================================================================
42 //function : ShapeUpgrade_ShapeDivide
43 //purpose  : 
44 //=======================================================================
45 ShapeUpgrade_ShapeDivide::ShapeUpgrade_ShapeDivide() : myStatus(0)
46 {
47   myPrecision = myMinTol = Precision::Confusion();
48   myMaxTol = 1; //Precision::Infinite() ?? pdn
49   mySplitFaceTool = new ShapeUpgrade_FaceDivide;
50   myContext = new ShapeBuild_ReShape;
51   //myMsgReg = new ShapeExtend_BasicMsgRegistrator;
52   mySegmentMode = Standard_True;
53   myEdgeMode = 2;
54 }
55
56 //=======================================================================
57 //function : ShapeUpgrade_ShapeDivide
58 //purpose  : 
59 //=======================================================================
60
61 ShapeUpgrade_ShapeDivide::ShapeUpgrade_ShapeDivide(const TopoDS_Shape &S) : myStatus(0)
62 {
63   myPrecision = myMinTol = Precision::Confusion();
64   myMaxTol = 1; //Precision::Infinite() ?? pdn
65   mySplitFaceTool = new ShapeUpgrade_FaceDivide;
66   myContext = new ShapeBuild_ReShape;
67   mySegmentMode = Standard_True;
68   myEdgeMode = 2;
69   Init(S);
70 }
71
72 //=======================================================================
73 //function : Init
74 //purpose  : 
75 //=======================================================================
76
77 void ShapeUpgrade_ShapeDivide::Init(const TopoDS_Shape &S)
78 {
79   myShape = S;
80 }
81
82 //=======================================================================
83 //function : Delete
84 //purpose  : 
85 //=======================================================================
86
87 void ShapeUpgrade_ShapeDivide::Delete()
88 {}
89
90
91 //=======================================================================
92 //function : SetPrecision
93 //purpose  : 
94 //=======================================================================
95
96 void ShapeUpgrade_ShapeDivide::SetPrecision (const Standard_Real Prec)
97 {
98   myPrecision = Prec;
99 }
100
101 //=======================================================================
102 //function : SetMaxTolerance
103 //purpose  : 
104 //=======================================================================
105
106 void ShapeUpgrade_ShapeDivide::SetMaxTolerance (const Standard_Real maxtol)
107 {
108   myMaxTol = maxtol;
109 }
110
111
112 //=======================================================================
113 //function : SetMinTolerance
114 //purpose  : 
115 //=======================================================================
116
117 void ShapeUpgrade_ShapeDivide::SetMinTolerance (const Standard_Real mintol)
118 {
119   myMinTol = mintol;
120 }
121
122
123 //=======================================================================
124 //function : SetSurfaceSegmentMode
125 //purpose  : 
126 //=======================================================================
127
128 void ShapeUpgrade_ShapeDivide::SetSurfaceSegmentMode(const Standard_Boolean Segment)
129 {
130   mySegmentMode = Segment;
131 }
132 //=======================================================================
133 //function : Perform
134 //purpose  : 
135 //=======================================================================
136
137 Standard_Boolean ShapeUpgrade_ShapeDivide::Perform(const Standard_Boolean newContext)
138 {
139   myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
140   if(myShape.IsNull()) {
141     myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
142     return Standard_False;
143   }
144   
145   if ( newContext || myContext.IsNull() ) 
146     myContext = new ShapeBuild_ReShape;
147
148   // Process COMPOUNDs separately in order to handle sharing in assemblies
149   // NOTE: not optimized: subshape can be processed twice (second time - no modif)
150   if ( myShape.ShapeType() == TopAbs_COMPOUND ) {
151     Standard_Integer locStatus = myStatus;
152     TopoDS_Compound C;
153     BRep_Builder B;
154     B.MakeCompound ( C );
155     TopoDS_Shape savShape = myShape;
156     for ( TopoDS_Iterator it(savShape,Standard_False); it.More(); it.Next() ) {
157       TopoDS_Shape shape = it.Value();
158       TopLoc_Location L = shape.Location();
159       if(myContext->ModeConsiderLocation()) {
160         TopLoc_Location nullLoc;
161         shape.Location ( nullLoc );
162       }
163       myShape = myContext->Apply ( shape );
164       Perform ( Standard_False );
165      if(myContext->ModeConsiderLocation()) 
166         myResult.Location ( L );
167       myResult.Orientation(TopAbs::Compose(myResult.Orientation(),savShape.Orientation()));
168       B.Add ( C, myResult );
169       locStatus |= myStatus;
170     }
171     
172     myShape = savShape;
173     myStatus = locStatus;
174     if ( Status ( ShapeExtend_DONE ) ) {
175       myResult = myContext->Apply ( C, TopAbs_SHAPE );
176       myContext->Replace ( myShape, myResult );
177       return Standard_True;
178     }
179     myResult = myShape;
180     return Standard_False;
181   }
182
183   // Process FACEs
184   Handle(ShapeUpgrade_FaceDivide) SplitFace = GetSplitFaceTool();
185   if ( ! SplitFace.IsNull() ) {
186     SplitFace->SetPrecision( myPrecision );
187     SplitFace->SetMaxTolerance(myMaxTol);
188     SplitFace->SetSurfaceSegmentMode(mySegmentMode);
189     Handle(ShapeUpgrade_WireDivide) SplitWire = SplitFace->GetWireDivideTool();
190     if ( ! SplitWire.IsNull() ) {
191       SplitWire->SetMinTolerance(myMinTol);
192       SplitWire->SetEdgeMode(myEdgeMode);
193     }
194     Message_Msg doneMsg = GetFaceMsg();
195
196     for(TopExp_Explorer exp(myShape,TopAbs_FACE); exp.More(); exp.Next()) {
197 //smh#8
198       TopoDS_Shape tmpF = exp.Current().Oriented ( TopAbs_FORWARD );
199       TopoDS_Face F = TopoDS::Face (tmpF); // protection against INTERNAL shapes: cts20105a.rle
200       TopoDS_Shape sh = myContext->Apply ( F, TopAbs_SHAPE );
201       for (TopExp_Explorer exp2(sh,TopAbs_FACE); exp2.More(); exp2.Next()) {
202 //szv: try-catch added
203         try {
204           OCC_CATCH_SIGNALS
205           for (; exp2.More(); exp2.Next()) {
206             TopoDS_Face face = TopoDS::Face ( exp2.Current() );
207             SplitFace->Init(face);
208             SplitFace->SetContext(myContext);
209             SplitFace->Perform();
210             if(SplitFace->Status(ShapeExtend_FAIL)) {
211               myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
212             }
213             if(SplitFace->Status(ShapeExtend_DONE)) {
214               myContext->Replace(face,SplitFace->Result());
215               SendMsg( face, doneMsg, Message_Info );
216               if(SplitFace->Status(ShapeExtend_DONE1))
217                 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
218               if(SplitFace->Status(ShapeExtend_DONE2))
219                 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
220             }
221           }
222         }
223         catch (Standard_Failure) {
224           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
225 #ifdef OCCT_DEBUG
226           cout << "\nError: Exception in ShapeUpgrade_FaceDivide::Perform(): ";
227           Standard_Failure::Caught()->Print(cout); cout << endl;
228 #endif
229         }
230       }
231     }
232   }
233   
234   // Process free WIREs
235   Handle(ShapeUpgrade_WireDivide) SplitWire = SplitFace->GetWireDivideTool();
236   if ( ! SplitWire.IsNull() ) {
237     SplitWire->SetFace (TopoDS_Face());
238     SplitWire->SetPrecision( myPrecision );
239     SplitWire->SetMaxTolerance(myMaxTol);
240     SplitWire->SetMinTolerance(myMinTol);
241     SplitWire->SetEdgeMode(myEdgeMode);
242     Message_Msg doneMsg = GetWireMsg();
243
244     TopExp_Explorer exp;//svv Jan 10 2000 : porting on DEC
245     for (exp.Init (myShape, TopAbs_WIRE, TopAbs_FACE); exp.More(); exp.Next()) {
246 //smh#8
247       TopoDS_Shape tmpW = exp.Current().Oriented ( TopAbs_FORWARD );
248       TopoDS_Wire W = TopoDS::Wire (tmpW );   // protection against INTERNAL shapes
249       TopoDS_Shape sh = myContext->Apply ( W, TopAbs_SHAPE );
250       for (TopExp_Explorer exp2 (sh,TopAbs_WIRE); exp2.More(); exp2.Next()) {
251         TopoDS_Wire wire = TopoDS::Wire ( exp2.Current() );
252         SplitWire->Load (wire);
253         SplitWire->SetContext(myContext);
254         SplitWire->Perform();
255         if(SplitWire->Status(ShapeExtend_FAIL)) {
256           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
257         }
258         if(SplitWire->Status(ShapeExtend_DONE)) {
259           myContext->Replace(wire,SplitWire->Wire());
260           SendMsg( wire, doneMsg, Message_Info );
261           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
262         }
263       }
264     }
265   
266     // Process free EDGEs
267     Message_Msg edgeDoneMsg = GetEdgeMsg();
268     for (exp.Init (myShape, TopAbs_EDGE, TopAbs_WIRE); exp.More(); exp.Next()) {
269 //smh#8
270       TopoDS_Shape tmpE = exp.Current().Oriented ( TopAbs_FORWARD );
271       TopoDS_Edge E = TopoDS::Edge (tmpE );                                       // protection against INTERNAL shapes
272       TopoDS_Vertex V1,V2;
273       TopExp::Vertices(E,V2,V1);
274       if( V1.IsNull() && V2.IsNull() ) continue; // skl 27.10.2004 for OCC5624
275       TopoDS_Shape sh = myContext->Apply ( E, TopAbs_SHAPE );
276       for (TopExp_Explorer exp2 (sh,TopAbs_EDGE); exp2.More(); exp2.Next()) {
277         TopoDS_Edge edge = TopoDS::Edge ( exp2.Current() );
278         SplitWire->Load (edge);
279         SplitWire->SetContext(myContext);
280         SplitWire->Perform();
281         if(SplitWire->Status(ShapeExtend_FAIL)) {
282           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
283         }
284         if(SplitWire->Status(ShapeExtend_DONE)) {
285           myContext->Replace(edge,SplitWire->Wire());
286           SendMsg( edge, edgeDoneMsg, Message_Info );
287           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
288         }
289       }
290     }
291   }
292   myResult = myContext->Apply ( myShape, TopAbs_SHAPE );
293   return ! myResult.IsSame ( myShape );
294 }
295
296 //=======================================================================
297 //function : Result
298 //purpose  : 
299 //=======================================================================
300
301 TopoDS_Shape ShapeUpgrade_ShapeDivide::Result() const
302 {
303   return myResult;
304 }
305
306 //=======================================================================
307 //function : GetContext
308 //purpose  : 
309 //=======================================================================
310
311 Handle(ShapeBuild_ReShape) ShapeUpgrade_ShapeDivide::GetContext() const
312 {
313   //if ( myContext.IsNull() ) myContext = new ShapeBuild_ReShape;
314   return myContext;
315 }
316
317 //=======================================================================
318 //function : SetContext
319 //purpose  : 
320 //=======================================================================
321
322 void ShapeUpgrade_ShapeDivide::SetContext (const Handle(ShapeBuild_ReShape) &context)
323 {
324   myContext = context;
325 }
326
327 //=======================================================================
328 //function : SetSplitFaceTool
329 //purpose  : 
330 //=======================================================================
331
332 void ShapeUpgrade_ShapeDivide::SetSplitFaceTool(const Handle(ShapeUpgrade_FaceDivide)& splitFaceTool)
333 {
334   mySplitFaceTool = splitFaceTool;
335 }
336
337 //=======================================================================
338 //function : GetSplitFaceTool
339 //purpose  : 
340 //=======================================================================
341
342 Handle(ShapeUpgrade_FaceDivide) ShapeUpgrade_ShapeDivide::GetSplitFaceTool () const
343 {
344   return mySplitFaceTool;
345 }
346
347 //=======================================================================
348 //function : Status
349 //purpose  : 
350 //=======================================================================
351
352 Standard_Boolean ShapeUpgrade_ShapeDivide::Status (const ShapeExtend_Status status) const
353 {
354   return ShapeExtend::DecodeStatus ( myStatus, status );
355 }
356
357 //=======================================================================
358 //function : SetEdgeMode
359 //purpose  : 
360 //=======================================================================
361
362  void ShapeUpgrade_ShapeDivide::SetEdgeMode(const Standard_Integer aEdgeMode) 
363 {
364   myEdgeMode = aEdgeMode;
365 }
366
367 //=======================================================================
368 //function : SetMsgRegistrator
369 //purpose  : 
370 //=======================================================================
371
372 void ShapeUpgrade_ShapeDivide::SetMsgRegistrator(const Handle(ShapeExtend_BasicMsgRegistrator)& msgreg) 
373 {
374   myMsgReg = msgreg;
375 }
376
377 //=======================================================================
378 //function : MsgRegistrator
379 //purpose  : 
380 //=======================================================================
381
382 Handle(ShapeExtend_BasicMsgRegistrator) ShapeUpgrade_ShapeDivide::MsgRegistrator() const
383 {
384   return myMsgReg;
385 }
386
387 //=======================================================================
388 //function : SendMsg
389 //purpose  :
390 //=======================================================================
391
392 void ShapeUpgrade_ShapeDivide::SendMsg(const TopoDS_Shape& shape,
393                                        const Message_Msg& message,
394                                        const Message_Gravity gravity) const
395 {
396   if ( !myMsgReg.IsNull() )
397     myMsgReg->Send (shape, message, gravity);
398 }
399
400 Message_Msg ShapeUpgrade_ShapeDivide::GetFaceMsg() const
401 {
402   return "ShapeDivide.FaceDivide.MSG0";
403 }
404 Message_Msg ShapeUpgrade_ShapeDivide::GetWireMsg() const
405 {
406   return "ShapeDivide.WireDivide.MSG0";
407 }
408 Message_Msg ShapeUpgrade_ShapeDivide::GetEdgeMsg() const
409 {
410   return "ShapeDivide.EdgeDivide.MSG0";
411 }