Salome HOME
PAL9166. Add possibility to make sketcher on an existing plane
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SketcherDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_SketcherDriver.hxx"
4 #include "GEOMImpl_ISketcher.hxx"
5 #include "GEOMImpl_Types.hxx"
6 #include "GEOM_Function.hxx"
7
8 #include <BRepBuilderAPI_Transform.hxx>
9 #include <BRep_Tool.hxx>
10 #include <Geom_Plane.hxx>
11 #include <TopoDS.hxx>
12 #include <TopoDS_Shape.hxx>
13 #include <gp_Pln.hxx>
14
15 #include <Sketcher_Profile.hxx>
16
17 #include <Standard_ConstructionError.hxx>
18
19 //=======================================================================
20 //function : GetID
21 //purpose  :
22 //=======================================================================
23 const Standard_GUID& GEOMImpl_SketcherDriver::GetID()
24 {
25   static Standard_GUID aSketcherDriver("FF1BBB64-5D14-4df2-980B-3A668264EA16");
26   return aSketcherDriver;
27 }
28
29
30 //=======================================================================
31 //function : GEOMImpl_SketcherDriver
32 //purpose  :
33 //=======================================================================
34 GEOMImpl_SketcherDriver::GEOMImpl_SketcherDriver()
35 {
36 }
37
38 //=======================================================================
39 //function : Execute
40 //purpose  :
41 //=======================================================================
42 Standard_Integer GEOMImpl_SketcherDriver::Execute(TFunction_Logbook& log) const
43 {
44   if (Label().IsNull()) return 0;
45   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
46
47   GEOMImpl_ISketcher aCI (aFunction);
48   //Standard_Integer aType = aFunction->GetType();
49
50   // retrieve the command
51   TCollection_AsciiString aCommand = aCI.GetCommand();
52   if (aCommand.IsEmpty())
53     return 0;
54
55   TopoDS_Shape aShape;
56
57   // create sketcher
58   Sketcher_Profile aProfile (aCommand.ToCString());
59
60   if (!aProfile.IsDone()) {
61     Standard_ConstructionError::Raise("Sketcher creation failed");
62   }
63
64   aShape = aProfile.GetShape();
65   if (aShape.IsNull())
66     return 0;
67
68   gp_Ax3 aWPlane;
69   if ( aFunction->GetType() == SKETCHER_NINE_DOUBLS )
70   {
71     gp_Pnt aOrigin =
72       gp_Pnt(aCI.GetWorkingPlane(1), aCI.GetWorkingPlane(2), aCI.GetWorkingPlane(3));
73     gp_Dir aDirZ =
74       gp_Dir(aCI.GetWorkingPlane(4), aCI.GetWorkingPlane(5), aCI.GetWorkingPlane(6));
75     gp_Dir aDirX =
76       gp_Dir(aCI.GetWorkingPlane(7), aCI.GetWorkingPlane(8), aCI.GetWorkingPlane(9));
77     aWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
78   }
79   else
80   {
81     Handle(GEOM_Function) aRefFace = aCI.GetWorkingPlane();
82     TopoDS_Shape aShape = aRefFace->GetValue();
83     if ( aShape.IsNull() || aShape.ShapeType() != TopAbs_FACE )
84       return 0;
85     Handle(Geom_Surface) aGS = BRep_Tool::Surface( TopoDS::Face( aShape ));
86     if ( aGS.IsNull() || !aGS->IsKind( STANDARD_TYPE( Geom_Plane )))
87       return 0;
88     Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast( aGS );
89     aWPlane = aGPlane->Pln().Position();
90   }
91   gp_Trsf aTrans;
92   aTrans.SetTransformation(aWPlane);
93   aTrans.Invert();
94   BRepBuilderAPI_Transform aTransformation (aShape, aTrans, Standard_False);
95   aShape = aTransformation.Shape();
96
97   if (aShape.IsNull())
98     return 0;
99
100   // set the function result
101   aFunction->SetValue(aShape);
102
103   log.SetTouched(Label());
104
105   return 1;
106 }
107
108
109 //=======================================================================
110 //function :  GEOMImpl_SketcherDriver_Type_
111 //purpose  :
112 //=======================================================================
113 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SketcherDriver_Type_()
114 {
115
116   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
117   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
118   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
119   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
120   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
121   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
122
123
124   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
125   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_SketcherDriver",
126                                                          sizeof(GEOMImpl_SketcherDriver),
127                                                          1,
128                                                          (Standard_Address)_Ancestors,
129                                                          (Standard_Address)NULL);
130
131   return _aType;
132 }
133
134 //=======================================================================
135 //function : DownCast
136 //purpose  :
137 //=======================================================================
138 const Handle(GEOMImpl_SketcherDriver) Handle(GEOMImpl_SketcherDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
139 {
140   Handle(GEOMImpl_SketcherDriver) _anOtherObject;
141
142   if (!AnObject.IsNull()) {
143      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SketcherDriver))) {
144        _anOtherObject = Handle(GEOMImpl_SketcherDriver)((Handle(GEOMImpl_SketcherDriver)&)AnObject);
145      }
146   }
147
148   return _anOtherObject ;
149 }