1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_Trim.h"
22 #include <GeomAPI_Dir2d.h>
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Pnt2d.h>
25 #include <GeomAPI_XY.h>
26 #include <GeomDataAPI_Point2D.h>
27 #include <GeomAlgoAPI_ShapeTools.h>
29 #include <ModelAPI_AttributeReference.h>
30 #include <ModelAPI_AttributeString.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_AttributeBoolean.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_AttributeDouble.h>
39 #include <ModelGeomAlgo_Shape.h>
41 #include <SketchPlugin_Arc.h>
42 #include <SketchPlugin_ConstraintMiddle.h>
43 #include <SketchPlugin_Circle.h>
44 #include <SketchPlugin_ConstraintCoincidence.h>
45 #include <SketchPlugin_ConstraintEqual.h>
46 #include <SketchPlugin_ConstraintTangent.h>
47 #include <SketchPlugin_ConstraintLength.h>
48 #include <SketchPlugin_ConstraintMirror.h>
49 #include <SketchPlugin_ConstraintCollinear.h>
50 #include <SketchPlugin_Ellipse.h>
51 #include <SketchPlugin_EllipticArc.h>
52 #include <SketchPlugin_Line.h>
53 #include <SketchPlugin_MultiRotation.h>
54 #include <SketchPlugin_MultiTranslation.h>
55 #include <SketchPlugin_Point.h>
57 #include <ModelAPI_EventReentrantMessage.h>
59 #include <ModelAPI_Events.h>
60 #include <SketchPlugin_Line.h>
61 #include <SketchPlugin_Arc.h>
62 #include <SketchPlugin_Circle.h>
64 #include <ModelGeomAlgo_Point2D.h>
65 #include <Events_Loop.h>
73 #ifdef DEBUG_TRIM_METHODS
77 static const double PI = 3.141592653589793238463;
79 static const std::string OPERATION_HIGHLIGHT_COLOR() { return "128, 0, 0"; }
80 static const std::string OPERATION_REMOVE_FEATURE_COLOR() { return "255, 174, 201"; }
82 SketchPlugin_Trim::SketchPlugin_Trim()
86 void SketchPlugin_Trim::initAttributes()
88 data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
89 data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
91 data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
92 data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
94 data()->attribute(PREVIEW_POINT())->setIsArgument(false);
95 data()->attribute(SELECTED_POINT())->setIsArgument(false);
96 data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
98 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
99 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
102 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
103 const std::string& thePointAttributeId,
104 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
105 std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
107 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
108 data()->attribute(theObjectAttributeId));
109 ObjectPtr aBaseObject = aBaseObjectAttr->value();
111 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
112 data()->attribute(thePointAttributeId));
113 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
114 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
115 anAttributePnt2d->y());
117 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end()) {
118 SketchPlugin_SegmentationTools::fillObjectShapes(
119 this, aBaseObject, myCashedShapes, myObjectToPoints);
122 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
123 if (!aShapes.empty()) {
124 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
125 for (; anIt != aLast; anIt++) {
126 GeomShapePtr aBaseShape = *anIt;
127 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
128 if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, anAttributePnt, aProjectedPoint)) {
130 if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
131 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
132 //GeomAPI_Shape::Orientation anOrientation = anEdge->orientation();
133 //if (anOrientation == GeomAPI_Shape::REVERSED) {
134 aStartPoint = anEdge->lastPoint();
135 aLastPoint = anEdge->firstPoint();
138 //aStartPoint = anEdge->firstPoint();
139 //aLastPoint = anEdge->lastPoint();
146 std::cout << "<findShapePoints> => "
147 << std::endl << "Attribute point: "
148 << anAttributePnt->x() << ", " << anAttributePnt->y() << ", " << anAttributePnt->z() << "]"
149 << std::endl << "Start Point: ["
150 << aStartPoint->x() << ", " << aStartPoint->y() << ", " << aStartPoint->z() << "]"
151 << std::endl << "Last Point: ["
152 << aLastPoint->x() << ", " << aLastPoint->y() << ", " << aLastPoint->z() << "]"
157 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Trim::convertPoint(
158 const std::shared_ptr<GeomAPI_Pnt>& thePoint)
160 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
164 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
165 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
166 ObjectPtr aBaseObject = aBaseObjectAttr->value();
167 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end()) {
168 SketchPlugin_SegmentationTools::fillObjectShapes(
169 this, aBaseObject, myCashedShapes, myObjectToPoints);
173 const GeomAlgoAPI_ShapeTools::PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
174 for (GeomAlgoAPI_ShapeTools::PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
175 aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
176 if (aPointIt->first->isEqual(thePoint)) {
177 const std::pair<std::list<AttributePoint2DPtr >,
178 std::list<ObjectPtr > >& anInfo = aPointIt->second;
179 const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
180 if (!anAttributes.empty()) {
181 aPoint = anAttributes.front()->pnt();
185 aPoint = sketch()->to2D(thePoint);
191 // returns an end of the shape to define direction of split if feature's attribute
193 aPoint = sketch()->to2D(thePoint);
198 void SketchPlugin_Trim::execute()
200 #ifdef DEBUG_TRIM_METHODS
201 std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
204 SketchPlugin_Sketch* aSketch = sketch();
206 setError("Error: Sketch object is empty.");
210 // Check the base objects are initialized.
211 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
212 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
213 if(!aBaseObjectAttr->isInitialized()) {
214 setError("Error: Base object is not initialized.");
217 ObjectPtr aBaseObject = aBaseObjectAttr->value();
218 if (!aBaseObject.get()) {
219 setError("Error: Base object is not initialized.");
222 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
224 /// Remove reference of this feature to feature used in preview, it is not necessary anymore
225 /// as trim will be removed after execute
226 AttributeReferencePtr aPreviewObjectAttr =
227 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
228 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
230 ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
231 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
232 data()->attribute(PREVIEW_POINT()));
233 std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
234 // nullify pointer of preview attribute
235 aPreviewObjectAttr->setValue(ResultPtr());
237 bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
240 std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
242 std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
244 findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
245 if (!aStartShapePoint || !aLastShapePoint) {
246 setError("Error: Selected point is not placed on any edge");
250 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
251 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
252 /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
253 std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
254 getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
255 // find references(attributes and features) to the base feature
256 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
257 std::list<AttributePtr> aRefsToFeature;
258 SketchPlugin_SegmentationTools::getRefAttributes(
259 aBaseFeature, aBaseRefAttributes, aRefsToFeature);
261 std::cout << "---- getRefAttributes ----" << std::endl;
262 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
263 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
264 std::cout << std::endl << "References to attributes of base feature [" <<
265 aBaseRefAttributes.size() << "]" << std::endl;
266 for (; aRefIt != aRefLast; aRefIt++) {
267 AttributePtr aBaseAttr = aRefIt->first;
268 std::list<AttributePtr> aRefAttributes = aRefIt->second;
269 std::string aRefsInfo;
270 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
271 aRefAttrLast = aRefAttributes.end();
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::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
281 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
282 std::cout << aPointAttr->id().c_str() <<
283 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
285 std::cout << std::endl;
286 std::cout << std::endl << "References to base feature [" <<
287 aRefsToFeature.size() << "]" << std::endl;
288 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
289 aRefAttrLast = aRefsToFeature.end();
290 std::string aRefsInfo;
291 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
292 if (!aRefsInfo.empty())
293 aRefsInfo.append(",");
294 AttributePtr aRAttr = *aRefAttrIt;
295 aRefsInfo.append(aRAttr->id());
296 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
297 aRefsInfo.append("(" + aRFeature->name() + ") ");
299 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
300 std::cout << "---- getRefAttributes:end ----" << std::endl;
303 keepCurrentFeature();
305 std::set<AttributePoint2DPtr> aFurtherCoincidences;
306 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
307 const std::string& aKind = aBaseFeature->getKind();
308 FeaturePtr aReplacingFeature, aNewFeature;
309 if (aKind == SketchPlugin_Circle::ID() ||
310 aKind == SketchPlugin_Ellipse::ID()) {
311 aReplacingFeature = trimClosed(aStartShapePoint2d, aLastShapePoint2d,
312 aFurtherCoincidences, aModifiedAttributes);
314 aFeaturesToDelete.insert(aBaseFeature);
315 // as circle is removed, erase it from dependencies(arguments) of this feature
316 // otherwise Trim feature will be removed with the circle before
317 // this operation is finished
318 aBaseObjectAttr->setObject(ResultPtr());
320 else if (aKind == SketchPlugin_Line::ID()) {
321 aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
322 aFurtherCoincidences, aModifiedAttributes);
324 else if (aKind == SketchPlugin_Arc::ID()) {
325 aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
326 aFurtherCoincidences, aModifiedAttributes);
328 else if (aKind == SketchPlugin_EllipticArc::ID()) {
329 aNewFeature = trimEllipticArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
330 aFurtherCoincidences, aModifiedAttributes);
333 restoreCurrentFeature();
335 // constraints to end points of trim feature
336 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end()) {
337 SketchPlugin_SegmentationTools::fillObjectShapes(
338 this, aBaseObject, myCashedShapes, myObjectToPoints);
341 // create coincidence to objects, intersected the base object
342 const GeomAlgoAPI_ShapeTools::PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
343 for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
344 aLast = aFurtherCoincidences.end();
345 anIt != aLast; anIt++) {
346 AttributePoint2DPtr aPointAttribute = (*anIt);
347 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
350 std::cout << "<compare Points> => " << std::endl
351 << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
352 if (aStartShapePoint2d.get())
353 std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
355 if (aLastShapePoint2d.get())
356 std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
360 std::shared_ptr<GeomAPI_Pnt> aPoint;
361 if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
362 aPoint = aStartShapePoint;
363 else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
364 aPoint = aLastShapePoint;
369 std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
370 for (GeomAlgoAPI_ShapeTools::PointToRefsMap::const_iterator aRefIt = aRefsMap.begin();
371 aRefIt != aRefsMap.end(); aRefIt++)
373 if (aRefIt->first->isEqual(aPoint)) {
374 anInfo = aRefIt->second;
375 // prefer a segment instead of a point, because further coincidence with a segment
376 // decreases only 1 DoF (instead of 2 for point) and prevents an overconstraint situation.
378 for (std::list<ObjectPtr>::const_iterator anIt = anInfo.second.begin();
379 anIt != anInfo.second.end() && !isEdge; ++anIt) {
380 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anIt);
382 GeomShapePtr aShape = aResult->shape();
383 isEdge = aShape && aShape->isEdge();
390 const std::list<ObjectPtr>& anObjects = anInfo.second;
391 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
392 anObjectIt != anObjects.end(); anObjectIt++) {
393 SketchPlugin_Tools::createConstraintAttrObject(sketch(),
394 SketchPlugin_ConstraintCoincidence::ID(),
395 aPointAttribute, *anObjectIt);
399 // move constraints from base feature to replacing feature: ignore coincidences to feature
400 // if attributes of coincidence participated in split
401 ResultPtr aReplacingResult;
402 if (aReplacingFeature.get()) {
403 aReplacingFeature->execute(); // need it to obtain result
404 aReplacingResult = aReplacingFeature->lastResult();
406 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
407 aLast = aRefsToFeature.end();
408 anIt != aLast; anIt++) {
409 AttributePtr anAttribute = *anIt;
411 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences, aFeaturesToDelete))
414 // move tangency constraint to the nearest feature if possible
415 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
418 if (aReplacingResult.get()) {
419 AttributeRefAttrPtr aRefAttr =
420 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
422 aRefAttr->setObject(aReplacingResult);
424 AttributeReferencePtr aReferenceAttr =
425 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
426 if (aReferenceAttr.get())
427 aReferenceAttr->setObject(aReplacingResult);
432 SketchPlugin_SegmentationTools::updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes);
434 // Wait all constraints being created, then send update events
435 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
436 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
438 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
440 // delete constraints
442 if (aFeaturesToDelete.size() > 0) {
443 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
445 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
446 anIt != aFeaturesToDelete.end(); anIt++) {
447 FeaturePtr aFeature = *anIt;
448 std::cout << aFeature->data()->name() << std::endl;
452 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
453 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
455 SketchPlugin_SegmentationTools::updateFeaturesAfterOperation(aFeaturesToUpdate);
457 // Send events to update the sub-features by the solver.
458 if(isUpdateFlushed) {
459 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
462 if (anIsEqualPreviewAndSelected) {
463 // equal preview and selected objects
464 // nothing to do if the preview and selected objects are different
465 if (aReplacingResult.get()) { // base object was removed
466 aPreviewObject = aReplacingResult;
467 //aMessage->setSelectedObject(aReplacingResult);
468 #ifdef DEBUG_TRIM_METHODS
469 if (!aSelectedShape.get())
470 std::cout << "Set empty selected object" << std::endl;
472 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
476 aPreviewObject = ObjectPtr();
478 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
479 aBaseObject = aBaseFeature->lastResult();
480 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
482 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
484 GeomShapePtr aShape = aBaseResult->shape();
485 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
486 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
487 aPreviewObject = aBaseResult;
489 if (!aPreviewObject.get() && aNewFeature.get()) {
490 ResultPtr aNewFeatureResult = aNewFeature->lastResult();
491 if (aNewFeatureResult.get()) {
492 GeomShapePtr aShape = aNewFeatureResult->shape();
493 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
494 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
495 aPreviewObject = aNewFeatureResult;
500 if (aPreviewObject.get()) {
501 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
502 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
503 ModelAPI_EventReentrantMessage::eventId(), this));
504 aMessage->setSelectedObject(aPreviewObject);
505 Events_Loop::loop()->send(aMessage);
508 std::cout << "SketchPlugin_Trim::done" << std::endl;
513 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
515 #ifdef DEBUG_TRIM_METHODS
516 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
518 std::string aFilledAttributeName;
520 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
521 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
522 if (aMessage.get()) {
523 ObjectPtr anObject = aMessage->selectedObject();
524 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
526 if (anObject.get() && aPoint.get()) {
527 if (myCashedShapes.find(anObject) == myCashedShapes.end()) {
528 SketchPlugin_SegmentationTools::fillObjectShapes(
529 this, anObject, myCashedShapes, myObjectToPoints);
531 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
532 if (aShapes.size() > 1) {
533 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
534 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
535 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
536 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
537 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
538 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
539 aRefSelectedAttr->setValue(anObject);
540 aRefPreviewAttr->setValue(anObject);
542 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
543 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
544 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
545 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
546 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
547 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
548 aPointSelectedAttr->setValue(aPoint);
549 aPointPreviewAttr->setValue(aPoint);
551 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
553 GeomShapePtr aSelectedShape = SketchPlugin_SegmentationTools::getSubShape(this,
554 SELECTED_OBJECT(), SELECTED_POINT(), myCashedShapes, myObjectToPoints);
555 #ifdef DEBUG_TRIM_METHODS
556 if (!aSelectedShape.get())
557 std::cout << "Set empty selected object" << std::endl;
559 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
561 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
565 return aFilledAttributeName;
569 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
570 const std::set<AttributePoint2DPtr>& theFurtherCoincidences,
571 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
573 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
574 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
577 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
578 if (!aRefPointAttr.get())
580 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
582 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
583 aLast = theFurtherCoincidences.end();
584 bool aFoundPoint = false;
585 for (; anIt != aLast && !aFoundPoint; anIt++) {
586 AttributePoint2DPtr aPointAttribute = (*anIt);
587 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
588 if (aPoint2d->isEqual(aRefPnt2d)) {
589 // create new coincidence and then remove the old one
590 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
591 SketchPlugin_ConstraintCoincidence::ID(),
592 aRefPointAttr, aPointAttribute);
593 theFeaturesToDelete.insert(aFeature);
599 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
600 const FeaturePtr& theFeature)
602 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
603 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
606 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
611 // get shape of tangent object to the current
612 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
613 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
614 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
615 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
616 aFeature->attribute(aTangentAttr));
617 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
619 // get shape of the feature of the attribute
620 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
621 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
622 GeomAlgoAPI_ShapeTools::PointToRefsMap aPointToAttributeOrObject;
623 std::list<FeaturePtr> aFeatures;
624 aFeatures.push_back(anAttributeFeature);
625 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
626 aPointToAttributeOrObject);
627 if (!aPointToAttributeOrObject.empty())
628 return true; // the attribute feature has a point of intersection, so we do not replace it
630 // get shape of the feature
631 aPointToAttributeOrObject.clear();
633 aFeatures.push_back(theFeature);
634 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
635 aPointToAttributeOrObject);
636 if (!aPointToAttributeOrObject.empty()) {
637 std::set<ResultPtr> anEdgeShapes;
638 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
639 if (!anEdgeShapes.empty()) {
640 ResultPtr aResult = *anEdgeShapes.begin();
642 aRefAttr->setObject(aResult);
643 return true; // the attribute feature has a point of intersection, so we do not replace it
650 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
652 return SketchPlugin_SegmentationTools::getAISObject(thePrevious,
653 this, PREVIEW_OBJECT(), PREVIEW_POINT(), SELECTED_OBJECT(), SELECTED_POINT());
656 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
657 std::set<FeaturePtr>& theFeaturesToUpdate)
659 std::shared_ptr<ModelAPI_Data> aData = data();
661 // Check the base objects are initialized.
662 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
663 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
664 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
665 ResultPtr aBaseFeatureResult = aBaseFeature->lastResult();
667 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
668 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
669 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
671 std::set<AttributePtr>::const_iterator aIt;
672 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
673 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
674 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
675 std::string aRefFeatureKind = aRefFeature->getKind();
676 std::string anAttributeId = anAttr->id();
677 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
678 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
679 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
680 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
681 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
682 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
683 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
684 theFeaturesToDelete.insert(aRefFeature);
685 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
686 theFeaturesToUpdate.insert(aRefFeature);
690 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
691 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
693 /// not found in references
694 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
697 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
698 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
699 aRLast = aRefAttributes.end();
701 std::set<FeaturePtr> aFeaturesToDelete;
702 for (; aRefIt != aRLast; aRefIt++) {
703 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
704 if (aRefAttr.get()) {
705 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
710 // delete constraints
711 if (aFeaturesToDelete.size() > 0) {
712 std::cout << "removeReferencesToAttribute: " << std::endl;
714 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
715 anIt != aFeaturesToDelete.end(); anIt++) {
716 FeaturePtr aFeature = *anIt;
717 std::cout << aFeature->data()->name() << std::endl;
721 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
722 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
725 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
726 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
727 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
728 std::set<AttributePoint2DPtr>& thePoints,
729 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
731 FeaturePtr anNewFeature;
733 // Check the base objects are initialized.
734 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
735 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
736 ObjectPtr aBaseObject = aBaseObjectAttr->value();
737 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
740 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
741 SketchPlugin_SegmentationTools::getFeaturePoints(
742 aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
744 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
745 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
747 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
748 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
749 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
750 aStartShapePoint, aLastShapePoint);
752 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
753 if (aStartShapePoint.get())
754 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
755 aStartShapePoint->y() << "]" << std::endl;
756 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
757 aStartFeaturePoint->y() << "]" << std::endl;
758 if (aLastShapePoint.get())
759 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
760 aLastShapePoint->y() << "]" << std::endl;
761 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
762 aLastFeaturePoint->y() << "]" << std::endl;
765 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
766 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
767 if (isStartPoint || isLastPoint) {
768 // result is one line: changed existing line
769 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
770 : SketchPlugin_Line::END_ID();
771 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
772 if (aStartShapePoint.get() && aLastShapePoint.get())
773 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
775 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
777 // it is important to delete references before the feature modification because
778 // if deletion will be after the feature modification, solver returns the feature back
779 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
780 theBaseRefAttributes);
782 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
783 //theModifiedAttributes.insert(
784 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
786 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
787 (aBaseFeature->attribute(aModifiedAttribute)));
790 // result is two lines: start line point - start shape point,
791 // last shape point - last line point
792 // create second line
793 anNewFeature = SketchPlugin_SegmentationTools::createLineFeature(
794 aBaseFeature, aLastShapePoint, aLastFeaturePoint);
795 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
796 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
798 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
799 theModifiedAttributes.insert(
800 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
801 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
804 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
806 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
807 (aBaseFeature->attribute(aModifiedAttribute)));
809 // Collinear constraint for lines
810 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
811 SketchPlugin_ConstraintCollinear::ID(),
812 aBaseFeature->lastResult(),
813 anNewFeature->lastResult());
818 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
819 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
820 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
821 std::set<AttributePoint2DPtr>& thePoints,
822 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
824 FeaturePtr anNewFeature;
825 // Check the base objects are initialized.
826 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
827 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
828 ObjectPtr aBaseObject = aBaseObjectAttr->value();
829 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
832 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
833 SketchPlugin_SegmentationTools::getFeaturePoints(
834 aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
836 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
837 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
839 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
840 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
841 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
842 aStartShapePoint, aLastShapePoint);
844 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
845 if (aStartShapePoint.get())
846 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
847 aStartShapePoint->y() << "]" << std::endl;
848 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
849 aStartArcPoint->y() << "]" << std::endl;
850 if (aLastShapePoint.get())
851 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
852 aLastShapePoint->y() << "]" << std::endl;
853 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
854 aLastArcPoint->y() << "]" << std::endl;
857 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
858 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
859 if (isStartPoint || isLastPoint) {
860 // result is one arc: changed existing arc
861 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
862 : SketchPlugin_Arc::END_ID();
863 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
864 if (aStartShapePoint.get() && aLastShapePoint.get())
865 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
867 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
869 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
870 theBaseRefAttributes);
872 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
874 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
875 (aBaseFeature->attribute(aModifiedAttribute)));
878 // result is two arcs: start arc point - start shape point, last shape point - last arc point
880 anNewFeature = SketchPlugin_SegmentationTools::createArcFeature(
881 aBaseFeature, aLastShapePoint, aLastArcPoint);
882 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
883 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
885 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
886 theModifiedAttributes.insert(
887 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
888 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
891 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
893 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
894 (aBaseFeature->attribute(aModifiedAttribute)));
896 // equal Radius constraint for arcs
897 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
898 SketchPlugin_ConstraintEqual::ID(),
899 aBaseFeature->lastResult(),
900 anNewFeature->lastResult());
901 // coincident centers constraint
902 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
903 SketchPlugin_ConstraintCoincidence::ID(),
904 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
905 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
908 std::cout << "Created arc on points:" << std::endl;
909 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
910 aStartShapePoint->y() << "]" << std::endl;
916 FeaturePtr SketchPlugin_Trim::trimEllipticArc(
917 const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
918 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
919 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
920 std::set<AttributePoint2DPtr>& thePoints,
921 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
923 FeaturePtr anNewFeature;
924 // Check the base objects are initialized.
925 AttributeReferencePtr aBaseObjectAttr = reference(SELECTED_OBJECT());
926 ObjectPtr aBaseObject = aBaseObjectAttr->value();
927 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
930 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
931 SketchPlugin_SegmentationTools::getFeaturePoints(
932 aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
934 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
935 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
937 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
938 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
939 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
940 aStartShapePoint, aLastShapePoint);
942 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
943 if (aStartShapePoint.get())
944 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
945 aStartShapePoint->y() << "]" << std::endl;
946 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
947 aStartArcPoint->y() << "]" << std::endl;
948 if (aLastShapePoint.get())
949 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
950 aLastShapePoint->y() << "]" << std::endl;
951 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
952 aLastArcPoint->y() << "]" << std::endl;
955 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
956 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
957 if (isStartPoint || isLastPoint) {
958 // result is one arc: changed existing arc
959 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_EllipticArc::START_POINT_ID()
960 : SketchPlugin_EllipticArc::END_POINT_ID();
961 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
962 if (aStartShapePoint.get() && aLastShapePoint.get())
963 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
965 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
967 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
968 theBaseRefAttributes);
970 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
972 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
973 (aBaseFeature->attribute(aModifiedAttribute)));
976 // result is two arcs: start arc point - start shape point, last shape point - last arc point
978 anNewFeature = SketchPlugin_SegmentationTools::createArcFeature(
979 aBaseFeature, aLastShapePoint, aLastArcPoint);
980 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
981 anNewFeature->attribute(SketchPlugin_EllipticArc::START_POINT_ID())));
983 std::string aModifiedAttribute = SketchPlugin_EllipticArc::END_POINT_ID();
984 theModifiedAttributes.insert(
985 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
986 anNewFeature->attribute(SketchPlugin_EllipticArc::END_POINT_ID())));
989 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
991 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
992 (aBaseFeature->attribute(aModifiedAttribute)));
994 // make elliptic arcs equal
995 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
996 SketchPlugin_ConstraintEqual::ID(),
997 aBaseFeature->lastResult(),
998 anNewFeature->lastResult());
999 // coincident centers constraint
1000 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1001 SketchPlugin_ConstraintCoincidence::ID(),
1002 aBaseFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID()),
1003 anNewFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID()));
1006 std::cout << "Created arc on points:" << std::endl;
1007 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1008 aStartShapePoint->y() << "]" << std::endl;
1011 return anNewFeature;
1014 FeaturePtr SketchPlugin_Trim::trimClosed(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1015 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1016 std::set<AttributePoint2DPtr>& thePoints,
1017 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1019 // Check the base objects are initialized.
1020 AttributeReferencePtr aBaseObjectAttr = reference(SELECTED_OBJECT());
1021 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1022 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1025 FeaturePtr anNewFeature = SketchPlugin_SegmentationTools::createArcFeature(
1026 aBaseFeature, theStartShapePoint, theLastShapePoint);
1027 // arc created by trim of circle is always correct, that means that it is not inversed
1028 const std::string& aReversedAttrName = anNewFeature->getKind() == SketchPlugin_Arc::ID() ?
1029 SketchPlugin_Arc::REVERSED_ID() : SketchPlugin_EllipticArc::REVERSED_ID();
1030 anNewFeature->boolean(aReversedAttrName)->setValue(false);
1032 if (aBaseFeature->getKind() == SketchPlugin_Circle::ID()) {
1033 theModifiedAttributes.insert(
1034 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1035 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1037 else if (aBaseFeature->getKind() == SketchPlugin_Ellipse::ID()) {
1038 theModifiedAttributes.insert(std::make_pair(
1039 aBaseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()),
1040 anNewFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID())));
1041 theModifiedAttributes.insert(std::make_pair(
1042 aBaseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()),
1043 anNewFeature->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID())));
1044 theModifiedAttributes.insert(std::make_pair(
1045 aBaseFeature->attribute(SketchPlugin_Ellipse::SECOND_FOCUS_ID()),
1046 anNewFeature->attribute(SketchPlugin_EllipticArc::SECOND_FOCUS_ID())));
1047 theModifiedAttributes.insert(std::make_pair(
1048 aBaseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()),
1049 anNewFeature->attribute(SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID())));
1050 theModifiedAttributes.insert(std::make_pair(
1051 aBaseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
1052 anNewFeature->attribute(SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID())));
1053 theModifiedAttributes.insert(std::make_pair(
1054 aBaseFeature->attribute(SketchPlugin_Ellipse::MINOR_AXIS_START_ID()),
1055 anNewFeature->attribute(SketchPlugin_EllipticArc::MINOR_AXIS_START_ID())));
1056 theModifiedAttributes.insert(std::make_pair(
1057 aBaseFeature->attribute(SketchPlugin_Ellipse::MINOR_AXIS_END_ID()),
1058 anNewFeature->attribute(SketchPlugin_EllipticArc::MINOR_AXIS_END_ID())));
1060 // update the PARENT_ID reference for all the features created by the ellipse
1061 const std::set<AttributePtr>& aRefs = aBaseFeature->data()->refsToMe();
1062 std::list<AttributePtr> aRefsToParent;
1063 for (std::set<AttributePtr>::const_iterator aRef = aRefs.begin(); aRef != aRefs.end(); ++aRef) {
1064 if ((*aRef)->id() == SketchPlugin_SketchEntity::PARENT_ID())
1065 aRefsToParent.push_back(*aRef);
1067 for (std::list<AttributePtr>::iterator aRef = aRefsToParent.begin();
1068 aRef != aRefsToParent.end(); ++aRef) {
1069 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*aRef)->setValue(anNewFeature);
1071 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRef)->owner());
1072 SketchPlugin_Tools::replaceInName(anOwner, aBaseFeature->name(), anNewFeature->name());
1073 SketchPlugin_Tools::replaceInName(anOwner->lastResult(),
1074 aBaseFeature->name(), anNewFeature->name());
1078 const std::string& aStartAttrName = anNewFeature->getKind() == SketchPlugin_Arc::ID() ?
1079 SketchPlugin_Arc::START_ID() : SketchPlugin_EllipticArc::START_POINT_ID();
1080 const std::string& aEndAttrName = anNewFeature->getKind() == SketchPlugin_Arc::ID() ?
1081 SketchPlugin_Arc::END_ID() : SketchPlugin_EllipticArc::END_POINT_ID();
1083 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1084 (anNewFeature->attribute(aStartAttrName)));
1085 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1086 (anNewFeature->attribute(aEndAttrName)));
1088 return anNewFeature;
1091 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1092 const AttributePoint2DPtr& theEndPointAttr,
1093 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1094 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1096 if (!theFirstPoint.get() || !theLastPoint.get())
1099 // if first point is closer to last point, swap first and last values
1100 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1101 theStartPointAttr->pnt()->distance(theLastPoint)) {
1102 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1103 theFirstPoint = theLastPoint;
1104 theLastPoint = aTmpPoint;
1108 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1109 const AttributePoint2DPtr& theStartPointAttr,
1110 const AttributePoint2DPtr& theEndPointAttr,
1111 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1112 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1114 if (!theFirstPoint.get() || !theSecondPoint.get())
1117 static const double anAngleTol = 1.e-12;
1119 const std::string& aCenterAttrName = theArc->getKind() == SketchPlugin_Arc::ID() ?
1120 SketchPlugin_Arc::CENTER_ID() : SketchPlugin_EllipticArc::CENTER_ID();
1121 const std::string& aReversedAttrName = theArc->getKind() == SketchPlugin_Arc::ID() ?
1122 SketchPlugin_Arc::REVERSED_ID() : SketchPlugin_EllipticArc::REVERSED_ID();
1124 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1125 theArc->attribute(aCenterAttrName))->pnt();
1126 bool isReversed = theArc->boolean(aReversedAttrName)->value();
1128 // collect directions to each point
1129 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1130 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1131 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1132 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1133 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1134 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1136 // sort points by their angular values
1137 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1138 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1139 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1140 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1141 aFirstPtAngle += aPeriod;
1142 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1143 aSecondPtAngle += aPeriod;
1145 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1146 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1147 theFirstPoint = theSecondPoint;
1148 theSecondPoint = aTmpPoint;
1152 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1153 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1155 std::string anAttributeType = theModifiedAttribute->attributeType();
1156 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1157 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1158 theModifiedAttribute);
1159 aModifiedAttribute->setValue(thePoint);
1162 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1163 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1164 theModifiedAttribute->id() <<
1165 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1170 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1171 const AttributePtr& theSourceAttribute)
1173 std::string anAttributeType = theModifiedAttribute->attributeType();
1174 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1175 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1176 theModifiedAttribute);
1177 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1178 theSourceAttribute);
1180 if (aModifiedAttribute.get() && aSourceAttribute.get())
1181 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1183 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1184 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1185 theModifiedAttribute);
1186 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1187 theSourceAttribute);
1189 if (aModifiedAttribute.get() && aSourceAttribute.get())
1190 aModifiedAttribute->setValue(aSourceAttribute->value());