0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / BOPTest / BOPTest_UtilityCommands.cxx
1 // Created on: 2016-04-01
2 // Created by: Nikolai BUKHALOV
3 // Copyright (c) 2000-2016 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <BOPTest.hxx>
17
18 #include <BOPTools_AlgoTools.hxx>
19 #include <BOPTools_AlgoTools2D.hxx>
20 #include <DBRep.hxx>
21 #include <IntTools_Context.hxx>
22 #include <TopoDS.hxx>
23 #include <TopoDS_Edge.hxx>
24 #include <TopoDS_Face.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <Draw.hxx>
28 #include <BOPAlgo_Tools.hxx>
29 #include <BRepLib.hxx>
30
31 static Standard_Integer attachpcurve (Draw_Interpretor&, Standard_Integer, const char**);
32 static Standard_Integer edgestowire  (Draw_Interpretor&, Standard_Integer, const char**);
33 static Standard_Integer edgestofaces  (Draw_Interpretor&, Standard_Integer, const char**);
34 static Standard_Integer BuildPcurvesOnPlane(Draw_Interpretor&, Standard_Integer, const char**);
35
36 //=======================================================================
37 //function : BOPCommands
38 //purpose  : 
39 //=======================================================================
40   void BOPTest::UtilityCommands(Draw_Interpretor& theCommands)
41 {
42   static Standard_Boolean done = Standard_False;
43   if (done) return;
44   done = Standard_True;
45   // Chapter's name
46   const char* group = "BOPTest commands";
47   // Commands
48   
49   theCommands.Add("attachpcurve", "attachpcurve eold enew face", __FILE__, attachpcurve, group);
50   theCommands.Add("edgestowire" , "edgestowire wire edges"     , __FILE__, edgestowire , group);
51   theCommands.Add("edgestofaces" , "edgestofaces faces edges [-a AngTol -s Shared(0/1)]", __FILE__, edgestofaces , group);
52   theCommands.Add("buildpcurvesonplane", "buildpcurvesonplane shape", __FILE__, BuildPcurvesOnPlane, group);
53   }
54
55 //=======================================================================
56 //function : BOPCommands
57 //purpose  : Attaches p-curve of the given edge to the given face.
58 //=======================================================================
59 static Standard_Integer attachpcurve(Draw_Interpretor& theDI,
60                                  Standard_Integer  theNArg,
61                                  const char ** theArgVal)
62 {
63   if (theNArg != 4)
64   {
65     theDI << "Use: " << theArgVal[0] << " eold enew face\n";
66     return 1;
67   }
68
69   TopoDS_Shape aShEOld(DBRep::Get(theArgVal[1]));
70   TopoDS_Shape aShENew(DBRep::Get(theArgVal[2]));
71   TopoDS_Shape aShFace(DBRep::Get(theArgVal[3]));
72
73   if (aShEOld.IsNull()) {
74     theDI << theArgVal[1] << " is null shape\n";
75     return 1;
76   } else if (aShEOld.ShapeType() != TopAbs_EDGE) {
77     theDI << theArgVal[1] << " is not an edge\n";
78     return 1;
79   }
80
81   if (aShENew.IsNull()) {
82     theDI << theArgVal[2] << " is null shape\n";
83     return 1;
84   } else if (aShENew.ShapeType() != TopAbs_EDGE) {
85     theDI << theArgVal[2] << " is not an edge\n";
86     return 1;
87   }
88
89   if (aShFace.IsNull()) {
90     theDI << theArgVal[3] << " is null shape\n";
91     return 1;
92   } else if (aShFace.ShapeType() != TopAbs_FACE) {
93     theDI << theArgVal[3] << " is not a face\n";
94     return 1;
95   }
96
97   TopoDS_Edge aEOld = TopoDS::Edge(aShEOld);
98   TopoDS_Edge aENew = TopoDS::Edge(aShENew);
99   TopoDS_Face aFace = TopoDS::Face(aShFace);
100
101   // Try to copy PCurve from old edge to the new one.
102   Handle(IntTools_Context) aCtx = new IntTools_Context;
103   const Standard_Integer   iRet =
104     BOPTools_AlgoTools2D::AttachExistingPCurve(aEOld, aENew, aFace, aCtx);
105
106   if (iRet) {
107     theDI << "Error! Code: " << iRet << "\n";
108   } else {
109     theDI << "PCurve is attached successfully\n";
110   }
111
112   return 0;
113 }
114
115 //=======================================================================
116 //function : edgestowire
117 //purpose  : Orients the edges to make wire
118 //=======================================================================
119 static Standard_Integer edgestowire(Draw_Interpretor& theDI,
120                                     Standard_Integer  theNArg,
121                                     const char ** theArgVal)
122 {
123   if (theNArg != 3) {
124     theDI << "Use: edgestowire wire edges\n";
125     return 1;
126   }
127   //
128   TopoDS_Shape anEdges = DBRep::Get(theArgVal[2]);
129   if (anEdges.IsNull()) {
130     theDI << "no edges\n";
131     return 1;
132   }
133   //
134   BOPTools_AlgoTools::OrientEdgesOnWire(anEdges);
135   DBRep::Set(theArgVal[1], anEdges);
136   return 0;
137 }
138
139 //=======================================================================
140 //function : edgestofaces
141 //purpose  : Creates planar faces from linear edges
142 //=======================================================================
143 static Standard_Integer edgestofaces(Draw_Interpretor& theDI,
144                                      Standard_Integer  theNArg,
145                                      const char ** theArgVal)
146 {
147   if (theNArg < 3) {
148     theDI << "Use: edgestofaces faces edges [-a AngTol -s Shared(0/1)]\n";
149     theDI << " AngTol - angular tolerance for comparing the planes;\n";
150     theDI << " Shared - boolean flag which defines whether the input\n";
151     theDI << "          edges are already shared or have to be intersected.\n";
152     return 1;
153   }
154   //
155   TopoDS_Shape anEdges = DBRep::Get(theArgVal[2]);
156   if (anEdges.IsNull()) {
157     theDI << "no edges\n";
158     return 1;
159   }
160   //
161   Standard_Real anAngTol = 1.e-8;
162   Standard_Boolean bShared = Standard_False;
163   //
164   for (Standard_Integer i = 3; i < theNArg; ++i) {
165     if (!strcmp(theArgVal[i], "-a") && (i+1 < theNArg)) {
166       anAngTol = Draw::Atof(theArgVal[i+1]);
167     }
168     if (!strcmp(theArgVal[i], "-s") && (i+1 < theNArg)) {
169       bShared = (Draw::Atoi(theArgVal[i+1]) == 1);
170     }
171   }
172   //
173   TopoDS_Shape aWires;
174   Standard_Integer iErr = BOPAlgo_Tools::EdgesToWires(anEdges, aWires, bShared, anAngTol);
175   if (iErr) {
176     theDI << "Unable to build wires from given edges\n";
177     return 0;
178   }
179   //
180   TopoDS_Shape aFaces;
181   Standard_Boolean bDone = BOPAlgo_Tools::WiresToFaces(aWires, aFaces, anAngTol);
182   if (!bDone) {
183     theDI << "Unable to build faces from wires\n";
184     return 0;
185   }
186   //
187   DBRep::Set(theArgVal[1], aFaces);
188   return 0;
189 }
190
191 //=======================================================================
192 //function : BuildPcurvesOnPlane
193 //purpose  : Build and store pcurves of edges on planes
194 //=======================================================================
195 static Standard_Integer BuildPcurvesOnPlane(Draw_Interpretor& theDI,
196                                  Standard_Integer  theNArg,
197                                  const char ** theArgVal)
198 {
199   if (theNArg != 2)
200   {
201     theDI << "Use: " << theArgVal[0] << " shape\n";
202     return 1;
203   }
204
205   TopoDS_Shape aShape(DBRep::Get(theArgVal[1]));
206   if (aShape.IsNull()) {
207     theDI << theArgVal[1] << " is null shape\n";
208     return 1;
209   }
210
211   TopExp_Explorer exp(aShape, TopAbs_FACE);
212   for (; exp.More(); exp.Next())
213   {
214     const TopoDS_Face& aF = TopoDS::Face(exp.Current());
215     BRepAdaptor_Surface aS(aF, Standard_False);
216     if (aS.GetType() == GeomAbs_Plane)
217     {
218       TopTools_ListOfShape aLE;
219       TopExp_Explorer exp1(aF, TopAbs_EDGE);
220       for (; exp1.More(); exp1.Next())
221         aLE.Append(exp1.Current());
222       BRepLib::BuildPCurveForEdgesOnPlane(aLE, aF);
223     }
224   }
225   return 0;
226 }