-void SketchPlugin_Split::fillObjectShapes(const ObjectPtr& theObject,
- const ObjectPtr& theSketch)
-{
- std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
- std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
- std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
- // current feature
- FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
- // edges on feature
- std::set<ResultPtr> anEdgeResults;
- ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeResults);
- if (!anEdgeResults.empty()) {
- GeomShapePtr aFeatureShape = (*anEdgeResults.begin())->shape();
-
- // coincidences to the feature
- ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
- aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
- // layed on feature coincidences to divide it on several shapes
- std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
- std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
- aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
- std::shared_ptr<GeomDataAPI_Dir> aX = 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()));
- std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
- std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
- ModelGeomAlgo_Point2D::getPointsInsideShape_p(aFeatureShape, aRefAttributes, aC->pnt(),
- aX->dir(), aY, aPoints, aPointToAttributes);
-
- GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
- }
- myCashedShapes[theObject] = aShapes;
- myCashedReferences[theObject] = aPointToAttributes;
-}
-
-GeomShapePtr SketchPlugin_Split::getSubShape(const std::string& theObjectAttributeId,
- const std::string& thePointAttributeId)
-{
- GeomShapePtr aBaseShape;
-
- AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
- data()->attribute(theObjectAttributeId));
- ObjectPtr aBaseObject = anObjectAttr->value();
- if (!aBaseObject.get())
- return aBaseShape;
-
- // point on feature
- AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
- data()->attribute(thePointAttributeId));
- std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
- std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
- anAttributePnt2d->y());
-
-#ifdef TRIM_SHAPE
- if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
- fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
-
- const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
- if (!aShapes.empty()) {
- std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
- for (; anIt != aLast; anIt++) {
- GeomShapePtr aShape = *anIt;
- std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
- if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
- aBaseShape = aShape;
- }
- }
-#else
- if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
- fillObjectShapes(aBaseObject, sketch()->data()->owner());
-
- std::shared_ptr<GeomAPI_Pnt> aStartPoint;
- std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
- const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
- std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
- for (; anIt != aLast; anIt++) {
- GeomShapePtr aCurrentShape = *anIt;
- std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
- if (ModelGeomAlgo_Point2D::isPointOnEdge(aCurrentShape, anAttributePnt, aProjectedPoint)) {
- if (aCurrentShape->shapeType() == GeomAPI_Shape::EDGE) {
- std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aCurrentShape));
- aStartPoint = anEdge->firstPoint();
- aSecondPoint = anEdge->lastPoint();
- }
- break;
- }
- }
-
- if (!aStartPoint.get() || !aSecondPoint.get())
- return aBaseShape;
-
- //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
- // data()->attribute(SketchPlugin_Constraint::VALUE()));
- FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject/*aBaseObjectAttr->value()*/);
-
- //AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
- // data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
- //AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
- // data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
- if (anObjectAttr->isInitialized() && aBaseFeature.get() && aPointAttr->isInitialized()) {
- //aFirstPointAttrOfSplit->isInitialized() &&
- //aSecondPointAttrOfSplit->isInitialized()) {
- ResultPtr aResult = getFeatureResult(aBaseFeature);
- GeomShapePtr aResultShape = aResult->shape();
- std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
-
- //std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
- //std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
- aPoints.push_back(aStartPoint);
-
- //std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
- //std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
- // sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
- aPoints.push_back(aSecondPoint);
-
- std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
- GeomAlgoAPI_ShapeTools::splitShape_p(aResultShape, aPoints, aSplitShapes);
- aBaseShape = GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
-#endif
- }
- return aBaseShape;
-}
-
-std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointOfRefAttr(
- const AttributePtr& theAttribute)
-{
- AttributePoint2DPtr aPointAttribute;
-
- if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
- AttributeRefAttrPtr aRefAttr =
- std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
- if (aRefAttr.get() && aRefAttr->isInitialized()) {
- AttributePtr anAttribute = aRefAttr->attr();
- if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
- aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
- }
- }
- return aPointAttribute;
-}
-
-void SketchPlugin_Split::getFeaturePoints(const FeaturePtr& theFeature,
- AttributePoint2DPtr& theStartPointAttr,
- AttributePoint2DPtr& theEndPointAttr)
-{
- std::string aFeatureKind = theFeature->getKind();
- std::string aStartAttributeName, anEndAttributeName;
- if (aFeatureKind == SketchPlugin_Line::ID()) {
- aStartAttributeName = SketchPlugin_Line::START_ID();
- anEndAttributeName = SketchPlugin_Line::END_ID();
- }
- else if (aFeatureKind == SketchPlugin_Arc::ID()) {
- aStartAttributeName = SketchPlugin_Arc::START_ID();
- anEndAttributeName = SketchPlugin_Arc::END_ID();
- }
- if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
- theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
- theFeature->attribute(aStartAttributeName));
- theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
- theFeature->attribute(anEndAttributeName));
- }
-}
-