1 // Copyright (C) 2014-2020 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 email : webmaster.salome@opencascade.com
20 #include <SketchPlugin_Plugin.h>
21 #include <SketchPlugin_Sketch.h>
22 #include <SketchPlugin_Line.h>
23 #include <SketchPlugin_Point.h>
24 #include <SketchPlugin_IntersectionPoint.h>
25 #include <SketchPlugin_Circle.h>
26 #include <SketchPlugin_Rectangle.h>
27 #include <SketchPlugin_Arc.h>
28 #include <SketchPlugin_BSpline.h>
29 #include <SketchPlugin_BSplinePeriodic.h>
30 #include <SketchPlugin_Projection.h>
31 #include <SketchPlugin_ConstraintAngle.h>
32 #include <SketchPlugin_ConstraintCoincidence.h>
33 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
34 #include <SketchPlugin_ConstraintCollinear.h>
35 #include <SketchPlugin_ConstraintDistance.h>
36 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
37 #include <SketchPlugin_ConstraintDistanceVertical.h>
38 #include <SketchPlugin_ConstraintEqual.h>
39 #include <SketchPlugin_Fillet.h>
40 #include <SketchPlugin_ConstraintHorizontal.h>
41 #include <SketchPlugin_ConstraintLength.h>
42 #include <SketchPlugin_ConstraintMiddle.h>
43 #include <SketchPlugin_ConstraintMirror.h>
44 #include <SketchPlugin_ConstraintParallel.h>
45 #include <SketchPlugin_ConstraintPerpendicular.h>
46 #include <SketchPlugin_ConstraintRadius.h>
47 #include <SketchPlugin_ConstraintRigid.h>
48 #include <SketchPlugin_ConstraintTangent.h>
49 #include <SketchPlugin_ConstraintVertical.h>
50 #include <SketchPlugin_MacroArc.h>
51 #include <SketchPlugin_MacroBSpline.h>
52 #include <SketchPlugin_MacroCircle.h>
53 #include <SketchPlugin_MacroRectangle.h>
54 #include <SketchPlugin_MultiRotation.h>
55 #include <SketchPlugin_MultiTranslation.h>
56 #include <SketchPlugin_Offset.h>
57 #include <SketchPlugin_Trim.h>
58 #include <SketchPlugin_Split.h>
59 #include <SketchPlugin_Validators.h>
60 #include <SketchPlugin_ExternalValidator.h>
61 #include <SketchPlugin_Ellipse.h>
62 #include <SketchPlugin_MacroEllipse.h>
63 #include <SketchPlugin_EllipticArc.h>
64 #include <SketchPlugin_MacroEllipticArc.h>
65 #include <SketchPlugin_SketchDrawer.h>
66 #include <SketchPlugin_SketchCopy.h>
67 #include <SketchPlugin_CurveFitting.h>
69 #include <SketcherPrs_Tools.h>
71 #include <Events_Loop.h>
72 #include <GeomDataAPI_Dir.h>
74 #include <ModelAPI_Session.h>
75 #include <ModelAPI_Document.h>
76 #include <ModelAPI_Validator.h>
77 #include <ModelAPI_Data.h>
79 #include <Config_PropManager.h>
88 #define SKETCH_ENTITY_COLOR "225,0,0"
89 #define SKETCH_EXTERNAL_COLOR "170,0,225"
90 #define SKETCH_AUXILIARY_COLOR "0,85,0"
91 #define SKETCH_OVERCONSTRAINT_COLOR "255,0,0"
92 #define SKETCH_FULLY_CONSTRAINED_COLOR "0,150,0"
94 //#define SET_PLANES_COLOR_IN_PREFERENCES
96 // the only created instance of this plugin
97 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
99 SketchPlugin_Plugin::SketchPlugin_Plugin()
101 SessionPtr aMgr = ModelAPI_Session::get();
102 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
103 aFactory->registerValidator("SketchPlugin_DistanceAttr",
104 new SketchPlugin_DistanceAttrValidator);
105 aFactory->registerValidator("SketchPlugin_ExternalValidator",
106 new SketchPlugin_ExternalValidator);
107 aFactory->registerValidator("SketchPlugin_TangentAttr",
108 new SketchPlugin_TangentAttrValidator);
109 aFactory->registerValidator("SketchPlugin_PerpendicularAttr",
110 new SketchPlugin_PerpendicularAttrValidator);
111 aFactory->registerValidator("SketchPlugin_NotFixed",
112 new SketchPlugin_NotFixedValidator);
113 aFactory->registerValidator("SketchPlugin_EqualAttr",
114 new SketchPlugin_EqualAttrValidator);
115 aFactory->registerValidator("SketchPlugin_MirrorAttr",
116 new SketchPlugin_MirrorAttrValidator);
117 aFactory->registerValidator("SketchPlugin_CoincidenceAttr",
118 new SketchPlugin_CoincidenceAttrValidator);
119 aFactory->registerValidator("SketchPlugin_CopyValidator",
120 new SketchPlugin_CopyValidator);
121 aFactory->registerValidator("SketchPlugin_SolverErrorValidator",
122 new SketchPlugin_SolverErrorValidator);
123 aFactory->registerValidator("SketchPlugin_FilletVertexValidator",
124 new SketchPlugin_FilletVertexValidator);
125 aFactory->registerValidator("SketchPlugin_SplitValidator",
126 new SketchPlugin_SplitValidator);
127 aFactory->registerValidator("SketchPlugin_TrimValidator",
128 new SketchPlugin_TrimValidator);
129 aFactory->registerValidator("SketchPlugin_MiddlePointAttr",
130 new SketchPlugin_MiddlePointAttrValidator);
131 aFactory->registerValidator("SketchPlugin_ArcTangentPoint",
132 new SketchPlugin_ArcTangentPointValidator);
133 aFactory->registerValidator("SketchPlugin_ArcTransversalPoint",
134 new SketchPlugin_ArcTransversalPointValidator);
135 aFactory->registerValidator("SketchPlugin_IntersectionValidator",
136 new SketchPlugin_IntersectionValidator);
137 aFactory->registerValidator("SketchPlugin_ProjectionValidator",
138 new SketchPlugin_ProjectionValidator);
139 aFactory->registerValidator("SketchPlugin_DifferentReference",
140 new SketchPlugin_DifferentReferenceValidator);
141 aFactory->registerValidator("SketchPlugin_DifferentPointReference",
142 new SketchPlugin_DifferentPointReferenceValidator);
143 aFactory->registerValidator("SketchPlugin_CirclePassedPointValidator",
144 new SketchPlugin_CirclePassedPointValidator);
145 aFactory->registerValidator("SketchPlugin_ThirdPointValidator",
146 new SketchPlugin_ThirdPointValidator);
147 aFactory->registerValidator("SketchPlugin_ArcEndPointValidator",
148 new SketchPlugin_ArcEndPointValidator);
149 aFactory->registerValidator("SketchPlugin_ArcEndPointIntersectionValidator",
150 new SketchPlugin_ArcEndPointIntersectionValidator);
151 aFactory->registerValidator("SketchPlugin_HasNoConstraint", new SketchPlugin_HasNoConstraint);
152 aFactory->registerValidator("SketchPlugin_ReplicationReference",
153 new SketchPlugin_ReplicationReferenceValidator);
154 aFactory->registerValidator("SketchPlugin_SketchFeatureValidator",
155 new SketchPlugin_SketchFeatureValidator);
156 aFactory->registerValidator("SketchPlugin_MultiRotationAngleValidator",
157 new SketchPlugin_MultiRotationAngleValidator);
158 aFactory->registerValidator("SketchPlugin_BSplineValidator",
159 new SketchPlugin_BSplineValidator);
160 aFactory->registerValidator("SketchPlugin_CurveFittingValidator",
161 new SketchPlugin_CurveFittingValidator);
163 // register this plugin
164 ModelAPI_Session::get()->registerPlugin(this);
166 Config_PropManager::registerProp("Visualization", "sketch_entity_color",
167 "Sketch entity color",
168 Config_Prop::Color, SKETCH_ENTITY_COLOR);
170 Config_PropManager::registerProp("Visualization", "sketch_external_color",
171 "Sketch external entity color",
172 Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
174 Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color",
175 "Sketch auxiliary entity color",
176 Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
178 Config_PropManager::registerProp("Visualization", "sketch_overconstraint_color",
179 "Sketch overconstraint color",
180 Config_Prop::Color, SKETCH_OVERCONSTRAINT_COLOR);
182 Config_PropManager::registerProp("Visualization", "sketch_fully_constrained_color",
183 "Sketch fully constrained color",
184 Config_Prop::Color, SKETCH_FULLY_CONSTRAINED_COLOR);
186 Config_PropManager::registerProp("Visualization", "sketch_line_width",
188 Config_Prop::IntSpin, "3");
190 // register sketcher properties
191 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
192 Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
193 Config_Prop::Color, YZ_PLANE_COLOR);
194 Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
195 Config_Prop::Color, XZ_PLANE_COLOR);
196 Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
197 Config_Prop::Color, XY_PLANE_COLOR);
202 FeaturePtr SketchPlugin_Plugin::createFeature(std::string theFeatureID)
204 if (theFeatureID == SketchPlugin_Sketch::ID()) {
205 return FeaturePtr(new SketchPlugin_Sketch);
206 } else if (theFeatureID == SketchPlugin_Point::ID()) {
207 return FeaturePtr(new SketchPlugin_Point);
208 } else if (theFeatureID == SketchPlugin_IntersectionPoint::ID()) {
209 return FeaturePtr(new SketchPlugin_IntersectionPoint);
210 } else if (theFeatureID == SketchPlugin_Line::ID()) {
211 return FeaturePtr(new SketchPlugin_Line);
212 } else if (theFeatureID == SketchPlugin_Circle::ID()) {
213 return FeaturePtr(new SketchPlugin_Circle);
214 } else if (theFeatureID == SketchPlugin_Arc::ID()) {
215 return FeaturePtr(new SketchPlugin_Arc);
216 } else if (theFeatureID == SketchPlugin_BSpline::ID()) {
217 return FeaturePtr(new SketchPlugin_BSpline);
218 } else if (theFeatureID == SketchPlugin_BSplinePeriodic::ID()) {
219 return FeaturePtr(new SketchPlugin_BSplinePeriodic);
220 } else if (theFeatureID == SketchPlugin_Projection::ID()) {
221 return FeaturePtr(new SketchPlugin_Projection);
222 } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
223 return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
224 } else if (theFeatureID == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
225 return FeaturePtr(new SketchPlugin_ConstraintCoincidenceInternal);
226 } else if (theFeatureID == SketchPlugin_ConstraintCollinear::ID()) {
227 return FeaturePtr(new SketchPlugin_ConstraintCollinear);
228 } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
229 return FeaturePtr(new SketchPlugin_ConstraintDistance);
230 } else if (theFeatureID == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
231 return FeaturePtr(new SketchPlugin_ConstraintDistanceHorizontal);
232 } else if (theFeatureID == SketchPlugin_ConstraintDistanceVertical::ID()) {
233 return FeaturePtr(new SketchPlugin_ConstraintDistanceVertical);
234 } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
235 return FeaturePtr(new SketchPlugin_ConstraintLength);
236 } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
237 return FeaturePtr(new SketchPlugin_ConstraintParallel);
238 } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
239 return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
240 } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
241 return FeaturePtr(new SketchPlugin_ConstraintRadius);
242 } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
243 return FeaturePtr(new SketchPlugin_ConstraintRigid);
244 } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
245 return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
246 } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
247 return FeaturePtr(new SketchPlugin_ConstraintVertical);
248 } else if (theFeatureID == SketchPlugin_ConstraintEqual::ID()) {
249 return FeaturePtr(new SketchPlugin_ConstraintEqual);
250 } else if (theFeatureID == SketchPlugin_ConstraintTangent::ID()) {
251 return FeaturePtr(new SketchPlugin_ConstraintTangent);
252 } else if (theFeatureID == SketchPlugin_ConstraintMiddle::ID()) {
253 return FeaturePtr(new SketchPlugin_ConstraintMiddle);
254 } else if (theFeatureID == SketchPlugin_ConstraintMirror::ID()) {
255 return FeaturePtr(new SketchPlugin_ConstraintMirror);
256 } else if (theFeatureID == SketchPlugin_Fillet::ID()) {
257 return FeaturePtr(new SketchPlugin_Fillet);
258 } else if (theFeatureID == SketchPlugin_MultiTranslation::ID()) {
259 return FeaturePtr(new SketchPlugin_MultiTranslation);
260 } else if (theFeatureID == SketchPlugin_MultiRotation::ID()) {
261 return FeaturePtr(new SketchPlugin_MultiRotation);
262 } else if (theFeatureID == SketchPlugin_ConstraintAngle::ID()) {
263 return FeaturePtr(new SketchPlugin_ConstraintAngle);
264 } else if (theFeatureID == SketchPlugin_Split::ID()) {
265 return FeaturePtr(new SketchPlugin_Split);
266 } else if (theFeatureID == SketchPlugin_Trim::ID()) {
267 return FeaturePtr(new SketchPlugin_Trim);
268 } else if (theFeatureID == SketchPlugin_MacroArc::ID()) {
269 return FeaturePtr(new SketchPlugin_MacroArc);
270 } else if (theFeatureID == SketchPlugin_MacroBSpline::ID()) {
271 return FeaturePtr(new SketchPlugin_MacroBSpline);
272 } else if (theFeatureID == SketchPlugin_MacroBSplinePeriodic::ID()) {
273 return FeaturePtr(new SketchPlugin_MacroBSplinePeriodic);
274 } else if (theFeatureID == SketchPlugin_MacroCircle::ID()) {
275 return FeaturePtr(new SketchPlugin_MacroCircle);
276 } else if (theFeatureID == SketchPlugin_Ellipse::ID()) {
277 return FeaturePtr(new SketchPlugin_Ellipse);
278 } else if (theFeatureID == SketchPlugin_MacroEllipse::ID()) {
279 return FeaturePtr(new SketchPlugin_MacroEllipse);
280 } else if (theFeatureID == SketchPlugin_EllipticArc::ID()) {
281 return FeaturePtr(new SketchPlugin_EllipticArc);
282 } else if (theFeatureID == SketchPlugin_MacroEllipticArc::ID()) {
283 return FeaturePtr(new SketchPlugin_MacroEllipticArc);
284 } else if (theFeatureID == SketchPlugin_CurveFitting::ID()) {
285 return FeaturePtr(new SketchPlugin_CurveFitting);
286 } else if (theFeatureID == SketchPlugin_SketchDrawer::ID()) {
287 return FeaturePtr(new SketchPlugin_SketchDrawer);
288 } else if (theFeatureID == SketchPlugin_SketchCopy::ID()) {
289 return FeaturePtr(new SketchPlugin_SketchCopy);
290 } else if (theFeatureID == SketchPlugin_Offset::ID()) {
291 return FeaturePtr(new SketchPlugin_Offset);
292 }else if (theFeatureID == SketchPlugin_MacroRectangle::ID()) {
293 return FeaturePtr(new SketchPlugin_MacroRectangle);
294 }else if (theFeatureID == SketchPlugin_Rectangle::ID()) {
295 return FeaturePtr(new SketchPlugin_Rectangle);
298 // feature of such kind is not found
303 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
305 const Events_ID kRequestEvent =
306 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
307 if (theMessage->eventID() == kRequestEvent) {
308 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
309 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
310 Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
314 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
315 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
317 const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
318 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
319 new ModelAPI_FeatureStateMessage(kResponseEvent, this));
321 bool aHasSketchPlane = false;
322 std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
323 std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
324 if (aSketchFeature.get()) {
325 std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
327 std::shared_ptr<GeomDataAPI_Dir> aNormal =
328 std::dynamic_pointer_cast<GeomDataAPI_Dir>(
329 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
330 // it is important to check whether the normal attribute is initialized
331 // because it is possible that normal values are filled when the plane is checked on validity
332 aHasSketchPlane = aNormal && aNormal->isInitialized() &&
333 !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
335 aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
336 aMsg->setState(SketchPlugin_IntersectionPoint::ID(), aHasSketchPlane);
337 aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
338 aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
339 aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
340 aMsg->setState(SketchPlugin_BSpline::ID(), aHasSketchPlane);
341 aMsg->setState(SketchPlugin_BSplinePeriodic::ID(), aHasSketchPlane);
342 aMsg->setState(SketchPlugin_Ellipse::ID(), aHasSketchPlane);
343 aMsg->setState(SketchPlugin_EllipticArc::ID(), aHasSketchPlane);
344 aMsg->setState(SketchPlugin_Projection::ID(), aHasSketchPlane);
345 aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
346 aMsg->setState(SketchPlugin_ConstraintCoincidenceInternal::ID(), aHasSketchPlane);
347 aMsg->setState(SketchPlugin_ConstraintCollinear::ID(), aHasSketchPlane);
348 aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
349 aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
350 aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
351 aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
352 aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
353 aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
354 aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
355 aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
356 aMsg->setState(SketchPlugin_ConstraintEqual::ID(), aHasSketchPlane);
357 aMsg->setState(SketchPlugin_ConstraintTangent::ID(), aHasSketchPlane);
358 aMsg->setState(SketchPlugin_ConstraintMiddle::ID(), aHasSketchPlane);
359 aMsg->setState(SketchPlugin_ConstraintMirror::ID(), aHasSketchPlane);
360 aMsg->setState(SketchPlugin_Fillet::ID(), aHasSketchPlane);
361 aMsg->setState(SketchPlugin_ConstraintAngle::ID(), aHasSketchPlane);
362 aMsg->setState(SketchPlugin_MultiRotation::ID(), aHasSketchPlane);
363 aMsg->setState(SketchPlugin_MultiTranslation::ID(), aHasSketchPlane);
364 aMsg->setState(SketchPlugin_Split::ID(), aHasSketchPlane);
365 aMsg->setState(SketchPlugin_Trim::ID(), aHasSketchPlane);
366 aMsg->setState(SketchPlugin_MacroArc::ID(), aHasSketchPlane);
367 aMsg->setState(SketchPlugin_MacroBSpline::ID(), aHasSketchPlane);
368 aMsg->setState(SketchPlugin_MacroBSplinePeriodic::ID(), aHasSketchPlane);
369 aMsg->setState(SketchPlugin_MacroCircle::ID(), aHasSketchPlane);
370 aMsg->setState(SketchPlugin_MacroEllipse::ID(), aHasSketchPlane);
371 aMsg->setState(SketchPlugin_MacroEllipticArc::ID(), aHasSketchPlane);
372 aMsg->setState(SketchPlugin_ConstraintDistanceHorizontal::ID(), aHasSketchPlane);
373 aMsg->setState(SketchPlugin_ConstraintDistanceVertical::ID(), aHasSketchPlane);
374 aMsg->setState(SketchPlugin_CurveFitting::ID(), aHasSketchPlane);
375 aMsg->setState(SketchPlugin_Offset::ID(), aHasSketchPlane);
376 aMsg->setState(SketchPlugin_MacroRectangle::ID(), aHasSketchPlane);
377 aMsg->setState(SketchPlugin_Rectangle::ID(), aHasSketchPlane);
378 // SketchRectangle is a python feature, so its ID is passed just as a string
379 //aMsg->setState("SketchRectangle", aHasSketchPlane);