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