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