1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
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_ConstraintCoincidence.h>
10 #include <SketchPlugin_ConstraintDistance.h>
11 #include <SketchPlugin_ConstraintLength.h>
12 #include <SketchPlugin_ConstraintParallel.h>
13 #include <SketchPlugin_ConstraintPerpendicular.h>
14 #include <SketchPlugin_ConstraintRadius.h>
15 #include <SketchPlugin_ConstraintRigid.h>
16 #include <SketchPlugin_Validators.h>
17 #include <SketchPlugin_ResultValidators.h>
19 #include <Events_Loop.h>
20 #include <GeomDataAPI_Dir.h>
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Data.h>
27 #include <Config_PropManager.h>
35 //#define SET_PLANES_COLOR_IN_PREFERENCES
39 // the only created instance of this plugin
40 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
42 SketchPlugin_Plugin::SketchPlugin_Plugin()
44 SessionPtr aMgr = ModelAPI_Session::get();
45 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
46 aFactory->registerValidator("SketchPlugin_DistanceAttr",
47 new SketchPlugin_DistanceAttrValidator);
48 aFactory->registerValidator("SketchPlugin_DifferentObjects",
49 new SketchPlugin_DifferentObjectsValidator);
50 aFactory->registerValidator("SketchPlugin_ResultPoint", new SketchPlugin_ResultPointValidator);
51 aFactory->registerValidator("SketchPlugin_ResultLine", new SketchPlugin_ResultLineValidator);
52 aFactory->registerValidator("SketchPlugin_ResultArc", new SketchPlugin_ResultArcValidator);
54 // register this plugin
55 ModelAPI_Session::get()->registerPlugin(this);
57 Config_PropManager::registerProp("Visualization", "sketch_edge_color", "Sketch edge color",
58 Config_Prop::Color, SKETCH_EDGE_COLOR);
60 Config_PropManager::registerProp("Visualization", "sketch_point_color", "Sketch point color",
61 Config_Prop::Color, SKETCH_POINT_COLOR);
63 Config_PropManager::registerProp("Visualization", "sketch_external_color", "Sketch external edge color",
64 Config_Prop::Color, SKETCH_EXTERNAL_EDGE_COLOR);
66 Config_PropManager::registerProp("Visualization", "sketch_parallel_color", "Sketch constraint color",
67 Config_Prop::Color, SKETCH_CONSTRAINT_COLOR);
68 Config_PropManager::registerProp("Visualization", "sketch_dimension_color", "Sketch dimension color",
69 Config_Prop::Color, SKETCH_DIMENSION_COLOR);
71 // register sketcher properties
72 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
73 Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
74 Config_Prop::Color, YZ_PLANE_COLOR);
75 Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
76 Config_Prop::Color, XZ_PLANE_COLOR);
77 Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
78 Config_Prop::Color, XY_PLANE_COLOR);
82 FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
84 if (theFeatureID == SketchPlugin_Sketch::ID()) {
85 return FeaturePtr(new SketchPlugin_Sketch);
86 } else if (theFeatureID == SketchPlugin_Point::ID()) {
87 return FeaturePtr(new SketchPlugin_Point);
88 } else if (theFeatureID == SketchPlugin_Line::ID()) {
89 return FeaturePtr(new SketchPlugin_Line);
90 } else if (theFeatureID == SketchPlugin_Circle::ID()) {
91 return FeaturePtr(new SketchPlugin_Circle);
92 } else if (theFeatureID == SketchPlugin_Arc::ID()) {
93 return FeaturePtr(new SketchPlugin_Arc);
94 } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
95 return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
96 } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
97 return FeaturePtr(new SketchPlugin_ConstraintDistance);
98 } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
99 return FeaturePtr(new SketchPlugin_ConstraintLength);
100 } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
101 return FeaturePtr(new SketchPlugin_ConstraintParallel);
102 } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
103 return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
104 } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
105 return FeaturePtr(new SketchPlugin_ConstraintRadius);
106 } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
107 return FeaturePtr(new SketchPlugin_ConstraintRigid);
109 // feature of such kind is not found
113 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
115 const Events_ID kRequestEvent =
116 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
117 if (theMessage->eventID() == kRequestEvent) {
118 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
119 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
120 Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
124 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
125 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
127 const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
128 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
129 std::make_shared<ModelAPI_FeatureStateMessage>(kResponseEvent, this);
131 bool aHasSketchPlane = false;
132 std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
133 std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
134 if (aSketchFeature.get()) {
135 std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
137 std::shared_ptr<GeomDataAPI_Dir> aNormal =
138 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
139 aHasSketchPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
141 aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
142 aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
143 aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
144 aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
145 aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
146 aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
147 aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
148 aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
149 aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
150 aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
151 aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);