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::createConstraint(sketch(), SketchPlugin_ConstraintCoincidence::ID(),
363 aPointAttribute, *anObjectIt);
367 // move constraints from base feature to replacing feature: ignore coincidences to feature
368 // if attributes of coincidence participated in split
369 ResultPtr aReplacingResult;
370 if (aReplacingFeature.get()) {
371 aReplacingFeature->execute(); // need it to obtain result
372 aReplacingResult = getFeatureResult(aReplacingFeature);
374 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
375 aLast = aRefsToFeature.end();
376 anIt != aLast; anIt++) {
377 AttributePtr anAttribute = *anIt;
379 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences, aFeaturesToDelete))
382 // move tangency constraint to the nearest feature if possible
383 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
386 if (aReplacingResult.get()) {
387 AttributeRefAttrPtr aRefAttr =
388 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
390 aRefAttr->setObject(aReplacingResult);
392 AttributeReferencePtr aReferenceAttr =
393 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
394 if (aReferenceAttr.get())
395 aReferenceAttr->setObject(aReplacingResult);
400 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
402 // Wait all constraints being created, then send update events
403 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
404 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
406 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
408 // delete constraints
410 if (aFeaturesToDelete.size() > 0) {
411 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
413 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
414 anIt != aFeaturesToDelete.end(); anIt++) {
415 FeaturePtr aFeature = *anIt;
416 std::cout << aFeature->data()->name() << std::endl;
420 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
421 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
423 updateFeaturesAfterTrim(aFeaturesToUpdate);
425 // Send events to update the sub-features by the solver.
426 if(isUpdateFlushed) {
427 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
430 if (anIsEqualPreviewAndSelected) {
431 // equal preview and selected objects
432 // nothing to do if the preview and selected objects are different
433 if (aReplacingResult.get()) { // base object was removed
434 aPreviewObject = aReplacingResult;
435 //aMessage->setSelectedObject(aReplacingResult);
436 #ifdef DEBUG_TRIM_METHODS
437 if (!aSelectedShape.get())
438 std::cout << "Set empty selected object" << std::endl;
440 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
444 aPreviewObject = ObjectPtr();
446 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
447 aBaseObject = getFeatureResult(aBaseFeature);
448 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
450 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
452 GeomShapePtr aShape = aBaseResult->shape();
453 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
454 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
455 aPreviewObject = aBaseResult;
457 if (!aPreviewObject.get() && aNewFeature.get()) {
458 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
459 if (aNewFeatureResult.get()) {
460 GeomShapePtr aShape = aNewFeatureResult->shape();
461 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
462 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
463 aPreviewObject = aNewFeatureResult;
468 if (aPreviewObject.get()) {
469 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
470 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
471 ModelAPI_EventReentrantMessage::eventId(), this));
472 aMessage->setSelectedObject(aPreviewObject);
473 Events_Loop::loop()->send(aMessage);
476 std::cout << "SketchPlugin_Trim::done" << std::endl;
480 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
482 #ifdef DEBUG_TRIM_METHODS
483 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
485 std::string aFilledAttributeName;
487 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
488 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
489 if (aMessage.get()) {
490 ObjectPtr anObject = aMessage->selectedObject();
491 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
493 if (anObject.get() && aPoint.get()) {
494 if (myCashedShapes.find(anObject) == myCashedShapes.end())
495 fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
496 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
497 if (aShapes.size() > 1) {
498 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
499 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
500 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
501 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
502 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
503 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
504 aRefSelectedAttr->setValue(anObject);
505 aRefPreviewAttr->setValue(anObject);
507 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
508 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
509 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
510 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
511 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
512 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
513 aPointSelectedAttr->setValue(aPoint);
514 aPointPreviewAttr->setValue(aPoint);
516 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
518 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
519 #ifdef DEBUG_TRIM_METHODS
520 if (!aSelectedShape.get())
521 std::cout << "Set empty selected object" << std::endl;
523 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
525 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
529 return aFilledAttributeName;
532 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
533 const std::set<AttributePoint2DPtr>& theFurtherCoincidences,
534 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
536 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
537 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
540 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
541 if (!aRefPointAttr.get())
543 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
545 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
546 aLast = theFurtherCoincidences.end();
547 bool aFoundPoint = false;
548 for (; anIt != aLast && !aFoundPoint; anIt++) {
549 AttributePoint2DPtr aPointAttribute = (*anIt);
550 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
551 if (aPoint2d->isEqual(aRefPnt2d)) {
552 // create new coincidence and then remove the old one
553 SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintCoincidence::ID(),
554 aRefPointAttr, aPointAttribute);
555 theFeaturesToDelete.insert(aFeature);
561 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
562 const FeaturePtr& theFeature)
564 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
565 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
568 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
573 // get shape of tangent object to the current
574 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
575 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
576 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
577 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
578 aFeature->attribute(aTangentAttr));
579 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
581 // get shape of the feature of the attribute
582 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
583 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
584 PointToRefsMap aPointToAttributeOrObject;
585 std::list<FeaturePtr> aFeatures;
586 aFeatures.push_back(anAttributeFeature);
587 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
588 aPointToAttributeOrObject);
589 if (!aPointToAttributeOrObject.empty())
590 return true; // the attribute feature has a point of intersection, so we do not replace it
592 // get shape of the feature
593 aPointToAttributeOrObject.clear();
595 aFeatures.push_back(theFeature);
596 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
597 aPointToAttributeOrObject);
598 if (!aPointToAttributeOrObject.empty()) {
599 std::set<ResultPtr> anEdgeShapes;
600 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
601 if (!anEdgeShapes.empty()) {
602 ResultPtr aResult = *anEdgeShapes.begin();
604 aRefAttr->setObject(aResult);
605 return true; // the attribute feature has a point of intersection, so we do not replace it
612 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
614 #ifdef DEBUG_TRIM_METHODS
615 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
618 AISObjectPtr anAIS = thePrevious;
620 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
621 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
622 if (aPreviewShape.get())
623 aShapes.push_back(aPreviewShape);
624 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
625 if (aSelectedShape.get())
626 aShapes.push_back(aSelectedShape);
629 return AISObjectPtr();
631 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
632 if (!aBaseShape.get())
633 return AISObjectPtr();
635 if (aBaseShape.get()) {
637 anAIS = AISObjectPtr(new GeomAPI_AISObject);
638 anAIS->createShape(aBaseShape);
640 std::vector<int> aColor;
641 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
642 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
643 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
644 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
645 // width when there is not base object should be extened in several points
646 // in order to see this preview over highlight
647 anAIS->setWidth(aWidth+4);
648 anAIS->setLineStyle(aLineStyle);
651 anAIS = AISObjectPtr();
656 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
657 const std::string& thePointAttributeId)
659 GeomShapePtr aBaseShape;
661 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
662 data()->attribute(theObjectAttributeId));
663 ObjectPtr aBaseObject = anObjectAttr->value();
664 if (!aBaseObject.get())
668 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
669 data()->attribute(thePointAttributeId));
670 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
671 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
672 anAttributePnt2d->y());
674 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
675 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
677 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
678 if (!aShapes.empty()) {
679 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
680 for (; anIt != aLast; anIt++) {
681 GeomShapePtr aShape = *anIt;
682 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
683 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
690 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
691 AttributePoint2DPtr& theStartPointAttr,
692 AttributePoint2DPtr& theEndPointAttr)
694 std::string aFeatureKind = theFeature->getKind();
695 std::string aStartAttributeName, anEndAttributeName;
696 if (aFeatureKind == SketchPlugin_Line::ID()) {
697 aStartAttributeName = SketchPlugin_Line::START_ID();
698 anEndAttributeName = SketchPlugin_Line::END_ID();
700 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
701 aStartAttributeName = SketchPlugin_Arc::START_ID();
702 anEndAttributeName = SketchPlugin_Arc::END_ID();
704 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
705 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
706 theFeature->attribute(aStartAttributeName));
707 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
708 theFeature->attribute(anEndAttributeName));
712 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
713 std::set<FeaturePtr>& theFeaturesToUpdate)
715 std::shared_ptr<ModelAPI_Data> aData = data();
717 // Check the base objects are initialized.
718 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
719 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
720 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
721 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
723 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
724 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
725 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
727 std::set<AttributePtr>::const_iterator aIt;
728 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
729 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
730 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
731 std::string aRefFeatureKind = aRefFeature->getKind();
732 std::string anAttributeId = anAttr->id();
733 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
734 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
735 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
736 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
737 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
738 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
739 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
740 theFeaturesToDelete.insert(aRefFeature);
741 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
742 theFeaturesToUpdate.insert(aRefFeature);
746 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
747 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
748 std::list<AttributePtr>& theRefsToFeature)
752 std::list<AttributePtr> aPointAttributes =
753 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
754 std::set<AttributePtr> aPointAttributesSet;
756 std::list<AttributePtr>::const_iterator aPIt =
757 aPointAttributes.begin(), aPLast = aPointAttributes.end();
758 for (; aPIt != aPLast; aPIt++)
759 aPointAttributesSet.insert(*aPIt);
761 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
762 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
763 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
765 std::set<AttributePtr>::const_iterator aIt;
766 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
767 AttributePtr anAttr = (*aIt);
768 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
769 if (anAttrFeature.get() != this &&
770 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
771 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
772 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
773 AttributePtr anAttrInRef = aRefAttr->attr();
774 if (anAttrInRef.get() &&
775 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
776 if (theRefs.find(anAttrInRef) != theRefs.end())
777 theRefs[anAttrInRef].push_back(aRefAttr);
779 std::list<AttributePtr> anAttrList;
780 anAttrList.push_back(aRefAttr);
781 theRefs[anAttrInRef] = anAttrList;
785 else { /// find attributes referenced to feature itself
786 theRefsToFeature.push_back(anAttr);
792 void SketchPlugin_Trim::updateRefAttConstraints(
793 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
794 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
795 std::set<FeaturePtr>& theFeaturesToDelete)
798 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
801 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
802 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
803 for (; anIt != aLast; anIt++) {
804 AttributePtr anAttribute = anIt->first;
806 /// not found in references
807 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
809 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
810 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
811 aRLast = aRefAttributes.end();
813 AttributePtr aNewAttribute = anIt->second;
814 if (aNewAttribute.get()) {
815 for (; aRefIt != aRLast; aRefIt++) {
816 AttributeRefAttrPtr aRefAttr =
817 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
818 if (aRefAttr.get()) {
819 aRefAttr->setAttr(aNewAttribute);
826 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
827 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
829 /// not found in references
830 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
833 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
834 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
835 aRLast = aRefAttributes.end();
837 std::set<FeaturePtr> aFeaturesToDelete;
838 for (; aRefIt != aRLast; aRefIt++) {
839 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
840 if (aRefAttr.get()) {
841 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
846 // delete constraints
847 if (aFeaturesToDelete.size() > 0) {
848 std::cout << "removeReferencesToAttribute: " << std::endl;
850 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
851 anIt != aFeaturesToDelete.end(); anIt++) {
852 FeaturePtr aFeature = *anIt;
853 std::cout << aFeature->data()->name() << std::endl;
857 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
858 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
861 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
863 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
864 aLast = theFeaturesToUpdate.end();
865 for (; anIt != aLast; anIt++) {
866 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
867 std::string aRefFeatureKind = aRefFeature->getKind();
868 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
869 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
870 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
871 if (aLenghtFeature.get()) {
872 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
873 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
875 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
876 aValueAttr->setValue(aValue);
882 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
883 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
884 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
885 std::set<AttributePoint2DPtr>& thePoints,
886 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
888 FeaturePtr anNewFeature;
890 // Check the base objects are initialized.
891 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
892 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
893 ObjectPtr aBaseObject = aBaseObjectAttr->value();
894 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
897 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
898 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
900 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
901 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
903 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
904 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
905 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
906 aStartShapePoint, aLastShapePoint);
908 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
909 if (aStartShapePoint.get())
910 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
911 aStartShapePoint->y() << "]" << std::endl;
912 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
913 aStartFeaturePoint->y() << "]" << std::endl;
914 if (aLastShapePoint.get())
915 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
916 aLastShapePoint->y() << "]" << std::endl;
917 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
918 aLastFeaturePoint->y() << "]" << std::endl;
921 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
922 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
923 if (isStartPoint || isLastPoint) {
924 // result is one line: changed existing line
925 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
926 : SketchPlugin_Line::END_ID();
927 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
928 if (aStartShapePoint.get() && aLastShapePoint.get())
929 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
931 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
933 // it is important to delete references before the feature modification because
934 // if deletion will be after the feature modification, solver returns the feature back
935 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
936 theBaseRefAttributes);
938 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
939 //theModifiedAttributes.insert(
940 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
942 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
943 (aBaseFeature->attribute(aModifiedAttribute)));
946 // result is two lines: start line point - start shape point,
947 // last shape point - last line point
948 // create second line
949 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
950 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
951 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
953 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
954 theModifiedAttributes.insert(
955 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
956 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
959 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
961 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
962 (aBaseFeature->attribute(aModifiedAttribute)));
964 // Collinear constraint for lines
965 SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintCollinear::ID(),
966 getFeatureResult(aBaseFeature),
967 getFeatureResult(anNewFeature));
972 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
973 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
974 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
975 std::set<AttributePoint2DPtr>& thePoints,
976 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
978 FeaturePtr anNewFeature;
979 // Check the base objects are initialized.
980 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
981 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
982 ObjectPtr aBaseObject = aBaseObjectAttr->value();
983 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
986 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
987 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
989 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
990 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
992 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
993 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
994 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
995 aStartShapePoint, aLastShapePoint);
997 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
998 if (aStartShapePoint.get())
999 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1000 aStartShapePoint->y() << "]" << std::endl;
1001 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1002 aStartArcPoint->y() << "]" << std::endl;
1003 if (aLastShapePoint.get())
1004 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1005 aLastShapePoint->y() << "]" << std::endl;
1006 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1007 aLastArcPoint->y() << "]" << std::endl;
1010 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1011 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1012 if (isStartPoint || isLastPoint) {
1013 // result is one arc: changed existing arc
1014 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1015 : SketchPlugin_Arc::END_ID();
1016 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1017 if (aStartShapePoint.get() && aLastShapePoint.get())
1018 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1020 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1022 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1023 theBaseRefAttributes);
1025 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1027 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1028 (aBaseFeature->attribute(aModifiedAttribute)));
1031 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1032 // create second arc
1033 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1034 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1035 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1037 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1038 theModifiedAttributes.insert(
1039 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1040 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1043 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1045 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1046 (aBaseFeature->attribute(aModifiedAttribute)));
1048 // equal Radius constraint for arcs
1049 SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintEqual::ID(),
1050 getFeatureResult(aBaseFeature),
1051 getFeatureResult(anNewFeature));
1052 // coincident centers constraint
1053 SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintCoincidence::ID(),
1054 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1055 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1058 std::cout << "Created arc on points:" << std::endl;
1059 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1060 aStartShapePoint->y() << "]" << std::endl;
1063 return anNewFeature;
1066 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1067 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1068 std::set<AttributePoint2DPtr>& thePoints,
1069 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1071 // Check the base objects are initialized.
1072 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1073 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1074 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1075 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1078 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1079 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1082 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1083 // arc created by trim of circle is always correct, that means that it is not inversed
1084 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1086 theModifiedAttributes.insert(
1087 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1088 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1090 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1091 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1092 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1093 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1095 return anNewFeature;
1098 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1099 const AttributePoint2DPtr& theEndPointAttr,
1100 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1101 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1103 if (!theFirstPoint.get() || !theLastPoint.get())
1106 // if first point is closer to last point, swap first and last values
1107 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1108 theStartPointAttr->pnt()->distance(theLastPoint)) {
1109 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1110 theFirstPoint = theLastPoint;
1111 theLastPoint = aTmpPoint;
1115 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1116 const AttributePoint2DPtr& theStartPointAttr,
1117 const AttributePoint2DPtr& theEndPointAttr,
1118 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1119 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1121 if (!theFirstPoint.get() || !theSecondPoint.get())
1124 static const double anAngleTol = 1.e-12;
1126 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1127 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1128 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1130 // collect directions to each point
1131 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1132 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1133 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1134 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1135 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1136 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1138 // sort points by their angular values
1139 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1140 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1141 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1142 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1143 aFirstPtAngle += aPeriod;
1144 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1145 aSecondPtAngle += aPeriod;
1147 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1148 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1149 theFirstPoint = theSecondPoint;
1150 theSecondPoint = aTmpPoint;
1154 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1155 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1157 std::string anAttributeType = theModifiedAttribute->attributeType();
1158 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1159 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1160 theModifiedAttribute);
1161 aModifiedAttribute->setValue(thePoint);
1164 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1165 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1166 theModifiedAttribute->id() <<
1167 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1173 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1174 const AttributePtr& theSourceAttribute)
1176 std::string anAttributeType = theModifiedAttribute->attributeType();
1177 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1178 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1179 theModifiedAttribute);
1180 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1181 theSourceAttribute);
1183 if (aModifiedAttribute.get() && aSourceAttribute.get())
1184 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1186 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1187 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1188 theModifiedAttribute);
1189 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1190 theSourceAttribute);
1192 if (aModifiedAttribute.get() && aSourceAttribute.get())
1193 aModifiedAttribute->setValue(aSourceAttribute->value());
1195 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1196 AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1197 theModifiedAttribute);
1198 AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1199 theSourceAttribute);
1200 if (!aSourceRefAttr.get())
1201 aRefAttributeToFill->setAttr(theSourceAttribute);
1203 if (aSourceRefAttr->isObject())
1204 aRefAttributeToFill->setObject(aSourceRefAttr->object());
1206 aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1211 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1212 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1213 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1216 std::cout << "---- createLineFeature ---" << std::endl;
1219 FeaturePtr aFeature;
1220 SketchPlugin_Sketch* aSketch = sketch();
1221 if (!aSketch || !theBaseFeature.get())
1224 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1226 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1227 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1229 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1230 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1232 aFeature->execute(); // to obtain result
1235 std::cout << "---- createLineFeature:end ---" << std::endl;
1241 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1242 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1243 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1245 FeaturePtr aFeature;
1246 SketchPlugin_Sketch* aSketch = sketch();
1247 if (!aSketch || !theBaseFeature.get())
1250 std::string aCenterAttributeId;
1251 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1252 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1253 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1254 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1256 if (aCenterAttributeId.empty())
1260 std::cout << "---- createArcFeature ---" << std::endl;
1263 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1264 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1265 // the "attribute updated"
1266 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1267 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1269 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1270 theBaseFeature->attribute(aCenterAttributeId));
1271 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1272 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1274 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1275 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1277 /// fill referersed state of created arc as it is on the base arc
1278 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1279 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1280 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1282 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1283 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1286 std::cout << "---- createArcFeature:end ---" << std::endl;
1292 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1293 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1295 std::shared_ptr<ModelAPI_Result> aResult;
1297 std::string aFeatureKind = theFeature->getKind();
1298 if (aFeatureKind == SketchPlugin_Line::ID())
1299 aResult = theFeature->firstResult();
1300 else if (aFeatureKind == SketchPlugin_Arc::ID())
1301 aResult = theFeature->lastResult();
1302 else if (aFeatureKind == SketchPlugin_Circle::ID())
1303 aResult = theFeature->lastResult();
1308 //********************************************************************
1309 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1310 const ObjectPtr& theSketch,
1311 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1312 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1314 PointToRefsMap aPointsInfo;
1316 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1317 std::map<std::shared_ptr<GeomAPI_Pnt>,
1318 std::list< AttributePoint2DPtr > > aPointToAttributes;
1319 std::map<std::shared_ptr<GeomAPI_Pnt>,
1320 std::list< ObjectPtr > > aPointToObjects;
1322 std::set<AttributePoint2DPtr > aRefAttributes;
1324 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1325 std::set<ResultPtr> anEdgeShapes;
1327 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1328 if (!anEdgeShapes.empty()) {
1329 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1331 // coincidences to the feature
1332 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1333 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1334 // layed on feature coincidences to divide it on several shapes
1335 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1336 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1337 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1338 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1339 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1340 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1341 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1342 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1344 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1345 aX->dir(), aY, aPointsInfo);
1347 std::list<FeaturePtr> aFeatures;
1348 CompositeFeaturePtr aSketchComposite =
1349 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1350 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1351 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1352 if (aFeature.get() && aFeature->getKind() != SketchPlugin_Projection::ID())
1353 aFeatures.push_back(aFeature);
1355 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1357 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1359 theObjectToPoints[theObject] = aPointsInfo;
1360 theCashedShapes[theObject] = aShapes;