Salome HOME
0ebe757d0a026251cdd29118e43ea57edd7eb66d
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SketcherDriver.cxx
1 // Copyright (C) 2007-2012  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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_SketcherDriver.hxx>
25 #include <GEOMImpl_ISketcher.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <GEOMUtils.hxx>
30
31 #include <Basics_Utils.hxx>
32
33 // OCCT Includes
34 #include <BRepBuilderAPI_Transform.hxx>
35 #include <BRep_Tool.hxx>
36 #include <Geom_Plane.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <gp_Pln.hxx>
40
41 #include <Sketcher_Profile.hxx>
42
43 #include <Standard_ConstructionError.hxx>
44
45 //=======================================================================
46 //function : GetID
47 //purpose  :
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_SketcherDriver::GetID()
50 {
51   static Standard_GUID aSketcherDriver("FF1BBB64-5D14-4df2-980B-3A668264EA16");
52   return aSketcherDriver;
53 }
54
55
56 //=======================================================================
57 //function : GEOMImpl_SketcherDriver
58 //purpose  :
59 //=======================================================================
60 GEOMImpl_SketcherDriver::GEOMImpl_SketcherDriver()
61 {
62 }
63
64 //=======================================================================
65 //function : Execute
66 //purpose  :
67 //=======================================================================
68 Standard_Integer GEOMImpl_SketcherDriver::Execute(TFunction_Logbook& log) const
69 {
70   if (Label().IsNull()) return 0;
71   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
72
73   GEOMImpl_ISketcher aCI (aFunction);
74   //Standard_Integer aType = aFunction->GetType();
75
76   // retrieve the command
77   TCollection_AsciiString aCommand = aCI.GetCommand();
78   if (aCommand.IsEmpty())
79     return 0;
80
81   TopoDS_Shape aShape;
82
83   // Set "C" numeric locale to save numbers correctly
84   Kernel_Utils::Localizer loc;
85
86   // create sketcher
87   Sketcher_Profile aProfile (aCommand.ToCString());
88
89   if (!aProfile.IsDone()) {
90     Standard_ConstructionError::Raise("Sketcher creation failed");
91   }
92
93   aShape = aProfile.GetShape();
94   if (aShape.IsNull())
95     return 0;
96
97   gp_Ax3 aWPlane;
98   if ( aFunction->GetType() == SKETCHER_NINE_DOUBLS )
99   {
100     gp_Pnt aOrigin =
101       gp_Pnt(aCI.GetWorkingPlane(1), aCI.GetWorkingPlane(2), aCI.GetWorkingPlane(3));
102     gp_Dir aDirZ =
103       gp_Dir(aCI.GetWorkingPlane(4), aCI.GetWorkingPlane(5), aCI.GetWorkingPlane(6));
104     gp_Dir aDirX =
105       gp_Dir(aCI.GetWorkingPlane(7), aCI.GetWorkingPlane(8), aCI.GetWorkingPlane(9));
106     aWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
107   }
108   else
109   {
110     Handle(GEOM_Function) aRefFace = aCI.GetWorkingPlane();
111     TopoDS_Shape aShape = aRefFace->GetValue();
112     //if ( aShape.IsNull() || aShape.ShapeType() != TopAbs_FACE )
113     //  return 0;
114     //Handle(Geom_Surface) aGS = BRep_Tool::Surface( TopoDS::Face( aShape ));
115     //if ( aGS.IsNull() || !aGS->IsKind( STANDARD_TYPE( Geom_Plane )))
116     //  return 0;
117     //Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast( aGS );
118     //aWPlane = aGPlane->Pln().Position();
119     aWPlane = GEOMUtils::GetPosition(aShape);
120   }
121   gp_Trsf aTrans;
122   aTrans.SetTransformation(aWPlane);
123   aTrans.Invert();
124   BRepBuilderAPI_Transform aTransformation (aShape, aTrans, Standard_False);
125   aShape = aTransformation.Shape();
126
127   if (aShape.IsNull())
128     return 0;
129
130   // set the function result
131   aFunction->SetValue(aShape);
132
133   log.SetTouched(Label());
134
135   return 1;
136 }
137
138
139 //=======================================================================
140 //function :  GEOMImpl_SketcherDriver_Type_
141 //purpose  :
142 //=======================================================================
143 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SketcherDriver_Type_()
144 {
145
146   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
147   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
148   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
149   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
150   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
151   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
152
153
154   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
155   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_SketcherDriver",
156                                                          sizeof(GEOMImpl_SketcherDriver),
157                                                          1,
158                                                          (Standard_Address)_Ancestors,
159                                                          (Standard_Address)NULL);
160
161   return _aType;
162 }
163
164 //=======================================================================
165 //function : DownCast
166 //purpose  :
167 //=======================================================================
168 const Handle(GEOMImpl_SketcherDriver) Handle(GEOMImpl_SketcherDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
169 {
170   Handle(GEOMImpl_SketcherDriver) _anOtherObject;
171
172   if (!AnObject.IsNull()) {
173      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SketcherDriver))) {
174        _anOtherObject = Handle(GEOMImpl_SketcherDriver)((Handle(GEOMImpl_SketcherDriver)&)AnObject);
175      }
176   }
177
178   return _anOtherObject ;
179 }