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>
66 static const double tolerance = 1.e-7;
68 SketchPlugin_Offset::SketchPlugin_Offset()
72 void SketchPlugin_Offset::initAttributes()
74 data()->addAttribute(EDGES_ID(), ModelAPI_AttributeRefList::typeId());
75 data()->addAttribute(VALUE_ID(), ModelAPI_AttributeDouble::typeId());
76 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
78 // store original entities
79 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
80 // store offset entities
81 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
82 // store mapping between original entity and index of the corresponding offset entity
83 data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeIntArray::typeId());
85 ModelAPI_Session::get()->validators()->
86 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
87 ModelAPI_Session::get()->validators()->
88 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
89 ModelAPI_Session::get()->validators()->
90 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
93 void SketchPlugin_Offset::execute()
95 SketchPlugin_Sketch* aSketch = sketch();
99 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
102 AttributeDoublePtr aValueAttr = real(VALUE_ID());
103 if (!aValueAttr->isInitialized()) return;
104 double aValue = aValueAttr->value();
105 if (aValue < tolerance) return;
108 AttributeBooleanPtr aReversedAttr = boolean(REVERSED_ID());
109 if (!aReversedAttr->isInitialized()) return;
110 if (aReversedAttr->value()) aValue = -aValue; // reverse offset direction
112 // 3. List of all selected edges
113 AttributeRefListPtr aSelectedEdges = reflist(EDGES_ID());
114 std::list<ObjectPtr> anEdgesList = aSelectedEdges->list();
116 // 4. Put all selected edges in a set to pass them into findWireOneWay() below
117 std::set<FeaturePtr> anEdgesSet;
118 std::list<ObjectPtr>::const_iterator anEdgesIt = anEdgesList.begin();
119 for (; anEdgesIt != anEdgesList.end(); anEdgesIt++) {
120 FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt);
122 anEdgesSet.insert(aFeature);
126 // Wait all objects being created, then send update events
127 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
128 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
130 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
132 // 5. Gather wires and make offset for each wire
133 ListOfMakeShape anOffsetAlgos;
134 for (anEdgesIt = anEdgesList.begin(); anEdgesIt != anEdgesList.end(); anEdgesIt++) {
135 FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt);
136 if (aFeature.get()) {
137 if (anEdgesSet.find(aFeature) == anEdgesSet.end())
140 // 5.a. End points (if any)
141 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
142 SketchPlugin_SegmentationTools::getFeaturePoints(aFeature, aStartPoint, anEndPoint);
144 // 5.b. Find a chain of edges
145 std::list<FeaturePtr> aChain;
146 aChain.push_back(aFeature);
147 if (aStartPoint && anEndPoint) { // not closed edge
148 bool isClosed = findWireOneWay(aFeature, aFeature, aStartPoint, anEdgesSet, aChain, true);
150 findWireOneWay(aFeature, aFeature, anEndPoint, anEdgesSet, aChain, false);
152 std::set<FeaturePtr>::iterator aPos = anEdgesSet.find(aFeature);
153 if (aPos != anEdgesSet.end())
154 anEdgesSet.erase(aPos);
157 ListOfShape aTopoChain;
158 std::list<FeaturePtr>::iterator aChainIt = aChain.begin();
159 for (; aChainIt != aChain.end(); ++aChainIt) {
160 FeaturePtr aChainFeature = (*aChainIt);
161 GeomShapePtr aTopoEdge = aChainFeature->lastResult()->shape();
162 if (aTopoEdge->shapeType() == GeomAPI_Shape::EDGE) {
163 aTopoChain.push_back(aTopoEdge);
166 std::shared_ptr<GeomAlgoAPI_WireBuilder> aWireBuilder(
167 new GeomAlgoAPI_WireBuilder(aTopoChain));
169 GeomShapePtr aWireShape = aWireBuilder->shape();
170 GeomWirePtr aWire (new GeomAPI_Wire (aWireShape));
172 // Fix for a problem of offset side change with selection change.
173 // Wire direction is defined by the first selected edge of this wire.
174 if (!aWire->isClosed()) {
175 ListOfShape aModified;
176 // First selected edge of current chain
177 GeomShapePtr aFirstSel = aFeature->lastResult()->shape();
178 aWireBuilder->modified(aFirstSel, aModified);
179 GeomShapePtr aModFS = aModified.front();
180 if (aModFS->orientation() != aFirstSel->orientation())
184 // 5.d. Make offset for the wire
185 std::shared_ptr<GeomAlgoAPI_Offset> anOffsetShape(
186 new GeomAlgoAPI_Offset(aPlane, aWireShape, aValue));
188 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList(new GeomAlgoAPI_MakeShapeList);
189 aMakeList->appendAlgo(aWireBuilder);
190 aMakeList->appendAlgo(anOffsetShape);
191 anOffsetAlgos.push_back(aMakeList);
195 // 6. Store offset results.
196 // Create sketch feature for each edge of anOffsetShape, and also store
197 // created features in CREATED_ID() to remove them on next execute()
198 addToSketch(anOffsetAlgos);
200 // send events to update the sub-features by the solver
202 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
205 bool SketchPlugin_Offset::findWireOneWay (const FeaturePtr& theFirstEdge,
206 const FeaturePtr& theEdge,
207 const std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint,
208 std::set<FeaturePtr>& theEdgesSet,
209 std::list<FeaturePtr>& theChain,
210 const bool isPrepend)
212 // 1. Find a single edge, coincident to theEndPoint by one of its ends
213 if (!theEndPoint) return false;
215 FeaturePtr aNextEdgeFeature;
218 std::set<AttributePoint2DPtr> aCoincPoints =
219 SketchPlugin_Tools::findPointsCoincidentToPoint(theEndPoint);
221 std::set<AttributePoint2DPtr>::iterator aPointsIt = aCoincPoints.begin();
222 for (; aPointsIt != aCoincPoints.end(); aPointsIt++) {
223 AttributePoint2DPtr aP = (*aPointsIt);
224 FeaturePtr aCoincFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aP->owner());
225 bool isInSet = (theEdgesSet.find(aCoincFeature) != theEdgesSet.end());
227 // Condition 0: not auxiliary
228 if (!isInSet && aCoincFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
231 // Condition 1: not a point feature
232 if (aCoincFeature->getKind() != SketchPlugin_Point::ID()) {
233 // Condition 2: it is not the current edge
234 if (aCoincFeature != theEdge) {
235 // Condition 3: it is in the set of interest.
236 // Empty set means all sketch edges.
237 if (isInSet || theEdgesSet.empty()) {
238 // Condition 4: consider only features with two end points
239 std::shared_ptr<GeomDataAPI_Point2D> aP1, aP2;
240 SketchPlugin_SegmentationTools::getFeaturePoints(aCoincFeature, aP1, aP2);
242 // Condition 5: consider only features, that have aP as one of they ends.
243 // For example, we do not need an arc, coincident to aP by its center.
244 if (theEndPoint->pnt()->isEqual(aP1->pnt()) ||
245 theEndPoint->pnt()->isEqual(aP2->pnt())) {
246 // Condition 6: only one edge can prolongate the chain. If several, we stop here.
252 aNextEdgeFeature = aCoincFeature;
260 // Only one edge can prolongate the chain. If several or none, we stop here.
264 // 2. So, we have the single edge, that prolongate the chain
266 // Condition 7: if we reached the very first edge of the chain
267 if (aNextEdgeFeature == theFirstEdge)
268 // Closed chain found
271 // 3. Add the found edge to the chain
273 theChain.push_front(aNextEdgeFeature);
275 theChain.push_back(aNextEdgeFeature);
276 // remove from the set, if the set is used
277 if (theEdgesSet.size()) {
278 std::set<FeaturePtr>::iterator aPos = theEdgesSet.find(aNextEdgeFeature);
279 if (aPos != theEdgesSet.end())
280 theEdgesSet.erase(aPos);
283 // 4. Which end of aNextEdgeFeature we need to proceed
284 std::shared_ptr<GeomDataAPI_Point2D> aP1, aP2;
285 SketchPlugin_SegmentationTools::getFeaturePoints(aNextEdgeFeature, aP1, aP2);
286 if (aP2->pnt()->isEqual(theEndPoint->pnt())) {
291 // 5. Continue gathering the chain (recursive)
292 return findWireOneWay (theFirstEdge, aNextEdgeFeature, aP2, theEdgesSet, theChain, isPrepend);
295 static void setRefListValue(AttributeRefListPtr theList, int theListSize,
296 ObjectPtr theValue, int theIndex)
298 if (theIndex < theListSize) {
299 ObjectPtr aCur = theList->object(theIndex);
300 if (aCur != theValue)
301 theList->substitute(aCur, theValue);
304 theList->append(theValue);
307 // Reorder shapes according to the wire's order
308 static void reorderShapes(ListOfShape& theShapes, GeomShapePtr theWire)
310 std::set<GeomShapePtr, GeomAPI_Shape::Comparator> aShapes;
311 aShapes.insert(theShapes.begin(), theShapes.end());
314 GeomWirePtr aWire(new GeomAPI_Wire(theWire));
315 GeomAPI_WireExplorer anExp(aWire);
316 for (; anExp.more(); anExp.next()) {
317 GeomShapePtr aCurEdge = anExp.current();
318 auto aFound = aShapes.find(aCurEdge);
319 if (aFound != aShapes.end()) {
320 theShapes.push_back(aCurEdge);
321 aShapes.erase(aFound);
326 static void removeLastFromIndex(AttributeRefListPtr theList, int theListSize, int& theLastIndex)
328 if (theLastIndex < theListSize) {
329 std::set<int> anIndicesToRemove;
330 for (; theLastIndex < theListSize; ++theLastIndex)
331 anIndicesToRemove.insert(theLastIndex);
332 theList->remove(anIndicesToRemove);
336 void SketchPlugin_Offset::addToSketch(const ListOfMakeShape& theOffsetAlgos)
338 AttributeRefListPtr aSelectedRefList = reflist(EDGES_ID());
339 AttributeRefListPtr aBaseRefList = reflist(ENTITY_A());
340 AttributeRefListPtr anOffsetRefList = reflist(ENTITY_B());
341 AttributeIntArrayPtr anOffsetToBaseMap = intArray(ENTITY_C());
343 // compare the list of selected edges and the previously stored,
344 // and store maping between them
345 std::map<ObjectPtr, std::list<ObjectPtr> > aMapExistent;
346 std::list<ObjectPtr> anObjectsToRemove;
347 std::list<ObjectPtr> aSelectedList = aSelectedRefList->list();
348 for (std::list<ObjectPtr>::iterator aSIt = aSelectedList.begin();
349 aSIt != aSelectedList.end(); ++aSIt) {
350 aMapExistent[*aSIt] = std::list<ObjectPtr>();
352 for (int anIndex = 0, aSize = anOffsetRefList->size(); anIndex < aSize; ++anIndex) {
353 ObjectPtr aCurrent = anOffsetRefList->object(anIndex);
354 int aBaseIndex = anOffsetToBaseMap->value(anIndex);
355 if (aBaseIndex >= 0) {
356 ObjectPtr aBaseObj = aBaseRefList->object(aBaseIndex);
357 std::map<ObjectPtr, std::list<ObjectPtr> >::iterator aFound = aMapExistent.find(aBaseObj);
358 if (aFound != aMapExistent.end())
359 aFound->second.push_back(aCurrent);
361 anObjectsToRemove.push_back(aCurrent);
364 anObjectsToRemove.push_back(aCurrent);
367 // update lists of base shapes and of offset shapes
368 int aBaseListSize = aBaseRefList->size();
369 int anOffsetListSize = anOffsetRefList->size();
370 int aBaseListIndex = 0, anOffsetListIndex = 0;
371 std::list<int> anOffsetBaseBackRefs;
372 std::set<GeomShapePtr, GeomAPI_Shape::ComparatorWithOri> aProcessedOffsets;
373 for (std::list<ObjectPtr>::iterator aSIt = aSelectedList.begin();
374 aSIt != aSelectedList.end(); ++aSIt) {
375 // find an offseted edge
376 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(*aSIt);
377 GeomShapePtr aBaseShape = aBaseFeature->lastResult()->shape();
378 ListOfShape aNewShapes;
379 for (ListOfMakeShape::const_iterator anAlgoIt = theOffsetAlgos.begin();
380 anAlgoIt != theOffsetAlgos.end(); ++anAlgoIt) {
381 (*anAlgoIt)->generated(aBaseShape, aNewShapes);
382 if (!aNewShapes.empty()) {
383 reorderShapes(aNewShapes, (*anAlgoIt)->shape());
388 // store base feature
389 setRefListValue(aBaseRefList, aBaseListSize, *aSIt, aBaseListIndex);
391 // create or update an offseted feature
392 const std::list<ObjectPtr>& anImages = aMapExistent[*aSIt];
393 std::list<ObjectPtr>::const_iterator anImgIt = anImages.begin();
394 for (ListOfShape::iterator aNewIt = aNewShapes.begin(); aNewIt != aNewShapes.end(); ++aNewIt) {
395 FeaturePtr aNewFeature;
396 if (anImgIt != anImages.end())
397 aNewFeature = ModelAPI_Feature::feature(*anImgIt++);
398 updateExistentOrCreateNew(*aNewIt, aNewFeature, anObjectsToRemove);
399 aProcessedOffsets.insert(*aNewIt);
401 // store an offseted feature
402 setRefListValue(anOffsetRefList, anOffsetListSize, aNewFeature, anOffsetListIndex);
404 anOffsetBaseBackRefs.push_back(aBaseListIndex);
408 anObjectsToRemove.insert(anObjectsToRemove.end(), anImgIt, anImages.end());
410 // create arcs generated from vertices
411 for (ListOfMakeShape::const_iterator anAlgoIt = theOffsetAlgos.begin();
412 anAlgoIt != theOffsetAlgos.end(); ++anAlgoIt) {
413 GeomShapePtr aCurWire = (*anAlgoIt)->shape();
414 GeomAPI_ShapeExplorer anExp(aCurWire, GeomAPI_Shape::EDGE);
415 for (; anExp.more(); anExp.next()) {
416 GeomShapePtr aCurEdge = anExp.current();
417 if (aProcessedOffsets.find(aCurEdge) == aProcessedOffsets.end()) {
418 FeaturePtr aNewFeature;
419 updateExistentOrCreateNew(aCurEdge, aNewFeature, anObjectsToRemove);
420 aProcessedOffsets.insert(aCurEdge);
422 // store an offseted feature
423 setRefListValue(anOffsetRefList, anOffsetListSize, aNewFeature, anOffsetListIndex);
425 anOffsetBaseBackRefs.push_back(-1);
431 removeLastFromIndex(aBaseRefList, aBaseListSize, aBaseListIndex);
432 removeLastFromIndex(anOffsetRefList, anOffsetListSize, anOffsetListIndex);
434 anOffsetToBaseMap->setSize((int)anOffsetBaseBackRefs.size(), false);
436 for (std::list<int>::iterator anIt = anOffsetBaseBackRefs.begin();
437 anIt != anOffsetBaseBackRefs.end(); ++anIt) {
438 anOffsetToBaseMap->setValue(anIndex++, *anIt, false);
441 // remove unused objects
442 std::set<FeaturePtr> aSet;
443 for (std::list<ObjectPtr>::iterator anIt = anObjectsToRemove.begin();
444 anIt != anObjectsToRemove.end(); ++anIt) {
445 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
447 aSet.insert(aFeature);
449 ModelAPI_Tools::removeFeaturesAndReferences(aSet);
452 static void findOrCreateFeatureByKind(SketchPlugin_Sketch* theSketch,
453 const std::string& theFeatureKind,
454 FeaturePtr& theFeature,
455 std::list<ObjectPtr>& thePoolOfFeatures)
458 // check the feature type is the same as required
459 if (theFeature->getKind() != theFeatureKind) {
460 // return feature to the pool and try to find the most appropriate
461 thePoolOfFeatures.push_back(theFeature);
462 theFeature = FeaturePtr();
466 // try to find appropriate feature in the pool
467 for (std::list<ObjectPtr>::iterator it = thePoolOfFeatures.begin();
468 it != thePoolOfFeatures.end(); ++it) {
469 FeaturePtr aCurFeature = ModelAPI_Feature::feature(*it);
470 if (aCurFeature->getKind() == theFeatureKind) {
471 theFeature = aCurFeature;
472 thePoolOfFeatures.erase(it);
476 // feature not found, create new
478 theFeature = theSketch->addFeature(theFeatureKind);
482 void SketchPlugin_Offset::updateExistentOrCreateNew(const GeomShapePtr& theShape,
483 FeaturePtr& theFeature,
484 std::list<ObjectPtr>& thePoolOfFeatures)
486 if (theShape->shapeType() != GeomAPI_Shape::EDGE)
489 std::shared_ptr<GeomAPI_Edge> aResEdge(new GeomAPI_Edge(theShape));
491 std::shared_ptr<GeomAPI_Pnt2d> aFP, aLP;
492 std::shared_ptr<GeomAPI_Pnt> aFP3d = aResEdge->firstPoint();
493 std::shared_ptr<GeomAPI_Pnt> aLP3d = aResEdge->lastPoint();
494 if (aFP3d && aLP3d) {
495 aFP = sketch()->to2D(aFP3d);
496 aLP = sketch()->to2D(aLP3d);
499 if (aResEdge->isLine()) {
500 findOrCreateFeatureByKind(sketch(), SketchPlugin_Line::ID(), theFeature, thePoolOfFeatures);
502 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
503 (theFeature->attribute(SketchPlugin_Line::START_ID()))->setValue(aFP);
504 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
505 (theFeature->attribute(SketchPlugin_Line::END_ID()))->setValue(aLP);
507 else if (aResEdge->isArc()) {
508 std::shared_ptr<GeomAPI_Circ> aCircEdge = aResEdge->circle();
509 std::shared_ptr<GeomAPI_Pnt> aCP3d = aCircEdge->center();
510 std::shared_ptr<GeomAPI_Pnt2d> aCP = sketch()->to2D(aCP3d);
512 findOrCreateFeatureByKind(sketch(), SketchPlugin_Arc::ID(), theFeature, thePoolOfFeatures);
514 GeomDirPtr aCircNormal = aCircEdge->normal();
515 GeomDirPtr aSketchNormal = sketch()->coordinatePlane()->normal();
516 if (aSketchNormal->dot(aCircNormal) < -tolerance)
519 bool aWasBlocked = theFeature->data()->blockSendAttributeUpdated(true);
520 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
521 (theFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(aLP);
522 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
523 (theFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(aFP);
524 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
525 (theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(aCP);
526 theFeature->data()->blockSendAttributeUpdated(aWasBlocked);
528 else if (aResEdge->isCircle()) {
529 std::shared_ptr<GeomAPI_Circ> aCircEdge = aResEdge->circle();
530 std::shared_ptr<GeomAPI_Pnt> aCP3d = aCircEdge->center();
531 std::shared_ptr<GeomAPI_Pnt2d> aCP = sketch()->to2D(aCP3d);
533 findOrCreateFeatureByKind(sketch(), SketchPlugin_Circle::ID(), theFeature, thePoolOfFeatures);
535 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
536 (theFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->setValue(aCP);
537 theFeature->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aCircEdge->radius());
539 else if (aResEdge->isEllipse()) {
540 std::shared_ptr<GeomAPI_Ellipse> anEllipseEdge = aResEdge->ellipse();
542 GeomPointPtr aCP3d = anEllipseEdge->center();
543 GeomPnt2dPtr aCP = sketch()->to2D(aCP3d);
545 GeomPointPtr aFocus3d = anEllipseEdge->firstFocus();
546 GeomPnt2dPtr aFocus = sketch()->to2D(aFocus3d);
548 if (aFP3d && aLP3d) {
550 findOrCreateFeatureByKind(sketch(), SketchPlugin_EllipticArc::ID(),
551 theFeature, thePoolOfFeatures);
553 bool aWasBlocked = theFeature->data()->blockSendAttributeUpdated(true);
554 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
555 (theFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID()))->setValue(aCP);
556 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
557 (theFeature->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID()))->setValue(aFocus);
558 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
559 (theFeature->attribute(SketchPlugin_EllipticArc::START_POINT_ID()))->setValue(aFP);
560 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
561 (theFeature->attribute(SketchPlugin_EllipticArc::END_POINT_ID()))->setValue(aLP);
562 theFeature->data()->blockSendAttributeUpdated(aWasBlocked);
566 findOrCreateFeatureByKind(sketch(), SketchPlugin_Ellipse::ID(),
567 theFeature, thePoolOfFeatures);
569 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
570 (theFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()))->setValue(aCP);
571 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
572 (theFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()))->setValue(aFocus);
573 theFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(
574 anEllipseEdge->minorRadius());
578 // convert to b-spline
579 mkBSpline(theFeature, aResEdge, thePoolOfFeatures);
582 if (theFeature.get()) {
583 theFeature->boolean(SketchPlugin_SketchEntity::COPY_ID())->setValue(true);
584 theFeature->execute();
586 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
587 ModelAPI_EventCreator::get()->sendUpdated(theFeature, aRedisplayEvent);
588 const std::list<ResultPtr>& aResults = theFeature->results();
589 for (std::list<ResultPtr>::const_iterator anIt = aResults.begin();
590 anIt != aResults.end(); ++anIt)
591 ModelAPI_EventCreator::get()->sendUpdated(*anIt, aRedisplayEvent);
595 void SketchPlugin_Offset::mkBSpline (FeaturePtr& theResult,
596 const GeomEdgePtr& theEdge,
597 std::list<ObjectPtr>& thePoolOfFeatures)
599 GeomCurvePtr aCurve (new GeomAPI_Curve (theEdge));
600 // Forced conversion to b-spline, if aCurve is not b-spline
601 GeomBSplinePtr aBSpline = GeomAPI_BSpline::convertToBSpline(aCurve);
603 const std::string& aBSplineKind = aBSpline->isPeriodic() ? SketchPlugin_BSplinePeriodic::ID()
604 : SketchPlugin_BSpline::ID();
605 findOrCreateFeatureByKind(sketch(), aBSplineKind, theResult, thePoolOfFeatures);
607 theResult->integer(SketchPlugin_BSpline::DEGREE_ID())->setValue(aBSpline->degree());
609 AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>
610 (theResult->attribute(SketchPlugin_BSpline::POLES_ID()));
611 std::list<GeomPointPtr> aPoles = aBSpline->poles();
612 aPolesAttr->setSize((int)aPoles.size());
613 std::list<GeomPointPtr>::iterator anIt = aPoles.begin();
614 for (int anIndex = 0; anIt != aPoles.end(); ++anIt, ++anIndex) {
615 GeomPnt2dPtr aPoleInSketch = sketch()->to2D(*anIt);
616 aPolesAttr->setPnt(anIndex, aPoleInSketch);
619 AttributeDoubleArrayPtr aWeightsAttr =
620 theResult->data()->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
621 std::list<double> aWeights = aBSpline->weights();
622 if (aWeights.empty()) { // rational B-spline
623 int aSize = (int)aPoles.size();
624 aWeightsAttr->setSize(aSize);
625 for (int anIndex = 0; anIndex < aSize; ++anIndex)
626 aWeightsAttr->setValue(anIndex, 1.0);
628 else { // non-rational B-spline
629 aWeightsAttr->setSize((int)aWeights.size());
630 std::list<double>::iterator anIt = aWeights.begin();
631 for (int anIndex = 0; anIt != aWeights.end(); ++anIt, ++anIndex)
632 aWeightsAttr->setValue(anIndex, *anIt);
635 AttributeDoubleArrayPtr aKnotsAttr =
636 theResult->data()->realArray(SketchPlugin_BSpline::KNOTS_ID());
637 std::list<double> aKnots = aBSpline->knots();
638 int aSize = (int)aKnots.size();
639 aKnotsAttr->setSize(aSize);
640 std::list<double>::iterator aKIt = aKnots.begin();
641 for (int index = 0; index < aSize; ++index, ++aKIt)
642 aKnotsAttr->setValue(index, *aKIt);
644 AttributeIntArrayPtr aMultsAttr =
645 theResult->data()->intArray(SketchPlugin_BSpline::MULTS_ID());
646 std::list<int> aMultiplicities = aBSpline->mults();
647 aSize = (int)aMultiplicities.size();
648 aMultsAttr->setSize(aSize);
649 std::list<int>::iterator aMIt = aMultiplicities.begin();
650 for (int index = 0; index < aSize; ++index, ++aMIt)
651 aMultsAttr->setValue(index, *aMIt);
654 void SketchPlugin_Offset::attributeChanged(const std::string& theID)
656 //// if (theID == EDGES_ID())
657 //// removeCreated();
660 bool SketchPlugin_Offset::customAction(const std::string& theActionId)
663 if (theActionId == ADD_WIRE_ACTION_ID()) {
667 std::string aMsg = "Error: Feature \"%1\" does not support action \"%2\".";
668 Events_InfoMessage("SketchPlugin_Offset", aMsg).arg(getKind()).arg(theActionId).send();
673 bool SketchPlugin_Offset::findWires()
675 AttributeRefListPtr aSelectedEdges = reflist(EDGES_ID());
676 std::list<ObjectPtr> anEdgesList = aSelectedEdges->list();
679 std::set<FeaturePtr> anEdgesSet;
682 std::set<FeaturePtr> aProcessedSet;
684 // Put all selected edges in a set to avoid adding them in reflist(EDGES_ID())
685 std::set<FeaturePtr> aSelectedSet;
686 std::list<ObjectPtr>::const_iterator anEdgesIt = anEdgesList.begin();
687 for (; anEdgesIt != anEdgesList.end(); anEdgesIt++) {
688 FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt);
690 aSelectedSet.insert(aFeature);
694 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
696 // Gather chains of edges
697 for (anEdgesIt = anEdgesList.begin(); anEdgesIt != anEdgesList.end(); anEdgesIt++) {
698 FeaturePtr aFeature = ModelAPI_Feature::feature(*anEdgesIt);
699 if (aFeature.get()) {
700 if (aProcessedSet.find(aFeature) != aProcessedSet.end())
702 aProcessedSet.insert(aFeature);
704 // End points (if any)
705 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
706 SketchPlugin_SegmentationTools::getFeaturePoints(aFeature, aStartPoint, anEndPoint);
708 std::list<FeaturePtr> aChain;
709 aChain.push_back(aFeature);
710 bool isClosed = findWireOneWay(aFeature, aFeature, aStartPoint, anEdgesSet, aChain, true);
712 findWireOneWay(aFeature, aFeature, anEndPoint, anEdgesSet, aChain, false);
714 std::list<FeaturePtr>::iterator aChainIt = aChain.begin();
715 for (; aChainIt != aChain.end(); ++aChainIt) {
716 FeaturePtr aChainFeature = (*aChainIt);
717 aProcessedSet.insert(aChainFeature);
718 if (aSelectedSet.find(aChainFeature) == aSelectedSet.end()) {
719 aSelectedEdges->append(aChainFeature->lastResult());
724 // TODO: hilight selection in the viewer
726 data()->blockSendAttributeUpdated(aWasBlocked);
731 AISObjectPtr SketchPlugin_Offset::getAISObject(AISObjectPtr thePrevious)
736 AISObjectPtr anAIS = SketcherPrs_Factory::offsetObject(this, sketch(),