Salome HOME
35ff48c6ab8101d120185aa8db6e6ec22343d364
[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_Circle.h>
8 #include <SketchPlugin_Arc.h>
9 #include <SketchPlugin_ConstraintCoincidence.h>
10 #include <SketchPlugin_ConstraintDistance.h>
11 #include <SketchPlugin_ConstraintEqual.h>
12 #include <SketchPlugin_ConstraintFillet.h>
13 #include <SketchPlugin_ConstraintHorizontal.h>
14 #include <SketchPlugin_ConstraintLength.h>
15 #include <SketchPlugin_ConstraintMirror.h>
16 #include <SketchPlugin_ConstraintParallel.h>
17 #include <SketchPlugin_ConstraintPerpendicular.h>
18 #include <SketchPlugin_ConstraintRadius.h>
19 #include <SketchPlugin_ConstraintRigid.h>
20 #include <SketchPlugin_ConstraintTangent.h>
21 #include <SketchPlugin_ConstraintVertical.h>
22 #include <SketchPlugin_Validators.h>
23 #include <SketchPlugin_ShapeValidator.h>
24
25 #include <Events_Loop.h>
26 #include <GeomDataAPI_Dir.h>
27
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Document.h>
30 #include <ModelAPI_Validator.h>
31 #include <ModelAPI_Data.h>
32
33 #include <Config_PropManager.h>
34
35 #include <memory>
36
37 #ifdef _DEBUG
38 #include <iostream>
39 #endif
40
41 //#define SET_PLANES_COLOR_IN_PREFERENCES
42
43 using namespace std;
44
45 // the only created instance of this plugin
46 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
47
48 SketchPlugin_Plugin::SketchPlugin_Plugin()
49 {
50   SessionPtr aMgr = ModelAPI_Session::get();
51   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
52   aFactory->registerValidator("SketchPlugin_DistanceAttr",
53                               new SketchPlugin_DistanceAttrValidator);  
54   //aFactory->registerValidator("SketchPlugin_DifferentObjects",
55   //                            new SketchPlugin_DifferentObjectsValidator);
56   aFactory->registerValidator("SketchPlugin_ShapeValidator", new SketchPlugin_ShapeValidator);
57
58   // register this plugin
59   ModelAPI_Session::get()->registerPlugin(this);
60
61   Config_PropManager::registerProp("Visualization", "sketch_entity_color", "Sketch enity color",
62                                    Config_Prop::Color, SKETCH_ENTITY_COLOR);
63
64   Config_PropManager::registerProp("Visualization", "sketch_external_color", "Sketch external entity color",
65                                    Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
66
67   Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color", "Sketch auxiliary entity color",
68                                    Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
69
70   Config_PropManager::registerProp("Visualization", "sketch_constraint_color", "Sketch constraint color",
71                                    Config_Prop::Color, SKETCH_CONSTRAINT_COLOR);
72
73   Config_PropManager::registerProp("Visualization", "sketch_dimension_color", "Sketch dimension color",
74                                    Config_Prop::Color, SKETCH_DIMENSION_COLOR);
75
76   // register sketcher properties
77 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
78   Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
79                                    Config_Prop::Color, YZ_PLANE_COLOR);
80   Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
81                                    Config_Prop::Color, XZ_PLANE_COLOR);
82   Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
83                                    Config_Prop::Color, XY_PLANE_COLOR);
84 #endif
85 }
86
87 FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
88 {
89   if (theFeatureID == SketchPlugin_Sketch::ID()) {
90     return FeaturePtr(new SketchPlugin_Sketch);
91   } else if (theFeatureID == SketchPlugin_Point::ID()) {
92     return FeaturePtr(new SketchPlugin_Point);
93   } else if (theFeatureID == SketchPlugin_Line::ID()) {
94     return FeaturePtr(new SketchPlugin_Line);
95   } else if (theFeatureID == SketchPlugin_Circle::ID()) {
96     return FeaturePtr(new SketchPlugin_Circle);
97   } else if (theFeatureID == SketchPlugin_Arc::ID()) {
98     return FeaturePtr(new SketchPlugin_Arc);
99   } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
100     return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
101   } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
102     return FeaturePtr(new SketchPlugin_ConstraintDistance);
103   } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
104     return FeaturePtr(new SketchPlugin_ConstraintLength);
105   } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
106     return FeaturePtr(new SketchPlugin_ConstraintParallel);
107   } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
108     return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
109   } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
110     return FeaturePtr(new SketchPlugin_ConstraintRadius);
111   } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
112     return FeaturePtr(new SketchPlugin_ConstraintRigid);
113   } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
114     return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
115   } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
116     return FeaturePtr(new SketchPlugin_ConstraintVertical);
117   } else if (theFeatureID == SketchPlugin_ConstraintEqual::ID()) {
118     return FeaturePtr(new SketchPlugin_ConstraintEqual);
119   } else if (theFeatureID == SketchPlugin_ConstraintTangent::ID()) {
120     return FeaturePtr(new SketchPlugin_ConstraintTangent);
121   } else if (theFeatureID == SketchPlugin_ConstraintMirror::ID()) {
122     return FeaturePtr(new SketchPlugin_ConstraintMirror);
123   } else if (theFeatureID == SketchPlugin_ConstraintFillet::ID()) {
124     return FeaturePtr(new SketchPlugin_ConstraintFillet);
125   }
126   // feature of such kind is not found
127   return FeaturePtr();
128 }
129
130 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
131 {
132   const Events_ID kRequestEvent =
133       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
134   if (theMessage->eventID() == kRequestEvent) {
135     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
136         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
137     Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
138   }
139 }
140
141 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
142 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
143 {
144   const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
145   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
146       std::make_shared<ModelAPI_FeatureStateMessage>(kResponseEvent, this);
147
148   bool aHasSketchPlane = false;
149   std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
150       std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
151   if (aSketchFeature.get()) {
152     std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
153     if (aData) {
154       std::shared_ptr<GeomDataAPI_Dir> aNormal =
155         std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
156       aHasSketchPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
157
158       aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
159       aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
160       aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
161       aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
162       aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
163       aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
164       aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
165       aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
166       aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
167       aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
168       aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
169       aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
170       aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
171       aMsg->setState(SketchPlugin_ConstraintEqual::ID(), aHasSketchPlane);
172       aMsg->setState(SketchPlugin_ConstraintTangent::ID(), aHasSketchPlane);
173       aMsg->setState(SketchPlugin_ConstraintMirror::ID(), aHasSketchPlane);
174       aMsg->setState(SketchPlugin_ConstraintFillet::ID(), aHasSketchPlane);
175     }
176   }
177   return aMsg;
178 }