Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SketcherDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_SketcherDriver.hxx>
24 #include <GEOMImpl_ISketcher.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepBuilderAPI_Transform.hxx>
29 #include <BRep_Tool.hxx>
30 #include <Geom_Plane.hxx>
31 #include <TopoDS.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <gp_Pln.hxx>
34
35 #include <Sketcher_Profile.hxx>
36
37 #include <Standard_ConstructionError.hxx>
38
39 //=======================================================================
40 //function : GetID
41 //purpose  :
42 //=======================================================================
43 const Standard_GUID& GEOMImpl_SketcherDriver::GetID()
44 {
45   static Standard_GUID aSketcherDriver("FF1BBB64-5D14-4df2-980B-3A668264EA16");
46   return aSketcherDriver;
47 }
48
49
50 //=======================================================================
51 //function : GEOMImpl_SketcherDriver
52 //purpose  :
53 //=======================================================================
54 GEOMImpl_SketcherDriver::GEOMImpl_SketcherDriver()
55 {
56 }
57
58 //=======================================================================
59 //function : Execute
60 //purpose  :
61 //=======================================================================
62 Standard_Integer GEOMImpl_SketcherDriver::Execute(TFunction_Logbook& log) const
63 {
64   if (Label().IsNull()) return 0;
65   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
66
67   GEOMImpl_ISketcher aCI (aFunction);
68   //Standard_Integer aType = aFunction->GetType();
69
70   // retrieve the command
71   TCollection_AsciiString aCommand = aCI.GetCommand();
72   if (aCommand.IsEmpty())
73     return 0;
74
75   TopoDS_Shape aShape;
76
77   // create sketcher
78   Sketcher_Profile aProfile (aCommand.ToCString());
79
80   if (!aProfile.IsDone()) {
81     Standard_ConstructionError::Raise("Sketcher creation failed");
82   }
83
84   aShape = aProfile.GetShape();
85   if (aShape.IsNull())
86     return 0;
87
88   gp_Ax3 aWPlane;
89   if ( aFunction->GetType() == SKETCHER_NINE_DOUBLS )
90   {
91     gp_Pnt aOrigin =
92       gp_Pnt(aCI.GetWorkingPlane(1), aCI.GetWorkingPlane(2), aCI.GetWorkingPlane(3));
93     gp_Dir aDirZ =
94       gp_Dir(aCI.GetWorkingPlane(4), aCI.GetWorkingPlane(5), aCI.GetWorkingPlane(6));
95     gp_Dir aDirX =
96       gp_Dir(aCI.GetWorkingPlane(7), aCI.GetWorkingPlane(8), aCI.GetWorkingPlane(9));
97     aWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
98   }
99   else
100   {
101     Handle(GEOM_Function) aRefFace = aCI.GetWorkingPlane();
102     TopoDS_Shape aShape = aRefFace->GetValue();
103     if ( aShape.IsNull() || aShape.ShapeType() != TopAbs_FACE )
104       return 0;
105     Handle(Geom_Surface) aGS = BRep_Tool::Surface( TopoDS::Face( aShape ));
106     if ( aGS.IsNull() || !aGS->IsKind( STANDARD_TYPE( Geom_Plane )))
107       return 0;
108     Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast( aGS );
109     aWPlane = aGPlane->Pln().Position();
110   }
111   gp_Trsf aTrans;
112   aTrans.SetTransformation(aWPlane);
113   aTrans.Invert();
114   BRepBuilderAPI_Transform aTransformation (aShape, aTrans, Standard_False);
115   aShape = aTransformation.Shape();
116
117   if (aShape.IsNull())
118     return 0;
119
120   // set the function result
121   aFunction->SetValue(aShape);
122
123   log.SetTouched(Label());
124
125   return 1;
126 }
127
128
129 //=======================================================================
130 //function :  GEOMImpl_SketcherDriver_Type_
131 //purpose  :
132 //=======================================================================
133 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SketcherDriver_Type_()
134 {
135
136   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
137   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
138   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
139   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
140   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
141   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
142
143
144   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
145   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_SketcherDriver",
146                                                          sizeof(GEOMImpl_SketcherDriver),
147                                                          1,
148                                                          (Standard_Address)_Ancestors,
149                                                          (Standard_Address)NULL);
150
151   return _aType;
152 }
153
154 //=======================================================================
155 //function : DownCast
156 //purpose  :
157 //=======================================================================
158 const Handle(GEOMImpl_SketcherDriver) Handle(GEOMImpl_SketcherDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
159 {
160   Handle(GEOMImpl_SketcherDriver) _anOtherObject;
161
162   if (!AnObject.IsNull()) {
163      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SketcherDriver))) {
164        _anOtherObject = Handle(GEOMImpl_SketcherDriver)((Handle(GEOMImpl_SketcherDriver)&)AnObject);
165      }
166   }
167
168   return _anOtherObject ;
169 }