SHAPER  9.15.0
SketchPlugin_Sketch.h
1 // Copyright (C) 2014-2025 CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 
20 #ifndef SketchPlugin_Sketch_H_
21 #define SketchPlugin_Sketch_H_
22 
23 #include "SketchPlugin.h"
24 #include <SketchPlugin_Feature.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Pln.h>
27 #include <GeomAPI_IPresentable.h>
28 #include <GeomAPI_ICustomPrs.h>
29 
30 #include <GeomAPI_Ax3.h>
31 #include <GeomAPI_XYZ.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Dir.h>
34 #include <list>
35 
36 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
37  #define YZ_PLANE_COLOR "225,0,0"
38  #define XZ_PLANE_COLOR "0,225,0"
39  #define XY_PLANE_COLOR "0,0,225"
40 #endif
41 
46 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature//, public GeomAPI_ICustomPrs
47 {
48  public:
50  inline static const std::string& ID()
51  {
52  static const std::string MY_SKETCH_ID("Sketch");
53  return MY_SKETCH_ID;
54  }
56  inline static const std::string& ORIGIN_ID()
57  {
58  static const std::string MY_ORIGIN_ID("Origin");
59  return MY_ORIGIN_ID;
60  }
62  inline static const std::string& DIRX_ID()
63  {
64  static const std::string MY_DIRX_ID("DirX");
65  return MY_DIRX_ID;
66  }
68  inline static const std::string& NORM_ID()
69  {
70  static const std::string MY_NORM_ID("Norm");
71  return MY_NORM_ID;
72  }
73 
75  inline static const std::string& DEFAULT_SIZE_ID()
76  {
77  static const std::string MY_DEFAULT_SIZE_ID_ID("DefaultSize");
78  return MY_DEFAULT_SIZE_ID_ID;
79  }
80 
81  inline static const std::string& AXES_ENABLED_ID()
82  {
83  static const std::string MY_AXES_ENABLED_ID("AxesEnabled");
84  return MY_AXES_ENABLED_ID;
85  }
86 
87  inline static const std::string& SUBSTRATE_ENABLED_ID()
88  {
89  static const std::string MY_SUBSTRATE_ENABLED_ID("SubstrateEnabled");
90  return MY_SUBSTRATE_ENABLED_ID;
91  }
92 
93  inline static const std::string& CONSTRUCTION_GRID_TYPE_ID()
94  {
95  static const std::string MY_CONSTRUCTION_GRID_TYPE_ID("GridType");
96  return MY_CONSTRUCTION_GRID_TYPE_ID;
97  }
98 
99  inline static const std::string& RECTANGULAR_CONSTRUCTION_GRID_STEP_X_ID()
100  {
101  static const std::string MY_RECTANGULAR_CONSTRUCTION_GRID_STEP_X_ID("RectangularGridStepX");
102  return MY_RECTANGULAR_CONSTRUCTION_GRID_STEP_X_ID;
103  }
104  inline static const std::string& RECTANGULAR_CONSTRUCTION_GRID_STEP_Y_ID()
105  {
106  static const std::string MY_RECTANGULAR_CONSTRUCTION_GRID_STEP_Y_ID("RectangularGridStepY");
107  return MY_RECTANGULAR_CONSTRUCTION_GRID_STEP_Y_ID;
108  }
109  inline static const std::string& RECTANGULAR_CONSTRUCTION_GRID_OFFSET_ANGLE_ID()
110  {
111  static const std::string MY_RECTANGULAR_CONSTRUCTION_GRID_OFFSET_ANGLE_ID("RectangularGridOffsetAngle");
112  return MY_RECTANGULAR_CONSTRUCTION_GRID_OFFSET_ANGLE_ID;
113  }
114  inline static const std::string& RECTANGULAR_CONSTRUCTION_GRID_OFFSET_X_ID()
115  {
116  static const std::string MY_RECTANGULAR_CONSTRUCTION_GRID_OFFSET_X_ID("RectangularGridOffsetX");
117  return MY_RECTANGULAR_CONSTRUCTION_GRID_OFFSET_X_ID;
118  }
119  inline static const std::string& RECTANGULAR_CONSTRUCTION_GRID_OFFSET_Y_ID()
120  {
121  static const std::string MY_RECTANGULAR_CONSTRUCTION_GRID_OFFSET_Y_ID("RectangularGridOffsetY");
122  return MY_RECTANGULAR_CONSTRUCTION_GRID_OFFSET_Y_ID;
123  }
124 
125  inline static const std::string& CIRCULAR_CONSTRUCTION_GRID_STEP_R_ID()
126  {
127  static const std::string MY_CIRCULAR_CONSTRUCTION_GRID_STEP_R_ID("CircularGridStepR");
128  return MY_CIRCULAR_CONSTRUCTION_GRID_STEP_R_ID;
129  }
130  inline static const std::string& CIRCULAR_CONSTRUCTION_GRID_NUM_OF_ANG_SEGMENTS_ID()
131  {
132  static const std::string MY_CIRCULAR_CONSTRUCTION_GRID_NUM_OF_ANG_SEGMENTS_ID("CircularGridNumOfAnularSegments");
133  return MY_CIRCULAR_CONSTRUCTION_GRID_NUM_OF_ANG_SEGMENTS_ID;
134  }
135  inline static const std::string& CIRCULAR_CONSTRUCTION_GRID_OFFSET_ANGLE_ID()
136  {
137  static const std::string MY_CIRCULAR_CONSTRUCTION_GRID_OFFSET_ANGLE_ID("CircularGridOffsetAngle");
138  return MY_CIRCULAR_CONSTRUCTION_GRID_OFFSET_ANGLE_ID;
139  }
140  inline static const std::string& CIRCULAR_CONSTRUCTION_GRID_OFFSET_X_ID()
141  {
142  static const std::string MY_CIRCULAR_CONSTRUCTION_GRID_OFFSET_X_ID("CircularGridOffsetX");
143  return MY_CIRCULAR_CONSTRUCTION_GRID_OFFSET_X_ID;
144  }
145  inline static const std::string& CIRCULAR_CONSTRUCTION_GRID_OFFSET_Y_ID()
146  {
147  static const std::string MY_CIRCULAR_CONSTRUCTION_GRID_OFFSET_Y_ID("CircularGridOffsetY");
148  return MY_CIRCULAR_CONSTRUCTION_GRID_OFFSET_Y_ID;
149  }
150 
152  inline static const std::string& FEATURES_ID()
153  {
154  static const std::string MY_FEATURES_ID("Features");
155  return MY_FEATURES_ID;
156  }
158  inline static const std::string& SOLVER_ERROR()
159  {
160  static const std::string MY_SOLVER_ERROR("SolverError");
161  return MY_SOLVER_ERROR;
162  }
163 
165  inline static const std::string& SOLVER_DOF()
166  {
167  static const std::string MY_SOLVER_DOF("SolverDOF");
168  return MY_SOLVER_DOF;
169  }
170 
172  inline static const std::string& ACTION_REMOVE_EXTERNAL()
173  {
174  static const std::string MY_ACTION_REMOVE_EXTERNAL("RemoveExternalLinks");
175  return MY_ACTION_REMOVE_EXTERNAL;
176  }
177 
179  SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
180  {
181  static std::string MY_KIND = SketchPlugin_Sketch::ID();
182  return MY_KIND;
183  }
184 
186  SKETCHPLUGIN_EXPORT virtual void execute();
187 
189  SKETCHPLUGIN_EXPORT virtual void initAttributes();
190 
194  std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
195  {
196  std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
197  data()->attribute(ORIGIN_ID()));
198  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
199  data()->attribute(NORM_ID()));
200  std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
201  data()->attribute(DIRX_ID()));
202  std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
203 
204  std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
205  ->added(aY->xyz()->multiplied(theY));
206 
207  return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
208  }
209 
212  std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
213  {
214  std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
215  data()->attribute(ORIGIN_ID()));
216  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
217  data()->attribute(NORM_ID()));
218  std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
219  data()->attribute(DIRX_ID()));
220  std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
221  return thePnt->to2D(aC->pnt(), aX->dir(), aY);
222  }
223 
225  SKETCHPLUGIN_EXPORT virtual bool isInHistory()
226  {
227  return true;
228  }
229 
232 
234  std::shared_ptr<GeomAPI_Pln> plane() const
235  {
236  std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
237  data()->attribute(ORIGIN_ID()));
238  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
239  data()->attribute(NORM_ID()));
240 
241  if (anOrigin.get() && aNorm.get() && anOrigin->isInitialized() && aNorm->isInitialized())
242  return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
243 
244  return std::shared_ptr<GeomAPI_Pln>();
245  }
246 
248  std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
249  {
250  DataPtr aData = data();
251  std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
252  aData->attribute(ORIGIN_ID()));
253  std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
254  aData->attribute(DIRX_ID()));
255  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
256  aData->attribute(NORM_ID()));
257 
258  if (!aNorm->isInitialized() || !aX->isInitialized() ||
259  aNorm->dir()->cross(aX->dir())->squareModulus() < 1.e-14)
260  return std::shared_ptr<GeomAPI_Ax3>();
261 
262  return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
263  }
264 
267  bool isPlaneSet() const
268  {
269  std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
270  data()->attribute(NORM_ID()));
271 
272  return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
273  }
274 
275 
277  SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
278 
280  virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
281 
283  SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const;
284 
286  SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
287  subFeature(const int theIndex, bool forTree = false);
288 
290  SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
291 
293  SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
294 
295  SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
296 
300  SKETCHPLUGIN_EXPORT virtual bool customAction(const std::string& theActionId);
301 
307  static void createPoint2DResult(ModelAPI_Feature* theFeature,
308  SketchPlugin_Sketch* theSketch,
309  const std::string& theAttributeID, const int theIndex);
310 
317  static void createLine2DResult(ModelAPI_Feature* theFeature,
318  SketchPlugin_Sketch* theSketch,
319  const std::string& theStartAttrID,
320  const std::string& theEndAttrID,
321  const int theIndex = 0);
322 
329  static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature,
330  SketchPlugin_Sketch* theSketch,
331  const bool theIsCopy = false);
332 
335  static std::shared_ptr<GeomAPI_Ax3> plane(SketchPlugin_Sketch* theSketch);
336 
338  //virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
339  // std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
340  //{
341  // bool isCustomized = false;
342  // // apply the color of the result to the presentation
343  // if (theDefaultPrs.get())
344  // isCustomized = theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
345  // // set the sketch presentation bold
346  // isCustomized = thePrs->setWidth(2) || isCustomized;
347 
348  // return isCustomized;
349  //}
350 
351 private:
354  bool removeLinksToExternal();
355 
357  void updateCoordinateAxis(ObjectPtr theSub, std::shared_ptr<GeomAPI_Ax3> thePlane);
358 
359 private:
360  std::shared_ptr<GeomAPI_Ax3> myPlane;
361 };
362 
363 #endif
The class represents a coordinate plane which is 2d plane with X and Y directions and origin.
Definition: GeomAPI_Ax3.h:33
3D direction defined by three normalized coordinates
Definition: GeomAPI_Dir.h:34
\Plane in 3D place, defined by normal, center and x-direction.
Definition: GeomAPI_Pln.h:38
3D point defined by three coordinates
Definition: GeomAPI_Pnt.h:38
Feature that consists of other features: like sketcher with edges inside.
Definition: ModelAPI_CompositeFeature.h:33
Feature function that represents the particular functionality of this operation.
Definition: ModelAPI_Feature.h:41
std::shared_ptr< ModelAPI_Attribute > attribute(const std::string &theID)
Returns the attribute by the identifier.
Definition: ModelAPI_Feature.h:271
virtual std::shared_ptr< ModelAPI_Data > data() const
Returns the data manager of this object: attributes.
Definition: ModelAPI_Object.cpp:45
Feature for creation of the new part in PartSet.
Definition: SketchPlugin_Sketch.h:47
static void createLine2DResult(ModelAPI_Feature *theFeature, SketchPlugin_Sketch *theSketch, const std::string &theStartAttrID, const std::string &theEndAttrID, const int theIndex=0)
Create a result for the segment given by a pair of attributes.
Definition: SketchPlugin_Sketch.cpp:513
static const std::string & DEFAULT_SIZE_ID()
Size of a skectch, if it has just been created on a default plane.
Definition: SketchPlugin_Sketch.h:75
static void createPoint2DResult(ModelAPI_Feature *theFeature, SketchPlugin_Sketch *theSketch, const std::string &theAttributeID, const int theIndex)
Create a result for the point in the attribute if the attribute is initialized.
Definition: SketchPlugin_Sketch.cpp:491
std::shared_ptr< GeomAPI_Pnt2d > to2D(const std::shared_ptr< GeomAPI_Pnt > &thePnt) const
Returns the point projected into the sketch plane.
Definition: SketchPlugin_Sketch.h:212
virtual std::shared_ptr< ModelAPI_Feature > addFeature(std::string theID)
appends a feature to the sketch sub-elements container
Definition: SketchPlugin_Sketch.cpp:271
virtual int subFeatureId(const int theIndex) const
Returns the sub-feature unique identifier in this composite feature by index.
Definition: SketchPlugin_Sketch.cpp:352
std::shared_ptr< GeomAPI_Ax3 > coordinatePlane() const
Returns currently defined plane as an object of Ax3.
Definition: SketchPlugin_Sketch.h:248
virtual int numberOfSubs(bool forTree=false) const
Returns the number of sub-elements.
Definition: SketchPlugin_Sketch.cpp:334
virtual bool isSub(ObjectPtr theObject) const
Returns true if feature or result belong to this composite feature as subs.
Definition: SketchPlugin_Sketch.cpp:370
virtual void execute()
Creates a new part document if needed.
Definition: SketchPlugin_Sketch.cpp:201
std::shared_ptr< GeomAPI_Pnt > to3D(const double theX, const double theY) const
Converts a 2D sketch space point into point in 3D space.
Definition: SketchPlugin_Sketch.h:194
static const std::string & ID()
Sketch feature kind.
Definition: SketchPlugin_Sketch.h:50
virtual void initAttributes()
Request for initialization of data model of the feature: adding all attributes.
Definition: SketchPlugin_Sketch.cpp:68
std::shared_ptr< GeomAPI_Pln > plane() const
Returns the basis plane for the sketch.
Definition: SketchPlugin_Sketch.h:234
bool isPlaneSet() const
Checks whether the plane is set in the sketch.
Definition: SketchPlugin_Sketch.h:267
virtual bool customAction(const std::string &theActionId)
Performs some custom feature specific functionality (normally called by some GUI button)
Definition: SketchPlugin_Sketch.cpp:585
virtual const std::string & getKind()
Returns the kind of a feature.
Definition: SketchPlugin_Sketch.h:179
virtual std::shared_ptr< ModelAPI_Feature > subFeature(const int theIndex, bool forTree=false)
Returns the sub-feature by zero-base index.
Definition: SketchPlugin_Sketch.cpp:341
static const std::string & ACTION_REMOVE_EXTERNAL()
Action ID to remove links to external entities while changing the sketch plane.
Definition: SketchPlugin_Sketch.h:172
static const std::string & SOLVER_DOF()
Sketch solver error.
Definition: SketchPlugin_Sketch.h:165
static FeaturePtr addUniqueNamedCopiedFeature(FeaturePtr theFeature, SketchPlugin_Sketch *theSketch, const bool theIsCopy=false)
Add new feature and fill the data of the feature by the data of the parameter feature.
Definition: SketchPlugin_Sketch.cpp:542
static const std::string & NORM_ID()
Vector Z, normal to the sketch plane.
Definition: SketchPlugin_Sketch.h:68
static const std::string & SOLVER_ERROR()
Sketch solver error.
Definition: SketchPlugin_Sketch.h:158
SketchPlugin_Sketch()
Use plugin manager for features creation.
Definition: SketchPlugin_Sketch.cpp:64
virtual void removeFeature(std::shared_ptr< ModelAPI_Feature > theFeature)
Just to synchronise the container of sub-features.
Definition: SketchPlugin_Sketch.cpp:318
static const std::string & DIRX_ID()
Vector X inside of the sketch plane.
Definition: SketchPlugin_Sketch.h:62
virtual bool isInHistory()
Returns true if this feature must be displayed in the history (top level of Part tree)
Definition: SketchPlugin_Sketch.h:225
static const std::string & ORIGIN_ID()
Origin point of the sketcher in 3D space.
Definition: SketchPlugin_Sketch.h:56
virtual void attributeChanged(const std::string &theID)
Called on change of any argument-attribute of this object.
Definition: SketchPlugin_Sketch.cpp:415
static const std::string & FEATURES_ID()
All features of this sketch (list of references)
Definition: SketchPlugin_Sketch.h:152