1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Sketch.h
4 // Created: 27 Mar 2014
5 // Author: Mikhail PONIKAROV
7 #ifndef SketchPlugin_Sketch_H_
8 #define SketchPlugin_Sketch_H_
10 #include "SketchPlugin.h"
11 #include <SketchPlugin_Feature.h>
12 #include <GeomAPI_Pnt.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_IPresentable.h>
15 #include <GeomAPI_Ax3.h>
16 #include <GeomAPI_XYZ.h>
17 #include <GeomDataAPI_Point.h>
18 #include <GeomDataAPI_Dir.h>
21 #define YZ_PLANE_COLOR "#ff0000"
22 #define XZ_PLANE_COLOR "#00ff00"
23 #define XY_PLANE_COLOR "#0000ff"
25 /**\class SketchPlugin_Sketch
27 * \brief Feature for creation of the new part in PartSet.
29 class SketchPlugin_Sketch : public ModelAPI_CompositeFeature//, public GeomAPI_IPresentable
32 /// Sketch feature kind
33 inline static const std::string& ID()
35 static const std::string MY_SKETCH_ID("Sketch");
38 /// Origin point of the sketcher in 3D space
39 inline static const std::string& ORIGIN_ID()
41 static const std::string MY_ORIGIN_ID("Origin");
44 /// Vector X inside of the sketch plane
45 inline static const std::string& DIRX_ID()
47 static const std::string MY_DIRX_ID("DirX");
50 /// Vector Z, normal to the sketch plane
51 inline static const std::string& NORM_ID()
53 static const std::string MY_NORM_ID("Norm");
56 /// All features of this sketch (list of references)
57 inline static const std::string& FEATURES_ID()
59 static const std::string MY_FEATURES_ID("Features");
60 return MY_FEATURES_ID;
63 /// Returns the kind of a feature
64 SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
66 static std::string MY_KIND = SketchPlugin_Sketch::ID();
70 /// Creates a new part document if needed
71 SKETCHPLUGIN_EXPORT virtual void execute();
73 /// Request for initialization of data model of the feature: adding all attributes
74 SKETCHPLUGIN_EXPORT virtual void initAttributes();
77 /// \param theDeltaX the delta for X coordinate is moved
78 /// \param theDeltaY the delta for Y coordinate is moved
79 SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY)
83 /// Return the distance between the feature and the point
84 /// \param thePoint the point
85 virtual double distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
90 /// Converts a 2D sketch space point into point in 3D space
91 /// \param theX an X coordinate
92 /// \param theY an Y coordinate
93 std::shared_ptr<GeomAPI_Pnt> to3D(const double theX, const double theY) const
95 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
96 data()->attribute(ORIGIN_ID()));
97 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
98 data()->attribute(NORM_ID()));
99 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
100 data()->attribute(DIRX_ID()));
101 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
103 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
104 ->added(aY->xyz()->multiplied(theY));
106 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
109 /// Returns the point projected into the sketch plane
110 /// \param thePnt a source 3d point
111 std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt) const
113 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
114 data()->attribute(ORIGIN_ID()));
115 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
116 data()->attribute(NORM_ID()));
117 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
118 data()->attribute(DIRX_ID()));
119 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
120 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
123 /// Returns true if this feature must be displayed in the history (top level of Part tree)
124 SKETCHPLUGIN_EXPORT virtual bool isInHistory()
129 /// Use plugin manager for features creation
130 SketchPlugin_Sketch();
132 /// Returns the basis plane for the sketch
133 std::shared_ptr<GeomAPI_Pln> plane() const
135 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
136 data()->attribute(ORIGIN_ID()));
137 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
138 data()->attribute(NORM_ID()));
140 if (!anOrigin || !aNorm)
141 return std::shared_ptr<GeomAPI_Pln>();
143 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
146 /// Returns currently defined plane as an object of Ax3
147 std::shared_ptr<GeomAPI_Ax3> coordinatePlane() const
149 DataPtr aData = data();
150 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
151 aData->attribute(ORIGIN_ID()));
152 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
153 aData->attribute(DIRX_ID()));
154 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
155 aData->attribute(NORM_ID()));
157 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
160 /// Checks whether the plane is set in the sketch.
161 /// \returns the boolean state
162 bool isPlaneSet() const
164 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
165 data()->attribute(NORM_ID()));
167 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
171 /// removes also all sub-sketch elements
172 SKETCHPLUGIN_EXPORT virtual void erase();
174 /// appends a feature to the sketch sub-elements container
175 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
177 /// Just to synchronise the container of sub-features
178 virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature);
180 /// Returns the number of sub-elements
181 SKETCHPLUGIN_EXPORT virtual int numberOfSubs() const;
183 /// Returns the sub-feature by zero-base index
184 SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
185 subFeature(const int theIndex) const;
187 /// Returns the sub-feature unique identifier in this composite feature by zero-base index
188 SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
190 /// Returns true if feature or reuslt belong to this composite feature as subs
191 SKETCHPLUGIN_EXPORT virtual bool isSub(ObjectPtr theObject) const;
193 /// Construction result is allways recomuted on the fly
194 SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
196 SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);