1 // Copyright (C) 2020 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_Offset.h>
22 #include <SketchPlugin_Sketch.h>
23 #include <SketchPlugin_Line.h>
24 #include <SketchPlugin_Point.h>
25 #include <SketchPlugin_Arc.h>
26 #include <SketchPlugin_Circle.h>
27 #include <SketchPlugin_Ellipse.h>
28 #include <SketchPlugin_EllipticArc.h>
29 #include <SketchPlugin_BSpline.h>
30 #include <SketchPlugin_BSplinePeriodic.h>
31 #include <SketchPlugin_Tools.h>
33 #include <SketcherPrs_Factory.h>
35 #include <Events_InfoMessage.h>
37 #include <ModelAPI_AttributeBoolean.h>
38 #include <ModelAPI_AttributeDouble.h>
39 #include <ModelAPI_AttributeDoubleArray.h>
40 #include <ModelAPI_AttributeInteger.h>
41 #include <ModelAPI_AttributeIntArray.h>
42 #include <ModelAPI_AttributeRefList.h>
43 #include <ModelAPI_Events.h>
44 #include <ModelAPI_ResultConstruction.h>
45 #include <ModelAPI_Tools.h>
46 #include <ModelAPI_Validator.h>
48 #include <GeomAlgoAPI_MakeShapeList.h>
49 #include <GeomAlgoAPI_Offset.h>
50 #include <GeomAlgoAPI_ShapeTools.h>
51 #include <GeomAlgoAPI_WireBuilder.h>
53 #include <GeomAPI_BSpline.h>
54 #include <GeomAPI_Circ.h>
55 #include <GeomAPI_Edge.h>
56 #include <GeomAPI_Ellipse.h>
57 #include <GeomAPI_ShapeExplorer.h>
58 #include <GeomAPI_Wire.h>
59 #include <GeomAPI_WireExplorer.h>
61 #include <GeomDataAPI_Point2D.h>
62 #include <GeomDataAPI_Point2DArray.h>
64 static const double tolerance = 1.e-7;
66 SketchPlugin_Offset::SketchPlugin_Offset()
70 void SketchPlugin_Offset::initAttributes()
72 data()->addAttribute(EDGES_ID(), ModelAPI_AttributeRefList::typeId());
73 data()->addAttribute(VALUE_ID(), ModelAPI_AttributeDouble::typeId());
74 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
76 // store original entities
77 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
78 // store offset entities
79 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
80 // store mapping between original entity and index of the corresponding offset entity
81 data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeIntArray::typeId());
83 ModelAPI_Session::get()->validators()->
84 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
85 ModelAPI_Session::get()->validators()->
86 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
87 ModelAPI_Session::get()->validators()->
88 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
91 void SketchPlugin_Offset::execute()
93 SketchPlugin_Sketch* aSketch = sketch();
97 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
100 AttributeDoublePtr aValueAttr = real(VALUE_ID());
101 if (!aValueAttr->isInitialized()) return;
102 double aValue = aValueAttr->value();
103 if (aValue < tolerance) return;
106 AttributeBooleanPtr aReversedAttr = boolean(REVERSED_ID());
107 if (!aReversedAttr->isInitialized()) return;
108 if (aReversedAttr->value()) aValue = -aValue; // reverse offset direction
110 // 3. List of all selected edges
111 AttributeRefListPtr aSelectedEdges = reflist(EDGES_ID());
112 std::list<ObjectPtr> anEdgesList = aSelectedEdges->list();
114 // 4. Put all selected edges in a set to pass them into findWireOneWay() below
115 std::set<FeaturePtr> anEdgesSet;
116 std::list<ObjectPtr>::const_iterator anEdgesIt = anEdgesList.begin();
117 for (; anEdgesIt != anEdgesList.end(); anEdgesIt++) {
118 FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt);
120 anEdgesSet.insert(aFeature);
124 // Wait all objects being created, then send update events
125 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
126 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
128 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
130 // 5. Gather wires and make offset for each wire
131 ListOfMakeShape anOffsetAlgos;
132 std::set<FeaturePtr> aProcessedEdgesSet;
133 for (anEdgesIt = anEdgesList.begin(); anEdgesIt != anEdgesList.end(); anEdgesIt++) {
134 FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt);
135 if (aFeature.get()) {
136 if (aProcessedEdgesSet.find(aFeature) != aProcessedEdgesSet.end())
139 // 5.a. End points (if any)
140 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
141 SketchPlugin_SegmentationTools::getFeaturePoints(aFeature, aStartPoint, anEndPoint);
143 // 5.b. Find a chain of edges
144 std::list<FeaturePtr> aChain;
145 aChain.push_back(aFeature);
146 bool isClosed = !(aStartPoint && anEndPoint); // not closed edge
148 isClosed = findWireOneWay(aFeature, aFeature, aStartPoint, anEdgesSet,
149 aProcessedEdgesSet, aChain, true);
151 isClosed = findWireOneWay(aFeature, aFeature, anEndPoint, anEdgesSet,
152 aProcessedEdgesSet, aChain, false);
155 aProcessedEdgesSet.insert(aFeature);
158 ListOfShape aTopoChain;
159 std::list<FeaturePtr>::iterator aChainIt = aChain.begin();
160 for (; aChainIt != aChain.end(); ++aChainIt) {
161 FeaturePtr aChainFeature = (*aChainIt);
162 GeomShapePtr aTopoEdge = aChainFeature->lastResult()->shape();
163 if (aTopoEdge->shapeType() == GeomAPI_Shape::EDGE) {
164 aTopoChain.push_back(aTopoEdge);
167 std::shared_ptr<GeomAlgoAPI_WireBuilder> aWireBuilder(
168 new GeomAlgoAPI_WireBuilder(aTopoChain, !isClosed));
170 GeomShapePtr aWireShape = aWireBuilder->shape();
171 GeomWirePtr aWire (new GeomAPI_Wire (aWireShape));
173 // Fix for a problem of offset side change with selection change.
174 // Wire direction is defined by the first selected edge of this wire.
176 if (!aWire->isClosed()) {
177 ListOfShape aModified;
178 // First selected edge of current chain
179 GeomShapePtr aFirstSel = aFeature->lastResult()->shape();
180 aWireBuilder->modified(aFirstSel, aModified);
181 GeomShapePtr aModFS = aModified.front();
182 if (aModFS->orientation() != aFirstSel->orientation())
186 // 5.d. Make offset for the wire
187 std::shared_ptr<GeomAlgoAPI_Offset> anOffsetShape(
188 new GeomAlgoAPI_Offset(aPlane, aWireShape, aValue*aSign));
190 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList(new GeomAlgoAPI_MakeShapeList);
191 aMakeList->appendAlgo(aWireBuilder);
192 aMakeList->appendAlgo(anOffsetShape);
193 anOffsetAlgos.push_back(aMakeList);
197 // 6. Store offset results.
198 // Create sketch feature for each edge of anOffsetShape, and also store
199 // created features in CREATED_ID() to remove them on next execute()
200 addToSketch(anOffsetAlgos);
202 // send events to update the sub-features by the solver
204 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
207 bool SketchPlugin_Offset::findWireOneWay (const FeaturePtr& theFirstEdge,
208 const FeaturePtr& theEdge,
209 const std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint,
210 std::set<FeaturePtr>& theEdgesSet,
211 std::set<FeaturePtr>& theProcessedEdgesSet,
212 std::list<FeaturePtr>& theChain,
213 const bool isPrepend)
215 // 1. Find a single edge, coincident to theEndPoint by one of its ends
216 if (!theEndPoint) return false;
218 FeaturePtr aNextEdgeFeature;
221 std::set<AttributePoint2DPtr> aCoincPoints =
222 SketchPlugin_Tools::findPointsCoincidentToPoint(theEndPoint);
224 std::set<AttributePoint2DPtr>::iterator aPointsIt = aCoincPoints.begin();
225 for (; aPointsIt != aCoincPoints.end(); aPointsIt++) {
226 AttributePoint2DPtr aP = (*aPointsIt);
227 FeaturePtr aCoincFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aP->owner());
228 bool isInSet = (theEdgesSet.find(aCoincFeature) != theEdgesSet.end());
230 // Condition 0: not auxiliary
231 if (!isInSet && aCoincFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
234 // Condition 1: not a point feature
235 if (aCoincFeature->getKind() != SketchPlugin_Point::ID()) {
236 // Condition 2: it is not the current edge
237 if (aCoincFeature != theEdge) {
238 // Condition 3: it is in the set of interest.
239 // Empty set means all sketch edges.
240 if (isInSet || theEdgesSet.empty()) {
241 // Condition 4: consider only features with two end points
242 std::shared_ptr<GeomDataAPI_Point2D> aP1, aP2;
243 SketchPlugin_SegmentationTools::getFeaturePoints(aCoincFeature, aP1, aP2);
245 // Condition 5: consider only features, that have aP as one of they ends.
246 // For example, we do not need an arc, coincident to aP by its center.
247 if (theEndPoint->pnt()->isEqual(aP1->pnt()) ||
248 theEndPoint->pnt()->isEqual(aP2->pnt())) {
249 // Condition 6: only one edge can prolongate the chain. If several, we stop here.
255 aNextEdgeFeature = aCoincFeature;
263 // Only one edge can prolongate the chain. If several or none, we stop here.
267 // 2. So, we have the single edge, that prolongate the chain
269 // Condition 7: if we reached the very first edge of the chain
270 if (aNextEdgeFeature == theFirstEdge)
271 // Closed chain found
274 // 3. Add the found edge to the chain
276 theChain.push_front(aNextEdgeFeature);
278 theChain.push_back(aNextEdgeFeature);
279 theProcessedEdgesSet.insert(aNextEdgeFeature);
281 // 4. Which end of aNextEdgeFeature we need to proceed
282 std::shared_ptr<GeomDataAPI_Point2D> aP1, aP2;
283 SketchPlugin_SegmentationTools::getFeaturePoints(aNextEdgeFeature, aP1, aP2);
284 if (aP2->pnt()->isEqual(theEndPoint->pnt())) {
289 // 5. Continue gathering the chain (recursive)
290 return findWireOneWay (theFirstEdge, aNextEdgeFeature, aP2, theEdgesSet,
291 theProcessedEdgesSet, theChain, isPrepend);
294 static void setRefListValue(AttributeRefListPtr theList, int theListSize,
295 ObjectPtr theValue, int theIndex)
297 if (theIndex < theListSize) {
298 ObjectPtr aCur = theList->object(theIndex);
299 if (aCur != theValue)
300 theList->substitute(aCur, theValue);
303 theList->append(theValue);
306 // Reorder shapes according to the wire's order
307 static void reorderShapes(ListOfShape& theShapes, GeomShapePtr theWire)
309 std::set<GeomShapePtr, GeomAPI_Shape::Comparator> aShapes;
310 aShapes.insert(theShapes.begin(), theShapes.end());
313 GeomWirePtr aWire(new GeomAPI_Wire(theWire));
314 GeomAPI_WireExplorer anExp(aWire);
315 for (; anExp.more(); anExp.next()) {
316 GeomShapePtr aCurEdge = anExp.current();
317 auto aFound = aShapes.find(aCurEdge);
318 if (aFound != aShapes.end()) {
319 theShapes.push_back(aCurEdge);
320 aShapes.erase(aFound);
325 static void removeLastFromIndex(AttributeRefListPtr theList, int theListSize, int& theLastIndex)
327 if (theLastIndex < theListSize) {
328 std::set<int> anIndicesToRemove;
329 for (; theLastIndex < theListSize; ++theLastIndex)
330 anIndicesToRemove.insert(theLastIndex);
331 theList->remove(anIndicesToRemove);
335 void SketchPlugin_Offset::addToSketch(const ListOfMakeShape& theOffsetAlgos)
337 AttributeRefListPtr aSelectedRefList = reflist(EDGES_ID());
338 AttributeRefListPtr aBaseRefList = reflist(ENTITY_A());
339 AttributeRefListPtr anOffsetRefList = reflist(ENTITY_B());
340 AttributeIntArrayPtr anOffsetToBaseMap = intArray(ENTITY_C());
342 // compare the list of selected edges and the previously stored,
343 // and store maping between them
344 std::map<ObjectPtr, std::list<ObjectPtr> > aMapExistent;
345 std::list<ObjectPtr> anObjectsToRemove;
346 std::list<ObjectPtr> aSelectedList = aSelectedRefList->list();
347 for (std::list<ObjectPtr>::iterator aSIt = aSelectedList.begin();
348 aSIt != aSelectedList.end(); ++aSIt) {
349 aMapExistent[*aSIt] = std::list<ObjectPtr>();
351 for (int anIndex = 0, aSize = anOffsetRefList->size(); anIndex < aSize; ++anIndex) {
352 ObjectPtr aCurrent = anOffsetRefList->object(anIndex);
353 int aBaseIndex = anOffsetToBaseMap->value(anIndex);
354 if (aBaseIndex >= 0) {
355 ObjectPtr aBaseObj = aBaseRefList->object(aBaseIndex);
356 std::map<ObjectPtr, std::list<ObjectPtr> >::iterator aFound = aMapExistent.find(aBaseObj);
357 if (aFound != aMapExistent.end())
358 aFound->second.push_back(aCurrent);
360 anObjectsToRemove.push_back(aCurrent);
363 anObjectsToRemove.push_back(aCurrent);
366 // update lists of base shapes and of offset shapes
367 int aBaseListSize = aBaseRefList->size();
368 int anOffsetListSize = anOffsetRefList->size();
369 int aBaseListIndex = 0, anOffsetListIndex = 0;
370 std::list<int> anOffsetBaseBackRefs;
371 std::set<GeomShapePtr, GeomAPI_Shape::ComparatorWithOri> aProcessedOffsets;
372 for (std::list<ObjectPtr>::iterator aSIt = aSelectedList.begin();
373 aSIt != aSelectedList.end(); ++aSIt) {
374 // find an offseted edge
375 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(*aSIt);
376 GeomShapePtr aBaseShape = aBaseFeature->lastResult()->shape();
377 ListOfShape aNewShapes;
378 for (ListOfMakeShape::const_iterator anAlgoIt = theOffsetAlgos.begin();
379 anAlgoIt != theOffsetAlgos.end(); ++anAlgoIt) {
380 (*anAlgoIt)->generated(aBaseShape, aNewShapes);
381 if (!aNewShapes.empty()) {
382 reorderShapes(aNewShapes, (*anAlgoIt)->shape());
387 // store base feature
388 setRefListValue(aBaseRefList, aBaseListSize, *aSIt, aBaseListIndex);
390 // create or update an offseted feature
391 const std::list<ObjectPtr>& anImages = aMapExistent[*aSIt];
392 std::list<ObjectPtr>::const_iterator anImgIt = anImages.begin();
393 for (ListOfShape::iterator aNewIt = aNewShapes.begin(); aNewIt != aNewShapes.end(); ++aNewIt) {
394 FeaturePtr aNewFeature;
395 if (anImgIt != anImages.end())
396 aNewFeature = ModelAPI_Feature::feature(*anImgIt++);
397 updateExistentOrCreateNew(*aNewIt, aNewFeature, anObjectsToRemove);
398 aProcessedOffsets.insert(*aNewIt);
400 // store an offseted feature
401 setRefListValue(anOffsetRefList, anOffsetListSize, aNewFeature, anOffsetListIndex);
403 anOffsetBaseBackRefs.push_back(aBaseListIndex);
407 anObjectsToRemove.insert(anObjectsToRemove.end(), anImgIt, anImages.end());
409 // create arcs generated from vertices
410 for (ListOfMakeShape::const_iterator anAlgoIt = theOffsetAlgos.begin();
411 anAlgoIt != theOffsetAlgos.end(); ++anAlgoIt) {
412 GeomShapePtr aCurWire = (*anAlgoIt)->shape();
413 GeomAPI_ShapeExplorer anExp(aCurWire, GeomAPI_Shape::EDGE);
414 for (; anExp.more(); anExp.next()) {
415 GeomShapePtr aCurEdge = anExp.current();
416 if (aProcessedOffsets.find(aCurEdge) == aProcessedOffsets.end()) {
417 FeaturePtr aNewFeature;
418 updateExistentOrCreateNew(aCurEdge, aNewFeature, anObjectsToRemove);
419 aProcessedOffsets.insert(aCurEdge);
421 // store an offseted feature
422 setRefListValue(anOffsetRefList, anOffsetListSize, aNewFeature, anOffsetListIndex);
424 anOffsetBaseBackRefs.push_back(-1);
430 removeLastFromIndex(aBaseRefList, aBaseListSize, aBaseListIndex);
431 removeLastFromIndex(anOffsetRefList, anOffsetListSize, anOffsetListIndex);
433 anOffsetToBaseMap->setSize((int)anOffsetBaseBackRefs.size(), false);
435 for (std::list<int>::iterator anIt = anOffsetBaseBackRefs.begin();
436 anIt != anOffsetBaseBackRefs.end(); ++anIt) {
437 anOffsetToBaseMap->setValue(anIndex++, *anIt, false);
440 // remove unused objects
441 std::set<FeaturePtr> aSet;
442 for (std::list<ObjectPtr>::iterator anIt = anObjectsToRemove.begin();
443 anIt != anObjectsToRemove.end(); ++anIt) {
444 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
446 aSet.insert(aFeature);
448 ModelAPI_Tools::removeFeaturesAndReferences(aSet);
451 static void findOrCreateFeatureByKind(SketchPlugin_Sketch* theSketch,
452 const std::string& theFeatureKind,
453 FeaturePtr& theFeature,
454 std::list<ObjectPtr>& thePoolOfFeatures)
457 // check the feature type is the same as required
458 if (theFeature->getKind() != theFeatureKind) {
459 // return feature to the pool and try to find the most appropriate
460 thePoolOfFeatures.push_back(theFeature);
461 theFeature = FeaturePtr();
465 // try to find appropriate feature in the pool
466 for (std::list<ObjectPtr>::iterator it = thePoolOfFeatures.begin();
467 it != thePoolOfFeatures.end(); ++it) {
468 FeaturePtr aCurFeature = ModelAPI_Feature::feature(*it);
469 if (aCurFeature->getKind() == theFeatureKind) {
470 theFeature = aCurFeature;
471 thePoolOfFeatures.erase(it);
475 // feature not found, create new
477 theFeature = theSketch->addFeature(theFeatureKind);
481 void SketchPlugin_Offset::updateExistentOrCreateNew(const GeomShapePtr& theShape,
482 FeaturePtr& theFeature,
483 std::list<ObjectPtr>& thePoolOfFeatures)
485 if (theShape->shapeType() != GeomAPI_Shape::EDGE)
488 std::shared_ptr<GeomAPI_Edge> aResEdge(new GeomAPI_Edge(theShape));
490 std::shared_ptr<GeomAPI_Pnt2d> aFP, aLP;
491 std::shared_ptr<GeomAPI_Pnt> aFP3d = aResEdge->firstPoint();
492 std::shared_ptr<GeomAPI_Pnt> aLP3d = aResEdge->lastPoint();
493 if (aFP3d && aLP3d) {
494 aFP = sketch()->to2D(aFP3d);
495 aLP = sketch()->to2D(aLP3d);
498 if (aResEdge->isLine()) {
499 findOrCreateFeatureByKind(sketch(), SketchPlugin_Line::ID(), theFeature, thePoolOfFeatures);
501 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
502 (theFeature->attribute(SketchPlugin_Line::START_ID()))->setValue(aFP);
503 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
504 (theFeature->attribute(SketchPlugin_Line::END_ID()))->setValue(aLP);
506 else if (aResEdge->isArc()) {
507 std::shared_ptr<GeomAPI_Circ> aCircEdge = aResEdge->circle();
508 std::shared_ptr<GeomAPI_Pnt> aCP3d = aCircEdge->center();
509 std::shared_ptr<GeomAPI_Pnt2d> aCP = sketch()->to2D(aCP3d);
511 findOrCreateFeatureByKind(sketch(), SketchPlugin_Arc::ID(), theFeature, thePoolOfFeatures);
513 GeomDirPtr aCircNormal = aCircEdge->normal();
514 GeomDirPtr aSketchNormal = sketch()->coordinatePlane()->normal();
515 if (aSketchNormal->dot(aCircNormal) < -tolerance)
518 bool aWasBlocked = theFeature->data()->blockSendAttributeUpdated(true);
519 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
520 (theFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(aLP);
521 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
522 (theFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(aFP);
523 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
524 (theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(aCP);
525 theFeature->data()->blockSendAttributeUpdated(aWasBlocked);
527 else if (aResEdge->isCircle()) {
528 std::shared_ptr<GeomAPI_Circ> aCircEdge = aResEdge->circle();
529 std::shared_ptr<GeomAPI_Pnt> aCP3d = aCircEdge->center();
530 std::shared_ptr<GeomAPI_Pnt2d> aCP = sketch()->to2D(aCP3d);
532 findOrCreateFeatureByKind(sketch(), SketchPlugin_Circle::ID(), theFeature, thePoolOfFeatures);
534 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
535 (theFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->setValue(aCP);
536 theFeature->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aCircEdge->radius());
538 else if (aResEdge->isEllipse()) {
539 std::shared_ptr<GeomAPI_Ellipse> anEllipseEdge = aResEdge->ellipse();
541 GeomPointPtr aCP3d = anEllipseEdge->center();
542 GeomPnt2dPtr aCP = sketch()->to2D(aCP3d);
544 GeomPointPtr aFocus3d = anEllipseEdge->firstFocus();
545 GeomPnt2dPtr aFocus = sketch()->to2D(aFocus3d);
547 if (aFP3d && aLP3d) {
549 findOrCreateFeatureByKind(sketch(), SketchPlugin_EllipticArc::ID(),
550 theFeature, thePoolOfFeatures);
552 bool aWasBlocked = theFeature->data()->blockSendAttributeUpdated(true);
553 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
554 (theFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID()))->setValue(aCP);
555 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
556 (theFeature->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID()))->setValue(aFocus);
557 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
558 (theFeature->attribute(SketchPlugin_EllipticArc::START_POINT_ID()))->setValue(aFP);
559 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
560 (theFeature->attribute(SketchPlugin_EllipticArc::END_POINT_ID()))->setValue(aLP);
561 theFeature->data()->blockSendAttributeUpdated(aWasBlocked);
565 findOrCreateFeatureByKind(sketch(), SketchPlugin_Ellipse::ID(),
566 theFeature, thePoolOfFeatures);
568 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
569 (theFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()))->setValue(aCP);
570 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
571 (theFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()))->setValue(aFocus);
572 theFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(
573 anEllipseEdge->minorRadius());
577 // convert to b-spline
578 mkBSpline(theFeature, aResEdge, thePoolOfFeatures);
581 if (theFeature.get()) {
582 theFeature->boolean(SketchPlugin_SketchEntity::COPY_ID())->setValue(true);
583 theFeature->execute();
585 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
586 ModelAPI_EventCreator::get()->sendUpdated(theFeature, aRedisplayEvent);
587 const std::list<ResultPtr>& aResults = theFeature->results();
588 for (std::list<ResultPtr>::const_iterator anIt = aResults.begin();
589 anIt != aResults.end(); ++anIt)
590 ModelAPI_EventCreator::get()->sendUpdated(*anIt, aRedisplayEvent);
594 void SketchPlugin_Offset::mkBSpline (FeaturePtr& theResult,
595 const GeomEdgePtr& theEdge,
596 std::list<ObjectPtr>& thePoolOfFeatures)
598 GeomCurvePtr aCurve (new GeomAPI_Curve (theEdge));
599 // Forced conversion to b-spline, if aCurve is not b-spline
600 GeomBSplinePtr aBSpline = GeomAPI_BSpline::convertToBSpline(aCurve);
602 const std::string& aBSplineKind = aBSpline->isPeriodic() ? SketchPlugin_BSplinePeriodic::ID()
603 : SketchPlugin_BSpline::ID();
604 findOrCreateFeatureByKind(sketch(), aBSplineKind, theResult, thePoolOfFeatures);
606 theResult->integer(SketchPlugin_BSpline::DEGREE_ID())->setValue(aBSpline->degree());
608 AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>
609 (theResult->attribute(SketchPlugin_BSpline::POLES_ID()));
610 std::list<GeomPointPtr> aPoles = aBSpline->poles();
611 aPolesAttr->setSize((int)aPoles.size());
612 std::list<GeomPointPtr>::iterator anIt = aPoles.begin();
613 for (int anIndex = 0; anIt != aPoles.end(); ++anIt, ++anIndex) {
614 GeomPnt2dPtr aPoleInSketch = sketch()->to2D(*anIt);
615 aPolesAttr->setPnt(anIndex, aPoleInSketch);
618 AttributeDoubleArrayPtr aWeightsAttr =
619 theResult->data()->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
620 std::list<double> aWeights = aBSpline->weights();
621 if (aWeights.empty()) { // rational B-spline
622 int aSize = (int)aPoles.size();
623 aWeightsAttr->setSize(aSize);
624 for (int anIndex = 0; anIndex < aSize; ++anIndex)
625 aWeightsAttr->setValue(anIndex, 1.0);
627 else { // non-rational B-spline
628 aWeightsAttr->setSize((int)aWeights.size());
629 std::list<double>::iterator anIt = aWeights.begin();
630 for (int anIndex = 0; anIt != aWeights.end(); ++anIt, ++anIndex)
631 aWeightsAttr->setValue(anIndex, *anIt);
634 AttributeDoubleArrayPtr aKnotsAttr =
635 theResult->data()->realArray(SketchPlugin_BSpline::KNOTS_ID());
636 std::list<double> aKnots = aBSpline->knots();
637 int aSize = (int)aKnots.size();
638 aKnotsAttr->setSize(aSize);
639 std::list<double>::iterator aKIt = aKnots.begin();
640 for (int index = 0; index < aSize; ++index, ++aKIt)
641 aKnotsAttr->setValue(index, *aKIt);
643 AttributeIntArrayPtr aMultsAttr =
644 theResult->data()->intArray(SketchPlugin_BSpline::MULTS_ID());
645 std::list<int> aMultiplicities = aBSpline->mults();
646 aSize = (int)aMultiplicities.size();
647 aMultsAttr->setSize(aSize);
648 std::list<int>::iterator aMIt = aMultiplicities.begin();
649 for (int index = 0; index < aSize; ++index, ++aMIt)
650 aMultsAttr->setValue(index, *aMIt);
653 void SketchPlugin_Offset::attributeChanged(const std::string& theID)
655 if (theID == EDGES_ID()) {
656 AttributeRefListPtr aSelected = reflist(EDGES_ID());
657 if (aSelected->size() == 0) {
658 // Clear list of objects
659 AttributeRefListPtr anOffsetAttr = reflist(SketchPlugin_Constraint::ENTITY_B());
660 std::list<ObjectPtr> anOffsetList = anOffsetAttr->list();
661 std::set<FeaturePtr> aFeaturesToBeRemoved;
662 for (std::list<ObjectPtr>::iterator anIt = anOffsetList.begin();
663 anIt != anOffsetList.end(); ++anIt) {
664 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
666 aFeaturesToBeRemoved.insert(aFeature);
669 reflist(SketchPlugin_Constraint::ENTITY_A())->clear();
670 anOffsetAttr->clear();
671 intArray(SketchPlugin_Constraint::ENTITY_C())->setSize(0);
673 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
678 bool SketchPlugin_Offset::customAction(const std::string& theActionId)
681 if (theActionId == ADD_WIRE_ACTION_ID()) {
685 std::string aMsg = "Error: Feature \"%1\" does not support action \"%2\".";
686 Events_InfoMessage("SketchPlugin_Offset", aMsg).arg(getKind()).arg(theActionId).send();
691 bool SketchPlugin_Offset::findWires()
693 AttributeRefListPtr aSelectedEdges = reflist(EDGES_ID());
694 std::list<ObjectPtr> anEdgesList = aSelectedEdges->list();
697 std::set<FeaturePtr> anEdgesSet;
700 std::set<FeaturePtr> aProcessedEdgesSet;
702 // Put all selected edges in a set to avoid adding them in reflist(EDGES_ID())
703 std::set<FeaturePtr> aSelectedSet;
704 std::list<ObjectPtr>::const_iterator anEdgesIt = anEdgesList.begin();
705 for (; anEdgesIt != anEdgesList.end(); anEdgesIt++) {
706 FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt);
708 aSelectedSet.insert(aFeature);
712 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
714 // Gather chains of edges
715 for (anEdgesIt = anEdgesList.begin(); anEdgesIt != anEdgesList.end(); anEdgesIt++) {
716 FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt);
717 if (aFeature.get()) {
718 if (aProcessedEdgesSet.find(aFeature) != aProcessedEdgesSet.end())
720 aProcessedEdgesSet.insert(aFeature);
722 // End points (if any)
723 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
724 SketchPlugin_SegmentationTools::getFeaturePoints(aFeature, aStartPoint, anEndPoint);
726 std::list<FeaturePtr> aChain;
727 aChain.push_back(aFeature);
728 bool isClosed = findWireOneWay(aFeature, aFeature, aStartPoint, anEdgesSet,
729 aProcessedEdgesSet, aChain, true);
731 findWireOneWay(aFeature, aFeature, anEndPoint, anEdgesSet,
732 aProcessedEdgesSet, aChain, false);
735 std::list<FeaturePtr>::iterator aChainIt = aChain.begin();
736 for (; aChainIt != aChain.end(); ++aChainIt) {
737 FeaturePtr aChainFeature = (*aChainIt);
738 if (aSelectedSet.find(aChainFeature) == aSelectedSet.end()) {
739 aSelectedEdges->append(aChainFeature->lastResult());
744 // TODO: hilight selection in the viewer
746 data()->blockSendAttributeUpdated(aWasBlocked);
751 AISObjectPtr SketchPlugin_Offset::getAISObject(AISObjectPtr thePrevious)
756 AISObjectPtr anAIS = SketcherPrs_Factory::offsetObject(this, sketch(),