0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / LocOpe / LocOpe_Prism.cxx
1 // Created on: 1996-09-04
2 // Created by: Olga PILLOT
3 // Copyright (c) 1996-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 <BRepLib_MakeVertex.hxx>
19 #include <BRepSweep_Prism.hxx>
20 #include <BRepTools_Modifier.hxx>
21 #include <BRepTools_TrsfModification.hxx>
22 #include <Geom_Curve.hxx>
23 #include <Geom_Line.hxx>
24 #include <Geom_TrimmedCurve.hxx>
25 #include <gp_Ax1.hxx>
26 #include <gp_Trsf.hxx>
27 #include <gp_Vec.hxx>
28 #include <LocOpe.hxx>
29 #include <LocOpe_BuildShape.hxx>
30 #include <LocOpe_Prism.hxx>
31 #include <StdFail_NotDone.hxx>
32 #include <TColgp_SequenceOfPnt.hxx>
33 #include <TopExp.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
39
40 //=======================================================================
41 //function : LocOpe_Prism
42 //purpose  : 
43 //=======================================================================
44 LocOpe_Prism::LocOpe_Prism()
45 : myIsTrans(Standard_False),
46   myDone(Standard_False)
47 {
48 }
49
50 //=======================================================================
51 //function : LocOpe_Prism
52 //purpose  : 
53 //=======================================================================
54
55 LocOpe_Prism::LocOpe_Prism(const TopoDS_Shape&  Base,
56                            const gp_Vec& V):
57        myBase(Base), myVec(V), myIsTrans(Standard_False)
58
59 {
60   IntPerf();
61 }
62
63 //=======================================================================
64 //function : LocOpe_Prism
65 //purpose  : 
66 //=======================================================================
67
68 LocOpe_Prism::LocOpe_Prism(const TopoDS_Shape&  Base,
69                            const gp_Vec& V,
70                            const gp_Vec& Vtra):
71        myBase(Base), myVec(V), myTra(Vtra), myIsTrans(Standard_True)
72
73 {
74   IntPerf();
75
76 }
77
78 //=======================================================================
79 //function : Perform
80 //purpose  : 
81 //=======================================================================
82
83 void LocOpe_Prism::Perform(const TopoDS_Shape& Base,
84                            const gp_Vec& V)
85 {
86   myMap.Clear();
87   myFirstShape.Nullify();
88   myLastShape.Nullify();
89   myBase.Nullify();
90   myRes.Nullify();
91
92   myBase = Base;
93   myVec = V;
94   myIsTrans = Standard_False;
95   IntPerf();
96 }
97
98 //=======================================================================
99 //function : Perform
100 //purpose  : 
101 //=======================================================================
102
103 void LocOpe_Prism::Perform(const TopoDS_Shape& Base,
104                            const gp_Vec& V,
105                            const gp_Vec& Vtra)
106 {
107   myMap.Clear();
108   myFirstShape.Nullify();
109   myLastShape.Nullify();
110   myBase.Nullify();
111   myRes.Nullify();
112
113   myBase = Base;
114   myVec = V;
115   myTra = Vtra;
116   myIsTrans = Standard_True;
117   IntPerf();
118 }
119
120
121 //=======================================================================
122 //function : IntPerf
123 //purpose  : 
124 //=======================================================================
125
126 void LocOpe_Prism::IntPerf()
127 {
128   TopoDS_Shape theBase = myBase;
129   BRepTools_Modifier Modif;
130   if (myIsTrans) {
131     gp_Trsf T;
132     T.SetTranslation(myTra);
133     Handle(BRepTools_TrsfModification) modbase = 
134       new BRepTools_TrsfModification(T);
135     Modif.Init(theBase);
136     Modif.Perform(modbase);
137     theBase = Modif.ModifiedShape(theBase);
138   }
139   
140   BRepSweep_Prism thePrism(theBase,myVec);
141
142   myFirstShape = thePrism.FirstShape();
143   myLastShape = thePrism.LastShape();
144
145   TopExp_Explorer exp;
146   if (theBase.ShapeType() == TopAbs_FACE) {
147     for (exp.Init(theBase,TopAbs_EDGE);exp.More();exp.Next()) {
148       const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
149       if (!myMap.IsBound(edg)) {
150         TopTools_ListOfShape thelist;
151         myMap.Bind(edg, thelist);
152         TopoDS_Shape desc = thePrism.Shape(edg);
153         if (!desc.IsNull()) {
154           myMap(edg).Append(desc);
155         }
156       }
157     }
158     myRes = thePrism.Shape();
159   }
160
161   else {
162     // Cas base != FACE
163     TopTools_IndexedDataMapOfShapeListOfShape theEFMap;
164     TopExp::MapShapesAndAncestors(theBase,TopAbs_EDGE,TopAbs_FACE,theEFMap);
165     TopTools_ListOfShape lfaces;
166     Standard_Boolean toremove = Standard_False;
167     for (Standard_Integer i=1; i<=theEFMap.Extent(); i++) {
168       const TopoDS_Shape& edg = theEFMap.FindKey(i);
169       TopTools_ListOfShape thelist1;
170       myMap.Bind(edg, thelist1);
171       TopoDS_Shape desc = thePrism.Shape(edg);
172       if (!desc.IsNull()) {
173         if (theEFMap(i).Extent() >= 2) {
174           toremove = Standard_True;
175         }
176         else {
177           myMap(edg).Append(desc);
178           lfaces.Append(desc);
179         }
180       }
181     }
182     if(toremove) {
183       // Rajouter les faces de FirstShape et LastShape
184       for (exp.Init(myFirstShape,TopAbs_FACE);exp.More();exp.Next()) {
185         lfaces.Append(exp.Current());
186       }
187       for (exp.Init(myLastShape,TopAbs_FACE);exp.More();exp.Next()) {
188         lfaces.Append(exp.Current());
189       }
190       
191       LocOpe_BuildShape BS(lfaces);
192       myRes = BS.Shape();
193     }
194     else {
195       for (exp.Init(theBase,TopAbs_EDGE);exp.More();exp.Next()) {
196         const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
197         if (!myMap.IsBound(edg)) {
198           TopTools_ListOfShape thelist2;
199           myMap.Bind(edg, thelist2);
200           TopoDS_Shape desc = thePrism.Shape(edg);
201           if (!desc.IsNull()) {
202             myMap(edg).Append(desc);
203           }
204         }
205       }
206       myRes = thePrism.Shape();
207     }
208   }
209
210   if (myIsTrans) {
211     // m-a-j des descendants
212     TopExp_Explorer anExp;
213     for (anExp.Init(myBase,TopAbs_EDGE); anExp.More(); anExp.Next()) {
214       const TopoDS_Edge& edg = TopoDS::Edge(anExp.Current());
215       const TopoDS_Edge& edgbis = TopoDS::Edge(Modif.ModifiedShape(edg));
216       if (!edgbis.IsSame(edg) && myMap.IsBound(edgbis)) {
217         myMap.Bind(edg,myMap(edgbis));
218         myMap.UnBind(edgbis);
219       }
220     }
221   }
222   myDone = Standard_True;
223 }
224
225 //=======================================================================
226 //function : Shape
227 //purpose  : 
228 //=======================================================================
229
230 const TopoDS_Shape& LocOpe_Prism::Shape () const
231 {
232   if (!myDone) {
233     throw StdFail_NotDone();
234   }
235   return myRes;
236 }
237
238
239 //=======================================================================
240 //function : FirstShape
241 //purpose  : 
242 //=======================================================================
243
244 const TopoDS_Shape& LocOpe_Prism::FirstShape () const
245 {
246   return myFirstShape;
247 }
248
249 //=======================================================================
250 //function : LastShape
251 //purpose  : 
252 //=======================================================================
253
254 const TopoDS_Shape& LocOpe_Prism::LastShape () const
255 {
256   return myLastShape;
257 }
258
259
260 //=======================================================================
261 //function : Shapes
262 //purpose  : 
263 //=======================================================================
264
265 const TopTools_ListOfShape& LocOpe_Prism::Shapes (const TopoDS_Shape& S) const
266 {
267   return myMap(S);
268 }
269
270
271 //=======================================================================
272 //function : Curves
273 //purpose  : 
274 //=======================================================================
275
276 void LocOpe_Prism::Curves(TColGeom_SequenceOfCurve& Scurves) const
277 {
278   Scurves.Clear();
279   TColgp_SequenceOfPnt spt;
280   LocOpe::SampleEdges(myFirstShape,spt);
281   Standard_Real height = 
282     Sqrt(myVec.X()*myVec.X()+myVec.Y()*myVec.Y()+myVec.Z()*myVec.Z());
283   Standard_Real u1 = -2*height;
284   Standard_Real u2 =  2*height;
285
286   for (Standard_Integer jj=1;jj<=spt.Length(); jj++) {
287     gp_Ax1 theAx(spt(jj),myVec);
288     Handle(Geom_Line) theLin = new Geom_Line(theAx);
289     Handle(Geom_TrimmedCurve) trlin = 
290       new Geom_TrimmedCurve(theLin, u1, u2, Standard_True);   
291     Scurves.Append(trlin);
292   }
293 }
294
295 //=======================================================================
296 //function : BarycCurve
297 //purpose  : 
298 //=======================================================================
299
300 Handle(Geom_Curve) LocOpe_Prism::BarycCurve() const
301 {
302   gp_Pnt bar(0., 0., 0.);
303   TColgp_SequenceOfPnt spt;
304   LocOpe::SampleEdges(myFirstShape,spt);
305   for (Standard_Integer jj=1;jj<=spt.Length(); jj++) {
306     const gp_Pnt& pvt = spt(jj);
307     bar.ChangeCoord() += pvt.XYZ();
308   }
309   bar.ChangeCoord().Divide(spt.Length());
310   gp_Ax1 newAx(bar,myVec);
311   Handle(Geom_Line) theLin = new Geom_Line(newAx);
312   return theLin;
313 }
314