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(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
79 data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
81 data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
82 data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
84 data()->attribute(PREVIEW_POINT())->setIsArgument(false);
85 data()->attribute(SELECTED_POINT())->setIsArgument(false);
86 data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
88 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
89 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
92 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
93 const std::string& thePointAttributeId,
94 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
95 std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
97 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
98 data()->attribute(theObjectAttributeId));
99 ObjectPtr aBaseObject = aBaseObjectAttr->value();
101 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102 data()->attribute(thePointAttributeId));
103 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
104 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
105 anAttributePnt2d->y());
107 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
108 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
110 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
111 if (!aShapes.empty()) {
112 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
113 for (; anIt != aLast; anIt++) {
114 GeomShapePtr aBaseShape = *anIt;
115 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
116 if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, anAttributePnt, aProjectedPoint)) {
118 if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
119 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
120 //GeomAPI_Shape::Orientation anOrientation = anEdge->orientation();
121 //if (anOrientation == GeomAPI_Shape::REVERSED) {
122 aStartPoint = anEdge->lastPoint();
123 aLastPoint = anEdge->firstPoint();
126 //aStartPoint = anEdge->firstPoint();
127 //aLastPoint = anEdge->lastPoint();
134 std::cout << "<findShapePoints> => "
135 << std::endl << "Attribute point: "
136 << anAttributePnt->x() << ", " << anAttributePnt->y() << ", " << anAttributePnt->z() << "]"
137 << std::endl << "Start Point: ["
138 << aStartPoint->x() << ", " << aStartPoint->y() << ", " << aStartPoint->z() << "]"
139 << std::endl << "Last Point: ["
140 << aLastPoint->x() << ", " << aLastPoint->y() << ", " << aLastPoint->z() << "]"
145 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Trim::convertPoint(
146 const std::shared_ptr<GeomAPI_Pnt>& thePoint)
148 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
152 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
153 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
154 ObjectPtr aBaseObject = aBaseObjectAttr->value();
155 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
156 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
159 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
160 for (PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
161 aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
162 if (aPointIt->first->isEqual(thePoint)) {
163 const std::pair<std::list<AttributePoint2DPtr >,
164 std::list<ObjectPtr > >& anInfo = aPointIt->second;
165 const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
166 if (!anAttributes.empty()) {
167 aPoint = anAttributes.front()->pnt();
171 aPoint = sketch()->to2D(thePoint);
177 // returns an end of the shape to define direction of split if feature's attribute
179 aPoint = sketch()->to2D(thePoint);
184 void SketchPlugin_Trim::execute()
186 #ifdef DEBUG_TRIM_METHODS
187 std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
190 SketchPlugin_Sketch* aSketch = sketch();
192 setError("Error: Sketch object is empty.");
196 // Check the base objects are initialized.
197 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
198 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
199 if(!aBaseObjectAttr->isInitialized()) {
200 setError("Error: Base object is not initialized.");
203 ObjectPtr aBaseObject = aBaseObjectAttr->value();
204 if (!aBaseObject.get()) {
205 setError("Error: Base object is not initialized.");
208 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
210 /// Remove reference of this feature to feature used in preview, it is not necessary anymore
211 /// as trim will be removed after execute
212 AttributeReferencePtr aPreviewObjectAttr =
213 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
214 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
216 ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
217 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
218 data()->attribute(PREVIEW_POINT()));
219 std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
220 // nullify pointer of preview attribute
221 aPreviewObjectAttr->setValue(ResultPtr());
223 bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
226 std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
228 std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
230 findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
232 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
233 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
234 /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
235 std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
236 getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
237 // find references(attributes and features) to the base feature
238 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
239 std::list<AttributePtr> aRefsToFeature;
240 getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
242 std::cout << "---- getRefAttributes ----" << std::endl;
243 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
244 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
245 std::cout << std::endl << "References to attributes of base feature [" <<
246 aBaseRefAttributes.size() << "]" << std::endl;
247 for (; aRefIt != aRefLast; aRefIt++) {
248 AttributePtr aBaseAttr = aRefIt->first;
249 std::list<AttributePtr> aRefAttributes = aRefIt->second;
250 std::string aRefsInfo;
251 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
252 aRefAttrLast = aRefAttributes.end();
253 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
254 if (!aRefsInfo.empty())
255 aRefsInfo.append(",");
256 AttributePtr aRAttr = *aRefAttrIt;
257 aRefsInfo.append(aRAttr->id());
258 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
259 aRefsInfo.append("(" + aRFeature->name() + ") ");
261 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
262 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
263 std::cout << aPointAttr->id().c_str() <<
264 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
266 std::cout << std::endl;
267 std::cout << std::endl << "References to base feature [" <<
268 aRefsToFeature.size() << "]" << std::endl;
269 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
270 aRefAttrLast = aRefsToFeature.end();
271 std::string aRefsInfo;
272 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
273 if (!aRefsInfo.empty())
274 aRefsInfo.append(",");
275 AttributePtr aRAttr = *aRefAttrIt;
276 aRefsInfo.append(aRAttr->id());
277 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
278 aRefsInfo.append("(" + aRFeature->name() + ") ");
280 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
281 std::cout << "---- getRefAttributes:end ----" << std::endl;
283 std::set<AttributePoint2DPtr> aFurtherCoincidences;
284 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
285 const std::string& aKind = aBaseFeature->getKind();
286 FeaturePtr aReplacingFeature, aNewFeature;
287 if (aKind == SketchPlugin_Circle::ID()) {
288 aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
289 aFurtherCoincidences, aModifiedAttributes);
291 aFeaturesToDelete.insert(aBaseFeature);
292 // as circle is removed, erase it from dependencies(arguments) of this feature
293 // otherwise Trim feature will be removed with the circle before
294 // this operation is finished
295 aBaseObjectAttr->setObject(ResultPtr());
297 else if (aKind == SketchPlugin_Line::ID()) {
298 aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
299 aFurtherCoincidences, aModifiedAttributes);
301 else if (aKind == SketchPlugin_Arc::ID()) {
302 aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
303 aFurtherCoincidences, aModifiedAttributes);
306 // constraints to end points of trim feature
307 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
308 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
310 // create coincidence to objects, intersected the base object
311 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
312 for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
313 aLast = aFurtherCoincidences.end();
314 anIt != aLast; anIt++) {
315 AttributePoint2DPtr aPointAttribute = (*anIt);
316 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
319 std::cout << "<compare Points> => " << std::endl
320 << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
321 if (aStartShapePoint2d.get())
322 std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
324 if (aLastShapePoint2d.get())
325 std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
329 std::shared_ptr<GeomAPI_Pnt> aPoint;
330 if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
331 aPoint = aStartShapePoint;
332 else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
333 aPoint = aLastShapePoint;
338 std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
339 for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
342 if (aRefIt->first->isEqual(aPoint)) {
343 anInfo = aRefIt->second;
347 const std::list<ObjectPtr>& anObjects = anInfo.second;
348 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
349 anObjectIt != anObjects.end(); anObjectIt++) {
350 createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute,
355 // move constraints from base feature to replacing feature: ignore coincidences to feature
356 // if attributes of coincidence participated in split
357 ResultPtr aReplacingResult;
358 if (aReplacingFeature.get()) {
359 aReplacingFeature->execute(); // need it to obtain result
360 aReplacingResult = getFeatureResult(aReplacingFeature);
362 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
363 aLast = aRefsToFeature.end();
364 anIt != aLast; anIt++) {
365 AttributePtr anAttribute = *anIt;
367 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences))
370 // move tangency constraint to the nearest feature if possible
371 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
374 if (aReplacingResult.get()) {
375 AttributeRefAttrPtr aRefAttr =
376 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
378 aRefAttr->setObject(aReplacingResult);
380 AttributeReferencePtr aReferenceAttr =
381 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
382 if (aReferenceAttr.get())
383 aReferenceAttr->setObject(aReplacingResult);
388 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
390 // Wait all constraints being created, then send update events
391 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
392 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
394 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
396 // delete constraints
398 if (aFeaturesToDelete.size() > 0) {
399 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
401 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
402 anIt != aFeaturesToDelete.end(); anIt++) {
403 FeaturePtr aFeature = *anIt;
404 std::cout << aFeature->data()->name() << std::endl;
408 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
409 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
411 updateFeaturesAfterTrim(aFeaturesToUpdate);
413 // Send events to update the sub-features by the solver.
414 if(isUpdateFlushed) {
415 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
418 if (anIsEqualPreviewAndSelected) {
419 // equal preview and selected objects
420 // nothing to do if the preview and selected objects are different
421 if (aReplacingResult.get()) { // base object was removed
422 aPreviewObject = aReplacingResult;
423 //aMessage->setSelectedObject(aReplacingResult);
424 #ifdef DEBUG_TRIM_METHODS
425 if (!aSelectedShape.get())
426 std::cout << "Set empty selected object" << std::endl;
428 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
432 aPreviewObject = ObjectPtr();
434 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
435 aBaseObject = getFeatureResult(aBaseFeature);
436 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
438 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
440 GeomShapePtr aShape = aBaseResult->shape();
441 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
442 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
443 aPreviewObject = aBaseResult;
445 if (!aPreviewObject.get() && aNewFeature.get()) {
446 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
447 if (aNewFeatureResult.get()) {
448 GeomShapePtr aShape = aNewFeatureResult->shape();
449 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
450 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
451 aPreviewObject = aNewFeatureResult;
456 if (aPreviewObject.get()) {
457 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
458 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
459 ModelAPI_EventReentrantMessage::eventId(), this));
460 aMessage->setSelectedObject(aPreviewObject);
461 Events_Loop::loop()->send(aMessage);
464 std::cout << "SketchPlugin_Trim::done" << std::endl;
468 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
470 #ifdef DEBUG_TRIM_METHODS
471 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
473 std::string aFilledAttributeName;
475 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
476 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
477 if (aMessage.get()) {
478 ObjectPtr anObject = aMessage->selectedObject();
479 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
481 if (anObject.get() && aPoint.get()) {
482 if (myCashedShapes.find(anObject) == myCashedShapes.end())
483 fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
484 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
485 if (aShapes.size() > 1) {
486 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
487 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
488 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
489 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
490 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
491 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
492 aRefSelectedAttr->setValue(anObject);
493 aRefPreviewAttr->setValue(anObject);
495 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
496 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
497 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
498 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
499 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
500 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
501 aPointSelectedAttr->setValue(aPoint);
502 aPointPreviewAttr->setValue(aPoint);
504 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
506 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
507 #ifdef DEBUG_TRIM_METHODS
508 if (!aSelectedShape.get())
509 std::cout << "Set empty selected object" << std::endl;
511 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
513 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
517 return aFilledAttributeName;
520 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
521 const std::set<AttributePoint2DPtr>& theFurtherCoincidences)
523 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
524 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
527 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
528 if (!aRefPointAttr.get())
530 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
532 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
533 aLast = theFurtherCoincidences.end();
534 bool aFoundPoint = false;
535 for (; anIt != aLast && !aFoundPoint; anIt++) {
536 AttributePoint2DPtr aPointAttribute = (*anIt);
537 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
538 if (aPoint2d->isEqual(aRefPnt2d)) {
539 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
541 if (aRefAttr.get()) {
542 aRefAttr->setAttr(aPointAttribute);
550 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
551 const FeaturePtr& theFeature)
553 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
554 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
557 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
562 // get shape of tangent object to the current
563 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
564 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
565 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
566 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
567 aFeature->attribute(aTangentAttr));
568 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
570 // get shape of the feature of the attribute
571 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
572 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
573 PointToRefsMap aPointToAttributeOrObject;
574 std::list<FeaturePtr> aFeatures;
575 aFeatures.push_back(anAttributeFeature);
576 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
577 aPointToAttributeOrObject);
578 if (!aPointToAttributeOrObject.empty())
579 return true; // the attribute feature has a point of intersection, so we do not replace it
581 // get shape of the feature
582 aPointToAttributeOrObject.clear();
584 aFeatures.push_back(theFeature);
585 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
586 aPointToAttributeOrObject);
587 if (!aPointToAttributeOrObject.empty()) {
588 std::set<ResultPtr> anEdgeShapes;
589 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
590 if (!anEdgeShapes.empty()) {
591 ResultPtr aResult = *anEdgeShapes.begin();
593 aRefAttr->setObject(aResult);
594 return true; // the attribute feature has a point of intersection, so we do not replace it
601 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
603 #ifdef DEBUG_TRIM_METHODS
604 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
607 AISObjectPtr anAIS = thePrevious;
609 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
610 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
611 if (aPreviewShape.get())
612 aShapes.push_back(aPreviewShape);
613 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
614 if (aSelectedShape.get())
615 aShapes.push_back(aSelectedShape);
618 return AISObjectPtr();
620 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
621 if (!aBaseShape.get())
622 return AISObjectPtr();
624 if (aBaseShape.get()) {
626 anAIS = AISObjectPtr(new GeomAPI_AISObject);
627 anAIS->createShape(aBaseShape);
629 std::vector<int> aColor;
630 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
631 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
632 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
633 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
634 // width when there is not base object should be extened in several points
635 // in order to see this preview over highlight
636 anAIS->setWidth(aWidth+4);
637 anAIS->setLineStyle(aLineStyle);
640 anAIS = AISObjectPtr();
645 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
646 const std::string& thePointAttributeId)
648 GeomShapePtr aBaseShape;
650 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
651 data()->attribute(theObjectAttributeId));
652 ObjectPtr aBaseObject = anObjectAttr->value();
653 if (!aBaseObject.get())
657 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
658 data()->attribute(thePointAttributeId));
659 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
660 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
661 anAttributePnt2d->y());
663 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
664 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
666 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
667 if (!aShapes.empty()) {
668 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
669 for (; anIt != aLast; anIt++) {
670 GeomShapePtr aShape = *anIt;
671 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
672 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
679 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
680 AttributePoint2DPtr& theStartPointAttr,
681 AttributePoint2DPtr& theEndPointAttr)
683 std::string aFeatureKind = theFeature->getKind();
684 std::string aStartAttributeName, anEndAttributeName;
685 if (aFeatureKind == SketchPlugin_Line::ID()) {
686 aStartAttributeName = SketchPlugin_Line::START_ID();
687 anEndAttributeName = SketchPlugin_Line::END_ID();
689 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
690 aStartAttributeName = SketchPlugin_Arc::START_ID();
691 anEndAttributeName = SketchPlugin_Arc::END_ID();
693 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
694 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
695 theFeature->attribute(aStartAttributeName));
696 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
697 theFeature->attribute(anEndAttributeName));
701 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
702 std::set<FeaturePtr>& theFeaturesToUpdate)
704 std::shared_ptr<ModelAPI_Data> aData = data();
706 // Check the base objects are initialized.
707 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
708 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
709 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
710 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
712 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
713 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
714 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
716 std::set<AttributePtr>::const_iterator aIt;
717 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
718 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
719 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
720 std::string aRefFeatureKind = aRefFeature->getKind();
721 std::string anAttributeId = anAttr->id();
722 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
723 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
724 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
725 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
726 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
727 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
728 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
729 theFeaturesToDelete.insert(aRefFeature);
730 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
731 theFeaturesToUpdate.insert(aRefFeature);
735 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
736 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
737 std::list<AttributePtr>& theRefsToFeature)
741 std::list<AttributePtr> aPointAttributes =
742 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
743 std::set<AttributePtr> aPointAttributesSet;
745 std::list<AttributePtr>::const_iterator aPIt =
746 aPointAttributes.begin(), aPLast = aPointAttributes.end();
747 for (; aPIt != aPLast; aPIt++)
748 aPointAttributesSet.insert(*aPIt);
750 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
751 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
752 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
754 std::set<AttributePtr>::const_iterator aIt;
755 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
756 AttributePtr anAttr = (*aIt);
757 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
758 if (anAttrFeature.get() != this &&
759 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
760 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
761 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
762 AttributePtr anAttrInRef = aRefAttr->attr();
763 if (anAttrInRef.get() &&
764 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
765 if (theRefs.find(anAttrInRef) != theRefs.end())
766 theRefs[anAttrInRef].push_back(aRefAttr);
768 std::list<AttributePtr> anAttrList;
769 anAttrList.push_back(aRefAttr);
770 theRefs[anAttrInRef] = anAttrList;
774 else { /// find attributes referenced to feature itself
775 theRefsToFeature.push_back(anAttr);
781 void SketchPlugin_Trim::updateRefAttConstraints(
782 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
783 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
784 std::set<FeaturePtr>& theFeaturesToDelete)
787 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
790 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
791 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
792 for (; anIt != aLast; anIt++) {
793 AttributePtr anAttribute = anIt->first;
795 /// not found in references
796 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
798 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
799 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
800 aRLast = aRefAttributes.end();
802 AttributePtr aNewAttribute = anIt->second;
803 if (aNewAttribute.get()) {
804 for (; aRefIt != aRLast; aRefIt++) {
805 AttributeRefAttrPtr aRefAttr =
806 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
807 if (aRefAttr.get()) {
808 aRefAttr->setAttr(aNewAttribute);
815 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
816 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
818 /// not found in references
819 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
822 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
823 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
824 aRLast = aRefAttributes.end();
826 std::set<FeaturePtr> aFeaturesToDelete;
827 for (; aRefIt != aRLast; aRefIt++) {
828 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
829 if (aRefAttr.get()) {
830 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
835 // delete constraints
836 if (aFeaturesToDelete.size() > 0) {
837 std::cout << "removeReferencesToAttribute: " << std::endl;
839 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
840 anIt != aFeaturesToDelete.end(); anIt++) {
841 FeaturePtr aFeature = *anIt;
842 std::cout << aFeature->data()->name() << std::endl;
846 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
847 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
850 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
852 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
853 aLast = theFeaturesToUpdate.end();
854 for (; anIt != aLast; anIt++) {
855 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
856 std::string aRefFeatureKind = aRefFeature->getKind();
857 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
858 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
859 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
860 if (aLenghtFeature.get()) {
861 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
862 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
864 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
865 aValueAttr->setValue(aValue);
871 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
872 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
873 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
874 std::set<AttributePoint2DPtr>& thePoints,
875 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
877 FeaturePtr anNewFeature;
879 // Check the base objects are initialized.
880 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
881 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
882 ObjectPtr aBaseObject = aBaseObjectAttr->value();
883 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
886 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
887 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
889 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
890 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
892 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
893 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
894 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
895 aStartShapePoint, aLastShapePoint);
897 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
898 if (aStartShapePoint.get())
899 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
900 aStartShapePoint->y() << "]" << std::endl;
901 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
902 aStartFeaturePoint->y() << "]" << std::endl;
903 if (aLastShapePoint.get())
904 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
905 aLastShapePoint->y() << "]" << std::endl;
906 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
907 aLastFeaturePoint->y() << "]" << std::endl;
910 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
911 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
912 if (isStartPoint || isLastPoint) {
913 // result is one line: changed existing line
914 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
915 : SketchPlugin_Line::END_ID();
916 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
917 if (aStartShapePoint.get() && aLastShapePoint.get())
918 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
920 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
922 // it is important to delete references before the feature modification because
923 // if deletion will be after the feature modification, solver returns the feature back
924 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
925 theBaseRefAttributes);
927 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
928 //theModifiedAttributes.insert(
929 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
931 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
932 (aBaseFeature->attribute(aModifiedAttribute)));
935 // result is two lines: start line point - start shape point,
936 // last shape point - last line point
937 // create second line
938 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
939 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
940 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
942 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
943 theModifiedAttributes.insert(
944 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
945 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
948 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
950 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
951 (aBaseFeature->attribute(aModifiedAttribute)));
953 // Collinear constraint for lines
954 createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(),
955 getFeatureResult(aBaseFeature),
956 getFeatureResult(anNewFeature));
962 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
963 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
964 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
965 std::set<AttributePoint2DPtr>& thePoints,
966 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
968 FeaturePtr anNewFeature;
969 // Check the base objects are initialized.
970 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
971 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
972 ObjectPtr aBaseObject = aBaseObjectAttr->value();
973 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
976 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
977 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
979 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
980 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
982 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
983 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
984 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
985 aStartShapePoint, aLastShapePoint);
987 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
988 if (aStartShapePoint.get())
989 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
990 aStartShapePoint->y() << "]" << std::endl;
991 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
992 aStartArcPoint->y() << "]" << std::endl;
993 if (aLastShapePoint.get())
994 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
995 aLastShapePoint->y() << "]" << std::endl;
996 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
997 aLastArcPoint->y() << "]" << std::endl;
1000 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1001 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1002 if (isStartPoint || isLastPoint) {
1003 // result is one arc: changed existing arc
1004 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1005 : SketchPlugin_Arc::END_ID();
1006 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1007 if (aStartShapePoint.get() && aLastShapePoint.get())
1008 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1010 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1012 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1013 theBaseRefAttributes);
1015 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1017 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1018 (aBaseFeature->attribute(aModifiedAttribute)));
1021 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1022 // create second arc
1023 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1024 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1025 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1027 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1028 theModifiedAttributes.insert(
1029 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1030 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1033 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1035 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1036 (aBaseFeature->attribute(aModifiedAttribute)));
1038 // equal Radius constraint for arcs
1039 createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1040 getFeatureResult(aBaseFeature),
1041 getFeatureResult(anNewFeature));
1042 // coincident centers constraint
1043 createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1044 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1045 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1048 std::cout << "Created arc on points:" << std::endl;
1049 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1050 aStartShapePoint->y() << "]" << std::endl;
1053 return anNewFeature;
1056 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1057 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1058 std::set<AttributePoint2DPtr>& thePoints,
1059 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1061 // Check the base objects are initialized.
1062 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1063 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1064 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1065 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1068 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1069 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1072 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1073 // arc created by trim of circle is always correct, that means that it is not inversed
1074 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1076 theModifiedAttributes.insert(
1077 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1078 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1080 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1081 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1082 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1083 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1085 return anNewFeature;
1088 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1089 const AttributePoint2DPtr& theEndPointAttr,
1090 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1091 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1093 if (!theFirstPoint.get() || !theLastPoint.get())
1096 // if first point is closer to last point, swap first and last values
1097 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1098 theStartPointAttr->pnt()->distance(theLastPoint)) {
1099 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1100 theFirstPoint = theLastPoint;
1101 theLastPoint = aTmpPoint;
1105 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1106 const AttributePoint2DPtr& theStartPointAttr,
1107 const AttributePoint2DPtr& theEndPointAttr,
1108 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1109 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1111 if (!theFirstPoint.get() || !theSecondPoint.get())
1114 static const double anAngleTol = 1.e-12;
1116 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1117 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1118 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1120 // collect directions to each point
1121 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1122 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1123 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1124 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1125 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1126 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1128 // sort points by their angular values
1129 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1130 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1131 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1132 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1133 aFirstPtAngle += aPeriod;
1134 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1135 aSecondPtAngle += aPeriod;
1137 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1138 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1139 theFirstPoint = theSecondPoint;
1140 theSecondPoint = aTmpPoint;
1144 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1145 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1147 std::string anAttributeType = theModifiedAttribute->attributeType();
1148 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1149 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1150 theModifiedAttribute);
1151 aModifiedAttribute->setValue(thePoint);
1154 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1155 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1156 theModifiedAttribute->id() <<
1157 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1163 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1164 const AttributePtr& theSourceAttribute)
1166 std::string anAttributeType = theModifiedAttribute->attributeType();
1167 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1168 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1169 theModifiedAttribute);
1170 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1171 theSourceAttribute);
1173 if (aModifiedAttribute.get() && aSourceAttribute.get())
1174 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1176 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1177 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1178 theModifiedAttribute);
1179 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1180 theSourceAttribute);
1182 if (aModifiedAttribute.get() && aSourceAttribute.get())
1183 aModifiedAttribute->setValue(aSourceAttribute->value());
1185 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1186 AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1187 theModifiedAttribute);
1188 AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1189 theSourceAttribute);
1190 if (!aSourceRefAttr.get())
1191 aRefAttributeToFill->setAttr(theSourceAttribute);
1193 if (aSourceRefAttr->isObject())
1194 aRefAttributeToFill->setObject(aSourceRefAttr->object());
1196 aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1201 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1202 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1203 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1206 std::cout << "---- createLineFeature ---" << std::endl;
1209 FeaturePtr aFeature;
1210 SketchPlugin_Sketch* aSketch = sketch();
1211 if (!aSketch || !theBaseFeature.get())
1214 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1216 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1217 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1219 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1220 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1222 aFeature->execute(); // to obtain result
1225 std::cout << "---- createLineFeature:end ---" << std::endl;
1231 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1232 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1233 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1235 FeaturePtr aFeature;
1236 SketchPlugin_Sketch* aSketch = sketch();
1237 if (!aSketch || !theBaseFeature.get())
1240 std::string aCenterAttributeId;
1241 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1242 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1243 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1244 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1246 if (aCenterAttributeId.empty())
1250 std::cout << "---- createArcFeature ---" << std::endl;
1253 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1254 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1255 // the "attribute updated"
1256 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1257 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1259 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1260 theBaseFeature->attribute(aCenterAttributeId));
1261 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1262 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1264 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1265 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1267 /// fill referersed state of created arc as it is on the base arc
1268 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1269 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1270 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1272 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1273 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1276 std::cout << "---- createArcFeature:end ---" << std::endl;
1282 FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId,
1283 const AttributePtr& theFirstAttribute,
1284 const AttributePtr& theSecondAttribute)
1286 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1287 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1288 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1289 aRefAttr->setAttr(theFirstAttribute);
1291 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1292 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1293 aRefAttr->setAttr(theSecondAttribute);
1296 std::cout << "<createConstraint to attribute> :"
1297 << "first attribute - " << theFirstAttribute->id()
1298 << "second attribute - " << theSecondAttribute->id()
1305 FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId,
1306 const AttributePtr& theFirstAttribute,
1307 const ObjectPtr& theSecondObject)
1309 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1310 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1311 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1312 aRefAttr->setAttr(theFirstAttribute);
1314 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1315 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1316 aRefAttr->setObject(theSecondObject);
1319 std::cout << "<createConstraint to attribute> :"
1320 << "first attribute - " << theFirstAttribute->id()
1321 << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
1328 FeaturePtr SketchPlugin_Trim::createConstraintForObjects(
1329 const std::string& theConstraintId,
1330 const ObjectPtr& theFirstObject,
1331 const ObjectPtr& theSecondObject)
1333 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1334 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1335 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1336 aRefAttr->setObject(theFirstObject);
1338 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1339 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1340 aRefAttr->setObject(theSecondObject);
1345 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1346 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1348 std::shared_ptr<ModelAPI_Result> aResult;
1350 std::string aFeatureKind = theFeature->getKind();
1351 if (aFeatureKind == SketchPlugin_Line::ID())
1352 aResult = theFeature->firstResult();
1353 else if (aFeatureKind == SketchPlugin_Arc::ID())
1354 aResult = theFeature->lastResult();
1355 else if (aFeatureKind == SketchPlugin_Circle::ID())
1356 aResult = theFeature->lastResult();
1361 //********************************************************************
1362 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1363 const ObjectPtr& theSketch,
1364 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1365 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1367 PointToRefsMap aPointsInfo;
1369 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1370 std::map<std::shared_ptr<GeomAPI_Pnt>,
1371 std::list< AttributePoint2DPtr > > aPointToAttributes;
1372 std::map<std::shared_ptr<GeomAPI_Pnt>,
1373 std::list< ObjectPtr > > aPointToObjects;
1375 std::set<AttributePoint2DPtr > aRefAttributes;
1377 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1378 std::set<ResultPtr> anEdgeShapes;
1380 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1381 if (!anEdgeShapes.empty()) {
1382 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1384 // coincidences to the feature
1385 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1386 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1387 // layed on feature coincidences to divide it on several shapes
1388 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1389 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1390 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1391 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1392 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1393 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1394 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1395 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1397 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1398 aX->dir(), aY, aPointsInfo);
1400 std::list<FeaturePtr> aFeatures;
1401 CompositeFeaturePtr aSketchComposite =
1402 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1403 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1404 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1406 aFeatures.push_back(aFeature);
1408 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1410 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1412 theObjectToPoints[theObject] = aPointsInfo;
1413 theCashedShapes[theObject] = aShapes;