0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[occt.git] / src / TopoDSToStep / TopoDSToStep_Builder.cxx
1 // Created on: 1994-11-25
2 // Created by: Frederic MAUPAS
3 // Copyright (c) 1994-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
18 #include <Message_ProgressIndicator.hxx>
19 #include <StdFail_NotDone.hxx>
20 #include <StepShape_ClosedShell.hxx>
21 #include <StepShape_ConnectedFaceSet.hxx>
22 #include <StepShape_Face.hxx>
23 #include <StepShape_FaceSurface.hxx>
24 #include <StepShape_HArray1OfFace.hxx>
25 #include <StepShape_OpenShell.hxx>
26 #include <StepShape_TopologicalRepresentationItem.hxx>
27 #include <TCollection_HAsciiString.hxx>
28 #include <TColStd_SequenceOfTransient.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Shell.hxx>
34 #include <TopoDSToStep_Builder.hxx>
35 #include <TopoDSToStep_MakeStepFace.hxx>
36 #include <TopoDSToStep_Tool.hxx>
37 #include <Transfer_FinderProcess.hxx>
38 #include <TransferBRep_ShapeMapper.hxx>
39
40 // ============================================================================
41 // Method  : TopoDSToStep_Builder::TopoDSToStep_Builder
42 // Purpose :
43 // ============================================================================
44 TopoDSToStep_Builder::TopoDSToStep_Builder()
45 {
46   done = Standard_False;
47 }
48
49 // ============================================================================
50 // Method  : TopoDSToStep_Builder::TopoDSToStep_Builder
51 // Purpose :
52 // ============================================================================
53
54 TopoDSToStep_Builder::TopoDSToStep_Builder
55 (const TopoDS_Shape& aShape,
56  TopoDSToStep_Tool& aTool, const Handle(Transfer_FinderProcess)& FP)
57 {
58   done = Standard_False;
59   Init(aShape, aTool, FP);
60 }
61
62 // ============================================================================
63 // Method  : TopoDSToStep_Builder::Init
64 // Purpose :
65 // ============================================================================
66
67 void TopoDSToStep_Builder::Init(const TopoDS_Shape& aShape,
68                                 TopoDSToStep_Tool& myTool,
69                                 const Handle(Transfer_FinderProcess)& FP)
70 {
71   
72    if (myTool.IsBound(aShape)) {
73     myError  = TopoDSToStep_BuilderDone;
74     done     = Standard_True;
75     myResult = myTool.Find(aShape);
76     return;
77   }
78
79   Handle(Message_ProgressIndicator) progress = FP->GetProgress();
80
81   switch (aShape.ShapeType()) 
82     {      
83     case TopAbs_SHELL: 
84       { 
85         TopoDS_Shell myShell = TopoDS::Shell(aShape);     
86         myTool.SetCurrentShell(myShell);
87         
88         Handle(StepShape_FaceSurface)                   FS;
89         Handle(StepShape_TopologicalRepresentationItem) Fpms;
90         TColStd_SequenceOfTransient                 mySeq;
91         
92 //      const TopoDS_Shell ForwardShell = 
93 //        TopoDS::Shell(myShell.Oriented(TopAbs_FORWARD));
94
95 //      TopExp_Explorer myExp(ForwardShell, TopAbs_FACE);
96 //  CKY  9-DEC-1997 (PRO9824 et consorts)
97 //   Pour passer les orientations : ELLES SONT DONNEES EN RELATIF
98 //   Donc, pour SHELL, on doit l ecrire en direct en STEP (pas le choix)
99 //   -> il faut repercuter en dessous, donc explorer le Shell TEL QUEL
100 //   Pour FACE WIRE, d une part on ECRIT SON ORIENTATION relative au contenant
101 //     (puisqu on peut), d autre part on EXPLORE EN FORWARD : ainsi les
102 //     orientations des sous-shapes sont relatives a leur contenant immediat
103 //     et la recombinaison en lecture est sans malice
104 //  Il reste ici et la du code relatif a "en Faceted on combine differemment"
105 //  -> reste encore du menage a faire
106
107
108
109         TopExp_Explorer myExp(myShell, TopAbs_FACE);
110
111         TopoDSToStep_MakeStepFace MkFace;
112
113         for (;myExp.More();myExp.Next()) {
114
115           const TopoDS_Face Face = TopoDS::Face(myExp.Current());
116     
117     MkFace.Init(Face, myTool, FP);
118
119           if (MkFace.IsDone()) {
120             FS = Handle(StepShape_FaceSurface)::DownCast(MkFace.Value());
121             Fpms = FS;
122             mySeq.Append(Fpms);
123           }
124           else {
125             // MakeFace Error Handling : warning only
126 //          std::cout << "Warning : one Face has not been mapped" << std::endl;
127 //        Handle(TransferBRep_ShapeMapper) errShape =
128 //          new TransferBRep_ShapeMapper(Face);
129 //          FP->AddWarning(errShape, " a Face from a Shell has not been mapped");
130           }
131     if (!progress.IsNull()) progress->Increment();
132         }
133
134         Standard_Integer nbFaces = mySeq.Length();
135         if ( nbFaces >= 1) {
136           Handle(StepShape_HArray1OfFace) aSet = 
137             new StepShape_HArray1OfFace(1,nbFaces);
138           for (Standard_Integer i=1; i<=nbFaces; i++ ) {
139             aSet->SetValue(i, Handle(StepShape_Face)::DownCast(mySeq.Value(i)));
140           }
141           Handle(StepShape_ConnectedFaceSet) CFSpms;
142           if (myShell.Closed())
143             CFSpms = new StepShape_ClosedShell();
144           else
145             CFSpms = new StepShape_OpenShell();
146           Handle(TCollection_HAsciiString) aName = 
147             new TCollection_HAsciiString("");
148           CFSpms->Init(aName, aSet);
149
150           // --------------------------------------------------------------
151           // To add later : if not facetted context & shell is reversed
152           //                then shall create an oriented_shell with
153           //                orientation flag to false.
154           // --------------------------------------------------------------
155
156           myTool.Bind(aShape, CFSpms);
157           myResult = CFSpms;
158           done     = Standard_True;
159         }
160         else {
161           // Builder Error handling;
162           myError = TopoDSToStep_NoFaceMapped;
163           done    = Standard_False;
164         }
165         break;
166       }
167       
168     case TopAbs_FACE:
169       {
170         const TopoDS_Face Face = TopoDS::Face(aShape);    
171
172         Handle(StepShape_FaceSurface)                   FS;
173         Handle(StepShape_TopologicalRepresentationItem) Fpms;
174
175         TopoDSToStep_MakeStepFace MkFace(Face, myTool, FP);
176
177         if (MkFace.IsDone()) {
178           FS = Handle(StepShape_FaceSurface)::DownCast(MkFace.Value());
179           Fpms = FS;
180           myResult = Fpms;
181           myError = TopoDSToStep_BuilderDone;
182           done = Standard_True;
183         }
184         else {
185           // MakeFace Error Handling : Face not Mapped
186           myError = TopoDSToStep_BuilderOther;
187 //        Handle(TransferBRep_ShapeMapper) errShape =
188 //          new TransferBRep_ShapeMapper(Face);
189 //        FP->AddWarning(errShape, " the Face has not been mapped");
190           done = Standard_False;
191         }
192         if (!progress.IsNull()) progress->Increment();
193         break;
194       }
195     default: break;
196     }
197 }
198
199 // ============================================================================
200 // Method  : TopoDSToStep_Builder::Value
201 // Purpose :
202 // ============================================================================
203
204 const Handle(StepShape_TopologicalRepresentationItem)& 
205 TopoDSToStep_Builder::Value() const 
206 {
207   StdFail_NotDone_Raise_if (!done, "TopoDSToStep_Builder::Value() - no result");
208   return myResult;
209 }
210
211 // ============================================================================
212 // Method  : TopoDSToStep_Builder::Error
213 // Purpose :
214 // ============================================================================
215
216 TopoDSToStep_BuilderError TopoDSToStep_Builder::Error() const 
217 {
218   return myError;
219 }
220