#include "SketchPlugin_ConstraintDistance.h"
#include <SketchPlugin_Point.h>
+#include <SketchPlugin_Circle.h>
+#include <SketchPlugin_Line.h>
-#include <GeomAPI_Lin2D.h>
-#include <GeomAPI_Pnt2D.h>
+#include <GeomAPI_Lin2d.h>
+#include <GeomAPI_Pnt2d.h>
#include <GeomDataAPI_Point2D.h>
#include <ModelAPI_AttributeDouble.h>
#include <ModelAPI_Data.h>
-#include <AIS_LengthDimension.hxx>
-#include <gp_Pnt.hxx>
-#include <gp_Pln.hxx>
-
-/// Obtain the point object from specified constraint parameter
-static boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(
- DataPtr theData,
- const std::string& theAttribute);
-
+#include <Config_PropManager.h>
SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
{
}
+//*************************************************************************************
void SketchPlugin_ConstraintDistance::initAttributes()
{
- data()->addAttribute(CONSTRAINT_ATTR_VALUE, ModelAPI_AttributeDouble::type());
- data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
- data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
- data()->addAttribute(CONSTRAINT_ATTR_ENTITY_B, ModelAPI_AttributeRefAttr::type());
+ data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
+ data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
+ data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
+ data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
}
+//*************************************************************************************
void SketchPlugin_ConstraintDistance::execute()
{
boost::shared_ptr<ModelAPI_Data> aData = data();
-
- boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr_A =
- boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
- boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr_B =
- boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
-
- AttributeDoublePtr anAttribute =
- boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
-
- if (anAttr_A && anAttr_B && anAttribute->value() == 0)
- {
- FeaturePtr aFeature_A = anAttr_A->feature();
- FeaturePtr aFeature_B = anAttr_B->feature();
-
- double aValue = 40; // TODO
- anAttribute->setValue(aValue);
+ AttributeDoublePtr anAttrValue = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ aData->attribute(SketchPlugin_Constraint::VALUE()));
+
+ if(anAttrValue->isInitialized())
+ return;
+ boost::shared_ptr<GeomDataAPI_Point2D> aPointA =
+ getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
+ boost::shared_ptr<GeomDataAPI_Point2D> aPointB =
+ getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
+
+ double aDistance = -1.;
+ if (aPointA && aPointB) { // both points
+ aDistance = aPointA->pnt()->distance(aPointB->pnt());
+ } else {
+ if (!aPointA && aPointB) { //Line and point
+ boost::shared_ptr<SketchPlugin_Line> aLine =
+ getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
+ if (aLine) {
+ aDistance = aLine->distanceToPoint(aPointB->pnt());
+ }
+ } else if (aPointA && !aPointB) { // Point and line
+ boost::shared_ptr<SketchPlugin_Line> aLine =
+ getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
+ if (aLine) {
+ aDistance = aLine->distanceToPoint(aPointA->pnt());
+ }
+ }
+ }
+ if(aDistance >= 0) {
+ anAttrValue->setValue(aDistance);
}
}
-Handle(AIS_InteractiveObject) SketchPlugin_ConstraintDistance::getAISShape(
- Handle_AIS_InteractiveObject thePrevious)
+//*************************************************************************************
+AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
{
if (!sketch())
return thePrevious;
boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
DataPtr aData = data();
- boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
- boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
- if (!aPoint_A || !aPoint_B)
+ boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
+ aData, SketchPlugin_Constraint::ENTITY_A());
+ boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
+ aData, SketchPlugin_Constraint::ENTITY_B());
+
+ boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
+ boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
+
+ if (aPoint_A && aPoint_B) {
+ aPnt_A = aPoint_A->pnt();
+ aPnt_B = aPoint_B->pnt();
+ } else if (!aPoint_A && aPoint_B) {
+ boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
+ aData, SketchPlugin_Constraint::ENTITY_A());
+ if (aLine) {
+ aPnt_B = aPoint_B->pnt();
+ aPnt_A = getProjectionPoint(aLine, aPnt_B);
+ }
+ } else if (aPoint_A && !aPoint_B) {
+ boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
+ aData, SketchPlugin_Constraint::ENTITY_B());
+ if (aLine) {
+ aPnt_A = aPoint_A->pnt();
+ aPnt_B = getProjectionPoint(aLine, aPnt_A);
+ }
+ }
+ if (!aPnt_A || !aPnt_B)
return thePrevious;
- // fly out calculation
- boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
- boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
- boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
-
- boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin =
- boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoint_A->x(), aPoint_A->y(),
- aPoint_B->x(), aPoint_B->y()));
- boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aFlyOutPnt);
- double aDistance = aFlyOutPnt->distance(aResult);
-
- if (!aFeatureLin->isRight(aFlyOutPnt))
- aDistance = -aDistance;
- double aFlyout = aDistance;
-
- //Build dimension here
- boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
- boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
- if (aFlyout < 0)
- aPoint1.swap(aPoint2);
-
+ boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = boost::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+
+ boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
+ boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
+ boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = boost::shared_ptr<GeomAPI_Pnt>();
+ if(aFlyOutAttr->isInitialized()) {
+ aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
+ } else {
+ boost::shared_ptr<GeomAPI_Lin2d> aLine = boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
+ double aDist = aPoint1->distance(aPoint2)/5.;
+ boost::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
+ aFlyOutAttr->setValue(aFPnt);
+ aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
+ }
// value calculation
- boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
- boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
+ boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
+ ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
double aValue = aValueAttr->value();
- if (aValue == 0) { // TODO! the default value
- aValue = aPoint1->distance(aPoint2);
- }
- Handle(AIS_InteractiveObject) anAIS = thePrevious;
- if (anAIS.IsNull())
- {
- Handle(AIS_LengthDimension) aDimAIS =
- new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
- aDimAIS->SetCustomValue(aValue);
-
- Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
- anAspect->MakeArrows3d (Standard_False);
- anAspect->MakeText3d(false);
- anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
- anAspect->MakeTextShaded(false);
- aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
- aDimAIS->SetDimensionAspect (anAspect);
- aDimAIS->SetFlyout(aFlyout);
- aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
-
- anAIS = aDimAIS;
- }
- else {
- // update presentation
- Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
- if (!aDimAIS.IsNull()) {
- aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
- aDimAIS->SetCustomValue(aValue);
- aDimAIS->SetFlyout(aFlyout);
-
- aDimAIS->Redisplay(Standard_True);
- }
- }
+ AISObjectPtr anAIS = thePrevious;
+ if (!anAIS)
+ anAIS = AISObjectPtr(new GeomAPI_AISObject);
+ anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
+
+ // Set color from preferences
+ std::vector<int> aRGB = Config_PropManager::color("Visualization", "distance_color",
+ DISTANCE_COLOR);
+ anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
return anAIS;
}
+//*************************************************************************************
+void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
+{
+ boost::shared_ptr<ModelAPI_Data> aData = data();
+ if (!aData->isValid())
+ return;
+ boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+ aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
+}
+
+//*************************************************************************************
boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
const std::string& theAttribute)
{
return aPointAttr;
FeaturePtr aFeature;
- boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
- boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
+ boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
+ ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
if (anAttr)
- aFeature = anAttr->feature();
-
- if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND)
- aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
- (aFeature->data()->attribute(POINT_ATTR_COORD));
+ aFeature = ModelAPI_Feature::feature(anAttr->object());
+
+ if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
+ aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
+ else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
+ aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
+ else if (anAttr->attr()) {
+ aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
+ }
return aPointAttr;
}
+//*************************************************************************************
+boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
+ const std::string& theAttribute)
+{
+ boost::shared_ptr<SketchPlugin_Line> aLine;
+ if (!theData)
+ return aLine;
+
+ boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
+ ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
+ if (anAttr) {
+ FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
+ if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
+ aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
+ }
+ }
+ return aLine;
+}
+
+//*************************************************************************************
+boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
+ const boost::shared_ptr<SketchPlugin_Line>& theLine,
+ const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
+{
+ boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
+ boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aData->attribute(SketchPlugin_Line::START_ID()));
+ boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aData->attribute(SketchPlugin_Line::END_ID()));
+
+ GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
+ return aLin2d.project(thePoint);
+}