Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[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   Config_PropManager::registerProp("Sketch planes", "planes_color", "Color", Config_Prop::Color,
57                                    SKETCH_PLANE_COLOR);
58   Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
59                                    PLANE_SIZE);
60   Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
61                                    Config_Prop::Integer, SKETCH_WIDTH);
62
63   Config_PropManager::registerProp("Visualization", "parallel_color", "Parallel constraint color",
64                                    Config_Prop::Color, PARALLEL_COLOR);
65   Config_PropManager::registerProp("Visualization", "perpendicular_color",
66                                    "Perpendicular constraint color", Config_Prop::Color,
67                                    PERPENDICULAR_COLOR);
68   Config_PropManager::registerProp("Visualization", "distance_color", "Distance color",
69                                    Config_Prop::Color, DISTANCE_COLOR);
70   Config_PropManager::registerProp("Visualization", "length_color", "Length color",
71                                    Config_Prop::Color, LENGTH_COLOR);
72   Config_PropManager::registerProp("Visualization", "radius_color", "Radius color",
73                                    Config_Prop::Color, RADIUS_COLOR);
74   Config_PropManager::registerProp("Visualization", "fixing_color", "Fixing color",
75                                    Config_Prop::Color, FIXING_COLOR);
76 }
77
78 FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
79 {
80   if (theFeatureID == SketchPlugin_Sketch::ID()) {
81     return FeaturePtr(new SketchPlugin_Sketch);
82   } else if (theFeatureID == SketchPlugin_Point::ID()) {
83     return FeaturePtr(new SketchPlugin_Point);
84   } else if (theFeatureID == SketchPlugin_Line::ID()) {
85     return FeaturePtr(new SketchPlugin_Line);
86   } else if (theFeatureID == SketchPlugin_Circle::ID()) {
87     return FeaturePtr(new SketchPlugin_Circle);
88   } else if (theFeatureID == SketchPlugin_Arc::ID()) {
89     return FeaturePtr(new SketchPlugin_Arc);
90   } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
91     return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
92   } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
93     return FeaturePtr(new SketchPlugin_ConstraintDistance);
94   } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
95     return FeaturePtr(new SketchPlugin_ConstraintLength);
96   } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
97     return FeaturePtr(new SketchPlugin_ConstraintParallel);
98   } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
99     return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
100   } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
101     return FeaturePtr(new SketchPlugin_ConstraintRadius);
102   } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
103     return FeaturePtr(new SketchPlugin_ConstraintRigid);
104   }
105   // feature of such kind is not found
106   return FeaturePtr();
107 }
108
109 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
110 {
111   const Events_ID kRequestEvent =
112       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
113   if (theMessage->eventID() == kRequestEvent) {
114     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
115         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
116     Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
117   }
118 }
119
120 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
121 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
122 {
123   const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
124   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
125       std::make_shared<ModelAPI_FeatureStateMessage>(kResponseEvent, this);
126
127   bool aHasSketchPlane = false;
128   std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
129       std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
130   if (aSketchFeature.get()) {
131     std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
132     if (aData) {
133       std::shared_ptr<GeomDataAPI_Dir> aNormal =
134         std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
135       aHasSketchPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
136
137       aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
138       aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
139       aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
140       aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
141       aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
142       aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
143       aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
144       aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
145       aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
146       aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
147       aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
148     }
149   }
150   return aMsg;
151 }