if (anAttr) {
myAttrs[theID] = std::shared_ptr<ModelAPI_Attribute>(anAttr);
anAttr->setObject(myObject);
+ anAttr->setID(theID);
} else {
Events_Error::send("Can not create unknown type of attribute " + theAttrType);
}
static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
if (myObject) {
- myObject->attributeChanged();
+ myObject->attributeChanged(theAttr->id());
}
}
}
{
///< needed here to emit signal that feature changed on change of the attribute
std::shared_ptr<ModelAPI_Object> myObject;
+ std::string myID; ///< identifier of this attribute in Data class
protected:
// accessible from the attributes
- bool myIsInitialized;
- bool myIsArgument;
- bool myIsImmutable;
+ bool myIsInitialized; ///< is some value assigned to this attribute
+ bool myIsArgument; ///< is this attribute used as an argument for execution
+ bool myIsImmutable; ///< is this attribute can be changed programmatically (e.g. by constraint)
public:
return myIsImmutable;
}
+ /// ID of the attribute in Data
+ MODELAPI_EXPORT const std::string& id() const
+ {
+ return myID;
+ }
+
protected:
/// Objects are created for features automatically
ModelAPI_Attribute()
myIsImmutable = false;
}
+ /// Sets the ID of the attribute in Data (called from Data)
+ MODELAPI_EXPORT void setID(const std::string theID)
+ {
+ myID = theID;
+ }
+
+ friend class Model_Data;
};
//! Pointer on attribute object
virtual std::string groupName() = 0;
/// Called on change of any argument-attribute of this object
- MODELAPI_EXPORT virtual void attributeChanged()
+ /// \param theID identifier of changed attribute
+ MODELAPI_EXPORT virtual void attributeChanged(const std::string& theID)
{}
/// To use virtuality for destructors
return data()->selection(EXTERNAL_ID())->context();
}
-void SketchPlugin_Circle::attributeChanged() {
- static bool myIsUpdated = false; // to avoid infinitive cycle on attrubtes change
- std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
- // update arguments due to the selection value
- if (aSelection && !aSelection->isNull() && aSelection->isEdge() && !myIsUpdated) {
- myIsUpdated = true;
- std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
- std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
- std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
- std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
- aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
- real(RADIUS_ID())->setValue(aCirc->radius());
- myIsUpdated = false;
+void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
+ if (theID == EXTERNAL_ID()) {
+ std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
+ // update arguments due to the selection value
+ if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
+ std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
+ std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
+ aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
+ real(RADIUS_ID())->setValue(aCirc->radius());
+ }
}
}
virtual double distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
/// Called on change of any argument-attribute of this object
- SKETCHPLUGIN_EXPORT virtual void attributeChanged();
+ SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
/// Use plugin manager for features creation
SketchPlugin_Circle();
// value calculation
std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
- // TODO: has to be calculated on definition of reference object
- double aValue;
+ double aValue = 0;
// Issue #196: checking the positivity of the distance constraint
// there is a validator for a distance constraint, that the value should be positive
// in case if an invalid value is set, the current distance value is shown
if (aValueAttr->isInitialized())
aValue = aValueAttr->value();
- else {
- aValue = calculateCurrentDistance();
- aValueAttr->setValue(aValue);
- }
- // End TODO
AISObjectPtr anAIS = thePrevious;
if (!anAIS)
return aDistance;
}
+void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID) {
+ if (theID == SketchPlugin_Constraint::ENTITY_A() ||
+ theID == SketchPlugin_Constraint::ENTITY_B())
+ {
+ std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
+ ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
+ if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
+ double aDistance = calculateCurrentDistance();
+ if (aDistance > 0) { // set as value the length of updated references
+ aValueAttr->setValue(aDistance);
+ }
+ }
+ }
+}
//*************************************************************************************
std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
/// \param theDeltaY the delta for Y coordinate is moved
SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY);
+ /// Called on change of any argument-attribute of this object
+ /// \param theID identifier of changed attribute
+ SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
+
/// Returns the current distance between the feature attributes
double calculateCurrentDistance() const;
}
}
-AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
+bool SketchPlugin_ConstraintLength::getPoints(
+ std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
+ std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
+ std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
{
if (!sketch())
- return thePrevious;
-
+ return false;
std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
-
std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
if (!anAttr)
- return thePrevious;
+ return false;
FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
- return thePrevious;
-
- std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
- GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
-
+ return false;
DataPtr aData = aFeature->data();
- std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = std::dynamic_pointer_cast<
+ theStartPoint = std::dynamic_pointer_cast<
GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
- std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<
+ theEndPoint = std::dynamic_pointer_cast<
GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
+ thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
+ thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
+ return true;
+}
- std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aStartPoint->x(), aStartPoint->y());
- std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(anEndPoint->x(), anEndPoint->y());
+AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
+{
+ std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
+ std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
+ if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
+ return thePrevious; // not possible to show length because points are not defined
+
+ std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
+ std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = std::shared_ptr<GeomAPI_Pnt>();
if (aFlyOutAttr->isInitialized()) {
aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
// Set color from preferences
- std::vector<int> aRGB = Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR);
+ std::vector<int> aRGB =
+ Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR);
anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
return anAIS;
}
aPoint->move(theDeltaX, theDeltaY);
}
+void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
+ if (theID == SketchPlugin_Constraint::ENTITY_A())
+ {
+ std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
+ ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
+ if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
+ std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
+ std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
+ if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
+ double aLength = aPoint1->distance(aPoint2);
+ aValueAttr->setValue(aLength);
+ }
+ }
+ }
+}
#include <SketchPlugin_Sketch.h>
#include <list>
+class GeomDataAPI_Point2D;
+
#define LENGTH_COLOR "#ff00ff"
/** \class SketchPlugin_ConstraintLength
/// \param theDeltaY the delta for Y coordinate is moved
SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY);
+ /// Called on change of any argument-attribute of this object
+ /// \param theID identifier of changed attribute
+ SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
+
/// \brief Use plugin manager for features creation
SketchPlugin_ConstraintLength();
+
+private:
+ // retrns the points-base of length, returns false if it is not possible
+ bool getPoints(
+ std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
+ std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
+ std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint);
};
#endif
}
}
-AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
+double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
{
+ static const double kErrorResult = -1.;
if (!sketch())
- return thePrevious;
+ return kErrorResult;
std::shared_ptr<ModelAPI_Data> aData = data();
std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
if (!anAttr)
- return thePrevious;
- FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
- std::string aKind = aFeature ? aFeature->getKind() : "";
+ return kErrorResult;
+ theCirc = ModelAPI_Feature::feature(anAttr->object());
+ std::string aKind = theCirc ? theCirc->getKind() : "";
if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
- return thePrevious;
+ return kErrorResult;
+
+ DataPtr aCircData = theCirc->data();
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
+ if (aKind == SketchPlugin_Circle::ID()) {
+ AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
+ return aCircRadius->value();
+ } else {
+ aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
+ return aCenterAttr->pnt()->distance(aStartAttr->pnt());
+ }
+ return kErrorResult;
+}
+
+AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
+{
+ std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
+ double aRadius = circleRadius(aCyrcFeature);
+ if (aRadius < 0)
+ return thePrevious; // can not create a good presentation
// Flyout point
std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
- GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+ GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt;
if (aFlyoutAttr->isInitialized()) {
aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
}
// Prepare a circle
- std::shared_ptr<ModelAPI_Data> aCyrcData = aFeature->data();
std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
- double aRadius;
- if (aKind == SketchPlugin_Circle::ID()) {
+ if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
- aCyrcData->attribute(SketchPlugin_Circle::CENTER_ID()));
- AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
- aCyrcData->attribute(SketchPlugin_Circle::RADIUS_ID()));
- aRadius = aCircRadius->value();
+ aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
if (!aFlyoutPnt) {
double aShift = aRadius * 1.1;
std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
aFlyoutAttr->setValue(aFPnt);
aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
}
- } else if (aKind == SketchPlugin_Arc::ID()) {
+ } else { // arc
aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
- aCyrcData->attribute(SketchPlugin_Arc::CENTER_ID()));
- std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
- GeomDataAPI_Point2D>(aCyrcData->attribute(SketchPlugin_Arc::START_ID()));
- aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
+ aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
if (!aFlyoutPnt) {
std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
- GeomDataAPI_Point2D>(aCyrcData->attribute(SketchPlugin_Arc::START_ID()));
+ GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
aFlyoutAttr->setValue(aStartAttr->pnt());
aFlyoutPnt = sketch()->to3D(aStartAttr->pnt()->x(), aStartAttr->pnt()->y());
}
std::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
// Value
- // TODO: has to be calculated on definition of reference object
std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
- ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
- double aValue = aRadius;
+ ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
if (aValueAttr->isInitialized())
- aValue = aValueAttr->value();
- else {
- aValueAttr->setValue(aValue);
- }
- // End TODO
+ aRadius = aValueAttr->value();
AISObjectPtr anAIS = thePrevious;
if (!anAIS)
anAIS = AISObjectPtr(new GeomAPI_AISObject);
- anAIS->createRadius(aCircle, aFlyoutPnt, aValue);
+ anAIS->createRadius(aCircle, aFlyoutPnt, aRadius);
// Set color from preferences
std::vector<int> aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR);
aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
}
+
+void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
+ if (theID == SketchPlugin_Constraint::ENTITY_A()) {
+ std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
+ ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
+ if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
+ std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
+ double aRadius = circleRadius(aCyrcFeature);
+ if (aRadius > 0) { // set as value the radius of updated reference to another circle
+ aValueAttr->setValue(aRadius);
+ }
+ }
+ }
+}
/// \param theDeltaY the delta for Y coordinate is moved
SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY);
+ /// Called on change of any argument-attribute of this object
+ /// \param theID identifier of changed attribute
+ SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
+
/// \brief Use plugin manager for features creation
SketchPlugin_ConstraintRadius();
+
+private:
+ /// Checks and gets the radius of referenced circle (or arc) otherwise returns -1.
+ /// \param theCircData the found referenced circle returned by this method
+ double circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc);
};
#endif
return data()->selection(EXTERNAL_ID())->context();
}
-void SketchPlugin_Line::attributeChanged() {
- static bool myIsUpdated = false; // to avoid infinitive cycle on attrubtes change
- std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
- // update arguments due to the selection value
- if (aSelection && !aSelection->isNull() && aSelection->isEdge() && !myIsUpdated) {
- myIsUpdated = true;
- std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
- std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
- std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
- aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
- std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
- std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
- anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
- myIsUpdated = false;
+void SketchPlugin_Line::attributeChanged(const std::string& theID) {
+ if (theID == EXTERNAL_ID()) {
+ std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
+ // update arguments due to the selection value
+ if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
+ std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
+ aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
+ std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
+ anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
+ }
}
}
virtual double distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
/// Called on change of any argument-attribute of this object
- SKETCHPLUGIN_EXPORT virtual void attributeChanged();
+ SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
/// Use plugin manager for features creation
SketchPlugin_Line();
ModelAPI_CompositeFeature::erase();
}
-void SketchPlugin_Sketch::attributeChanged() {
- static bool kIsUpdated = false; // to avoid infinitive cycle on attrubtes change
- static bool kIsAttrChanged = false;
- std::shared_ptr<GeomAPI_Shape> aSelection =
- data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
- if (aSelection && !kIsUpdated) { // update arguments due to the selection value
- kIsUpdated = true;
- // update the sketch plane
- std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
- if (aPlane) {
- double anA, aB, aC, aD;
- aPlane->coefficients(anA, aB, aC, aD);
-
- // calculate attributes of the sketch
- std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
- std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
- std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
- aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
- std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
- // X axis is preferable to be dirX on the sketch
- static const double tol = 1.e-7;
- bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
- std::shared_ptr<GeomAPI_Dir> aTempDir(
- isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
- std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
- std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
-
- kIsAttrChanged = false; // track the attributes were really changed during the plane update
- std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
- <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
- anOrigin->setValue(anOrigPnt);
- std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
- data()->attribute(SketchPlugin_Sketch::NORM_ID()));
- aNormal->setValue(aNormDir);
- std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
- data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
- aDirX->setValue(aXDir);
- std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
- data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
- aDirY->setValue(aYDir);
- std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
-
- if (kIsAttrChanged) {
- /* now it is in updater
- // the plane was changed, so reexecute sub-elements to update shapes (located in new plane)
- ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
- list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
- for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
- std::shared_ptr<SketchPlugin_Feature> aFeature =
- std::dynamic_pointer_cast<SketchPlugin_Feature>(*aSubIt);
- if (aFeature && aFactory->validate(aFeature)) {
- aFeature->execute();
- }
- }
- */
- kIsAttrChanged = false;
+void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
+ if (theID == SketchPlugin_Feature::EXTERNAL_ID()) {
+ std::shared_ptr<GeomAPI_Shape> aSelection =
+ data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
+ if (aSelection) { // update arguments due to the selection value
+ // update the sketch plane
+ std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
+ if (aPlane) {
+ double anA, aB, aC, aD;
+ aPlane->coefficients(anA, aB, aC, aD);
+
+ // calculate attributes of the sketch
+ std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
+ std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
+ std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
+ aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
+ std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
+ // X axis is preferable to be dirX on the sketch
+ static const double tol = 1.e-7;
+ bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
+ std::shared_ptr<GeomAPI_Dir> aTempDir(
+ isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
+ std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
+ std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
+
+ // update position of the sketch
+ std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
+ <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+ anOrigin->setValue(anOrigPnt);
+ std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ data()->attribute(SketchPlugin_Sketch::NORM_ID()));
+ aNormal->setValue(aNormDir);
+ std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
+ aDirX->setValue(aXDir);
+ std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
+ aDirY->setValue(aYDir);
+ std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
}
}
- kIsUpdated = false;
- } else if (kIsUpdated) { // other attributes are updated during the selection comupation
- kIsAttrChanged = true;
}
}
/// Returns the point projected into the sketch plane
std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pnt>& thePnt);
- SKETCHPLUGIN_EXPORT virtual void attributeChanged();
+ SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
protected:
/// Creates a plane and append it to the list
/// \param theX the X normal value
//if (aPointWidget)
// connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
// SIGNAL(storedPoint2D(ObjectPtr, const std::string&)))
+ //}
if (!isEnableStretch) continue;
foreach(QWidget* eachWidget, (*anIt)->getControls()) {