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