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