1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_Trim.h"
22 #include <GeomAPI_Dir2d.h>
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Pnt2d.h>
25 #include <GeomAPI_XY.h>
26 #include <GeomDataAPI_Point2D.h>
27 #include <GeomAlgoAPI_ShapeTools.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_Tools.h>
34 #include <ModelAPI_AttributeBoolean.h>
36 #include <ModelAPI_Validator.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_AttributeDouble.h>
40 #include <ModelGeomAlgo_Shape.h>
42 #include <SketchPlugin_Arc.h>
43 #include <SketchPlugin_ConstraintMiddle.h>
44 #include <SketchPlugin_Circle.h>
45 #include <SketchPlugin_ConstraintCoincidence.h>
46 #include <SketchPlugin_ConstraintEqual.h>
47 #include <SketchPlugin_ConstraintTangent.h>
48 #include <SketchPlugin_ConstraintLength.h>
49 #include <SketchPlugin_ConstraintMirror.h>
50 #include <SketchPlugin_ConstraintCollinear.h>
51 #include <SketchPlugin_Line.h>
52 #include <SketchPlugin_MultiRotation.h>
53 #include <SketchPlugin_MultiTranslation.h>
54 #include <SketchPlugin_Point.h>
55 #include <SketchPlugin_Projection.h>
56 #include <SketchPlugin_Tools.h>
58 #include <ModelAPI_EventReentrantMessage.h>
60 #include <ModelAPI_Events.h>
61 #include <SketchPlugin_Line.h>
62 #include <SketchPlugin_Arc.h>
63 #include <SketchPlugin_Circle.h>
65 #include <ModelGeomAlgo_Point2D.h>
66 #include <Events_Loop.h>
74 #ifdef DEBUG_TRIM_METHODS
78 static const double PI = 3.141592653589793238463;
80 static const std::string OPERATION_HIGHLIGHT_COLOR() { return "128, 0, 0"; }
81 static const std::string OPERATION_REMOVE_FEATURE_COLOR() { return "255, 174, 201"; }
83 SketchPlugin_Trim::SketchPlugin_Trim()
87 void SketchPlugin_Trim::initAttributes()
89 data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
90 data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
92 data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
93 data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
95 data()->attribute(PREVIEW_POINT())->setIsArgument(false);
96 data()->attribute(SELECTED_POINT())->setIsArgument(false);
97 data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
99 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
100 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
103 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
104 const std::string& thePointAttributeId,
105 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
106 std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
108 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
109 data()->attribute(theObjectAttributeId));
110 ObjectPtr aBaseObject = aBaseObjectAttr->value();
112 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
113 data()->attribute(thePointAttributeId));
114 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
115 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
116 anAttributePnt2d->y());
118 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
119 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
121 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
122 if (!aShapes.empty()) {
123 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
124 for (; anIt != aLast; anIt++) {
125 GeomShapePtr aBaseShape = *anIt;
126 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
127 if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, anAttributePnt, aProjectedPoint)) {
129 if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
130 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
131 //GeomAPI_Shape::Orientation anOrientation = anEdge->orientation();
132 //if (anOrientation == GeomAPI_Shape::REVERSED) {
133 aStartPoint = anEdge->lastPoint();
134 aLastPoint = anEdge->firstPoint();
137 //aStartPoint = anEdge->firstPoint();
138 //aLastPoint = anEdge->lastPoint();
145 std::cout << "<findShapePoints> => "
146 << std::endl << "Attribute point: "
147 << anAttributePnt->x() << ", " << anAttributePnt->y() << ", " << anAttributePnt->z() << "]"
148 << std::endl << "Start Point: ["
149 << aStartPoint->x() << ", " << aStartPoint->y() << ", " << aStartPoint->z() << "]"
150 << std::endl << "Last Point: ["
151 << aLastPoint->x() << ", " << aLastPoint->y() << ", " << aLastPoint->z() << "]"
156 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Trim::convertPoint(
157 const std::shared_ptr<GeomAPI_Pnt>& thePoint)
159 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
163 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
164 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
165 ObjectPtr aBaseObject = aBaseObjectAttr->value();
166 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
167 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
170 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
171 for (PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
172 aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
173 if (aPointIt->first->isEqual(thePoint)) {
174 const std::pair<std::list<AttributePoint2DPtr >,
175 std::list<ObjectPtr > >& anInfo = aPointIt->second;
176 const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
177 if (!anAttributes.empty()) {
178 aPoint = anAttributes.front()->pnt();
182 aPoint = sketch()->to2D(thePoint);
188 // returns an end of the shape to define direction of split if feature's attribute
190 aPoint = sketch()->to2D(thePoint);
195 void SketchPlugin_Trim::execute()
197 #ifdef DEBUG_TRIM_METHODS
198 std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
201 SketchPlugin_Sketch* aSketch = sketch();
203 setError("Error: Sketch object is empty.");
207 // Check the base objects are initialized.
208 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
209 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
210 if(!aBaseObjectAttr->isInitialized()) {
211 setError("Error: Base object is not initialized.");
214 ObjectPtr aBaseObject = aBaseObjectAttr->value();
215 if (!aBaseObject.get()) {
216 setError("Error: Base object is not initialized.");
219 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
221 /// Remove reference of this feature to feature used in preview, it is not necessary anymore
222 /// as trim will be removed after execute
223 AttributeReferencePtr aPreviewObjectAttr =
224 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
225 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
227 ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
228 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
229 data()->attribute(PREVIEW_POINT()));
230 std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
231 // nullify pointer of preview attribute
232 aPreviewObjectAttr->setValue(ResultPtr());
234 bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
237 std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
239 std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
241 findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
242 if (!aStartShapePoint || !aLastShapePoint) {
243 setError("Error: Selected point is not placed on any edge");
247 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
248 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
249 /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
250 std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
251 getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
252 // find references(attributes and features) to the base feature
253 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
254 std::list<AttributePtr> aRefsToFeature;
255 getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
257 std::cout << "---- getRefAttributes ----" << std::endl;
258 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
259 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
260 std::cout << std::endl << "References to attributes of base feature [" <<
261 aBaseRefAttributes.size() << "]" << std::endl;
262 for (; aRefIt != aRefLast; aRefIt++) {
263 AttributePtr aBaseAttr = aRefIt->first;
264 std::list<AttributePtr> aRefAttributes = aRefIt->second;
265 std::string aRefsInfo;
266 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
267 aRefAttrLast = aRefAttributes.end();
268 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
269 if (!aRefsInfo.empty())
270 aRefsInfo.append(",");
271 AttributePtr aRAttr = *aRefAttrIt;
272 aRefsInfo.append(aRAttr->id());
273 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
274 aRefsInfo.append("(" + aRFeature->name() + ") ");
276 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
277 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
278 std::cout << aPointAttr->id().c_str() <<
279 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
281 std::cout << std::endl;
282 std::cout << std::endl << "References to base feature [" <<
283 aRefsToFeature.size() << "]" << std::endl;
284 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
285 aRefAttrLast = aRefsToFeature.end();
286 std::string aRefsInfo;
287 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
288 if (!aRefsInfo.empty())
289 aRefsInfo.append(",");
290 AttributePtr aRAttr = *aRefAttrIt;
291 aRefsInfo.append(aRAttr->id());
292 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
293 aRefsInfo.append("(" + aRFeature->name() + ") ");
295 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
296 std::cout << "---- getRefAttributes:end ----" << std::endl;
298 std::set<AttributePoint2DPtr> aFurtherCoincidences;
299 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
300 const std::string& aKind = aBaseFeature->getKind();
301 FeaturePtr aReplacingFeature, aNewFeature;
302 if (aKind == SketchPlugin_Circle::ID()) {
303 aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
304 aFurtherCoincidences, aModifiedAttributes);
306 aFeaturesToDelete.insert(aBaseFeature);
307 // as circle is removed, erase it from dependencies(arguments) of this feature
308 // otherwise Trim feature will be removed with the circle before
309 // this operation is finished
310 aBaseObjectAttr->setObject(ResultPtr());
312 else if (aKind == SketchPlugin_Line::ID()) {
313 aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
314 aFurtherCoincidences, aModifiedAttributes);
316 else if (aKind == SketchPlugin_Arc::ID()) {
317 aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
318 aFurtherCoincidences, aModifiedAttributes);
321 // constraints to end points of trim feature
322 if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
323 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
325 // create coincidence to objects, intersected the base object
326 const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
327 for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
328 aLast = aFurtherCoincidences.end();
329 anIt != aLast; anIt++) {
330 AttributePoint2DPtr aPointAttribute = (*anIt);
331 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
334 std::cout << "<compare Points> => " << std::endl
335 << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
336 if (aStartShapePoint2d.get())
337 std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
339 if (aLastShapePoint2d.get())
340 std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
344 std::shared_ptr<GeomAPI_Pnt> aPoint;
345 if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
346 aPoint = aStartShapePoint;
347 else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
348 aPoint = aLastShapePoint;
353 std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
354 for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
357 if (aRefIt->first->isEqual(aPoint)) {
358 anInfo = aRefIt->second;
362 const std::list<ObjectPtr>& anObjects = anInfo.second;
363 for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
364 anObjectIt != anObjects.end(); anObjectIt++) {
365 SketchPlugin_Tools::createConstraintAttrObject(sketch(),
366 SketchPlugin_ConstraintCoincidence::ID(),
367 aPointAttribute, *anObjectIt);
371 // move constraints from base feature to replacing feature: ignore coincidences to feature
372 // if attributes of coincidence participated in split
373 ResultPtr aReplacingResult;
374 if (aReplacingFeature.get()) {
375 aReplacingFeature->execute(); // need it to obtain result
376 aReplacingResult = getFeatureResult(aReplacingFeature);
378 for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
379 aLast = aRefsToFeature.end();
380 anIt != aLast; anIt++) {
381 AttributePtr anAttribute = *anIt;
383 if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences, aFeaturesToDelete))
386 // move tangency constraint to the nearest feature if possible
387 if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
390 if (aReplacingResult.get()) {
391 AttributeRefAttrPtr aRefAttr =
392 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
394 aRefAttr->setObject(aReplacingResult);
396 AttributeReferencePtr aReferenceAttr =
397 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
398 if (aReferenceAttr.get())
399 aReferenceAttr->setObject(aReplacingResult);
404 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
406 // Wait all constraints being created, then send update events
407 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
408 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
410 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
412 // delete constraints
414 if (aFeaturesToDelete.size() > 0) {
415 std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
417 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
418 anIt != aFeaturesToDelete.end(); anIt++) {
419 FeaturePtr aFeature = *anIt;
420 std::cout << aFeature->data()->name() << std::endl;
424 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
425 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
427 updateFeaturesAfterTrim(aFeaturesToUpdate);
429 // Send events to update the sub-features by the solver.
430 if(isUpdateFlushed) {
431 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
434 if (anIsEqualPreviewAndSelected) {
435 // equal preview and selected objects
436 // nothing to do if the preview and selected objects are different
437 if (aReplacingResult.get()) { // base object was removed
438 aPreviewObject = aReplacingResult;
439 //aMessage->setSelectedObject(aReplacingResult);
440 #ifdef DEBUG_TRIM_METHODS
441 if (!aSelectedShape.get())
442 std::cout << "Set empty selected object" << std::endl;
444 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
448 aPreviewObject = ObjectPtr();
450 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
451 aBaseObject = getFeatureResult(aBaseFeature);
452 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
454 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
456 GeomShapePtr aShape = aBaseResult->shape();
457 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
458 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
459 aPreviewObject = aBaseResult;
461 if (!aPreviewObject.get() && aNewFeature.get()) {
462 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
463 if (aNewFeatureResult.get()) {
464 GeomShapePtr aShape = aNewFeatureResult->shape();
465 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
466 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
467 aPreviewObject = aNewFeatureResult;
472 if (aPreviewObject.get()) {
473 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
474 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
475 ModelAPI_EventReentrantMessage::eventId(), this));
476 aMessage->setSelectedObject(aPreviewObject);
477 Events_Loop::loop()->send(aMessage);
480 std::cout << "SketchPlugin_Trim::done" << std::endl;
485 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
487 #ifdef DEBUG_TRIM_METHODS
488 std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
490 std::string aFilledAttributeName;
492 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
493 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
494 if (aMessage.get()) {
495 ObjectPtr anObject = aMessage->selectedObject();
496 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
498 if (anObject.get() && aPoint.get()) {
499 if (myCashedShapes.find(anObject) == myCashedShapes.end())
500 fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
501 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
502 if (aShapes.size() > 1) {
503 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
504 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
505 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
506 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
507 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
508 data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
509 aRefSelectedAttr->setValue(anObject);
510 aRefPreviewAttr->setValue(anObject);
512 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
513 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
514 data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
515 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
516 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
517 data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
518 aPointSelectedAttr->setValue(aPoint);
519 aPointPreviewAttr->setValue(aPoint);
521 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
523 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
524 #ifdef DEBUG_TRIM_METHODS
525 if (!aSelectedShape.get())
526 std::cout << "Set empty selected object" << std::endl;
528 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
530 aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
534 return aFilledAttributeName;
538 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
539 const std::set<AttributePoint2DPtr>& theFurtherCoincidences,
540 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
542 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
543 if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
546 AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
547 if (!aRefPointAttr.get())
549 std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
551 std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
552 aLast = theFurtherCoincidences.end();
553 bool aFoundPoint = false;
554 for (; anIt != aLast && !aFoundPoint; anIt++) {
555 AttributePoint2DPtr aPointAttribute = (*anIt);
556 std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
557 if (aPoint2d->isEqual(aRefPnt2d)) {
558 // create new coincidence and then remove the old one
559 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
560 SketchPlugin_ConstraintCoincidence::ID(),
561 aRefPointAttr, aPointAttribute);
562 theFeaturesToDelete.insert(aFeature);
568 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
569 const FeaturePtr& theFeature)
571 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
572 if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
575 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
580 // get shape of tangent object to the current
581 std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
582 if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
583 aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
584 AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
585 aFeature->attribute(aTangentAttr));
586 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
588 // get shape of the feature of the attribute
589 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
590 anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
591 PointToRefsMap aPointToAttributeOrObject;
592 std::list<FeaturePtr> aFeatures;
593 aFeatures.push_back(anAttributeFeature);
594 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
595 aPointToAttributeOrObject);
596 if (!aPointToAttributeOrObject.empty())
597 return true; // the attribute feature has a point of intersection, so we do not replace it
599 // get shape of the feature
600 aPointToAttributeOrObject.clear();
602 aFeatures.push_back(theFeature);
603 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
604 aPointToAttributeOrObject);
605 if (!aPointToAttributeOrObject.empty()) {
606 std::set<ResultPtr> anEdgeShapes;
607 ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
608 if (!anEdgeShapes.empty()) {
609 ResultPtr aResult = *anEdgeShapes.begin();
611 aRefAttr->setObject(aResult);
612 return true; // the attribute feature has a point of intersection, so we do not replace it
619 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
621 #ifdef DEBUG_TRIM_METHODS
622 std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
625 AISObjectPtr anAIS = thePrevious;
627 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
628 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
629 if (aPreviewShape.get())
630 aShapes.push_back(aPreviewShape);
631 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
632 if (aSelectedShape.get())
633 aShapes.push_back(aSelectedShape);
636 return AISObjectPtr();
638 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
639 if (!aBaseShape.get())
640 return AISObjectPtr();
642 if (aBaseShape.get()) {
644 anAIS = AISObjectPtr(new GeomAPI_AISObject);
645 anAIS->createShape(aBaseShape);
647 std::vector<int> aColor;
648 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
649 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
650 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
651 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
652 // width when there is not base object should be extened in several points
653 // in order to see this preview over highlight
654 anAIS->setWidth(aWidth+4);
655 anAIS->setLineStyle(aLineStyle);
658 anAIS = AISObjectPtr();
663 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
664 const std::string& thePointAttributeId)
666 GeomShapePtr aBaseShape;
668 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
669 data()->attribute(theObjectAttributeId));
670 ObjectPtr aBaseObject = anObjectAttr->value();
671 if (!aBaseObject.get())
675 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
676 data()->attribute(thePointAttributeId));
677 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
678 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
679 anAttributePnt2d->y());
681 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
682 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
684 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
685 if (!aShapes.empty()) {
686 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
687 for (; anIt != aLast; anIt++) {
688 GeomShapePtr aShape = *anIt;
689 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
690 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
697 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
698 AttributePoint2DPtr& theStartPointAttr,
699 AttributePoint2DPtr& theEndPointAttr)
701 std::string aFeatureKind = theFeature->getKind();
702 std::string aStartAttributeName, anEndAttributeName;
703 if (aFeatureKind == SketchPlugin_Line::ID()) {
704 aStartAttributeName = SketchPlugin_Line::START_ID();
705 anEndAttributeName = SketchPlugin_Line::END_ID();
707 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
708 aStartAttributeName = SketchPlugin_Arc::START_ID();
709 anEndAttributeName = SketchPlugin_Arc::END_ID();
711 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
712 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
713 theFeature->attribute(aStartAttributeName));
714 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
715 theFeature->attribute(anEndAttributeName));
719 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
720 std::set<FeaturePtr>& theFeaturesToUpdate)
722 std::shared_ptr<ModelAPI_Data> aData = data();
724 // Check the base objects are initialized.
725 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
726 aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
727 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
728 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
730 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
731 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
732 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
734 std::set<AttributePtr>::const_iterator aIt;
735 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
736 std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
737 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
738 std::string aRefFeatureKind = aRefFeature->getKind();
739 std::string anAttributeId = anAttr->id();
740 if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
741 anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
742 (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
743 anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
744 (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
745 anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
746 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
747 theFeaturesToDelete.insert(aRefFeature);
748 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
749 theFeaturesToUpdate.insert(aRefFeature);
753 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
754 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
755 std::list<AttributePtr>& theRefsToFeature)
759 std::list<AttributePtr> aPointAttributes =
760 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
761 std::set<AttributePtr> aPointAttributesSet;
763 std::list<AttributePtr>::const_iterator aPIt =
764 aPointAttributes.begin(), aPLast = aPointAttributes.end();
765 for (; aPIt != aPLast; aPIt++)
766 aPointAttributesSet.insert(*aPIt);
768 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
769 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
770 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
772 std::set<AttributePtr>::const_iterator aIt;
773 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
774 AttributePtr anAttr = (*aIt);
775 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
776 if (anAttrFeature.get() != this &&
777 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
778 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
779 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
780 AttributePtr anAttrInRef = aRefAttr->attr();
781 if (anAttrInRef.get() &&
782 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
783 if (theRefs.find(anAttrInRef) != theRefs.end())
784 theRefs[anAttrInRef].push_back(aRefAttr);
786 std::list<AttributePtr> anAttrList;
787 anAttrList.push_back(aRefAttr);
788 theRefs[anAttrInRef] = anAttrList;
792 else { /// find attributes referenced to feature itself
793 theRefsToFeature.push_back(anAttr);
799 void SketchPlugin_Trim::updateRefAttConstraints(
800 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
801 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
802 std::set<FeaturePtr>& theFeaturesToDelete)
805 std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
808 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
809 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
810 for (; anIt != aLast; anIt++) {
811 AttributePtr anAttribute = anIt->first;
813 /// not found in references
814 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
816 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
817 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
818 aRLast = aRefAttributes.end();
820 AttributePtr aNewAttribute = anIt->second;
821 if (aNewAttribute.get()) {
822 for (; aRefIt != aRLast; aRefIt++) {
823 AttributeRefAttrPtr aRefAttr =
824 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
825 if (aRefAttr.get()) {
826 aRefAttr->setAttr(aNewAttribute);
833 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
834 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
836 /// not found in references
837 if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
840 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
841 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
842 aRLast = aRefAttributes.end();
844 std::set<FeaturePtr> aFeaturesToDelete;
845 for (; aRefIt != aRLast; aRefIt++) {
846 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
847 if (aRefAttr.get()) {
848 aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
853 // delete constraints
854 if (aFeaturesToDelete.size() > 0) {
855 std::cout << "removeReferencesToAttribute: " << std::endl;
857 for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
858 anIt != aFeaturesToDelete.end(); anIt++) {
859 FeaturePtr aFeature = *anIt;
860 std::cout << aFeature->data()->name() << std::endl;
864 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
865 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
868 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
870 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
871 aLast = theFeaturesToUpdate.end();
872 for (; anIt != aLast; anIt++) {
873 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
874 std::string aRefFeatureKind = aRefFeature->getKind();
875 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
876 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
877 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
878 if (aLenghtFeature.get()) {
879 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
880 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
882 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
883 aValueAttr->setValue(aValue);
889 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
890 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
891 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
892 std::set<AttributePoint2DPtr>& thePoints,
893 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
895 FeaturePtr anNewFeature;
897 // Check the base objects are initialized.
898 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
899 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
900 ObjectPtr aBaseObject = aBaseObjectAttr->value();
901 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
904 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
905 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
907 std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
908 std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
910 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
911 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
912 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
913 aStartShapePoint, aLastShapePoint);
915 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
916 if (aStartShapePoint.get())
917 std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
918 aStartShapePoint->y() << "]" << std::endl;
919 std::cout << "1st point: [" << aStartFeaturePoint->x() << ", " <<
920 aStartFeaturePoint->y() << "]" << std::endl;
921 if (aLastShapePoint.get())
922 std::cout << "2st point: [" << aLastShapePoint->x() << ", " <<
923 aLastShapePoint->y() << "]" << std::endl;
924 std::cout << "End point: [" << aLastFeaturePoint->x() << ", " <<
925 aLastFeaturePoint->y() << "]" << std::endl;
928 bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
929 bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
930 if (isStartPoint || isLastPoint) {
931 // result is one line: changed existing line
932 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
933 : SketchPlugin_Line::END_ID();
934 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
935 if (aStartShapePoint.get() && aLastShapePoint.get())
936 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
938 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
940 // it is important to delete references before the feature modification because
941 // if deletion will be after the feature modification, solver returns the feature back
942 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
943 theBaseRefAttributes);
945 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
946 //theModifiedAttributes.insert(
947 // std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
949 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
950 (aBaseFeature->attribute(aModifiedAttribute)));
953 // result is two lines: start line point - start shape point,
954 // last shape point - last line point
955 // create second line
956 anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
957 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
958 (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
960 std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
961 theModifiedAttributes.insert(
962 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
963 anNewFeature->attribute(SketchPlugin_Line::END_ID())));
966 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
968 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
969 (aBaseFeature->attribute(aModifiedAttribute)));
971 // Collinear constraint for lines
972 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
973 SketchPlugin_ConstraintCollinear::ID(),
974 getFeatureResult(aBaseFeature),
975 getFeatureResult(anNewFeature));
980 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
981 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
982 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
983 std::set<AttributePoint2DPtr>& thePoints,
984 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
986 FeaturePtr anNewFeature;
987 // Check the base objects are initialized.
988 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
989 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
990 ObjectPtr aBaseObject = aBaseObjectAttr->value();
991 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
994 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
995 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
997 std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
998 std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
1000 std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
1001 std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
1002 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1003 aStartShapePoint, aLastShapePoint);
1005 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1006 if (aStartShapePoint.get())
1007 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1008 aStartShapePoint->y() << "]" << std::endl;
1009 std::cout << "Start arc attribute point: [" << aStartArcPoint->x() << ", " <<
1010 aStartArcPoint->y() << "]" << std::endl;
1011 if (aLastShapePoint.get())
1012 std::cout << "Last shape point: [" << aLastShapePoint->x() << ", " <<
1013 aLastShapePoint->y() << "]" << std::endl;
1014 std::cout << "Last arc attribute point: [" << aLastArcPoint->x() << ", " <<
1015 aLastArcPoint->y() << "]" << std::endl;
1018 bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1019 bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1020 if (isStartPoint || isLastPoint) {
1021 // result is one arc: changed existing arc
1022 std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1023 : SketchPlugin_Arc::END_ID();
1024 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1025 if (aStartShapePoint.get() && aLastShapePoint.get())
1026 aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1028 aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1030 removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1031 theBaseRefAttributes);
1033 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1035 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1036 (aBaseFeature->attribute(aModifiedAttribute)));
1039 // result is two arcs: start arc point - start shape point, last shape point - last arc point
1040 // create second arc
1041 anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1042 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1043 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1045 std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1046 theModifiedAttributes.insert(
1047 std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1048 anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1051 fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1053 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1054 (aBaseFeature->attribute(aModifiedAttribute)));
1056 // equal Radius constraint for arcs
1057 SketchPlugin_Tools::createConstraintObjectObject(sketch(),
1058 SketchPlugin_ConstraintEqual::ID(),
1059 getFeatureResult(aBaseFeature),
1060 getFeatureResult(anNewFeature));
1061 // coincident centers constraint
1062 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1063 SketchPlugin_ConstraintCoincidence::ID(),
1064 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1065 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1068 std::cout << "Created arc on points:" << std::endl;
1069 std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1070 aStartShapePoint->y() << "]" << std::endl;
1073 return anNewFeature;
1076 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1077 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1078 std::set<AttributePoint2DPtr>& thePoints,
1079 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1081 // Check the base objects are initialized.
1082 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1083 data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1084 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1085 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1088 //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1089 //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1092 FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1093 // arc created by trim of circle is always correct, that means that it is not inversed
1094 anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1096 theModifiedAttributes.insert(
1097 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1098 anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1100 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1101 (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1102 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1103 (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1105 return anNewFeature;
1108 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1109 const AttributePoint2DPtr& theEndPointAttr,
1110 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1111 std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1113 if (!theFirstPoint.get() || !theLastPoint.get())
1116 // if first point is closer to last point, swap first and last values
1117 if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1118 theStartPointAttr->pnt()->distance(theLastPoint)) {
1119 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1120 theFirstPoint = theLastPoint;
1121 theLastPoint = aTmpPoint;
1125 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1126 const AttributePoint2DPtr& theStartPointAttr,
1127 const AttributePoint2DPtr& theEndPointAttr,
1128 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1129 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1131 if (!theFirstPoint.get() || !theSecondPoint.get())
1134 static const double anAngleTol = 1.e-12;
1136 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1137 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1138 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1140 // collect directions to each point
1141 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1142 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1143 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1144 new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1145 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1146 new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1148 // sort points by their angular values
1149 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1150 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1151 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1152 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1153 aFirstPtAngle += aPeriod;
1154 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1155 aSecondPtAngle += aPeriod;
1157 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1158 std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1159 theFirstPoint = theSecondPoint;
1160 theSecondPoint = aTmpPoint;
1164 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1165 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1167 std::string anAttributeType = theModifiedAttribute->attributeType();
1168 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1169 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1170 theModifiedAttribute);
1171 aModifiedAttribute->setValue(thePoint);
1174 FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1175 std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1176 theModifiedAttribute->id() <<
1177 "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1183 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1184 const AttributePtr& theSourceAttribute)
1186 std::string anAttributeType = theModifiedAttribute->attributeType();
1187 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1188 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1189 theModifiedAttribute);
1190 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1191 theSourceAttribute);
1193 if (aModifiedAttribute.get() && aSourceAttribute.get())
1194 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1196 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1197 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1198 theModifiedAttribute);
1199 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1200 theSourceAttribute);
1202 if (aModifiedAttribute.get() && aSourceAttribute.get())
1203 aModifiedAttribute->setValue(aSourceAttribute->value());
1207 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1208 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1209 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1212 std::cout << "---- createLineFeature ---" << std::endl;
1215 FeaturePtr aFeature;
1216 SketchPlugin_Sketch* aSketch = sketch();
1217 if (!aSketch || !theBaseFeature.get())
1220 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1222 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1223 fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1225 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1226 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1228 aFeature->execute(); // to obtain result
1231 std::cout << "---- createLineFeature:end ---" << std::endl;
1237 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1238 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1239 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1241 FeaturePtr aFeature;
1242 SketchPlugin_Sketch* aSketch = sketch();
1243 if (!aSketch || !theBaseFeature.get())
1246 std::string aCenterAttributeId;
1247 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1248 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1249 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1250 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1252 if (aCenterAttributeId.empty())
1256 std::cout << "---- createArcFeature ---" << std::endl;
1259 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1260 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1261 // the "attribute updated"
1262 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1263 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1265 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1266 theBaseFeature->attribute(aCenterAttributeId));
1267 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1268 fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1270 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1271 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1273 /// fill referersed state of created arc as it is on the base arc
1274 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1275 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1276 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1278 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1279 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1282 std::cout << "---- createArcFeature:end ---" << std::endl;
1288 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1289 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1291 std::shared_ptr<ModelAPI_Result> aResult;
1293 std::string aFeatureKind = theFeature->getKind();
1294 if (aFeatureKind == SketchPlugin_Line::ID())
1295 aResult = theFeature->firstResult();
1296 else if (aFeatureKind == SketchPlugin_Arc::ID())
1297 aResult = theFeature->lastResult();
1298 else if (aFeatureKind == SketchPlugin_Circle::ID())
1299 aResult = theFeature->lastResult();
1304 //********************************************************************
1305 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1306 const ObjectPtr& theSketch,
1307 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1308 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1310 PointToRefsMap aPointsInfo;
1312 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1313 std::map<std::shared_ptr<GeomAPI_Pnt>,
1314 std::list< AttributePoint2DPtr > > aPointToAttributes;
1315 std::map<std::shared_ptr<GeomAPI_Pnt>,
1316 std::list< ObjectPtr > > aPointToObjects;
1318 std::set<AttributePoint2DPtr > aRefAttributes;
1320 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1321 std::set<ResultPtr> anEdgeShapes;
1323 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1324 if (!anEdgeShapes.empty()) {
1325 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1327 // coincidences to the feature
1328 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1329 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1330 // layed on feature coincidences to divide it on several shapes
1331 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1332 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1333 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1334 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1335 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1336 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1337 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1338 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1340 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1341 aX->dir(), aY, aPointsInfo);
1343 std::list<FeaturePtr> aFeatures;
1344 CompositeFeaturePtr aSketchComposite =
1345 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1346 for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1347 FeaturePtr aFeature = aSketchComposite->subFeature(i);
1348 if (aFeature.get() && aFeature->getKind() != SketchPlugin_Projection::ID())
1349 aFeatures.push_back(aFeature);
1351 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1353 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1355 theObjectToPoints[theObject] = aPointsInfo;
1356 theCashedShapes[theObject] = aShapes;