X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FConstructionPlugin%2FConstructionPlugin_Plane.cpp;h=07a223b44e5053d8e6180e69eea43f56ca6ddda1;hb=3e7f97125b2198724911564b24b0a30cbd393e3d;hp=37faed97091646b4f309eda8efca90f12f5ee3e2;hpb=3d7368386762b057fff327d24d0079b4a73c3004;p=modules%2Fshaper.git diff --git a/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp b/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp index 37faed970..07a223b44 100644 --- a/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp +++ b/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp @@ -6,13 +6,18 @@ #include "ConstructionPlugin_Plane.h" +#include + #include #include #include +#include +#include +#include +#include #include - -#define PLANE_SIZE 300 +#include ConstructionPlugin_Plane::ConstructionPlugin_Plane() { @@ -20,36 +25,145 @@ ConstructionPlugin_Plane::ConstructionPlugin_Plane() void ConstructionPlugin_Plane::initAttributes() { - data()->addAttribute(FACE_ATTR, ModelAPI_AttributeSelection::type()); - data()->addAttribute(DISTANCE_ATTR, ModelAPI_AttributeDouble::type()); + data()->addAttribute(ConstructionPlugin_Plane::METHOD(), ModelAPI_AttributeString::typeId()); + // Face & Distance + data()->addAttribute(ConstructionPlugin_Plane::FACE(), ModelAPI_AttributeSelection::typeId()); + data()->addAttribute(ConstructionPlugin_Plane::DISTANCE(), ModelAPI_AttributeDouble::typeId()); + // General equation + data()->addAttribute(ConstructionPlugin_Plane::A(), ModelAPI_AttributeDouble::typeId()); + data()->addAttribute(ConstructionPlugin_Plane::B(), ModelAPI_AttributeDouble::typeId()); + data()->addAttribute(ConstructionPlugin_Plane::C(), ModelAPI_AttributeDouble::typeId()); + data()->addAttribute(ConstructionPlugin_Plane::D(), ModelAPI_AttributeDouble::typeId()); + + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ConstructionPlugin_Plane::A()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ConstructionPlugin_Plane::B()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ConstructionPlugin_Plane::C()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ConstructionPlugin_Plane::D()); } void ConstructionPlugin_Plane::execute() { - AttributeSelectionPtr aFaceAttr = data()->selection(FACE_ATTR); - AttributeDoublePtr aDistAttr = data()->real(DISTANCE_ATTR); - if ((aFaceAttr.get() != NULL) && (aDistAttr.get() != NULL) && - aFaceAttr->isInitialized() && aDistAttr->isInitialized()) { + AttributeStringPtr aMethodTypeAttr = string(ConstructionPlugin_Plane::METHOD()); + std::string aMethodType = aMethodTypeAttr->value(); + std::shared_ptr aPlaneFace; + if (aMethodType == "PlaneByFaceAndDistance") { + aPlaneFace = createPlaneByFaceAndDistance(); + } else if (aMethodType == "PlaneByGeneralEquation") { + aPlaneFace = createPlaneByGeneralEquation(); + } + if (!aPlaneFace.get()) + return; + ResultConstructionPtr aConstr = document()->createConstruction(data()); + aConstr->setInfinite(true); + aConstr->setShape(aPlaneFace); + setResult(aConstr); +} + +bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs, + std::shared_ptr theDefaultPrs) +{ + std::vector aColor; + // get color from the attribute of the result + if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) { + AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID()); + if (aColorAttr.get() && aColorAttr->size()) { + aColor.push_back(aColorAttr->value(0)); + aColor.push_back(aColorAttr->value(1)); + aColor.push_back(aColorAttr->value(2)); + } + } + if (aColor.empty()) + aColor = Config_PropManager::color("Visualization", "construction_plane_color", + ConstructionPlugin_Plane::DEFAULT_COLOR()); + + bool isCustomized = false; + if (aColor.size() == 3) + isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]); + + isCustomized = thePrs->setTransparensy(0.6) || isCustomized; + + return isCustomized; +} + +std::shared_ptr ConstructionPlugin_Plane::createPlaneByFaceAndDistance() +{ + AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::FACE()); + AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE()); + std::shared_ptr aPlane; + if ((aFaceAttr.get() != NULL) && + (aDistAttr.get() != NULL) && + aFaceAttr->isInitialized() && aDistAttr->isInitialized()) { double aDist = aDistAttr->value(); GeomShapePtr aShape = aFaceAttr->value(); + if (!aShape.get()) { + aShape = aFaceAttr->context()->shape(); + } + if (aShape.get() != NULL) { std::shared_ptr aPln = GeomAlgoAPI_FaceBuilder::plane(aShape); std::shared_ptr aOrig = aPln->location(); std::shared_ptr aDir = aPln->direction(); aOrig->translate(aDir, aDist); - std::shared_ptr aPlane = - GeomAlgoAPI_FaceBuilder::square(aOrig, aDir, PLANE_SIZE); - ResultConstructionPtr aConstr = document()->createConstruction(data()); - aConstr->setShape(aPlane); - setResult(aConstr); + std::shared_ptr aNewPln = std::shared_ptr( + new GeomAPI_Pln(aOrig, aDir)); + + // Create rectangular face close to the selected + double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax; + aShape->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax); + + // use all 8 points of the bounding box to find the 2D bounds + bool isFirst = true; + double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d; + for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) { + for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) { + for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) { + std::shared_ptr aPnt = std::shared_ptr(new GeomAPI_Pnt( + aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax)); + std::shared_ptr aPnt2d = aPnt->to2D(aNewPln); + if (isFirst || aPnt2d->x() < aMinX2d) + aMinX2d = aPnt2d->x(); + if (isFirst || aPnt2d->y() < aMinY2d) + aMinY2d = aPnt2d->y(); + if (isFirst || aPnt2d->x() > aMaxX2d) + aMaxX2d = aPnt2d->x(); + if (isFirst || aPnt2d->y() > aMaxY2d) + aMaxY2d = aPnt2d->y(); + if (isFirst) + isFirst = !isFirst; + } + } + } + double aWgap = (aMaxX2d - aMinX2d) * 0.1; + double aHgap = (aMaxY2d - aMinY2d) * 0.1; + aPlane = GeomAlgoAPI_FaceBuilder::planarFace(aNewPln, + aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap); } } + return aPlane; } -void ConstructionPlugin_Plane::customisePresentation(AISObjectPtr thePrs) +std::shared_ptr ConstructionPlugin_Plane::createPlaneByGeneralEquation() { - thePrs->setColor(50, 255, 50); - thePrs->setTransparensy(0.6); -} \ No newline at end of file + AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A()); + AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B()); + AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C()); + AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D()); + std::shared_ptr aPlaneFace; + if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) && + (anAttrC.get() != NULL) && (anAttrD.get() != NULL) && + anAttrA->isInitialized() && anAttrB->isInitialized() && + anAttrC->isInitialized() && anAttrD->isInitialized() ) { + double aA = anAttrA->value(), aB = anAttrB->value(), + aC = anAttrC->value(), aD = anAttrD->value(); + std::shared_ptr aPlane = + std::shared_ptr(new GeomAPI_Pln(aA, aB, aC, aD)); + std::string kDefaultPlaneSize = "200"; + double aSize = Config_PropManager::integer("Sketch planes", "planes_size", kDefaultPlaneSize); + aSize *= 4.; + aPlaneFace = GeomAlgoAPI_FaceBuilder::square(aPlane, aSize); + } + return aPlaneFace; +} +