Salome HOME
updated copyright message
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SketcherDriver.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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 #include <GEOMUtils.hxx>
30 #include <Sketcher_Profile.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 #include <Standard_ConstructionError.hxx>
42
43 //=======================================================================
44 //function : GetID
45 //purpose  :
46 //=======================================================================
47 const Standard_GUID& GEOMImpl_SketcherDriver::GetID()
48 {
49   static Standard_GUID aSketcherDriver("FF1BBB64-5D14-4df2-980B-3A668264EA16");
50   return aSketcherDriver;
51 }
52
53
54 //=======================================================================
55 //function : GEOMImpl_SketcherDriver
56 //purpose  :
57 //=======================================================================
58 GEOMImpl_SketcherDriver::GEOMImpl_SketcherDriver()
59 {
60 }
61
62 //=======================================================================
63 //function : Execute
64 //purpose  :
65 //=======================================================================
66 Standard_Integer GEOMImpl_SketcherDriver::Execute(Handle(TFunction_Logbook)& log) const
67 {
68   if (Label().IsNull()) return 0;
69   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
70
71   GEOMImpl_ISketcher aCI (aFunction);
72   //Standard_Integer aType = aFunction->GetType();
73
74   // retrieve the command
75   TCollection_AsciiString aCommand = aCI.GetCommand();
76   if (aCommand.IsEmpty())
77     return 0;
78
79   // Set "C" numeric locale to save numbers correctly
80   Kernel_Utils::Localizer loc;
81
82   // create sketcher
83   Sketcher_Profile aProfile( aCommand.ToCString() );
84   bool isDone = false;
85   TopoDS_Shape aShape = aProfile.GetShape( &isDone );
86
87   if ( !isDone ) {
88     Standard_ConstructionError::Raise("Sketcher creation failed");
89   }
90
91   if (aShape.IsNull())
92     return 0;
93
94   gp_Ax3 aWPlane;
95   if ( aFunction->GetType() == SKETCHER_NINE_DOUBLS )
96   {
97     gp_Pnt aOrigin =
98       gp_Pnt(aCI.GetWorkingPlane(1), aCI.GetWorkingPlane(2), aCI.GetWorkingPlane(3));
99     gp_Dir aDirZ =
100       gp_Dir(aCI.GetWorkingPlane(4), aCI.GetWorkingPlane(5), aCI.GetWorkingPlane(6));
101     gp_Dir aDirX =
102       gp_Dir(aCI.GetWorkingPlane(7), aCI.GetWorkingPlane(8), aCI.GetWorkingPlane(9));
103     aWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
104   }
105   else
106   {
107     Handle(GEOM_Function) aRefFace = aCI.GetWorkingPlane();
108     TopoDS_Shape aShape = aRefFace->GetValue();
109     //if ( aShape.IsNull() || aShape.ShapeType() != TopAbs_FACE )
110     //  return 0;
111     //Handle(Geom_Surface) aGS = BRep_Tool::Surface( TopoDS::Face( aShape ));
112     //if ( aGS.IsNull() || !aGS->IsKind( STANDARD_TYPE( Geom_Plane )))
113     //  return 0;
114     //Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast( aGS );
115     //aWPlane = aGPlane->Pln().Position();
116     aWPlane = GEOMUtils::GetPosition(aShape);
117   }
118   gp_Trsf aTrans;
119   aTrans.SetTransformation(aWPlane);
120   aTrans.Invert();
121   BRepBuilderAPI_Transform aTransformation (aShape, aTrans, Standard_False);
122   aShape = aTransformation.Shape();
123
124   if (aShape.IsNull())
125     return 0;
126
127   // set the function result
128   aFunction->SetValue(aShape);
129
130   log->SetTouched(Label());
131
132   return 1;
133 }
134
135 //================================================================================
136 /*!
137  * \brief Returns a name of creation operation and names and values of creation parameters
138  */
139 //================================================================================
140
141 bool GEOMImpl_SketcherDriver::
142 GetCreationInformation(std::string&             theOperationName,
143                        std::vector<GEOM_Param>& theParams)
144 {
145   if (Label().IsNull()) return 0;
146   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
147
148   GEOMImpl_ISketcher aCI( function );
149   Standard_Integer aType = function->GetType();
150
151   theOperationName = "SKETCH";
152
153   switch ( aType ) {
154   case SKETCHER_NINE_DOUBLS:
155     AddParam( theParams, "Command", aCI.GetCommand() );
156     AddParam( theParams, "Origin")
157       << aCI.GetWorkingPlane(1) << " "
158       << aCI.GetWorkingPlane(2) << " "
159       << aCI.GetWorkingPlane(3);
160     AddParam( theParams, "OZ")
161       << aCI.GetWorkingPlane(4) << " "
162       << aCI.GetWorkingPlane(5) << " "
163       << aCI.GetWorkingPlane(6);
164     AddParam( theParams, "OX")
165       << aCI.GetWorkingPlane(7) << " "
166       << aCI.GetWorkingPlane(8) << " "
167       << aCI.GetWorkingPlane(9);
168     break;
169   case SKETCHER_PLANE:
170     AddParam( theParams, "Command", aCI.GetCommand() );
171     AddParam( theParams, "Working plane", aCI.GetWorkingPlane(), "XOY" );
172     break;
173   default:
174     return false;
175   }
176
177   return true;
178 }
179
180 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_SketcherDriver,GEOM_BaseDriver)