0030024: Data Exchange - STEP, IGES export support of BRep shapes based on tessellate...
[occt.git] / src / TopoDSToStep / TopoDSToStep_MakeManifoldSolidBrep.cxx
1 // Created on: 1993-07-23
2 // Created by: Martine LANGLOIS
3 // Copyright (c) 1993-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 <BRepClass3d.hxx>
19 #include <Interface_Static.hxx>
20 #include <Message_ProgressScope.hxx>
21 #include <StdFail_NotDone.hxx>
22 #include <StepShape_ClosedShell.hxx>
23 #include <StepShape_HArray1OfFace.hxx>
24 #include <StepShape_ManifoldSolidBrep.hxx>
25 #include <StepShape_OpenShell.hxx>
26 #include <StepVisual_TessellatedShell.hxx>
27 #include <StepVisual_TessellatedSolid.hxx>
28 #include <TCollection_HAsciiString.hxx>
29 #include <TopoDS_Shell.hxx>
30 #include <TopoDS_Solid.hxx>
31 #include <TopoDSToStep.hxx>
32 #include <TopoDSToStep_Builder.hxx>
33 #include <TopoDSToStep_MakeManifoldSolidBrep.hxx>
34 #include <TopoDSToStep_Tool.hxx>
35 #include <Transfer_FinderProcess.hxx>
36 #include <TransferBRep_ShapeMapper.hxx>
37
38 static std::pair<Handle(StepShape_ManifoldSolidBrep), Handle(StepVisual_TessellatedItem)>
39 MakeManifoldSolidBrep(const TopoDS_Shell& aShell,
40                       const Handle(Transfer_FinderProcess)& FP,
41                       const Message_ProgressRange& theProgress)
42 {
43   Handle(StepShape_ManifoldSolidBrep) theManifoldSolidBrep;
44   Handle(StepVisual_TessellatedItem) aTessItem;
45
46   MoniTool_DataMapOfShapeTransient aMap;
47   TopoDSToStep_Tool aTool(aMap, Standard_False);
48
49   const Standard_Integer aWriteTessGeom = Interface_Static::IVal("write.step.tessellated");
50
51   TopoDSToStep_Builder StepB(aShell, aTool, FP, aWriteTessGeom, theProgress);
52   if (theProgress.UserBreak())
53     return std::make_pair(theManifoldSolidBrep, aTessItem);
54
55   TopoDSToStep::AddResult(FP, aTool);
56
57   if (StepB.IsDone()) {
58     aTessItem = StepB.TessellatedValue();
59     Handle(StepShape_ClosedShell) aCShell = Handle(StepShape_ClosedShell)::DownCast(StepB.Value());
60     // si OPEN on le force a CLOSED mais que c est une honte !
61     if (aCShell.IsNull()) {
62       Handle(StepShape_OpenShell) aOShell = Handle(StepShape_OpenShell)::DownCast(StepB.Value());
63       if (aOShell.IsNull()) return std::make_pair(theManifoldSolidBrep, aTessItem);
64       else {
65         aCShell = new StepShape_ClosedShell;
66         aCShell->Init(aOShell->Name(), aOShell->CfsFaces());
67       }
68     }
69     theManifoldSolidBrep = new StepShape_ManifoldSolidBrep();
70     Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString("");
71     theManifoldSolidBrep->Init(aName, aCShell);
72   }
73
74   return std::make_pair(theManifoldSolidBrep, aTessItem);
75 }
76
77 //=============================================================================
78 // Create a ManifoldSolidBrep of StepShape from a Shell of TopoDS
79 //=============================================================================
80
81 TopoDSToStep_MakeManifoldSolidBrep::
82   TopoDSToStep_MakeManifoldSolidBrep(const TopoDS_Shell& aShell,
83                                      const Handle(Transfer_FinderProcess)& FP,
84                                      const Message_ProgressRange& theProgress)
85 {
86   std::pair<Handle(StepShape_ManifoldSolidBrep), Handle(StepVisual_TessellatedItem)>
87     aResult = MakeManifoldSolidBrep(aShell, FP, theProgress);
88   done = !aResult.first.IsNull() || !aResult.second.IsNull();
89   if (done) 
90   {
91     theManifoldSolidBrep = aResult.first;
92     theTessellatedItem = aResult.second;
93   }
94   if (!done && !theProgress.UserBreak()) 
95   {
96     Handle(TransferBRep_ShapeMapper) errShape = new TransferBRep_ShapeMapper(aShell);
97     FP->AddWarning(errShape, " Closed Shell not mapped to ManifoldSolidBrep");
98   }
99 }
100
101 //=============================================================================
102 // Create a ManifoldSolidBrep of StepShape from a Solid of TopoDS containing
103 // only one closed shell
104 //=============================================================================
105
106 TopoDSToStep_MakeManifoldSolidBrep::
107   TopoDSToStep_MakeManifoldSolidBrep(const TopoDS_Solid& aSolid,
108                                      const Handle(Transfer_FinderProcess)& FP,
109                                      const Message_ProgressRange& theProgress)
110 {
111   TopoDS_Shell aOuterShell = BRepClass3d::OuterShell(aSolid);
112   if (!aOuterShell.IsNull())
113   {
114     std::pair<Handle(StepShape_ManifoldSolidBrep), Handle(StepVisual_TessellatedItem)>
115       aResult = MakeManifoldSolidBrep(aOuterShell, FP, theProgress);
116     done = !aResult.first.IsNull() || !aResult.second.IsNull();
117     if (done) 
118     {
119       theManifoldSolidBrep = aResult.first;
120       if (!aResult.second.IsNull()) {
121         Handle(StepVisual_TessellatedSolid) aTessSolid = new StepVisual_TessellatedSolid();
122         Handle(StepVisual_TessellatedShell) aTessShell = Handle(StepVisual_TessellatedShell)::DownCast(aResult.second);
123         Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString("");
124         Handle(StepVisual_HArray1OfTessellatedStructuredItem) anItems
125           = new StepVisual_HArray1OfTessellatedStructuredItem(1, aTessShell->NbItems());
126         for (Standard_Integer i = 1; i <= aTessShell->NbItems(); ++i) 
127         {
128           anItems->SetValue(i, aTessShell->ItemsValue(i));
129         }
130         Standard_Boolean aHasGeomLink = !theManifoldSolidBrep.IsNull();
131         aTessSolid->Init(aName, anItems, aHasGeomLink, theManifoldSolidBrep);
132         theTessellatedItem = aTessSolid;
133       }
134     }
135     if (!done && !theProgress.UserBreak()) 
136     {
137       Handle(TransferBRep_ShapeMapper) errShape = new TransferBRep_ShapeMapper(aOuterShell);
138       FP->AddWarning(errShape, " Outer Shell of Solid not mapped to ManifoldSolidBrep");
139     }
140   }
141   else
142   {
143     Handle(TransferBRep_ShapeMapper) errShape = new TransferBRep_ShapeMapper(aOuterShell);
144     FP->AddWarning(errShape, " Outer Shell is null; not mapped to ManifoldSolidBrep ");
145     done = Standard_False;
146   }
147 }
148
149 //=============================================================================
150 // renvoi des valeurs
151 //=============================================================================
152
153 const Handle(StepShape_ManifoldSolidBrep) &
154       TopoDSToStep_MakeManifoldSolidBrep::Value() const
155 {
156   StdFail_NotDone_Raise_if (!done, "TopoDSToStep_MakeManifoldSolidBrep::Value() - no result");
157   return theManifoldSolidBrep;
158 }
159
160 // ============================================================================
161 // Method  : TopoDSToStep_MakeManifoldSolidBrep::TessellatedValue
162 // Purpose : Returns TessellatedItem as the optional result
163 // ============================================================================
164
165 const Handle(StepVisual_TessellatedItem)& 
166 TopoDSToStep_MakeManifoldSolidBrep::TessellatedValue() const
167 {
168   StdFail_NotDone_Raise_if(!done, "TopoDSToStep_MakeManifoldSolidBrep::TessellatedValue() - no result");
169   return theTessellatedItem;
170 }