1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Trim.cpp
4 // Created: 22 Feb 2017
5 // Author: Natalia ERMOLAEVA
7 #include "SketchPlugin_Trim.h"
9 #include <GeomAPI_Dir2d.h>
10 #include <GeomAPI_Edge.h>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomAPI_XY.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <GeomAlgoAPI_ShapeTools.h>
15 #include <GeomAlgoAPI_CompoundBuilder.h>
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_Tools.h>
21 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelGeomAlgo_Shape.h>
29 #include <SketchPlugin_Arc.h>
30 #include <SketchPlugin_ConstraintMiddle.h>
31 #include <SketchPlugin_Circle.h>
32 #include <SketchPlugin_ConstraintCoincidence.h>
33 #include <SketchPlugin_ConstraintEqual.h>
34 //#include <SketchPlugin_ConstraintParallel.h>
35 #include <SketchPlugin_ConstraintTangent.h>
36 #include <SketchPlugin_ConstraintLength.h>
37 #include <SketchPlugin_ConstraintMirror.h>
38 #include <SketchPlugin_ConstraintCollinear.h>
39 #include <SketchPlugin_Line.h>
40 #include <SketchPlugin_MultiRotation.h>
41 #include <SketchPlugin_MultiTranslation.h>
42 #include <SketchPlugin_Point.h>
44 #include <ModelAPI_EventReentrantMessage.h>
46 #include <ModelAPI_Events.h>
47 #include <SketchPlugin_Line.h>
48 #include <SketchPlugin_Arc.h>
49 #include <SketchPlugin_Circle.h>
51 #include <ModelGeomAlgo_Point2D.h>
52 #include <Events_Loop.h>
56 //#define DEBUG_TRIM_METHODS
63 #ifdef DEBUG_TRIM_METHODS
67 static const double PI = 3.141592653589793238463;
69 static const std::string OPERATION_HIGHLIGHT_COLOR() { return "128, 0, 0"; }
70 static const std::string OPERATION_REMOVE_FEATURE_COLOR() { return "255, 174, 201"; }
72 SketchPlugin_Trim::SketchPlugin_Trim()
76 void SketchPlugin_Trim::initAttributes()
78 data()->addAttribute(SketchPlugin_Trim::SELECTED_OBJECT(),
79 ModelAPI_AttributeReference::typeId());
80 data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
82 data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
83 data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
85 data()->attribute(PREVIEW_POINT())->setIsArgument(false);
86 data()->attribute(SELECTED_POINT())->setIsArgument(false);
87 data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
89 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
90 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
93 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
94 const std::string& thePointAttributeId,
95 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
96 std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
98 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
99 data()->attribute(theObjectAttributeId));
100 ObjectPtr aBaseObject = aBaseObjectAttr->value();
102 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
103 data()->attribute(thePointAttributeId));
104 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
105 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
106 anAttributePnt2d->y());
108 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
109 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
111 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
112 if (!aShapes.empty()) {
113 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
114 for (; anIt != aLast; anIt++) {
115 GeomShapePtr aBaseShape = *anIt;
116 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
117 if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, anAttributePnt, aProjectedPoint)) {
119 if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
120 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
121 //GeomAPI_Shape::Orientation anOrientation = anEdge->orientation();
122 //if (anOrientation == GeomAPI_Shape::REVERSED) {
123 aStartPoint = anEdge->lastPoint();
124 aLastPoint = anEdge->firstPoint();
127 //aStartPoint = anEdge->firstPoint();
128 //aLastPoint = anEdge->lastPoint();
135 std::cout << "<findShapePoints> => "
136 << std::endl << "Attribute point: "
137 << anAttributePnt->x() << ", " << anAttributePnt->y() << ", " << anAttributePnt->z() << "]"
138 << std::endl << "Start Point: ["
139 << aStartPoint->x() << ", " << aStartPoint->y() << ", " << aStartPoint->z() << "]"
140 << std::endl << "Last Point: ["
141 << aLastPoint->x() << ", " << aLastPoint->y() << ", " << aLastPoint->z() << "]"
146 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Trim::convertPoint(
147 const std::shared_ptr<GeomAPI_Pnt>& thePoint)
149 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
153 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
154 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
155 ObjectPtr aBaseObject = aBaseObjectAttr->value();
156 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
157 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
160 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
161 for (PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
162 aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
163 if (aPointIt->first->isEqual(thePoint)) {
164 const std::pair<std::list<AttributePoint2DPtr >,
165 std::list<ObjectPtr > >& anInfo = aPointIt->second;
166 const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
167 if (!anAttributes.empty()) {
168 aPoint = anAttributes.front()->pnt();
172 aPoint = sketch()->to2D(thePoint);
178 // returns an end of the shape to define direction of split if feature's attribute
180 aPoint = sketch()->to2D(thePoint);
185 void SketchPlugin_Trim::execute()
187 #ifdef DEBUG_TRIM_METHODS
188 std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
191 SketchPlugin_Sketch* aSketch = sketch();
193 setError("Error: Sketch object is empty.");
197 // Check the base objects are initialized.
198 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
199 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
200 if(!aBaseObjectAttr->isInitialized()) {
201 setError("Error: Base object is not initialized.");
204 ObjectPtr aBaseObject = aBaseObjectAttr->value();
205 if (!aBaseObject.get()) {
206 setError("Error: Base object is not initialized.");
209 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
211 /// Remove reference of this feature to feature used in preview, it is not necessary anymore
212 /// as trim will be removed after execute
213 AttributeReferencePtr aPreviewObjectAttr =
214 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
215 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
217 ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
218 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219 data()->attribute(PREVIEW_POINT()));
220 std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
221 // nullify pointer of preview attribute
222 aPreviewObjectAttr->setValue(ResultPtr());
224 bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
227 std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
229 std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
231 findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
233 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
234 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
236 std::set<FeaturePtr> aFeaturesToDelete;
237 getConstraints(aFeaturesToDelete);
239 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
240 std::list<AttributePtr> aRefsToFeature;
241 getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
243 std::cout << "---- getRefAttributes ----" << std::endl;
244 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
245 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
246 std::cout << std::endl << "References to attributes of base feature [" <<
247 aBaseRefAttributes.size() << "]" << std::endl;
248 for (; aRefIt != aRefLast; aRefIt++) {
249 AttributePtr aBaseAttr = aRefIt->first;
250 std::list<AttributePtr> aRefAttributes = aRefIt->second;
251 std::string aRefsInfo;
252 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
253 aRefAttrLast = aRefAttributes.end();
254 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
255 if (!aRefsInfo.empty())
256 aRefsInfo.append(",");
257 AttributePtr aRAttr = *aRefAttrIt;
258 aRefsInfo.append(aRAttr->id());
259 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
260 aRefsInfo.append("(" + aRFeature->name() + ") ");
262 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
263 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
264 std::cout << aPointAttr->id().c_str() <<
265 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
267 std::cout << std::endl;
268 std::cout << std::endl << "References to base feature [" <<
269 aRefsToFeature.size() << "]" << std::endl;
270 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
271 aRefAttrLast = aRefsToFeature.end();
272 std::string aRefsInfo;
273 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
274 if (!aRefsInfo.empty())
275 aRefsInfo.append(",");
276 AttributePtr aRAttr = *aRefAttrIt;
277 aRefsInfo.append(aRAttr->id());
278 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
279 aRefsInfo.append("(" + aRFeature->name() + ") ");
281 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
282 std::cout << "---- getRefAttributes:end ----" << std::endl;
284 // coincidence to result points
285 // find coincidences to the base object, it should be used when attribute is found
286 // in myObjectToPoints
287 //std::map<AttributePtr, FeaturePtr> aCoincidencesToBaseFeature;
288 //getCoincidencesToObject(aBaseObject, aCoincidencesToBaseFeature);
290 std::set<AttributePoint2DPtr> aFurtherCoincidences;
291 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
292 const std::string& aKind = aBaseFeature->getKind();
293 FeaturePtr aReplacingFeature, aNewFeature;
294 if (aKind == SketchPlugin_Circle::ID()) {
295 aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
296 aFurtherCoincidences, aModifiedAttributes);
298 aFeaturesToDelete.insert(aBaseFeature);
299 // as circle is removed, erase it from dependencies(arguments) of this feature
300 // otherwise Trim feature will be removed with the circle before
301 // this operation is finished
302 aBaseObjectAttr->setObject(ResultPtr());
304 else if (aKind == SketchPlugin_Line::ID()) {
305 aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
306 aFurtherCoincidences, aModifiedAttributes);
308 else if (aKind == SketchPlugin_Arc::ID()) {
309 aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
310 aFurtherCoincidences, aModifiedAttributes);
313 // constraints to end points of trim feature
314 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
315 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
317 // create coincidence to objects, intersected the base object
318 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
319 for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
320 aLast = aFurtherCoincidences.end();
321 anIt != aLast; anIt++) {
322 AttributePoint2DPtr aPointAttribute = (*anIt);
323 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
326 std::cout << "<compare Points> => " << std::endl
327 << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
328 if (aStartShapePoint2d.get())
329 std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
331 if (aLastShapePoint2d.get())
332 std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
336 std::shared_ptr<GeomAPI_Pnt> aPoint;
337 if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
338 aPoint = aStartShapePoint;
339 else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
340 aPoint = aLastShapePoint;
345 std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
346 for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
349 if (aRefIt->first->isEqual(aPoint)) {
350 anInfo = aRefIt->second;
354 const std::list<ObjectPtr>& anObjects = anInfo.second;
355 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
356 anObjectIt != anObjects.end(); anObjectIt++) {
357 createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute,
362 // move constraints from base feature to replacing feature: ignore coincidences to feature
363 // if attributes of coincidence participated in split
364 ResultPtr aReplacingResult;
365 if (aReplacingFeature.get()) {
366 aReplacingFeature->execute(); // need it to obtain result
367 aReplacingResult = getFeatureResult(aReplacingFeature);
369 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
370 aLast = aRefsToFeature.end();
371 anIt != aLast; anIt++) {
372 AttributePtr anAttribute = *anIt;
374 //if (replaceCoincidenceAttribute(anAttribute, aModifiedAttributes))
377 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences))
380 if (aReplacingResult.get()) {
381 AttributeRefAttrPtr aRefAttr =
382 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
384 aRefAttr->setObject(aReplacingResult);
386 AttributeReferencePtr aReferenceAttr =
387 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
388 if (aReferenceAttr.get())
389 aReferenceAttr->setObject(aReplacingResult);
394 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
396 // Wait all constraints being created, then send update events
397 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
398 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
400 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
402 // delete constraints
404 if (aFeaturesToDelete.size() > 0) {
405 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
407 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
408 anIt != aFeaturesToDelete.end(); anIt++) {
409 FeaturePtr aFeature = *anIt;
410 std::cout << aFeature->data()->name() << std::endl;
414 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
415 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
417 // Send events to update the sub-features by the solver.
418 if(isUpdateFlushed) {
419 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
422 if (anIsEqualPreviewAndSelected) {
423 // equal preview and selected objects
424 // nothing to do if the preview and selected objects are different
425 if (aReplacingResult.get()) { // base object was removed
426 aPreviewObject = aReplacingResult;
427 //aMessage->setSelectedObject(aReplacingResult);
429 GeomShapePtr aSelectedShape = aReplacingResult->shape();
430 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
432 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
433 if (ModelGeomAlgo_Point2D::isPointOnEdge(aSelectedShape, aPreviewPnt, aProjectedPoint)) {
436 //aBaseShape = aShape;
438 #ifdef DEBUG_TRIM_METHODS
439 if (!aSelectedShape.get())
440 std::cout << "Set empty selected object" << std::endl;
442 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
447 aPreviewObject = ObjectPtr();
449 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
450 aBaseObject = getFeatureResult(aBaseFeature);
451 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
453 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
455 GeomShapePtr aShape = aBaseResult->shape();
456 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
457 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
458 aPreviewObject = aBaseResult;
460 if (!aPreviewObject.get() && aNewFeature.get()) {
461 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
462 if (aNewFeatureResult.get()) {
463 GeomShapePtr aShape = aNewFeatureResult->shape();
464 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
465 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
466 aPreviewObject = aNewFeatureResult;
471 if (aPreviewObject.get()) {
472 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
473 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
474 ModelAPI_EventReentrantMessage::eventId(), this));
475 aMessage->setSelectedObject(aPreviewObject);
476 Events_Loop::loop()->send(aMessage);
479 std::cout << "SketchPlugin_Trim::done" << std::endl;
483 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
485 #ifdef DEBUG_TRIM_METHODS
486 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
488 std::string aFilledAttributeName;
490 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
491 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
492 if (aMessage.get()) {
493 ObjectPtr anObject = aMessage->selectedObject();
494 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
496 if (anObject.get() && aPoint.get()) {
497 if (myCashedShapes.find(anObject) == myCashedShapes.end())
498 fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
499 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
500 if (aShapes.size() > 1) {
501 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
502 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
503 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
504 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
505 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
506 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
507 aRefSelectedAttr->setValue(anObject);
508 aRefPreviewAttr->setValue(anObject);
510 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
511 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
512 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
513 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
514 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
515 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
516 aPointSelectedAttr->setValue(aPoint);
517 aPointPreviewAttr->setValue(aPoint);
519 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
521 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
522 #ifdef DEBUG_TRIM_METHODS
523 if (!aSelectedShape.get())
524 std::cout << "Set empty selected object" << std::endl;
526 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
528 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
532 return aFilledAttributeName;
535 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
536 const std::set<AttributePoint2DPtr>& theFurtherCoincidences)
538 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
539 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
542 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
543 if (!aRefPointAttr.get())
545 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
547 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
548 aLast = theFurtherCoincidences.end();
549 bool aFoundPoint = false;
550 for (; anIt != aLast && !aFoundPoint; anIt++) {
551 AttributePoint2DPtr aPointAttribute = (*anIt);
552 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
553 if (aPoint2d->isEqual(aRefPnt2d)) {
554 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
556 if (aRefAttr.get()) {
557 aRefAttr->setAttr(aPointAttribute);
565 bool SketchPlugin_Trim::replaceCoincidenceAttribute(const AttributePtr& theCoincidenceAttribute,
566 const std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
568 FeaturePtr aCoincidenceFeature = ModelAPI_Feature::feature(theCoincidenceAttribute->owner());
569 if (aCoincidenceFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
572 AttributeRefAttrPtr aCAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
573 aCoincidenceFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
574 AttributeRefAttrPtr aCAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
575 aCoincidenceFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
576 AttributePtr aCAttrRefA = aCAttrA->attr();
577 AttributePtr aCAttrRefB = aCAttrB->attr();
579 bool isProcessed = false;
580 for (std::set<std::pair<AttributePtr, AttributePtr>>::const_iterator
581 anIt = theModifiedAttributes.begin(); anIt != theModifiedAttributes.end(); anIt++) {
582 AttributePtr anAttributeBefore = anIt->first;
583 if (anAttributeBefore == aCAttrRefA) {
584 aCAttrA->setAttr(anIt->second);
587 if (anAttributeBefore == aCAttrRefB) {
588 aCAttrB->setAttr(anIt->second);
595 bool SketchPlugin_Trim::isMacro() const
600 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
602 #ifdef DEBUG_TRIM_METHODS
603 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
606 AISObjectPtr anAIS = thePrevious;
608 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
609 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
610 if (aPreviewShape.get())
611 aShapes.push_back(aPreviewShape);
612 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
613 if (aSelectedShape.get())
614 aShapes.push_back(aSelectedShape);
617 return AISObjectPtr();
619 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
620 if (!aBaseShape.get())
621 return AISObjectPtr();
623 if (aBaseShape.get()) {
625 anAIS = AISObjectPtr(new GeomAPI_AISObject);
626 anAIS->createShape(aBaseShape);
628 std::vector<int> aColor;
629 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
630 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
631 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
632 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
633 // width when there is not base object should be extened in several points
634 // in order to see this preview over highlight
635 anAIS->setWidth(aWidth+4);
636 anAIS->setLineStyle(aLineStyle);
639 anAIS = AISObjectPtr();
644 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
645 const std::string& thePointAttributeId)
647 GeomShapePtr aBaseShape;
649 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
650 data()->attribute(theObjectAttributeId));
651 ObjectPtr aBaseObject = anObjectAttr->value();
652 if (!aBaseObject.get())
656 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
657 data()->attribute(thePointAttributeId));
658 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
659 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
660 anAttributePnt2d->y());
662 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
663 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
665 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
666 if (!aShapes.empty()) {
667 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
668 for (; anIt != aLast; anIt++) {
669 GeomShapePtr aShape = *anIt;
670 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
671 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
678 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
679 AttributePoint2DPtr& theStartPointAttr,
680 AttributePoint2DPtr& theEndPointAttr)
682 std::string aFeatureKind = theFeature->getKind();
683 std::string aStartAttributeName, anEndAttributeName;
684 if (aFeatureKind == SketchPlugin_Line::ID()) {
685 aStartAttributeName = SketchPlugin_Line::START_ID();
686 anEndAttributeName = SketchPlugin_Line::END_ID();
688 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
689 aStartAttributeName = SketchPlugin_Arc::START_ID();
690 anEndAttributeName = SketchPlugin_Arc::END_ID();
692 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
693 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
694 theFeature->attribute(aStartAttributeName));
695 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
696 theFeature->attribute(anEndAttributeName));
700 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete)
702 std::shared_ptr<ModelAPI_Data> aData = data();
704 // Check the base objects are initialized.
705 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
706 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
707 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
708 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
710 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
711 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
712 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
714 std::set<AttributePtr>::const_iterator aIt;
715 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
716 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
717 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
718 std::string aRefFeatureKind = aRefFeature->getKind();
719 if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
720 aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
721 aRefFeatureKind == SketchPlugin_MultiTranslation::ID() ||
722 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
723 theFeaturesToDelete.insert(aRefFeature);
727 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
728 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
729 std::list<AttributePtr>& theRefsToFeature)
733 std::list<AttributePtr> aPointAttributes =
734 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
735 std::set<AttributePtr> aPointAttributesSet;
737 std::list<AttributePtr>::const_iterator aPIt =
738 aPointAttributes.begin(), aPLast = aPointAttributes.end();
739 for (; aPIt != aPLast; aPIt++)
740 aPointAttributesSet.insert(*aPIt);
742 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
743 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
744 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
746 std::set<AttributePtr>::const_iterator aIt;
747 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
748 AttributePtr anAttr = (*aIt);
749 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
750 if (anAttrFeature.get() != this &&
751 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
752 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
753 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
754 AttributePtr anAttrInRef = aRefAttr->attr();
755 if (anAttrInRef.get() &&
756 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
757 if (theRefs.find(anAttrInRef) != theRefs.end())
758 theRefs[anAttrInRef].push_back(aRefAttr);
760 std::list<AttributePtr> anAttrList;
761 anAttrList.push_back(aRefAttr);
762 theRefs[anAttrInRef] = anAttrList;
766 else { /// find attributes referenced to feature itself
767 theRefsToFeature.push_back(anAttr);
773 /*void SketchPlugin_Trim::getCoincidencesToObject(const ObjectPtr& theObject,
774 std::map<AttributePtr, FeaturePtr>& theCoincidencesToBaseFeature)
776 const std::set<AttributePtr>& aRefsList = theObject->data()->refsToMe();
777 std::set<AttributePtr>::const_iterator aIt;
778 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
779 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
780 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
781 if (aRefFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
783 AttributePtr anAttribute;
784 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
785 (aRefFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
786 if (aRefAttr->isObject() && aRefAttr->object() == theObject)
788 anAttribute = aRefFeature->attribute(SketchPlugin_Constraint::ENTITY_B());
791 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
792 (aRefFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
793 if (aRefAttr->isObject() && aRefAttr->object() == theObject)
794 anAttribute = aRefFeature->attribute(SketchPlugin_Constraint::ENTITY_A());
796 if (!anAttribute.get())
799 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
800 if (aRefAttr->isObject())
801 continue; // one of attributes of coincidence contains link to an attribute
803 anAttribute = aRefAttr->attr();
804 if (anAttribute.get())
806 theCoincidencesToBaseFeature[anAttribute] = aRefFeature;
811 void SketchPlugin_Trim::updateRefAttConstraints(
812 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
813 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
814 std::set<FeaturePtr>& theFeaturesToDelete)
817 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
820 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
821 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
822 for (; anIt != aLast; anIt++) {
823 AttributePtr anAttribute = anIt->first;
825 /// not found in references
826 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
828 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
829 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
830 aRLast = aRefAttributes.end();
832 AttributePtr aNewAttribute = anIt->second;
833 if (aNewAttribute.get()) {
834 for (; aRefIt != aRLast; aRefIt++) {
835 AttributeRefAttrPtr aRefAttr =
836 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
837 if (aRefAttr.get()) {
838 aRefAttr->setAttr(aNewAttribute);
845 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
846 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
848 /// not found in references
849 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
852 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
853 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
854 aRLast = aRefAttributes.end();
856 std::set<FeaturePtr> aFeaturesToDelete;
857 for (; aRefIt != aRLast; aRefIt++) {
858 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
859 if (aRefAttr.get()) {
860 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
865 // delete constraints
866 if (aFeaturesToDelete.size() > 0) {
867 std::cout << "removeReferencesToAttribute: " << std::endl;
869 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
870 anIt != aFeaturesToDelete.end(); anIt++) {
871 FeaturePtr aFeature = *anIt;
872 std::cout << aFeature->data()->name() << std::endl;
876 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
877 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
880 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
881 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
882 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
883 std::set<AttributePoint2DPtr>& thePoints,
884 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
886 FeaturePtr anNewFeature;
888 // Check the base objects are initialized.
889 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
890 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
891 ObjectPtr aBaseObject = aBaseObjectAttr->value();
892 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
895 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
896 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
898 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
899 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
901 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
902 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
903 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
904 aStartShapePoint, aLastShapePoint);
906 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
907 if (aStartShapePoint.get())
908 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
909 aStartShapePoint->y() << "]" << std::endl;
910 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
911 aStartFeaturePoint->y() << "]" << std::endl;
912 if (aLastShapePoint.get())
913 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
914 aLastShapePoint->y() << "]" << std::endl;
915 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
916 aLastFeaturePoint->y() << "]" << std::endl;
919 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
920 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
921 if (isStartPoint || isLastPoint) {
922 // result is one line: changed existing line
923 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
924 : SketchPlugin_Line::END_ID();
925 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
926 if (aStartShapePoint.get() && aLastShapePoint.get())
927 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
929 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
931 // it is important to delete references before the feature modification because
932 // if deletion will be after the feature modification, solver returns the feature back
933 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
934 theBaseRefAttributes);
936 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
937 //theModifiedAttributes.insert(
938 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
940 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
941 (aBaseFeature->attribute(aModifiedAttribute)));
944 // result is two lines: start line point - start shape point,
945 // last shape point - last line point
946 // create second line
947 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
948 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
949 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
951 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
952 theModifiedAttributes.insert(
953 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
954 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
957 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
959 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
960 (aBaseFeature->attribute(aModifiedAttribute)));
962 // Collinear constraint for lines
963 createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(),
964 getFeatureResult(aBaseFeature),
965 getFeatureResult(anNewFeature));
971 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
972 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
973 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
974 std::set<AttributePoint2DPtr>& thePoints,
975 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
977 FeaturePtr anNewFeature;
978 // Check the base objects are initialized.
979 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
980 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
981 ObjectPtr aBaseObject = aBaseObjectAttr->value();
982 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
985 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
986 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
988 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
989 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
991 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
992 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
993 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
994 aStartShapePoint, aLastShapePoint);
996 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
997 if (aStartShapePoint.get())
998 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
999 aStartShapePoint->y() << "]" << std::endl;
1000 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1001 aStartArcPoint->y() << "]" << std::endl;
1002 if (aLastShapePoint.get())
1003 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1004 aLastShapePoint->y() << "]" << std::endl;
1005 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1006 aLastArcPoint->y() << "]" << std::endl;
1009 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1010 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1011 if (isStartPoint || isLastPoint) {
1012 // result is one arc: changed existing arc
1013 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1014 : SketchPlugin_Arc::END_ID();
1015 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1016 if (aStartShapePoint.get() && aLastShapePoint.get())
1017 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1019 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1021 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1022 theBaseRefAttributes);
1024 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1026 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1027 (aBaseFeature->attribute(aModifiedAttribute)));
1030 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1031 // create second arc
1032 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1033 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1034 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1036 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1037 theModifiedAttributes.insert(
1038 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1039 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1042 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1044 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1045 (aBaseFeature->attribute(aModifiedAttribute)));
1047 // equal Radius constraint for arcs
1048 anNewFeature->execute(); // we need the created arc result to set equal constraint
1049 createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1050 getFeatureResult(aBaseFeature),
1051 getFeatureResult(anNewFeature));
1052 // coincident centers constraint
1053 createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1054 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1055 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1058 std::cout << "Created arc on points:" << std::endl;
1059 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1060 aStartShapePoint->y() << "]" << std::endl;
1063 return anNewFeature;
1066 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1067 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1068 std::set<AttributePoint2DPtr>& thePoints,
1069 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1071 // Check the base objects are initialized.
1072 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1073 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1074 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1075 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1078 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1079 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1082 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1083 // arc created by trim of circle is always correct, that means that it is not inversed
1084 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1086 theModifiedAttributes.insert(
1087 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1088 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1090 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1091 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1092 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1093 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1095 return anNewFeature;
1098 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1099 const AttributePoint2DPtr& theEndPointAttr,
1100 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1101 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1103 if (!theFirstPoint.get() || !theLastPoint.get())
1106 // if first point is closer to last point, swap first and last values
1107 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1108 theStartPointAttr->pnt()->distance(theLastPoint)) {
1109 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1110 theFirstPoint = theLastPoint;
1111 theLastPoint = aTmpPoint;
1115 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1116 const AttributePoint2DPtr& theStartPointAttr,
1117 const AttributePoint2DPtr& theEndPointAttr,
1118 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1119 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1121 if (!theFirstPoint.get() || !theSecondPoint.get())
1124 static const double anAngleTol = 1.e-12;
1126 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1127 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1128 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1130 // collect directions to each point
1131 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1132 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1133 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1134 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1135 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1136 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1138 // sort points by their angular values
1139 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1140 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1141 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1142 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1143 aFirstPtAngle += aPeriod;
1144 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1145 aSecondPtAngle += aPeriod;
1147 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1148 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1149 theFirstPoint = theSecondPoint;
1150 theSecondPoint = aTmpPoint;
1154 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1155 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1157 std::string anAttributeType = theModifiedAttribute->attributeType();
1158 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1159 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1160 theModifiedAttribute);
1161 aModifiedAttribute->setValue(thePoint);
1164 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1165 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1166 theModifiedAttribute->id() <<
1167 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1173 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1174 const AttributePtr& theSourceAttribute)
1176 std::string anAttributeType = theModifiedAttribute->attributeType();
1177 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1178 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1179 theModifiedAttribute);
1180 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1181 theSourceAttribute);
1183 if (aModifiedAttribute.get() && aSourceAttribute.get())
1184 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1186 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1187 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1188 theModifiedAttribute);
1189 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1190 theSourceAttribute);
1192 if (aModifiedAttribute.get() && aSourceAttribute.get())
1193 aModifiedAttribute->setValue(aSourceAttribute->value());
1195 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1196 AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1197 theModifiedAttribute);
1198 AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1199 theSourceAttribute);
1200 if (!aSourceRefAttr.get())
1201 aRefAttributeToFill->setAttr(theSourceAttribute);
1203 if (aSourceRefAttr->isObject())
1204 aRefAttributeToFill->setObject(aSourceRefAttr->object());
1206 aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1211 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1212 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1213 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1216 std::cout << "---- createLineFeature ---" << std::endl;
1219 FeaturePtr aFeature;
1220 SketchPlugin_Sketch* aSketch = sketch();
1221 if (!aSketch || !theBaseFeature.get())
1224 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1226 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1227 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1229 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1230 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1232 aFeature->execute(); // to obtain result
1235 std::cout << "---- createLineFeature:end ---" << std::endl;
1241 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1242 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1243 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1245 FeaturePtr aFeature;
1246 SketchPlugin_Sketch* aSketch = sketch();
1247 if (!aSketch || !theBaseFeature.get())
1250 std::string aCenterAttributeId;
1251 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1252 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1253 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1254 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1256 if (aCenterAttributeId.empty())
1260 std::cout << "---- createArcFeature ---" << std::endl;
1263 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1264 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1265 // the "attribute updated"
1266 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1267 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1269 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1270 theBaseFeature->attribute(aCenterAttributeId));
1271 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1272 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1274 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1275 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1277 /// fill referersed state of created arc as it is on the base arc
1278 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1279 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1280 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1282 //aFeature->execute(); // to obtain result
1283 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1286 std::cout << "---- createArcFeature:end ---" << std::endl;
1292 FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId,
1293 const AttributePtr& theFirstAttribute,
1294 const AttributePtr& theSecondAttribute)
1296 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1297 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1298 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1299 aRefAttr->setAttr(theFirstAttribute);
1301 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1302 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1303 aRefAttr->setAttr(theSecondAttribute);
1306 std::cout << "<createConstraint to attribute> :"
1307 << "first attribute - " << theFirstAttribute->id()
1308 << "second attribute - " << theSecondAttribute->id()
1315 FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId,
1316 const AttributePtr& theFirstAttribute,
1317 const ObjectPtr& theSecondObject)
1319 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1320 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1321 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1322 aRefAttr->setAttr(theFirstAttribute);
1324 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1325 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1326 aRefAttr->setObject(theSecondObject);
1329 std::cout << "<createConstraint to attribute> :"
1330 << "first attribute - " << theFirstAttribute->id()
1331 << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
1338 FeaturePtr SketchPlugin_Trim::createConstraintForObjects(
1339 const std::string& theConstraintId,
1340 const ObjectPtr& theFirstObject,
1341 const ObjectPtr& theSecondObject)
1343 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1344 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1345 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1346 aRefAttr->setObject(theFirstObject);
1348 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1349 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1350 aRefAttr->setObject(theSecondObject);
1355 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1356 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1358 std::shared_ptr<ModelAPI_Result> aResult;
1360 std::string aFeatureKind = theFeature->getKind();
1361 if (aFeatureKind == SketchPlugin_Line::ID())
1362 aResult = theFeature->firstResult();
1363 else if (aFeatureKind == SketchPlugin_Arc::ID())
1364 aResult = theFeature->lastResult();
1365 else if (aFeatureKind == SketchPlugin_Circle::ID())
1366 aResult = theFeature->lastResult();
1371 //********************************************************************
1372 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1373 const ObjectPtr& theSketch,
1374 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1375 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1377 PointToRefsMap aPointsInfo;
1379 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1380 std::map<std::shared_ptr<GeomAPI_Pnt>,
1381 std::list< AttributePoint2DPtr > > aPointToAttributes;
1382 std::map<std::shared_ptr<GeomAPI_Pnt>,
1383 std::list< ObjectPtr > > aPointToObjects;
1385 std::set<AttributePoint2DPtr > aRefAttributes;
1387 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1388 std::set<ResultPtr> anEdgeShapes;
1390 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1391 if (!anEdgeShapes.empty()) {
1392 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1394 // coincidences to the feature
1395 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1396 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1397 // layed on feature coincidences to divide it on several shapes
1398 //SketchPlugin_Sketch* aSketch = sketch();
1399 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1400 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1401 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1402 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1403 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1404 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1405 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1406 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1408 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1409 aX->dir(), aY, aPointsInfo);
1411 std::list<FeaturePtr> aFeatures;
1412 CompositeFeaturePtr aSketchComposite =
1413 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1414 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1415 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1417 aFeatures.push_back(aFeature);
1419 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1421 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1423 theObjectToPoints[theObject] = aPointsInfo;
1424 theCashedShapes[theObject] = aShapes;