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_Tools.h"
22 #include "SketchPlugin_Arc.h"
23 #include "SketchPlugin_Circle.h"
24 #include "SketchPlugin_ConstraintCoincidence.h"
25 #include "SketchPlugin_ConstraintCoincidenceInternal.h"
26 #include "SketchPlugin_ConstraintLength.h"
27 #include "SketchPlugin_ConstraintTangent.h"
28 #include "SketchPlugin_Ellipse.h"
29 #include "SketchPlugin_EllipticArc.h"
30 #include "SketchPlugin_Line.h"
31 #include "SketchPlugin_Point.h"
32 #include "SketchPlugin_Projection.h"
33 #include "SketchPlugin_SketchEntity.h"
34 #include "SketchPlugin_Split.h"
35 #include "SketchPlugin_Trim.h"
37 #include <SketcherPrs_Tools.h>
39 #include <ModelAPI_AttributeDouble.h>
41 #include <ModelGeomAlgo_Point2D.h>
42 #include <ModelGeomAlgo_Shape.h>
44 #include <GeomAPI_Dir2d.h>
45 #include <GeomAPI_Edge.h>
46 #include <GeomAPI_Pnt2d.h>
47 #include <GeomAPI_XY.h>
49 #include <GeomAlgoAPI_CompoundBuilder.h>
50 #include <GeomAlgoAPI_ShapeTools.h>
52 #include <GeomDataAPI_Point.h>
53 #include <GeomDataAPI_Point2D.h>
59 namespace SketchPlugin_Tools {
61 void clearExpressions(AttributeDoublePtr theAttribute)
63 theAttribute->setText(std::string());
66 void clearExpressions(AttributePointPtr theAttribute)
68 theAttribute->setText(std::string(), std::string(), std::string());
71 void clearExpressions(AttributePoint2DPtr theAttribute)
73 theAttribute->setText(std::string(), std::string());
76 void clearExpressions(AttributePtr theAttribute)
79 AttributeDoublePtr anAttributeDouble =
80 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
81 if (anAttributeDouble.get())
82 clearExpressions(anAttributeDouble);
84 AttributePointPtr anAttributePoint =
85 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
86 if (anAttributePoint.get())
87 clearExpressions(anAttributePoint);
89 AttributePoint2DPtr anAttributePoint2D =
90 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
91 if (anAttributePoint2D.get())
92 clearExpressions(anAttributePoint2D);
95 void clearExpressions(FeaturePtr theFeature)
97 if (!theFeature.get())
100 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
101 std::list<AttributePtr>::iterator anAttributeIt = anAttributes.begin();
102 for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
103 clearExpressions(*anAttributeIt);
107 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(const FeaturePtr theStartCoin)
109 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
110 SketchPlugin_Constraint::ENTITY_A());
111 if (aPnt.get() == NULL)
112 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
116 std::set<FeaturePtr> findCoincidentConstraints(const FeaturePtr& theFeature)
118 std::set<FeaturePtr> aCoincident;
119 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
120 std::set<AttributePtr>::const_iterator aIt;
121 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
122 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aIt)->owner());
123 if (aConstrFeature && aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
124 aCoincident.insert(aConstrFeature);
129 void findCoincidences(const FeaturePtr theStartCoin,
130 const std::string& theAttr,
131 std::set<FeaturePtr>& theList,
132 const bool theIsAttrOnly)
134 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
138 FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
139 if(theList.find(aObj) == theList.end()) {
140 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincidencePoint(theStartCoin);
141 if(aOrig.get() == NULL) {
144 if(!theIsAttrOnly || !aPnt->isObject()) {
145 theList.insert(aObj);
147 std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(aObj);
148 std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
149 for (; aCIt != aCoincidences.end(); ++aCIt) {
150 FeaturePtr aConstrFeature = *aCIt;
151 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
152 if(aPnt.get() && aOrig->isEqual(aPnt)) {
153 findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(),
154 theList, theIsAttrOnly);
155 findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(),
156 theList, theIsAttrOnly);
162 std::set<FeaturePtr> findFeaturesCoincidentToPoint(const AttributePoint2DPtr& thePoint)
164 std::set<FeaturePtr> aCoincidentFeatures;
166 FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
167 aCoincidentFeatures.insert(anOwner);
169 std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
170 std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
171 for (; aCIt != aCoincidences.end(); ++aCIt) {
172 bool isPointUsedInCoincidence = false;
173 AttributeRefAttrPtr anOtherCoincidentAttr;
174 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
175 AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
178 if (!aRefAttr->isObject() && aRefAttr->attr() == thePoint)
179 isPointUsedInCoincidence = true;
181 anOtherCoincidentAttr = aRefAttr;
184 if (isPointUsedInCoincidence) {
186 if (anOtherCoincidentAttr->isObject())
187 anObj = anOtherCoincidentAttr->object();
189 anObj = anOtherCoincidentAttr->attr()->owner();
190 aCoincidentFeatures.insert(ModelAPI_Feature::feature(anObj));
194 return aCoincidentFeatures;
197 // Container for point-point coincidences.
198 // Useful to find points coincident to a given point.
199 class CoincidentPoints
202 void addCoincidence(const AttributePoint2DPtr& thePoint1,
203 const AttributePoint2DPtr& thePoint2 = AttributePoint2DPtr())
205 std::list< std::set<AttributePoint2DPtr> >::iterator aFound1 = find(thePoint1);
206 std::list< std::set<AttributePoint2DPtr> >::iterator aFound2 = find(thePoint2);
207 if (aFound1 == myCoincidentPoints.end()) {
208 if (aFound2 == myCoincidentPoints.end()) {
209 std::set<AttributePoint2DPtr> aNewSet;
210 aNewSet.insert(thePoint1);
212 aNewSet.insert(thePoint2);
213 myCoincidentPoints.push_back(aNewSet);
215 aFound2->insert(thePoint1);
216 } else if (aFound2 == myCoincidentPoints.end()) {
218 aFound1->insert(thePoint2);
220 aFound1->insert(aFound2->begin(), aFound2->end());
221 myCoincidentPoints.erase(aFound2);
225 std::set<AttributePoint2DPtr> coincidentPoints(const AttributePoint2DPtr& thePoint)
227 collectCoincidentPoints(thePoint);
229 std::list< std::set<AttributePoint2DPtr> >::iterator aFound = find(thePoint);
230 if (aFound == myCoincidentPoints.end())
231 return std::set<AttributePoint2DPtr>();
236 void coincidences(const FeaturePtr& theFeature,
237 std::set<FeaturePtr>& theCoincidences) const
239 // iterate through coincideces for the given feature
240 std::set<FeaturePtr> aCoincidences = SketchPlugin_Tools::findCoincidentConstraints(theFeature);
241 std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
242 for (; aCIt != aCoincidences.end(); ++aCIt)
244 if (theCoincidences.find(*aCIt) != theCoincidences.end())
245 continue; // already processed
246 theCoincidences.insert(*aCIt);
247 // iterate on coincident attributes
248 for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
249 AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
250 if (aRefAttr && !aRefAttr->isObject())
252 FeaturePtr anOwner = ModelAPI_Feature::feature(aRefAttr->attr()->owner());
253 if (anOwner != theFeature)
254 coincidences(anOwner, theCoincidences);
260 // Iteratively search points coincident to the given point
261 // (two points may be coincident through the third point)
262 void collectCoincidentPoints(const AttributePoint2DPtr& thePoint)
264 AttributePoint2DPtr aPoints[2];
266 FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
267 std::set<FeaturePtr> aCoincidences;
268 coincidences(anOwner, aCoincidences);
270 std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
271 for (; aCIt != aCoincidences.end(); ++aCIt) {
272 aPoints[0] = AttributePoint2DPtr();
273 aPoints[1] = AttributePoint2DPtr();
274 for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
275 AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
276 if (aRefAttr && !aRefAttr->isObject())
277 aPoints[aPtInd++] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
280 if (aPoints[0] && aPoints[1])
281 addCoincidence(aPoints[0], aPoints[1]);
285 std::list< std::set<AttributePoint2DPtr> >::iterator find(const AttributePoint2DPtr& thePoint)
287 std::list< std::set<AttributePoint2DPtr> >::iterator aSeek = myCoincidentPoints.begin();
288 for (; aSeek != myCoincidentPoints.end(); ++aSeek)
289 if (aSeek->find(thePoint) != aSeek->end())
291 return myCoincidentPoints.end();
295 std::list< std::set<AttributePoint2DPtr> > myCoincidentPoints;
298 std::set<AttributePoint2DPtr> findPointsCoincidentToPoint(const AttributePoint2DPtr& thePoint)
300 CoincidentPoints aCoincidentPoints;
301 return aCoincidentPoints.coincidentPoints(thePoint);
304 void resetAttribute(SketchPlugin_Feature* theFeature,
305 const std::string& theId)
307 AttributePtr anAttr = theFeature->attribute(theId);
313 void createCoincidenceOrTangency(SketchPlugin_Feature* theFeature,
314 const std::string& theId,
315 const AttributePtr theAttr,
316 const ObjectPtr theObject,
317 const bool theIsCanBeTangent)
319 AttributeRefAttrPtr aRefAttr = theFeature->refattr(theId);
320 if(aRefAttr.get() && aRefAttr->isInitialized()) {
321 FeaturePtr aConstraint;
322 if(!theIsCanBeTangent) {
323 aConstraint = theFeature->sketch()
324 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
326 if(aRefAttr->isObject()) {
327 ObjectPtr anObject = aRefAttr->object();
328 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
329 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
330 aConstraint = theFeature->sketch()
331 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
333 aConstraint = theFeature->sketch()
334 ->addFeature(SketchPlugin_ConstraintTangent::ID());
337 aConstraint = theFeature->sketch()
338 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
341 AttributeRefAttrPtr aRefAttrA = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
342 aRefAttr->isObject() ? aRefAttrA->setObject(aRefAttr->object())
343 : aRefAttrA->setAttr(aRefAttr->attr());
344 AttributeRefAttrPtr aRefAttrB = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
345 if(theObject.get()) {
346 aRefAttrB->setObject(theObject);
347 } else if(theAttr.get()) {
348 aRefAttrB->setAttr(theAttr);
353 void convertRefAttrToPointOrTangentCurve(const AttributeRefAttrPtr& theRefAttr,
354 const AttributePtr& theDefaultAttr,
355 std::shared_ptr<GeomAPI_Shape>& theTangentCurve,
356 std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
358 AttributePtr anAttr = theDefaultAttr;
359 if (theRefAttr->isObject()) {
360 FeaturePtr aTgFeature = ModelAPI_Feature::feature(theRefAttr->object());
362 if (aTgFeature->getKind() != SketchPlugin_Point::ID()) {
363 theTangentCurve = aTgFeature->lastResult()->shape();
366 anAttr = aTgFeature->attribute(SketchPlugin_Point::COORD_ID());
369 anAttr = theRefAttr->attr();
371 thePassingPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr)->pnt();
375 FeaturePtr createConstraintAttrAttr(SketchPlugin_Sketch* theSketch,
376 const std::string& theConstraintId,
377 const AttributePtr& theFirstAttribute,
378 const AttributePtr& theSecondAttribute)
380 FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
381 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
382 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
383 aRefAttr->setAttr(theFirstAttribute);
385 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
386 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
387 aRefAttr->setAttr(theSecondAttribute);
389 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
390 std::cout << "<createConstraint to attribute> :"
391 << " first attribute - " << theFirstAttribute->id()
392 << " second attribute - " << theSecondAttribute->id()
399 FeaturePtr createConstraintAttrObject(SketchPlugin_Sketch* theSketch,
400 const std::string& theConstraintId,
401 const AttributePtr& theFirstAttribute,
402 const ObjectPtr& theSecondObject)
404 FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
405 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
406 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
407 aRefAttr->setAttr(theFirstAttribute);
409 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
410 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
411 aRefAttr->setObject(theSecondObject);
413 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
414 std::cout << "<createConstraint to attribute> :"
415 << " first attribute - " << theFirstAttribute->id()
416 << " second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
423 FeaturePtr createConstraintObjectObject(SketchPlugin_Sketch* theSketch,
424 const std::string& theConstraintId,
425 const ObjectPtr& theFirstObject,
426 const ObjectPtr& theSecondObject)
428 FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
429 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
430 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
431 aRefAttr->setObject(theFirstObject);
433 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
434 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
435 aRefAttr->setObject(theSecondObject);
437 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
438 std::cout << "<createConstraint to attribute> :"
439 << " first object - " << ModelAPI_Feature::feature(theFirstObject)->getKind()
440 << " second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
447 void createAuxiliaryPointOnEllipse(const FeaturePtr& theEllipseFeature,
448 const std::string& theEllipsePoint)
450 SketchPlugin_Sketch* aSketch =
451 std::dynamic_pointer_cast<SketchPlugin_Feature>(theEllipseFeature)->sketch();
453 FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
454 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
455 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theEllipseFeature);
457 AttributePoint2DPtr anElPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
458 theEllipseFeature->attribute(theEllipsePoint));
460 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
461 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
462 aCoord->setValue(anElPoint->x(), anElPoint->y());
464 aPointFeature->execute();
465 std::string aName = theEllipseFeature->name() + "_" + theEllipsePoint;
466 aPointFeature->data()->setName(aName);
467 aPointFeature->lastResult()->data()->setName(aName);
469 createConstraintAttrAttr(aSketch,
470 SketchPlugin_ConstraintCoincidenceInternal::ID(), anElPoint, aCoord);
473 void createAuxiliaryAxisOfEllipse(const FeaturePtr& theEllipseFeature,
474 const std::string& theStartPoint,
475 const std::string& theEndPoint)
477 SketchPlugin_Sketch* aSketch =
478 std::dynamic_pointer_cast<SketchPlugin_Feature>(theEllipseFeature)->sketch();
480 FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
481 aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
482 aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theEllipseFeature);
484 AttributePoint2DPtr aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
485 theEllipseFeature->attribute(theStartPoint));
486 AttributePoint2DPtr aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
487 theEllipseFeature->attribute(theEndPoint));
489 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
490 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
491 aLineStart->setValue(aStartPoint->x(), aStartPoint->y());
493 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
494 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
495 aLineEnd->setValue(aEndPoint->x(), aEndPoint->y());
497 aLineFeature->execute();
498 std::string aName = theEllipseFeature->name() + "_" +
499 (theStartPoint == SketchPlugin_Ellipse::MAJOR_AXIS_START_ID() ? "major_axis" : "minor_axis");
500 aLineFeature->data()->setName(aName);
501 aLineFeature->lastResult()->data()->setName(aName);
503 createConstraintAttrAttr(aSketch,
504 SketchPlugin_ConstraintCoincidenceInternal::ID(), aStartPoint, aLineStart);
505 createConstraintAttrAttr(aSketch,
506 SketchPlugin_ConstraintCoincidenceInternal::ID(), aEndPoint, aLineEnd);
509 GeomPnt2dPtr flyoutPointCoordinates(const ConstraintPtr& theConstraint)
511 // currently process Length constraints only
512 if (theConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
513 return GeomPnt2dPtr();
515 AttributeRefAttrPtr aLineAttr = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
516 if (!aLineAttr || !aLineAttr->isObject())
517 return GeomPnt2dPtr();
518 FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
519 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
520 return GeomPnt2dPtr();
522 std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
523 aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
524 std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
525 aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
527 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
528 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
529 theConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
530 std::shared_ptr<GeomAPI_Pnt2d> aFltPnt = aFlyoutAttr->pnt();
532 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
534 double X = aStartPnt->x() + aFltPnt->x() * aLineDir->x() - aFltPnt->y() * aLineDir->y();
535 double Y = aStartPnt->y() + aFltPnt->x() * aLineDir->y() + aFltPnt->y() * aLineDir->x();
537 return GeomPnt2dPtr(new GeomAPI_Pnt2d(X, Y));
541 void customizeFeaturePrs(const AISObjectPtr& thePrs, bool isAxiliary)
543 std::vector<int> aColor;
546 thePrs->setLineStyle(SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY());
547 aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
548 aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
551 thePrs->setLineStyle(SketchPlugin_SketchEntity::SKETCH_LINE_STYLE());
552 aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
553 aWidth = Config_PropManager::integer("Visualization", "sketch_line_width");
555 thePrs->setWidth(aWidth);
556 thePrs->setColor(aColor[0], aColor[1], aColor[2]);
559 void setDimensionColor(const AISObjectPtr& theDimPrs)
561 std::vector<int> aColor = Config_PropManager::color("Visualization", "sketch_dimension_color");
562 if (aColor.size() == 3)
563 theDimPrs->setColor(aColor[0], aColor[1], aColor[2]);
566 } // namespace SketchPlugin_Tools
569 // =================================================================================================
570 // namespace SketchPlugin_SegmentationTools
571 // =================================================================================================
573 void SketchPlugin_SegmentationTools::getFeaturePoints(const FeaturePtr& theFeature,
574 AttributePoint2DPtr& theStartPointAttr,
575 AttributePoint2DPtr& theEndPointAttr)
577 std::string aFeatureKind = theFeature->getKind();
578 std::string aStartAttributeName, anEndAttributeName;
579 if (aFeatureKind == SketchPlugin_Line::ID()) {
580 aStartAttributeName = SketchPlugin_Line::START_ID();
581 anEndAttributeName = SketchPlugin_Line::END_ID();
583 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
584 aStartAttributeName = SketchPlugin_Arc::START_ID();
585 anEndAttributeName = SketchPlugin_Arc::END_ID();
587 else if (aFeatureKind == SketchPlugin_EllipticArc::ID()) {
588 aStartAttributeName = SketchPlugin_EllipticArc::START_POINT_ID();
589 anEndAttributeName = SketchPlugin_EllipticArc::END_POINT_ID();
591 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
592 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
593 theFeature->attribute(aStartAttributeName));
594 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
595 theFeature->attribute(anEndAttributeName));
600 void SketchPlugin_SegmentationTools::getRefAttributes(
601 const FeaturePtr& theFeature,
602 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
603 std::list<AttributePtr>& theRefsToFeature)
607 std::list<AttributePtr> aPointAttributes =
608 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
609 std::set<AttributePtr> aPointAttributesSet;
611 std::list<AttributePtr>::const_iterator aPIt =
612 aPointAttributes.begin(), aPLast = aPointAttributes.end();
613 for (; aPIt != aPLast; aPIt++)
614 aPointAttributesSet.insert(*aPIt);
616 std::set<AttributePtr> aRefsAttributes = theFeature->lastResult()->data()->refsToMe();
617 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
618 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
620 std::set<AttributePtr>::const_iterator aIt;
621 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
622 AttributePtr anAttr = (*aIt);
623 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
624 if (!anAttrFeature->isMacro() && // <- skip reference from Trim or Split feature
625 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
626 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
627 if (!aRefAttr->isObject()) { // find attributes referenced to feature point attributes
628 AttributePtr anAttrInRef = aRefAttr->attr();
629 if (anAttrInRef.get() &&
630 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
631 if (theRefs.find(anAttrInRef) != theRefs.end())
632 theRefs[anAttrInRef].push_back(aRefAttr);
634 std::list<AttributePtr> anAttrList;
635 anAttrList.push_back(aRefAttr);
636 theRefs[anAttrInRef] = anAttrList;
640 else { // find attributes referenced to feature itself
641 theRefsToFeature.push_back(anAttr);
647 GeomShapePtr SketchPlugin_SegmentationTools::getSubShape(
648 SketchPlugin_Feature* theFeature,
649 const std::string& theObjectAttributeId,
650 const std::string& thePointAttributeId,
651 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
652 std::map<ObjectPtr, GeomAlgoAPI_ShapeTools::PointToRefsMap>& theObjectToPoints)
654 GeomShapePtr aBaseShape;
656 AttributeReferencePtr anObjectAttr = theFeature->reference(theObjectAttributeId);
657 ObjectPtr aBaseObject = anObjectAttr->value();
658 if (!aBaseObject.get())
662 AttributePoint2DPtr aPointAttr =
663 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(thePointAttributeId));
664 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
665 std::shared_ptr<GeomAPI_Pnt> anAttributePnt =
666 theFeature->sketch()->to3D(anAttributePnt2d->x(), anAttributePnt2d->y());
668 if (theCashedShapes.find(aBaseObject) == theCashedShapes.end())
669 fillObjectShapes(theFeature, aBaseObject, theCashedShapes, theObjectToPoints);
671 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
672 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
673 const std::set<GeomShapePtr>& aShapes = theCashedShapes[aBaseObject];
674 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
675 for (; anIt != aLast; anIt++) {
676 GeomShapePtr aCurrentShape = *anIt;
677 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
678 if (ModelGeomAlgo_Point2D::isPointOnEdge(aCurrentShape, anAttributePnt, aProjectedPoint)) {
679 if (theFeature->getKind() == SketchPlugin_Split::ID()) {
680 // for Split operation collect start and end points of the shape
681 if (aCurrentShape->shapeType() == GeomAPI_Shape::EDGE) {
682 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aCurrentShape));
683 aStartPoint = anEdge->firstPoint();
684 aSecondPoint = anEdge->lastPoint();
688 aBaseShape = aCurrentShape;
693 if (!aStartPoint.get() || !aSecondPoint.get())
696 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
697 if (anObjectAttr->isInitialized() && aBaseFeature.get() && aPointAttr->isInitialized()) {
698 ResultPtr aResult = aBaseFeature->lastResult();
699 GeomShapePtr aResultShape = aResult->shape();
700 std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
702 aPoints.push_back(aStartPoint);
703 aPoints.push_back(aSecondPoint);
705 std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
706 GeomAlgoAPI_ShapeTools::splitShape_p(aResultShape, aPoints, aSplitShapes);
707 aBaseShape = GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
712 void SketchPlugin_SegmentationTools::fillObjectShapes(
713 SketchPlugin_Feature* theOpFeature,
714 const ObjectPtr& theObject,
715 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
716 std::map<ObjectPtr, GeomAlgoAPI_ShapeTools::PointToRefsMap>& theObjectToPoints)
718 SketchPlugin_Sketch* aSketch = theOpFeature->sketch();
720 GeomAlgoAPI_ShapeTools::PointToRefsMap aPoints;
721 std::set<GeomShapePtr> aShapes;
723 std::set<AttributePoint2DPtr > aRefAttributes;
725 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
726 std::set<ResultPtr> anEdgeShapes;
728 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
729 if (!anEdgeShapes.empty()) {
730 GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
732 // coincidences to the feature
733 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
734 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
735 // layed on feature coincidences to divide it on several shapes
736 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
737 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
738 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
739 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
740 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
741 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
742 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
743 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
745 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
746 aX->dir(), aY, aPoints);
748 if (theOpFeature->getKind() == SketchPlugin_Trim::ID()) {
749 // collect all intersection points with other edges for Trim operation only
750 std::list<FeaturePtr> aFeatures;
751 for (int i = 0; i < aSketch->numberOfSubs(); i++) {
752 FeaturePtr aFeature = aSketch->subFeature(i);
753 if (aFeature.get() && aFeature->getKind() != SketchPlugin_Projection::ID())
754 aFeatures.push_back(aFeature);
756 ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPoints);
759 if (!aPoints.empty())
760 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPoints, aShapes);
762 theObjectToPoints[theObject] = aPoints;
763 theCashedShapes[theObject] = aShapes;
766 void SketchPlugin_SegmentationTools::updateRefAttConstraints(
767 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
768 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes)
770 #if defined DEBUG_SPLIT || defined DEBUG_TRIM
771 std::cout << "updateRefAttConstraints" << std::endl;
774 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
775 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
776 for (; anIt != aLast; anIt++) {
777 AttributePtr anAttribute = anIt->first;
778 AttributePtr aNewAttribute = anIt->second;
780 // not found in references
781 if (!aNewAttribute.get() ||
782 theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
784 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
785 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
786 aRLast = aRefAttributes.end();
788 for (; aRefIt != aRLast; aRefIt++) {
789 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
790 if (aRefAttr.get()) {
791 aRefAttr->setAttr(aNewAttribute);
793 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->owner());
794 std::cout << " -" << getFeatureInfo(aFeature) << std::endl;
801 void SketchPlugin_SegmentationTools::updateFeaturesAfterOperation(
802 const std::set<FeaturePtr>& theFeaturesToUpdate)
804 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
805 aLast = theFeaturesToUpdate.end();
806 for (; anIt != aLast; anIt++) {
807 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
808 std::string aRefFeatureKind = aRefFeature->getKind();
809 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
810 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
811 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
812 if (aLenghtFeature.get()) {
813 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
814 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
816 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
817 aValueAttr->setValue(aValue);
823 AISObjectPtr SketchPlugin_SegmentationTools::getAISObject(
824 AISObjectPtr thePrevious,
825 SketchPlugin_Feature* theOpFeature,
826 const std::string& thePreviewObjectAttrName,
827 const std::string& thePreviewPointAttrName,
828 const std::string& theSelectedObjectAttrName,
829 const std::string& theSelectedPointAttrName)
831 #if defined DEBUG_SPLIT || defined DEBUG_TRIM_METHODS
832 std::cout << "getAISObject: " << theOpFeature->data()->name() << std::endl;
835 AISObjectPtr anAIS = thePrevious;
837 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
838 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
839 std::map<ObjectPtr, GeomAlgoAPI_ShapeTools::PointToRefsMap> aObjectToPoints;
840 GeomShapePtr aPreviewShape = getSubShape(theOpFeature,
841 thePreviewObjectAttrName, thePreviewPointAttrName, aCashedShapes, aObjectToPoints);
842 if (aPreviewShape.get())
843 aShapes.push_back(aPreviewShape);
844 GeomShapePtr aSelectedShape = getSubShape(theOpFeature,
845 theSelectedObjectAttrName, theSelectedPointAttrName, aCashedShapes, aObjectToPoints);
846 if (aSelectedShape.get())
847 aShapes.push_back(aSelectedShape);
850 return AISObjectPtr();
852 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
853 if (!aBaseShape.get())
854 return AISObjectPtr();
856 if (aBaseShape.get()) {
858 anAIS = AISObjectPtr(new GeomAPI_AISObject);
859 anAIS->createShape(aBaseShape);
861 std::vector<int> aColor;
862 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
863 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
864 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
865 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
866 // width when there is not base object should be extened in several points
867 // in order to see this preview over highlight
868 anAIS->setWidth(aWidth+4);
869 anAIS->setLineStyle(aLineStyle);
872 anAIS = AISObjectPtr();
876 #define GEOM_DATA_POINT2D(f, a) std::dynamic_pointer_cast<GeomDataAPI_Point2D>((f)->attribute(a))
878 FeaturePtr SketchPlugin_SegmentationTools::createLineFeature(
879 const FeaturePtr& theBaseFeature,
880 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
881 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
884 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
885 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBaseFeature);
886 SketchPlugin_Sketch* aSketch = aSketchFeature->sketch();
887 if (!aSketch || !theBaseFeature.get())
890 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
892 GEOM_DATA_POINT2D(aFeature, SketchPlugin_Line::START_ID())->setValue(theFirstPoint);
893 GEOM_DATA_POINT2D(aFeature, SketchPlugin_Line::END_ID())->setValue(theSecondPoint);
895 aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(
896 theBaseFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value());
897 aFeature->execute(); // to obtain result
905 std::string myCenter;
909 std::string myReversed;
913 ArcAttributes(const std::string& theKind) : myKind(theKind)
915 if (myKind == SketchPlugin_Arc::ID()) {
916 myCenter = SketchPlugin_Arc::CENTER_ID();
917 myStart = SketchPlugin_Arc::START_ID();
918 myEnd = SketchPlugin_Arc::END_ID();
919 myReversed = SketchPlugin_Arc::REVERSED_ID();
921 else if (myKind == SketchPlugin_Circle::ID()) {
922 myCenter = SketchPlugin_Circle::CENTER_ID();
924 else if (myKind == SketchPlugin_Ellipse::ID()) {
925 myCenter = SketchPlugin_Ellipse::CENTER_ID();
926 myFocus = SketchPlugin_Ellipse::FIRST_FOCUS_ID();
928 else if (myKind == SketchPlugin_EllipticArc::ID()) {
929 myCenter = SketchPlugin_EllipticArc::CENTER_ID();
930 myFocus = SketchPlugin_EllipticArc::FIRST_FOCUS_ID();
931 myStart = SketchPlugin_EllipticArc::START_POINT_ID();
932 myEnd = SketchPlugin_EllipticArc::END_POINT_ID();
933 myReversed = SketchPlugin_EllipticArc::REVERSED_ID();
938 FeaturePtr SketchPlugin_SegmentationTools::createArcFeature(
939 const FeaturePtr& theBaseFeature,
940 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
941 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
944 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
945 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBaseFeature);
946 SketchPlugin_Sketch* aSketch = aSketchFeature->sketch();
947 if (!aSketch || !theBaseFeature.get())
950 ArcAttributes aBaseAttrs(theBaseFeature->getKind());
951 ArcAttributes aTargetAttrs;
952 if (aBaseAttrs.myKind == SketchPlugin_Arc::ID() ||
953 aBaseAttrs.myKind == SketchPlugin_Circle::ID())
954 aTargetAttrs = ArcAttributes(SketchPlugin_Arc::ID());
955 else if (aBaseAttrs.myKind == SketchPlugin_Ellipse::ID() ||
956 aBaseAttrs.myKind == SketchPlugin_EllipticArc::ID())
957 aTargetAttrs = ArcAttributes(SketchPlugin_EllipticArc::ID());
959 if (aTargetAttrs.myKind.empty())
962 aFeature = aSketch->addFeature(aTargetAttrs.myKind);
963 // update fillet arc: make the arc correct for sure, so, it is not needed to process
964 // the "attribute updated"
965 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
966 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
968 GEOM_DATA_POINT2D(aFeature, aTargetAttrs.myCenter)->setValue(
969 GEOM_DATA_POINT2D(theBaseFeature, aBaseAttrs.myCenter)->pnt());
970 if (!aTargetAttrs.myFocus.empty()) {
971 GEOM_DATA_POINT2D(aFeature, aTargetAttrs.myFocus)->setValue(
972 GEOM_DATA_POINT2D(theBaseFeature, aBaseAttrs.myFocus)->pnt());
974 GEOM_DATA_POINT2D(aFeature, aTargetAttrs.myStart)->setValue(theFirstPoint);
975 GEOM_DATA_POINT2D(aFeature, aTargetAttrs.myEnd)->setValue(theSecondPoint);
977 aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(
978 theBaseFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value());
980 /// fill referersed state of created arc as it is on the base arc
981 bool aReversed = aBaseAttrs.myReversed.empty() ? false :
982 theBaseFeature->boolean(aBaseAttrs.myReversed)->value();
983 aFeature->boolean(aTargetAttrs.myReversed)->setValue(aReversed);
985 aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
986 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);