+
+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::wstring 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);
+ }
+ }
+ }
+ }
+ for (std::list<FeaturePtr>::iterator anIt = aRemove.begin(); anIt != aRemove.end(); ++anIt)
+ document()->removeFeature(*anIt);
+ return true;
+}
+
+
+static ObjectPtr findAxis(GeomShapePtr theAxisToCompare,
+ ObjectPtr theOX,
+ ObjectPtr theOY,
+ ObjectPtr theOZ)
+{
+ if (theAxisToCompare) {
+ ObjectPtr anAxes[] = { theOX, theOY, theOZ };
+ for (int i = 0; i < 3; ++i) {
+ ResultPtr anAx = std::dynamic_pointer_cast<ModelAPI_Result>(anAxes[i]);
+ if (anAx && theAxisToCompare->isEqual(anAx->shape()))
+ return anAxes[i];
+ }
+ }
+ return ObjectPtr();
+}
+
+static ObjectPtr findAxis(ObjectPtr theAxisToCompare,
+ ObjectPtr theOX,
+ ObjectPtr theOY,
+ ObjectPtr theOZ)
+{
+ if (theAxisToCompare == theOX)
+ return theOX;
+ else if (theAxisToCompare == theOY)
+ return theOY;
+ else if (theAxisToCompare == theOZ)
+ return theOZ;
+ // nothing helped, search by shape
+ ResultPtr anAxis = std::dynamic_pointer_cast<ModelAPI_Result>(theAxisToCompare);
+ return findAxis(anAxis ? anAxis->shape() : GeomShapePtr(), theOX, theOY, theOZ);
+}
+
+GeomShapePtr axisOnNewPlane(ObjectPtr theAxis, GeomAx3Ptr theOldPlane, GeomAx3Ptr theNewPlane)
+{
+ ResultPtr anAxis = std::dynamic_pointer_cast<ModelAPI_Result>(theAxis);
+ if (!anAxis)
+ return GeomShapePtr();
+
+ GeomEdgePtr anAxisEdge = anAxis->shape()->edge();
+ GeomLinePtr anAxisLine = anAxisEdge->line();
+ GeomDirPtr anAxisDir = anAxisLine->direction();
+
+ double aFirstParam, aLastParam;
+ anAxisEdge->getRange(aFirstParam, aLastParam);
+
+ if (theOldPlane->dirX()->isParallel(anAxisDir))
+ anAxisDir = theNewPlane->dirX();
+ else if (theOldPlane->dirY()->isParallel(anAxisDir))
+ anAxisDir = theNewPlane->dirY();
+ else if (theOldPlane->normal()->isParallel(anAxisDir))
+ anAxisDir = theNewPlane->normal();
+
+ GeomPointPtr aFirstPoint(new GeomAPI_Pnt(aFirstParam * anAxisDir->x(),
+ aFirstParam * anAxisDir->y(),
+ aFirstParam * anAxisDir->z()));
+ GeomPointPtr aLastPoint(new GeomAPI_Pnt(aLastParam * anAxisDir->x(),
+ aLastParam * anAxisDir->y(),
+ aLastParam * anAxisDir->z()));
+ return GeomAlgoAPI_EdgeBuilder::line(aFirstPoint, aLastPoint);
+}
+
+void SketchPlugin_Sketch::updateCoordinateAxis(ObjectPtr theSub, GeomAx3Ptr thePlane)
+{
+ FeaturePtr aFeature = ModelAPI_Feature::feature(theSub);
+ if (!aFeature)
+ return;
+
+ DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+ ObjectPtr anOX = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), L"OX");
+ ObjectPtr anOY = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), L"OY");
+ ObjectPtr anOZ = aRootDoc->objectByName(ModelAPI_ResultConstruction::group(), L"OZ");
+
+ AttributeSelectionPtr anExtFeature;
+ if (aFeature->getKind() == SketchPlugin_Projection::ID())
+ anExtFeature = aFeature->selection(SketchPlugin_Projection::EXTERNAL_FEATURE_ID());
+ else if (aFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
+ anExtFeature = aFeature->selection(SketchPlugin_IntersectionPoint::EXTERNAL_FEATURE_ID());
+ else
+ return;
+
+ ObjectPtr aContext = anExtFeature->context();
+ GeomShapePtr aShape = anExtFeature->value();
+ if (!aShape) { // selected object is a construction
+ ObjectPtr anAxis = findAxis(aContext, anOX, anOY, anOZ);
+ GeomShapePtr aNewAxis = axisOnNewPlane(anAxis, myPlane, thePlane);
+ anAxis = findAxis(aNewAxis, anOX, anOY, anOZ);
+ if (anAxis)
+ anExtFeature->setValue(anAxis, aShape);
+ }
+}