Salome HOME
Update B-Spline GUI
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Plugin.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
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 #include <SketchPlugin_Plugin.h>
21 #include <SketchPlugin_Sketch.h>
22 #include <SketchPlugin_Line.h>
23 #include <SketchPlugin_Point.h>
24 #include <SketchPlugin_IntersectionPoint.h>
25 #include <SketchPlugin_Circle.h>
26 #include <SketchPlugin_Arc.h>
27 #include <SketchPlugin_BSpline.h>
28 #include <SketchPlugin_Projection.h>
29 #include <SketchPlugin_ConstraintAngle.h>
30 #include <SketchPlugin_ConstraintCoincidence.h>
31 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
32 #include <SketchPlugin_ConstraintCollinear.h>
33 #include <SketchPlugin_ConstraintDistance.h>
34 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
35 #include <SketchPlugin_ConstraintDistanceVertical.h>
36 #include <SketchPlugin_ConstraintEqual.h>
37 #include <SketchPlugin_Fillet.h>
38 #include <SketchPlugin_ConstraintHorizontal.h>
39 #include <SketchPlugin_ConstraintLength.h>
40 #include <SketchPlugin_ConstraintMiddle.h>
41 #include <SketchPlugin_ConstraintMirror.h>
42 #include <SketchPlugin_ConstraintParallel.h>
43 #include <SketchPlugin_ConstraintPerpendicular.h>
44 #include <SketchPlugin_ConstraintRadius.h>
45 #include <SketchPlugin_ConstraintRigid.h>
46 #include <SketchPlugin_ConstraintTangent.h>
47 #include <SketchPlugin_ConstraintVertical.h>
48 #include <SketchPlugin_MacroArc.h>
49 #include <SketchPlugin_MacroBSpline.h>
50 #include <SketchPlugin_MacroCircle.h>
51 #include <SketchPlugin_MultiRotation.h>
52 #include <SketchPlugin_MultiTranslation.h>
53 #include <SketchPlugin_Trim.h>
54 #include <SketchPlugin_Split.h>
55 #include <SketchPlugin_Validators.h>
56 #include <SketchPlugin_ExternalValidator.h>
57 #include <SketchPlugin_Ellipse.h>
58 #include <SketchPlugin_MacroEllipse.h>
59 #include <SketchPlugin_EllipticArc.h>
60 #include <SketchPlugin_MacroEllipticArc.h>
61 #include <SketchPlugin_SketchDrawer.h>
62
63 #include <SketcherPrs_Tools.h>
64
65 #include <Events_Loop.h>
66 #include <GeomDataAPI_Dir.h>
67
68 #include <ModelAPI_Session.h>
69 #include <ModelAPI_Document.h>
70 #include <ModelAPI_Validator.h>
71 #include <ModelAPI_Data.h>
72
73 #include <Config_PropManager.h>
74
75 #include <memory>
76 #include <sstream>
77
78 #ifdef _DEBUG
79 #include <iostream>
80 #endif
81
82 #define SKETCH_ENTITY_COLOR "225,0,0"
83 #define SKETCH_EXTERNAL_COLOR "170,0,225"
84 #define SKETCH_AUXILIARY_COLOR "0,85,0"
85 #define SKETCH_OVERCONSTRAINT_COLOR "255,0,0"
86 #define SKETCH_FULLY_CONSTRAINED_COLOR "0,150,0"
87
88 //#define SET_PLANES_COLOR_IN_PREFERENCES
89
90 // the only created instance of this plugin
91 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
92
93 SketchPlugin_Plugin::SketchPlugin_Plugin()
94 {
95   SessionPtr aMgr = ModelAPI_Session::get();
96   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
97   aFactory->registerValidator("SketchPlugin_DistanceAttr",
98                               new SketchPlugin_DistanceAttrValidator);
99   aFactory->registerValidator("SketchPlugin_ExternalValidator",
100                               new SketchPlugin_ExternalValidator);
101   aFactory->registerValidator("SketchPlugin_TangentAttr",
102                               new SketchPlugin_TangentAttrValidator);
103   aFactory->registerValidator("SketchPlugin_PerpendicularAttr",
104                               new SketchPlugin_PerpendicularAttrValidator);
105   aFactory->registerValidator("SketchPlugin_NotFixed",
106                               new SketchPlugin_NotFixedValidator);
107   aFactory->registerValidator("SketchPlugin_EqualAttr",
108                               new SketchPlugin_EqualAttrValidator);
109   aFactory->registerValidator("SketchPlugin_MirrorAttr",
110                               new SketchPlugin_MirrorAttrValidator);
111   aFactory->registerValidator("SketchPlugin_CoincidenceAttr",
112                               new SketchPlugin_CoincidenceAttrValidator);
113   aFactory->registerValidator("SketchPlugin_CopyValidator",
114                               new SketchPlugin_CopyValidator);
115   aFactory->registerValidator("SketchPlugin_SolverErrorValidator",
116                               new SketchPlugin_SolverErrorValidator);
117   aFactory->registerValidator("SketchPlugin_FilletVertexValidator",
118                               new SketchPlugin_FilletVertexValidator);
119   aFactory->registerValidator("SketchPlugin_SplitValidator",
120                               new SketchPlugin_SplitValidator);
121   aFactory->registerValidator("SketchPlugin_TrimValidator",
122                               new SketchPlugin_TrimValidator);
123   aFactory->registerValidator("SketchPlugin_MiddlePointAttr",
124                               new SketchPlugin_MiddlePointAttrValidator);
125   aFactory->registerValidator("SketchPlugin_ArcTangentPoint",
126                               new SketchPlugin_ArcTangentPointValidator);
127   aFactory->registerValidator("SketchPlugin_ArcTransversalPoint",
128                               new SketchPlugin_ArcTransversalPointValidator);
129   aFactory->registerValidator("SketchPlugin_IntersectionValidator",
130                               new SketchPlugin_IntersectionValidator);
131   aFactory->registerValidator("SketchPlugin_ProjectionValidator",
132                               new SketchPlugin_ProjectionValidator);
133   aFactory->registerValidator("SketchPlugin_DifferentReference",
134                               new SketchPlugin_DifferentReferenceValidator);
135   aFactory->registerValidator("SketchPlugin_DifferentPointReference",
136                               new SketchPlugin_DifferentPointReferenceValidator);
137   aFactory->registerValidator("SketchPlugin_CirclePassedPointValidator",
138                               new SketchPlugin_CirclePassedPointValidator);
139   aFactory->registerValidator("SketchPlugin_ThirdPointValidator",
140                               new SketchPlugin_ThirdPointValidator);
141   aFactory->registerValidator("SketchPlugin_ArcEndPointValidator",
142                               new SketchPlugin_ArcEndPointValidator);
143   aFactory->registerValidator("SketchPlugin_ArcEndPointIntersectionValidator",
144                               new SketchPlugin_ArcEndPointIntersectionValidator);
145   aFactory->registerValidator("SketchPlugin_HasNoConstraint", new SketchPlugin_HasNoConstraint);
146   aFactory->registerValidator("SketchPlugin_ReplicationReference",
147                               new SketchPlugin_ReplicationReferenceValidator);
148   aFactory->registerValidator("SketchPlugin_SketchFeatureValidator",
149                               new SketchPlugin_SketchFeatureValidator);
150   aFactory->registerValidator("SketchPlugin_MultiRotationAngleValidator",
151                               new SketchPlugin_MultiRotationAngleValidator);
152   aFactory->registerValidator("SketchPlugin_BSplineValidator",
153                               new SketchPlugin_BSplineValidator);
154
155   // register this plugin
156   ModelAPI_Session::get()->registerPlugin(this);
157
158   Config_PropManager::registerProp("Visualization", "sketch_entity_color",
159                                    "Sketch entity color",
160                                    Config_Prop::Color, SKETCH_ENTITY_COLOR);
161
162   Config_PropManager::registerProp("Visualization", "sketch_external_color",
163                                    "Sketch external entity color",
164                                    Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
165
166   Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color",
167                                    "Sketch auxiliary entity color",
168                                    Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
169
170   Config_PropManager::registerProp("Visualization", "sketch_overconstraint_color",
171                                    "Sketch overconstraint color",
172                                    Config_Prop::Color, SKETCH_OVERCONSTRAINT_COLOR);
173
174   Config_PropManager::registerProp("Visualization", "sketch_fully_constrained_color",
175                                    "Sketch fully constrained color",
176                                    Config_Prop::Color, SKETCH_FULLY_CONSTRAINED_COLOR);
177
178   Config_PropManager::registerProp("Visualization", "sketch_line_width",
179                                    "Sketch line width",
180                                    Config_Prop::IntSpin, "3");
181
182   // register sketcher properties
183 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
184   Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
185                                    Config_Prop::Color, YZ_PLANE_COLOR);
186   Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
187                                    Config_Prop::Color, XZ_PLANE_COLOR);
188   Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
189                                    Config_Prop::Color, XY_PLANE_COLOR);
190 #endif
191
192 }
193
194 FeaturePtr SketchPlugin_Plugin::createFeature(std::string theFeatureID)
195 {
196   if (theFeatureID == SketchPlugin_Sketch::ID()) {
197     return FeaturePtr(new SketchPlugin_Sketch);
198   } else if (theFeatureID == SketchPlugin_Point::ID()) {
199     return FeaturePtr(new SketchPlugin_Point);
200   } else if (theFeatureID == SketchPlugin_IntersectionPoint::ID()) {
201     return FeaturePtr(new SketchPlugin_IntersectionPoint);
202   } else if (theFeatureID == SketchPlugin_Line::ID()) {
203     return FeaturePtr(new SketchPlugin_Line);
204   } else if (theFeatureID == SketchPlugin_Circle::ID()) {
205     return FeaturePtr(new SketchPlugin_Circle);
206   } else if (theFeatureID == SketchPlugin_Arc::ID()) {
207     return FeaturePtr(new SketchPlugin_Arc);
208   } else if (theFeatureID == SketchPlugin_BSpline::ID()) {
209     return FeaturePtr(new SketchPlugin_BSpline);
210   } else if (theFeatureID == SketchPlugin_Projection::ID()) {
211     return FeaturePtr(new SketchPlugin_Projection);
212   } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
213     return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
214   } else if (theFeatureID == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
215     return FeaturePtr(new SketchPlugin_ConstraintCoincidenceInternal);
216   } else if (theFeatureID == SketchPlugin_ConstraintCollinear::ID()) {
217     return FeaturePtr(new SketchPlugin_ConstraintCollinear);
218   } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
219     return FeaturePtr(new SketchPlugin_ConstraintDistance);
220   } else if (theFeatureID == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
221     return FeaturePtr(new SketchPlugin_ConstraintDistanceHorizontal);
222   } else if (theFeatureID == SketchPlugin_ConstraintDistanceVertical::ID()) {
223     return FeaturePtr(new SketchPlugin_ConstraintDistanceVertical);
224   } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
225     return FeaturePtr(new SketchPlugin_ConstraintLength);
226   } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
227     return FeaturePtr(new SketchPlugin_ConstraintParallel);
228   } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
229     return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
230   } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
231     return FeaturePtr(new SketchPlugin_ConstraintRadius);
232   } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
233     return FeaturePtr(new SketchPlugin_ConstraintRigid);
234   } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
235     return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
236   } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
237     return FeaturePtr(new SketchPlugin_ConstraintVertical);
238   } else if (theFeatureID == SketchPlugin_ConstraintEqual::ID()) {
239     return FeaturePtr(new SketchPlugin_ConstraintEqual);
240   } else if (theFeatureID == SketchPlugin_ConstraintTangent::ID()) {
241     return FeaturePtr(new SketchPlugin_ConstraintTangent);
242   } else if (theFeatureID == SketchPlugin_ConstraintMiddle::ID()) {
243     return FeaturePtr(new SketchPlugin_ConstraintMiddle);
244   } else if (theFeatureID == SketchPlugin_ConstraintMirror::ID()) {
245     return FeaturePtr(new SketchPlugin_ConstraintMirror);
246   } else if (theFeatureID == SketchPlugin_Fillet::ID()) {
247     return FeaturePtr(new SketchPlugin_Fillet);
248   } else if (theFeatureID == SketchPlugin_MultiTranslation::ID()) {
249     return FeaturePtr(new SketchPlugin_MultiTranslation);
250   } else if (theFeatureID == SketchPlugin_MultiRotation::ID()) {
251     return FeaturePtr(new SketchPlugin_MultiRotation);
252   } else if (theFeatureID == SketchPlugin_ConstraintAngle::ID()) {
253     return FeaturePtr(new SketchPlugin_ConstraintAngle);
254   } else if (theFeatureID == SketchPlugin_Split::ID()) {
255     return FeaturePtr(new SketchPlugin_Split);
256   } else if (theFeatureID == SketchPlugin_Trim::ID()) {
257     return FeaturePtr(new SketchPlugin_Trim);
258   } else if (theFeatureID == SketchPlugin_MacroArc::ID()) {
259     return FeaturePtr(new SketchPlugin_MacroArc);
260   } else if (theFeatureID == SketchPlugin_MacroBSpline::ID()) {
261     return FeaturePtr(new SketchPlugin_MacroBSpline);
262   } else if (theFeatureID == SketchPlugin_MacroCircle::ID()) {
263     return FeaturePtr(new SketchPlugin_MacroCircle);
264   } else if (theFeatureID == SketchPlugin_Ellipse::ID()) {
265     return FeaturePtr(new SketchPlugin_Ellipse);
266   } else if (theFeatureID == SketchPlugin_MacroEllipse::ID()) {
267     return FeaturePtr(new SketchPlugin_MacroEllipse);
268   } else if (theFeatureID == SketchPlugin_EllipticArc::ID()) {
269     return FeaturePtr(new SketchPlugin_EllipticArc);
270   } else if (theFeatureID == SketchPlugin_MacroEllipticArc::ID()) {
271     return FeaturePtr(new SketchPlugin_MacroEllipticArc);
272   } else if (theFeatureID == SketchPlugin_SketchDrawer::ID()) {
273     return FeaturePtr(new SketchPlugin_SketchDrawer);
274   }
275   // feature of such kind is not found
276   return FeaturePtr();
277 }
278
279 // LCOV_EXCL_START
280 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
281 {
282   const Events_ID kRequestEvent =
283       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
284   if (theMessage->eventID() == kRequestEvent) {
285     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
286         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
287     Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
288   }
289 }
290
291 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
292 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
293 {
294   const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
295   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
296       new ModelAPI_FeatureStateMessage(kResponseEvent, this));
297
298   bool aHasSketchPlane = false;
299   std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
300       std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
301   if (aSketchFeature.get()) {
302     std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
303     if (aData) {
304       std::shared_ptr<GeomDataAPI_Dir> aNormal =
305         std::dynamic_pointer_cast<GeomDataAPI_Dir>(
306         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
307       // it is important to check whether the normal attribute is initialized
308       // because it is possible that normal values are filled when the plane is checked on validity
309       aHasSketchPlane = aNormal && aNormal->isInitialized() &&
310                         !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
311
312       aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
313       aMsg->setState(SketchPlugin_IntersectionPoint::ID(), aHasSketchPlane);
314       aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
315       aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
316       aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
317       aMsg->setState(SketchPlugin_BSpline::ID(), aHasSketchPlane);
318       aMsg->setState(SketchPlugin_Ellipse::ID(), aHasSketchPlane);
319       aMsg->setState(SketchPlugin_EllipticArc::ID(), aHasSketchPlane);
320       aMsg->setState(SketchPlugin_Projection::ID(), aHasSketchPlane);
321       aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
322       aMsg->setState(SketchPlugin_ConstraintCoincidenceInternal::ID(), aHasSketchPlane);
323       aMsg->setState(SketchPlugin_ConstraintCollinear::ID(), aHasSketchPlane);
324       aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
325       aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
326       aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
327       aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
328       aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
329       aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
330       aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
331       aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
332       aMsg->setState(SketchPlugin_ConstraintEqual::ID(), aHasSketchPlane);
333       aMsg->setState(SketchPlugin_ConstraintTangent::ID(), aHasSketchPlane);
334       aMsg->setState(SketchPlugin_ConstraintMiddle::ID(), aHasSketchPlane);
335       aMsg->setState(SketchPlugin_ConstraintMirror::ID(), aHasSketchPlane);
336       aMsg->setState(SketchPlugin_Fillet::ID(), aHasSketchPlane);
337       aMsg->setState(SketchPlugin_ConstraintAngle::ID(), aHasSketchPlane);
338       aMsg->setState(SketchPlugin_MultiRotation::ID(), aHasSketchPlane);
339       aMsg->setState(SketchPlugin_MultiTranslation::ID(), aHasSketchPlane);
340       aMsg->setState(SketchPlugin_Split::ID(), aHasSketchPlane);
341       aMsg->setState(SketchPlugin_Trim::ID(), aHasSketchPlane);
342       aMsg->setState(SketchPlugin_MacroArc::ID(), aHasSketchPlane);
343       aMsg->setState(SketchPlugin_MacroBSpline::ID(), aHasSketchPlane);
344       aMsg->setState(SketchPlugin_MacroCircle::ID(), aHasSketchPlane);
345       aMsg->setState(SketchPlugin_MacroEllipse::ID(), aHasSketchPlane);
346       aMsg->setState(SketchPlugin_MacroEllipticArc::ID(), aHasSketchPlane);
347       aMsg->setState(SketchPlugin_ConstraintDistanceHorizontal::ID(), aHasSketchPlane);
348       aMsg->setState(SketchPlugin_ConstraintDistanceVertical::ID(), aHasSketchPlane);
349       // SketchRectangle is a python feature, so its ID is passed just as a string
350       aMsg->setState("SketchRectangle", aHasSketchPlane);
351     }
352   }
353   return aMsg;
354 }
355 // LCOV_EXCL_STOP