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