1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_Split.h"
23 #include <Events_Message.h>
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Pnt2d.h>
28 #include <GeomAPI_XY.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAlgoAPI_CompoundBuilder.h>
33 #include <ModelAPI_AttributeBoolean.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeRefAttr.h>
36 #include <ModelAPI_AttributeReference.h>
37 #include <ModelAPI_AttributeString.h>
38 #include <ModelAPI_Events.h>
39 #include <ModelAPI_Validator.h>
40 #include <ModelAPI_Session.h>
41 #include <ModelAPI_Tools.h>
43 #include <ModelGeomAlgo_Shape.h>
45 #include <SketchPlugin_Arc.h>
46 #include <SketchPlugin_Circle.h>
47 #include <SketchPlugin_ConstraintCoincidence.h>
48 #include <SketchPlugin_ConstraintEqual.h>
49 #include <SketchPlugin_ConstraintLength.h>
50 #include <SketchPlugin_ConstraintMiddle.h>
51 #include <SketchPlugin_ConstraintMirror.h>
52 #include <SketchPlugin_ConstraintParallel.h>
53 #include <SketchPlugin_ConstraintTangent.h>
54 #include <SketchPlugin_Line.h>
55 #include <SketchPlugin_MultiRotation.h>
56 #include <SketchPlugin_MultiTranslation.h>
57 #include <SketchPlugin_Point.h>
58 #include <SketchPlugin_Tools.h>
60 #include <ModelGeomAlgo_Point2D.h>
61 #include <ModelAPI_EventReentrantMessage.h>
62 #include <Events_Loop.h>
66 //#define CREATE_CONSTRAINTS
72 static const double PI = 3.141592653589793238463;
74 SketchPlugin_Split::SketchPlugin_Split()
78 void SketchPlugin_Split::initAttributes()
80 data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
81 data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
83 data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
84 data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
86 data()->attribute(PREVIEW_POINT())->setIsArgument(false);
87 data()->attribute(SELECTED_POINT())->setIsArgument(false);
88 data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
90 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
91 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
94 //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
95 //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
96 //data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
99 void SketchPlugin_Split::execute()
101 std::shared_ptr<ModelAPI_Data> aData = data();
103 // Check the base objects are initialized.
104 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
105 data()->attribute(SELECTED_OBJECT()));
106 //ObjectPtr aBaseObject = anObjectAttr->value();
107 //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
108 // aData->attribute(SketchPlugin_Constraint::VALUE()));
109 if(!aBaseObjectAttr->isInitialized()) {
110 setError("Error: Base object is not initialized.");
113 ObjectPtr aBaseObject = aBaseObjectAttr->value();
114 AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
115 // getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
116 AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
117 // getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
118 if (!aFirstPointAttrOfSplit.get() || !aFirstPointAttrOfSplit->isInitialized() ||
119 !aSecondPointAttrOfSplit.get() || !aSecondPointAttrOfSplit->isInitialized()) {
120 setError("Error: Sub-shape is not initialized.");
124 /// Remove reference of this feature to feature used in preview, it is not necessary anymore
125 /// as trim will be removed after execute
126 AttributeReferencePtr aPreviewObjectAttr =
127 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
128 data()->attribute(PREVIEW_OBJECT()));
130 ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
131 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
132 data()->attribute(PREVIEW_POINT()));
133 std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
134 // nullify pointer of preview attribute
135 aPreviewObjectAttr->setValue(ResultPtr());
136 bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
138 // Wait all constraints being created, then send update events
139 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
140 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
142 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
144 // Find feature constraints
145 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
146 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
147 std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
149 //std::map<FeaturePtr, IdToPointPair> aTangentFeatures;
150 std::map<FeaturePtr, IdToPointPair> aCoincidenceToFeature;
151 getConstraints(aFeaturesToDelete, aFeaturesToUpdate, /*aTangentFeatures, */
152 aCoincidenceToFeature);
154 std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
155 std::list<AttributePtr> aRefsToFeature;
156 getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
158 std::map<AttributePtr, AttributePtr> aBasePointModifiedAttributes;
161 std::cout << std::endl;
162 std::cout << "SketchPlugin_Split::execute()" << std::endl;
163 std::cout << std::endl;
165 SketchPlugin_Sketch* aSketch = sketch();
166 std::cout << "SKETCH FEATURES (before split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
167 for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
168 std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
171 std::cout << std::endl;
172 std::cout << "---- IN PARAMETERS ----" << std::endl;
173 std::cout << "Base feature:" << getFeatureInfo(aBaseFeature) << std::endl;
174 std::cout << std::endl;
176 if (!aCoincidenceToFeature.empty()) {
177 std::cout << "Coincidences to base feature[" <<
178 aCoincidenceToFeature.size() << "]: " << std::endl;
179 std::map<FeaturePtr, IdToPointPair>::const_iterator anIt = aCoincidenceToFeature.begin(),
180 aLast = aCoincidenceToFeature.end();
181 for (int i = 1; anIt != aLast; anIt++, i++) {
182 FeaturePtr aFeature = (*anIt).first;
183 std::string anAttributeId = (*anIt).second.first;
184 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = (*anIt).second.second;
186 std::cout << i << "-" << getFeatureInfo(aFeature) << std::endl;
187 std::cout << " -Attribute to correct:" << anAttributeId << std::endl;
188 std::cout << " -Point attribute:" <<
189 ModelGeomAlgo_Point2D::getPointAttributeInfo(aPointAttr) << std::endl;
193 std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
194 aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
195 std::cout << std::endl << "References to attributes of base feature [" <<
196 aBaseRefAttributes.size() << "]" << std::endl;
197 for (; aRefIt != aRefLast; aRefIt++) {
198 AttributePtr aBaseAttr = aRefIt->first;
199 std::list<AttributePtr> aRefAttributes = aRefIt->second;
200 std::string aRefsInfo;
201 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
202 aRefAttrLast = aRefAttributes.end();
203 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
204 if (!aRefsInfo.empty())
205 aRefsInfo.append(",");
206 AttributePtr aRAttr = *aRefAttrIt;
207 aRefsInfo.append(aRAttr->id());
208 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
209 aRefsInfo.append("(" + aRFeature->name() + ") ");
211 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
212 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
213 std::cout << aPointAttr->id().c_str() <<
214 ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
216 std::cout << std::endl;
217 std::cout << std::endl << "References to base feature [" <<
218 aRefsToFeature.size() << "]" << std::endl;
219 std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
220 aRefAttrLast = aRefsToFeature.end();
221 std::string aRefsInfo;
222 for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
223 if (!aRefsInfo.empty())
224 aRefsInfo.append(",");
225 AttributePtr aRAttr = *aRefAttrIt;
226 aRefsInfo.append(aRAttr->id());
227 FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
228 aRefsInfo.append("(" + aRFeature->name() + ") ");
230 std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
233 std::cout << std::endl;
234 std::cout << "---- SPLIT ----" << std::endl;
235 std::cout << std::endl;
238 std::string aFeatureKind = aBaseFeature->getKind();
239 FeaturePtr aSplitFeature, anAfterFeature;
240 std::set<AttributePoint2DPtr> aFurtherCoincidences;
241 std::set<FeaturePtr> aCreatedFeatures;
242 std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
243 FeaturePtr aReplacingFeature, aNewFeature;
244 if (aFeatureKind == SketchPlugin_Line::ID())
245 aNewFeature = splitLine(aSplitFeature, aBaseFeature, anAfterFeature,
246 aFurtherCoincidences, aCreatedFeatures, aModifiedAttributes);
247 else if (aFeatureKind == SketchPlugin_Arc::ID())
248 aNewFeature = splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences,
249 aCreatedFeatures, aModifiedAttributes);
250 if (aFeatureKind == SketchPlugin_Circle::ID()) {
251 FeaturePtr aCircleFeature = aBaseFeature;
252 aReplacingFeature = splitCircle(aSplitFeature, aBaseFeature, anAfterFeature,
253 aFurtherCoincidences, aCreatedFeatures, aModifiedAttributes);
255 updateRefFeatureConstraints(getFeatureResult(aBaseFeature), aRefsToFeature);
257 AttributePtr aCenterAttr = aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID());
258 aFeaturesToDelete.insert(aCircleFeature);
259 // as circle is removed, temporary fill this attribute*/
260 aBaseObjectAttr->setObject(ResultPtr());
264 std::cout << "---- OUT PARAMETERS ----" << std::endl;
265 std::cout << "Base modified feature:" << getFeatureInfo(aBaseFeature) << std::endl;
266 std::cout << "Split feature:" << getFeatureInfo(aSplitFeature) << std::endl;
267 std::cout << "After feature:" << getFeatureInfo(anAfterFeature) << std::endl;
268 std::cout << std::endl;
270 std::cout << "Created features by split:[" << aCreatedFeatures.size() << "]" << std::endl;
271 std::set<FeaturePtr>::const_iterator aFIt = aCreatedFeatures.begin(),
272 aFLast = aCreatedFeatures.end();
273 for (; aFIt != aFLast; aFIt++) {
274 std::cout << getFeatureInfo(*aFIt) << std::endl;
276 std::cout << std::endl;
278 std::cout << "Attributes for further Coincidences:" << std::endl;
279 std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
280 aLast = aFurtherCoincidences.end();
281 for (; anIt != aLast; anIt++) {
282 AttributePtr anAttribute = *anIt;
283 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
284 std::cout << ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute)
285 << " [" << getFeatureInfo(aFeature, false) << "]" << std::endl;
288 std::cout << "Modifed attributes (constraints to attributes are moved here):" << std::endl;
289 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
290 aPIt = aModifiedAttributes.begin(), aPLast = aModifiedAttributes.end();
291 std::string aResInfo;
292 for (; aPIt != aPLast; aPIt++) {
293 if (!aResInfo.empty())
296 std::pair<AttributePtr, AttributePtr> aPair = *aPIt;
298 AttributePtr anAttr = aPair.first;
299 aResInfo.append(anAttr->id());
300 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->owner());
301 aResInfo.append("(" + aFeature->name() + ") ");
303 aResInfo.append(" - is modified to - ");
305 anAttr = aPair.second;
306 aResInfo.append(anAttr->id());
307 aFeature = ModelAPI_Feature::feature(anAttr->owner());
308 aResInfo.append("(" + aFeature->name() + ") ");
310 std::cout << aResInfo << std::endl;
313 std::set<ResultPtr> aFeatureResults;
314 aFeatureResults.insert(getFeatureResult(aBaseFeature));
315 if (anAfterFeature.get() && anAfterFeature != aBaseFeature)
316 aFeatureResults.insert(getFeatureResult(anAfterFeature));
318 // coincidence to feature
319 updateCoincidenceConstraintsToFeature(aCoincidenceToFeature, aFurtherCoincidences,
320 aFeatureResults, aSplitFeature, aFeaturesToDelete);
322 updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes);
324 // delete constraints
326 std::cout << "remove features and references:" << std::endl;
327 std::set<FeaturePtr>::const_iterator aDIt = aFeaturesToDelete.begin(),
328 aDLast = aFeaturesToDelete.end();
329 for (; aDIt != aDLast; aDIt++) {
330 std::cout << getFeatureInfo(*aDIt, false) << std::endl;
331 std::cout << std::endl;
334 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
335 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
338 std::cout << "update features after split:" << std::endl;
339 std::set<FeaturePtr>::const_iterator anUIt = aFeaturesToUpdate.begin(),
340 anULast = aFeaturesToUpdate.end();
341 for (; anUIt != anULast; anUIt++) {
342 std::cout << getFeatureInfo(*anUIt, false) << std::endl;
343 std::cout << std::endl;
346 updateFeaturesAfterSplit(aFeaturesToUpdate);
348 // Send events to update the sub-features by the solver.
349 if(isUpdateFlushed) {
350 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
353 if (anIsEqualPreviewAndSelected) {
354 // equal preview and selected objects
355 // nothing to do if the preview and selected objects are different
356 ResultPtr aReplacingResult;
357 if (aReplacingFeature.get()) {
358 aReplacingFeature->execute(); // need it to obtain result
359 aReplacingResult = getFeatureResult(aReplacingFeature);
361 if (aReplacingResult.get()) { // base object was removed
362 aPreviewObject = aReplacingResult;
363 //aMessage->setSelectedObject(aReplacingResult);
365 //GeomShapePtr aSelectedShape = aReplacingResult->shape();
366 //std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
367 // aPreviewPnt2d->y());
368 //std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
369 //if (ModelGeomAlgo_Point2D::isPointOnEdge(aSelectedShape, aPreviewPnt, aProjectedPoint)) {
370 //bool aValue = true;
372 //aBaseShape = aShape;
375 if (!aSelectedShape.get())
376 std::cout << "Set empty selected object" << std::endl;
378 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
380 //bool aValue = true;
383 aPreviewObject = ObjectPtr();
385 aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
386 aBaseObject = getFeatureResult(aBaseFeature);
387 std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
389 ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
391 GeomShapePtr aShape = aBaseResult->shape();
392 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
393 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
394 aPreviewObject = aBaseResult;
396 if (!aPreviewObject.get() && aNewFeature.get()) {
397 ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
398 if (aNewFeatureResult.get()) {
399 GeomShapePtr aShape = aNewFeatureResult->shape();
400 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
401 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
402 aPreviewObject = aNewFeatureResult;
407 if (aPreviewObject.get()) {
408 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
409 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
410 ModelAPI_EventReentrantMessage::eventId(), this));
411 aMessage->setSelectedObject(aPreviewObject);
412 Events_Loop::loop()->send(aMessage);
417 std::cout << "SKETCH FEATURES (after split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
418 for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
419 std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
425 std::string SketchPlugin_Split::processEvent(const std::shared_ptr<Events_Message>& theMessage)
428 std::cout << "SketchPlugin_Split::processEvent:" << data()->name() << std::endl;
430 std::string aFilledAttributeName;
432 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
433 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
434 if (aMessage.get()) {
435 ObjectPtr anObject = aMessage->selectedObject();
436 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
438 if (anObject.get() && aPoint.get()) {
439 //if (myCashedShapes.find(anObject) == myCashedShapes.end())
440 // fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
441 if (myCashedShapes.find(anObject) == myCashedShapes.end())
442 fillObjectShapes(anObject, sketch()->data()->owner());
443 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
444 if (aShapes.size() > 1) {
445 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
446 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
447 data()->attribute(SELECTED_OBJECT()));
448 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
449 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
450 data()->attribute(PREVIEW_OBJECT()));
451 aRefSelectedAttr->setValue(anObject);
452 aRefPreviewAttr->setValue(anObject);
454 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
455 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
456 data()->attribute(SELECTED_POINT()));
457 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
458 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
459 data()->attribute(PREVIEW_POINT()));
460 aPointSelectedAttr->setValue(aPoint);
461 aPointPreviewAttr->setValue(aPoint);
463 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
465 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
466 if (aSelectedShape.get()) {
467 aFilledAttributeName = SELECTED_OBJECT();
470 // #2480 - sub shape is not initialized when split sketch
471 // If restarted operation use some selection on the shape that is split and
472 // result selectiona can not participate in new split(checked shape above is null),
473 // reset filled values of selection set in this method above
474 aRefSelectedAttr->setValue(ResultPtr());
475 aRefPreviewAttr->setValue(ResultPtr());
478 if (!aSelectedShape.get())
479 std::cout << "Set empty selected object" << std::endl;
481 std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
486 return aFilledAttributeName;
490 AISObjectPtr SketchPlugin_Split::getAISObject(AISObjectPtr thePrevious)
492 /*AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
493 data()->attribute(SketchPlugin_Constraint::VALUE()));
494 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
496 AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
497 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
498 AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
499 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
501 if (aBaseObjectAttr->isInitialized() && aBaseFeature.get() &&
502 aFirstPointAttrOfSplit->isInitialized() &&
503 aSecondPointAttrOfSplit->isInitialized()) {
505 ResultPtr aResult = getFeatureResult(aBaseFeature);
506 GeomShapePtr aBaseShape = aResult->shape();
507 std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
509 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
510 std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
511 aPoints.push_back(aStartPoint);
513 std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
514 std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
515 sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
516 aPoints.push_back(aSecondPoint);
518 std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
520 GeomAlgoAPI_ShapeTools::splitShape_p(aBaseShape, aPoints, aSplitShapes);
521 std::shared_ptr<GeomAPI_Shape> aShape =
522 GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
524 AISObjectPtr anAIS = thePrevious;
527 anAIS = AISObjectPtr(new GeomAPI_AISObject);
528 anAIS->createShape(aShape);
529 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
530 aBaseFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
532 bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
534 std::vector<int> aColor;
535 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
536 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
537 if (isConstruction) {
538 aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
539 aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
540 aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
543 aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
545 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
546 anAIS->setWidth(aWidth + 1);
547 anAIS->setLineStyle(aLineStyle);
551 return AISObjectPtr();*/
553 std::cout << "SketchPlugin_Split::getAISObject: " << data()->name() << std::endl;
556 AISObjectPtr anAIS = thePrevious;
558 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
559 GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
560 if (aPreviewShape.get())
561 aShapes.push_back(aPreviewShape);
562 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
563 if (aSelectedShape.get())
564 aShapes.push_back(aSelectedShape);
567 return AISObjectPtr();
569 GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
570 if (!aBaseShape.get())
571 return AISObjectPtr();
573 if (aBaseShape.get()) {
575 anAIS = AISObjectPtr(new GeomAPI_AISObject);
576 anAIS->createShape(aBaseShape);
578 std::vector<int> aColor;
579 aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
580 double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
581 int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
582 anAIS->setColor(aColor[0], aColor[1], aColor[2]);
583 // width when there is not base object should be extened in several points
584 // in order to see this preview over highlight
585 anAIS->setWidth(aWidth+4);
586 anAIS->setLineStyle(aLineStyle);
589 anAIS = AISObjectPtr();
593 //********************************************************************
594 void SketchPlugin_Split::fillObjectShapes(const ObjectPtr& theObject,
595 const ObjectPtr& theSketch)
597 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
598 std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
599 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
601 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
603 std::set<ResultPtr> anEdgeResults;
604 ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeResults);
605 if (!anEdgeResults.empty()) {
606 GeomShapePtr aFeatureShape = (*anEdgeResults.begin())->shape();
608 // coincidences to the feature
609 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
610 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
611 // layed on feature coincidences to divide it on several shapes
612 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
613 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
614 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
615 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
616 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
617 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
618 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
619 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
620 std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
621 ModelGeomAlgo_Point2D::getPointsInsideShape_p(aFeatureShape, aRefAttributes, aC->pnt(),
622 aX->dir(), aY, aPoints, aPointToAttributes);
624 if (!aPoints.empty())
625 GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
627 myCashedShapes[theObject] = aShapes;
628 myCashedReferences[theObject] = aPointToAttributes;
631 GeomShapePtr SketchPlugin_Split::getSubShape(const std::string& theObjectAttributeId,
632 const std::string& thePointAttributeId)
634 GeomShapePtr aBaseShape;
636 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
637 data()->attribute(theObjectAttributeId));
638 ObjectPtr aBaseObject = anObjectAttr->value();
639 if (!aBaseObject.get())
643 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
644 data()->attribute(thePointAttributeId));
645 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
646 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
647 anAttributePnt2d->y());
650 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
651 fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
653 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
654 if (!aShapes.empty()) {
655 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
656 for (; anIt != aLast; anIt++) {
657 GeomShapePtr aShape = *anIt;
658 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
659 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
664 if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
665 fillObjectShapes(aBaseObject, sketch()->data()->owner());
667 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
668 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
669 const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
670 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
671 for (; anIt != aLast; anIt++) {
672 GeomShapePtr aCurrentShape = *anIt;
673 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
674 if (ModelGeomAlgo_Point2D::isPointOnEdge(aCurrentShape, anAttributePnt, aProjectedPoint)) {
675 if (aCurrentShape->shapeType() == GeomAPI_Shape::EDGE) {
676 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aCurrentShape));
677 aStartPoint = anEdge->firstPoint();
678 aSecondPoint = anEdge->lastPoint();
684 if (!aStartPoint.get() || !aSecondPoint.get())
687 //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
688 // data()->attribute(SketchPlugin_Constraint::VALUE()));
689 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject/*aBaseObjectAttr->value()*/);
691 //AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
692 // data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
693 //AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
694 // data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
695 if (anObjectAttr->isInitialized() && aBaseFeature.get() && aPointAttr->isInitialized()) {
696 //aFirstPointAttrOfSplit->isInitialized() &&
697 //aSecondPointAttrOfSplit->isInitialized()) {
698 ResultPtr aResult = getFeatureResult(aBaseFeature);
699 GeomShapePtr aResultShape = aResult->shape();
700 std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
702 //std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
703 //std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
704 aPoints.push_back(aStartPoint);
706 //std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
707 //std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
708 // sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
709 aPoints.push_back(aSecondPoint);
711 std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
712 GeomAlgoAPI_ShapeTools::splitShape_p(aResultShape, aPoints, aSplitShapes);
713 aBaseShape = GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
719 void SketchPlugin_Split::getFeaturePoints(const FeaturePtr& theFeature,
720 AttributePoint2DPtr& theStartPointAttr,
721 AttributePoint2DPtr& theEndPointAttr)
723 std::string aFeatureKind = theFeature->getKind();
724 std::string aStartAttributeName, anEndAttributeName;
725 if (aFeatureKind == SketchPlugin_Line::ID()) {
726 aStartAttributeName = SketchPlugin_Line::START_ID();
727 anEndAttributeName = SketchPlugin_Line::END_ID();
729 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
730 aStartAttributeName = SketchPlugin_Arc::START_ID();
731 anEndAttributeName = SketchPlugin_Arc::END_ID();
733 if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
734 theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
735 theFeature->attribute(aStartAttributeName));
736 theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
737 theFeature->attribute(anEndAttributeName));
741 void SketchPlugin_Split::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
742 std::set<FeaturePtr>& theFeaturesToUpdate,
743 std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature)
745 std::shared_ptr<ModelAPI_Data> aData = data();
747 // Check the base objects are initialized.
748 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
749 data()->attribute(SELECTED_OBJECT()));
750 //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
751 // aData->attribute(SketchPlugin_Constraint::VALUE()));
752 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
753 ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
755 std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
756 std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
757 aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
759 std::set<AttributePtr>::const_iterator aIt;
760 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
761 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
762 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
763 std::string aRefFeatureKind = aRefFeature->getKind();
764 if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
765 aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
766 aRefFeatureKind == SketchPlugin_MultiTranslation::ID() ||
767 aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
768 theFeaturesToDelete.insert(aRefFeature);
769 else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
770 theFeaturesToUpdate.insert(aRefFeature);
771 else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
772 std::string anAttributeToBeModified;
773 AttributePoint2DPtr aCoincidentPoint;
774 AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
775 AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
776 bool isToFeature = false;
777 if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
778 FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
780 isToFeature = aFeature.get() && aFeature == aBaseFeature;
781 anAttributeToBeModified = anAttrA->id();
783 aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
785 isToFeature = aFeature.get() && aFeature == aBaseFeature;
786 anAttributeToBeModified = anAttrB->id();
789 aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
791 if (!isToFeature) { /// coincidence to point on base feature
792 AttributePtr anAttribute;
794 if (!anAttrA->isObject()) {
795 AttributePtr aCurAttribute = anAttrA->attr();
796 if (aCurAttribute.get()) {
797 FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
798 if (aCurFeature.get() && aCurFeature == aBaseFeature) {
799 anAttribute = anAttrB->attr();
800 anAttributeToBeModified = anAttrA->id();
804 if (!anAttribute.get() && !anAttrB->isObject()) {
805 AttributePtr aCurAttribute = anAttrB->attr();
806 if (aCurAttribute.get()) {
807 FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
808 if (aCurFeature.get() && aCurFeature == aBaseFeature) {
809 anAttribute = anAttrA->attr();
810 anAttributeToBeModified = anAttrB->id();
814 if (anAttribute.get())
815 aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
817 if (aCoincidentPoint.get() && isToFeature)
818 theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
824 void SketchPlugin_Split::getRefAttributes(const FeaturePtr& theFeature,
825 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
826 std::list<AttributePtr>& theRefsToFeature)
830 std::list<AttributePtr> aPointAttributes =
831 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
832 std::set<AttributePtr> aPointAttributesSet;
834 std::list<AttributePtr>::const_iterator aPIt =
835 aPointAttributes.begin(), aPLast = aPointAttributes.end();
836 for (; aPIt != aPLast; aPIt++)
837 aPointAttributesSet.insert(*aPIt);
839 std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
840 std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
841 aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
843 std::set<AttributePtr>::const_iterator aIt;
844 for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
845 AttributePtr anAttr = (*aIt);
846 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
847 if (anAttrFeature.get() != this &&
848 anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
849 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
850 if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
851 AttributePtr anAttrInRef = aRefAttr->attr();
852 if (anAttrInRef.get() &&
853 aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
854 if (theRefs.find(anAttrInRef) != theRefs.end())
855 theRefs[anAttrInRef].push_back(aRefAttr);
857 std::list<AttributePtr> anAttrList;
858 anAttrList.push_back(aRefAttr);
859 theRefs[anAttrInRef] = anAttrList;
863 else { /// find attributes referenced to feature itself
864 theRefsToFeature.push_back(anAttr);
870 void SketchPlugin_Split::updateCoincidenceConstraintsToFeature(
871 const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
872 const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
873 const std::set<ResultPtr>& theFeatureResults,
874 const FeaturePtr& theSplitFeature,
875 std::set<FeaturePtr>& theFeaturesToDelete)
877 if (theCoincidenceToFeature.empty())
880 // we should build coincidence constraints to end of the split feature
881 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aNewCoincidencesToSplitFeature;
882 AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
883 getFeaturePoints(theSplitFeature, aStartPointAttr, anEndPointAttr);
884 if (theFurtherCoincidences.find(aStartPointAttr) == theFurtherCoincidences.end())
885 aNewCoincidencesToSplitFeature.insert(aStartPointAttr);
886 if (theFurtherCoincidences.find(anEndPointAttr) == theFurtherCoincidences.end())
887 aNewCoincidencesToSplitFeature.insert(anEndPointAttr);
889 std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = theCoincidenceToFeature.begin(),
890 aCLast = theCoincidenceToFeature.end();
892 std::cout << std::endl;
893 std::cout << "Coincidences to feature(modified):"<< std::endl;
895 for (; aCIt != aCLast; aCIt++) {
896 FeaturePtr aCoincFeature = aCIt->first;
897 std::string anAttributeId = aCIt->second.first;
898 std::string aSecondAttribute = anAttributeId == SketchPlugin_Constraint::ENTITY_A() ?
899 SketchPlugin_Constraint::ENTITY_B() : SketchPlugin_Constraint::ENTITY_A();
901 AttributePoint2DPtr aCoincPoint = aCIt->second.second;
902 std::set<AttributePoint2DPtr>::const_iterator aFCIt = theFurtherCoincidences.begin(),
903 aFCLast = theFurtherCoincidences.end();
904 std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
905 AttributePoint2DPtr aFeaturePointAttribute;
906 for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
907 AttributePoint2DPtr aFCAttribute = *aFCIt;
908 if (aCoincPnt->isEqual(aFCAttribute->pnt()))
909 aFeaturePointAttribute = aFCAttribute;
911 if (aFeaturePointAttribute.get()) {
912 // create new constraint and remove the current
913 aCoincFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
914 SketchPlugin_ConstraintCoincidence::ID(),
915 aFeaturePointAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
916 theFeaturesToDelete.insert(aCIt->first);
917 // create new coincidences to split feature points
918 std::set<AttributePoint2DPtr>::const_iterator aSFIt = aNewCoincidencesToSplitFeature.begin(),
919 aSFLast = aNewCoincidencesToSplitFeature.end();
920 for (; aSFIt != aSFLast; aSFIt++) {
921 AttributePoint2DPtr aSFAttribute = *aSFIt;
922 if (aCoincPnt->isEqual(aSFAttribute->pnt())) {
923 SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
924 SketchPlugin_ConstraintCoincidence::ID(),
925 aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
930 /// find feature by shape intersected the point
931 ResultPtr aResultForCoincidence = *(theFeatureResults.begin());
933 if (theFeatureResults.size() > 1) { // try to find point on additional feature
934 ResultPtr anAddtionalResult = *(theFeatureResults.begin()++);
935 GeomShapePtr aShape = anAddtionalResult->shape();
937 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
938 std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
940 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
941 if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
942 aResultForCoincidence = anAddtionalResult;
944 aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
947 std::cout << " -" << getFeatureInfo(aCoincFeature) << std::endl;
952 void SketchPlugin_Split::updateRefFeatureConstraints(
953 const ResultPtr& theFeatureBaseResult,
954 const std::list<AttributePtr>& theRefsToFeature)
956 std::list<AttributePtr>::const_iterator anIt = theRefsToFeature.begin(),
957 aLast = theRefsToFeature.end();
958 for (; anIt != aLast; anIt++) {
959 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
961 aRefAttr->setObject(theFeatureBaseResult);
965 void SketchPlugin_Split::updateRefAttConstraints(
966 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
967 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes)
970 std::cout << "SketchPlugin_Split::updateRefAttConstraints" << std::endl;
973 std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
974 anIt = theModifiedAttributes.begin(), aLast = theModifiedAttributes.end();
975 for (; anIt != aLast; anIt++) {
976 AttributePtr anAttribute = anIt->first;
978 /// not found in references
979 if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
981 std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
982 std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
983 aRLast = aRefAttributes.end();
985 AttributePtr aNewAttribute = anIt->second;
986 for (; aRefIt != aRLast; aRefIt++) {
987 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
988 if (aRefAttr.get()) {
989 aRefAttr->setAttr(aNewAttribute);
991 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->owner());
992 std::cout << " -" << getFeatureInfo(aFeature) << std::endl;
999 FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature,
1000 FeaturePtr& theBaseFeatureModified,
1001 FeaturePtr& theAfterFeature,
1002 std::set<AttributePoint2DPtr>& thePoints,
1003 std::set<FeaturePtr>& theCreatedFeatures,
1004 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1006 FeaturePtr anNewFeature;
1008 std::set<FeaturePtr> aCreatedFeatures;
1009 FeaturePtr aConstraintFeature;
1010 theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1012 SketchPlugin_Sketch* aSketch = sketch();
1014 return anNewFeature;
1016 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1017 data()->attribute(SELECTED_OBJECT()));
1018 //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1019 // data()->attribute(SketchPlugin_Constraint::VALUE()));
1020 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1021 std::string aFeatureKind = aBaseFeature->getKind();
1022 if (aFeatureKind != SketchPlugin_Line::ID())
1023 return anNewFeature;
1025 AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1026 //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1027 AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1028 //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1029 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1031 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1032 if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1033 setError("Error: Feature has no start and end points.");
1034 return anNewFeature;
1037 arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
1038 aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1041 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1042 std::cout << "Start point: " <<
1043 ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1044 std::cout << "1st point: " <<
1045 ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1046 std::cout << "2nd point: " <<
1047 ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1048 std::cout << "End point: " <<
1049 ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1052 /// create a split feature
1054 createLineFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1055 theCreatedFeatures.insert(theSplitFeature);
1057 // before split feature
1058 if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1059 theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1060 theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1063 theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1064 /// move end arc point to start of split
1067 // after split feature
1068 if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1069 FeaturePtr aFeature;
1070 if (!theBaseFeatureModified.get()) {
1071 aFeature = aBaseFeature; ///< use base feature to store all constraints here
1072 fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), aSecondPointAttrOfSplit);
1073 aFeature->execute(); // to update result
1076 aFeature = createLineFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1077 theCreatedFeatures.insert(aFeature);
1078 theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1079 aFeature->attribute(SketchPlugin_Line::END_ID())));
1080 anNewFeature = aFeature;
1082 aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1083 SketchPlugin_ConstraintCoincidence::ID(),
1084 theSplitFeature->attribute(SketchPlugin_Line::END_ID()),
1085 aFeature->attribute(SketchPlugin_Line::START_ID()));
1086 theCreatedFeatures.insert(aConstraintFeature);
1088 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1089 (aFeature->attribute(SketchPlugin_Line::START_ID())));
1090 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1091 (aFeature->attribute(SketchPlugin_Line::END_ID())));
1093 if (!theBaseFeatureModified.get())
1094 theBaseFeatureModified = aFeature;
1096 theAfterFeature = aFeature;
1099 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1100 (theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1101 theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1102 theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1104 // base split, that is defined before split feature should be changed at end
1105 // (after the after feature creation). Otherwise modified value will be used in after feature
1106 // before split feature
1107 if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1108 /// move end arc point to start of split
1109 fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1110 aFirstPointAttrOfSplit);
1111 theBaseFeatureModified->execute(); // to update result
1112 aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1113 SketchPlugin_ConstraintCoincidence::ID(),
1114 theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1115 theSplitFeature->attribute(SketchPlugin_Line::START_ID()));
1116 theCreatedFeatures.insert(aConstraintFeature);
1118 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1119 (theBaseFeatureModified->attribute(SketchPlugin_Line::START_ID())));
1120 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1121 (theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID())));
1124 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1125 (theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1127 #ifdef CREATE_CONSTRAINTS
1128 // additional constraints between split and base features
1129 aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1130 SketchPlugin_ConstraintParallel::ID(),
1131 getFeatureResult(aBaseFeature),
1132 getFeatureResult(theSplitFeature));
1133 theCreatedFeatures.insert(aConstraintFeature);
1134 if (theAfterFeature.get()) {
1135 aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1136 SketchPlugin_ConstraintParallel::ID(),
1137 getFeatureResult(aBaseFeature),
1138 getFeatureResult(theAfterFeature));
1139 theCreatedFeatures.insert(aConstraintFeature);
1142 return anNewFeature;
1145 FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature,
1146 FeaturePtr& theBaseFeatureModified,
1147 FeaturePtr& theAfterFeature,
1148 std::set<AttributePoint2DPtr>& thePoints,
1149 std::set<FeaturePtr>& theCreatedFeatures,
1150 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1152 FeaturePtr anNewFeature;
1154 std::set<FeaturePtr> aCreatedFeatures;
1155 FeaturePtr aConstraintFeature;
1156 theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1158 SketchPlugin_Sketch* aSketch = sketch();
1160 return anNewFeature;
1162 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1163 data()->attribute(SELECTED_OBJECT()));
1164 //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1165 // data()->attribute(SketchPlugin_Constraint::VALUE()));
1166 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1167 std::string aFeatureKind = aBaseFeature->getKind();
1168 if (aFeatureKind != SketchPlugin_Arc::ID())
1169 return anNewFeature;
1171 AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1172 //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1173 AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1174 //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1175 AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1176 getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1177 if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1178 setError("Error: Feature has no start and end points.");
1179 return anNewFeature;
1182 arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1183 aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1185 std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1186 std::cout << "Start point: " <<
1187 ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1188 std::cout << "1st point: " <<
1189 ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1190 std::cout << "2nd point: " <<
1191 ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1192 std::cout << "End point: " <<
1193 ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1197 theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1198 theCreatedFeatures.insert(theSplitFeature);
1200 // before split feature
1201 if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1202 theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1203 theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1206 theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1207 /// move end arc point to start of split
1210 // after split feature
1211 if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1212 FeaturePtr aFeature;
1213 if (!theBaseFeatureModified.get()) {
1214 aFeature = aBaseFeature; ///< use base feature to store all constraints here
1215 fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttrOfSplit);
1216 aFeature->execute(); // to update result
1219 aFeature = createArcFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1220 theCreatedFeatures.insert(aFeature);
1221 theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1222 aFeature->attribute(SketchPlugin_Arc::END_ID())));
1223 anNewFeature = aFeature;
1225 aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1226 SketchPlugin_ConstraintCoincidence::ID(),
1227 theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
1228 aFeature->attribute(SketchPlugin_Arc::START_ID()));
1229 theCreatedFeatures.insert(aConstraintFeature);
1231 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1232 (aFeature->attribute(SketchPlugin_Arc::START_ID())));
1233 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1234 (aFeature->attribute(SketchPlugin_Arc::END_ID())));
1236 if (!theBaseFeatureModified.get())
1237 theBaseFeatureModified = aFeature;
1239 theAfterFeature = aFeature;
1242 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1243 (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1244 theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1245 theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1247 // base split, that is defined before split feature should be changed at end
1248 // (after the after feature creation). Otherwise modified value will be used in after feature
1249 // before split feature
1250 if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1251 /// move end arc point to start of split
1252 fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1253 aFirstPointAttrOfSplit);
1254 theBaseFeatureModified->execute(); // to update result
1255 aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1256 SketchPlugin_ConstraintCoincidence::ID(),
1257 theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1258 theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1259 theCreatedFeatures.insert(aConstraintFeature);
1261 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1262 (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1263 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1264 (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1267 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1268 (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1270 // additional constraints between split and base features
1271 #ifdef CREATE_CONSTRAINTS
1272 aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1273 SketchPlugin_ConstraintEqual::ID(),
1274 getFeatureResult(aBaseFeature),
1275 getFeatureResult(theSplitFeature));
1276 theCreatedFeatures.insert(aConstraintFeature);
1277 aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1278 SketchPlugin_ConstraintTangent::ID(),
1279 getFeatureResult(theSplitFeature),
1280 getFeatureResult(aBaseFeature));
1281 theCreatedFeatures.insert(aConstraintFeature);
1282 if (theAfterFeature.get()) {
1283 aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1284 SketchPlugin_ConstraintEqual::ID(),
1285 getFeatureResult(aBaseFeature),
1286 getFeatureResult(theAfterFeature));
1287 theCreatedFeatures.insert(aConstraintFeature);
1288 aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1289 SketchPlugin_ConstraintTangent::ID(),
1290 getFeatureResult(theSplitFeature),
1291 getFeatureResult(theAfterFeature));
1292 theCreatedFeatures.insert(aConstraintFeature);
1295 return anNewFeature;
1298 FeaturePtr SketchPlugin_Split::splitCircle(FeaturePtr& theSplitFeature,
1299 FeaturePtr& theBaseFeatureModified,
1300 FeaturePtr& theAfterFeature,
1301 std::set<AttributePoint2DPtr>& thePoints,
1302 std::set<FeaturePtr>& theCreatedFeatures,
1303 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1305 FeaturePtr anNewFeature;
1307 std::set<FeaturePtr> aCreatedFeatures;
1308 FeaturePtr aConstraintFeature;
1309 theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1311 SketchPlugin_Sketch* aSketch = sketch();
1313 return anNewFeature;
1315 AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1316 data()->attribute(SELECTED_OBJECT()));
1317 //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1318 // data()->attribute(SketchPlugin_Constraint::VALUE()));
1319 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1320 std::string aFeatureKind = aBaseFeature->getKind();
1321 if (aFeatureKind != SketchPlugin_Circle::ID())
1322 return anNewFeature;
1324 AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1325 //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1326 AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1327 //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1331 createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1332 bool aSplitReversed = std::dynamic_pointer_cast<SketchPlugin_Arc>(theSplitFeature)->isReversed();
1333 theCreatedFeatures.insert(theSplitFeature);
1335 /// base feature is a left part of the circle
1336 theBaseFeatureModified = createArcFeature(aBaseFeature,
1337 aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1338 anNewFeature = theBaseFeatureModified;
1339 std::dynamic_pointer_cast<SketchPlugin_Arc>(
1340 theBaseFeatureModified)->setReversed(!aSplitReversed);
1341 theBaseFeatureModified->execute();
1343 theModifiedAttributes.insert(
1344 std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1345 theBaseFeatureModified->attribute(SketchPlugin_Arc::CENTER_ID())));
1347 theCreatedFeatures.insert(theBaseFeatureModified);
1349 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1350 (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1351 thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1352 (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1354 // additional constraints between split and base features
1355 aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1356 SketchPlugin_ConstraintCoincidence::ID(),
1357 theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1358 theSplitFeature->attribute(SketchPlugin_Arc::END_ID()));
1359 theCreatedFeatures.insert(aConstraintFeature);
1360 aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1361 SketchPlugin_ConstraintCoincidence::ID(),
1362 theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()),
1363 theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1364 theCreatedFeatures.insert(aConstraintFeature);
1366 #ifdef CREATE_CONSTRAINTS
1367 aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1368 SketchPlugin_ConstraintTangent::ID(),
1369 getFeatureResult(theSplitFeature),
1370 getFeatureResult(theBaseFeatureModified));
1371 theCreatedFeatures.insert(aConstraintFeature);
1373 return anNewFeature;
1376 void SketchPlugin_Split::arrangePointsOnLine(
1377 const AttributePoint2DPtr& theStartPointAttr,
1378 const AttributePoint2DPtr& theEndPointAttr,
1379 AttributePoint2DPtr& theFirstPointAttr,
1380 AttributePoint2DPtr& theLastPointAttr) const
1382 // if first point is closer to last point, swap first and last values
1383 if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
1384 theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
1385 AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1386 theFirstPointAttr = theLastPointAttr;
1387 theLastPointAttr = aTmpPoint;
1391 void SketchPlugin_Split::arrangePointsOnArc(
1392 const FeaturePtr& theArc,
1393 const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
1394 const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
1395 std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
1396 std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const
1398 static const double anAngleTol = 1.e-12;
1400 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1401 theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1402 bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1404 // collect directions to each point
1405 std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1406 new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1407 std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1408 new GeomAPI_Dir2d(theFirstPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1409 std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1410 new GeomAPI_Dir2d(theSecondPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1412 // sort points by their angular values
1413 double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1414 double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1415 double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1416 if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1417 aFirstPtAngle += aPeriod;
1418 if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1419 aSecondPtAngle += aPeriod;
1421 if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1422 AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1423 theFirstPointAttr = theSecondPointAttr;
1424 theSecondPointAttr = aTmpPoint;
1428 void SketchPlugin_Split::fillAttribute(const AttributePtr& theModifiedAttribute,
1429 const AttributePtr& theSourceAttribute)
1431 std::string anAttributeType = theModifiedAttribute->attributeType();
1432 if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1433 AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1434 theModifiedAttribute);
1435 AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1436 theSourceAttribute);
1438 if (aModifiedAttribute.get() && aSourceAttribute.get())
1439 aModifiedAttribute->setValue(aSourceAttribute->pnt());
1441 else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1442 AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1443 theModifiedAttribute);
1444 AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1445 theSourceAttribute);
1447 if (aModifiedAttribute.get() && aSourceAttribute.get())
1448 aModifiedAttribute->setValue(aSourceAttribute->value());
1452 FeaturePtr SketchPlugin_Split::createLineFeature(const FeaturePtr& theBaseFeature,
1453 const AttributePtr& theFirstPointAttr,
1454 const AttributePtr& theSecondPointAttr)
1456 FeaturePtr aFeature;
1457 SketchPlugin_Sketch* aSketch = sketch();
1458 if (!aSketch || !theBaseFeature.get())
1461 aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1463 fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPointAttr);
1464 fillAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPointAttr);
1466 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1467 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1469 aFeature->execute(); // to obtain result
1474 FeaturePtr SketchPlugin_Split::createArcFeature(const FeaturePtr& theBaseFeature,
1475 const AttributePtr& theFirstPointAttr,
1476 const AttributePtr& theSecondPointAttr)
1478 FeaturePtr aFeature;
1479 SketchPlugin_Sketch* aSketch = sketch();
1480 if (!aSketch || !theBaseFeature.get())
1483 std::string aCenterAttributeId;
1484 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1485 aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1486 else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1487 aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1489 if (aCenterAttributeId.empty())
1492 aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1493 // update fillet arc: make the arc correct for sure, so, it is not needed to process
1494 // the "attribute updated"
1495 // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1496 bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1498 fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1499 theBaseFeature->attribute(aCenterAttributeId));
1500 fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
1501 fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
1503 fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1504 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1506 /// fill referersed state of created arc as it is on the base arc
1507 if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1508 bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1509 aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1511 aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1512 aFeature->execute(); // to obtain result
1517 void SketchPlugin_Split::updateFeaturesAfterSplit(
1518 const std::set<FeaturePtr>& theFeaturesToUpdate)
1520 std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
1521 aLast = theFeaturesToUpdate.end();
1522 for (; anIt != aLast; anIt++) {
1523 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1524 std::string aRefFeatureKind = aRefFeature->getKind();
1525 if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
1526 std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
1527 std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
1528 if (aLenghtFeature.get()) {
1529 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
1530 ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
1532 if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
1533 aValueAttr->setValue(aValue);
1539 std::shared_ptr<ModelAPI_Result> SketchPlugin_Split::getFeatureResult(
1540 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1542 std::shared_ptr<ModelAPI_Result> aResult;
1544 std::string aFeatureKind = theFeature->getKind();
1545 if (aFeatureKind == SketchPlugin_Line::ID())
1546 aResult = theFeature->firstResult();
1547 else if (aFeatureKind == SketchPlugin_Arc::ID())
1548 aResult = theFeature->lastResult();
1549 else if (aFeatureKind == SketchPlugin_Circle::ID())
1550 aResult = theFeature->lastResult();
1556 std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_Split::getEdgeAttributes(
1557 const std::shared_ptr<ModelAPI_Feature>& theFeature)
1559 std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
1561 std::string aFeatureKind = theFeature->getKind();
1562 if (aFeatureKind == SketchPlugin_Line::ID()) {
1563 anAttributes.insert(theFeature->attribute(SketchPlugin_Line::START_ID()));
1564 anAttributes.insert(theFeature->attribute(SketchPlugin_Line::END_ID()));
1566 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1567 anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::START_ID()));
1568 anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::END_ID()));
1570 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
1573 return anAttributes;
1577 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointAttribute
1578 (const bool isFirstAttribute)
1580 std::shared_ptr<GeomDataAPI_Point2D> anAttribute;
1582 GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
1583 if (!aSelectedShape.get())
1586 if (aSelectedShape->shapeType() != GeomAPI_Shape::EDGE)
1589 AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1590 data()->attribute(SELECTED_OBJECT()));
1591 ObjectPtr aBaseObject = anObjectAttr->value();
1592 if (!aBaseObject.get())
1595 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelectedShape));
1597 std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
1598 std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
1600 std::shared_ptr<GeomDataAPI_Point2D> aFirstPointAttr, aLastPointAttr;
1601 /// find the points in feature attributes
1602 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1603 std::list<AttributePtr> a2DPointAttributes = aBaseFeature->data()->attributes(
1604 GeomDataAPI_Point2D::typeId());
1605 std::list<AttributePtr>::const_iterator anIt = a2DPointAttributes.begin(),
1606 aLast = a2DPointAttributes.end();
1607 for (; anIt != aLast; anIt++) {
1608 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
1609 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
1610 std::shared_ptr<GeomAPI_Pnt2d> aPoint2D = anAttributePoint->pnt();
1611 std::shared_ptr<GeomAPI_Pnt> aPoint3D = sketch()->to3D(aPoint2D->x(), aPoint2D->y());
1612 if (aFirstPnt->isEqual(aPoint3D))
1613 aFirstPointAttr = anAttributePoint;
1614 else if (aLastPnt->isEqual(aPoint3D))
1615 aLastPointAttr = anAttributePoint;
1618 /// find the points in coincident features
1619 PntToAttributesMap aRefAttributes = myCashedReferences[aBaseObject];
1620 PntToAttributesMap::const_iterator
1621 aRIt = aRefAttributes.begin(), aRLast = aRefAttributes.end();
1622 for (; aRIt != aRLast; aRIt++) {
1623 std::shared_ptr<GeomDataAPI_Point2D> anAttribute = aRIt->first;
1624 std::shared_ptr<GeomAPI_Pnt> aPoint = aRIt->second;
1625 if (!aFirstPointAttr.get() && aFirstPnt->isEqual(aPoint))
1626 aFirstPointAttr = anAttribute;
1627 if (!aLastPointAttr.get() && aLastPnt->isEqual(aPoint))
1628 aLastPointAttr = anAttribute;
1629 if (aFirstPointAttr.get() && aLastPointAttr.get())
1632 if (!aFirstPointAttr.get() || !aLastPointAttr)
1635 return isFirstAttribute ? aFirstPointAttr : aLastPointAttr;
1639 std::string SketchPlugin_Split::getFeatureInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
1640 const bool isUseAttributesInfo)
1643 if (!theFeature.get()) {
1647 if (theFeature->data()->isValid())
1648 anInfo.append(theFeature->data()->name().c_str());
1650 if (isUseAttributesInfo) {
1651 std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(theFeature,
1652 getEdgeAttributes(theFeature));
1653 /// processing of feature with point 2d attributes, like line, arc, circle
1654 if (!aPointsInfo.empty()) {
1657 anInfo += aPointsInfo;
1659 else { /// process constraint coincidence, find points in ref attr attributes
1660 std::list<AttributePtr> anAttrs = theFeature->data()->attributes(
1661 ModelAPI_AttributeRefAttr::typeId());
1662 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
1663 std::string anAttributesInfo;
1664 for(; anIt != aLast; anIt++) {
1665 if (!anAttributesInfo.empty()) {
1666 anAttributesInfo.append(", ");
1667 anAttributesInfo += "\n";
1669 AttributePtr anAttr = *anIt;
1670 std::string aValue = "not defined";
1671 std::string aType = anAttr->attributeType();
1672 if (aType == ModelAPI_AttributeRefAttr::typeId()) {
1673 std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1674 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
1675 if (aRefAttr.get()) {
1676 if (aRefAttr->isObject()) {
1677 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1678 aValue = "<object:>" + getFeatureInfo(aFeature, false);
1681 AttributePtr anAttribute = aRefAttr->attr();
1682 if (anAttribute.get()) {
1683 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
1684 aValue = "<attr:>" + ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute) +
1685 " [" + getFeatureInfo(aFeature, false) + "]";
1690 anAttributesInfo.append(" " + anAttr->id() + ": " + aValue);
1692 if (!anAttributesInfo.empty())
1693 anInfo = anInfo + "\n" + anAttributesInfo;