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);
242 if (!aStartShapePoint || !aLastShapePoint) {
243 setError("Error: Selected point is not placed on any edge");
247 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
248 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
249 /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
250 std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
251 getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
252 // find references(attributes and features) to the base feature
253 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
254 std::list<AttributePtr> aRefsToFeature;
255 getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
257 std::cout << "---- getRefAttributes ----" << std::endl;
258 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
259 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
260 std::cout << std::endl << "References to attributes of base feature [" <<
261 aBaseRefAttributes.size() << "]" << std::endl;
262 for (; aRefIt != aRefLast; aRefIt++) {
263 AttributePtr aBaseAttr = aRefIt->first;
264 std::list<AttributePtr> aRefAttributes = aRefIt->second;
265 std::string aRefsInfo;
266 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
267 aRefAttrLast = aRefAttributes.end();
268 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
269 if (!aRefsInfo.empty())
270 aRefsInfo.append(",");
271 AttributePtr aRAttr = *aRefAttrIt;
272 aRefsInfo.append(aRAttr->id());
273 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
274 aRefsInfo.append("(" + aRFeature->name() + ") ");
276 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
277 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
278 std::cout << aPointAttr->id().c_str() <<
279 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
281 std::cout << std::endl;
282 std::cout << std::endl << "References to base feature [" <<
283 aRefsToFeature.size() << "]" << std::endl;
284 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
285 aRefAttrLast = aRefsToFeature.end();
286 std::string aRefsInfo;
287 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
288 if (!aRefsInfo.empty())
289 aRefsInfo.append(",");
290 AttributePtr aRAttr = *aRefAttrIt;
291 aRefsInfo.append(aRAttr->id());
292 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
293 aRefsInfo.append("(" + aRFeature->name() + ") ");
295 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
296 std::cout << "---- getRefAttributes:end ----" << std::endl;
299 keepCurrentFeature();
301 std::set<AttributePoint2DPtr> aFurtherCoincidences;
302 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
303 const std::string& aKind = aBaseFeature->getKind();
304 FeaturePtr aReplacingFeature, aNewFeature;
305 if (aKind == SketchPlugin_Circle::ID()) {
306 aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
307 aFurtherCoincidences, aModifiedAttributes);
309 aFeaturesToDelete.insert(aBaseFeature);
310 // as circle is removed, erase it from dependencies(arguments) of this feature
311 // otherwise Trim feature will be removed with the circle before
312 // this operation is finished
313 aBaseObjectAttr->setObject(ResultPtr());
315 else if (aKind == SketchPlugin_Line::ID()) {
316 aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
317 aFurtherCoincidences, aModifiedAttributes);
319 else if (aKind == SketchPlugin_Arc::ID()) {
320 aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
321 aFurtherCoincidences, aModifiedAttributes);
324 restoreCurrentFeature();
326 // constraints to end points of trim feature
327 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
328 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
330 // create coincidence to objects, intersected the base object
331 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
332 for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
333 aLast = aFurtherCoincidences.end();
334 anIt != aLast; anIt++) {
335 AttributePoint2DPtr aPointAttribute = (*anIt);
336 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
339 std::cout << "<compare Points> => " << std::endl
340 << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
341 if (aStartShapePoint2d.get())
342 std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
344 if (aLastShapePoint2d.get())
345 std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
349 std::shared_ptr<GeomAPI_Pnt> aPoint;
350 if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
351 aPoint = aStartShapePoint;
352 else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
353 aPoint = aLastShapePoint;
358 std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
359 for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
362 if (aRefIt->first->isEqual(aPoint)) {
363 anInfo = aRefIt->second;
367 const std::list<ObjectPtr>& anObjects = anInfo.second;
368 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
369 anObjectIt != anObjects.end(); anObjectIt++) {
370 SketchPlugin_Tools::createConstraintAttrObject(sketch(),
371 SketchPlugin_ConstraintCoincidence::ID(),
372 aPointAttribute, *anObjectIt);
376 // move constraints from base feature to replacing feature: ignore coincidences to feature
377 // if attributes of coincidence participated in split
378 ResultPtr aReplacingResult;
379 if (aReplacingFeature.get()) {
380 aReplacingFeature->execute(); // need it to obtain result
381 aReplacingResult = getFeatureResult(aReplacingFeature);
383 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
384 aLast = aRefsToFeature.end();
385 anIt != aLast; anIt++) {
386 AttributePtr anAttribute = *anIt;
388 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences, aFeaturesToDelete))
391 // move tangency constraint to the nearest feature if possible
392 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
395 if (aReplacingResult.get()) {
396 AttributeRefAttrPtr aRefAttr =
397 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
399 aRefAttr->setObject(aReplacingResult);
401 AttributeReferencePtr aReferenceAttr =
402 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
403 if (aReferenceAttr.get())
404 aReferenceAttr->setObject(aReplacingResult);
409 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
411 // Wait all constraints being created, then send update events
412 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
413 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
415 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
417 // delete constraints
419 if (aFeaturesToDelete.size() > 0) {
420 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
422 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
423 anIt != aFeaturesToDelete.end(); anIt++) {
424 FeaturePtr aFeature = *anIt;
425 std::cout << aFeature->data()->name() << std::endl;
429 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
430 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
432 updateFeaturesAfterTrim(aFeaturesToUpdate);
434 // Send events to update the sub-features by the solver.
435 if(isUpdateFlushed) {
436 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
439 if (anIsEqualPreviewAndSelected) {
440 // equal preview and selected objects
441 // nothing to do if the preview and selected objects are different
442 if (aReplacingResult.get()) { // base object was removed
443 aPreviewObject = aReplacingResult;
444 //aMessage->setSelectedObject(aReplacingResult);
445 #ifdef DEBUG_TRIM_METHODS
446 if (!aSelectedShape.get())
447 std::cout << "Set empty selected object" << std::endl;
449 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
453 aPreviewObject = ObjectPtr();
455 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
456 aBaseObject = getFeatureResult(aBaseFeature);
457 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
459 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
461 GeomShapePtr aShape = aBaseResult->shape();
462 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
463 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
464 aPreviewObject = aBaseResult;
466 if (!aPreviewObject.get() && aNewFeature.get()) {
467 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
468 if (aNewFeatureResult.get()) {
469 GeomShapePtr aShape = aNewFeatureResult->shape();
470 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
471 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
472 aPreviewObject = aNewFeatureResult;
477 if (aPreviewObject.get()) {
478 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
479 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
480 ModelAPI_EventReentrantMessage::eventId(), this));
481 aMessage->setSelectedObject(aPreviewObject);
482 Events_Loop::loop()->send(aMessage);
485 std::cout << "SketchPlugin_Trim::done" << std::endl;
490 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
492 #ifdef DEBUG_TRIM_METHODS
493 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
495 std::string aFilledAttributeName;
497 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
498 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
499 if (aMessage.get()) {
500 ObjectPtr anObject = aMessage->selectedObject();
501 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
503 if (anObject.get() && aPoint.get()) {
504 if (myCashedShapes.find(anObject) == myCashedShapes.end())
505 fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
506 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
507 if (aShapes.size() > 1) {
508 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
509 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
510 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
511 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
512 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
513 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
514 aRefSelectedAttr->setValue(anObject);
515 aRefPreviewAttr->setValue(anObject);
517 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
518 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
519 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
520 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
521 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
522 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
523 aPointSelectedAttr->setValue(aPoint);
524 aPointPreviewAttr->setValue(aPoint);
526 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
528 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
529 #ifdef DEBUG_TRIM_METHODS
530 if (!aSelectedShape.get())
531 std::cout << "Set empty selected object" << std::endl;
533 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
535 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
539 return aFilledAttributeName;
543 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
544 const std::set<AttributePoint2DPtr>& theFurtherCoincidences,
545 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
547 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
548 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
551 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
552 if (!aRefPointAttr.get())
554 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
556 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
557 aLast = theFurtherCoincidences.end();
558 bool aFoundPoint = false;
559 for (; anIt != aLast && !aFoundPoint; anIt++) {
560 AttributePoint2DPtr aPointAttribute = (*anIt);
561 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
562 if (aPoint2d->isEqual(aRefPnt2d)) {
563 // create new coincidence and then remove the old one
564 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
565 SketchPlugin_ConstraintCoincidence::ID(),
566 aRefPointAttr, aPointAttribute);
567 theFeaturesToDelete.insert(aFeature);
573 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
574 const FeaturePtr& theFeature)
576 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
577 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
580 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
585 // get shape of tangent object to the current
586 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
587 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
588 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
589 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
590 aFeature->attribute(aTangentAttr));
591 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
593 // get shape of the feature of the attribute
594 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
595 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
596 PointToRefsMap aPointToAttributeOrObject;
597 std::list<FeaturePtr> aFeatures;
598 aFeatures.push_back(anAttributeFeature);
599 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
600 aPointToAttributeOrObject);
601 if (!aPointToAttributeOrObject.empty())
602 return true; // the attribute feature has a point of intersection, so we do not replace it
604 // get shape of the feature
605 aPointToAttributeOrObject.clear();
607 aFeatures.push_back(theFeature);
608 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
609 aPointToAttributeOrObject);
610 if (!aPointToAttributeOrObject.empty()) {
611 std::set<ResultPtr> anEdgeShapes;
612 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
613 if (!anEdgeShapes.empty()) {
614 ResultPtr aResult = *anEdgeShapes.begin();
616 aRefAttr->setObject(aResult);
617 return true; // the attribute feature has a point of intersection, so we do not replace it
624 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
626 #ifdef DEBUG_TRIM_METHODS
627 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
630 AISObjectPtr anAIS = thePrevious;
632 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
633 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
634 if (aPreviewShape.get())
635 aShapes.push_back(aPreviewShape);
636 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
637 if (aSelectedShape.get())
638 aShapes.push_back(aSelectedShape);
641 return AISObjectPtr();
643 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
644 if (!aBaseShape.get())
645 return AISObjectPtr();
647 if (aBaseShape.get()) {
649 anAIS = AISObjectPtr(new GeomAPI_AISObject);
650 anAIS->createShape(aBaseShape);
652 std::vector<int> aColor;
653 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
654 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
655 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
656 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
657 // width when there is not base object should be extened in several points
658 // in order to see this preview over highlight
659 anAIS->setWidth(aWidth+4);
660 anAIS->setLineStyle(aLineStyle);
663 anAIS = AISObjectPtr();
668 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
669 const std::string& thePointAttributeId)
671 GeomShapePtr aBaseShape;
673 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
674 data()->attribute(theObjectAttributeId));
675 ObjectPtr aBaseObject = anObjectAttr->value();
676 if (!aBaseObject.get())
680 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
681 data()->attribute(thePointAttributeId));
682 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
683 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
684 anAttributePnt2d->y());
686 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
687 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
689 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
690 if (!aShapes.empty()) {
691 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
692 for (; anIt != aLast; anIt++) {
693 GeomShapePtr aShape = *anIt;
694 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
695 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
702 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
703 AttributePoint2DPtr& theStartPointAttr,
704 AttributePoint2DPtr& theEndPointAttr)
706 std::string aFeatureKind = theFeature->getKind();
707 std::string aStartAttributeName, anEndAttributeName;
708 if (aFeatureKind == SketchPlugin_Line::ID()) {
709 aStartAttributeName = SketchPlugin_Line::START_ID();
710 anEndAttributeName = SketchPlugin_Line::END_ID();
712 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
713 aStartAttributeName = SketchPlugin_Arc::START_ID();
714 anEndAttributeName = SketchPlugin_Arc::END_ID();
716 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
717 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
718 theFeature->attribute(aStartAttributeName));
719 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
720 theFeature->attribute(anEndAttributeName));
724 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
725 std::set<FeaturePtr>& theFeaturesToUpdate)
727 std::shared_ptr<ModelAPI_Data> aData = data();
729 // Check the base objects are initialized.
730 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
731 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
732 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
733 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
735 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
736 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
737 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
739 std::set<AttributePtr>::const_iterator aIt;
740 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
741 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
742 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
743 std::string aRefFeatureKind = aRefFeature->getKind();
744 std::string anAttributeId = anAttr->id();
745 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
746 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
747 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
748 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
749 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
750 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
751 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
752 theFeaturesToDelete.insert(aRefFeature);
753 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
754 theFeaturesToUpdate.insert(aRefFeature);
758 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
759 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
760 std::list<AttributePtr>& theRefsToFeature)
764 std::list<AttributePtr> aPointAttributes =
765 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
766 std::set<AttributePtr> aPointAttributesSet;
768 std::list<AttributePtr>::const_iterator aPIt =
769 aPointAttributes.begin(), aPLast = aPointAttributes.end();
770 for (; aPIt != aPLast; aPIt++)
771 aPointAttributesSet.insert(*aPIt);
773 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
774 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
775 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
777 std::set<AttributePtr>::const_iterator aIt;
778 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
779 AttributePtr anAttr = (*aIt);
780 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
781 if (anAttrFeature.get() != this &&
782 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
783 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
784 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
785 AttributePtr anAttrInRef = aRefAttr->attr();
786 if (anAttrInRef.get() &&
787 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
788 if (theRefs.find(anAttrInRef) != theRefs.end())
789 theRefs[anAttrInRef].push_back(aRefAttr);
791 std::list<AttributePtr> anAttrList;
792 anAttrList.push_back(aRefAttr);
793 theRefs[anAttrInRef] = anAttrList;
797 else { /// find attributes referenced to feature itself
798 theRefsToFeature.push_back(anAttr);
804 void SketchPlugin_Trim::updateRefAttConstraints(
805 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
806 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
807 std::set<FeaturePtr>& theFeaturesToDelete)
810 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
813 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
814 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
815 for (; anIt != aLast; anIt++) {
816 AttributePtr anAttribute = anIt->first;
818 /// not found in references
819 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
821 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
822 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
823 aRLast = aRefAttributes.end();
825 AttributePtr aNewAttribute = anIt->second;
826 if (aNewAttribute.get()) {
827 for (; aRefIt != aRLast; aRefIt++) {
828 AttributeRefAttrPtr aRefAttr =
829 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
830 if (aRefAttr.get()) {
831 aRefAttr->setAttr(aNewAttribute);
838 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
839 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
841 /// not found in references
842 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
845 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
846 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
847 aRLast = aRefAttributes.end();
849 std::set<FeaturePtr> aFeaturesToDelete;
850 for (; aRefIt != aRLast; aRefIt++) {
851 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
852 if (aRefAttr.get()) {
853 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
858 // delete constraints
859 if (aFeaturesToDelete.size() > 0) {
860 std::cout << "removeReferencesToAttribute: " << std::endl;
862 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
863 anIt != aFeaturesToDelete.end(); anIt++) {
864 FeaturePtr aFeature = *anIt;
865 std::cout << aFeature->data()->name() << std::endl;
869 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
870 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
873 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
875 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
876 aLast = theFeaturesToUpdate.end();
877 for (; anIt != aLast; anIt++) {
878 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
879 std::string aRefFeatureKind = aRefFeature->getKind();
880 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
881 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
882 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
883 if (aLenghtFeature.get()) {
884 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
885 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
887 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
888 aValueAttr->setValue(aValue);
894 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
895 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
896 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
897 std::set<AttributePoint2DPtr>& thePoints,
898 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
900 FeaturePtr anNewFeature;
902 // Check the base objects are initialized.
903 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
904 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
905 ObjectPtr aBaseObject = aBaseObjectAttr->value();
906 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
909 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
910 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
912 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
913 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
915 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
916 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
917 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
918 aStartShapePoint, aLastShapePoint);
920 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
921 if (aStartShapePoint.get())
922 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
923 aStartShapePoint->y() << "]" << std::endl;
924 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
925 aStartFeaturePoint->y() << "]" << std::endl;
926 if (aLastShapePoint.get())
927 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
928 aLastShapePoint->y() << "]" << std::endl;
929 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
930 aLastFeaturePoint->y() << "]" << std::endl;
933 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
934 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
935 if (isStartPoint || isLastPoint) {
936 // result is one line: changed existing line
937 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
938 : SketchPlugin_Line::END_ID();
939 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
940 if (aStartShapePoint.get() && aLastShapePoint.get())
941 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
943 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
945 // it is important to delete references before the feature modification because
946 // if deletion will be after the feature modification, solver returns the feature back
947 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
948 theBaseRefAttributes);
950 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
951 //theModifiedAttributes.insert(
952 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
954 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
955 (aBaseFeature->attribute(aModifiedAttribute)));
958 // result is two lines: start line point - start shape point,
959 // last shape point - last line point
960 // create second line
961 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
962 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
963 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
965 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
966 theModifiedAttributes.insert(
967 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
968 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
971 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
973 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
974 (aBaseFeature->attribute(aModifiedAttribute)));
976 // Collinear constraint for lines
977 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
978 SketchPlugin_ConstraintCollinear::ID(),
979 getFeatureResult(aBaseFeature),
980 getFeatureResult(anNewFeature));
985 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
986 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
987 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
988 std::set<AttributePoint2DPtr>& thePoints,
989 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
991 FeaturePtr anNewFeature;
992 // Check the base objects are initialized.
993 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
994 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
995 ObjectPtr aBaseObject = aBaseObjectAttr->value();
996 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
999 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1000 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1002 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
1003 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
1005 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
1006 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
1007 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1008 aStartShapePoint, aLastShapePoint);
1010 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1011 if (aStartShapePoint.get())
1012 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1013 aStartShapePoint->y() << "]" << std::endl;
1014 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1015 aStartArcPoint->y() << "]" << std::endl;
1016 if (aLastShapePoint.get())
1017 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1018 aLastShapePoint->y() << "]" << std::endl;
1019 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1020 aLastArcPoint->y() << "]" << std::endl;
1023 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1024 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1025 if (isStartPoint || isLastPoint) {
1026 // result is one arc: changed existing arc
1027 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1028 : SketchPlugin_Arc::END_ID();
1029 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1030 if (aStartShapePoint.get() && aLastShapePoint.get())
1031 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1033 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1035 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1036 theBaseRefAttributes);
1038 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1040 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1041 (aBaseFeature->attribute(aModifiedAttribute)));
1044 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1045 // create second arc
1046 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1047 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1048 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1050 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1051 theModifiedAttributes.insert(
1052 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1053 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1056 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1058 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1059 (aBaseFeature->attribute(aModifiedAttribute)));
1061 // equal Radius constraint for arcs
1062 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
1063 SketchPlugin_ConstraintEqual::ID(),
1064 getFeatureResult(aBaseFeature),
1065 getFeatureResult(anNewFeature));
1066 // coincident centers constraint
1067 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1068 SketchPlugin_ConstraintCoincidence::ID(),
1069 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1070 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1073 std::cout << "Created arc on points:" << std::endl;
1074 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1075 aStartShapePoint->y() << "]" << std::endl;
1078 return anNewFeature;
1081 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1082 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1083 std::set<AttributePoint2DPtr>& thePoints,
1084 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1086 // Check the base objects are initialized.
1087 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1088 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1089 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1090 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1093 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1094 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1097 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1098 // arc created by trim of circle is always correct, that means that it is not inversed
1099 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1101 theModifiedAttributes.insert(
1102 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1103 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1105 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1106 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1107 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1108 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1110 return anNewFeature;
1113 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1114 const AttributePoint2DPtr& theEndPointAttr,
1115 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1116 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1118 if (!theFirstPoint.get() || !theLastPoint.get())
1121 // if first point is closer to last point, swap first and last values
1122 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1123 theStartPointAttr->pnt()->distance(theLastPoint)) {
1124 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1125 theFirstPoint = theLastPoint;
1126 theLastPoint = aTmpPoint;
1130 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1131 const AttributePoint2DPtr& theStartPointAttr,
1132 const AttributePoint2DPtr& theEndPointAttr,
1133 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1134 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1136 if (!theFirstPoint.get() || !theSecondPoint.get())
1139 static const double anAngleTol = 1.e-12;
1141 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1142 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1143 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1145 // collect directions to each point
1146 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1147 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1148 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1149 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1150 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1151 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1153 // sort points by their angular values
1154 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1155 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1156 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1157 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1158 aFirstPtAngle += aPeriod;
1159 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1160 aSecondPtAngle += aPeriod;
1162 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1163 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1164 theFirstPoint = theSecondPoint;
1165 theSecondPoint = aTmpPoint;
1169 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1170 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1172 std::string anAttributeType = theModifiedAttribute->attributeType();
1173 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1174 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1175 theModifiedAttribute);
1176 aModifiedAttribute->setValue(thePoint);
1179 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1180 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1181 theModifiedAttribute->id() <<
1182 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1188 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1189 const AttributePtr& theSourceAttribute)
1191 std::string anAttributeType = theModifiedAttribute->attributeType();
1192 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1193 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1194 theModifiedAttribute);
1195 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1196 theSourceAttribute);
1198 if (aModifiedAttribute.get() && aSourceAttribute.get())
1199 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1201 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1202 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1203 theModifiedAttribute);
1204 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1205 theSourceAttribute);
1207 if (aModifiedAttribute.get() && aSourceAttribute.get())
1208 aModifiedAttribute->setValue(aSourceAttribute->value());
1212 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1213 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1214 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1217 std::cout << "---- createLineFeature ---" << std::endl;
1220 FeaturePtr aFeature;
1221 SketchPlugin_Sketch* aSketch = sketch();
1222 if (!aSketch || !theBaseFeature.get())
1225 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1227 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1228 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1230 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1231 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1233 aFeature->execute(); // to obtain result
1236 std::cout << "---- createLineFeature:end ---" << std::endl;
1242 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1243 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1244 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1246 FeaturePtr aFeature;
1247 SketchPlugin_Sketch* aSketch = sketch();
1248 if (!aSketch || !theBaseFeature.get())
1251 std::string aCenterAttributeId;
1252 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1253 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1254 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1255 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1257 if (aCenterAttributeId.empty())
1261 std::cout << "---- createArcFeature ---" << std::endl;
1264 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1265 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1266 // the "attribute updated"
1267 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1268 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1270 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1271 theBaseFeature->attribute(aCenterAttributeId));
1272 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1273 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1275 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1276 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1278 /// fill referersed state of created arc as it is on the base arc
1279 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1280 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1281 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1283 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1284 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1287 std::cout << "---- createArcFeature:end ---" << std::endl;
1293 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1294 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1296 std::shared_ptr<ModelAPI_Result> aResult;
1298 std::string aFeatureKind = theFeature->getKind();
1299 if (aFeatureKind == SketchPlugin_Line::ID())
1300 aResult = theFeature->firstResult();
1301 else if (aFeatureKind == SketchPlugin_Arc::ID())
1302 aResult = theFeature->lastResult();
1303 else if (aFeatureKind == SketchPlugin_Circle::ID())
1304 aResult = theFeature->lastResult();
1309 //********************************************************************
1310 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1311 const ObjectPtr& theSketch,
1312 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1313 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1315 PointToRefsMap aPointsInfo;
1317 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1318 std::map<std::shared_ptr<GeomAPI_Pnt>,
1319 std::list< AttributePoint2DPtr > > aPointToAttributes;
1320 std::map<std::shared_ptr<GeomAPI_Pnt>,
1321 std::list< ObjectPtr > > aPointToObjects;
1323 std::set<AttributePoint2DPtr > aRefAttributes;
1325 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1326 std::set<ResultPtr> anEdgeShapes;
1328 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1329 if (!anEdgeShapes.empty()) {
1330 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1332 // coincidences to the feature
1333 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1334 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1335 // layed on feature coincidences to divide it on several shapes
1336 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1337 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1338 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1339 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1340 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1341 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1342 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1343 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1345 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1346 aX->dir(), aY, aPointsInfo);
1348 std::list<FeaturePtr> aFeatures;
1349 CompositeFeaturePtr aSketchComposite =
1350 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1351 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1352 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1353 if (aFeature.get() && aFeature->getKind() != SketchPlugin_Projection::ID())
1354 aFeatures.push_back(aFeature);
1356 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1358 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1360 theObjectToPoints[theObject] = aPointsInfo;
1361 theCashedShapes[theObject] = aShapes;