X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketcherPrs%2FSketcherPrs_Tools.cpp;h=156e1265954e78e745b83ebd8b2c4cb8f8c02db8;hb=50a8df0c6a66da8067b16155e5ae39f8f26a7ebc;hp=9f845f57fc0d2ff8b160fb1174206ca0bf5662c0;hpb=1c0c005d8183e90aa20a076d64a89d5cfd4479f6;p=modules%2Fshaper.git diff --git a/src/SketcherPrs/SketcherPrs_Tools.cpp b/src/SketcherPrs/SketcherPrs_Tools.cpp index 9f845f57f..156e12659 100644 --- a/src/SketcherPrs/SketcherPrs_Tools.cpp +++ b/src/SketcherPrs/SketcherPrs_Tools.cpp @@ -1,69 +1,392 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: SketcherPrs_Tools.cpp -// Created: 10 March 2015 -// Author: Vitaly SMETANNIKOV +// Copyright (C) 2014-2020 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include "SketcherPrs_Tools.h" #include #include #include +#include +#include + #include #include +#include +#include +#include + +#include + +#include #include +#include + +#include +#include +#include + +#include +#include +#include + +#include namespace SketcherPrs_Tools { -ObjectPtr getResult(SketchPlugin_Constraint* theFeature, const std::string& theAttrName) +static ParameterStyle MyStyle = ParameterValue; + +void setParameterStyle(ParameterStyle theStyle) { - std::shared_ptr aData = theFeature->data(); - std::shared_ptr anAttr = - std::dynamic_pointer_cast(aData->attribute(theAttrName)); + MyStyle = theStyle; +} - return anAttr->object(); +ParameterStyle parameterStyle() +{ + return MyStyle; +} + +AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName) +{ + AttributePtr anAttribute; + if (theFeature) { + std::shared_ptr aData = theFeature->data(); + if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread + std::shared_ptr anAttr = aData->refattr(theAttrName); + if (!anAttr->isObject()) + anAttribute = anAttr->attr(); + } + } + return anAttribute; +} + +ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName) +{ + ObjectPtr anObject; + if (theFeature) { + std::shared_ptr aData = theFeature->data(); + if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread + std::shared_ptr anAttr = aData->refattr(theAttrName); + if (anAttr.get()) + anObject = anAttr->object(); + } + } + return anObject; } std::shared_ptr getShape(ObjectPtr theObject) { ResultConstructionPtr aRes = std::dynamic_pointer_cast(theObject); - if (aRes.get() != NULL) { + if (aRes.get() != NULL && aRes->data()->isValid()) { + /// essential check as it is called in openGl thread return aRes->shape(); } return std::shared_ptr(); } -std::shared_ptr getPoint(SketchPlugin_Constraint* theFeature, - const std::string& theAttribute) +std::shared_ptr getPoint(ModelAPI_Feature* theFeature, + const std::string& theAttribute) +{ + std::shared_ptr aPointAttr = + std::dynamic_pointer_cast(theFeature->attribute(theAttribute)); + if (!aPointAttr.get() || !aPointAttr->isInitialized()) { + aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr( + theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID()); + } + if (aPointAttr.get() != NULL) + return aPointAttr->pnt(); + return std::shared_ptr(); +} + +//************************************************************************************* +/// Find an attribute of the given object which corresponds to a vetrex +/// defined by a shape +/// \param theObject an object +/// \param theShape a vertex +/// \param thePlane a projection plane (sketcher plane) +std::shared_ptr findGeomPoint(ObjectPtr theObject, + const TopoDS_Shape& theShape, + const std::shared_ptr& thePlane) +{ + std::shared_ptr aGeomPoint; + + FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject); + if (anObjectFeature) { + if (theShape.ShapeType() == TopAbs_VERTEX) { + const TopoDS_Vertex& aShapeVertex = TopoDS::Vertex(theShape); + if (!aShapeVertex.IsNull()) { + gp_Pnt aShapePoint = BRep_Tool::Pnt(aShapeVertex); + std::shared_ptr aShapeGeomPnt = std::shared_ptr( + new GeomAPI_Pnt(aShapePoint.X(), aShapePoint.Y(), aShapePoint.Z())); + + // find the given point in the feature attributes + std::list anObjectAttiributes = + anObjectFeature->data()->attributes(GeomDataAPI_Point2D::typeId()); + std::list::const_iterator anIt = anObjectAttiributes.begin(), + aLast = anObjectAttiributes.end(); + for (; anIt != aLast && !aGeomPoint; anIt++) { + std::shared_ptr anAttributePoint = + std::dynamic_pointer_cast(*anIt); + + std::shared_ptr anAttributePnt = thePlane->to3D(anAttributePoint->x(), + anAttributePoint->y()); + if (anAttributePnt.get() && + anAttributePnt->distance(aShapeGeomPnt) < Precision::Confusion()) { + aGeomPoint = anAttributePoint; + break; + } + } + } + } + } + return aGeomPoint; +} + +//************************************************************************************* +std::shared_ptr getFeaturePoint(DataPtr theData, + const std::string& theAttribute, + const std::shared_ptr& thePlane) { std::shared_ptr aPointAttr; - if (!theFeature->data()) - return std::shared_ptr(); + if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread + return aPointAttr; FeaturePtr aFeature; std::shared_ptr anAttr = std::dynamic_pointer_cast< - ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute)); - if (anAttr) - aFeature = ModelAPI_Feature::feature(anAttr->object()); + ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute)); + if (anAttr) { + if (anAttr->isObject()) { + ObjectPtr anObject = anAttr->object(); + aFeature = ModelAPI_Feature::feature(anObject); + if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) { + // Attribute refers to a point + aPointAttr = std::dynamic_pointer_cast( + aFeature->data()->attribute(SketchPlugin_Point::COORD_ID())); + } + else { + // if the attribute refers on another object + ResultPtr aRes = std::dynamic_pointer_cast(anObject); + if (aRes.get()) { + GeomShapePtr aShape = aRes->shape(); + if (aShape.get()) { + TopoDS_Shape aTDSShape = aShape->impl(); + aPointAttr = findGeomPoint(anObject, aTDSShape, thePlane); + } + } + } + } + else if (anAttr->attr()) { + // If attribute is a point + aPointAttr = std::dynamic_pointer_cast(anAttr->attr()); + } + } + return aPointAttr; +} - if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID()) - aPointAttr = std::dynamic_pointer_cast( - aFeature->data()->attribute(SketchPlugin_Point::COORD_ID())); +//************************************************************************************* +std::list getFreePoints(const CompositeFeaturePtr& theSketch) +{ + std::list aFreePoints; + if (!theSketch) + return aFreePoints; - else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID()) - aPointAttr = std::dynamic_pointer_cast( - aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID())); + AttributeRefListPtr aFeatures = theSketch->reflist(SketchPlugin_Sketch::FEATURES_ID()); + if (!aFeatures) + return aFreePoints; + std::list anObjects = aFeatures->list(); + for (std::list::iterator anObjIt = anObjects.begin(); + anObjIt != anObjects.end(); ++anObjIt) { + FeaturePtr aCurrent = ModelAPI_Feature::feature(*anObjIt); + if (aCurrent && aCurrent->getKind() == SketchPlugin_Point::ID()) { + // check point is not referred by any constraints: the feature and result of point + bool aIsFree = true; + for(int aKind = 0; aIsFree && aKind < 2; aKind++) { // 0 for feature, 1 for result + ObjectPtr aReferenced = aCurrent; + if (aKind == 1) + if (!aCurrent->results().empty()) + aReferenced = aCurrent->firstResult(); + else + break; + const std::set& aRefs = aReferenced->data()->refsToMe(); + std::set::iterator aRIt = aRefs.begin(); + for (; aRIt != aRefs.end(); ++aRIt) { + FeaturePtr aRefFeat = ModelAPI_Feature::feature((*aRIt)->owner()); + std::shared_ptr aRefConstr = + std::dynamic_pointer_cast(aRefFeat); + if (aRefConstr) { + aIsFree = false; + break; + } + } + } + if (aIsFree) + aFreePoints.push_back(aCurrent->lastResult()); + } + } + return aFreePoints; +} + +//************************************************************************************* +FeaturePtr getFeatureLine(DataPtr theData, + const std::string& theAttribute) +{ + FeaturePtr aLine; + if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread) + return aLine; - else if (anAttr->attr()) { - aPointAttr = std::dynamic_pointer_cast(anAttr->attr()); + std::shared_ptr anAttr = + std::dynamic_pointer_cast(theData->attribute(theAttribute)); + if (anAttr) { + FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object()); + if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) { + return aFeature; + } } - return aPointAttr->pnt(); + return aLine; +} + +//************************************************************************************* +std::shared_ptr getProjectionPoint(const FeaturePtr theLine, + const std::shared_ptr& thePoint) +{ + DataPtr aData = theLine->data(); + if (!aData.get() || !aData->isValid()) + return std::shared_ptr(); + + std::shared_ptr aPoint1 = std::dynamic_pointer_cast( + aData->attribute(SketchPlugin_Line::START_ID())); + std::shared_ptr aPoint2 = std::dynamic_pointer_cast( + aData->attribute(SketchPlugin_Line::END_ID())); + + GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y()); + return aLin2d.project(thePoint); +} + +static int MyPixelRatio = 1; + +void setPixelRatio(int theRatio) +{ + MyPixelRatio = theRatio; +} + +int pixelRatio() +{ + return MyPixelRatio; +} + +static double MyArrowSize = 20; + +double getArrowSize() +{ + return MyArrowSize; +} + +void setArrowSize(double theSize) +{ + MyArrowSize = theSize; +} + +int getDefaultArrowSize() +{ + return 20; +} + +int getConfigArrowSize() +{ + return Config_PropManager::integer("Visualization", "dimension_arrow_size"); } +static double MyTextHeight = 16; +double getTextHeight() +{ + return MyTextHeight * MyPixelRatio; +} + +void setTextHeight(double theHeight) +{ + MyTextHeight = theHeight; +} + +double getDefaultTextHeight() +{ + return 16 * MyPixelRatio; +} + +double getConfigTextHeight() +{ + return Config_PropManager::integer("Visualization", "dimension_value_size") * MyPixelRatio; +} -}; \ No newline at end of file +double getFlyoutDistance(const ModelAPI_Feature* theConstraint) +{ + std::shared_ptr aFlyoutPoint = + std::dynamic_pointer_cast( + const_cast( + theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); + // for not initialized values return zero distance to avoid Presentation crash + if (!aFlyoutPoint->isInitialized()) + return 0; + return aFlyoutPoint->y(); +} + +std::shared_ptr getAnchorPoint(const ModelAPI_Feature* theConstraint, + const std::shared_ptr& thePlane) +{ + ModelAPI_Feature* aConstraint = const_cast(theConstraint); + AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); + if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object()) + return std::shared_ptr(); + + FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object()); + std::shared_ptr aCenter; + if (aFeature->getKind() == SketchPlugin_Arc::ID()) { // arc + aCenter = std::dynamic_pointer_cast( + aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt(); + } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) { // circle + aCenter = std::dynamic_pointer_cast( + aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt(); + } else + return std::shared_ptr(); + + std::shared_ptr anOrigin(new GeomAPI_Pnt2d(0.0, 0.0)); + std::shared_ptr aFlyoutPnt = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt(); + double aRadius = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value(); + double aLen = aFlyoutPnt->distance(anOrigin); + aRadius *= 1.001; // a gap to make point much closer to the circle, but not lying on it + aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen); + aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen); + return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y()); +} + +void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError) +{ + Events_InfoMessage("SketcherPrs_Tools", + "An empty AIS presentation: SketcherPrs_LengthDimension").send(); + static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION); + std::shared_ptr aConstraintPtr(theFeature); + ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent); +} +};