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;
482 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
484 #ifdef DEBUG_TRIM_METHODS
485 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
487 std::string aFilledAttributeName;
489 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
490 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
491 if (aMessage.get()) {
492 ObjectPtr anObject = aMessage->selectedObject();
493 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
495 if (anObject.get() && aPoint.get()) {
496 if (myCashedShapes.find(anObject) == myCashedShapes.end())
497 fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
498 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
499 if (aShapes.size() > 1) {
500 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
501 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
502 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
503 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
504 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
505 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
506 aRefSelectedAttr->setValue(anObject);
507 aRefPreviewAttr->setValue(anObject);
509 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
510 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
511 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
512 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
513 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
514 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
515 aPointSelectedAttr->setValue(aPoint);
516 aPointPreviewAttr->setValue(aPoint);
518 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
520 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
521 #ifdef DEBUG_TRIM_METHODS
522 if (!aSelectedShape.get())
523 std::cout << "Set empty selected object" << std::endl;
525 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
527 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
531 return aFilledAttributeName;
535 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
536 const std::set<AttributePoint2DPtr>& theFurtherCoincidences,
537 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
539 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
540 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
543 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
544 if (!aRefPointAttr.get())
546 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
548 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
549 aLast = theFurtherCoincidences.end();
550 bool aFoundPoint = false;
551 for (; anIt != aLast && !aFoundPoint; anIt++) {
552 AttributePoint2DPtr aPointAttribute = (*anIt);
553 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
554 if (aPoint2d->isEqual(aRefPnt2d)) {
555 // create new coincidence and then remove the old one
556 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
557 SketchPlugin_ConstraintCoincidence::ID(),
558 aRefPointAttr, aPointAttribute);
559 theFeaturesToDelete.insert(aFeature);
565 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
566 const FeaturePtr& theFeature)
568 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
569 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
572 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
577 // get shape of tangent object to the current
578 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
579 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
580 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
581 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
582 aFeature->attribute(aTangentAttr));
583 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
585 // get shape of the feature of the attribute
586 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
587 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
588 PointToRefsMap aPointToAttributeOrObject;
589 std::list<FeaturePtr> aFeatures;
590 aFeatures.push_back(anAttributeFeature);
591 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
592 aPointToAttributeOrObject);
593 if (!aPointToAttributeOrObject.empty())
594 return true; // the attribute feature has a point of intersection, so we do not replace it
596 // get shape of the feature
597 aPointToAttributeOrObject.clear();
599 aFeatures.push_back(theFeature);
600 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
601 aPointToAttributeOrObject);
602 if (!aPointToAttributeOrObject.empty()) {
603 std::set<ResultPtr> anEdgeShapes;
604 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
605 if (!anEdgeShapes.empty()) {
606 ResultPtr aResult = *anEdgeShapes.begin();
608 aRefAttr->setObject(aResult);
609 return true; // the attribute feature has a point of intersection, so we do not replace it
616 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
618 #ifdef DEBUG_TRIM_METHODS
619 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
622 AISObjectPtr anAIS = thePrevious;
624 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
625 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
626 if (aPreviewShape.get())
627 aShapes.push_back(aPreviewShape);
628 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
629 if (aSelectedShape.get())
630 aShapes.push_back(aSelectedShape);
633 return AISObjectPtr();
635 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
636 if (!aBaseShape.get())
637 return AISObjectPtr();
639 if (aBaseShape.get()) {
641 anAIS = AISObjectPtr(new GeomAPI_AISObject);
642 anAIS->createShape(aBaseShape);
644 std::vector<int> aColor;
645 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
646 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
647 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
648 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
649 // width when there is not base object should be extened in several points
650 // in order to see this preview over highlight
651 anAIS->setWidth(aWidth+4);
652 anAIS->setLineStyle(aLineStyle);
655 anAIS = AISObjectPtr();
660 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
661 const std::string& thePointAttributeId)
663 GeomShapePtr aBaseShape;
665 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
666 data()->attribute(theObjectAttributeId));
667 ObjectPtr aBaseObject = anObjectAttr->value();
668 if (!aBaseObject.get())
672 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
673 data()->attribute(thePointAttributeId));
674 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
675 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
676 anAttributePnt2d->y());
678 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
679 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
681 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
682 if (!aShapes.empty()) {
683 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
684 for (; anIt != aLast; anIt++) {
685 GeomShapePtr aShape = *anIt;
686 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
687 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
694 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
695 AttributePoint2DPtr& theStartPointAttr,
696 AttributePoint2DPtr& theEndPointAttr)
698 std::string aFeatureKind = theFeature->getKind();
699 std::string aStartAttributeName, anEndAttributeName;
700 if (aFeatureKind == SketchPlugin_Line::ID()) {
701 aStartAttributeName = SketchPlugin_Line::START_ID();
702 anEndAttributeName = SketchPlugin_Line::END_ID();
704 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
705 aStartAttributeName = SketchPlugin_Arc::START_ID();
706 anEndAttributeName = SketchPlugin_Arc::END_ID();
708 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
709 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
710 theFeature->attribute(aStartAttributeName));
711 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
712 theFeature->attribute(anEndAttributeName));
716 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
717 std::set<FeaturePtr>& theFeaturesToUpdate)
719 std::shared_ptr<ModelAPI_Data> aData = data();
721 // Check the base objects are initialized.
722 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
723 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
724 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
725 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
727 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
728 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
729 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
731 std::set<AttributePtr>::const_iterator aIt;
732 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
733 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
734 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
735 std::string aRefFeatureKind = aRefFeature->getKind();
736 std::string anAttributeId = anAttr->id();
737 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
738 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
739 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
740 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
741 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
742 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
743 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
744 theFeaturesToDelete.insert(aRefFeature);
745 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
746 theFeaturesToUpdate.insert(aRefFeature);
750 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
751 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
752 std::list<AttributePtr>& theRefsToFeature)
756 std::list<AttributePtr> aPointAttributes =
757 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
758 std::set<AttributePtr> aPointAttributesSet;
760 std::list<AttributePtr>::const_iterator aPIt =
761 aPointAttributes.begin(), aPLast = aPointAttributes.end();
762 for (; aPIt != aPLast; aPIt++)
763 aPointAttributesSet.insert(*aPIt);
765 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
766 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
767 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
769 std::set<AttributePtr>::const_iterator aIt;
770 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
771 AttributePtr anAttr = (*aIt);
772 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
773 if (anAttrFeature.get() != this &&
774 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
775 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
776 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
777 AttributePtr anAttrInRef = aRefAttr->attr();
778 if (anAttrInRef.get() &&
779 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
780 if (theRefs.find(anAttrInRef) != theRefs.end())
781 theRefs[anAttrInRef].push_back(aRefAttr);
783 std::list<AttributePtr> anAttrList;
784 anAttrList.push_back(aRefAttr);
785 theRefs[anAttrInRef] = anAttrList;
789 else { /// find attributes referenced to feature itself
790 theRefsToFeature.push_back(anAttr);
796 void SketchPlugin_Trim::updateRefAttConstraints(
797 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
798 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
799 std::set<FeaturePtr>& theFeaturesToDelete)
802 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
805 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
806 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
807 for (; anIt != aLast; anIt++) {
808 AttributePtr anAttribute = anIt->first;
810 /// not found in references
811 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
813 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
814 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
815 aRLast = aRefAttributes.end();
817 AttributePtr aNewAttribute = anIt->second;
818 if (aNewAttribute.get()) {
819 for (; aRefIt != aRLast; aRefIt++) {
820 AttributeRefAttrPtr aRefAttr =
821 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
822 if (aRefAttr.get()) {
823 aRefAttr->setAttr(aNewAttribute);
830 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
831 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
833 /// not found in references
834 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
837 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
838 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
839 aRLast = aRefAttributes.end();
841 std::set<FeaturePtr> aFeaturesToDelete;
842 for (; aRefIt != aRLast; aRefIt++) {
843 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
844 if (aRefAttr.get()) {
845 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
850 // delete constraints
851 if (aFeaturesToDelete.size() > 0) {
852 std::cout << "removeReferencesToAttribute: " << std::endl;
854 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
855 anIt != aFeaturesToDelete.end(); anIt++) {
856 FeaturePtr aFeature = *anIt;
857 std::cout << aFeature->data()->name() << std::endl;
861 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
862 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
865 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
867 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
868 aLast = theFeaturesToUpdate.end();
869 for (; anIt != aLast; anIt++) {
870 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
871 std::string aRefFeatureKind = aRefFeature->getKind();
872 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
873 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
874 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
875 if (aLenghtFeature.get()) {
876 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
877 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
879 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
880 aValueAttr->setValue(aValue);
886 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
887 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
888 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
889 std::set<AttributePoint2DPtr>& thePoints,
890 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
892 FeaturePtr anNewFeature;
894 // Check the base objects are initialized.
895 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
896 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
897 ObjectPtr aBaseObject = aBaseObjectAttr->value();
898 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
901 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
902 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
904 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
905 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
907 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
908 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
909 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
910 aStartShapePoint, aLastShapePoint);
912 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
913 if (aStartShapePoint.get())
914 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
915 aStartShapePoint->y() << "]" << std::endl;
916 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
917 aStartFeaturePoint->y() << "]" << std::endl;
918 if (aLastShapePoint.get())
919 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
920 aLastShapePoint->y() << "]" << std::endl;
921 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
922 aLastFeaturePoint->y() << "]" << std::endl;
925 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
926 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
927 if (isStartPoint || isLastPoint) {
928 // result is one line: changed existing line
929 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
930 : SketchPlugin_Line::END_ID();
931 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
932 if (aStartShapePoint.get() && aLastShapePoint.get())
933 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
935 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
937 // it is important to delete references before the feature modification because
938 // if deletion will be after the feature modification, solver returns the feature back
939 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
940 theBaseRefAttributes);
942 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
943 //theModifiedAttributes.insert(
944 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
946 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
947 (aBaseFeature->attribute(aModifiedAttribute)));
950 // result is two lines: start line point - start shape point,
951 // last shape point - last line point
952 // create second line
953 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
954 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
955 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
957 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
958 theModifiedAttributes.insert(
959 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
960 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
963 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
965 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
966 (aBaseFeature->attribute(aModifiedAttribute)));
968 // Collinear constraint for lines
969 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
970 SketchPlugin_ConstraintCollinear::ID(),
971 getFeatureResult(aBaseFeature),
972 getFeatureResult(anNewFeature));
977 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
978 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
979 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
980 std::set<AttributePoint2DPtr>& thePoints,
981 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
983 FeaturePtr anNewFeature;
984 // Check the base objects are initialized.
985 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
986 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
987 ObjectPtr aBaseObject = aBaseObjectAttr->value();
988 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
991 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
992 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
994 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
995 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
997 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
998 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
999 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1000 aStartShapePoint, aLastShapePoint);
1002 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1003 if (aStartShapePoint.get())
1004 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1005 aStartShapePoint->y() << "]" << std::endl;
1006 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1007 aStartArcPoint->y() << "]" << std::endl;
1008 if (aLastShapePoint.get())
1009 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1010 aLastShapePoint->y() << "]" << std::endl;
1011 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1012 aLastArcPoint->y() << "]" << std::endl;
1015 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1016 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1017 if (isStartPoint || isLastPoint) {
1018 // result is one arc: changed existing arc
1019 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1020 : SketchPlugin_Arc::END_ID();
1021 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1022 if (aStartShapePoint.get() && aLastShapePoint.get())
1023 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1025 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1027 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1028 theBaseRefAttributes);
1030 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1032 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1033 (aBaseFeature->attribute(aModifiedAttribute)));
1036 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1037 // create second arc
1038 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1039 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1040 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1042 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1043 theModifiedAttributes.insert(
1044 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1045 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1048 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1050 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1051 (aBaseFeature->attribute(aModifiedAttribute)));
1053 // equal Radius constraint for arcs
1054 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
1055 SketchPlugin_ConstraintEqual::ID(),
1056 getFeatureResult(aBaseFeature),
1057 getFeatureResult(anNewFeature));
1058 // coincident centers constraint
1059 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1060 SketchPlugin_ConstraintCoincidence::ID(),
1061 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1062 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1065 std::cout << "Created arc on points:" << std::endl;
1066 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1067 aStartShapePoint->y() << "]" << std::endl;
1070 return anNewFeature;
1073 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1074 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1075 std::set<AttributePoint2DPtr>& thePoints,
1076 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1078 // Check the base objects are initialized.
1079 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1080 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1081 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1082 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1085 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1086 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1089 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1090 // arc created by trim of circle is always correct, that means that it is not inversed
1091 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1093 theModifiedAttributes.insert(
1094 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1095 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1097 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1098 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1099 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1100 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1102 return anNewFeature;
1105 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1106 const AttributePoint2DPtr& theEndPointAttr,
1107 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1108 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1110 if (!theFirstPoint.get() || !theLastPoint.get())
1113 // if first point is closer to last point, swap first and last values
1114 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1115 theStartPointAttr->pnt()->distance(theLastPoint)) {
1116 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1117 theFirstPoint = theLastPoint;
1118 theLastPoint = aTmpPoint;
1122 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1123 const AttributePoint2DPtr& theStartPointAttr,
1124 const AttributePoint2DPtr& theEndPointAttr,
1125 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1126 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1128 if (!theFirstPoint.get() || !theSecondPoint.get())
1131 static const double anAngleTol = 1.e-12;
1133 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1134 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1135 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1137 // collect directions to each point
1138 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1139 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1140 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1141 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1142 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1143 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1145 // sort points by their angular values
1146 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1147 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1148 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1149 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1150 aFirstPtAngle += aPeriod;
1151 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1152 aSecondPtAngle += aPeriod;
1154 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1155 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1156 theFirstPoint = theSecondPoint;
1157 theSecondPoint = aTmpPoint;
1161 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1162 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1164 std::string anAttributeType = theModifiedAttribute->attributeType();
1165 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1166 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1167 theModifiedAttribute);
1168 aModifiedAttribute->setValue(thePoint);
1171 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1172 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1173 theModifiedAttribute->id() <<
1174 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1180 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1181 const AttributePtr& theSourceAttribute)
1183 std::string anAttributeType = theModifiedAttribute->attributeType();
1184 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1185 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1186 theModifiedAttribute);
1187 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1188 theSourceAttribute);
1190 if (aModifiedAttribute.get() && aSourceAttribute.get())
1191 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1193 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1194 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1195 theModifiedAttribute);
1196 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1197 theSourceAttribute);
1199 if (aModifiedAttribute.get() && aSourceAttribute.get())
1200 aModifiedAttribute->setValue(aSourceAttribute->value());
1204 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1205 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1206 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1209 std::cout << "---- createLineFeature ---" << std::endl;
1212 FeaturePtr aFeature;
1213 SketchPlugin_Sketch* aSketch = sketch();
1214 if (!aSketch || !theBaseFeature.get())
1217 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1219 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1220 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1222 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1223 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1225 aFeature->execute(); // to obtain result
1228 std::cout << "---- createLineFeature:end ---" << std::endl;
1234 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1235 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1236 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1238 FeaturePtr aFeature;
1239 SketchPlugin_Sketch* aSketch = sketch();
1240 if (!aSketch || !theBaseFeature.get())
1243 std::string aCenterAttributeId;
1244 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1245 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1246 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1247 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1249 if (aCenterAttributeId.empty())
1253 std::cout << "---- createArcFeature ---" << std::endl;
1256 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1257 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1258 // the "attribute updated"
1259 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1260 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1262 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1263 theBaseFeature->attribute(aCenterAttributeId));
1264 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1265 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1267 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1268 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1270 /// fill referersed state of created arc as it is on the base arc
1271 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1272 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1273 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1275 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1276 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1279 std::cout << "---- createArcFeature:end ---" << std::endl;
1285 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1286 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1288 std::shared_ptr<ModelAPI_Result> aResult;
1290 std::string aFeatureKind = theFeature->getKind();
1291 if (aFeatureKind == SketchPlugin_Line::ID())
1292 aResult = theFeature->firstResult();
1293 else if (aFeatureKind == SketchPlugin_Arc::ID())
1294 aResult = theFeature->lastResult();
1295 else if (aFeatureKind == SketchPlugin_Circle::ID())
1296 aResult = theFeature->lastResult();
1301 //********************************************************************
1302 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1303 const ObjectPtr& theSketch,
1304 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1305 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1307 PointToRefsMap aPointsInfo;
1309 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1310 std::map<std::shared_ptr<GeomAPI_Pnt>,
1311 std::list< AttributePoint2DPtr > > aPointToAttributes;
1312 std::map<std::shared_ptr<GeomAPI_Pnt>,
1313 std::list< ObjectPtr > > aPointToObjects;
1315 std::set<AttributePoint2DPtr > aRefAttributes;
1317 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1318 std::set<ResultPtr> anEdgeShapes;
1320 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1321 if (!anEdgeShapes.empty()) {
1322 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1324 // coincidences to the feature
1325 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1326 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1327 // layed on feature coincidences to divide it on several shapes
1328 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1329 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1330 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1331 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1332 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1333 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1334 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1335 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1337 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1338 aX->dir(), aY, aPointsInfo);
1340 std::list<FeaturePtr> aFeatures;
1341 CompositeFeaturePtr aSketchComposite =
1342 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1343 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1344 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1345 if (aFeature.get() && aFeature->getKind() != SketchPlugin_Projection::ID())
1346 aFeatures.push_back(aFeature);
1348 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1350 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1352 theObjectToPoints[theObject] = aPointsInfo;
1353 theCashedShapes[theObject] = aShapes;