- 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<GeomAPI_Dir> aDir = aPlane->direction();
+ std::shared_ptr<GeomAPI_Face> aFace;
+ if (aSelection->isFace()) {
+ aFace = aSelection->face();
+ } else if (aSelection->isCompound()) {
+ GeomAPI_ShapeIterator anIt(aSelection);
+ aFace = anIt.current()->face();
+ }
+ if (aFace.get()) {
+ std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
+ if (aPlane.get()) {
+ 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
+ // here can not be very small value to avoid very close to X normal axis (issue 595)
+ static const double tol = 0.1;
+ 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)));
+
+ bool aWasBlocked = data()->blockSendAttributeUpdated(true);
+ // 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);
+ data()->blockSendAttributeUpdated(aWasBlocked, true);
+ }
+ }
+ }
+ } else if (theID == NORM_ID() || theID == DIRX_ID() || theID == ORIGIN_ID()) {
+ // check if current and previous sketch planes are coordinate planes and they are different
+ GeomAx3Ptr aCurPlane;
+ bool areCoordPlanes = false;
+ if (isPlaneSet()) {
+ aCurPlane = coordinatePlane();
+ areCoordPlanes = isCoordinatePlane(aCurPlane) && isCoordinatePlane(myPlane);
+ }
+
+ // send all sub-elements are also updated: all entities become created on different plane
+ static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+ std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
+ std::list<ObjectPtr>::iterator aSub = aSubs.begin();
+ for(; aSub != aSubs.end(); aSub++) {
+ if (aSub->get()) {
+ if (areCoordPlanes)
+ updateCoordinateAxis(*aSub, aCurPlane);
+
+ ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
+ }
+ }
+ if (aCurPlane)
+ myPlane = aCurPlane;
+ }
+}
+
+void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
+ SketchPlugin_Sketch* theSketch,
+ const std::string& theAttributeID, const int theIndex)
+{
+ std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ theFeature->attribute(theAttributeID));
+
+ if (!aPoint || !aPoint->isInitialized())
+ return;
+
+ std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
+ //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
+ // make a visible point
+ std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
+ std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
+ theFeature->data(), theIndex);
+ aResult->setShape(aCenterPointShape);
+ aResult->setIsInHistory(false);
+
+ theFeature->setResult(aResult, theIndex);
+}
+
+void SketchPlugin_Sketch::createLine2DResult(ModelAPI_Feature* theFeature,
+ SketchPlugin_Sketch* theSketch,
+ const std::string& theStartAttrID,
+ const std::string& theEndAttrID,
+ const int theIndex)
+{
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(theStartAttrID));
+ std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(theEndAttrID));
+
+ if (!aStartAttr || !aStartAttr->isInitialized() ||
+ !anEndAttr || !anEndAttr->isInitialized())
+ return;
+
+ std::shared_ptr<GeomAPI_Pnt> aStart(theSketch->to3D(aStartAttr->x(), aStartAttr->y()));
+ std::shared_ptr<GeomAPI_Pnt> anEnd(theSketch->to3D(anEndAttr->x(), anEndAttr->y()));
+ //std::cout<<"Execute line "<<aStart->x()<<" "<<aStart->y()<<" "<<aStart->z()<<" - "
+ // <<anEnd->x()<<" "<<anEnd->y()<<" "<<anEnd->z()<<std::endl;
+ // make linear edge
+ std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
+ // store the result
+ std::shared_ptr<ModelAPI_ResultConstruction> aResult =
+ theFeature->document()->createConstruction(theFeature->data(), theIndex);
+ aResult->setShape(anEdge);
+ aResult->setIsInHistory(false);
+ theFeature->setResult(aResult, theIndex);
+}
+
+FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
+ SketchPlugin_Sketch* theSketch,
+ const bool theIsCopy)
+{
+ FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
+ // addFeature generates a unique name for the feature, it caches the name
+ std::string aUniqueFeatureName = aNewFeature->data()->name();
+ // all attribute values are copied\pasted to the new feature, name is not an exception
+ theFeature->data()->copyTo(aNewFeature->data());
+ // external state should not be copied as a new object is an object of the current sketch
+ if (theFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()).get())
+ aNewFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->setValue(ResultPtr(),
+ GeomShapePtr());
+ aNewFeature->data()->setName(aUniqueFeatureName);
+ // text expressions could block setValue of some attributes
+ SketchPlugin_Tools::clearExpressions(aNewFeature);
+ // Set copy attribute
+ AttributeBooleanPtr anAttr = aNewFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
+ if(anAttr.get()) {
+ anAttr->setValue(theIsCopy);
+ }
+
+ return aNewFeature;
+}
+
+std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
+{
+ std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
+
+ std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
+ aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+ std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
+ std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+
+ if (aNorm.get() && aNorm->isInitialized() && anOrigin.get() && anOrigin->isInitialized())
+ return std::shared_ptr<GeomAPI_Ax3>(
+ new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
+
+ return std::shared_ptr<GeomAPI_Ax3>();
+}
+
+bool SketchPlugin_Sketch::customAction(const std::string& theActionId)
+{
+ bool isOk = false;
+ if (theActionId == ACTION_REMOVE_EXTERNAL())
+ isOk = removeLinksToExternal();
+ else {
+ std::string aMsg = "Error: Feature \"%1\" does not support action \"%2\".";
+ Events_InfoMessage("SketchPlugin_Sketch", aMsg).arg(getKind()).arg(theActionId).send();
+ }
+ return isOk;
+}
+
+static bool isExternalBased(const FeaturePtr theFeature)
+{
+ return theFeature->getKind() == SketchPlugin_Projection::ID() ||
+ theFeature->getKind() == SketchPlugin_IntersectionPoint::ID();
+}
+
+bool SketchPlugin_Sketch::removeLinksToExternal()
+{
+ std::list<FeaturePtr> aRemove;
+ std::list<ObjectPtr> aSubs = reflist(FEATURES_ID())->list();
+ for (std::list<ObjectPtr>::iterator anIt = aSubs.begin(); anIt != aSubs.end(); ++anIt) {
+ FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
+ if (!aFeature)
+ continue;
+ if (isExternalBased(aFeature)) {
+ // mark feature as to be removed
+ aRemove.push_back(aFeature);
+ }
+ else {
+ AttributeSelectionPtr anExtAttr =
+ aFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
+ ResultPtr anExternal = anExtAttr ? anExtAttr->context() : ResultPtr();
+ if (anExternal) {
+ FeaturePtr anExtFeature = ModelAPI_Feature::feature(anExternal);
+ if (anExtFeature && isExternalBased(anExtFeature)) {
+ // make result of projection/intersection as non-external,
+ aFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->setValue(
+ ObjectPtr(), GeomShapePtr());
+ // set feature auxiliary if the parent is not included into sketch result
+ bool isIncludedToSketchResult = false;
+ if (anExtFeature->getKind() == SketchPlugin_Projection::ID()) {
+ isIncludedToSketchResult = anExtFeature->boolean(
+ SketchPlugin_Projection::INCLUDE_INTO_RESULT())->value();
+ }
+ if (anExtFeature->getKind() == SketchPlugin_IntersectionPoint::ID()) {
+ isIncludedToSketchResult = anExtFeature->boolean(
+ SketchPlugin_IntersectionPoint::INCLUDE_INTO_RESULT())->value();
+ }
+
+ aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID())->setValue(false);
+ aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(
+ !isIncludedToSketchResult);
+ }