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