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