0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / DNaming / DNaming_SphereDriver.cxx
1 // Created on: 2009-06-18
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2009-2014 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
17 #include <BRep_Tool.hxx>
18 #include <BRepAlgo.hxx>
19 #include <BRepPrimAPI_MakeSphere.hxx>
20 #include <DNaming.hxx>
21 #include <DNaming_SphereDriver.hxx>
22 #include <gp_Ax2.hxx>
23 #include <gp_Dir.hxx>
24 #include <gp_Pnt.hxx>
25 #include <gp_Vec.hxx>
26 #include <ModelDefinitions.hxx>
27 #include <Standard_Type.hxx>
28 #include <TColStd_ListOfInteger.hxx>
29 #include <TDataStd_Integer.hxx>
30 #include <TDataStd_Real.hxx>
31 #include <TDataStd_UAttribute.hxx>
32 #include <TDF_Label.hxx>
33 #include <TFunction_Function.hxx>
34 #include <TFunction_Logbook.hxx>
35 #include <TNaming.hxx>
36 #include <TNaming_Builder.hxx>
37 #include <TNaming_NamedShape.hxx>
38 #include <TopExp.hxx>
39 #include <TopLoc_Location.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Iterator.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Solid.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46
47 IMPLEMENT_STANDARD_RTTIEXT(DNaming_SphereDriver,TFunction_Driver)
48
49 //=======================================================================
50 //function : DNaming_SphereDriver
51 //purpose  : Constructor
52 //=======================================================================
53 DNaming_SphereDriver::DNaming_SphereDriver()
54 {}
55
56 //=======================================================================
57 //function : Validate
58 //purpose  : Validates labels of a function in <theLog>
59 //=======================================================================
60 void DNaming_SphereDriver::Validate(Handle(TFunction_Logbook)&) const
61 {}
62
63 //=======================================================================
64 //function : MustExecute
65 //purpose  : Analyses in <theLog> if the loaded function must be executed
66 //=======================================================================
67 Standard_Boolean DNaming_SphereDriver::MustExecute(const Handle(TFunction_Logbook)&) const {
68   return Standard_True;
69 }
70
71 //=======================================================================
72 //function : Execute
73 //purpose  : Executes the function 
74 //=======================================================================
75 Standard_Integer DNaming_SphereDriver::Execute(Handle(TFunction_Logbook)& theLog) const {
76   Handle(TFunction_Function) aFunction;
77   Label().FindAttribute(TFunction_Function::GetID(),aFunction);
78   if(aFunction.IsNull()) return -1;
79
80   Standard_Real aRadius = DNaming::GetReal(aFunction,SPHERE_RADIUS)->Get();
81   Handle(TDataStd_UAttribute) anObject = DNaming::GetObjectArg(aFunction,SPHERE_CENTER);
82   Handle(TNaming_NamedShape) aNSCnt = DNaming::GetObjectValue(anObject);
83   if (aNSCnt.IsNull() || aNSCnt->IsEmpty()) {
84 #ifdef OCCT_DEBUG
85     cout<<"SphereDriver:: Center point is null or empty"<<endl;
86 #endif
87     aFunction->SetFailure(WRONG_ARGUMENT);
88     return -1;
89   }
90
91   Handle(TNaming_NamedShape) aPrevSphere = DNaming::GetFunctionResult(aFunction);
92
93 // Save location
94   TopLoc_Location aLocation;
95   if (!aPrevSphere.IsNull() && !aPrevSphere->IsEmpty()) {
96     aLocation = aPrevSphere->Get().Location();
97   }
98   
99   TopoDS_Shape aCntShape = aNSCnt->Get();
100   if(aCntShape.IsNull()) {
101 #ifdef OCCT_DEBUG
102     cout<<"SphereDriver:: Center point is null"<<endl;
103 #endif
104     aFunction->SetFailure(WRONG_ARGUMENT);
105     return -1;
106   }
107   gp_Pnt aCenter = gp_Pnt(0.,0.,0.);
108   if(aCntShape.ShapeType() == TopAbs_VERTEX) {
109     aCenter = BRep_Tool::Pnt(TopoDS::Vertex(aCntShape));
110   }
111   gp_Ax2 anAxis = gp_Ax2(aCenter, gp_Dir(0,0,1), gp_Dir(1,0,0));
112   BRepPrimAPI_MakeSphere aMakeSphere(anAxis, aRadius);  
113
114   aMakeSphere.Build();
115   if (!aMakeSphere.IsDone()) {
116     aFunction->SetFailure(ALGO_FAILED);
117     return -1;
118   }
119
120   TopoDS_Shape aResult = aMakeSphere.Solid();
121   if (!BRepAlgo::IsValid(aResult)) {
122     aFunction->SetFailure(RESULT_NOT_VALID);
123     return -1;
124   }
125
126   // Naming
127   LoadNamingDS(RESPOSITION(aFunction), aMakeSphere);
128   // restore location
129   if(!aLocation.IsIdentity()) 
130     TNaming::Displace(RESPOSITION(aFunction), aLocation, Standard_True);
131   
132   theLog->SetValid(RESPOSITION(aFunction), Standard_True); 
133   aFunction->SetFailure(DONE);
134   return 0;
135 }
136
137 //=======================================================================
138 //function : LoadAndName
139 //purpose  : 
140 //=======================================================================
141 void DNaming_SphereDriver::LoadNamingDS (const TDF_Label& theResultLabel, 
142                                            BRepPrimAPI_MakeSphere& MS) const 
143 {
144
145   Handle(TDF_TagSource) Tagger = TDF_TagSource::Set(theResultLabel);
146   if (Tagger.IsNull()) return;
147   Tagger->Set(0);
148
149   TNaming_Builder Builder (theResultLabel);
150   Builder.Generated (MS.Solid());
151
152   BRepPrim_Sphere& S = MS.Sphere();
153
154   //Load faces of the Sph :
155   if (S.HasBottom()) {
156     TopoDS_Face BottomFace = S.BottomFace ();
157     TNaming_Builder BOF (theResultLabel.NewChild()); 
158     BOF.Generated (BottomFace);
159   }
160
161   if (S.HasTop()) { 
162     TopoDS_Face TopFace = S.TopFace ();
163     TNaming_Builder TOF (theResultLabel.NewChild()); 
164     TOF.Generated (TopFace);
165   }
166
167   TopoDS_Face LateralFace = S.LateralFace();
168   TNaming_Builder LOF (theResultLabel.NewChild()); 
169   LOF.Generated(LateralFace); 
170
171   if (S.HasSides()) {
172     TopoDS_Face StartFace = S.StartFace();
173     TNaming_Builder SF(theResultLabel.NewChild()); 
174     SF.Generated(StartFace); 
175     TopoDS_Face EndFace = S.EndFace();
176     TNaming_Builder EF(theResultLabel.NewChild()); 
177     EF.Generated(EndFace); 
178   }
179   TopTools_IndexedMapOfShape LateralEdges;
180   TopExp::MapShapes(LateralFace, TopAbs_EDGE, LateralEdges);
181   Standard_Integer i = 1;
182   TColStd_ListOfInteger goodEdges;
183   for (; i <= LateralEdges.Extent(); i++) 
184     if (!BRep_Tool::Degenerated(TopoDS::Edge(LateralEdges.FindKey(i)))) goodEdges.Append(i);
185   
186   if (goodEdges.Extent() == 1) {
187     const TopoDS_Edge& aLateralEdge = TopoDS::Edge(LateralEdges.FindKey(goodEdges.First()));
188     TNaming_Builder MeridianBuilder(theResultLabel.NewChild());
189     MeridianBuilder.Generated(aLateralEdge);
190     TopoDS_Iterator it(aLateralEdge);
191     for(;it.More();it.Next()) {
192       //const TopoDS_Shape& aV = it.Value();
193       TNaming_Builder aVBuilder(theResultLabel.NewChild());
194       aVBuilder.Generated(it.Value());
195     }
196   }
197
198 }