1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <SketchSolver_ConstraintMirror.h>
4 #include <SketchSolver_Error.h>
5 #include <SketchSolver_Manager.h>
7 #include <ModelAPI_AttributeRefList.h>
9 void SketchSolver_ConstraintMirror::getAttributes(
10 EntityWrapperPtr& theMirrorLine,
11 std::vector<EntityWrapperPtr>& theBaseEntities,
12 std::vector<EntityWrapperPtr>& theMirrorEntities)
14 AttributePtr aMirLineAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A());
15 AttributeRefAttrPtr aMirLineRefAttr =
16 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aMirLineAttr);
17 if (!aMirLineRefAttr || !aMirLineRefAttr->isInitialized() || !aMirLineRefAttr->isObject()) {
18 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
22 myType = TYPE(myBaseConstraint);
23 myStorage->update(aMirLineAttr/*, myGroupID*/);
24 theMirrorLine = myStorage->entity(aMirLineAttr);
26 // Create SolveSpace entity for all features
27 AttributeRefListPtr aBaseRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
28 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
29 AttributeRefListPtr aMirroredRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
30 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_C()));
31 myNumberOfObjects = aMirroredRefList->size();
32 if (!aBaseRefList || !aMirroredRefList) {
33 myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
37 std::list<ObjectPtr> aBaseList = aBaseRefList->list();
38 std::list<ObjectPtr> aMirroredList = aMirroredRefList->list();
41 for (int i = 0; i < 2; i++) {
42 std::list<ObjectPtr>::iterator anIter = i == 0 ? aBaseList.begin() : aMirroredList.begin();
43 std::list<ObjectPtr>::iterator aEndIter = i == 0 ? aBaseList.end() : aMirroredList.end();
44 std::vector<EntityWrapperPtr>* aList = i == 0 ? &theBaseEntities : & theMirrorEntities;
45 for ( ; anIter != aEndIter; anIter++) {
46 aFeature = ModelAPI_Feature::feature(*anIter);
50 myStorage->update(aFeature, GID_UNKNOWN, true);
51 aList->push_back(myStorage->entity(aFeature));
55 if (theBaseEntities.size() > theMirrorEntities.size() || aMirroredList.empty())
56 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
59 void SketchSolver_ConstraintMirror::process()
62 if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
63 // Not enough parameters are assigned
67 EntityWrapperPtr aMirrorLine;
68 std::vector<EntityWrapperPtr> aBaseList;
69 std::vector<EntityWrapperPtr> aMirrorList;
70 getAttributes(aMirrorLine, aBaseList, aMirrorList);
71 if (!myErrorMsg.empty())
74 if (aBaseList.size() != aMirrorList.size()) {
75 myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
79 std::list<ConstraintWrapperPtr> aNewConstraints;
80 SketchSolver_ConstraintType aConstrType = getType();
81 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
82 std::list<ConstraintWrapperPtr> aMirConstrList;
84 // update mirrored features to be in the current group
85 std::vector<EntityWrapperPtr>::iterator aMIt = aMirrorList.begin();
86 for (; aMIt != aMirrorList.end(); ++aMIt)
87 myStorage->update((*aMIt)->baseFeature(), myGroupID);
89 std::vector<EntityWrapperPtr>::iterator aBIt = aBaseList.begin();
90 for (aMIt = aMirrorList.begin(); aBIt != aBaseList.end(); ++aBIt, ++aMIt) {
91 aNewConstraints = aBuilder->createConstraint(
92 myBaseConstraint, myGroupID, mySketchID, aConstrType,
93 0.0, *aBIt, *aMIt, aMirrorLine);
94 aMirConstrList.insert(aMirConstrList.end(), aNewConstraints.begin(), aNewConstraints.end());
96 myStorage->addConstraint(myBaseConstraint, aMirConstrList);
102 void SketchSolver_ConstraintMirror::update()
109 void SketchSolver_ConstraintMirror::adjustConstraint()
111 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
113 const std::list<ConstraintWrapperPtr>& aConstraints = myStorage->constraint(myBaseConstraint);
114 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aConstraints.begin();
115 for (; aCIt != aConstraints.end(); ++aCIt)
116 if ((*aCIt)->type() == CONSTRAINT_SYMMETRIC)
117 aBuilder->adjustConstraint(*aCIt);