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