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