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