0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / DNaming / DNaming_CylinderDriver.cxx
1 // Created on: 2009-05-04
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 #include <DNaming_CylinderDriver.ixx>
17 #include <TFunction_Function.hxx>
18 #include <TDataStd_Real.hxx>
19 #include <TDataStd_Integer.hxx>
20 #include <TFunction_Logbook.hxx>
21 #include <TNaming.hxx>
22 #include <TNaming_NamedShape.hxx>
23 #include <BRepPrimAPI_MakeCylinder.hxx>
24 #include <BRepCheck_Analyzer.hxx>
25 #include <TopLoc_Location.hxx>
26 #include <TopoDS_Solid.hxx>
27 #include <Standard_GUID.hxx>
28 #include <Standard_Real.hxx>
29 #include <TDF_Label.hxx>
30 #include <ModelDefinitions.hxx>
31
32 #include <DNaming.hxx>
33 #include <TNaming_Builder.hxx>
34 #include <TopExp.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <BRepAdaptor_Curve.hxx>
37 #include <TopoDS.hxx>
38 #include <gp_Lin.hxx>
39 #include <Geom_TrimmedCurve.hxx> 
40 #include <Geom_Line.hxx> 
41 //=======================================================================
42 //function : DNaming_CylinderDriver
43 //purpose  : Constructor
44 //=======================================================================
45 DNaming_CylinderDriver::DNaming_CylinderDriver()
46 {}
47
48 //=======================================================================
49 //function : Validate
50 //purpose  : Validates labels of a function in <log>.
51 //=======================================================================
52 void DNaming_CylinderDriver::Validate(TFunction_Logbook&) const
53 {}
54
55 //=======================================================================
56 //function : MustExecute
57 //purpose  : Analyse in <log> if the loaded function must be executed
58 //=======================================================================
59 Standard_Boolean DNaming_CylinderDriver::MustExecute(const TFunction_Logbook&) const
60 {
61   return Standard_True;
62 }
63
64 //=======================================================================
65 //function : Execute
66 //purpose  : Execute the function and push in <log> the impacted labels
67 //=======================================================================
68 Standard_Integer DNaming_CylinderDriver::Execute(TFunction_Logbook& theLog) const
69 {
70   Handle(TFunction_Function) aFunction;
71   Label().FindAttribute(TFunction_Function::GetID(),aFunction);
72   if(aFunction.IsNull()) return -1;
73
74
75
76   Standard_Real aRadius = DNaming::GetReal(aFunction,CYL_RADIUS)->Get();
77   Standard_Real aHeight = DNaming::GetReal(aFunction,CYL_HEIGHT)->Get();
78   Handle(TDataStd_UAttribute) anObject = DNaming::GetObjectArg(aFunction,CYL_AXIS);
79   Handle(TNaming_NamedShape) aNSAxis = DNaming::GetObjectValue(anObject);
80   if (aNSAxis->IsEmpty()) {
81 #ifdef DNAMING_DEB
82     cout<<"CylinderDriver:: Axis is empty"<<endl;
83 #endif
84     aFunction->SetFailure(WRONG_AXIS);
85     return -1;
86   }
87   TopoDS_Shape aTopoDSAxis = aNSAxis->Get();
88   if (aTopoDSAxis.IsNull()) {
89 #ifdef DNAMING_DEB
90     cout<<"CylinderDriver:: Axis is null"<<endl;
91 #endif
92     aFunction->SetFailure(WRONG_AXIS);
93     return -1;
94   }
95   // Creation of gp axis (gp_Ax2):
96   if (aTopoDSAxis.ShapeType() != TopAbs_EDGE && aTopoDSAxis.ShapeType() != TopAbs_WIRE) {
97 #ifdef DNAMING_DEB
98     cout<<"CylinderDriver:: Wrong axis, ShapeType = " << aTopoDSAxis.ShapeType() <<endl;
99 #endif    
100     aFunction->SetFailure(WRONG_AXIS);
101     return -1;
102   }
103
104   gp_Ax2 anAxis;
105   if (aTopoDSAxis.ShapeType() == TopAbs_WIRE) {
106     TopExp_Explorer anExplorer(aTopoDSAxis, TopAbs_EDGE);
107     aTopoDSAxis = anExplorer.Current();
108   }
109
110   BRepAdaptor_Curve aCurveAda(TopoDS::Edge(aTopoDSAxis));
111   if (aCurveAda.GetType() == GeomAbs_Line) {
112     gp_Lin aLin = aCurveAda.Line();
113     anAxis = gp_Ax2(aLin.Location(), aLin.Direction());
114     if(!aTopoDSAxis.Infinite()) {
115       TopoDS_Vertex V1, V2;
116       TopExp::Vertices(TopoDS::Edge(aTopoDSAxis), V1, V2);
117       gp_Pnt aP1 = BRep_Tool::Pnt(V1);
118       anAxis.SetLocation(aP1);
119     }
120   } else {
121 #ifdef DNAMING_DEB
122     cout<<"CylinderDriver:: I don't support wires for a while"<<endl;
123 #endif    
124     aFunction->SetFailure(WRONG_AXIS);
125     return -1;
126   }
127   
128   Handle(TNaming_NamedShape) aPrevCyl = DNaming::GetFunctionResult(aFunction);
129 // Save location
130   TopLoc_Location aLocation;
131   if (!aPrevCyl.IsNull() && !aPrevCyl->IsEmpty()) {
132     aLocation = aPrevCyl->Get().Location();
133   }
134
135   BRepPrimAPI_MakeCylinder aMakeCylinder(anAxis, aRadius, aHeight);  
136   aMakeCylinder.Build();
137   if (!aMakeCylinder.IsDone()) {
138     aFunction->SetFailure(ALGO_FAILED);
139     return -1;
140   }
141
142   TopoDS_Shape aResult = aMakeCylinder.Solid();
143   BRepCheck_Analyzer aCheck(aResult);
144   if (!aCheck.IsValid(aResult)) {
145     aFunction->SetFailure(RESULT_NOT_VALID);
146     return -1;
147   }
148
149   // Naming
150   LoadNamingDS(RESPOSITION(aFunction), aMakeCylinder);
151
152   // restore location
153   if(!aLocation.IsIdentity()) 
154     TNaming::Displace(RESPOSITION(aFunction), aLocation, Standard_True);
155
156
157   theLog.SetValid(RESPOSITION(aFunction), Standard_True);  
158   aFunction->SetFailure(DONE);
159   return 0;
160 }
161
162
163 //=======================================================================
164 //function : LoadAndName
165 //purpose  : 
166 //=======================================================================
167 void DNaming_CylinderDriver::LoadNamingDS (const TDF_Label& theResultLabel, 
168                                            BRepPrimAPI_MakeCylinder& MS) const 
169 {
170   TNaming_Builder Builder (theResultLabel);
171   Builder.Generated (MS.Solid());
172
173   BRepPrim_Cylinder& S = MS.Cylinder();
174
175   //Load faces of the Cyl :
176   if (S.HasBottom()) {
177     TopoDS_Face BottomFace = S.BottomFace ();
178     TNaming_Builder BOF (theResultLabel.FindChild(1,Standard_True)); 
179     BOF.Generated (BottomFace);
180   }
181
182   if (S.HasTop()) { 
183     TopoDS_Face TopFace = S.TopFace ();
184     TNaming_Builder TOF (theResultLabel.FindChild(2,Standard_True)); 
185     TOF.Generated (TopFace);
186   }
187
188   TopoDS_Face LateralFace = S.LateralFace();
189   TNaming_Builder LOF (theResultLabel.FindChild(3,Standard_True)); 
190   LOF.Generated(LateralFace); 
191
192   if (S.HasSides()) {
193     TopoDS_Face StartFace = S.StartFace();
194     TNaming_Builder SF(theResultLabel.FindChild(4,Standard_True)); 
195     SF.Generated(StartFace); 
196     TopoDS_Face EndFace = S.EndFace();
197     TNaming_Builder EF(theResultLabel.FindChild(5,Standard_True)); 
198     EF.Generated(EndFace); 
199   }
200
201 }