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