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