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