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