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