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