1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <SketchPlugin_Plugin.h>
22 #include <SketchPlugin_Sketch.h>
23 #include <SketchPlugin_Line.h>
24 #include <SketchPlugin_Point.h>
25 #include <SketchPlugin_IntersectionPoint.h>
26 #include <SketchPlugin_Circle.h>
27 #include <SketchPlugin_Arc.h>
28 #include <SketchPlugin_Projection.h>
29 #include <SketchPlugin_ConstraintAngle.h>
30 #include <SketchPlugin_ConstraintCoincidence.h>
31 #include <SketchPlugin_ConstraintCollinear.h>
32 #include <SketchPlugin_ConstraintDistance.h>
33 #include <SketchPlugin_ConstraintEqual.h>
34 #include <SketchPlugin_Fillet.h>
35 #include <SketchPlugin_ConstraintHorizontal.h>
36 #include <SketchPlugin_ConstraintLength.h>
37 #include <SketchPlugin_ConstraintMiddle.h>
38 #include <SketchPlugin_ConstraintMirror.h>
39 #include <SketchPlugin_ConstraintParallel.h>
40 #include <SketchPlugin_ConstraintPerpendicular.h>
41 #include <SketchPlugin_ConstraintRadius.h>
42 #include <SketchPlugin_ConstraintRigid.h>
43 #include <SketchPlugin_ConstraintTangent.h>
44 #include <SketchPlugin_ConstraintVertical.h>
45 #include <SketchPlugin_MacroArc.h>
46 #include <SketchPlugin_MacroCircle.h>
47 #include <SketchPlugin_MultiRotation.h>
48 #include <SketchPlugin_MultiTranslation.h>
49 #include <SketchPlugin_Trim.h>
50 #include <SketchPlugin_Split.h>
51 #include <SketchPlugin_Validators.h>
52 #include <SketchPlugin_ExternalValidator.h>
54 #include <Events_Loop.h>
55 #include <GeomDataAPI_Dir.h>
57 #include <ModelAPI_Session.h>
58 #include <ModelAPI_Document.h>
59 #include <ModelAPI_Validator.h>
60 #include <ModelAPI_Data.h>
62 #include <Config_PropManager.h>
70 #define SKETCH_ENTITY_COLOR "225,0,0"
71 #define SKETCH_EXTERNAL_COLOR "170,0,225"
72 #define SKETCH_AUXILIARY_COLOR "0,85,0"
73 #define SKETCH_OVERCONSTRAINT_COLOR "0,0,0"
74 #define SKETCH_FULLY_CONSTRAINED_COLOR "0,150,0"
76 //#define SET_PLANES_COLOR_IN_PREFERENCES
78 // the only created instance of this plugin
79 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
81 SketchPlugin_Plugin::SketchPlugin_Plugin()
83 SessionPtr aMgr = ModelAPI_Session::get();
84 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
85 aFactory->registerValidator("SketchPlugin_DistanceAttr",
86 new SketchPlugin_DistanceAttrValidator);
87 aFactory->registerValidator("SketchPlugin_ExternalValidator",
88 new SketchPlugin_ExternalValidator);
89 aFactory->registerValidator("SketchPlugin_TangentAttr",
90 new SketchPlugin_TangentAttrValidator);
91 aFactory->registerValidator("SketchPlugin_NotFixed",
92 new SketchPlugin_NotFixedValidator);
93 aFactory->registerValidator("SketchPlugin_EqualAttr",
94 new SketchPlugin_EqualAttrValidator);
95 aFactory->registerValidator("SketchPlugin_MirrorAttr",
96 new SketchPlugin_MirrorAttrValidator);
97 aFactory->registerValidator("SketchPlugin_CoincidenceAttr",
98 new SketchPlugin_CoincidenceAttrValidator);
99 aFactory->registerValidator("SketchPlugin_CopyValidator",
100 new SketchPlugin_CopyValidator);
101 aFactory->registerValidator("SketchPlugin_SolverErrorValidator",
102 new SketchPlugin_SolverErrorValidator);
103 aFactory->registerValidator("SketchPlugin_FilletVertexValidator",
104 new SketchPlugin_FilletVertexValidator);
105 aFactory->registerValidator("SketchPlugin_SplitValidator",
106 new SketchPlugin_SplitValidator);
107 aFactory->registerValidator("SketchPlugin_TrimValidator",
108 new SketchPlugin_TrimValidator);
109 aFactory->registerValidator("SketchPlugin_MiddlePointAttr",
110 new SketchPlugin_MiddlePointAttrValidator);
111 aFactory->registerValidator("SketchPlugin_ArcTangentPoint",
112 new SketchPlugin_ArcTangentPointValidator);
113 aFactory->registerValidator("SketchPlugin_IntersectionValidator",
114 new SketchPlugin_IntersectionValidator);
115 aFactory->registerValidator("SketchPlugin_ProjectionValidator",
116 new SketchPlugin_ProjectionValidator);
117 aFactory->registerValidator("SketchPlugin_DifferentReference",
118 new SketchPlugin_DifferentReferenceValidator);
119 aFactory->registerValidator("SketchPlugin_DifferentPointReference",
120 new SketchPlugin_DifferentPointReferenceValidator);
121 aFactory->registerValidator("SketchPlugin_CirclePassedPointValidator",
122 new SketchPlugin_CirclePassedPointValidator);
123 aFactory->registerValidator("SketchPlugin_ThirdPointValidator",
124 new SketchPlugin_ThirdPointValidator);
125 aFactory->registerValidator("SketchPlugin_ArcEndPointValidator",
126 new SketchPlugin_ArcEndPointValidator);
127 aFactory->registerValidator("SketchPlugin_ArcEndPointIntersectionValidator",
128 new SketchPlugin_ArcEndPointIntersectionValidator);
130 // register this plugin
131 ModelAPI_Session::get()->registerPlugin(this);
133 Config_PropManager::registerProp("Visualization", "sketch_entity_color",
134 "Sketch entity color",
135 Config_Prop::Color, SKETCH_ENTITY_COLOR);
137 Config_PropManager::registerProp("Visualization", "sketch_external_color",
138 "Sketch external entity color",
139 Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
141 Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color",
142 "Sketch auxiliary entity color",
143 Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
145 Config_PropManager::registerProp("Visualization", "sketch_dimension_color",
146 "Sketch dimension color",
147 Config_Prop::Color, SKETCH_DIMENSION_COLOR);
149 Config_PropManager::registerProp("Visualization", "sketch_overconstraint_color",
150 "Sketch overconstraint color",
151 Config_Prop::Color, SKETCH_OVERCONSTRAINT_COLOR);
153 Config_PropManager::registerProp("Visualization", "sketch_fully_constrained_color",
154 "Sketch fully constrained color",
155 Config_Prop::Color, SKETCH_FULLY_CONSTRAINED_COLOR);
157 // register sketcher properties
158 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
159 Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
160 Config_Prop::Color, YZ_PLANE_COLOR);
161 Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
162 Config_Prop::Color, XZ_PLANE_COLOR);
163 Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
164 Config_Prop::Color, XY_PLANE_COLOR);
168 FeaturePtr SketchPlugin_Plugin::createFeature(std::string theFeatureID)
170 if (theFeatureID == SketchPlugin_Sketch::ID()) {
171 return FeaturePtr(new SketchPlugin_Sketch);
172 } else if (theFeatureID == SketchPlugin_Point::ID()) {
173 return FeaturePtr(new SketchPlugin_Point);
174 } else if (theFeatureID == SketchPlugin_IntersectionPoint::ID()) {
175 return FeaturePtr(new SketchPlugin_IntersectionPoint);
176 } else if (theFeatureID == SketchPlugin_Line::ID()) {
177 return FeaturePtr(new SketchPlugin_Line);
178 } else if (theFeatureID == SketchPlugin_Circle::ID()) {
179 return FeaturePtr(new SketchPlugin_Circle);
180 } else if (theFeatureID == SketchPlugin_Arc::ID()) {
181 return FeaturePtr(new SketchPlugin_Arc);
182 } else if (theFeatureID == SketchPlugin_Projection::ID()) {
183 return FeaturePtr(new SketchPlugin_Projection);
184 } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
185 return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
186 } else if (theFeatureID == SketchPlugin_ConstraintCollinear::ID()) {
187 return FeaturePtr(new SketchPlugin_ConstraintCollinear);
188 } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
189 return FeaturePtr(new SketchPlugin_ConstraintDistance);
190 } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
191 return FeaturePtr(new SketchPlugin_ConstraintLength);
192 } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
193 return FeaturePtr(new SketchPlugin_ConstraintParallel);
194 } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
195 return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
196 } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
197 return FeaturePtr(new SketchPlugin_ConstraintRadius);
198 } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
199 return FeaturePtr(new SketchPlugin_ConstraintRigid);
200 } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
201 return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
202 } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
203 return FeaturePtr(new SketchPlugin_ConstraintVertical);
204 } else if (theFeatureID == SketchPlugin_ConstraintEqual::ID()) {
205 return FeaturePtr(new SketchPlugin_ConstraintEqual);
206 } else if (theFeatureID == SketchPlugin_ConstraintTangent::ID()) {
207 return FeaturePtr(new SketchPlugin_ConstraintTangent);
208 } else if (theFeatureID == SketchPlugin_ConstraintMiddle::ID()) {
209 return FeaturePtr(new SketchPlugin_ConstraintMiddle);
210 } else if (theFeatureID == SketchPlugin_ConstraintMirror::ID()) {
211 return FeaturePtr(new SketchPlugin_ConstraintMirror);
212 } else if (theFeatureID == SketchPlugin_Fillet::ID()) {
213 return FeaturePtr(new SketchPlugin_Fillet);
214 } else if (theFeatureID == SketchPlugin_MultiTranslation::ID()) {
215 return FeaturePtr(new SketchPlugin_MultiTranslation);
216 } else if (theFeatureID == SketchPlugin_MultiRotation::ID()) {
217 return FeaturePtr(new SketchPlugin_MultiRotation);
218 } else if (theFeatureID == SketchPlugin_ConstraintAngle::ID()) {
219 return FeaturePtr(new SketchPlugin_ConstraintAngle);
220 } else if (theFeatureID == SketchPlugin_Split::ID()) {
221 return FeaturePtr(new SketchPlugin_Split);
222 } else if (theFeatureID == SketchPlugin_Trim::ID()) {
223 return FeaturePtr(new SketchPlugin_Trim);
224 } else if (theFeatureID == SketchPlugin_MacroArc::ID()) {
225 return FeaturePtr(new SketchPlugin_MacroArc);
226 } else if (theFeatureID == SketchPlugin_MacroCircle::ID()) {
227 return FeaturePtr(new SketchPlugin_MacroCircle);
229 // feature of such kind is not found
233 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
235 const Events_ID kRequestEvent =
236 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
237 if (theMessage->eventID() == kRequestEvent) {
238 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
239 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
240 Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
244 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
245 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
247 const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
248 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
249 new ModelAPI_FeatureStateMessage(kResponseEvent, this));
251 bool aHasSketchPlane = false;
252 std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
253 std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
254 if (aSketchFeature.get()) {
255 std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
257 std::shared_ptr<GeomDataAPI_Dir> aNormal =
258 std::dynamic_pointer_cast<GeomDataAPI_Dir>(
259 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
260 // it is important to check whether the normal attribute is initialized
261 // because it is possible that normal values are filled when the plane is checked on validity
262 aHasSketchPlane = aNormal && aNormal->isInitialized() &&
263 !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
265 aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
266 aMsg->setState(SketchPlugin_IntersectionPoint::ID(), aHasSketchPlane);
267 aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
268 aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
269 aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
270 aMsg->setState(SketchPlugin_Projection::ID(), aHasSketchPlane);
271 aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
272 aMsg->setState(SketchPlugin_ConstraintCollinear::ID(), aHasSketchPlane);
273 aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
274 aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
275 aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
276 aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
277 aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
278 aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
279 aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
280 aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
281 aMsg->setState(SketchPlugin_ConstraintEqual::ID(), aHasSketchPlane);
282 aMsg->setState(SketchPlugin_ConstraintTangent::ID(), aHasSketchPlane);
283 aMsg->setState(SketchPlugin_ConstraintMiddle::ID(), aHasSketchPlane);
284 aMsg->setState(SketchPlugin_ConstraintMirror::ID(), aHasSketchPlane);
285 aMsg->setState(SketchPlugin_Fillet::ID(), aHasSketchPlane);
286 aMsg->setState(SketchPlugin_ConstraintAngle::ID(), aHasSketchPlane);
287 aMsg->setState(SketchPlugin_MultiRotation::ID(), aHasSketchPlane);
288 aMsg->setState(SketchPlugin_MultiTranslation::ID(), aHasSketchPlane);
289 aMsg->setState(SketchPlugin_Split::ID(), aHasSketchPlane);
290 aMsg->setState(SketchPlugin_Trim::ID(), aHasSketchPlane);
291 aMsg->setState(SketchPlugin_MacroArc::ID(), aHasSketchPlane);
292 aMsg->setState(SketchPlugin_MacroCircle::ID(), aHasSketchPlane);
293 // SketchRectangle is a python feature, so its ID is passed just as a string
294 aMsg->setState("SketchRectangle", aHasSketchPlane);