0031336: Modeling data - extend BRepPrimAPI_MakeBox with planar shape creation
[occt.git] / src / BRepPreviewAPI / BRepPreviewAPI_MakeBox.cxx
1 // Created on: 2020-01-31
2 // Created by: Svetlana SHUTINA
3 // Copyright (c) 2020 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 <BRepPreviewAPI_MakeBox.hxx>
17
18 #include <BRep_Builder.hxx>
19 #include <BRepBuilderAPI_MakeEdge.hxx>
20 #include <BRepBuilderAPI_MakeFace.hxx>
21 #include <BRepBuilderAPI_MakeVertex.hxx>
22 #include <BRepBuilderAPI_MakeWire.hxx>
23
24 #include <TopoDS_Compound.hxx>
25
26 //=======================================================================
27 //function : Build
28 //purpose  :
29 //=======================================================================
30 void BRepPreviewAPI_MakeBox::Build()
31 {
32   gp_Pnt anLocation = myWedge.Axes().Location();
33
34   gp_Pnt aFirstPoint (anLocation.X(), anLocation.Y(), anLocation.Z());
35   gp_Pnt aSecondPoint (anLocation.X() + myWedge.GetXMax(), anLocation.Y() + myWedge.GetYMax(), anLocation.Z() + myWedge.GetZMax());
36
37   Standard_Boolean aThinOnX = Abs (aFirstPoint.X() - aSecondPoint.X()) < Precision::Confusion();
38   Standard_Boolean aThinOnY = Abs (aFirstPoint.Y() - aSecondPoint.Y()) < Precision::Confusion();
39   Standard_Boolean aThinOnZ = Abs (aFirstPoint.Z() - aSecondPoint.Z()) < Precision::Confusion();
40   
41   Standard_Integer aPreviewType = (int)aThinOnX + (int)aThinOnY + (int)aThinOnZ;
42
43   if (aPreviewType == 3) // thin box in all directions is a point
44   {
45     makeVertex (aFirstPoint);
46   }
47   else if (aPreviewType == 2) // thin box in two directions is a point
48   {
49     makeEdge (aFirstPoint, aSecondPoint);
50   }
51   // thin box in only one direction is a rectangular face
52   else if (aPreviewType == 1)
53   {
54     gp_Pnt aPnt1, aPnt2, aPnt3, aPnt4;
55     if (aThinOnX)
56     {
57       aPnt1 = gp_Pnt (aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z());
58       aPnt2 = gp_Pnt (aFirstPoint.X(), aSecondPoint.Y(), aFirstPoint.Z());
59       aPnt3 = gp_Pnt (aFirstPoint.X(), aSecondPoint.Y(), aSecondPoint.Z());
60       aPnt4 = gp_Pnt (aFirstPoint.X(), aFirstPoint.Y(), aSecondPoint.Z());
61     }
62     else if (aThinOnY)
63     {
64       aPnt1 = gp_Pnt (aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z());
65       aPnt2 = gp_Pnt (aSecondPoint.X(), aFirstPoint.Y(), aFirstPoint.Z());
66       aPnt3 = gp_Pnt (aSecondPoint.X(), aFirstPoint.Y(), aSecondPoint.Z());
67       aPnt4 = gp_Pnt (aFirstPoint.X(), aFirstPoint.Y(), aSecondPoint.Z());
68     }
69     else if (aThinOnZ)
70     {
71       aPnt1 = gp_Pnt (aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z());
72       aPnt2 = gp_Pnt (aSecondPoint.X(), aFirstPoint.Y(), aFirstPoint.Z());
73       aPnt3 = gp_Pnt (aSecondPoint.X(), aSecondPoint.Y(), aFirstPoint.Z());
74       aPnt4 = gp_Pnt (aFirstPoint.X(), aSecondPoint.Y(), aFirstPoint.Z());
75     }
76
77     makeRectangle (aPnt1, aPnt2, aPnt3, aPnt4);
78   }
79
80   if (!myShape.IsNull())
81   {
82     Done();
83     return;
84   }
85
86   // box is a valid shape
87   Solid();
88 }
89
90 //=======================================================================
91 //function : makeVertex
92 //purpose  :
93 //=======================================================================
94 void BRepPreviewAPI_MakeBox::makeVertex (const gp_Pnt& thePoint)
95 {
96   myShape = BRepBuilderAPI_MakeVertex (thePoint);
97 }
98
99 //=======================================================================
100 //function : makeEdge
101 //purpose  :
102 //=======================================================================
103 void BRepPreviewAPI_MakeBox::makeEdge (const gp_Pnt& thePoint1, const gp_Pnt& thePoint2)
104 {
105   myShape = BRepBuilderAPI_MakeEdge (thePoint1, thePoint2);
106 }
107
108 //=======================================================================
109 //function : makeRectangle
110 //purpose  :
111 //=======================================================================
112 void BRepPreviewAPI_MakeBox::makeRectangle (const gp_Pnt& thePnt1, const gp_Pnt& thePnt2,
113                                             const gp_Pnt& thePnt3, const gp_Pnt& thePnt4)
114 {
115   TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge (thePnt1, thePnt2);
116   TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge (thePnt2, thePnt3);
117   TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge (thePnt3, thePnt4);
118   TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge (thePnt4, thePnt1);
119
120   BRepBuilderAPI_MakeWire aWire (anEdge1, anEdge2, anEdge3, anEdge4);
121   BRepBuilderAPI_MakeFace aFace (aWire);
122
123   myShape = aFace.Shape();
124 }