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