]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Plugin.cpp
Salome HOME
Issue #2117: Problem when creating an arc by 3 points ending on the same circle
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Plugin.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include <SketchPlugin_Plugin.h>
4 #include <SketchPlugin_Sketch.h>
5 #include <SketchPlugin_Line.h>
6 #include <SketchPlugin_Point.h>
7 #include <SketchPlugin_IntersectionPoint.h>
8 #include <SketchPlugin_Circle.h>
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Projection.h>
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintCollinear.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15 #include <SketchPlugin_ConstraintEqual.h>
16 #include <SketchPlugin_Fillet.h>
17 #include <SketchPlugin_ConstraintSplit.h>
18 #include <SketchPlugin_ConstraintHorizontal.h>
19 #include <SketchPlugin_ConstraintLength.h>
20 #include <SketchPlugin_ConstraintMiddle.h>
21 #include <SketchPlugin_ConstraintMirror.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
24 #include <SketchPlugin_ConstraintRadius.h>
25 #include <SketchPlugin_ConstraintRigid.h>
26 #include <SketchPlugin_ConstraintTangent.h>
27 #include <SketchPlugin_ConstraintVertical.h>
28 #include <SketchPlugin_MacroArc.h>
29 #include <SketchPlugin_MacroCircle.h>
30 #include <SketchPlugin_MultiRotation.h>
31 #include <SketchPlugin_MultiTranslation.h>
32 #include <SketchPlugin_Trim.h>
33 #include <SketchPlugin_Validators.h>
34 #include <SketchPlugin_ExternalValidator.h>
35
36 #include <Events_Loop.h>
37 #include <GeomDataAPI_Dir.h>
38
39 #include <ModelAPI_Session.h>
40 #include <ModelAPI_Document.h>
41 #include <ModelAPI_Validator.h>
42 #include <ModelAPI_Data.h>
43
44 #include <Config_PropManager.h>
45
46 #include <memory>
47
48 #ifdef _DEBUG
49 #include <iostream>
50 #endif
51
52 #define SKETCH_ENTITY_COLOR "225,0,0"
53 #define SKETCH_EXTERNAL_COLOR "170,0,225"
54 #define SKETCH_AUXILIARY_COLOR "0,85,0"
55 #define SKETCH_OVERCONSTRAINT_COLOR "0,0,0"
56 #define SKETCH_FULLY_CONSTRAINED_COLOR "0,150,0"
57
58 //#define SET_PLANES_COLOR_IN_PREFERENCES
59
60 // the only created instance of this plugin
61 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
62
63 SketchPlugin_Plugin::SketchPlugin_Plugin()
64 {
65   SessionPtr aMgr = ModelAPI_Session::get();
66   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
67   aFactory->registerValidator("SketchPlugin_DistanceAttr",
68                               new SketchPlugin_DistanceAttrValidator);
69   aFactory->registerValidator("SketchPlugin_ExternalValidator",
70                               new SketchPlugin_ExternalValidator);
71   aFactory->registerValidator("SketchPlugin_TangentAttr",
72                               new SketchPlugin_TangentAttrValidator);
73   aFactory->registerValidator("SketchPlugin_NotFixed",
74                               new SketchPlugin_NotFixedValidator);
75   aFactory->registerValidator("SketchPlugin_EqualAttr",
76                               new SketchPlugin_EqualAttrValidator);
77   aFactory->registerValidator("SketchPlugin_MirrorAttr",
78                               new SketchPlugin_MirrorAttrValidator);
79   aFactory->registerValidator("SketchPlugin_CoincidenceAttr",
80                               new SketchPlugin_CoincidenceAttrValidator);
81   aFactory->registerValidator("SketchPlugin_CopyValidator",
82                               new SketchPlugin_CopyValidator);
83   aFactory->registerValidator("SketchPlugin_SolverErrorValidator",
84                               new SketchPlugin_SolverErrorValidator);
85   aFactory->registerValidator("SketchPlugin_FilletVertexValidator",
86                               new SketchPlugin_FilletVertexValidator);
87   aFactory->registerValidator("SketchPlugin_SplitValidator",
88                               new SketchPlugin_SplitValidator);
89   aFactory->registerValidator("SketchPlugin_TrimValidator",
90                               new SketchPlugin_TrimValidator);
91   aFactory->registerValidator("SketchPlugin_MiddlePointAttr",
92                               new SketchPlugin_MiddlePointAttrValidator);
93   aFactory->registerValidator("SketchPlugin_ArcTangentPoint",
94                               new SketchPlugin_ArcTangentPointValidator);
95   aFactory->registerValidator("SketchPlugin_IntersectionValidator",
96                               new SketchPlugin_IntersectionValidator);
97   aFactory->registerValidator("SketchPlugin_ProjectionValidator",
98                               new SketchPlugin_ProjectionValidator);
99   aFactory->registerValidator("SketchPlugin_DifferentReference",
100                               new SketchPlugin_DifferentReferenceValidator);
101   aFactory->registerValidator("SketchPlugin_DifferentPointReference",
102                               new SketchPlugin_DifferentPointReferenceValidator);
103   aFactory->registerValidator("SketchPlugin_CirclePassedPointValidator",
104                               new SketchPlugin_CirclePassedPointValidator);
105   aFactory->registerValidator("SketchPlugin_ThirdPointValidator",
106                               new SketchPlugin_ThirdPointValidator);
107   aFactory->registerValidator("SketchPlugin_ArcEndPointValidator",
108                               new SketchPlugin_ArcEndPointValidator);
109   aFactory->registerValidator("SketchPlugin_ArcEndPointIntersectionValidator",
110                               new SketchPlugin_ArcEndPointIntersectionValidator);
111
112   // register this plugin
113   ModelAPI_Session::get()->registerPlugin(this);
114
115   Config_PropManager::registerProp("Visualization", "sketch_entity_color",
116                                    "Sketch entity color",
117                                    Config_Prop::Color, SKETCH_ENTITY_COLOR);
118
119   Config_PropManager::registerProp("Visualization", "sketch_external_color",
120                                    "Sketch external entity color",
121                                    Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
122
123   Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color",
124                                    "Sketch auxiliary entity color",
125                                    Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
126
127   Config_PropManager::registerProp("Visualization", "sketch_dimension_color",
128                                    "Sketch dimension color",
129                                    Config_Prop::Color, SKETCH_DIMENSION_COLOR);
130
131   Config_PropManager::registerProp("Visualization", "sketch_overconstraint_color",
132                                    "Sketch overconstraint color",
133                                    Config_Prop::Color, SKETCH_OVERCONSTRAINT_COLOR);
134
135   Config_PropManager::registerProp("Visualization", "sketch_fully_constrained_color",
136                                    "Sketch fully constrained color",
137                                    Config_Prop::Color, SKETCH_FULLY_CONSTRAINED_COLOR);
138
139   // register sketcher properties
140 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
141   Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
142                                    Config_Prop::Color, YZ_PLANE_COLOR);
143   Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
144                                    Config_Prop::Color, XZ_PLANE_COLOR);
145   Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
146                                    Config_Prop::Color, XY_PLANE_COLOR);
147 #endif
148 }
149
150 FeaturePtr SketchPlugin_Plugin::createFeature(std::string theFeatureID)
151 {
152   if (theFeatureID == SketchPlugin_Sketch::ID()) {
153     return FeaturePtr(new SketchPlugin_Sketch);
154   } else if (theFeatureID == SketchPlugin_Point::ID()) {
155     return FeaturePtr(new SketchPlugin_Point);
156   } else if (theFeatureID == SketchPlugin_IntersectionPoint::ID()) {
157     return FeaturePtr(new SketchPlugin_IntersectionPoint);
158   } else if (theFeatureID == SketchPlugin_Line::ID()) {
159     return FeaturePtr(new SketchPlugin_Line);
160   } else if (theFeatureID == SketchPlugin_Circle::ID()) {
161     return FeaturePtr(new SketchPlugin_Circle);
162   } else if (theFeatureID == SketchPlugin_Arc::ID()) {
163     return FeaturePtr(new SketchPlugin_Arc);
164   } else if (theFeatureID == SketchPlugin_Projection::ID()) {
165     return FeaturePtr(new SketchPlugin_Projection);
166   } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
167     return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
168   } else if (theFeatureID == SketchPlugin_ConstraintCollinear::ID()) {
169     return FeaturePtr(new SketchPlugin_ConstraintCollinear);
170   } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
171     return FeaturePtr(new SketchPlugin_ConstraintDistance);
172   } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
173     return FeaturePtr(new SketchPlugin_ConstraintLength);
174   } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
175     return FeaturePtr(new SketchPlugin_ConstraintParallel);
176   } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
177     return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
178   } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
179     return FeaturePtr(new SketchPlugin_ConstraintRadius);
180   } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
181     return FeaturePtr(new SketchPlugin_ConstraintRigid);
182   } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
183     return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
184   } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
185     return FeaturePtr(new SketchPlugin_ConstraintVertical);
186   } else if (theFeatureID == SketchPlugin_ConstraintEqual::ID()) {
187     return FeaturePtr(new SketchPlugin_ConstraintEqual);
188   } else if (theFeatureID == SketchPlugin_ConstraintTangent::ID()) {
189     return FeaturePtr(new SketchPlugin_ConstraintTangent);
190   } else if (theFeatureID == SketchPlugin_ConstraintMiddle::ID()) {
191     return FeaturePtr(new SketchPlugin_ConstraintMiddle);
192   } else if (theFeatureID == SketchPlugin_ConstraintMirror::ID()) {
193     return FeaturePtr(new SketchPlugin_ConstraintMirror);
194   } else if (theFeatureID == SketchPlugin_Fillet::ID()) {
195     return FeaturePtr(new SketchPlugin_Fillet);
196   } else if (theFeatureID == SketchPlugin_ConstraintSplit::ID()) {
197     return FeaturePtr(new SketchPlugin_ConstraintSplit);
198   } else if (theFeatureID == SketchPlugin_MultiTranslation::ID()) {
199     return FeaturePtr(new SketchPlugin_MultiTranslation);
200   } else if (theFeatureID == SketchPlugin_MultiRotation::ID()) {
201     return FeaturePtr(new SketchPlugin_MultiRotation);
202   } else if (theFeatureID == SketchPlugin_ConstraintAngle::ID()) {
203     return FeaturePtr(new SketchPlugin_ConstraintAngle);
204   } else if (theFeatureID == SketchPlugin_Trim::ID()) {
205     return FeaturePtr(new SketchPlugin_Trim);
206   } else if (theFeatureID == SketchPlugin_MacroArc::ID()) {
207     return FeaturePtr(new SketchPlugin_MacroArc);
208   } else if (theFeatureID == SketchPlugin_MacroCircle::ID()) {
209     return FeaturePtr(new SketchPlugin_MacroCircle);
210   }
211   // feature of such kind is not found
212   return FeaturePtr();
213 }
214
215 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
216 {
217   const Events_ID kRequestEvent =
218       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
219   if (theMessage->eventID() == kRequestEvent) {
220     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
221         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
222     Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
223   }
224 }
225
226 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
227 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
228 {
229   const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
230   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
231       new ModelAPI_FeatureStateMessage(kResponseEvent, this));
232
233   bool aHasSketchPlane = false;
234   std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
235       std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
236   if (aSketchFeature.get()) {
237     std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
238     if (aData) {
239       std::shared_ptr<GeomDataAPI_Dir> aNormal =
240         std::dynamic_pointer_cast<GeomDataAPI_Dir>(
241         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
242       // it is important to check whether the normal attribute is initialized
243       // because it is possible that normal values are filled when the plane is checked on validity
244       aHasSketchPlane = aNormal && aNormal->isInitialized() &&
245                         !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
246
247       aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
248       aMsg->setState(SketchPlugin_IntersectionPoint::ID(), aHasSketchPlane);
249       aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
250       aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
251       aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
252       aMsg->setState(SketchPlugin_Projection::ID(), aHasSketchPlane);
253       aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
254       aMsg->setState(SketchPlugin_ConstraintCollinear::ID(), aHasSketchPlane);
255       aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
256       aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
257       aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
258       aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
259       aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
260       aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
261       aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
262       aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
263       aMsg->setState(SketchPlugin_ConstraintEqual::ID(), aHasSketchPlane);
264       aMsg->setState(SketchPlugin_ConstraintTangent::ID(), aHasSketchPlane);
265       aMsg->setState(SketchPlugin_ConstraintMiddle::ID(), aHasSketchPlane);
266       aMsg->setState(SketchPlugin_ConstraintMirror::ID(), aHasSketchPlane);
267       aMsg->setState(SketchPlugin_Fillet::ID(), aHasSketchPlane);
268       aMsg->setState(SketchPlugin_ConstraintSplit::ID(), aHasSketchPlane);
269       aMsg->setState(SketchPlugin_ConstraintAngle::ID(), aHasSketchPlane);
270       aMsg->setState(SketchPlugin_MultiRotation::ID(), aHasSketchPlane);
271       aMsg->setState(SketchPlugin_MultiTranslation::ID(), aHasSketchPlane);
272       aMsg->setState(SketchPlugin_Trim::ID(), aHasSketchPlane);
273       aMsg->setState(SketchPlugin_MacroArc::ID(), aHasSketchPlane);
274       aMsg->setState(SketchPlugin_MacroCircle::ID(), aHasSketchPlane);
275       // SketchRectangle is a python feature, so its ID is passed just as a string
276       aMsg->setState("SketchRectangle", aHasSketchPlane);
277     }
278   }
279   return aMsg;
280 }