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>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_Tools.h>
34 #include <ModelAPI_AttributeBoolean.h>
36 #include <ModelAPI_Validator.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_AttributeDouble.h>
40 #include <ModelGeomAlgo_Shape.h>
42 #include <SketchPlugin_Arc.h>
43 #include <SketchPlugin_ConstraintMiddle.h>
44 #include <SketchPlugin_Circle.h>
45 #include <SketchPlugin_ConstraintCoincidence.h>
46 #include <SketchPlugin_ConstraintEqual.h>
47 #include <SketchPlugin_ConstraintTangent.h>
48 #include <SketchPlugin_ConstraintLength.h>
49 #include <SketchPlugin_ConstraintMirror.h>
50 #include <SketchPlugin_ConstraintCollinear.h>
51 #include <SketchPlugin_Line.h>
52 #include <SketchPlugin_MultiRotation.h>
53 #include <SketchPlugin_MultiTranslation.h>
54 #include <SketchPlugin_Point.h>
55 #include <SketchPlugin_Projection.h>
56 #include <SketchPlugin_Tools.h>
58 #include <ModelAPI_EventReentrantMessage.h>
60 #include <ModelAPI_Events.h>
61 #include <SketchPlugin_Line.h>
62 #include <SketchPlugin_Arc.h>
63 #include <SketchPlugin_Circle.h>
65 #include <ModelGeomAlgo_Point2D.h>
66 #include <Events_Loop.h>
74 #ifdef DEBUG_TRIM_METHODS
78 static const double PI = 3.141592653589793238463;
80 static const std::string OPERATION_HIGHLIGHT_COLOR() { return "128, 0, 0"; }
81 static const std::string OPERATION_REMOVE_FEATURE_COLOR() { return "255, 174, 201"; }
83 SketchPlugin_Trim::SketchPlugin_Trim()
87 void SketchPlugin_Trim::initAttributes()
89 data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
90 data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
92 data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
93 data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
95 data()->attribute(PREVIEW_POINT())->setIsArgument(false);
96 data()->attribute(SELECTED_POINT())->setIsArgument(false);
97 data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
99 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
100 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
103 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
104 const std::string& thePointAttributeId,
105 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
106 std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
108 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
109 data()->attribute(theObjectAttributeId));
110 ObjectPtr aBaseObject = aBaseObjectAttr->value();
112 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
113 data()->attribute(thePointAttributeId));
114 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
115 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
116 anAttributePnt2d->y());
118 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
119 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
121 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
122 if (!aShapes.empty()) {
123 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
124 for (; anIt != aLast; anIt++) {
125 GeomShapePtr aBaseShape = *anIt;
126 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
127 if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, anAttributePnt, aProjectedPoint)) {
129 if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
130 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
131 //GeomAPI_Shape::Orientation anOrientation = anEdge->orientation();
132 //if (anOrientation == GeomAPI_Shape::REVERSED) {
133 aStartPoint = anEdge->lastPoint();
134 aLastPoint = anEdge->firstPoint();
137 //aStartPoint = anEdge->firstPoint();
138 //aLastPoint = anEdge->lastPoint();
145 std::cout << "<findShapePoints> => "
146 << std::endl << "Attribute point: "
147 << anAttributePnt->x() << ", " << anAttributePnt->y() << ", " << anAttributePnt->z() << "]"
148 << std::endl << "Start Point: ["
149 << aStartPoint->x() << ", " << aStartPoint->y() << ", " << aStartPoint->z() << "]"
150 << std::endl << "Last Point: ["
151 << aLastPoint->x() << ", " << aLastPoint->y() << ", " << aLastPoint->z() << "]"
156 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Trim::convertPoint(
157 const std::shared_ptr<GeomAPI_Pnt>& thePoint)
159 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
163 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
164 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
165 ObjectPtr aBaseObject = aBaseObjectAttr->value();
166 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
167 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
170 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
171 for (PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
172 aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
173 if (aPointIt->first->isEqual(thePoint)) {
174 const std::pair<std::list<AttributePoint2DPtr >,
175 std::list<ObjectPtr > >& anInfo = aPointIt->second;
176 const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
177 if (!anAttributes.empty()) {
178 aPoint = anAttributes.front()->pnt();
182 aPoint = sketch()->to2D(thePoint);
188 // returns an end of the shape to define direction of split if feature's attribute
190 aPoint = sketch()->to2D(thePoint);
195 void SketchPlugin_Trim::execute()
197 #ifdef DEBUG_TRIM_METHODS
198 std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
201 SketchPlugin_Sketch* aSketch = sketch();
203 setError("Error: Sketch object is empty.");
207 // Check the base objects are initialized.
208 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
209 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
210 if(!aBaseObjectAttr->isInitialized()) {
211 setError("Error: Base object is not initialized.");
214 ObjectPtr aBaseObject = aBaseObjectAttr->value();
215 if (!aBaseObject.get()) {
216 setError("Error: Base object is not initialized.");
219 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
221 /// Remove reference of this feature to feature used in preview, it is not necessary anymore
222 /// as trim will be removed after execute
223 AttributeReferencePtr aPreviewObjectAttr =
224 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
225 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
227 ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
228 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
229 data()->attribute(PREVIEW_POINT()));
230 std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
231 // nullify pointer of preview attribute
232 aPreviewObjectAttr->setValue(ResultPtr());
234 bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
237 std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
239 std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
241 findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
243 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
244 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
245 /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
246 std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
247 getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
248 // find references(attributes and features) to the base feature
249 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
250 std::list<AttributePtr> aRefsToFeature;
251 getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
253 std::cout << "---- getRefAttributes ----" << std::endl;
254 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
255 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
256 std::cout << std::endl << "References to attributes of base feature [" <<
257 aBaseRefAttributes.size() << "]" << std::endl;
258 for (; aRefIt != aRefLast; aRefIt++) {
259 AttributePtr aBaseAttr = aRefIt->first;
260 std::list<AttributePtr> aRefAttributes = aRefIt->second;
261 std::string aRefsInfo;
262 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
263 aRefAttrLast = aRefAttributes.end();
264 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
265 if (!aRefsInfo.empty())
266 aRefsInfo.append(",");
267 AttributePtr aRAttr = *aRefAttrIt;
268 aRefsInfo.append(aRAttr->id());
269 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
270 aRefsInfo.append("(" + aRFeature->name() + ") ");
272 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
273 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
274 std::cout << aPointAttr->id().c_str() <<
275 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
277 std::cout << std::endl;
278 std::cout << std::endl << "References to base feature [" <<
279 aRefsToFeature.size() << "]" << std::endl;
280 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
281 aRefAttrLast = aRefsToFeature.end();
282 std::string aRefsInfo;
283 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
284 if (!aRefsInfo.empty())
285 aRefsInfo.append(",");
286 AttributePtr aRAttr = *aRefAttrIt;
287 aRefsInfo.append(aRAttr->id());
288 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
289 aRefsInfo.append("(" + aRFeature->name() + ") ");
291 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
292 std::cout << "---- getRefAttributes:end ----" << std::endl;
294 std::set<AttributePoint2DPtr> aFurtherCoincidences;
295 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
296 const std::string& aKind = aBaseFeature->getKind();
297 FeaturePtr aReplacingFeature, aNewFeature;
298 if (aKind == SketchPlugin_Circle::ID()) {
299 aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
300 aFurtherCoincidences, aModifiedAttributes);
302 aFeaturesToDelete.insert(aBaseFeature);
303 // as circle is removed, erase it from dependencies(arguments) of this feature
304 // otherwise Trim feature will be removed with the circle before
305 // this operation is finished
306 aBaseObjectAttr->setObject(ResultPtr());
308 else if (aKind == SketchPlugin_Line::ID()) {
309 aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
310 aFurtherCoincidences, aModifiedAttributes);
312 else if (aKind == SketchPlugin_Arc::ID()) {
313 aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
314 aFurtherCoincidences, aModifiedAttributes);
317 // constraints to end points of trim feature
318 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
319 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
321 // create coincidence to objects, intersected the base object
322 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
323 for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
324 aLast = aFurtherCoincidences.end();
325 anIt != aLast; anIt++) {
326 AttributePoint2DPtr aPointAttribute = (*anIt);
327 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
330 std::cout << "<compare Points> => " << std::endl
331 << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
332 if (aStartShapePoint2d.get())
333 std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
335 if (aLastShapePoint2d.get())
336 std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
340 std::shared_ptr<GeomAPI_Pnt> aPoint;
341 if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
342 aPoint = aStartShapePoint;
343 else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
344 aPoint = aLastShapePoint;
349 std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
350 for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
353 if (aRefIt->first->isEqual(aPoint)) {
354 anInfo = aRefIt->second;
358 const std::list<ObjectPtr>& anObjects = anInfo.second;
359 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
360 anObjectIt != anObjects.end(); anObjectIt++) {
361 SketchPlugin_Tools::createConstraintAttrObject(sketch(),
362 SketchPlugin_ConstraintCoincidence::ID(),
363 aPointAttribute, *anObjectIt);
367 // move constraints from base feature to replacing feature: ignore coincidences to feature
368 // if attributes of coincidence participated in split
369 ResultPtr aReplacingResult;
370 if (aReplacingFeature.get()) {
371 aReplacingFeature->execute(); // need it to obtain result
372 aReplacingResult = getFeatureResult(aReplacingFeature);
374 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
375 aLast = aRefsToFeature.end();
376 anIt != aLast; anIt++) {
377 AttributePtr anAttribute = *anIt;
379 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences, aFeaturesToDelete))
382 // move tangency constraint to the nearest feature if possible
383 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
386 if (aReplacingResult.get()) {
387 AttributeRefAttrPtr aRefAttr =
388 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
390 aRefAttr->setObject(aReplacingResult);
392 AttributeReferencePtr aReferenceAttr =
393 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
394 if (aReferenceAttr.get())
395 aReferenceAttr->setObject(aReplacingResult);
400 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
402 // Wait all constraints being created, then send update events
403 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
404 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
406 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
408 // delete constraints
410 if (aFeaturesToDelete.size() > 0) {
411 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
413 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
414 anIt != aFeaturesToDelete.end(); anIt++) {
415 FeaturePtr aFeature = *anIt;
416 std::cout << aFeature->data()->name() << std::endl;
420 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
421 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
423 updateFeaturesAfterTrim(aFeaturesToUpdate);
425 // Send events to update the sub-features by the solver.
426 if(isUpdateFlushed) {
427 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
430 if (anIsEqualPreviewAndSelected) {
431 // equal preview and selected objects
432 // nothing to do if the preview and selected objects are different
433 if (aReplacingResult.get()) { // base object was removed
434 aPreviewObject = aReplacingResult;
435 //aMessage->setSelectedObject(aReplacingResult);
436 #ifdef DEBUG_TRIM_METHODS
437 if (!aSelectedShape.get())
438 std::cout << "Set empty selected object" << std::endl;
440 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
444 aPreviewObject = ObjectPtr();
446 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
447 aBaseObject = getFeatureResult(aBaseFeature);
448 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
450 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
452 GeomShapePtr aShape = aBaseResult->shape();
453 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
454 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
455 aPreviewObject = aBaseResult;
457 if (!aPreviewObject.get() && aNewFeature.get()) {
458 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
459 if (aNewFeatureResult.get()) {
460 GeomShapePtr aShape = aNewFeatureResult->shape();
461 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
462 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
463 aPreviewObject = aNewFeatureResult;
468 if (aPreviewObject.get()) {
469 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
470 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
471 ModelAPI_EventReentrantMessage::eventId(), this));
472 aMessage->setSelectedObject(aPreviewObject);
473 Events_Loop::loop()->send(aMessage);
476 std::cout << "SketchPlugin_Trim::done" << std::endl;
481 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
483 #ifdef DEBUG_TRIM_METHODS
484 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
486 std::string aFilledAttributeName;
488 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
489 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
490 if (aMessage.get()) {
491 ObjectPtr anObject = aMessage->selectedObject();
492 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
494 if (anObject.get() && aPoint.get()) {
495 if (myCashedShapes.find(anObject) == myCashedShapes.end())
496 fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
497 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
498 if (aShapes.size() > 1) {
499 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
500 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
501 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
502 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
503 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
504 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
505 aRefSelectedAttr->setValue(anObject);
506 aRefPreviewAttr->setValue(anObject);
508 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
509 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
510 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
511 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
512 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
513 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
514 aPointSelectedAttr->setValue(aPoint);
515 aPointPreviewAttr->setValue(aPoint);
517 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
519 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
520 #ifdef DEBUG_TRIM_METHODS
521 if (!aSelectedShape.get())
522 std::cout << "Set empty selected object" << std::endl;
524 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
526 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
530 return aFilledAttributeName;
534 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
535 const std::set<AttributePoint2DPtr>& theFurtherCoincidences,
536 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
538 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
539 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
542 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
543 if (!aRefPointAttr.get())
545 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
547 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
548 aLast = theFurtherCoincidences.end();
549 bool aFoundPoint = false;
550 for (; anIt != aLast && !aFoundPoint; anIt++) {
551 AttributePoint2DPtr aPointAttribute = (*anIt);
552 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
553 if (aPoint2d->isEqual(aRefPnt2d)) {
554 // create new coincidence and then remove the old one
555 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
556 SketchPlugin_ConstraintCoincidence::ID(),
557 aRefPointAttr, aPointAttribute);
558 theFeaturesToDelete.insert(aFeature);
564 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
565 const FeaturePtr& theFeature)
567 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
568 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
571 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
576 // get shape of tangent object to the current
577 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
578 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
579 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
580 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
581 aFeature->attribute(aTangentAttr));
582 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
584 // get shape of the feature of the attribute
585 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
586 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
587 PointToRefsMap aPointToAttributeOrObject;
588 std::list<FeaturePtr> aFeatures;
589 aFeatures.push_back(anAttributeFeature);
590 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
591 aPointToAttributeOrObject);
592 if (!aPointToAttributeOrObject.empty())
593 return true; // the attribute feature has a point of intersection, so we do not replace it
595 // get shape of the feature
596 aPointToAttributeOrObject.clear();
598 aFeatures.push_back(theFeature);
599 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
600 aPointToAttributeOrObject);
601 if (!aPointToAttributeOrObject.empty()) {
602 std::set<ResultPtr> anEdgeShapes;
603 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
604 if (!anEdgeShapes.empty()) {
605 ResultPtr aResult = *anEdgeShapes.begin();
607 aRefAttr->setObject(aResult);
608 return true; // the attribute feature has a point of intersection, so we do not replace it
615 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
617 #ifdef DEBUG_TRIM_METHODS
618 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
621 AISObjectPtr anAIS = thePrevious;
623 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
624 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
625 if (aPreviewShape.get())
626 aShapes.push_back(aPreviewShape);
627 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
628 if (aSelectedShape.get())
629 aShapes.push_back(aSelectedShape);
632 return AISObjectPtr();
634 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
635 if (!aBaseShape.get())
636 return AISObjectPtr();
638 if (aBaseShape.get()) {
640 anAIS = AISObjectPtr(new GeomAPI_AISObject);
641 anAIS->createShape(aBaseShape);
643 std::vector<int> aColor;
644 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
645 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
646 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
647 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
648 // width when there is not base object should be extened in several points
649 // in order to see this preview over highlight
650 anAIS->setWidth(aWidth+4);
651 anAIS->setLineStyle(aLineStyle);
654 anAIS = AISObjectPtr();
659 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
660 const std::string& thePointAttributeId)
662 GeomShapePtr aBaseShape;
664 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
665 data()->attribute(theObjectAttributeId));
666 ObjectPtr aBaseObject = anObjectAttr->value();
667 if (!aBaseObject.get())
671 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
672 data()->attribute(thePointAttributeId));
673 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
674 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
675 anAttributePnt2d->y());
677 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
678 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
680 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
681 if (!aShapes.empty()) {
682 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
683 for (; anIt != aLast; anIt++) {
684 GeomShapePtr aShape = *anIt;
685 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
686 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
693 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
694 AttributePoint2DPtr& theStartPointAttr,
695 AttributePoint2DPtr& theEndPointAttr)
697 std::string aFeatureKind = theFeature->getKind();
698 std::string aStartAttributeName, anEndAttributeName;
699 if (aFeatureKind == SketchPlugin_Line::ID()) {
700 aStartAttributeName = SketchPlugin_Line::START_ID();
701 anEndAttributeName = SketchPlugin_Line::END_ID();
703 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
704 aStartAttributeName = SketchPlugin_Arc::START_ID();
705 anEndAttributeName = SketchPlugin_Arc::END_ID();
707 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
708 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
709 theFeature->attribute(aStartAttributeName));
710 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
711 theFeature->attribute(anEndAttributeName));
715 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
716 std::set<FeaturePtr>& theFeaturesToUpdate)
718 std::shared_ptr<ModelAPI_Data> aData = data();
720 // Check the base objects are initialized.
721 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
722 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
723 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
724 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
726 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
727 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
728 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
730 std::set<AttributePtr>::const_iterator aIt;
731 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
732 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
733 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
734 std::string aRefFeatureKind = aRefFeature->getKind();
735 std::string anAttributeId = anAttr->id();
736 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
737 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
738 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
739 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
740 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
741 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
742 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
743 theFeaturesToDelete.insert(aRefFeature);
744 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
745 theFeaturesToUpdate.insert(aRefFeature);
749 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
750 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
751 std::list<AttributePtr>& theRefsToFeature)
755 std::list<AttributePtr> aPointAttributes =
756 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
757 std::set<AttributePtr> aPointAttributesSet;
759 std::list<AttributePtr>::const_iterator aPIt =
760 aPointAttributes.begin(), aPLast = aPointAttributes.end();
761 for (; aPIt != aPLast; aPIt++)
762 aPointAttributesSet.insert(*aPIt);
764 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
765 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
766 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
768 std::set<AttributePtr>::const_iterator aIt;
769 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
770 AttributePtr anAttr = (*aIt);
771 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
772 if (anAttrFeature.get() != this &&
773 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
774 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
775 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
776 AttributePtr anAttrInRef = aRefAttr->attr();
777 if (anAttrInRef.get() &&
778 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
779 if (theRefs.find(anAttrInRef) != theRefs.end())
780 theRefs[anAttrInRef].push_back(aRefAttr);
782 std::list<AttributePtr> anAttrList;
783 anAttrList.push_back(aRefAttr);
784 theRefs[anAttrInRef] = anAttrList;
788 else { /// find attributes referenced to feature itself
789 theRefsToFeature.push_back(anAttr);
795 void SketchPlugin_Trim::updateRefAttConstraints(
796 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
797 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
798 std::set<FeaturePtr>& theFeaturesToDelete)
801 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
804 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
805 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
806 for (; anIt != aLast; anIt++) {
807 AttributePtr anAttribute = anIt->first;
809 /// not found in references
810 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
812 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
813 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
814 aRLast = aRefAttributes.end();
816 AttributePtr aNewAttribute = anIt->second;
817 if (aNewAttribute.get()) {
818 for (; aRefIt != aRLast; aRefIt++) {
819 AttributeRefAttrPtr aRefAttr =
820 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
821 if (aRefAttr.get()) {
822 aRefAttr->setAttr(aNewAttribute);
829 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
830 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
832 /// not found in references
833 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
836 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
837 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
838 aRLast = aRefAttributes.end();
840 std::set<FeaturePtr> aFeaturesToDelete;
841 for (; aRefIt != aRLast; aRefIt++) {
842 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
843 if (aRefAttr.get()) {
844 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
849 // delete constraints
850 if (aFeaturesToDelete.size() > 0) {
851 std::cout << "removeReferencesToAttribute: " << std::endl;
853 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
854 anIt != aFeaturesToDelete.end(); anIt++) {
855 FeaturePtr aFeature = *anIt;
856 std::cout << aFeature->data()->name() << std::endl;
860 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
861 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
864 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
866 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
867 aLast = theFeaturesToUpdate.end();
868 for (; anIt != aLast; anIt++) {
869 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
870 std::string aRefFeatureKind = aRefFeature->getKind();
871 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
872 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
873 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
874 if (aLenghtFeature.get()) {
875 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
876 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
878 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
879 aValueAttr->setValue(aValue);
885 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
886 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
887 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
888 std::set<AttributePoint2DPtr>& thePoints,
889 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
891 FeaturePtr anNewFeature;
893 // Check the base objects are initialized.
894 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
895 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
896 ObjectPtr aBaseObject = aBaseObjectAttr->value();
897 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
900 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
901 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
903 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
904 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
906 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
907 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
908 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
909 aStartShapePoint, aLastShapePoint);
911 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
912 if (aStartShapePoint.get())
913 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
914 aStartShapePoint->y() << "]" << std::endl;
915 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
916 aStartFeaturePoint->y() << "]" << std::endl;
917 if (aLastShapePoint.get())
918 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
919 aLastShapePoint->y() << "]" << std::endl;
920 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
921 aLastFeaturePoint->y() << "]" << std::endl;
924 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
925 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
926 if (isStartPoint || isLastPoint) {
927 // result is one line: changed existing line
928 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
929 : SketchPlugin_Line::END_ID();
930 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
931 if (aStartShapePoint.get() && aLastShapePoint.get())
932 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
934 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
936 // it is important to delete references before the feature modification because
937 // if deletion will be after the feature modification, solver returns the feature back
938 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
939 theBaseRefAttributes);
941 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
942 //theModifiedAttributes.insert(
943 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
945 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
946 (aBaseFeature->attribute(aModifiedAttribute)));
949 // result is two lines: start line point - start shape point,
950 // last shape point - last line point
951 // create second line
952 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
953 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
954 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
956 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
957 theModifiedAttributes.insert(
958 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
959 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
962 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
964 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
965 (aBaseFeature->attribute(aModifiedAttribute)));
967 // Collinear constraint for lines
968 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
969 SketchPlugin_ConstraintCollinear::ID(),
970 getFeatureResult(aBaseFeature),
971 getFeatureResult(anNewFeature));
976 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
977 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
978 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
979 std::set<AttributePoint2DPtr>& thePoints,
980 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
982 FeaturePtr anNewFeature;
983 // Check the base objects are initialized.
984 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
985 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
986 ObjectPtr aBaseObject = aBaseObjectAttr->value();
987 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
990 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
991 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
993 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
994 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
996 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
997 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
998 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
999 aStartShapePoint, aLastShapePoint);
1001 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1002 if (aStartShapePoint.get())
1003 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1004 aStartShapePoint->y() << "]" << std::endl;
1005 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1006 aStartArcPoint->y() << "]" << std::endl;
1007 if (aLastShapePoint.get())
1008 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1009 aLastShapePoint->y() << "]" << std::endl;
1010 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1011 aLastArcPoint->y() << "]" << std::endl;
1014 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1015 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1016 if (isStartPoint || isLastPoint) {
1017 // result is one arc: changed existing arc
1018 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1019 : SketchPlugin_Arc::END_ID();
1020 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1021 if (aStartShapePoint.get() && aLastShapePoint.get())
1022 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1024 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1026 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1027 theBaseRefAttributes);
1029 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1031 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1032 (aBaseFeature->attribute(aModifiedAttribute)));
1035 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1036 // create second arc
1037 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1038 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1039 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1041 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1042 theModifiedAttributes.insert(
1043 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1044 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1047 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1049 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1050 (aBaseFeature->attribute(aModifiedAttribute)));
1052 // equal Radius constraint for arcs
1053 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
1054 SketchPlugin_ConstraintEqual::ID(),
1055 getFeatureResult(aBaseFeature),
1056 getFeatureResult(anNewFeature));
1057 // coincident centers constraint
1058 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1059 SketchPlugin_ConstraintCoincidence::ID(),
1060 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1061 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1064 std::cout << "Created arc on points:" << std::endl;
1065 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1066 aStartShapePoint->y() << "]" << std::endl;
1069 return anNewFeature;
1072 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1073 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1074 std::set<AttributePoint2DPtr>& thePoints,
1075 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1077 // Check the base objects are initialized.
1078 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1079 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1080 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1081 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1084 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1085 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1088 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1089 // arc created by trim of circle is always correct, that means that it is not inversed
1090 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1092 theModifiedAttributes.insert(
1093 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1094 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1096 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1097 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1098 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1099 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1101 return anNewFeature;
1104 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1105 const AttributePoint2DPtr& theEndPointAttr,
1106 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1107 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1109 if (!theFirstPoint.get() || !theLastPoint.get())
1112 // if first point is closer to last point, swap first and last values
1113 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1114 theStartPointAttr->pnt()->distance(theLastPoint)) {
1115 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1116 theFirstPoint = theLastPoint;
1117 theLastPoint = aTmpPoint;
1121 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1122 const AttributePoint2DPtr& theStartPointAttr,
1123 const AttributePoint2DPtr& theEndPointAttr,
1124 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1125 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1127 if (!theFirstPoint.get() || !theSecondPoint.get())
1130 static const double anAngleTol = 1.e-12;
1132 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1133 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1134 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1136 // collect directions to each point
1137 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1138 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1139 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1140 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1141 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1142 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1144 // sort points by their angular values
1145 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1146 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1147 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1148 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1149 aFirstPtAngle += aPeriod;
1150 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1151 aSecondPtAngle += aPeriod;
1153 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1154 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1155 theFirstPoint = theSecondPoint;
1156 theSecondPoint = aTmpPoint;
1160 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1161 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1163 std::string anAttributeType = theModifiedAttribute->attributeType();
1164 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1165 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1166 theModifiedAttribute);
1167 aModifiedAttribute->setValue(thePoint);
1170 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1171 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1172 theModifiedAttribute->id() <<
1173 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1179 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1180 const AttributePtr& theSourceAttribute)
1182 std::string anAttributeType = theModifiedAttribute->attributeType();
1183 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1184 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1185 theModifiedAttribute);
1186 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1187 theSourceAttribute);
1189 if (aModifiedAttribute.get() && aSourceAttribute.get())
1190 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1192 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1193 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1194 theModifiedAttribute);
1195 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1196 theSourceAttribute);
1198 if (aModifiedAttribute.get() && aSourceAttribute.get())
1199 aModifiedAttribute->setValue(aSourceAttribute->value());
1203 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1204 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1205 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1208 std::cout << "---- createLineFeature ---" << std::endl;
1211 FeaturePtr aFeature;
1212 SketchPlugin_Sketch* aSketch = sketch();
1213 if (!aSketch || !theBaseFeature.get())
1216 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1218 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1219 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1221 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1222 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1224 aFeature->execute(); // to obtain result
1227 std::cout << "---- createLineFeature:end ---" << std::endl;
1233 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1234 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1235 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1237 FeaturePtr aFeature;
1238 SketchPlugin_Sketch* aSketch = sketch();
1239 if (!aSketch || !theBaseFeature.get())
1242 std::string aCenterAttributeId;
1243 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1244 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1245 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1246 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1248 if (aCenterAttributeId.empty())
1252 std::cout << "---- createArcFeature ---" << std::endl;
1255 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1256 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1257 // the "attribute updated"
1258 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1259 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1261 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1262 theBaseFeature->attribute(aCenterAttributeId));
1263 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1264 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1266 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1267 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1269 /// fill referersed state of created arc as it is on the base arc
1270 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1271 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1272 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1274 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1275 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1278 std::cout << "---- createArcFeature:end ---" << std::endl;
1284 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1285 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1287 std::shared_ptr<ModelAPI_Result> aResult;
1289 std::string aFeatureKind = theFeature->getKind();
1290 if (aFeatureKind == SketchPlugin_Line::ID())
1291 aResult = theFeature->firstResult();
1292 else if (aFeatureKind == SketchPlugin_Arc::ID())
1293 aResult = theFeature->lastResult();
1294 else if (aFeatureKind == SketchPlugin_Circle::ID())
1295 aResult = theFeature->lastResult();
1300 //********************************************************************
1301 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1302 const ObjectPtr& theSketch,
1303 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1304 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1306 PointToRefsMap aPointsInfo;
1308 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1309 std::map<std::shared_ptr<GeomAPI_Pnt>,
1310 std::list< AttributePoint2DPtr > > aPointToAttributes;
1311 std::map<std::shared_ptr<GeomAPI_Pnt>,
1312 std::list< ObjectPtr > > aPointToObjects;
1314 std::set<AttributePoint2DPtr > aRefAttributes;
1316 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1317 std::set<ResultPtr> anEdgeShapes;
1319 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1320 if (!anEdgeShapes.empty()) {
1321 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1323 // coincidences to the feature
1324 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1325 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1326 // layed on feature coincidences to divide it on several shapes
1327 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1328 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1329 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1330 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1331 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1332 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1333 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1334 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1336 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1337 aX->dir(), aY, aPointsInfo);
1339 std::list<FeaturePtr> aFeatures;
1340 CompositeFeaturePtr aSketchComposite =
1341 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1342 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1343 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1344 if (aFeature.get() && aFeature->getKind() != SketchPlugin_Projection::ID())
1345 aFeatures.push_back(aFeature);
1347 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1349 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1351 theObjectToPoints[theObject] = aPointsInfo;
1352 theCashedShapes[theObject] = aShapes;