Salome HOME
Horizontal and vertical constraints
[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_ConstraintHorizontal.h>
12 #include <SketchPlugin_ConstraintLength.h>
13 #include <SketchPlugin_ConstraintParallel.h>
14 #include <SketchPlugin_ConstraintPerpendicular.h>
15 #include <SketchPlugin_ConstraintRadius.h>
16 #include <SketchPlugin_ConstraintRigid.h>
17 #include <SketchPlugin_ConstraintVertical.h>
18 #include <SketchPlugin_Validators.h>
19 #include <SketchPlugin_ResultValidators.h>
20 #include <SketchPlugin_ShapeValidator.h>
21
22 #include <Events_Loop.h>
23 #include <GeomDataAPI_Dir.h>
24
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Document.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Data.h>
29
30 #include <Config_PropManager.h>
31
32 #include <memory>
33
34 #ifdef _DEBUG
35 #include <iostream>
36 #endif
37
38 //#define SET_PLANES_COLOR_IN_PREFERENCES
39
40 using namespace std;
41
42 // the only created instance of this plugin
43 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
44
45 SketchPlugin_Plugin::SketchPlugin_Plugin()
46 {
47   SessionPtr aMgr = ModelAPI_Session::get();
48   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
49   aFactory->registerValidator("SketchPlugin_DistanceAttr",
50                               new SketchPlugin_DistanceAttrValidator);  
51   //aFactory->registerValidator("SketchPlugin_DifferentObjects",
52   //                            new SketchPlugin_DifferentObjectsValidator);
53   aFactory->registerValidator("SketchPlugin_ResultPoint", new SketchPlugin_ResultPointValidator);
54   aFactory->registerValidator("SketchPlugin_ResultLine", new SketchPlugin_ResultLineValidator);
55   aFactory->registerValidator("SketchPlugin_ResultArc", new SketchPlugin_ResultArcValidator);
56   aFactory->registerValidator("SketchPlugin_ShapeValidator",
57                               new SketchPlugin_ShapeValidator);
58
59   // register this plugin
60   ModelAPI_Session::get()->registerPlugin(this);
61
62   Config_PropManager::registerProp("Visualization", "sketch_entity_color", "Sketch enity color",
63                                    Config_Prop::Color, SKETCH_ENTITY_COLOR);
64
65   Config_PropManager::registerProp("Visualization", "sketch_external_color", "Sketch external entity color",
66                                    Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
67
68   Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color", "Sketch auxiliary entity color",
69                                    Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
70
71   Config_PropManager::registerProp("Visualization", "sketch_constraint_color", "Sketch constraint color",
72                                    Config_Prop::Color, SKETCH_CONSTRAINT_COLOR);
73
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   } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
115     return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
116   } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
117     return FeaturePtr(new SketchPlugin_ConstraintVertical);
118   }
119   // feature of such kind is not found
120   return FeaturePtr();
121 }
122
123 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
124 {
125   const Events_ID kRequestEvent =
126       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
127   if (theMessage->eventID() == kRequestEvent) {
128     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
129         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
130     Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
131   }
132 }
133
134 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
135 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
136 {
137   const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
138   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
139       std::make_shared<ModelAPI_FeatureStateMessage>(kResponseEvent, this);
140
141   bool aHasSketchPlane = false;
142   std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
143       std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
144   if (aSketchFeature.get()) {
145     std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
146     if (aData) {
147       std::shared_ptr<GeomDataAPI_Dir> aNormal =
148         std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
149       aHasSketchPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
150
151       aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
152       aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
153       aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
154       aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
155       aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
156       aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
157       aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
158       aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
159       aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
160       aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
161       aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
162       aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
163       aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
164     }
165   }
166   return aMsg;
167 }