1 // Copyright (C) 2014-2019 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_Arc.h>
27 #include <SketchPlugin_Projection.h>
28 #include <SketchPlugin_ConstraintAngle.h>
29 #include <SketchPlugin_ConstraintCoincidence.h>
30 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
31 #include <SketchPlugin_ConstraintCollinear.h>
32 #include <SketchPlugin_ConstraintDistance.h>
33 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
34 #include <SketchPlugin_ConstraintDistanceVertical.h>
35 #include <SketchPlugin_ConstraintEqual.h>
36 #include <SketchPlugin_Fillet.h>
37 #include <SketchPlugin_ConstraintHorizontal.h>
38 #include <SketchPlugin_ConstraintLength.h>
39 #include <SketchPlugin_ConstraintMiddle.h>
40 #include <SketchPlugin_ConstraintMirror.h>
41 #include <SketchPlugin_ConstraintParallel.h>
42 #include <SketchPlugin_ConstraintPerpendicular.h>
43 #include <SketchPlugin_ConstraintRadius.h>
44 #include <SketchPlugin_ConstraintRigid.h>
45 #include <SketchPlugin_ConstraintTangent.h>
46 #include <SketchPlugin_ConstraintVertical.h>
47 #include <SketchPlugin_MacroArc.h>
48 #include <SketchPlugin_MacroCircle.h>
49 #include <SketchPlugin_MultiRotation.h>
50 #include <SketchPlugin_MultiTranslation.h>
51 #include <SketchPlugin_Trim.h>
52 #include <SketchPlugin_Split.h>
53 #include <SketchPlugin_Validators.h>
54 #include <SketchPlugin_ExternalValidator.h>
55 #include <SketchPlugin_Ellipse.h>
56 #include <SketchPlugin_MacroEllipse.h>
57 #include <SketchPlugin_EllipticArc.h>
58 #include <SketchPlugin_MacroEllipticArc.h>
59 #include <SketchPlugin_SketchDrawer.h>
61 #include <SketcherPrs_Tools.h>
63 #include <Events_Loop.h>
64 #include <GeomDataAPI_Dir.h>
66 #include <ModelAPI_Session.h>
67 #include <ModelAPI_Document.h>
68 #include <ModelAPI_Validator.h>
69 #include <ModelAPI_Data.h>
71 #include <Config_PropManager.h>
80 #define SKETCH_ENTITY_COLOR "225,0,0"
81 #define SKETCH_EXTERNAL_COLOR "170,0,225"
82 #define SKETCH_AUXILIARY_COLOR "0,85,0"
83 #define SKETCH_OVERCONSTRAINT_COLOR "255,0,0"
84 #define SKETCH_FULLY_CONSTRAINED_COLOR "0,150,0"
86 //#define SET_PLANES_COLOR_IN_PREFERENCES
88 // the only created instance of this plugin
89 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
91 SketchPlugin_Plugin::SketchPlugin_Plugin()
93 SessionPtr aMgr = ModelAPI_Session::get();
94 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
95 aFactory->registerValidator("SketchPlugin_DistanceAttr",
96 new SketchPlugin_DistanceAttrValidator);
97 aFactory->registerValidator("SketchPlugin_ExternalValidator",
98 new SketchPlugin_ExternalValidator);
99 aFactory->registerValidator("SketchPlugin_TangentAttr",
100 new SketchPlugin_TangentAttrValidator);
101 aFactory->registerValidator("SketchPlugin_PerpendicularAttr",
102 new SketchPlugin_PerpendicularAttrValidator);
103 aFactory->registerValidator("SketchPlugin_NotFixed",
104 new SketchPlugin_NotFixedValidator);
105 aFactory->registerValidator("SketchPlugin_EqualAttr",
106 new SketchPlugin_EqualAttrValidator);
107 aFactory->registerValidator("SketchPlugin_MirrorAttr",
108 new SketchPlugin_MirrorAttrValidator);
109 aFactory->registerValidator("SketchPlugin_CoincidenceAttr",
110 new SketchPlugin_CoincidenceAttrValidator);
111 aFactory->registerValidator("SketchPlugin_CopyValidator",
112 new SketchPlugin_CopyValidator);
113 aFactory->registerValidator("SketchPlugin_SolverErrorValidator",
114 new SketchPlugin_SolverErrorValidator);
115 aFactory->registerValidator("SketchPlugin_FilletVertexValidator",
116 new SketchPlugin_FilletVertexValidator);
117 aFactory->registerValidator("SketchPlugin_SplitValidator",
118 new SketchPlugin_SplitValidator);
119 aFactory->registerValidator("SketchPlugin_TrimValidator",
120 new SketchPlugin_TrimValidator);
121 aFactory->registerValidator("SketchPlugin_MiddlePointAttr",
122 new SketchPlugin_MiddlePointAttrValidator);
123 aFactory->registerValidator("SketchPlugin_ArcTangentPoint",
124 new SketchPlugin_ArcTangentPointValidator);
125 aFactory->registerValidator("SketchPlugin_ArcTransversalPoint",
126 new SketchPlugin_ArcTransversalPointValidator);
127 aFactory->registerValidator("SketchPlugin_IntersectionValidator",
128 new SketchPlugin_IntersectionValidator);
129 aFactory->registerValidator("SketchPlugin_ProjectionValidator",
130 new SketchPlugin_ProjectionValidator);
131 aFactory->registerValidator("SketchPlugin_DifferentReference",
132 new SketchPlugin_DifferentReferenceValidator);
133 aFactory->registerValidator("SketchPlugin_DifferentPointReference",
134 new SketchPlugin_DifferentPointReferenceValidator);
135 aFactory->registerValidator("SketchPlugin_CirclePassedPointValidator",
136 new SketchPlugin_CirclePassedPointValidator);
137 aFactory->registerValidator("SketchPlugin_ThirdPointValidator",
138 new SketchPlugin_ThirdPointValidator);
139 aFactory->registerValidator("SketchPlugin_ArcEndPointValidator",
140 new SketchPlugin_ArcEndPointValidator);
141 aFactory->registerValidator("SketchPlugin_ArcEndPointIntersectionValidator",
142 new SketchPlugin_ArcEndPointIntersectionValidator);
143 aFactory->registerValidator("SketchPlugin_HasNoConstraint", new SketchPlugin_HasNoConstraint);
144 aFactory->registerValidator("SketchPlugin_ReplicationReference",
145 new SketchPlugin_ReplicationReferenceValidator);
146 aFactory->registerValidator("SketchPlugin_SketchFeatureValidator",
147 new SketchPlugin_SketchFeatureValidator);
148 aFactory->registerValidator("SketchPlugin_MultiRotationAngleValidator",
149 new SketchPlugin_MultiRotationAngleValidator);
151 // register this plugin
152 ModelAPI_Session::get()->registerPlugin(this);
154 Config_PropManager::registerProp("Visualization", "sketch_entity_color",
155 "Sketch entity color",
156 Config_Prop::Color, SKETCH_ENTITY_COLOR);
158 Config_PropManager::registerProp("Visualization", "sketch_external_color",
159 "Sketch external entity color",
160 Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
162 Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color",
163 "Sketch auxiliary entity color",
164 Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
166 Config_PropManager::registerProp("Visualization", "sketch_overconstraint_color",
167 "Sketch overconstraint color",
168 Config_Prop::Color, SKETCH_OVERCONSTRAINT_COLOR);
170 Config_PropManager::registerProp("Visualization", "sketch_fully_constrained_color",
171 "Sketch fully constrained color",
172 Config_Prop::Color, SKETCH_FULLY_CONSTRAINED_COLOR);
174 // register sketcher properties
175 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
176 Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
177 Config_Prop::Color, YZ_PLANE_COLOR);
178 Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
179 Config_Prop::Color, XZ_PLANE_COLOR);
180 Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
181 Config_Prop::Color, XY_PLANE_COLOR);
186 FeaturePtr SketchPlugin_Plugin::createFeature(std::string theFeatureID)
188 if (theFeatureID == SketchPlugin_Sketch::ID()) {
189 return FeaturePtr(new SketchPlugin_Sketch);
190 } else if (theFeatureID == SketchPlugin_Point::ID()) {
191 return FeaturePtr(new SketchPlugin_Point);
192 } else if (theFeatureID == SketchPlugin_IntersectionPoint::ID()) {
193 return FeaturePtr(new SketchPlugin_IntersectionPoint);
194 } else if (theFeatureID == SketchPlugin_Line::ID()) {
195 return FeaturePtr(new SketchPlugin_Line);
196 } else if (theFeatureID == SketchPlugin_Circle::ID()) {
197 return FeaturePtr(new SketchPlugin_Circle);
198 } else if (theFeatureID == SketchPlugin_Arc::ID()) {
199 return FeaturePtr(new SketchPlugin_Arc);
200 } else if (theFeatureID == SketchPlugin_Projection::ID()) {
201 return FeaturePtr(new SketchPlugin_Projection);
202 } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
203 return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
204 } else if (theFeatureID == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
205 return FeaturePtr(new SketchPlugin_ConstraintCoincidenceInternal);
206 } else if (theFeatureID == SketchPlugin_ConstraintCollinear::ID()) {
207 return FeaturePtr(new SketchPlugin_ConstraintCollinear);
208 } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
209 return FeaturePtr(new SketchPlugin_ConstraintDistance);
210 } else if (theFeatureID == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
211 return FeaturePtr(new SketchPlugin_ConstraintDistanceHorizontal);
212 } else if (theFeatureID == SketchPlugin_ConstraintDistanceVertical::ID()) {
213 return FeaturePtr(new SketchPlugin_ConstraintDistanceVertical);
214 } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
215 return FeaturePtr(new SketchPlugin_ConstraintLength);
216 } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
217 return FeaturePtr(new SketchPlugin_ConstraintParallel);
218 } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
219 return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
220 } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
221 return FeaturePtr(new SketchPlugin_ConstraintRadius);
222 } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
223 return FeaturePtr(new SketchPlugin_ConstraintRigid);
224 } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
225 return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
226 } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
227 return FeaturePtr(new SketchPlugin_ConstraintVertical);
228 } else if (theFeatureID == SketchPlugin_ConstraintEqual::ID()) {
229 return FeaturePtr(new SketchPlugin_ConstraintEqual);
230 } else if (theFeatureID == SketchPlugin_ConstraintTangent::ID()) {
231 return FeaturePtr(new SketchPlugin_ConstraintTangent);
232 } else if (theFeatureID == SketchPlugin_ConstraintMiddle::ID()) {
233 return FeaturePtr(new SketchPlugin_ConstraintMiddle);
234 } else if (theFeatureID == SketchPlugin_ConstraintMirror::ID()) {
235 return FeaturePtr(new SketchPlugin_ConstraintMirror);
236 } else if (theFeatureID == SketchPlugin_Fillet::ID()) {
237 return FeaturePtr(new SketchPlugin_Fillet);
238 } else if (theFeatureID == SketchPlugin_MultiTranslation::ID()) {
239 return FeaturePtr(new SketchPlugin_MultiTranslation);
240 } else if (theFeatureID == SketchPlugin_MultiRotation::ID()) {
241 return FeaturePtr(new SketchPlugin_MultiRotation);
242 } else if (theFeatureID == SketchPlugin_ConstraintAngle::ID()) {
243 return FeaturePtr(new SketchPlugin_ConstraintAngle);
244 } else if (theFeatureID == SketchPlugin_Split::ID()) {
245 return FeaturePtr(new SketchPlugin_Split);
246 } else if (theFeatureID == SketchPlugin_Trim::ID()) {
247 return FeaturePtr(new SketchPlugin_Trim);
248 } else if (theFeatureID == SketchPlugin_MacroArc::ID()) {
249 return FeaturePtr(new SketchPlugin_MacroArc);
250 } else if (theFeatureID == SketchPlugin_MacroCircle::ID()) {
251 return FeaturePtr(new SketchPlugin_MacroCircle);
252 } else if (theFeatureID == SketchPlugin_Ellipse::ID()) {
253 return FeaturePtr(new SketchPlugin_Ellipse);
254 } else if (theFeatureID == SketchPlugin_MacroEllipse::ID()) {
255 return FeaturePtr(new SketchPlugin_MacroEllipse);
256 } else if (theFeatureID == SketchPlugin_EllipticArc::ID()) {
257 return FeaturePtr(new SketchPlugin_EllipticArc);
258 } else if (theFeatureID == SketchPlugin_MacroEllipticArc::ID()) {
259 return FeaturePtr(new SketchPlugin_MacroEllipticArc);
260 } else if (theFeatureID == SketchPlugin_SketchDrawer::ID()) {
261 return FeaturePtr(new SketchPlugin_SketchDrawer);
263 // feature of such kind is not found
268 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
270 const Events_ID kRequestEvent =
271 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
272 if (theMessage->eventID() == kRequestEvent) {
273 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
274 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
275 Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
279 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
280 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
282 const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
283 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
284 new ModelAPI_FeatureStateMessage(kResponseEvent, this));
286 bool aHasSketchPlane = false;
287 std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
288 std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
289 if (aSketchFeature.get()) {
290 std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
292 std::shared_ptr<GeomDataAPI_Dir> aNormal =
293 std::dynamic_pointer_cast<GeomDataAPI_Dir>(
294 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
295 // it is important to check whether the normal attribute is initialized
296 // because it is possible that normal values are filled when the plane is checked on validity
297 aHasSketchPlane = aNormal && aNormal->isInitialized() &&
298 !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
300 aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
301 aMsg->setState(SketchPlugin_IntersectionPoint::ID(), aHasSketchPlane);
302 aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
303 aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
304 aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
305 aMsg->setState(SketchPlugin_Ellipse::ID(), aHasSketchPlane);
306 aMsg->setState(SketchPlugin_EllipticArc::ID(), aHasSketchPlane);
307 aMsg->setState(SketchPlugin_Projection::ID(), aHasSketchPlane);
308 aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
309 aMsg->setState(SketchPlugin_ConstraintCoincidenceInternal::ID(), aHasSketchPlane);
310 aMsg->setState(SketchPlugin_ConstraintCollinear::ID(), aHasSketchPlane);
311 aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
312 aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
313 aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
314 aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
315 aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
316 aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
317 aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
318 aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
319 aMsg->setState(SketchPlugin_ConstraintEqual::ID(), aHasSketchPlane);
320 aMsg->setState(SketchPlugin_ConstraintTangent::ID(), aHasSketchPlane);
321 aMsg->setState(SketchPlugin_ConstraintMiddle::ID(), aHasSketchPlane);
322 aMsg->setState(SketchPlugin_ConstraintMirror::ID(), aHasSketchPlane);
323 aMsg->setState(SketchPlugin_Fillet::ID(), aHasSketchPlane);
324 aMsg->setState(SketchPlugin_ConstraintAngle::ID(), aHasSketchPlane);
325 aMsg->setState(SketchPlugin_MultiRotation::ID(), aHasSketchPlane);
326 aMsg->setState(SketchPlugin_MultiTranslation::ID(), aHasSketchPlane);
327 aMsg->setState(SketchPlugin_Split::ID(), aHasSketchPlane);
328 aMsg->setState(SketchPlugin_Trim::ID(), aHasSketchPlane);
329 aMsg->setState(SketchPlugin_MacroArc::ID(), aHasSketchPlane);
330 aMsg->setState(SketchPlugin_MacroCircle::ID(), aHasSketchPlane);
331 aMsg->setState(SketchPlugin_MacroEllipse::ID(), aHasSketchPlane);
332 aMsg->setState(SketchPlugin_MacroEllipticArc::ID(), aHasSketchPlane);
333 aMsg->setState(SketchPlugin_ConstraintDistanceHorizontal::ID(), aHasSketchPlane);
334 aMsg->setState(SketchPlugin_ConstraintDistanceVertical::ID(), aHasSketchPlane);
335 // SketchRectangle is a python feature, so its ID is passed just as a string
336 aMsg->setState("SketchRectangle", aHasSketchPlane);