0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / TopoDS / TopoDS_Builder.cxx
1 // Created on: 1991-04-09
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1991-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 <Standard_NullObject.hxx>
19 #include <TopoDS_Builder.hxx>
20 #include <TopoDS_Compound.hxx>
21 #include <TopoDS_CompSolid.hxx>
22 #include <TopoDS_FrozenShape.hxx>
23 #include <TopoDS_ListIteratorOfListOfShape.hxx>
24 #include <TopoDS_Shape.hxx>
25 #include <TopoDS_Shell.hxx>
26 #include <TopoDS_Solid.hxx>
27 #include <TopoDS_TCompound.hxx>
28 #include <TopoDS_TShape.hxx>
29 #include <TopoDS_TWire.hxx>
30 #include <TopoDS_UnCompatibleShapes.hxx>
31 #include <TopoDS_Wire.hxx>
32
33 //=======================================================================
34 //function : MakeShape
35 //purpose  : Make a Shape from a TShape
36 //=======================================================================
37 void TopoDS_Builder::MakeShape (TopoDS_Shape& S, 
38                                 const Handle(TopoDS_TShape)& T) const
39 {
40   S.TShape(T);
41   S.Location(TopLoc_Location());
42   S.Orientation(TopAbs_FORWARD);
43 }
44
45
46 //=======================================================================
47 //function : Add
48 //purpose  : insert aComponent in aShape
49 //=======================================================================
50
51 void TopoDS_Builder::Add (TopoDS_Shape& aShape, 
52                           const TopoDS_Shape& aComponent) const
53 {
54   // From now the Component cannot be edited
55   aComponent.TShape()->Free(Standard_False);
56
57   // Note that freezing aComponent before testing if aShape is free
58   // prevents from self-insertion
59   // but aShape will be frozen when the Exception is raised
60   if (aShape.Free())
61   {
62     static const unsigned int aTb[9]=
63     {
64       //COMPOUND to:
65       (1<<((unsigned int)TopAbs_COMPOUND)),
66       //COMPSOLID to:
67       (1<<((unsigned int)TopAbs_COMPOUND)),
68       //SOLID to:
69       (1<<((unsigned int)TopAbs_COMPOUND)) |
70       (1<<((unsigned int)TopAbs_COMPSOLID)),
71       //SHELL to:
72       (1<<((unsigned int)TopAbs_COMPOUND)) |
73       (1<<((unsigned int)TopAbs_SOLID)),
74       //FACE to:
75       (1<<((unsigned int)TopAbs_COMPOUND)) |
76       (1<<((unsigned int)TopAbs_SHELL)),
77       //WIRE to:
78       (1<<((unsigned int)TopAbs_COMPOUND)) |
79       (1<<((unsigned int)TopAbs_FACE)),
80       //EDGE to:
81       (1<<((unsigned int)TopAbs_COMPOUND)) |
82       (1<<((unsigned int)TopAbs_SOLID)) |
83       (1<<((unsigned int)TopAbs_WIRE)),
84       //VERTEX to:
85       (1<<((unsigned int)TopAbs_COMPOUND)) |
86       (1<<((unsigned int)TopAbs_SOLID)) |
87       (1<<((unsigned int)TopAbs_FACE)) |
88       (1<<((unsigned int)TopAbs_EDGE)),
89       //SHAPE to:
90       0
91     };
92     //
93     const unsigned int iC=(unsigned int)aComponent.ShapeType();
94     const unsigned int iS=(unsigned int)aShape.ShapeType();
95     //
96     if ((aTb[iC] & (1<<iS)) != 0) {
97       TopoDS_ListOfShape& L = aShape.TShape()->myShapes;
98       L.Append(aComponent);
99       TopoDS_Shape& S = L.Last();
100       //
101       // compute the relative Orientation
102       if (aShape.Orientation() == TopAbs_REVERSED)
103         S.Reverse();
104       //
105       // and the Relative Location
106       const TopLoc_Location& aLoc=aShape.Location();
107       if (!aLoc.IsIdentity())
108         S.Move(aLoc.Inverted());
109       //
110       // Set the TShape as modified.
111       aShape.TShape()->Modified(Standard_True);
112     }
113     else {
114       throw TopoDS_UnCompatibleShapes("TopoDS_Builder::Add");
115     }
116   }
117   else {
118     throw TopoDS_FrozenShape("TopoDS_Buider::Add");
119   }
120 }
121
122
123 //=======================================================================
124 //function : Remove
125 //purpose  : Remove a Shape from an other one
126 //=======================================================================
127
128 void TopoDS_Builder::Remove (TopoDS_Shape& aShape, 
129                              const TopoDS_Shape& aComponent) const
130 {
131   // check  if aShape  is  not Frozen
132   TopoDS_FrozenShape_Raise_if (!aShape.Free(),"TopoDS_Builder::Remove");
133   
134   // compute the relative Orientation and Location of aComponent
135   TopoDS_Shape S = aComponent;
136   if (aShape.Orientation() == TopAbs_REVERSED)
137     S.Reverse();
138   S.Location(S.Location().Predivided(aShape.Location()));
139
140   TopoDS_ListOfShape& L = aShape.TShape()->myShapes;
141   TopoDS_ListIteratorOfListOfShape It(L);
142   while (It.More()) {
143     if (It.Value() == S) {
144       L.Remove(It);
145       aShape.TShape()->Modified(Standard_True);
146       break;
147     }
148     It.Next();
149   }
150 }