Salome HOME
635583b0c14011a42a5f6d631431eb8442655f2f
[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_ConstraintFillet.h>
17 #include <SketchPlugin_ConstraintHorizontal.h>
18 #include <SketchPlugin_ConstraintLength.h>
19 #include <SketchPlugin_ConstraintMiddle.h>
20 #include <SketchPlugin_ConstraintMirror.h>
21 #include <SketchPlugin_ConstraintParallel.h>
22 #include <SketchPlugin_ConstraintPerpendicular.h>
23 #include <SketchPlugin_ConstraintRadius.h>
24 #include <SketchPlugin_ConstraintRigid.h>
25 #include <SketchPlugin_ConstraintTangent.h>
26 #include <SketchPlugin_ConstraintVertical.h>
27 #include <SketchPlugin_MultiRotation.h>
28 #include <SketchPlugin_MultiTranslation.h>
29 #include <SketchPlugin_Validators.h>
30 #include <SketchPlugin_ExternalValidator.h>
31
32 #include <Events_Loop.h>
33 #include <GeomDataAPI_Dir.h>
34
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Document.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_Data.h>
39
40 #include <Config_PropManager.h>
41
42 #include <memory>
43
44 #ifdef _DEBUG
45 #include <iostream>
46 #endif
47
48 //#define SET_PLANES_COLOR_IN_PREFERENCES
49
50 using namespace std;
51
52 // the only created instance of this plugin
53 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
54
55 SketchPlugin_Plugin::SketchPlugin_Plugin()
56 {
57   SessionPtr aMgr = ModelAPI_Session::get();
58   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
59   aFactory->registerValidator("SketchPlugin_DistanceAttr",
60                               new SketchPlugin_DistanceAttrValidator);  
61   aFactory->registerValidator("SketchPlugin_ExternalValidator",
62                               new SketchPlugin_ExternalValidator);
63   aFactory->registerValidator("SketchPlugin_TangentAttr",
64                               new SketchPlugin_TangentAttrValidator);
65   aFactory->registerValidator("SketchPlugin_NotFixed",
66                               new SketchPlugin_NotFixedValidator);
67   aFactory->registerValidator("SketchPlugin_EqualAttr",
68                               new SketchPlugin_EqualAttrValidator);
69   aFactory->registerValidator("SketchPlugin_MirrorAttr",
70                               new SketchPlugin_MirrorAttrValidator);
71   aFactory->registerValidator("SketchPlugin_CoincidenceAttr",
72                               new SketchPlugin_CoincidenceAttrValidator);
73   aFactory->registerValidator("SketchPlugin_CopyValidator",
74                               new SketchPlugin_CopyValidator);
75   aFactory->registerValidator("SketchPlugin_SolverErrorValidator",
76                               new SketchPlugin_SolverErrorValidator);
77   aFactory->registerValidator("SketchPlugin_FilletVertexValidator",
78                               new SketchPlugin_FilletVertexValidator);
79   aFactory->registerValidator("SketchPlugin_MiddlePointAttr",
80                               new SketchPlugin_MiddlePointAttrValidator);
81   aFactory->registerValidator("SketchPlugin_ArcTangentPoint",
82                               new SketchPlugin_ArcTangentPointValidator);
83   aFactory->registerValidator("SketchPlugin_IntersectionValidator",
84                               new SketchPlugin_IntersectionValidator);
85   aFactory->registerValidator("SketchPlugin_ProjectionValidator",
86                               new SketchPlugin_ProjectionValidator);
87
88   // register this plugin
89   ModelAPI_Session::get()->registerPlugin(this);
90   
91   Config_PropManager::registerProp("Visualization", "sketch_entity_color", "Sketch entity color",
92                                    Config_Prop::Color, SKETCH_ENTITY_COLOR);
93
94   Config_PropManager::registerProp("Visualization", "sketch_external_color", "Sketch external entity color",
95                                    Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
96
97   Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color", "Sketch auxiliary entity color",
98                                    Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
99
100   Config_PropManager::registerProp("Visualization", "sketch_dimension_color", "Sketch dimension color",
101                                    Config_Prop::Color, SKETCH_DIMENSION_COLOR);
102
103   Config_PropManager::registerProp("Visualization", "sketch_overconstraint_color",
104                                    "Sketch overconstraint color",
105                                    Config_Prop::Color, SKETCH_OVERCONSTRAINT_COLOR);
106
107   // register sketcher properties
108 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
109   Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
110                                    Config_Prop::Color, YZ_PLANE_COLOR);
111   Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
112                                    Config_Prop::Color, XZ_PLANE_COLOR);
113   Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
114                                    Config_Prop::Color, XY_PLANE_COLOR);
115 #endif
116 }
117
118 FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
119 {
120   if (theFeatureID == SketchPlugin_Sketch::ID()) {
121     return FeaturePtr(new SketchPlugin_Sketch);
122   } else if (theFeatureID == SketchPlugin_Point::ID()) {
123     return FeaturePtr(new SketchPlugin_Point);
124   } else if (theFeatureID == SketchPlugin_IntersectionPoint::ID()) {
125     return FeaturePtr(new SketchPlugin_IntersectionPoint);
126   } else if (theFeatureID == SketchPlugin_Line::ID()) {
127     return FeaturePtr(new SketchPlugin_Line);
128   } else if (theFeatureID == SketchPlugin_Circle::ID()) {
129     return FeaturePtr(new SketchPlugin_Circle);
130   } else if (theFeatureID == SketchPlugin_Arc::ID()) {
131     return FeaturePtr(new SketchPlugin_Arc);
132   } else if (theFeatureID == SketchPlugin_Projection::ID()) {
133     return FeaturePtr(new SketchPlugin_Projection);
134   } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
135     return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
136   } else if (theFeatureID == SketchPlugin_ConstraintCollinear::ID()) {
137     return FeaturePtr(new SketchPlugin_ConstraintCollinear);
138   } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
139     return FeaturePtr(new SketchPlugin_ConstraintDistance);
140   } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
141     return FeaturePtr(new SketchPlugin_ConstraintLength);
142   } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
143     return FeaturePtr(new SketchPlugin_ConstraintParallel);
144   } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
145     return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
146   } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
147     return FeaturePtr(new SketchPlugin_ConstraintRadius);
148   } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
149     return FeaturePtr(new SketchPlugin_ConstraintRigid);
150   } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
151     return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
152   } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
153     return FeaturePtr(new SketchPlugin_ConstraintVertical);
154   } else if (theFeatureID == SketchPlugin_ConstraintEqual::ID()) {
155     return FeaturePtr(new SketchPlugin_ConstraintEqual);
156   } else if (theFeatureID == SketchPlugin_ConstraintTangent::ID()) {
157     return FeaturePtr(new SketchPlugin_ConstraintTangent);
158   } else if (theFeatureID == SketchPlugin_ConstraintMiddle::ID()) {
159     return FeaturePtr(new SketchPlugin_ConstraintMiddle);
160   } else if (theFeatureID == SketchPlugin_ConstraintMirror::ID()) {
161     return FeaturePtr(new SketchPlugin_ConstraintMirror);
162   } else if (theFeatureID == SketchPlugin_ConstraintFillet::ID()) {
163     return FeaturePtr(new SketchPlugin_ConstraintFillet);
164   } else if (theFeatureID == SketchPlugin_MultiTranslation::ID()) {
165     return FeaturePtr(new SketchPlugin_MultiTranslation);
166   } else if (theFeatureID == SketchPlugin_MultiRotation::ID()) {
167     return FeaturePtr(new SketchPlugin_MultiRotation);
168   } else if (theFeatureID == SketchPlugin_ConstraintAngle::ID()) {
169     return FeaturePtr(new SketchPlugin_ConstraintAngle);
170   }
171   // feature of such kind is not found
172   return FeaturePtr();
173 }
174
175 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
176 {
177   const Events_ID kRequestEvent =
178       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
179   if (theMessage->eventID() == kRequestEvent) {
180     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
181         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
182     Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
183   }
184 }
185
186 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
187 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
188 {
189   const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
190   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
191       new ModelAPI_FeatureStateMessage(kResponseEvent, this));
192
193   bool aHasSketchPlane = false;
194   std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
195       std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
196   if (aSketchFeature.get()) {
197     std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
198     if (aData) {
199       std::shared_ptr<GeomDataAPI_Dir> aNormal =
200         std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
201       // it is important to check whether the normal attribute is initialized
202       // because it is possible that normal values are filled when the plane is checked on validity
203       aHasSketchPlane = aNormal && aNormal->isInitialized() &&
204                         !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
205
206       aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
207       aMsg->setState(SketchPlugin_IntersectionPoint::ID(), aHasSketchPlane);
208       aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
209       aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
210       aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
211       aMsg->setState(SketchPlugin_Projection::ID(), aHasSketchPlane);
212       aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
213       aMsg->setState(SketchPlugin_ConstraintCollinear::ID(), aHasSketchPlane);
214       aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
215       aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
216       aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
217       aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
218       aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
219       aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
220       aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
221       aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
222       aMsg->setState(SketchPlugin_ConstraintEqual::ID(), aHasSketchPlane);
223       aMsg->setState(SketchPlugin_ConstraintTangent::ID(), aHasSketchPlane);
224       aMsg->setState(SketchPlugin_ConstraintMiddle::ID(), aHasSketchPlane);
225       aMsg->setState(SketchPlugin_ConstraintMirror::ID(), aHasSketchPlane);
226       aMsg->setState(SketchPlugin_ConstraintFillet::ID(), aHasSketchPlane);
227       aMsg->setState(SketchPlugin_ConstraintAngle::ID(), aHasSketchPlane);
228       aMsg->setState(SketchPlugin_MultiRotation::ID(), aHasSketchPlane);
229       aMsg->setState(SketchPlugin_MultiTranslation::ID(), aHasSketchPlane);
230       // SketchRectangle is a python feature, so its ID is passed just as a string
231       aMsg->setState("SketchRectangle", aHasSketchPlane);
232     }
233   }
234   return aMsg;
235 }