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