1 // Created on: 2009-06-18
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2009-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BRepAlgo.hxx>
18 #include <BRepPrimAPI_MakeSphere.hxx>
19 #include <DNaming.hxx>
20 #include <DNaming_SphereDriver.hxx>
24 #include <ModelDefinitions.hxx>
25 #include <Standard_Type.hxx>
26 #include <TColStd_ListOfInteger.hxx>
27 #include <TDataStd_Integer.hxx>
28 #include <TDataStd_Real.hxx>
29 #include <TDataStd_UAttribute.hxx>
30 #include <TDF_Label.hxx>
31 #include <TFunction_Function.hxx>
32 #include <TFunction_Logbook.hxx>
33 #include <TNaming.hxx>
34 #include <TNaming_Builder.hxx>
35 #include <TNaming_NamedShape.hxx>
37 #include <TopLoc_Location.hxx>
39 #include <TopoDS_Iterator.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Solid.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
44 IMPLEMENT_STANDARD_RTTIEXT(DNaming_SphereDriver,TFunction_Driver)
46 //=======================================================================
47 //function : DNaming_SphereDriver
48 //purpose : Constructor
49 //=======================================================================
50 DNaming_SphereDriver::DNaming_SphereDriver()
53 //=======================================================================
55 //purpose : Validates labels of a function in <theLog>
56 //=======================================================================
57 void DNaming_SphereDriver::Validate(Handle(TFunction_Logbook)&) const
60 //=======================================================================
61 //function : MustExecute
62 //purpose : Analyses in <theLog> if the loaded function must be executed
63 //=======================================================================
64 Standard_Boolean DNaming_SphereDriver::MustExecute(const Handle(TFunction_Logbook)&) const {
68 //=======================================================================
70 //purpose : Executes the function
71 //=======================================================================
72 Standard_Integer DNaming_SphereDriver::Execute(Handle(TFunction_Logbook)& theLog) const {
73 Handle(TFunction_Function) aFunction;
74 Label().FindAttribute(TFunction_Function::GetID(),aFunction);
75 if(aFunction.IsNull()) return -1;
77 Standard_Real aRadius = DNaming::GetReal(aFunction,SPHERE_RADIUS)->Get();
78 Handle(TDataStd_UAttribute) anObject = DNaming::GetObjectArg(aFunction,SPHERE_CENTER);
79 Handle(TNaming_NamedShape) aNSCnt = DNaming::GetObjectValue(anObject);
80 if (aNSCnt.IsNull() || aNSCnt->IsEmpty()) {
82 std::cout<<"SphereDriver:: Center point is null or empty"<<std::endl;
84 aFunction->SetFailure(WRONG_ARGUMENT);
88 Handle(TNaming_NamedShape) aPrevSphere = DNaming::GetFunctionResult(aFunction);
91 TopLoc_Location aLocation;
92 if (!aPrevSphere.IsNull() && !aPrevSphere->IsEmpty()) {
93 aLocation = aPrevSphere->Get().Location();
96 TopoDS_Shape aCntShape = aNSCnt->Get();
97 if(aCntShape.IsNull()) {
99 std::cout<<"SphereDriver:: Center point is null"<<std::endl;
101 aFunction->SetFailure(WRONG_ARGUMENT);
104 gp_Pnt aCenter = gp_Pnt(0.,0.,0.);
105 if(aCntShape.ShapeType() == TopAbs_VERTEX) {
106 aCenter = BRep_Tool::Pnt(TopoDS::Vertex(aCntShape));
108 gp_Ax2 anAxis = gp_Ax2(aCenter, gp_Dir(0,0,1), gp_Dir(1,0,0));
109 BRepPrimAPI_MakeSphere aMakeSphere(anAxis, aRadius);
112 if (!aMakeSphere.IsDone()) {
113 aFunction->SetFailure(ALGO_FAILED);
117 TopoDS_Shape aResult = aMakeSphere.Solid();
118 if (!BRepAlgo::IsValid(aResult)) {
119 aFunction->SetFailure(RESULT_NOT_VALID);
124 LoadNamingDS(RESPOSITION(aFunction), aMakeSphere);
126 if(!aLocation.IsIdentity())
127 TNaming::Displace(RESPOSITION(aFunction), aLocation, Standard_True);
129 theLog->SetValid(RESPOSITION(aFunction), Standard_True);
130 aFunction->SetFailure(DONE);
134 //=======================================================================
135 //function : LoadAndName
137 //=======================================================================
138 void DNaming_SphereDriver::LoadNamingDS (const TDF_Label& theResultLabel,
139 BRepPrimAPI_MakeSphere& MS) const
142 Handle(TDF_TagSource) Tagger = TDF_TagSource::Set(theResultLabel);
143 if (Tagger.IsNull()) return;
146 TNaming_Builder Builder (theResultLabel);
147 Builder.Generated (MS.Solid());
149 BRepPrim_Sphere& S = MS.Sphere();
151 //Load faces of the Sph :
153 TopoDS_Face BottomFace = S.BottomFace ();
154 TNaming_Builder BOF (theResultLabel.NewChild());
155 BOF.Generated (BottomFace);
159 TopoDS_Face TopFace = S.TopFace ();
160 TNaming_Builder TOF (theResultLabel.NewChild());
161 TOF.Generated (TopFace);
164 TopoDS_Face LateralFace = S.LateralFace();
165 TNaming_Builder LOF (theResultLabel.NewChild());
166 LOF.Generated(LateralFace);
169 TopoDS_Face StartFace = S.StartFace();
170 TNaming_Builder SF(theResultLabel.NewChild());
171 SF.Generated(StartFace);
172 TopoDS_Face EndFace = S.EndFace();
173 TNaming_Builder EF(theResultLabel.NewChild());
174 EF.Generated(EndFace);
176 TopTools_IndexedMapOfShape LateralEdges;
177 TopExp::MapShapes(LateralFace, TopAbs_EDGE, LateralEdges);
178 Standard_Integer i = 1;
179 TColStd_ListOfInteger goodEdges;
180 for (; i <= LateralEdges.Extent(); i++)
181 if (!BRep_Tool::Degenerated(TopoDS::Edge(LateralEdges.FindKey(i)))) goodEdges.Append(i);
183 if (goodEdges.Extent() == 1) {
184 const TopoDS_Edge& aLateralEdge = TopoDS::Edge(LateralEdges.FindKey(goodEdges.First()));
185 TNaming_Builder MeridianBuilder(theResultLabel.NewChild());
186 MeridianBuilder.Generated(aLateralEdge);
187 TopoDS_Iterator it(aLateralEdge);
188 for(;it.More();it.Next()) {
189 //const TopoDS_Shape& aV = it.Value();
190 TNaming_Builder aVBuilder(theResultLabel.NewChild());
191 aVBuilder.Generated(it.Value());