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>
37 // the only created instance of this plugin
38 static SketchPlugin_Plugin* MY_SKETCH_INSTANCE = new SketchPlugin_Plugin();
40 SketchPlugin_Plugin::SketchPlugin_Plugin()
42 SessionPtr aMgr = ModelAPI_Session::get();
43 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
44 aFactory->registerValidator("SketchPlugin_DistanceAttr",
45 new SketchPlugin_DistanceAttrValidator);
46 aFactory->registerValidator("SketchPlugin_DifferentObjects",
47 new SketchPlugin_DifferentObjectsValidator);
48 aFactory->registerValidator("SketchPlugin_ResultPoint", new SketchPlugin_ResultPointValidator);
49 aFactory->registerValidator("SketchPlugin_ResultLine", new SketchPlugin_ResultLineValidator);
50 aFactory->registerValidator("SketchPlugin_ResultArc", new SketchPlugin_ResultArcValidator);
52 // register this plugin
53 ModelAPI_Session::get()->registerPlugin(this);
55 // register sketcher properties
57 Config_PropManager::registerProp("Visualization", "parallel_color", "Parallel constraint color",
58 Config_Prop::Color, PARALLEL_COLOR);
59 Config_PropManager::registerProp("Visualization", "perpendicular_color",
60 "Perpendicular constraint color", Config_Prop::Color,
62 Config_PropManager::registerProp("Visualization", "distance_color", "Distance color",
63 Config_Prop::Color, DISTANCE_COLOR);
64 Config_PropManager::registerProp("Visualization", "length_color", "Length color",
65 Config_Prop::Color, LENGTH_COLOR);
66 Config_PropManager::registerProp("Visualization", "radius_color", "Radius color",
67 Config_Prop::Color, RADIUS_COLOR);
68 Config_PropManager::registerProp("Visualization", "fixing_color", "Fixing color",
69 Config_Prop::Color, FIXING_COLOR);
72 FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
74 if (theFeatureID == SketchPlugin_Sketch::ID()) {
75 return FeaturePtr(new SketchPlugin_Sketch);
76 } else if (theFeatureID == SketchPlugin_Point::ID()) {
77 return FeaturePtr(new SketchPlugin_Point);
78 } else if (theFeatureID == SketchPlugin_Line::ID()) {
79 return FeaturePtr(new SketchPlugin_Line);
80 } else if (theFeatureID == SketchPlugin_Circle::ID()) {
81 return FeaturePtr(new SketchPlugin_Circle);
82 } else if (theFeatureID == SketchPlugin_Arc::ID()) {
83 return FeaturePtr(new SketchPlugin_Arc);
84 } else if (theFeatureID == SketchPlugin_ConstraintCoincidence::ID()) {
85 return FeaturePtr(new SketchPlugin_ConstraintCoincidence);
86 } else if (theFeatureID == SketchPlugin_ConstraintDistance::ID()) {
87 return FeaturePtr(new SketchPlugin_ConstraintDistance);
88 } else if (theFeatureID == SketchPlugin_ConstraintLength::ID()) {
89 return FeaturePtr(new SketchPlugin_ConstraintLength);
90 } else if (theFeatureID == SketchPlugin_ConstraintParallel::ID()) {
91 return FeaturePtr(new SketchPlugin_ConstraintParallel);
92 } else if (theFeatureID == SketchPlugin_ConstraintPerpendicular::ID()) {
93 return FeaturePtr(new SketchPlugin_ConstraintPerpendicular);
94 } else if (theFeatureID == SketchPlugin_ConstraintRadius::ID()) {
95 return FeaturePtr(new SketchPlugin_ConstraintRadius);
96 } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) {
97 return FeaturePtr(new SketchPlugin_ConstraintRigid);
99 // feature of such kind is not found
103 void SketchPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
105 const Events_ID kRequestEvent =
106 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
107 if (theMessage->eventID() == kRequestEvent) {
108 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
109 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
110 Events_Loop::loop()->send(getFeaturesState(aStateMessage->feature()), false);
114 std::shared_ptr<ModelAPI_FeatureStateMessage> SketchPlugin_Plugin
115 ::getFeaturesState(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
117 const Events_ID kResponseEvent = Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
118 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
119 std::make_shared<ModelAPI_FeatureStateMessage>(kResponseEvent, this);
121 bool aHasSketchPlane = false;
122 std::shared_ptr<SketchPlugin_Sketch> aSketchFeature =
123 std::dynamic_pointer_cast<SketchPlugin_Sketch>(theFeature);
124 if (aSketchFeature.get()) {
125 std::shared_ptr<ModelAPI_Data> aData = aSketchFeature->data();
127 std::shared_ptr<GeomDataAPI_Dir> aNormal =
128 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
129 aHasSketchPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
131 aMsg->setState(SketchPlugin_Point::ID(), aHasSketchPlane);
132 aMsg->setState(SketchPlugin_Line::ID(), aHasSketchPlane);
133 aMsg->setState(SketchPlugin_Circle::ID(), aHasSketchPlane);
134 aMsg->setState(SketchPlugin_Arc::ID(), aHasSketchPlane);
135 aMsg->setState(SketchPlugin_ConstraintCoincidence::ID(), aHasSketchPlane);
136 aMsg->setState(SketchPlugin_ConstraintDistance::ID(), aHasSketchPlane);
137 aMsg->setState(SketchPlugin_ConstraintLength::ID(), aHasSketchPlane);
138 aMsg->setState(SketchPlugin_ConstraintParallel::ID(), aHasSketchPlane);
139 aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane);
140 aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane);
141 aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane);