Salome HOME
Construction elements are auxiliary entities:
[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_construction_color", "Sketch construction color",
70                                    Config_Prop::Color, SKETCH_CONSTRUCTION_COLOR);
71
72   Config_PropManager::registerProp("Visualization", "sketch_parallel_color", "Sketch constraint color",
73                                    Config_Prop::Color, SKETCH_CONSTRAINT_COLOR);
74   Config_PropManager::registerProp("Visualization", "sketch_dimension_color", "Sketch dimension color",
75                                    Config_Prop::Color, SKETCH_DIMENSION_COLOR);
76
77   // register sketcher properties
78 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
79   Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
80                                    Config_Prop::Color, YZ_PLANE_COLOR);
81   Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
82                                    Config_Prop::Color, XZ_PLANE_COLOR);
83   Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
84                                    Config_Prop::Color, XY_PLANE_COLOR);
85 #endif
86 }
87
88 FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
89 {
90   if (theFeatureID == SketchPlugin_Sketch::ID()) {
91     return FeaturePtr(new SketchPlugin_Sketch);
92   } else if (theFeatureID == SketchPlugin_Point::ID()) {
93     return FeaturePtr(new SketchPlugin_Point);
94   } else if (theFeatureID == SketchPlugin_Line::ID()) {
95     return FeaturePtr(new SketchPlugin_Line);
96   } else if (theFeatureID == SketchPlugin_Circle::ID()) {
97     return FeaturePtr(new SketchPlugin_Circle);
98   } else if (theFeatureID == SketchPlugin_Arc::ID()) {
99     return FeaturePtr(new SketchPlugin_Arc);
100   } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
101     return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
102   } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
103     return FeaturePtr(new SketchPlugin_ConstraintDistance);
104   } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
105     return FeaturePtr(new SketchPlugin_ConstraintLength);
106   } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
107     return FeaturePtr(new SketchPlugin_ConstraintParallel);
108   } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
109     return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
110   } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
111     return FeaturePtr(new SketchPlugin_ConstraintRadius);
112   } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
113     return FeaturePtr(new SketchPlugin_ConstraintRigid);
114   }
115   // feature of such kind is not found
116   return FeaturePtr();
117 }
118
119 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
120 {
121   const Events_ID kRequestEvent =
122       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
123   if (theMessage->eventID() == kRequestEvent) {
124     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
125         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
126     Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
127   }
128 }
129
130 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
131 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
132 {
133   const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
134   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
135       std::make_shared<ModelAPI_FeatureStateMessage>(kResponseEvent, this);
136
137   bool aHasSketchPlane = false;
138   std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
139       std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
140   if (aSketchFeature.get()) {
141     std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
142     if (aData) {
143       std::shared_ptr<GeomDataAPI_Dir> aNormal =
144         std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
145       aHasSketchPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
146
147       aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
148       aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
149       aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
150       aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
151       aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
152       aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
153       aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
154       aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
155       aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
156       aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
157       aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
158     }
159   }
160   return aMsg;
161 }