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 std::shared_ptr<GeomAPI_Pnt2d> aPoint2D = sketch()->to2D(aPoint);
349 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
350 anObjectIt != anObjects.end(); anObjectIt++) {
351 ObjectPtr anObject = *anObjectIt;
352 // find an attribute on the point feature and if it is, append it into attribute list
353 // the case when the feature just touch the source feature, not intersect
354 FeaturePtr aRefFeature = ModelAPI_Feature::feature(anObject);
355 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
356 aRefFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
357 bool aFoundAttribute = false;
358 for (std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator
359 aPntAttrIt = anAttributes.begin(); aPntAttrIt != anAttributes.end(); aPntAttrIt++) {
360 std::shared_ptr<GeomDataAPI_Point2D> anAttrPoint =
361 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPntAttrIt);
362 if (anAttrPoint->pnt()->isEqual(aPoint2D)) {
363 createConstraint(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute, anAttrPoint);
364 aFoundAttribute = true;
368 if (!aFoundAttribute)
369 createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute,
374 // move constraints from base feature to replacing feature: ignore coincidences to feature
375 // if attributes of coincidence participated in split
376 ResultPtr aReplacingResult;
377 if (aReplacingFeature.get()) {
378 aReplacingFeature->execute(); // need it to obtain result
379 aReplacingResult = getFeatureResult(aReplacingFeature);
381 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
382 aLast = aRefsToFeature.end();
383 anIt != aLast; anIt++) {
384 AttributePtr anAttribute = *anIt;
386 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences))
389 // move tangency constraint to the nearest feature if possible
390 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
393 if (aReplacingResult.get()) {
394 AttributeRefAttrPtr aRefAttr =
395 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
397 aRefAttr->setObject(aReplacingResult);
399 AttributeReferencePtr aReferenceAttr =
400 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
401 if (aReferenceAttr.get())
402 aReferenceAttr->setObject(aReplacingResult);
407 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
409 // Wait all constraints being created, then send update events
410 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
411 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
413 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
415 // delete constraints
417 if (aFeaturesToDelete.size() > 0) {
418 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
420 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
421 anIt != aFeaturesToDelete.end(); anIt++) {
422 FeaturePtr aFeature = *anIt;
423 std::cout << aFeature->data()->name() << std::endl;
427 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
428 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
430 updateFeaturesAfterTrim(aFeaturesToUpdate);
432 // Send events to update the sub-features by the solver.
433 if(isUpdateFlushed) {
434 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
437 if (anIsEqualPreviewAndSelected) {
438 // equal preview and selected objects
439 // nothing to do if the preview and selected objects are different
440 if (aReplacingResult.get()) { // base object was removed
441 aPreviewObject = aReplacingResult;
442 //aMessage->setSelectedObject(aReplacingResult);
443 #ifdef DEBUG_TRIM_METHODS
444 if (!aSelectedShape.get())
445 std::cout << "Set empty selected object" << std::endl;
447 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
451 aPreviewObject = ObjectPtr();
453 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
454 aBaseObject = getFeatureResult(aBaseFeature);
455 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
457 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
459 GeomShapePtr aShape = aBaseResult->shape();
460 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
461 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
462 aPreviewObject = aBaseResult;
464 if (!aPreviewObject.get() && aNewFeature.get()) {
465 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
466 if (aNewFeatureResult.get()) {
467 GeomShapePtr aShape = aNewFeatureResult->shape();
468 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
469 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
470 aPreviewObject = aNewFeatureResult;
475 if (aPreviewObject.get()) {
476 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
477 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
478 ModelAPI_EventReentrantMessage::eventId(), this));
479 aMessage->setSelectedObject(aPreviewObject);
480 Events_Loop::loop()->send(aMessage);
483 std::cout << "SketchPlugin_Trim::done" << std::endl;
487 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
489 #ifdef DEBUG_TRIM_METHODS
490 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
492 std::string aFilledAttributeName;
494 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
495 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
496 if (aMessage.get()) {
497 ObjectPtr anObject = aMessage->selectedObject();
498 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
500 if (anObject.get() && aPoint.get()) {
501 if (myCashedShapes.find(anObject) == myCashedShapes.end())
502 fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
503 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
504 if (aShapes.size() > 1) {
505 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
506 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
507 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
508 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
509 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
510 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
511 aRefSelectedAttr->setValue(anObject);
512 aRefPreviewAttr->setValue(anObject);
514 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
515 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
516 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
517 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
518 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
519 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
520 aPointSelectedAttr->setValue(aPoint);
521 aPointPreviewAttr->setValue(aPoint);
523 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
525 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
526 #ifdef DEBUG_TRIM_METHODS
527 if (!aSelectedShape.get())
528 std::cout << "Set empty selected object" << std::endl;
530 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
532 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
536 return aFilledAttributeName;
539 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
540 const std::set<AttributePoint2DPtr>& theFurtherCoincidences)
542 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
543 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
546 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
547 if (!aRefPointAttr.get())
549 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
551 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
552 aLast = theFurtherCoincidences.end();
553 bool aFoundPoint = false;
554 for (; anIt != aLast && !aFoundPoint; anIt++) {
555 AttributePoint2DPtr aPointAttribute = (*anIt);
556 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
557 if (aPoint2d->isEqual(aRefPnt2d)) {
558 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
560 if (aRefAttr.get()) {
561 aRefAttr->setAttr(aPointAttribute);
569 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
570 const FeaturePtr& theFeature)
572 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
573 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
576 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
581 // get shape of tangent object to the current
582 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
583 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
584 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
585 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
586 aFeature->attribute(aTangentAttr));
587 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
589 // get shape of the feature of the attribute
590 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
591 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
592 PointToRefsMap aPointToAttributeOrObject;
593 std::list<FeaturePtr> aFeatures;
594 aFeatures.push_back(anAttributeFeature);
595 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
596 aPointToAttributeOrObject);
597 if (!aPointToAttributeOrObject.empty())
598 return true; // the attribute feature has a point of intersection, so we do not replace it
600 // get shape of the feature
601 aPointToAttributeOrObject.clear();
603 aFeatures.push_back(theFeature);
604 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
605 aPointToAttributeOrObject);
606 if (!aPointToAttributeOrObject.empty()) {
607 std::set<ResultPtr> anEdgeShapes;
608 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
609 if (!anEdgeShapes.empty()) {
610 ResultPtr aResult = *anEdgeShapes.begin();
612 aRefAttr->setObject(aResult);
613 return true; // the attribute feature has a point of intersection, so we do not replace it
620 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
622 #ifdef DEBUG_TRIM_METHODS
623 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
626 AISObjectPtr anAIS = thePrevious;
628 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
629 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
630 if (aPreviewShape.get())
631 aShapes.push_back(aPreviewShape);
632 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
633 if (aSelectedShape.get())
634 aShapes.push_back(aSelectedShape);
637 return AISObjectPtr();
639 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
640 if (!aBaseShape.get())
641 return AISObjectPtr();
643 if (aBaseShape.get()) {
645 anAIS = AISObjectPtr(new GeomAPI_AISObject);
646 anAIS->createShape(aBaseShape);
648 std::vector<int> aColor;
649 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
650 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
651 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
652 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
653 // width when there is not base object should be extened in several points
654 // in order to see this preview over highlight
655 anAIS->setWidth(aWidth+4);
656 anAIS->setLineStyle(aLineStyle);
659 anAIS = AISObjectPtr();
664 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
665 const std::string& thePointAttributeId)
667 GeomShapePtr aBaseShape;
669 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
670 data()->attribute(theObjectAttributeId));
671 ObjectPtr aBaseObject = anObjectAttr->value();
672 if (!aBaseObject.get())
676 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
677 data()->attribute(thePointAttributeId));
678 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
679 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
680 anAttributePnt2d->y());
682 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
683 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
685 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
686 if (!aShapes.empty()) {
687 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
688 for (; anIt != aLast; anIt++) {
689 GeomShapePtr aShape = *anIt;
690 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
691 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
698 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
699 AttributePoint2DPtr& theStartPointAttr,
700 AttributePoint2DPtr& theEndPointAttr)
702 std::string aFeatureKind = theFeature->getKind();
703 std::string aStartAttributeName, anEndAttributeName;
704 if (aFeatureKind == SketchPlugin_Line::ID()) {
705 aStartAttributeName = SketchPlugin_Line::START_ID();
706 anEndAttributeName = SketchPlugin_Line::END_ID();
708 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
709 aStartAttributeName = SketchPlugin_Arc::START_ID();
710 anEndAttributeName = SketchPlugin_Arc::END_ID();
712 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
713 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
714 theFeature->attribute(aStartAttributeName));
715 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
716 theFeature->attribute(anEndAttributeName));
720 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
721 std::set<FeaturePtr>& theFeaturesToUpdate)
723 std::shared_ptr<ModelAPI_Data> aData = data();
725 // Check the base objects are initialized.
726 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
727 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
728 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
729 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
731 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
732 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
733 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
735 std::set<AttributePtr>::const_iterator aIt;
736 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
737 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
738 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
739 std::string aRefFeatureKind = aRefFeature->getKind();
740 std::string anAttributeId = anAttr->id();
741 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
742 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
743 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
744 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
745 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
746 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
747 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
748 theFeaturesToDelete.insert(aRefFeature);
749 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
750 theFeaturesToUpdate.insert(aRefFeature);
754 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
755 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
756 std::list<AttributePtr>& theRefsToFeature)
760 std::list<AttributePtr> aPointAttributes =
761 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
762 std::set<AttributePtr> aPointAttributesSet;
764 std::list<AttributePtr>::const_iterator aPIt =
765 aPointAttributes.begin(), aPLast = aPointAttributes.end();
766 for (; aPIt != aPLast; aPIt++)
767 aPointAttributesSet.insert(*aPIt);
769 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
770 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
771 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
773 std::set<AttributePtr>::const_iterator aIt;
774 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
775 AttributePtr anAttr = (*aIt);
776 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
777 if (anAttrFeature.get() != this &&
778 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
779 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
780 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
781 AttributePtr anAttrInRef = aRefAttr->attr();
782 if (anAttrInRef.get() &&
783 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
784 if (theRefs.find(anAttrInRef) != theRefs.end())
785 theRefs[anAttrInRef].push_back(aRefAttr);
787 std::list<AttributePtr> anAttrList;
788 anAttrList.push_back(aRefAttr);
789 theRefs[anAttrInRef] = anAttrList;
793 else { /// find attributes referenced to feature itself
794 theRefsToFeature.push_back(anAttr);
800 void SketchPlugin_Trim::updateRefAttConstraints(
801 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
802 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
803 std::set<FeaturePtr>& theFeaturesToDelete)
806 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
809 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
810 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
811 for (; anIt != aLast; anIt++) {
812 AttributePtr anAttribute = anIt->first;
814 /// not found in references
815 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
817 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
818 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
819 aRLast = aRefAttributes.end();
821 AttributePtr aNewAttribute = anIt->second;
822 if (aNewAttribute.get()) {
823 for (; aRefIt != aRLast; aRefIt++) {
824 AttributeRefAttrPtr aRefAttr =
825 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
826 if (aRefAttr.get()) {
827 aRefAttr->setAttr(aNewAttribute);
834 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
835 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
837 /// not found in references
838 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
841 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
842 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
843 aRLast = aRefAttributes.end();
845 std::set<FeaturePtr> aFeaturesToDelete;
846 for (; aRefIt != aRLast; aRefIt++) {
847 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
848 if (aRefAttr.get()) {
849 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
854 // delete constraints
855 if (aFeaturesToDelete.size() > 0) {
856 std::cout << "removeReferencesToAttribute: " << std::endl;
858 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
859 anIt != aFeaturesToDelete.end(); anIt++) {
860 FeaturePtr aFeature = *anIt;
861 std::cout << aFeature->data()->name() << std::endl;
865 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
866 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
869 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
871 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
872 aLast = theFeaturesToUpdate.end();
873 for (; anIt != aLast; anIt++) {
874 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
875 std::string aRefFeatureKind = aRefFeature->getKind();
876 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
877 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
878 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
879 if (aLenghtFeature.get()) {
880 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
881 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
883 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
884 aValueAttr->setValue(aValue);
890 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
891 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
892 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
893 std::set<AttributePoint2DPtr>& thePoints,
894 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
896 FeaturePtr anNewFeature;
898 // Check the base objects are initialized.
899 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
900 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
901 ObjectPtr aBaseObject = aBaseObjectAttr->value();
902 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
905 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
906 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
908 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
909 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
911 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
912 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
913 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
914 aStartShapePoint, aLastShapePoint);
916 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
917 if (aStartShapePoint.get())
918 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
919 aStartShapePoint->y() << "]" << std::endl;
920 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
921 aStartFeaturePoint->y() << "]" << std::endl;
922 if (aLastShapePoint.get())
923 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
924 aLastShapePoint->y() << "]" << std::endl;
925 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
926 aLastFeaturePoint->y() << "]" << std::endl;
929 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
930 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
931 if (isStartPoint || isLastPoint) {
932 // result is one line: changed existing line
933 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
934 : SketchPlugin_Line::END_ID();
935 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
936 if (aStartShapePoint.get() && aLastShapePoint.get())
937 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
939 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
941 // it is important to delete references before the feature modification because
942 // if deletion will be after the feature modification, solver returns the feature back
943 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
944 theBaseRefAttributes);
946 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
947 //theModifiedAttributes.insert(
948 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
950 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
951 (aBaseFeature->attribute(aModifiedAttribute)));
954 // result is two lines: start line point - start shape point,
955 // last shape point - last line point
956 // create second line
957 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
958 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
959 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
961 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
962 theModifiedAttributes.insert(
963 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
964 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
967 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
969 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
970 (aBaseFeature->attribute(aModifiedAttribute)));
972 // Collinear constraint for lines
973 createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(),
974 getFeatureResult(aBaseFeature),
975 getFeatureResult(anNewFeature));
981 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
982 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
983 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
984 std::set<AttributePoint2DPtr>& thePoints,
985 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
987 FeaturePtr anNewFeature;
988 // Check the base objects are initialized.
989 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
990 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
991 ObjectPtr aBaseObject = aBaseObjectAttr->value();
992 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
995 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
996 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
998 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
999 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
1001 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
1002 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
1003 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1004 aStartShapePoint, aLastShapePoint);
1006 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1007 if (aStartShapePoint.get())
1008 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1009 aStartShapePoint->y() << "]" << std::endl;
1010 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1011 aStartArcPoint->y() << "]" << std::endl;
1012 if (aLastShapePoint.get())
1013 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1014 aLastShapePoint->y() << "]" << std::endl;
1015 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1016 aLastArcPoint->y() << "]" << std::endl;
1019 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1020 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1021 if (isStartPoint || isLastPoint) {
1022 // result is one arc: changed existing arc
1023 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1024 : SketchPlugin_Arc::END_ID();
1025 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1026 if (aStartShapePoint.get() && aLastShapePoint.get())
1027 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1029 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1031 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1032 theBaseRefAttributes);
1034 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1036 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1037 (aBaseFeature->attribute(aModifiedAttribute)));
1040 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1041 // create second arc
1042 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1043 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1044 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1046 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1047 theModifiedAttributes.insert(
1048 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1049 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1052 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1054 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1055 (aBaseFeature->attribute(aModifiedAttribute)));
1057 // equal Radius constraint for arcs
1058 createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1059 getFeatureResult(aBaseFeature),
1060 getFeatureResult(anNewFeature));
1061 // coincident centers constraint
1062 createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1063 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1064 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1067 std::cout << "Created arc on points:" << std::endl;
1068 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1069 aStartShapePoint->y() << "]" << std::endl;
1072 return anNewFeature;
1075 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1076 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1077 std::set<AttributePoint2DPtr>& thePoints,
1078 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1080 // Check the base objects are initialized.
1081 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1082 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1083 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1084 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1087 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1088 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1091 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1092 // arc created by trim of circle is always correct, that means that it is not inversed
1093 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1095 theModifiedAttributes.insert(
1096 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1097 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1099 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1100 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1101 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1102 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1104 return anNewFeature;
1107 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1108 const AttributePoint2DPtr& theEndPointAttr,
1109 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1110 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1112 if (!theFirstPoint.get() || !theLastPoint.get())
1115 // if first point is closer to last point, swap first and last values
1116 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1117 theStartPointAttr->pnt()->distance(theLastPoint)) {
1118 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1119 theFirstPoint = theLastPoint;
1120 theLastPoint = aTmpPoint;
1124 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1125 const AttributePoint2DPtr& theStartPointAttr,
1126 const AttributePoint2DPtr& theEndPointAttr,
1127 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1128 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1130 if (!theFirstPoint.get() || !theSecondPoint.get())
1133 static const double anAngleTol = 1.e-12;
1135 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1136 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1137 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1139 // collect directions to each point
1140 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1141 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1142 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1143 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1144 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1145 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1147 // sort points by their angular values
1148 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1149 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1150 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1151 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1152 aFirstPtAngle += aPeriod;
1153 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1154 aSecondPtAngle += aPeriod;
1156 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1157 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1158 theFirstPoint = theSecondPoint;
1159 theSecondPoint = aTmpPoint;
1163 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1164 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1166 std::string anAttributeType = theModifiedAttribute->attributeType();
1167 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1168 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1169 theModifiedAttribute);
1170 aModifiedAttribute->setValue(thePoint);
1173 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1174 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1175 theModifiedAttribute->id() <<
1176 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1182 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1183 const AttributePtr& theSourceAttribute)
1185 std::string anAttributeType = theModifiedAttribute->attributeType();
1186 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1187 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1188 theModifiedAttribute);
1189 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1190 theSourceAttribute);
1192 if (aModifiedAttribute.get() && aSourceAttribute.get())
1193 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1195 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1196 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1197 theModifiedAttribute);
1198 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1199 theSourceAttribute);
1201 if (aModifiedAttribute.get() && aSourceAttribute.get())
1202 aModifiedAttribute->setValue(aSourceAttribute->value());
1204 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1205 AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1206 theModifiedAttribute);
1207 AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1208 theSourceAttribute);
1209 if (!aSourceRefAttr.get())
1210 aRefAttributeToFill->setAttr(theSourceAttribute);
1212 if (aSourceRefAttr->isObject())
1213 aRefAttributeToFill->setObject(aSourceRefAttr->object());
1215 aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1220 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1221 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1222 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1225 std::cout << "---- createLineFeature ---" << std::endl;
1228 FeaturePtr aFeature;
1229 SketchPlugin_Sketch* aSketch = sketch();
1230 if (!aSketch || !theBaseFeature.get())
1233 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1235 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1236 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1238 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1239 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1241 aFeature->execute(); // to obtain result
1244 std::cout << "---- createLineFeature:end ---" << std::endl;
1250 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1251 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1252 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1254 FeaturePtr aFeature;
1255 SketchPlugin_Sketch* aSketch = sketch();
1256 if (!aSketch || !theBaseFeature.get())
1259 std::string aCenterAttributeId;
1260 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1261 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1262 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1263 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1265 if (aCenterAttributeId.empty())
1269 std::cout << "---- createArcFeature ---" << std::endl;
1272 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1273 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1274 // the "attribute updated"
1275 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1276 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1278 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1279 theBaseFeature->attribute(aCenterAttributeId));
1280 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1281 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1283 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1284 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1286 /// fill referersed state of created arc as it is on the base arc
1287 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1288 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1289 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1291 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1292 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1295 std::cout << "---- createArcFeature:end ---" << std::endl;
1301 FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId,
1302 const AttributePtr& theFirstAttribute,
1303 const AttributePtr& theSecondAttribute)
1305 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1306 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1307 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1308 aRefAttr->setAttr(theFirstAttribute);
1310 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1311 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1312 aRefAttr->setAttr(theSecondAttribute);
1315 std::cout << "<createConstraint to attribute> :"
1316 << "first attribute - " << theFirstAttribute->id()
1317 << "second attribute - " << theSecondAttribute->id()
1324 FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId,
1325 const AttributePtr& theFirstAttribute,
1326 const ObjectPtr& theSecondObject)
1328 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1329 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1330 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1331 aRefAttr->setAttr(theFirstAttribute);
1333 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1334 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1335 aRefAttr->setObject(theSecondObject);
1338 std::cout << "<createConstraint to attribute> :"
1339 << "first attribute - " << theFirstAttribute->id()
1340 << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
1347 FeaturePtr SketchPlugin_Trim::createConstraintForObjects(
1348 const std::string& theConstraintId,
1349 const ObjectPtr& theFirstObject,
1350 const ObjectPtr& theSecondObject)
1352 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1353 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1354 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1355 aRefAttr->setObject(theFirstObject);
1357 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1358 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1359 aRefAttr->setObject(theSecondObject);
1364 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1365 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1367 std::shared_ptr<ModelAPI_Result> aResult;
1369 std::string aFeatureKind = theFeature->getKind();
1370 if (aFeatureKind == SketchPlugin_Line::ID())
1371 aResult = theFeature->firstResult();
1372 else if (aFeatureKind == SketchPlugin_Arc::ID())
1373 aResult = theFeature->lastResult();
1374 else if (aFeatureKind == SketchPlugin_Circle::ID())
1375 aResult = theFeature->lastResult();
1380 //********************************************************************
1381 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1382 const ObjectPtr& theSketch,
1383 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1384 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1386 PointToRefsMap aPointsInfo;
1388 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1389 std::map<std::shared_ptr<GeomAPI_Pnt>,
1390 std::list< AttributePoint2DPtr > > aPointToAttributes;
1391 std::map<std::shared_ptr<GeomAPI_Pnt>,
1392 std::list< ObjectPtr > > aPointToObjects;
1394 std::set<AttributePoint2DPtr > aRefAttributes;
1396 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1397 std::set<ResultPtr> anEdgeShapes;
1399 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1400 if (!anEdgeShapes.empty()) {
1401 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1403 // coincidences to the feature
1404 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1405 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1406 // layed on feature coincidences to divide it on several shapes
1407 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1408 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1409 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1410 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1411 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1412 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1413 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1414 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1416 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1417 aX->dir(), aY, aPointsInfo);
1419 std::list<FeaturePtr> aFeatures;
1420 CompositeFeaturePtr aSketchComposite =
1421 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1422 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1423 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1425 aFeatures.push_back(aFeature);
1427 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1429 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1431 theObjectToPoints[theObject] = aPointsInfo;
1432 theCashedShapes[theObject] = aShapes;