Salome HOME
030f786f18b1d78d2e5d0f218b0dd3e0d98ebb85
[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_ConstraintLength.h>
12 #include <SketchPlugin_ConstraintParallel.h>
13 #include <SketchPlugin_ConstraintPerpendicular.h>
14 #include <SketchPlugin_ConstraintRadius.h>
15 #include <SketchPlugin_ConstraintRigid.h>
16 #include <SketchPlugin_Validators.h>
17 #include <SketchPlugin_ResultValidators.h>
18
19 #include <Events_Loop.h>
20 #include <GeomDataAPI_Dir.h>
21
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Data.h>
26
27 #include <Config_PropManager.h>
28
29 #include <memory>
30
31 #ifdef _DEBUG
32 #include <iostream>
33 #endif
34
35 using namespace std;
36
37 // the only created instance of this plugin
38 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
39
40 SketchPlugin_Plugin::SketchPlugin_Plugin()
41 {
42   SessionPtr aMgr = ModelAPI_Session::get();
43   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
44   aFactory->registerValidator("SketchPlugin_DistanceAttr",
45                               new SketchPlugin_DistanceAttrValidator);  
46   aFactory->registerValidator("SketchPlugin_DifferentObjects",
47                               new SketchPlugin_DifferentObjectsValidator);
48   aFactory->registerValidator("SketchPlugin_ResultPoint", new SketchPlugin_ResultPointValidator);
49   aFactory->registerValidator("SketchPlugin_ResultLine", new SketchPlugin_ResultLineValidator);
50   aFactory->registerValidator("SketchPlugin_ResultArc", new SketchPlugin_ResultArcValidator);
51
52   // register this plugin
53   ModelAPI_Session::get()->registerPlugin(this);
54
55   // register sketcher properties
56
57   Config_PropManager::registerProp("Visualization", "parallel_color", "Parallel constraint color",
58                                    Config_Prop::Color, PARALLEL_COLOR);
59   Config_PropManager::registerProp("Visualization", "perpendicular_color",
60                                    "Perpendicular constraint color", Config_Prop::Color,
61                                    PERPENDICULAR_COLOR);
62   Config_PropManager::registerProp("Visualization", "distance_color", "Distance color",
63                                    Config_Prop::Color, DISTANCE_COLOR);
64   Config_PropManager::registerProp("Visualization", "length_color", "Length color",
65                                    Config_Prop::Color, LENGTH_COLOR);
66   Config_PropManager::registerProp("Visualization", "radius_color", "Radius color",
67                                    Config_Prop::Color, RADIUS_COLOR);
68   Config_PropManager::registerProp("Visualization", "fixing_color", "Fixing color",
69                                    Config_Prop::Color, FIXING_COLOR);
70 }
71
72 FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
73 {
74   if (theFeatureID == SketchPlugin_Sketch::ID()) {
75     return FeaturePtr(new SketchPlugin_Sketch);
76   } else if (theFeatureID == SketchPlugin_Point::ID()) {
77     return FeaturePtr(new SketchPlugin_Point);
78   } else if (theFeatureID == SketchPlugin_Line::ID()) {
79     return FeaturePtr(new SketchPlugin_Line);
80   } else if (theFeatureID == SketchPlugin_Circle::ID()) {
81     return FeaturePtr(new SketchPlugin_Circle);
82   } else if (theFeatureID == SketchPlugin_Arc::ID()) {
83     return FeaturePtr(new SketchPlugin_Arc);
84   } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
85     return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
86   } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
87     return FeaturePtr(new SketchPlugin_ConstraintDistance);
88   } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
89     return FeaturePtr(new SketchPlugin_ConstraintLength);
90   } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
91     return FeaturePtr(new SketchPlugin_ConstraintParallel);
92   } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
93     return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
94   } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
95     return FeaturePtr(new SketchPlugin_ConstraintRadius);
96   } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
97     return FeaturePtr(new SketchPlugin_ConstraintRigid);
98   }
99   // feature of such kind is not found
100   return FeaturePtr();
101 }
102
103 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
104 {
105   const Events_ID kRequestEvent =
106       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
107   if (theMessage->eventID() == kRequestEvent) {
108     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
109         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
110     Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
111   }
112 }
113
114 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
115 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
116 {
117   const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
118   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
119       std::make_shared<ModelAPI_FeatureStateMessage>(kResponseEvent, this);
120
121   bool aHasSketchPlane = false;
122   std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
123       std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
124   if (aSketchFeature.get()) {
125     std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
126     if (aData) {
127       std::shared_ptr<GeomDataAPI_Dir> aNormal =
128         std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
129       aHasSketchPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
130
131       aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
132       aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
133       aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
134       aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
135       aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
136       aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
137       aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
138       aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
139       aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
140       aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
141       aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
142     }
143   }
144   return aMsg;
145 }