0024057: Eliminate compiler warning C4100 in MSVC++ with warning level 4
[occt.git] / src / DNaming / DNaming_SphereDriver.cxx
1 // Created on: 2009-06-18
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2009-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21
22 #include <DNaming_SphereDriver.ixx>
23
24 #include <gp_Ax2.hxx>
25 #include <gp_Dir.hxx>
26 #include <gp_Vec.hxx>
27 #include <gp_Pnt.hxx>
28 #include <BRep_Tool.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Iterator.hxx>
32 #include <TopoDS_Vertex.hxx>
33 #include <TopExp.hxx>
34
35 #include <BRepPrimAPI_MakeSphere.hxx>
36 #include <TopLoc_Location.hxx>
37 #include <BRepAlgo.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <TColStd_ListOfInteger.hxx>
40
41 #include <TDataStd_Integer.hxx>
42 #include <TDataStd_Real.hxx>
43 #include <TDataStd_UAttribute.hxx>
44 #include <TopoDS_Solid.hxx>
45 #include <TNaming.hxx>
46 #include <TNaming_NamedShape.hxx>
47 #include <TNaming_Builder.hxx>
48 #include <TFunction_Function.hxx>
49
50 #include <DNaming.hxx>
51 #include <ModelDefinitions.hxx>
52
53 //=======================================================================
54 //function : DNaming_SphereDriver
55 //purpose  : Constructor
56 //=======================================================================
57 DNaming_SphereDriver::DNaming_SphereDriver()
58 {}
59
60 //=======================================================================
61 //function : Validate
62 //purpose  : Validates labels of a function in <theLog>
63 //=======================================================================
64 void DNaming_SphereDriver::Validate(TFunction_Logbook&) const
65 {}
66
67 //=======================================================================
68 //function : MustExecute
69 //purpose  : Analyses in <theLog> if the loaded function must be executed
70 //=======================================================================
71 Standard_Boolean DNaming_SphereDriver::MustExecute(const TFunction_Logbook&) const {
72   return Standard_True;
73 }
74
75 //=======================================================================
76 //function : Execute
77 //purpose  : Executes the function 
78 //=======================================================================
79 Standard_Integer DNaming_SphereDriver::Execute(TFunction_Logbook& theLog) const {
80   Handle(TFunction_Function) aFunction;
81   Label().FindAttribute(TFunction_Function::GetID(),aFunction);
82   if(aFunction.IsNull()) return -1;
83
84   Standard_Real aRadius = DNaming::GetReal(aFunction,SPHERE_RADIUS)->Get();
85   Handle(TDataStd_UAttribute) anObject = DNaming::GetObjectArg(aFunction,SPHERE_CENTER);
86   Handle(TNaming_NamedShape) aNSCnt = DNaming::GetObjectValue(anObject);
87   if (aNSCnt.IsNull() || aNSCnt->IsEmpty()) {
88 #ifdef DEB
89     cout<<"SphereDriver:: Center point is null or empty"<<endl;
90 #endif
91     aFunction->SetFailure(WRONG_ARGUMENT);
92     return -1;
93   }
94
95   Handle(TNaming_NamedShape) aPrevSphere = DNaming::GetFunctionResult(aFunction);
96
97 // Save location
98   TopLoc_Location aLocation;
99   if (!aPrevSphere.IsNull() && !aPrevSphere->IsEmpty()) {
100     aLocation = aPrevSphere->Get().Location();
101   }
102   
103   TopoDS_Shape aCntShape = aNSCnt->Get();
104   if(aCntShape.IsNull()) {
105 #ifdef DEB
106     cout<<"SphereDriver:: Center point is null"<<endl;
107 #endif
108     aFunction->SetFailure(WRONG_ARGUMENT);
109     return -1;
110   }
111   gp_Pnt aCenter = gp_Pnt(0.,0.,0.);
112   if(aCntShape.ShapeType() == TopAbs_VERTEX) {
113     aCenter = BRep_Tool::Pnt(TopoDS::Vertex(aCntShape));
114   }
115   gp_Ax2 anAxis = gp_Ax2(aCenter, gp_Dir(0,0,1), gp_Dir(1,0,0));
116   BRepPrimAPI_MakeSphere aMakeSphere(anAxis, aRadius);  
117
118   aMakeSphere.Build();
119   if (!aMakeSphere.IsDone()) {
120     aFunction->SetFailure(ALGO_FAILED);
121     return -1;
122   }
123
124   TopoDS_Shape aResult = aMakeSphere.Solid();
125   if (!BRepAlgo::IsValid(aResult)) {
126     aFunction->SetFailure(RESULT_NOT_VALID);
127     return -1;
128   }
129
130   // Naming
131   LoadNamingDS(RESPOSITION(aFunction), aMakeSphere);
132   // restore location
133   if(!aLocation.IsIdentity()) 
134     TNaming::Displace(RESPOSITION(aFunction), aLocation, Standard_True);
135   
136   theLog.SetValid(RESPOSITION(aFunction), Standard_True); 
137   aFunction->SetFailure(DONE);
138   return 0;
139 }
140
141 //=======================================================================
142 //function : LoadAndName
143 //purpose  : 
144 //=======================================================================
145 void DNaming_SphereDriver::LoadNamingDS (const TDF_Label& theResultLabel, 
146                                            BRepPrimAPI_MakeSphere& MS) const 
147 {
148
149   Handle(TDF_TagSource) Tagger = TDF_TagSource::Set(theResultLabel);
150   if (Tagger.IsNull()) return;
151   Tagger->Set(0);
152
153   TNaming_Builder Builder (theResultLabel);
154   Builder.Generated (MS.Solid());
155
156   BRepPrim_Sphere& S = MS.Sphere();
157
158   //Load faces of the Sph :
159   if (S.HasBottom()) {
160     TopoDS_Face BottomFace = S.BottomFace ();
161     TNaming_Builder BOF (theResultLabel.NewChild()); 
162     BOF.Generated (BottomFace);
163   }
164
165   if (S.HasTop()) { 
166     TopoDS_Face TopFace = S.TopFace ();
167     TNaming_Builder TOF (theResultLabel.NewChild()); 
168     TOF.Generated (TopFace);
169   }
170
171   TopoDS_Face LateralFace = S.LateralFace();
172   TNaming_Builder LOF (theResultLabel.NewChild()); 
173   LOF.Generated(LateralFace); 
174
175   if (S.HasSides()) {
176     TopoDS_Face StartFace = S.StartFace();
177     TNaming_Builder SF(theResultLabel.NewChild()); 
178     SF.Generated(StartFace); 
179     TopoDS_Face EndFace = S.EndFace();
180     TNaming_Builder EF(theResultLabel.NewChild()); 
181     EF.Generated(EndFace); 
182   }
183   TopTools_IndexedMapOfShape LateralEdges;
184   TopExp::MapShapes(LateralFace, TopAbs_EDGE, LateralEdges);
185   Standard_Integer i = 1;
186   TColStd_ListOfInteger goodEdges;
187   for (; i <= LateralEdges.Extent(); i++) 
188     if (!BRep_Tool::Degenerated(TopoDS::Edge(LateralEdges.FindKey(i)))) goodEdges.Append(i);
189   
190   if (goodEdges.Extent() == 1) {
191     const TopoDS_Edge& aLateralEdge = TopoDS::Edge(LateralEdges.FindKey(goodEdges.First()));
192     TNaming_Builder MeridianBuilder(theResultLabel.NewChild());
193     MeridianBuilder.Generated(aLateralEdge);
194     TopoDS_Iterator it(aLateralEdge);
195     for(;it.More();it.Next()) {
196       //const TopoDS_Shape& aV = it.Value();
197       TNaming_Builder aVBuilder(theResultLabel.NewChild());
198       aVBuilder.Generated(it.Value());
199     }
200   }
201
202 }