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_ConstraintEqual.h>
12 #include <SketchPlugin_ConstraintFillet.h>
13 #include <SketchPlugin_ConstraintHorizontal.h>
14 #include <SketchPlugin_ConstraintLength.h>
15 #include <SketchPlugin_ConstraintMirror.h>
16 #include <SketchPlugin_ConstraintParallel.h>
17 #include <SketchPlugin_ConstraintPerpendicular.h>
18 #include <SketchPlugin_ConstraintRadius.h>
19 #include <SketchPlugin_ConstraintRigid.h>
20 #include <SketchPlugin_ConstraintTangent.h>
21 #include <SketchPlugin_ConstraintVertical.h>
22 #include <SketchPlugin_Validators.h>
23 #include <SketchPlugin_ExternalValidator.h>
25 #include <Events_Loop.h>
26 #include <GeomDataAPI_Dir.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Document.h>
30 #include <ModelAPI_Validator.h>
31 #include <ModelAPI_Data.h>
33 #include <Config_PropManager.h>
41 //#define SET_PLANES_COLOR_IN_PREFERENCES
45 // the only created instance of this plugin
46 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
48 SketchPlugin_Plugin::SketchPlugin_Plugin()
50 SessionPtr aMgr = ModelAPI_Session::get();
51 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
52 aFactory->registerValidator("SketchPlugin_DistanceAttr",
53 new SketchPlugin_DistanceAttrValidator);
54 aFactory->registerValidator("SketchPlugin_ExternalValidator",
55 new SketchPlugin_ExternalValidator);
56 aFactory->registerValidator("SketchPlugin_TangentAttr",
57 new SketchPlugin_TangentAttrValidator);
58 aFactory->registerValidator("SketchPlugin_NotFixed",
59 new SketchPlugin_NotFixedValidator);
60 aFactory->registerValidator("SketchPlugin_EqualAttr",
61 new SketchPlugin_EqualAttrValidator);
62 aFactory->registerValidator("SketchPlugin_MirrorAttr",
63 new SketchPlugin_MirrorAttrValidator);
64 aFactory->registerValidator("SketchPlugin_CoincidenceAttr",
65 new SketchPlugin_CoincidenceAttrValidator);
67 // register this plugin
68 ModelAPI_Session::get()->registerPlugin(this);
70 Config_PropManager::registerProp("Visualization", "sketch_entity_color", "Sketch enity color",
71 Config_Prop::Color, SKETCH_ENTITY_COLOR);
73 Config_PropManager::registerProp("Visualization", "sketch_external_color", "Sketch external entity color",
74 Config_Prop::Color, SKETCH_EXTERNAL_COLOR);
76 Config_PropManager::registerProp("Visualization", "sketch_auxiliary_color", "Sketch auxiliary entity color",
77 Config_Prop::Color, SKETCH_AUXILIARY_COLOR);
79 Config_PropManager::registerProp("Visualization", "sketch_constraint_color", "Sketch constraint color",
80 Config_Prop::Color, SKETCH_CONSTRAINT_COLOR);
82 Config_PropManager::registerProp("Visualization", "sketch_dimension_color", "Sketch dimension color",
83 Config_Prop::Color, SKETCH_DIMENSION_COLOR);
85 // register sketcher properties
86 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
87 Config_PropManager::registerProp("Visualization", "yz_plane_color", "YZ plane color",
88 Config_Prop::Color, YZ_PLANE_COLOR);
89 Config_PropManager::registerProp("Visualization", "xz_plane_color", "XZ plane color",
90 Config_Prop::Color, XZ_PLANE_COLOR);
91 Config_PropManager::registerProp("Visualization", "xy_plane_color", "XY plane color",
92 Config_Prop::Color, XY_PLANE_COLOR);
96 FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
98 if (theFeatureID == SketchPlugin_Sketch::ID()) {
99 return FeaturePtr(new SketchPlugin_Sketch);
100 } else if (theFeatureID == SketchPlugin_Point::ID()) {
101 return FeaturePtr(new SketchPlugin_Point);
102 } else if (theFeatureID == SketchPlugin_Line::ID()) {
103 return FeaturePtr(new SketchPlugin_Line);
104 } else if (theFeatureID == SketchPlugin_Circle::ID()) {
105 return FeaturePtr(new SketchPlugin_Circle);
106 } else if (theFeatureID == SketchPlugin_Arc::ID()) {
107 return FeaturePtr(new SketchPlugin_Arc);
108 } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
109 return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
110 } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
111 return FeaturePtr(new SketchPlugin_ConstraintDistance);
112 } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
113 return FeaturePtr(new SketchPlugin_ConstraintLength);
114 } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
115 return FeaturePtr(new SketchPlugin_ConstraintParallel);
116 } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
117 return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
118 } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
119 return FeaturePtr(new SketchPlugin_ConstraintRadius);
120 } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
121 return FeaturePtr(new SketchPlugin_ConstraintRigid);
122 } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) {
123 return FeaturePtr(new SketchPlugin_ConstraintHorizontal);
124 } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) {
125 return FeaturePtr(new SketchPlugin_ConstraintVertical);
126 } else if (theFeatureID == SketchPlugin_ConstraintEqual::ID()) {
127 return FeaturePtr(new SketchPlugin_ConstraintEqual);
128 } else if (theFeatureID == SketchPlugin_ConstraintTangent::ID()) {
129 return FeaturePtr(new SketchPlugin_ConstraintTangent);
130 } else if (theFeatureID == SketchPlugin_ConstraintMirror::ID()) {
131 return FeaturePtr(new SketchPlugin_ConstraintMirror);
132 } else if (theFeatureID == SketchPlugin_ConstraintFillet::ID()) {
133 return FeaturePtr(new SketchPlugin_ConstraintFillet);
135 // feature of such kind is not found
139 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
141 const Events_ID kRequestEvent =
142 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
143 if (theMessage->eventID() == kRequestEvent) {
144 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
145 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
146 Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
150 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
151 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
153 const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
154 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
155 std::make_shared<ModelAPI_FeatureStateMessage>(kResponseEvent, this);
157 bool aHasSketchPlane = false;
158 std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
159 std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
160 if (aSketchFeature.get()) {
161 std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
163 std::shared_ptr<GeomDataAPI_Dir> aNormal =
164 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
165 aHasSketchPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
167 aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
168 aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
169 aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
170 aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
171 aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
172 aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
173 aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
174 aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
175 aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
176 aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
177 aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);
178 aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane);
179 aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane);
180 aMsg->setState(SketchPlugin_ConstraintEqual::ID(), aHasSketchPlane);
181 aMsg->setState(SketchPlugin_ConstraintTangent::ID(), aHasSketchPlane);
182 aMsg->setState(SketchPlugin_ConstraintMirror::ID(), aHasSketchPlane);
183 aMsg->setState(SketchPlugin_ConstraintFillet::ID(), aHasSketchPlane);