1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Tools.cpp
4 // Created: 07 July 2015
5 // Author: Sergey POKHODENKO
7 #include "SketchPlugin_Tools.h"
9 #include <GeomDataAPI_Point.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <SketcherPrs_Tools.h>
13 #include <SketchPlugin_ConstraintCoincidence.h>
14 #include <SketchPlugin_SketchEntity.h>
16 namespace SketchPlugin_Tools {
18 void clearExpressions(AttributeDoublePtr theAttribute)
20 theAttribute->setText(std::string());
23 void clearExpressions(AttributePointPtr theAttribute)
25 theAttribute->setText(std::string(), std::string(), std::string());
28 void clearExpressions(AttributePoint2DPtr theAttribute)
30 theAttribute->setText(std::string(), std::string());
33 void clearExpressions(AttributePtr theAttribute)
36 AttributeDoublePtr anAttributeDouble =
37 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
38 if (anAttributeDouble.get())
39 clearExpressions(anAttributeDouble);
41 AttributePointPtr anAttributePoint =
42 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
43 if (anAttributePoint.get())
44 clearExpressions(anAttributePoint);
46 AttributePoint2DPtr anAttributePoint2D =
47 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
48 if (anAttributePoint2D.get())
49 clearExpressions(anAttributePoint2D);
52 void clearExpressions(FeaturePtr theFeature)
54 if (!theFeature.get())
57 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
58 std::list<AttributePtr>::iterator anAttributeIt = anAttributes.begin();
59 for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
60 clearExpressions(*anAttributeIt);
64 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(const FeaturePtr theStartCoin)
66 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
67 SketchPlugin_Constraint::ENTITY_A());
68 if (aPnt.get() == NULL)
69 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
73 void findCoincidences(const FeaturePtr theStartCoin,
74 const std::string& theAttr,
75 std::set<FeaturePtr>& theList)
77 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
81 FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
82 if(theList.find(aObj) == theList.end()) {
83 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincidencePoint(theStartCoin);
84 if(aOrig.get() == NULL) {
88 const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
89 std::set<AttributePtr>::const_iterator aIt;
90 for(aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
91 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
92 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
93 if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
94 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
95 if(aPnt.get() && aOrig->isEqual(aPnt)) {
96 findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(), theList);
97 findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(), theList);
104 void updateMultiAttribute(const AttributePtr& theFirstAngleAttribute,
105 const AttributePtr& theSecondAngleAttribute,
107 const bool toMultiply)
109 if (theValue == 0 || !theFirstAngleAttribute->isInitialized())
112 AttributeDoublePtr aDoubleFirstAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
113 theFirstAngleAttribute);
114 double aValue = aDoubleFirstAttr->value();
116 AttributeDoublePtr aDoubleSecondAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
117 theSecondAngleAttribute);
119 aDoubleSecondAttr->setValue(aValue*theValue);
121 aDoubleSecondAttr->setValue(aValue/theValue);
124 void updateMultiAttribute(const AttributePtr& theFirstAttribute,
125 const AttributePtr& theSecondAttribute,
126 const AttributePtr& theModifiedAttribute,
128 const bool toMultiply)
130 if (theValue == 0 || !theFirstAttribute->isInitialized()
131 || !theSecondAttribute->isInitialized())
134 std::shared_ptr<GeomDataAPI_Point2D> aFirstPoint =
135 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFirstAttribute);
136 std::shared_ptr<GeomDataAPI_Point2D> aSecondPoint =
137 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theSecondAttribute);
138 std::shared_ptr<GeomDataAPI_Point2D> aModifiedPoint =
139 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theModifiedAttribute);
141 if (!aFirstPoint.get() || !aSecondPoint.get() || !aModifiedPoint.get())
144 if (aFirstPoint->pnt()->isEqual(aSecondPoint->pnt()))
145 aModifiedPoint->setValue(aFirstPoint->pnt());
147 double aDx = aSecondPoint->x() - aFirstPoint->x();
148 double aDy = aSecondPoint->y() - aFirstPoint->y();
150 double aX = toMultiply ? aDx * theValue : aDx / theValue;
151 double anY = toMultiply ? aDy * theValue : aDy / theValue;
153 aModifiedPoint->setValue(aFirstPoint->x() + aX, aFirstPoint->y() + anY);
157 } // namespace SketchPlugin_Tools