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