0028426: Implementation of the EdgesToFaces function
[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 <Draw.hxx>
27 #include <BOPAlgo_Tools.hxx>
28
29 static Standard_Integer attachpcurve (Draw_Interpretor&, Standard_Integer, const char**);
30 static Standard_Integer edgestowire  (Draw_Interpretor&, Standard_Integer, const char**);
31 static Standard_Integer edgestofaces  (Draw_Interpretor&, Standard_Integer, const char**);
32
33 //=======================================================================
34 //function : BOPCommands
35 //purpose  : 
36 //=======================================================================
37   void BOPTest::UtilityCommands(Draw_Interpretor& theCommands)
38 {
39   static Standard_Boolean done = Standard_False;
40   if (done) return;
41   done = Standard_True;
42   // Chapter's name
43   const char* group = "BOPTest commands";
44   // Commands
45   
46   theCommands.Add("attachpcurve", "attachpcurve eold enew face", __FILE__, attachpcurve, group);
47   theCommands.Add("edgestowire" , "edgestowire wire edges"     , __FILE__, edgestowire , group);
48   theCommands.Add("edgestofaces" , "edgestofaces faces edges [-a AngTol -s Shared(0/1)]", __FILE__, edgestofaces , group);
49 }
50
51 //=======================================================================
52 //function : BOPCommands
53 //purpose  : Attaches p-curve of the given edge to the given face.
54 //=======================================================================
55 static Standard_Integer attachpcurve(Draw_Interpretor& theDI,
56                                  Standard_Integer  theNArg,
57                                  const char ** theArgVal)
58 {
59   if (theNArg != 4)
60   {
61     theDI << "Use: " << theArgVal[0] << " eold enew face\n";
62     return 1;
63   }
64
65   TopoDS_Shape aShEOld(DBRep::Get(theArgVal[1]));
66   TopoDS_Shape aShENew(DBRep::Get(theArgVal[2]));
67   TopoDS_Shape aShFace(DBRep::Get(theArgVal[3]));
68
69   if (aShEOld.IsNull()) {
70     theDI << theArgVal[1] << " is null shape\n";
71     return 1;
72   } else if (aShEOld.ShapeType() != TopAbs_EDGE) {
73     theDI << theArgVal[1] << " is not an edge\n";
74     return 1;
75   }
76
77   if (aShENew.IsNull()) {
78     theDI << theArgVal[2] << " is null shape\n";
79     return 1;
80   } else if (aShENew.ShapeType() != TopAbs_EDGE) {
81     theDI << theArgVal[2] << " is not an edge\n";
82     return 1;
83   }
84
85   if (aShFace.IsNull()) {
86     theDI << theArgVal[3] << " is null shape\n";
87     return 1;
88   } else if (aShFace.ShapeType() != TopAbs_FACE) {
89     theDI << theArgVal[3] << " is not a face\n";
90     return 1;
91   }
92
93   TopoDS_Edge aEOld = TopoDS::Edge(aShEOld);
94   TopoDS_Edge aENew = TopoDS::Edge(aShENew);
95   TopoDS_Face aFace = TopoDS::Face(aShFace);
96
97   // Try to copy PCurve from old edge to the new one.
98   Handle(IntTools_Context) aCtx = new IntTools_Context;
99   const Standard_Integer   iRet =
100     BOPTools_AlgoTools2D::AttachExistingPCurve(aEOld, aENew, aFace, aCtx);
101
102   if (iRet) {
103     theDI << "Error! Code: " << iRet << "\n";
104   } else {
105     theDI << "PCurve is attached successfully\n";
106   }
107
108   return 0;
109 }
110
111 //=======================================================================
112 //function : edgestowire
113 //purpose  : Orients the edges to make wire
114 //=======================================================================
115 static Standard_Integer edgestowire(Draw_Interpretor& theDI,
116                                     Standard_Integer  theNArg,
117                                     const char ** theArgVal)
118 {
119   if (theNArg != 3) {
120     theDI << "Use: edgestowire wire edges\n";
121     return 1;
122   }
123   //
124   TopoDS_Shape anEdges = DBRep::Get(theArgVal[2]);
125   if (anEdges.IsNull()) {
126     theDI << "no edges\n";
127     return 1;
128   }
129   //
130   BOPTools_AlgoTools::OrientEdgesOnWire(anEdges);
131   DBRep::Set(theArgVal[1], anEdges);
132   return 0;
133 }
134
135 //=======================================================================
136 //function : edgestofaces
137 //purpose  : Creates planar faces from linear edges
138 //=======================================================================
139 static Standard_Integer edgestofaces(Draw_Interpretor& theDI,
140                                      Standard_Integer  theNArg,
141                                      const char ** theArgVal)
142 {
143   if (theNArg < 3) {
144     theDI << "Use: edgestofaces faces edges [-a AngTol -s Shared(0/1)]\n";
145     theDI << " AngTol - angular tolerance for comparing the planes;\n";
146     theDI << " Shared - boolean flag which defines whether the input\n";
147     theDI << "          edges are already shared or have to be intersected.\n";
148     return 1;
149   }
150   //
151   TopoDS_Shape anEdges = DBRep::Get(theArgVal[2]);
152   if (anEdges.IsNull()) {
153     theDI << "no edges\n";
154     return 1;
155   }
156   //
157   Standard_Real anAngTol = 1.e-8;
158   Standard_Boolean bShared = Standard_False;
159   //
160   for (Standard_Integer i = 3; i < theNArg; ++i) {
161     if (!strcmp(theArgVal[i], "-a") && (i+1 < theNArg)) {
162       anAngTol = Draw::Atof(theArgVal[i+1]);
163     }
164     if (!strcmp(theArgVal[i], "-s") && (i+1 < theNArg)) {
165       bShared = (Draw::Atoi(theArgVal[i+1]) == 1);
166     }
167   }
168   //
169   TopoDS_Shape aWires;
170   Standard_Integer iErr = BOPAlgo_Tools::EdgesToWires(anEdges, aWires, bShared, anAngTol);
171   if (iErr) {
172     theDI << "Unable to build wires from given edges\n";
173     return 0;
174   }
175   //
176   TopoDS_Shape aFaces;
177   Standard_Boolean bDone = BOPAlgo_Tools::WiresToFaces(aWires, aFaces, anAngTol);
178   if (!bDone) {
179     theDI << "Unable to build faces from wires\n";
180     return 0;
181   }
182   //
183   DBRep::Set(theArgVal[1], aFaces);
184   return 0;
185 }