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_ConstraintParallel.h>
49 #include <SketchPlugin_ConstraintTangent.h>
50 #include <SketchPlugin_ConstraintLength.h>
51 #include <SketchPlugin_ConstraintMirror.h>
52 #include <SketchPlugin_ConstraintCollinear.h>
53 #include <SketchPlugin_Line.h>
54 #include <SketchPlugin_MultiRotation.h>
55 #include <SketchPlugin_MultiTranslation.h>
56 #include <SketchPlugin_Point.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>
70 //#define DEBUG_TRIM_METHODS
77 #ifdef DEBUG_TRIM_METHODS
81 static const double PI = 3.141592653589793238463;
83 static const std::string OPERATION_HIGHLIGHT_COLOR() { return "128, 0, 0"; }
84 static const std::string OPERATION_REMOVE_FEATURE_COLOR() { return "255, 174, 201"; }
86 SketchPlugin_Trim::SketchPlugin_Trim()
90 void SketchPlugin_Trim::initAttributes()
92 data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
93 data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
95 data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
96 data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
98 data()->attribute(PREVIEW_POINT())->setIsArgument(false);
99 data()->attribute(SELECTED_POINT())->setIsArgument(false);
100 data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
102 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
103 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
106 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
107 const std::string& thePointAttributeId,
108 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
109 std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
111 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
112 data()->attribute(theObjectAttributeId));
113 ObjectPtr aBaseObject = aBaseObjectAttr->value();
115 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
116 data()->attribute(thePointAttributeId));
117 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
118 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
119 anAttributePnt2d->y());
121 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
122 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
124 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
125 if (!aShapes.empty()) {
126 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
127 for (; anIt != aLast; anIt++) {
128 GeomShapePtr aBaseShape = *anIt;
129 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
130 if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, anAttributePnt, aProjectedPoint)) {
132 if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
133 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
134 //GeomAPI_Shape::Orientation anOrientation = anEdge->orientation();
135 //if (anOrientation == GeomAPI_Shape::REVERSED) {
136 aStartPoint = anEdge->lastPoint();
137 aLastPoint = anEdge->firstPoint();
140 //aStartPoint = anEdge->firstPoint();
141 //aLastPoint = anEdge->lastPoint();
148 std::cout << "<findShapePoints> => "
149 << std::endl << "Attribute point: "
150 << anAttributePnt->x() << ", " << anAttributePnt->y() << ", " << anAttributePnt->z() << "]"
151 << std::endl << "Start Point: ["
152 << aStartPoint->x() << ", " << aStartPoint->y() << ", " << aStartPoint->z() << "]"
153 << std::endl << "Last Point: ["
154 << aLastPoint->x() << ", " << aLastPoint->y() << ", " << aLastPoint->z() << "]"
159 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Trim::convertPoint(
160 const std::shared_ptr<GeomAPI_Pnt>& thePoint)
162 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
166 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
167 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
168 ObjectPtr aBaseObject = aBaseObjectAttr->value();
169 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
170 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
173 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
174 for (PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
175 aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
176 if (aPointIt->first->isEqual(thePoint)) {
177 const std::pair<std::list<AttributePoint2DPtr >,
178 std::list<ObjectPtr > >& anInfo = aPointIt->second;
179 const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
180 if (!anAttributes.empty()) {
181 aPoint = anAttributes.front()->pnt();
185 aPoint = sketch()->to2D(thePoint);
191 // returns an end of the shape to define direction of split if feature's attribute
193 aPoint = sketch()->to2D(thePoint);
198 void SketchPlugin_Trim::execute()
200 #ifdef DEBUG_TRIM_METHODS
201 std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
204 SketchPlugin_Sketch* aSketch = sketch();
206 setError("Error: Sketch object is empty.");
210 // Check the base objects are initialized.
211 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
212 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
213 if(!aBaseObjectAttr->isInitialized()) {
214 setError("Error: Base object is not initialized.");
217 ObjectPtr aBaseObject = aBaseObjectAttr->value();
218 if (!aBaseObject.get()) {
219 setError("Error: Base object is not initialized.");
222 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
224 /// Remove reference of this feature to feature used in preview, it is not necessary anymore
225 /// as trim will be removed after execute
226 AttributeReferencePtr aPreviewObjectAttr =
227 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
228 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
230 ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
231 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
232 data()->attribute(PREVIEW_POINT()));
233 std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
234 // nullify pointer of preview attribute
235 aPreviewObjectAttr->setValue(ResultPtr());
237 bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
240 std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
242 std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
244 findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
246 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
247 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
248 /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
249 std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
250 getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
251 // find references(attributes and features) to the base feature
252 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
253 std::list<AttributePtr> aRefsToFeature;
254 getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
256 std::cout << "---- getRefAttributes ----" << std::endl;
257 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
258 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
259 std::cout << std::endl << "References to attributes of base feature [" <<
260 aBaseRefAttributes.size() << "]" << std::endl;
261 for (; aRefIt != aRefLast; aRefIt++) {
262 AttributePtr aBaseAttr = aRefIt->first;
263 std::list<AttributePtr> aRefAttributes = aRefIt->second;
264 std::string aRefsInfo;
265 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
266 aRefAttrLast = aRefAttributes.end();
267 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
268 if (!aRefsInfo.empty())
269 aRefsInfo.append(",");
270 AttributePtr aRAttr = *aRefAttrIt;
271 aRefsInfo.append(aRAttr->id());
272 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
273 aRefsInfo.append("(" + aRFeature->name() + ") ");
275 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
276 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
277 std::cout << aPointAttr->id().c_str() <<
278 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
280 std::cout << std::endl;
281 std::cout << std::endl << "References to base feature [" <<
282 aRefsToFeature.size() << "]" << std::endl;
283 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
284 aRefAttrLast = aRefsToFeature.end();
285 std::string aRefsInfo;
286 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
287 if (!aRefsInfo.empty())
288 aRefsInfo.append(",");
289 AttributePtr aRAttr = *aRefAttrIt;
290 aRefsInfo.append(aRAttr->id());
291 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
292 aRefsInfo.append("(" + aRFeature->name() + ") ");
294 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
295 std::cout << "---- getRefAttributes:end ----" << std::endl;
297 std::set<AttributePoint2DPtr> aFurtherCoincidences;
298 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
299 const std::string& aKind = aBaseFeature->getKind();
300 FeaturePtr aReplacingFeature, aNewFeature;
301 if (aKind == SketchPlugin_Circle::ID()) {
302 aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
303 aFurtherCoincidences, aModifiedAttributes);
305 aFeaturesToDelete.insert(aBaseFeature);
306 // as circle is removed, erase it from dependencies(arguments) of this feature
307 // otherwise Trim feature will be removed with the circle before
308 // this operation is finished
309 aBaseObjectAttr->setObject(ResultPtr());
311 else if (aKind == SketchPlugin_Line::ID()) {
312 aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
313 aFurtherCoincidences, aModifiedAttributes);
315 else if (aKind == SketchPlugin_Arc::ID()) {
316 aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
317 aFurtherCoincidences, aModifiedAttributes);
320 // constraints to end points of trim feature
321 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
322 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
324 // create coincidence to objects, intersected the base object
325 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
326 for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
327 aLast = aFurtherCoincidences.end();
328 anIt != aLast; anIt++) {
329 AttributePoint2DPtr aPointAttribute = (*anIt);
330 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
333 std::cout << "<compare Points> => " << std::endl
334 << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
335 if (aStartShapePoint2d.get())
336 std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
338 if (aLastShapePoint2d.get())
339 std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
343 std::shared_ptr<GeomAPI_Pnt> aPoint;
344 if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
345 aPoint = aStartShapePoint;
346 else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
347 aPoint = aLastShapePoint;
352 std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
353 for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
356 if (aRefIt->first->isEqual(aPoint)) {
357 anInfo = aRefIt->second;
361 const std::list<ObjectPtr>& anObjects = anInfo.second;
362 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
363 anObjectIt != anObjects.end(); anObjectIt++) {
364 createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute,
369 // move constraints from base feature to replacing feature: ignore coincidences to feature
370 // if attributes of coincidence participated in split
371 ResultPtr aReplacingResult;
372 if (aReplacingFeature.get()) {
373 aReplacingFeature->execute(); // need it to obtain result
374 aReplacingResult = getFeatureResult(aReplacingFeature);
376 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
377 aLast = aRefsToFeature.end();
378 anIt != aLast; anIt++) {
379 AttributePtr anAttribute = *anIt;
381 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences))
384 // move tangency constraint to the nearest feature if possible
385 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
388 if (aReplacingResult.get()) {
389 AttributeRefAttrPtr aRefAttr =
390 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
392 aRefAttr->setObject(aReplacingResult);
394 AttributeReferencePtr aReferenceAttr =
395 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
396 if (aReferenceAttr.get())
397 aReferenceAttr->setObject(aReplacingResult);
402 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
404 // Wait all constraints being created, then send update events
405 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
406 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
408 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
410 // delete constraints
412 if (aFeaturesToDelete.size() > 0) {
413 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
415 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
416 anIt != aFeaturesToDelete.end(); anIt++) {
417 FeaturePtr aFeature = *anIt;
418 std::cout << aFeature->data()->name() << std::endl;
422 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
423 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
425 updateFeaturesAfterTrim(aFeaturesToUpdate);
427 // Send events to update the sub-features by the solver.
428 if(isUpdateFlushed) {
429 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
432 if (anIsEqualPreviewAndSelected) {
433 // equal preview and selected objects
434 // nothing to do if the preview and selected objects are different
435 if (aReplacingResult.get()) { // base object was removed
436 aPreviewObject = aReplacingResult;
437 //aMessage->setSelectedObject(aReplacingResult);
438 #ifdef DEBUG_TRIM_METHODS
439 if (!aSelectedShape.get())
440 std::cout << "Set empty selected object" << std::endl;
442 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
446 aPreviewObject = ObjectPtr();
448 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
449 aBaseObject = getFeatureResult(aBaseFeature);
450 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
452 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
454 GeomShapePtr aShape = aBaseResult->shape();
455 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
456 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
457 aPreviewObject = aBaseResult;
459 if (!aPreviewObject.get() && aNewFeature.get()) {
460 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
461 if (aNewFeatureResult.get()) {
462 GeomShapePtr aShape = aNewFeatureResult->shape();
463 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
464 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
465 aPreviewObject = aNewFeatureResult;
470 if (aPreviewObject.get()) {
471 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
472 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
473 ModelAPI_EventReentrantMessage::eventId(), this));
474 aMessage->setSelectedObject(aPreviewObject);
475 Events_Loop::loop()->send(aMessage);
478 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;
534 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
535 const std::set<AttributePoint2DPtr>& theFurtherCoincidences)
537 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
538 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
541 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
542 if (!aRefPointAttr.get())
544 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
546 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
547 aLast = theFurtherCoincidences.end();
548 bool aFoundPoint = false;
549 for (; anIt != aLast && !aFoundPoint; anIt++) {
550 AttributePoint2DPtr aPointAttribute = (*anIt);
551 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
552 if (aPoint2d->isEqual(aRefPnt2d)) {
553 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
555 if (aRefAttr.get()) {
556 aRefAttr->setAttr(aPointAttribute);
564 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
565 const FeaturePtr& theFeature)
567 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
568 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
571 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
576 // get shape of tangent object to the current
577 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
578 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
579 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
580 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
581 aFeature->attribute(aTangentAttr));
582 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
584 // get shape of the feature of the attribute
585 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
586 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
587 PointToRefsMap aPointToAttributeOrObject;
588 std::list<FeaturePtr> aFeatures;
589 aFeatures.push_back(anAttributeFeature);
590 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
591 aPointToAttributeOrObject);
592 if (!aPointToAttributeOrObject.empty())
593 return true; // the attribute feature has a point of intersection, so we do not replace it
595 // get shape of the feature
596 aPointToAttributeOrObject.clear();
598 aFeatures.push_back(theFeature);
599 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
600 aPointToAttributeOrObject);
601 if (!aPointToAttributeOrObject.empty()) {
602 std::set<ResultPtr> anEdgeShapes;
603 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
604 if (!anEdgeShapes.empty()) {
605 ResultPtr aResult = *anEdgeShapes.begin();
607 aRefAttr->setObject(aResult);
608 return true; // the attribute feature has a point of intersection, so we do not replace it
615 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
617 #ifdef DEBUG_TRIM_METHODS
618 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
621 AISObjectPtr anAIS = thePrevious;
623 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
624 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
625 if (aPreviewShape.get())
626 aShapes.push_back(aPreviewShape);
627 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
628 if (aSelectedShape.get())
629 aShapes.push_back(aSelectedShape);
632 return AISObjectPtr();
634 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
635 if (!aBaseShape.get())
636 return AISObjectPtr();
638 if (aBaseShape.get()) {
640 anAIS = AISObjectPtr(new GeomAPI_AISObject);
641 anAIS->createShape(aBaseShape);
643 std::vector<int> aColor;
644 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
645 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
646 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
647 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
648 // width when there is not base object should be extened in several points
649 // in order to see this preview over highlight
650 anAIS->setWidth(aWidth+4);
651 anAIS->setLineStyle(aLineStyle);
654 anAIS = AISObjectPtr();
659 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
660 const std::string& thePointAttributeId)
662 GeomShapePtr aBaseShape;
664 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
665 data()->attribute(theObjectAttributeId));
666 ObjectPtr aBaseObject = anObjectAttr->value();
667 if (!aBaseObject.get())
671 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
672 data()->attribute(thePointAttributeId));
673 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
674 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
675 anAttributePnt2d->y());
677 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
678 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
680 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
681 if (!aShapes.empty()) {
682 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
683 for (; anIt != aLast; anIt++) {
684 GeomShapePtr aShape = *anIt;
685 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
686 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
693 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
694 AttributePoint2DPtr& theStartPointAttr,
695 AttributePoint2DPtr& theEndPointAttr)
697 std::string aFeatureKind = theFeature->getKind();
698 std::string aStartAttributeName, anEndAttributeName;
699 if (aFeatureKind == SketchPlugin_Line::ID()) {
700 aStartAttributeName = SketchPlugin_Line::START_ID();
701 anEndAttributeName = SketchPlugin_Line::END_ID();
703 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
704 aStartAttributeName = SketchPlugin_Arc::START_ID();
705 anEndAttributeName = SketchPlugin_Arc::END_ID();
707 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
708 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
709 theFeature->attribute(aStartAttributeName));
710 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
711 theFeature->attribute(anEndAttributeName));
715 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
716 std::set<FeaturePtr>& theFeaturesToUpdate)
718 std::shared_ptr<ModelAPI_Data> aData = data();
720 // Check the base objects are initialized.
721 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
722 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
723 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
724 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
726 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
727 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
728 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
730 std::set<AttributePtr>::const_iterator aIt;
731 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
732 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
733 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
734 std::string aRefFeatureKind = aRefFeature->getKind();
735 std::string anAttributeId = anAttr->id();
736 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
737 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
738 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
739 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
740 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
741 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
742 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
743 theFeaturesToDelete.insert(aRefFeature);
744 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
745 theFeaturesToUpdate.insert(aRefFeature);
749 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
750 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
751 std::list<AttributePtr>& theRefsToFeature)
755 std::list<AttributePtr> aPointAttributes =
756 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
757 std::set<AttributePtr> aPointAttributesSet;
759 std::list<AttributePtr>::const_iterator aPIt =
760 aPointAttributes.begin(), aPLast = aPointAttributes.end();
761 for (; aPIt != aPLast; aPIt++)
762 aPointAttributesSet.insert(*aPIt);
764 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
765 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
766 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
768 std::set<AttributePtr>::const_iterator aIt;
769 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
770 AttributePtr anAttr = (*aIt);
771 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
772 if (anAttrFeature.get() != this &&
773 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
774 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
775 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
776 AttributePtr anAttrInRef = aRefAttr->attr();
777 if (anAttrInRef.get() &&
778 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
779 if (theRefs.find(anAttrInRef) != theRefs.end())
780 theRefs[anAttrInRef].push_back(aRefAttr);
782 std::list<AttributePtr> anAttrList;
783 anAttrList.push_back(aRefAttr);
784 theRefs[anAttrInRef] = anAttrList;
788 else { /// find attributes referenced to feature itself
789 theRefsToFeature.push_back(anAttr);
795 void SketchPlugin_Trim::updateRefAttConstraints(
796 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
797 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
798 std::set<FeaturePtr>& theFeaturesToDelete)
801 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
804 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
805 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
806 for (; anIt != aLast; anIt++) {
807 AttributePtr anAttribute = anIt->first;
809 /// not found in references
810 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
812 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
813 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
814 aRLast = aRefAttributes.end();
816 AttributePtr aNewAttribute = anIt->second;
817 if (aNewAttribute.get()) {
818 for (; aRefIt != aRLast; aRefIt++) {
819 AttributeRefAttrPtr aRefAttr =
820 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
821 if (aRefAttr.get()) {
822 aRefAttr->setAttr(aNewAttribute);
829 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
830 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
832 /// not found in references
833 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
836 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
837 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
838 aRLast = aRefAttributes.end();
840 std::set<FeaturePtr> aFeaturesToDelete;
841 for (; aRefIt != aRLast; aRefIt++) {
842 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
843 if (aRefAttr.get()) {
844 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
849 // delete constraints
850 if (aFeaturesToDelete.size() > 0) {
851 std::cout << "removeReferencesToAttribute: " << std::endl;
853 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
854 anIt != aFeaturesToDelete.end(); anIt++) {
855 FeaturePtr aFeature = *anIt;
856 std::cout << aFeature->data()->name() << std::endl;
860 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
861 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
864 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
866 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
867 aLast = theFeaturesToUpdate.end();
868 for (; anIt != aLast; anIt++) {
869 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
870 std::string aRefFeatureKind = aRefFeature->getKind();
871 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
872 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
873 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
874 if (aLenghtFeature.get()) {
875 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
876 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
878 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
879 aValueAttr->setValue(aValue);
885 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
886 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
887 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
888 std::set<AttributePoint2DPtr>& thePoints,
889 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
891 FeaturePtr anNewFeature;
893 // Check the base objects are initialized.
894 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
895 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
896 ObjectPtr aBaseObject = aBaseObjectAttr->value();
897 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
900 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
901 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
903 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
904 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
906 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
907 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
908 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
909 aStartShapePoint, aLastShapePoint);
911 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
912 if (aStartShapePoint.get())
913 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
914 aStartShapePoint->y() << "]" << std::endl;
915 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
916 aStartFeaturePoint->y() << "]" << std::endl;
917 if (aLastShapePoint.get())
918 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
919 aLastShapePoint->y() << "]" << std::endl;
920 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
921 aLastFeaturePoint->y() << "]" << std::endl;
924 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
925 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
926 if (isStartPoint || isLastPoint) {
927 // result is one line: changed existing line
928 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
929 : SketchPlugin_Line::END_ID();
930 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
931 if (aStartShapePoint.get() && aLastShapePoint.get())
932 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
934 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
936 // it is important to delete references before the feature modification because
937 // if deletion will be after the feature modification, solver returns the feature back
938 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
939 theBaseRefAttributes);
941 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
942 //theModifiedAttributes.insert(
943 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
945 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
946 (aBaseFeature->attribute(aModifiedAttribute)));
949 // result is two lines: start line point - start shape point,
950 // last shape point - last line point
951 // create second line
952 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
953 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
954 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
956 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
957 theModifiedAttributes.insert(
958 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
959 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
962 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
964 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
965 (aBaseFeature->attribute(aModifiedAttribute)));
967 // Collinear constraint for lines
968 createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(),
969 getFeatureResult(aBaseFeature),
970 getFeatureResult(anNewFeature));
976 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
977 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
978 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
979 std::set<AttributePoint2DPtr>& thePoints,
980 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
982 FeaturePtr anNewFeature;
983 // Check the base objects are initialized.
984 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
985 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
986 ObjectPtr aBaseObject = aBaseObjectAttr->value();
987 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
990 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
991 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
993 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
994 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
996 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
997 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
998 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
999 aStartShapePoint, aLastShapePoint);
1001 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1002 if (aStartShapePoint.get())
1003 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1004 aStartShapePoint->y() << "]" << std::endl;
1005 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1006 aStartArcPoint->y() << "]" << std::endl;
1007 if (aLastShapePoint.get())
1008 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1009 aLastShapePoint->y() << "]" << std::endl;
1010 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1011 aLastArcPoint->y() << "]" << std::endl;
1014 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1015 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1016 if (isStartPoint || isLastPoint) {
1017 // result is one arc: changed existing arc
1018 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1019 : SketchPlugin_Arc::END_ID();
1020 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1021 if (aStartShapePoint.get() && aLastShapePoint.get())
1022 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1024 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1026 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1027 theBaseRefAttributes);
1029 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1031 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1032 (aBaseFeature->attribute(aModifiedAttribute)));
1035 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1036 // create second arc
1037 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1038 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1039 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1041 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1042 theModifiedAttributes.insert(
1043 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1044 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1047 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1049 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1050 (aBaseFeature->attribute(aModifiedAttribute)));
1052 // equal Radius constraint for arcs
1053 createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1054 getFeatureResult(aBaseFeature),
1055 getFeatureResult(anNewFeature));
1056 // coincident centers constraint
1057 createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1058 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1059 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1062 std::cout << "Created arc on points:" << std::endl;
1063 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1064 aStartShapePoint->y() << "]" << std::endl;
1067 return anNewFeature;
1070 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1071 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1072 std::set<AttributePoint2DPtr>& thePoints,
1073 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1075 // Check the base objects are initialized.
1076 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1077 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1078 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1079 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1082 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1083 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1086 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1087 // arc created by trim of circle is always correct, that means that it is not inversed
1088 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1090 theModifiedAttributes.insert(
1091 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1092 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1094 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1095 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1096 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1097 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1099 return anNewFeature;
1102 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1103 const AttributePoint2DPtr& theEndPointAttr,
1104 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1105 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1107 if (!theFirstPoint.get() || !theLastPoint.get())
1110 // if first point is closer to last point, swap first and last values
1111 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1112 theStartPointAttr->pnt()->distance(theLastPoint)) {
1113 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1114 theFirstPoint = theLastPoint;
1115 theLastPoint = aTmpPoint;
1119 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1120 const AttributePoint2DPtr& theStartPointAttr,
1121 const AttributePoint2DPtr& theEndPointAttr,
1122 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1123 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1125 if (!theFirstPoint.get() || !theSecondPoint.get())
1128 static const double anAngleTol = 1.e-12;
1130 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1131 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1132 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1134 // collect directions to each point
1135 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1136 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1137 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1138 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1139 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1140 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1142 // sort points by their angular values
1143 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1144 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1145 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1146 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1147 aFirstPtAngle += aPeriod;
1148 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1149 aSecondPtAngle += aPeriod;
1151 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1152 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1153 theFirstPoint = theSecondPoint;
1154 theSecondPoint = aTmpPoint;
1158 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1159 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1161 std::string anAttributeType = theModifiedAttribute->attributeType();
1162 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1163 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1164 theModifiedAttribute);
1165 aModifiedAttribute->setValue(thePoint);
1168 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1169 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1170 theModifiedAttribute->id() <<
1171 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1177 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1178 const AttributePtr& theSourceAttribute)
1180 std::string anAttributeType = theModifiedAttribute->attributeType();
1181 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1182 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1183 theModifiedAttribute);
1184 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1185 theSourceAttribute);
1187 if (aModifiedAttribute.get() && aSourceAttribute.get())
1188 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1190 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1191 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1192 theModifiedAttribute);
1193 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1194 theSourceAttribute);
1196 if (aModifiedAttribute.get() && aSourceAttribute.get())
1197 aModifiedAttribute->setValue(aSourceAttribute->value());
1199 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1200 AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1201 theModifiedAttribute);
1202 AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1203 theSourceAttribute);
1204 if (!aSourceRefAttr.get())
1205 aRefAttributeToFill->setAttr(theSourceAttribute);
1207 if (aSourceRefAttr->isObject())
1208 aRefAttributeToFill->setObject(aSourceRefAttr->object());
1210 aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1215 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1216 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1217 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1220 std::cout << "---- createLineFeature ---" << std::endl;
1223 FeaturePtr aFeature;
1224 SketchPlugin_Sketch* aSketch = sketch();
1225 if (!aSketch || !theBaseFeature.get())
1228 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1230 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1231 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1233 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1234 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1236 aFeature->execute(); // to obtain result
1239 std::cout << "---- createLineFeature:end ---" << std::endl;
1245 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1246 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1247 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1249 FeaturePtr aFeature;
1250 SketchPlugin_Sketch* aSketch = sketch();
1251 if (!aSketch || !theBaseFeature.get())
1254 std::string aCenterAttributeId;
1255 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1256 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1257 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1258 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1260 if (aCenterAttributeId.empty())
1264 std::cout << "---- createArcFeature ---" << std::endl;
1267 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1268 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1269 // the "attribute updated"
1270 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1271 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1273 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1274 theBaseFeature->attribute(aCenterAttributeId));
1275 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1276 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1278 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1279 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1281 /// fill referersed state of created arc as it is on the base arc
1282 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1283 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1284 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1286 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1287 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1290 std::cout << "---- createArcFeature:end ---" << std::endl;
1296 FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId,
1297 const AttributePtr& theFirstAttribute,
1298 const AttributePtr& theSecondAttribute)
1300 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1301 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1302 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1303 aRefAttr->setAttr(theFirstAttribute);
1305 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1306 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1307 aRefAttr->setAttr(theSecondAttribute);
1310 std::cout << "<createConstraint to attribute> :"
1311 << "first attribute - " << theFirstAttribute->id()
1312 << "second attribute - " << theSecondAttribute->id()
1319 FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId,
1320 const AttributePtr& theFirstAttribute,
1321 const ObjectPtr& theSecondObject)
1323 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1324 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1325 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1326 aRefAttr->setAttr(theFirstAttribute);
1328 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1329 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1330 aRefAttr->setObject(theSecondObject);
1333 std::cout << "<createConstraint to attribute> :"
1334 << "first attribute - " << theFirstAttribute->id()
1335 << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
1342 FeaturePtr SketchPlugin_Trim::createConstraintForObjects(
1343 const std::string& theConstraintId,
1344 const ObjectPtr& theFirstObject,
1345 const ObjectPtr& theSecondObject)
1347 FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1348 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1349 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1350 aRefAttr->setObject(theFirstObject);
1352 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1353 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1354 aRefAttr->setObject(theSecondObject);
1359 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1360 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1362 std::shared_ptr<ModelAPI_Result> aResult;
1364 std::string aFeatureKind = theFeature->getKind();
1365 if (aFeatureKind == SketchPlugin_Line::ID())
1366 aResult = theFeature->firstResult();
1367 else if (aFeatureKind == SketchPlugin_Arc::ID())
1368 aResult = theFeature->lastResult();
1369 else if (aFeatureKind == SketchPlugin_Circle::ID())
1370 aResult = theFeature->lastResult();
1375 //********************************************************************
1376 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1377 const ObjectPtr& theSketch,
1378 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1379 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1381 PointToRefsMap aPointsInfo;
1383 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1384 std::map<std::shared_ptr<GeomAPI_Pnt>,
1385 std::list< AttributePoint2DPtr > > aPointToAttributes;
1386 std::map<std::shared_ptr<GeomAPI_Pnt>,
1387 std::list< ObjectPtr > > aPointToObjects;
1389 std::set<AttributePoint2DPtr > aRefAttributes;
1391 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1392 std::set<ResultPtr> anEdgeShapes;
1394 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1395 if (!anEdgeShapes.empty()) {
1396 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1398 // coincidences to the feature
1399 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1400 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1401 // layed on feature coincidences to divide it on several shapes
1402 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1403 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1404 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1405 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1406 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1407 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1408 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1409 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1411 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1412 aX->dir(), aY, aPointsInfo);
1414 std::list<FeaturePtr> aFeatures;
1415 CompositeFeaturePtr aSketchComposite =
1416 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1417 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1418 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1420 aFeatures.push_back(aFeature);
1422 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1424 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1426 theObjectToPoints[theObject] = aPointsInfo;
1427 theCashedShapes[theObject] = aShapes;