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