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 <BRep_Tool.hxx>
18 #include <BRepAlgo.hxx>
19 #include <BRepPrimAPI_MakeSphere.hxx>
20 #include <DNaming.hxx>
21 #include <DNaming_SphereDriver.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>
39 #include <TopLoc_Location.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>
47 //=======================================================================
48 //function : DNaming_SphereDriver
49 //purpose : Constructor
50 //=======================================================================
51 DNaming_SphereDriver::DNaming_SphereDriver()
54 //=======================================================================
56 //purpose : Validates labels of a function in <theLog>
57 //=======================================================================
58 void DNaming_SphereDriver::Validate(Handle(TFunction_Logbook)&) const
61 //=======================================================================
62 //function : MustExecute
63 //purpose : Analyses in <theLog> if the loaded function must be executed
64 //=======================================================================
65 Standard_Boolean DNaming_SphereDriver::MustExecute(const Handle(TFunction_Logbook)&) const {
69 //=======================================================================
71 //purpose : Executes the function
72 //=======================================================================
73 Standard_Integer DNaming_SphereDriver::Execute(Handle(TFunction_Logbook)& theLog) const {
74 Handle(TFunction_Function) aFunction;
75 Label().FindAttribute(TFunction_Function::GetID(),aFunction);
76 if(aFunction.IsNull()) return -1;
78 Standard_Real aRadius = DNaming::GetReal(aFunction,SPHERE_RADIUS)->Get();
79 Handle(TDataStd_UAttribute) anObject = DNaming::GetObjectArg(aFunction,SPHERE_CENTER);
80 Handle(TNaming_NamedShape) aNSCnt = DNaming::GetObjectValue(anObject);
81 if (aNSCnt.IsNull() || aNSCnt->IsEmpty()) {
83 cout<<"SphereDriver:: Center point is null or empty"<<endl;
85 aFunction->SetFailure(WRONG_ARGUMENT);
89 Handle(TNaming_NamedShape) aPrevSphere = DNaming::GetFunctionResult(aFunction);
92 TopLoc_Location aLocation;
93 if (!aPrevSphere.IsNull() && !aPrevSphere->IsEmpty()) {
94 aLocation = aPrevSphere->Get().Location();
97 TopoDS_Shape aCntShape = aNSCnt->Get();
98 if(aCntShape.IsNull()) {
100 cout<<"SphereDriver:: Center point is null"<<endl;
102 aFunction->SetFailure(WRONG_ARGUMENT);
105 gp_Pnt aCenter = gp_Pnt(0.,0.,0.);
106 if(aCntShape.ShapeType() == TopAbs_VERTEX) {
107 aCenter = BRep_Tool::Pnt(TopoDS::Vertex(aCntShape));
109 gp_Ax2 anAxis = gp_Ax2(aCenter, gp_Dir(0,0,1), gp_Dir(1,0,0));
110 BRepPrimAPI_MakeSphere aMakeSphere(anAxis, aRadius);
113 if (!aMakeSphere.IsDone()) {
114 aFunction->SetFailure(ALGO_FAILED);
118 TopoDS_Shape aResult = aMakeSphere.Solid();
119 if (!BRepAlgo::IsValid(aResult)) {
120 aFunction->SetFailure(RESULT_NOT_VALID);
125 LoadNamingDS(RESPOSITION(aFunction), aMakeSphere);
127 if(!aLocation.IsIdentity())
128 TNaming::Displace(RESPOSITION(aFunction), aLocation, Standard_True);
130 theLog->SetValid(RESPOSITION(aFunction), Standard_True);
131 aFunction->SetFailure(DONE);
135 //=======================================================================
136 //function : LoadAndName
138 //=======================================================================
139 void DNaming_SphereDriver::LoadNamingDS (const TDF_Label& theResultLabel,
140 BRepPrimAPI_MakeSphere& MS) const
143 Handle(TDF_TagSource) Tagger = TDF_TagSource::Set(theResultLabel);
144 if (Tagger.IsNull()) return;
147 TNaming_Builder Builder (theResultLabel);
148 Builder.Generated (MS.Solid());
150 BRepPrim_Sphere& S = MS.Sphere();
152 //Load faces of the Sph :
154 TopoDS_Face BottomFace = S.BottomFace ();
155 TNaming_Builder BOF (theResultLabel.NewChild());
156 BOF.Generated (BottomFace);
160 TopoDS_Face TopFace = S.TopFace ();
161 TNaming_Builder TOF (theResultLabel.NewChild());
162 TOF.Generated (TopFace);
165 TopoDS_Face LateralFace = S.LateralFace();
166 TNaming_Builder LOF (theResultLabel.NewChild());
167 LOF.Generated(LateralFace);
170 TopoDS_Face StartFace = S.StartFace();
171 TNaming_Builder SF(theResultLabel.NewChild());
172 SF.Generated(StartFace);
173 TopoDS_Face EndFace = S.EndFace();
174 TNaming_Builder EF(theResultLabel.NewChild());
175 EF.Generated(EndFace);
177 TopTools_IndexedMapOfShape LateralEdges;
178 TopExp::MapShapes(LateralFace, TopAbs_EDGE, LateralEdges);
179 Standard_Integer i = 1;
180 TColStd_ListOfInteger goodEdges;
181 for (; i <= LateralEdges.Extent(); i++)
182 if (!BRep_Tool::Degenerated(TopoDS::Edge(LateralEdges.FindKey(i)))) goodEdges.Append(i);
184 if (goodEdges.Extent() == 1) {
185 const TopoDS_Edge& aLateralEdge = TopoDS::Edge(LateralEdges.FindKey(goodEdges.First()));
186 TNaming_Builder MeridianBuilder(theResultLabel.NewChild());
187 MeridianBuilder.Generated(aLateralEdge);
188 TopoDS_Iterator it(aLateralEdge);
189 for(;it.More();it.Next()) {
190 //const TopoDS_Shape& aV = it.Value();
191 TNaming_Builder aVBuilder(theResultLabel.NewChild());
192 aVBuilder.Generated(it.Value());