1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Line.cxx
4 // Created: 27 Mar 2014
5 // Author: Mikhail PONIKAROV
7 #include "SketchPlugin_Line.h"
8 #include "SketchPlugin_Sketch.h"
9 #include "SketchPlugin_ConstraintCoincidence.h"
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_EventReentrantMessage.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_AttributeSelection.h>
15 #include <ModelAPI_AttributeBoolean.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_Session.h>
20 #include <GeomAPI_Pnt.h>
21 #include <GeomAPI_Lin2d.h>
22 #include <GeomAPI_Pnt2d.h>
24 #include <GeomAlgoAPI_EdgeBuilder.h>
25 #include <GeomDataAPI_Point2D.h>
27 SketchPlugin_Line::SketchPlugin_Line()
28 : SketchPlugin_SketchEntity()
31 void SketchPlugin_Line::initAttributes()
33 SketchPlugin_SketchEntity::initAttributes();
34 /// new attributes should be added to end of the feature in order to provide
35 /// correct attribute values in previous saved studies
36 data()->addAttribute(LENGTH_ID(), ModelAPI_AttributeDouble::typeId());
39 void SketchPlugin_Line::initDerivedClassAttributes()
41 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
42 data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
43 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
44 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
47 void SketchPlugin_Line::execute()
49 SketchPlugin_Sketch* aSketch = sketch();
51 // compute a start point in 3D view
52 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
53 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
54 // compute an end point in 3D view
55 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
56 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
57 if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
58 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
59 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
60 //std::cout<<"Execute line "<<aStart->x()<<" "<<aStart->y()<<" "<<aStart->z()<<" - "
61 // <<anEnd->x()<<" "<<anEnd->y()<<" "<<anEnd->z()<<std::endl;
63 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
65 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(
67 aConstr->setShape(anEdge);
68 aConstr->setIsInHistory(false);
71 static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
72 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
73 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(anId, this));
74 aMessage->setCreatedFeature(ModelAPI_Feature::feature(
75 data()->attribute(START_ID())->owner()));
76 Events_Loop::loop()->send(aMessage);
81 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
83 std::shared_ptr<ModelAPI_Data> aData = data();
84 if (!aData->isValid())
87 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
88 (aData->attribute(START_ID()));
89 aPoint1->move(theDeltaX, theDeltaY);
91 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
92 (aData->attribute(END_ID()));
93 aPoint2->move(theDeltaX, theDeltaY);
96 std::string SketchPlugin_Line::processEvent(const std::shared_ptr<Events_Message>& theMessage)
98 std::string aFilledAttributeName;
100 std::shared_ptr<ModelAPI_EventReentrantMessage> aReentrantMessage =
101 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
102 if (aReentrantMessage.get()) {
103 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
105 // Initialize new line with first point equal to end of previous
106 std::shared_ptr<ModelAPI_Data> aSFData = aCreatedFeature->data();
107 std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
108 aSFData->attribute(SketchPlugin_Line::END_ID()));
109 std::shared_ptr<ModelAPI_Data> aNFData = data();
110 std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111 aNFData->attribute(SketchPlugin_Line::START_ID()));
112 aNPoint->setValue(aSPoint->x(), aSPoint->y());
113 SketchPlugin_ConstraintCoincidence::createCoincidenceFeature(sketch(), aSPoint, aNPoint);
115 return aFilledAttributeName;
118 double SketchPlugin_Line::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
122 std::shared_ptr<ModelAPI_Data> aData = data();
123 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
124 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
125 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
126 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
128 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
130 if (false/*projection*/) { // TODO: if it has not been necessary, remove this block
131 std::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
132 aDelta = aResult->distance(thePoint);
134 aDelta = aLin2d.distance(thePoint);
140 const std::string& SketchPlugin_Line::getKind()
142 static std::string MY_KIND = SketchPlugin_Line::ID();
146 bool SketchPlugin_Line::isFixed() {
147 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
150 void SketchPlugin_Line::attributeChanged(const std::string& theID) {
151 // the second condition for unability to move external segments anywhere
152 // isCopy() is checked temporary for case when copied lines stored external id state
153 // to be removed after debug
154 if ((theID == EXTERNAL_ID() || isFixed()) && !isCopy()) {
155 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
157 // empty shape in selection shows that the shape is equal to context
158 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
160 aSelection = anExtRes->shape();
162 // update arguments due to the selection value
163 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
164 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
165 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
166 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
167 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
168 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
169 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
170 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
174 else if (theID == START_ID() || theID == END_ID()) {
179 void SketchPlugin_Line::updateLenghtValue()
181 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
182 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
183 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
184 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
185 if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
186 double aDistance = aStartAttr->pnt()->distance(anEndAttr->pnt());
187 data()->real(LENGTH_ID())->setValue(aDistance);