1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_Trim.h"
23 #include <GeomAPI_Dir2d.h>
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Pnt2d.h>
26 #include <GeomAPI_XY.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <GeomAlgoAPI_ShapeTools.h>
29 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <ModelAPI_AttributeReference.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_AttributeRefAttr.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_AttributeBoolean.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_AttributeDouble.h>
41 #include <ModelGeomAlgo_Shape.h>
43 #include <SketchPlugin_Arc.h>
44 #include <SketchPlugin_ConstraintMiddle.h>
45 #include <SketchPlugin_Circle.h>
46 #include <SketchPlugin_ConstraintCoincidence.h>
47 #include <SketchPlugin_ConstraintEqual.h>
48 #include <SketchPlugin_ConstraintTangent.h>
49 #include <SketchPlugin_ConstraintLength.h>
50 #include <SketchPlugin_ConstraintMirror.h>
51 #include <SketchPlugin_ConstraintCollinear.h>
52 #include <SketchPlugin_Line.h>
53 #include <SketchPlugin_MultiRotation.h>
54 #include <SketchPlugin_MultiTranslation.h>
55 #include <SketchPlugin_Point.h>
56 #include <SketchPlugin_Projection.h>
57 #include <SketchPlugin_Tools.h>
59 #include <ModelAPI_EventReentrantMessage.h>
61 #include <ModelAPI_Events.h>
62 #include <SketchPlugin_Line.h>
63 #include <SketchPlugin_Arc.h>
64 #include <SketchPlugin_Circle.h>
66 #include <ModelGeomAlgo_Point2D.h>
67 #include <Events_Loop.h>
75 #ifdef DEBUG_TRIM_METHODS
79 static const double PI = 3.141592653589793238463;
81 static const std::string OPERATION_HIGHLIGHT_COLOR() { return "128, 0, 0"; }
82 static const std::string OPERATION_REMOVE_FEATURE_COLOR() { return "255, 174, 201"; }
84 SketchPlugin_Trim::SketchPlugin_Trim()
88 void SketchPlugin_Trim::initAttributes()
90 data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
91 data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
93 data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
94 data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
96 data()->attribute(PREVIEW_POINT())->setIsArgument(false);
97 data()->attribute(SELECTED_POINT())->setIsArgument(false);
98 data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
100 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
101 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
104 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
105 const std::string& thePointAttributeId,
106 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
107 std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
109 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
110 data()->attribute(theObjectAttributeId));
111 ObjectPtr aBaseObject = aBaseObjectAttr->value();
113 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114 data()->attribute(thePointAttributeId));
115 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
116 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
117 anAttributePnt2d->y());
119 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
120 fillObjectShapes(aBaseObject, sketch()->data()->owner(), 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 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
171 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
172 for (PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
173 aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
174 if (aPointIt->first->isEqual(thePoint)) {
175 const std::pair<std::list<AttributePoint2DPtr >,
176 std::list<ObjectPtr > >& anInfo = aPointIt->second;
177 const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
178 if (!anAttributes.empty()) {
179 aPoint = anAttributes.front()->pnt();
183 aPoint = sketch()->to2D(thePoint);
189 // returns an end of the shape to define direction of split if feature's attribute
191 aPoint = sketch()->to2D(thePoint);
196 void SketchPlugin_Trim::execute()
198 #ifdef DEBUG_TRIM_METHODS
199 std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
202 SketchPlugin_Sketch* aSketch = sketch();
204 setError("Error: Sketch object is empty.");
208 // Check the base objects are initialized.
209 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
210 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
211 if(!aBaseObjectAttr->isInitialized()) {
212 setError("Error: Base object is not initialized.");
215 ObjectPtr aBaseObject = aBaseObjectAttr->value();
216 if (!aBaseObject.get()) {
217 setError("Error: Base object is not initialized.");
220 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
222 /// Remove reference of this feature to feature used in preview, it is not necessary anymore
223 /// as trim will be removed after execute
224 AttributeReferencePtr aPreviewObjectAttr =
225 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
226 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
228 ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
229 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
230 data()->attribute(PREVIEW_POINT()));
231 std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
232 // nullify pointer of preview attribute
233 aPreviewObjectAttr->setValue(ResultPtr());
235 bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
238 std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
240 std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
242 findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
244 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
245 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
246 /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
247 std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
248 getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
249 // find references(attributes and features) to the base feature
250 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
251 std::list<AttributePtr> aRefsToFeature;
252 getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
254 std::cout << "---- getRefAttributes ----" << std::endl;
255 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
256 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
257 std::cout << std::endl << "References to attributes of base feature [" <<
258 aBaseRefAttributes.size() << "]" << std::endl;
259 for (; aRefIt != aRefLast; aRefIt++) {
260 AttributePtr aBaseAttr = aRefIt->first;
261 std::list<AttributePtr> aRefAttributes = aRefIt->second;
262 std::string aRefsInfo;
263 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
264 aRefAttrLast = aRefAttributes.end();
265 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
266 if (!aRefsInfo.empty())
267 aRefsInfo.append(",");
268 AttributePtr aRAttr = *aRefAttrIt;
269 aRefsInfo.append(aRAttr->id());
270 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
271 aRefsInfo.append("(" + aRFeature->name() + ") ");
273 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
274 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
275 std::cout << aPointAttr->id().c_str() <<
276 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
278 std::cout << std::endl;
279 std::cout << std::endl << "References to base feature [" <<
280 aRefsToFeature.size() << "]" << std::endl;
281 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
282 aRefAttrLast = aRefsToFeature.end();
283 std::string aRefsInfo;
284 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
285 if (!aRefsInfo.empty())
286 aRefsInfo.append(",");
287 AttributePtr aRAttr = *aRefAttrIt;
288 aRefsInfo.append(aRAttr->id());
289 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
290 aRefsInfo.append("(" + aRFeature->name() + ") ");
292 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
293 std::cout << "---- getRefAttributes:end ----" << std::endl;
295 std::set<AttributePoint2DPtr> aFurtherCoincidences;
296 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
297 const std::string& aKind = aBaseFeature->getKind();
298 FeaturePtr aReplacingFeature, aNewFeature;
299 if (aKind == SketchPlugin_Circle::ID()) {
300 aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
301 aFurtherCoincidences, aModifiedAttributes);
303 aFeaturesToDelete.insert(aBaseFeature);
304 // as circle is removed, erase it from dependencies(arguments) of this feature
305 // otherwise Trim feature will be removed with the circle before
306 // this operation is finished
307 aBaseObjectAttr->setObject(ResultPtr());
309 else if (aKind == SketchPlugin_Line::ID()) {
310 aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
311 aFurtherCoincidences, aModifiedAttributes);
313 else if (aKind == SketchPlugin_Arc::ID()) {
314 aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
315 aFurtherCoincidences, aModifiedAttributes);
318 // constraints to end points of trim feature
319 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
320 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
322 // create coincidence to objects, intersected the base object
323 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
324 for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
325 aLast = aFurtherCoincidences.end();
326 anIt != aLast; anIt++) {
327 AttributePoint2DPtr aPointAttribute = (*anIt);
328 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
331 std::cout << "<compare Points> => " << std::endl
332 << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
333 if (aStartShapePoint2d.get())
334 std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
336 if (aLastShapePoint2d.get())
337 std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
341 std::shared_ptr<GeomAPI_Pnt> aPoint;
342 if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
343 aPoint = aStartShapePoint;
344 else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
345 aPoint = aLastShapePoint;
350 std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
351 for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
354 if (aRefIt->first->isEqual(aPoint)) {
355 anInfo = aRefIt->second;
359 const std::list<ObjectPtr>& anObjects = anInfo.second;
360 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
361 anObjectIt != anObjects.end(); anObjectIt++) {
362 SketchPlugin_Tools::createConstraintAttrObject(sketch(),
363 SketchPlugin_ConstraintCoincidence::ID(),
364 aPointAttribute, *anObjectIt);
368 // move constraints from base feature to replacing feature: ignore coincidences to feature
369 // if attributes of coincidence participated in split
370 ResultPtr aReplacingResult;
371 if (aReplacingFeature.get()) {
372 aReplacingFeature->execute(); // need it to obtain result
373 aReplacingResult = getFeatureResult(aReplacingFeature);
375 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
376 aLast = aRefsToFeature.end();
377 anIt != aLast; anIt++) {
378 AttributePtr anAttribute = *anIt;
380 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences, aFeaturesToDelete))
383 // move tangency constraint to the nearest feature if possible
384 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
387 if (aReplacingResult.get()) {
388 AttributeRefAttrPtr aRefAttr =
389 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
391 aRefAttr->setObject(aReplacingResult);
393 AttributeReferencePtr aReferenceAttr =
394 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
395 if (aReferenceAttr.get())
396 aReferenceAttr->setObject(aReplacingResult);
401 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
403 // Wait all constraints being created, then send update events
404 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
405 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
407 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
409 // delete constraints
411 if (aFeaturesToDelete.size() > 0) {
412 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
414 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
415 anIt != aFeaturesToDelete.end(); anIt++) {
416 FeaturePtr aFeature = *anIt;
417 std::cout << aFeature->data()->name() << std::endl;
421 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
422 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
424 updateFeaturesAfterTrim(aFeaturesToUpdate);
426 // Send events to update the sub-features by the solver.
427 if(isUpdateFlushed) {
428 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
431 if (anIsEqualPreviewAndSelected) {
432 // equal preview and selected objects
433 // nothing to do if the preview and selected objects are different
434 if (aReplacingResult.get()) { // base object was removed
435 aPreviewObject = aReplacingResult;
436 //aMessage->setSelectedObject(aReplacingResult);
437 #ifdef DEBUG_TRIM_METHODS
438 if (!aSelectedShape.get())
439 std::cout << "Set empty selected object" << std::endl;
441 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
445 aPreviewObject = ObjectPtr();
447 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
448 aBaseObject = getFeatureResult(aBaseFeature);
449 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
451 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
453 GeomShapePtr aShape = aBaseResult->shape();
454 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
455 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
456 aPreviewObject = aBaseResult;
458 if (!aPreviewObject.get() && aNewFeature.get()) {
459 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
460 if (aNewFeatureResult.get()) {
461 GeomShapePtr aShape = aNewFeatureResult->shape();
462 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
463 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
464 aPreviewObject = aNewFeatureResult;
469 if (aPreviewObject.get()) {
470 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
471 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
472 ModelAPI_EventReentrantMessage::eventId(), this));
473 aMessage->setSelectedObject(aPreviewObject);
474 Events_Loop::loop()->send(aMessage);
477 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;
533 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
534 const std::set<AttributePoint2DPtr>& theFurtherCoincidences,
535 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
537 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
538 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
541 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
542 if (!aRefPointAttr.get())
544 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
546 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
547 aLast = theFurtherCoincidences.end();
548 bool aFoundPoint = false;
549 for (; anIt != aLast && !aFoundPoint; anIt++) {
550 AttributePoint2DPtr aPointAttribute = (*anIt);
551 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
552 if (aPoint2d->isEqual(aRefPnt2d)) {
553 // create new coincidence and then remove the old one
554 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
555 SketchPlugin_ConstraintCoincidence::ID(),
556 aRefPointAttr, aPointAttribute);
557 theFeaturesToDelete.insert(aFeature);
563 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
564 const FeaturePtr& theFeature)
566 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
567 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
570 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
575 // get shape of tangent object to the current
576 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
577 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
578 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
579 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
580 aFeature->attribute(aTangentAttr));
581 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
583 // get shape of the feature of the attribute
584 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
585 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
586 PointToRefsMap aPointToAttributeOrObject;
587 std::list<FeaturePtr> aFeatures;
588 aFeatures.push_back(anAttributeFeature);
589 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
590 aPointToAttributeOrObject);
591 if (!aPointToAttributeOrObject.empty())
592 return true; // the attribute feature has a point of intersection, so we do not replace it
594 // get shape of the feature
595 aPointToAttributeOrObject.clear();
597 aFeatures.push_back(theFeature);
598 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
599 aPointToAttributeOrObject);
600 if (!aPointToAttributeOrObject.empty()) {
601 std::set<ResultPtr> anEdgeShapes;
602 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
603 if (!anEdgeShapes.empty()) {
604 ResultPtr aResult = *anEdgeShapes.begin();
606 aRefAttr->setObject(aResult);
607 return true; // the attribute feature has a point of intersection, so we do not replace it
614 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
616 #ifdef DEBUG_TRIM_METHODS
617 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
620 AISObjectPtr anAIS = thePrevious;
622 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
623 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
624 if (aPreviewShape.get())
625 aShapes.push_back(aPreviewShape);
626 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
627 if (aSelectedShape.get())
628 aShapes.push_back(aSelectedShape);
631 return AISObjectPtr();
633 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
634 if (!aBaseShape.get())
635 return AISObjectPtr();
637 if (aBaseShape.get()) {
639 anAIS = AISObjectPtr(new GeomAPI_AISObject);
640 anAIS->createShape(aBaseShape);
642 std::vector<int> aColor;
643 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
644 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
645 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
646 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
647 // width when there is not base object should be extened in several points
648 // in order to see this preview over highlight
649 anAIS->setWidth(aWidth+4);
650 anAIS->setLineStyle(aLineStyle);
653 anAIS = AISObjectPtr();
658 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
659 const std::string& thePointAttributeId)
661 GeomShapePtr aBaseShape;
663 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
664 data()->attribute(theObjectAttributeId));
665 ObjectPtr aBaseObject = anObjectAttr->value();
666 if (!aBaseObject.get())
670 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
671 data()->attribute(thePointAttributeId));
672 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
673 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
674 anAttributePnt2d->y());
676 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
677 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
679 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
680 if (!aShapes.empty()) {
681 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
682 for (; anIt != aLast; anIt++) {
683 GeomShapePtr aShape = *anIt;
684 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
685 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
692 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
693 AttributePoint2DPtr& theStartPointAttr,
694 AttributePoint2DPtr& theEndPointAttr)
696 std::string aFeatureKind = theFeature->getKind();
697 std::string aStartAttributeName, anEndAttributeName;
698 if (aFeatureKind == SketchPlugin_Line::ID()) {
699 aStartAttributeName = SketchPlugin_Line::START_ID();
700 anEndAttributeName = SketchPlugin_Line::END_ID();
702 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
703 aStartAttributeName = SketchPlugin_Arc::START_ID();
704 anEndAttributeName = SketchPlugin_Arc::END_ID();
706 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
707 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
708 theFeature->attribute(aStartAttributeName));
709 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
710 theFeature->attribute(anEndAttributeName));
714 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
715 std::set<FeaturePtr>& theFeaturesToUpdate)
717 std::shared_ptr<ModelAPI_Data> aData = data();
719 // Check the base objects are initialized.
720 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
721 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
722 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
723 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
725 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
726 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
727 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
729 std::set<AttributePtr>::const_iterator aIt;
730 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
731 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
732 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
733 std::string aRefFeatureKind = aRefFeature->getKind();
734 std::string anAttributeId = anAttr->id();
735 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
736 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
737 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
738 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
739 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
740 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
741 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
742 theFeaturesToDelete.insert(aRefFeature);
743 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
744 theFeaturesToUpdate.insert(aRefFeature);
748 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
749 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
750 std::list<AttributePtr>& theRefsToFeature)
754 std::list<AttributePtr> aPointAttributes =
755 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
756 std::set<AttributePtr> aPointAttributesSet;
758 std::list<AttributePtr>::const_iterator aPIt =
759 aPointAttributes.begin(), aPLast = aPointAttributes.end();
760 for (; aPIt != aPLast; aPIt++)
761 aPointAttributesSet.insert(*aPIt);
763 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
764 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
765 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
767 std::set<AttributePtr>::const_iterator aIt;
768 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
769 AttributePtr anAttr = (*aIt);
770 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
771 if (anAttrFeature.get() != this &&
772 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
773 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
774 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
775 AttributePtr anAttrInRef = aRefAttr->attr();
776 if (anAttrInRef.get() &&
777 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
778 if (theRefs.find(anAttrInRef) != theRefs.end())
779 theRefs[anAttrInRef].push_back(aRefAttr);
781 std::list<AttributePtr> anAttrList;
782 anAttrList.push_back(aRefAttr);
783 theRefs[anAttrInRef] = anAttrList;
787 else { /// find attributes referenced to feature itself
788 theRefsToFeature.push_back(anAttr);
794 void SketchPlugin_Trim::updateRefAttConstraints(
795 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
796 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
797 std::set<FeaturePtr>& theFeaturesToDelete)
800 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
803 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
804 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
805 for (; anIt != aLast; anIt++) {
806 AttributePtr anAttribute = anIt->first;
808 /// not found in references
809 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
811 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
812 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
813 aRLast = aRefAttributes.end();
815 AttributePtr aNewAttribute = anIt->second;
816 if (aNewAttribute.get()) {
817 for (; aRefIt != aRLast; aRefIt++) {
818 AttributeRefAttrPtr aRefAttr =
819 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
820 if (aRefAttr.get()) {
821 aRefAttr->setAttr(aNewAttribute);
828 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
829 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
831 /// not found in references
832 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
835 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
836 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
837 aRLast = aRefAttributes.end();
839 std::set<FeaturePtr> aFeaturesToDelete;
840 for (; aRefIt != aRLast; aRefIt++) {
841 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
842 if (aRefAttr.get()) {
843 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
848 // delete constraints
849 if (aFeaturesToDelete.size() > 0) {
850 std::cout << "removeReferencesToAttribute: " << std::endl;
852 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
853 anIt != aFeaturesToDelete.end(); anIt++) {
854 FeaturePtr aFeature = *anIt;
855 std::cout << aFeature->data()->name() << std::endl;
859 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
860 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
863 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
865 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
866 aLast = theFeaturesToUpdate.end();
867 for (; anIt != aLast; anIt++) {
868 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
869 std::string aRefFeatureKind = aRefFeature->getKind();
870 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
871 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
872 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
873 if (aLenghtFeature.get()) {
874 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
875 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
877 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
878 aValueAttr->setValue(aValue);
884 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
885 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
886 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
887 std::set<AttributePoint2DPtr>& thePoints,
888 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
890 FeaturePtr anNewFeature;
892 // Check the base objects are initialized.
893 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
894 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
895 ObjectPtr aBaseObject = aBaseObjectAttr->value();
896 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
899 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
900 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
902 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
903 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
905 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
906 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
907 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
908 aStartShapePoint, aLastShapePoint);
910 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
911 if (aStartShapePoint.get())
912 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
913 aStartShapePoint->y() << "]" << std::endl;
914 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
915 aStartFeaturePoint->y() << "]" << std::endl;
916 if (aLastShapePoint.get())
917 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
918 aLastShapePoint->y() << "]" << std::endl;
919 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
920 aLastFeaturePoint->y() << "]" << std::endl;
923 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
924 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
925 if (isStartPoint || isLastPoint) {
926 // result is one line: changed existing line
927 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
928 : SketchPlugin_Line::END_ID();
929 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
930 if (aStartShapePoint.get() && aLastShapePoint.get())
931 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
933 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
935 // it is important to delete references before the feature modification because
936 // if deletion will be after the feature modification, solver returns the feature back
937 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
938 theBaseRefAttributes);
940 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
941 //theModifiedAttributes.insert(
942 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
944 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
945 (aBaseFeature->attribute(aModifiedAttribute)));
948 // result is two lines: start line point - start shape point,
949 // last shape point - last line point
950 // create second line
951 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
952 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
953 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
955 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
956 theModifiedAttributes.insert(
957 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
958 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
961 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
963 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
964 (aBaseFeature->attribute(aModifiedAttribute)));
966 // Collinear constraint for lines
967 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
968 SketchPlugin_ConstraintCollinear::ID(),
969 getFeatureResult(aBaseFeature),
970 getFeatureResult(anNewFeature));
975 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
976 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
977 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
978 std::set<AttributePoint2DPtr>& thePoints,
979 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
981 FeaturePtr anNewFeature;
982 // Check the base objects are initialized.
983 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
984 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
985 ObjectPtr aBaseObject = aBaseObjectAttr->value();
986 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
989 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
990 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
992 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
993 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
995 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
996 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
997 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
998 aStartShapePoint, aLastShapePoint);
1000 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1001 if (aStartShapePoint.get())
1002 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1003 aStartShapePoint->y() << "]" << std::endl;
1004 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1005 aStartArcPoint->y() << "]" << std::endl;
1006 if (aLastShapePoint.get())
1007 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1008 aLastShapePoint->y() << "]" << std::endl;
1009 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1010 aLastArcPoint->y() << "]" << std::endl;
1013 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1014 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1015 if (isStartPoint || isLastPoint) {
1016 // result is one arc: changed existing arc
1017 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1018 : SketchPlugin_Arc::END_ID();
1019 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1020 if (aStartShapePoint.get() && aLastShapePoint.get())
1021 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1023 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1025 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1026 theBaseRefAttributes);
1028 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1030 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1031 (aBaseFeature->attribute(aModifiedAttribute)));
1034 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1035 // create second arc
1036 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1037 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1038 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1040 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1041 theModifiedAttributes.insert(
1042 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1043 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1046 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1048 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1049 (aBaseFeature->attribute(aModifiedAttribute)));
1051 // equal Radius constraint for arcs
1052 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
1053 SketchPlugin_ConstraintEqual::ID(),
1054 getFeatureResult(aBaseFeature),
1055 getFeatureResult(anNewFeature));
1056 // coincident centers constraint
1057 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1058 SketchPlugin_ConstraintCoincidence::ID(),
1059 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1060 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1063 std::cout << "Created arc on points:" << std::endl;
1064 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1065 aStartShapePoint->y() << "]" << std::endl;
1068 return anNewFeature;
1071 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1072 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1073 std::set<AttributePoint2DPtr>& thePoints,
1074 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1076 // Check the base objects are initialized.
1077 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1078 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1079 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1080 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1083 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1084 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1087 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1088 // arc created by trim of circle is always correct, that means that it is not inversed
1089 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1091 theModifiedAttributes.insert(
1092 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1093 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1095 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1096 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1097 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1098 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1100 return anNewFeature;
1103 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1104 const AttributePoint2DPtr& theEndPointAttr,
1105 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1106 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1108 if (!theFirstPoint.get() || !theLastPoint.get())
1111 // if first point is closer to last point, swap first and last values
1112 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1113 theStartPointAttr->pnt()->distance(theLastPoint)) {
1114 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1115 theFirstPoint = theLastPoint;
1116 theLastPoint = aTmpPoint;
1120 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1121 const AttributePoint2DPtr& theStartPointAttr,
1122 const AttributePoint2DPtr& theEndPointAttr,
1123 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1124 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1126 if (!theFirstPoint.get() || !theSecondPoint.get())
1129 static const double anAngleTol = 1.e-12;
1131 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1132 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1133 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1135 // collect directions to each point
1136 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1137 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1138 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1139 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1140 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1141 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1143 // sort points by their angular values
1144 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1145 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1146 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1147 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1148 aFirstPtAngle += aPeriod;
1149 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1150 aSecondPtAngle += aPeriod;
1152 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1153 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1154 theFirstPoint = theSecondPoint;
1155 theSecondPoint = aTmpPoint;
1159 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1160 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1162 std::string anAttributeType = theModifiedAttribute->attributeType();
1163 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1164 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1165 theModifiedAttribute);
1166 aModifiedAttribute->setValue(thePoint);
1169 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1170 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1171 theModifiedAttribute->id() <<
1172 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1178 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1179 const AttributePtr& theSourceAttribute)
1181 std::string anAttributeType = theModifiedAttribute->attributeType();
1182 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1183 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1184 theModifiedAttribute);
1185 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1186 theSourceAttribute);
1188 if (aModifiedAttribute.get() && aSourceAttribute.get())
1189 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1191 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1192 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1193 theModifiedAttribute);
1194 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1195 theSourceAttribute);
1197 if (aModifiedAttribute.get() && aSourceAttribute.get())
1198 aModifiedAttribute->setValue(aSourceAttribute->value());
1200 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1201 AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1202 theModifiedAttribute);
1203 AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1204 theSourceAttribute);
1205 if (!aSourceRefAttr.get())
1206 aRefAttributeToFill->setAttr(theSourceAttribute);
1208 if (aSourceRefAttr->isObject())
1209 aRefAttributeToFill->setObject(aSourceRefAttr->object());
1211 aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1216 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1217 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1218 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1221 std::cout << "---- createLineFeature ---" << std::endl;
1224 FeaturePtr aFeature;
1225 SketchPlugin_Sketch* aSketch = sketch();
1226 if (!aSketch || !theBaseFeature.get())
1229 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1231 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1232 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1234 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1235 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1237 aFeature->execute(); // to obtain result
1240 std::cout << "---- createLineFeature:end ---" << std::endl;
1246 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1247 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1248 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1250 FeaturePtr aFeature;
1251 SketchPlugin_Sketch* aSketch = sketch();
1252 if (!aSketch || !theBaseFeature.get())
1255 std::string aCenterAttributeId;
1256 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1257 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1258 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1259 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1261 if (aCenterAttributeId.empty())
1265 std::cout << "---- createArcFeature ---" << std::endl;
1268 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1269 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1270 // the "attribute updated"
1271 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1272 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1274 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1275 theBaseFeature->attribute(aCenterAttributeId));
1276 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1277 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1279 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1280 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1282 /// fill referersed state of created arc as it is on the base arc
1283 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1284 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1285 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1287 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1288 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1291 std::cout << "---- createArcFeature:end ---" << std::endl;
1297 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1298 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1300 std::shared_ptr<ModelAPI_Result> aResult;
1302 std::string aFeatureKind = theFeature->getKind();
1303 if (aFeatureKind == SketchPlugin_Line::ID())
1304 aResult = theFeature->firstResult();
1305 else if (aFeatureKind == SketchPlugin_Arc::ID())
1306 aResult = theFeature->lastResult();
1307 else if (aFeatureKind == SketchPlugin_Circle::ID())
1308 aResult = theFeature->lastResult();
1313 //********************************************************************
1314 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1315 const ObjectPtr& theSketch,
1316 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1317 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1319 PointToRefsMap aPointsInfo;
1321 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1322 std::map<std::shared_ptr<GeomAPI_Pnt>,
1323 std::list< AttributePoint2DPtr > > aPointToAttributes;
1324 std::map<std::shared_ptr<GeomAPI_Pnt>,
1325 std::list< ObjectPtr > > aPointToObjects;
1327 std::set<AttributePoint2DPtr > aRefAttributes;
1329 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1330 std::set<ResultPtr> anEdgeShapes;
1332 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1333 if (!anEdgeShapes.empty()) {
1334 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1336 // coincidences to the feature
1337 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1338 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1339 // layed on feature coincidences to divide it on several shapes
1340 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1341 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1342 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1343 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1344 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1345 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1346 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1347 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1349 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1350 aX->dir(), aY, aPointsInfo);
1352 std::list<FeaturePtr> aFeatures;
1353 CompositeFeaturePtr aSketchComposite =
1354 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1355 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1356 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1357 if (aFeature.get() && aFeature->getKind() != SketchPlugin_Projection::ID())
1358 aFeatures.push_back(aFeature);
1360 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1362 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1364 theObjectToPoints[theObject] = aPointsInfo;
1365 theCashedShapes[theObject] = aShapes;