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 <SketchSolver_ConstraintMirror.h>
22 #include <SketchSolver_Error.h>
24 #include <PlaneGCSSolver_Tools.h>
25 #include <PlaneGCSSolver_UpdateFeature.h>
27 #include <GeomAPI_XY.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <ModelAPI_AttributeRefList.h>
30 #include <SketchPlugin_Arc.h>
31 #include <SketchPlugin_Circle.h>
34 static void mirrorPoints(const std::shared_ptr<GeomAPI_Lin2d>& theMirrorLine,
35 const AttributePoint2DPtr& theOriginal,
36 const AttributePoint2DPtr& theMirrored);
38 static void mirrorEntities(const std::shared_ptr<GeomAPI_Lin2d>& theMirrorLine,
39 const FeaturePtr& theOriginal,
40 const FeaturePtr& theMirrored);
44 void SketchSolver_ConstraintMirror::getAttributes(
46 std::vector<EntityWrapperPtr>&)
48 AttributeRefAttrPtr aMirLineRefAttr =
49 myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
50 if (!aMirLineRefAttr || !aMirLineRefAttr->isInitialized() || !aMirLineRefAttr->isObject()) {
51 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
55 FeaturePtr aMirrorLine = ModelAPI_Feature::feature(aMirLineRefAttr->object());
56 myFeatures.insert(aMirrorLine);
58 myType = TYPE(myBaseConstraint);
59 myStorage->update(aMirrorLine);
62 AttributeRefListPtr aBaseRefList =
63 myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_B());
64 AttributeRefListPtr aMirroredRefList =
65 myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_C());
66 myNumberOfObjects = aMirroredRefList->size();
67 if (!aBaseRefList || !aMirroredRefList) {
68 myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
72 // store only original entities because mirrored ones
73 // will be updated separately in adjustConstraint
74 std::list<ObjectPtr> aList = aBaseRefList->list();
75 std::list<ObjectPtr>::iterator anIt = aList.begin();
76 for (; anIt != aList.end(); ++anIt) {
77 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
79 myStorage->update(aFeature);
80 myFeatures.insert(aFeature);
83 // add mirrored features to the list
84 aList = aMirroredRefList->list();
85 for (anIt = aList.begin(); anIt != aList.end(); ++anIt) {
86 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
88 myFeatures.insert(aFeature);
92 void SketchSolver_ConstraintMirror::process()
95 if (!myBaseConstraint || !myStorage) {
96 // Not enough parameters are assigned
100 EntityWrapperPtr aMirrorLine;
101 std::vector<EntityWrapperPtr> aBaseList;
102 getAttributes(aMirrorLine, aBaseList);
103 if (!myErrorMsg.empty())
107 myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateFeature::GROUP());
111 void SketchSolver_ConstraintMirror::update()
118 void SketchSolver_ConstraintMirror::adjustConstraint()
120 AttributeRefAttrPtr aMirrLineRefAttr =
121 myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
122 std::shared_ptr<GeomAPI_Lin2d> aMirrorLine =
123 PlaneGCSSolver_Tools::line(ModelAPI_Feature::feature(aMirrLineRefAttr->object()));
125 AttributeRefListPtr aBaseRefList =
126 myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_B());
127 AttributeRefListPtr aMirroredRefList =
128 myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_C());
130 std::list<ObjectPtr> aBaseList = aBaseRefList->list();
131 std::list<ObjectPtr> aMirroredList = aMirroredRefList->list();
132 std::list<ObjectPtr>::iterator aBIt, aMIt;
133 for (aBIt = aBaseList.begin(), aMIt = aMirroredList.begin();
134 aBIt != aBaseList.end() && aMIt != aMirroredList.end();
136 FeaturePtr aBase = ModelAPI_Feature::feature(*aBIt);
137 FeaturePtr aMirrored = ModelAPI_Feature::feature(*aMIt);
138 mirrorEntities(aMirrorLine, aBase, aMirrored);
140 // update mirrored entity if it exists in the storage
141 if (myStorage->entity(aMirrored))
142 myStorage->update(aMirrored);
146 void SketchSolver_ConstraintMirror::notify(const FeaturePtr& theFeature,
147 PlaneGCSSolver_Update*)
149 if (myFeatures.find(theFeature) == myFeatures.end())
150 return; // the feature is not used by constraint => nothing to update
154 void SketchSolver_ConstraintMirror::blockEvents(bool isBlocked)
156 std::set<FeaturePtr>::iterator anIt = myFeatures.begin();
157 for (; anIt != myFeatures.end(); ++anIt)
158 (*anIt)->data()->blockSendAttributeUpdated(isBlocked);
160 SketchSolver_Constraint::blockEvents(isBlocked);
166 // ================= Auxiliary functions ==================================
167 void mirrorPoints(const std::shared_ptr<GeomAPI_Lin2d>& theMirrorLine,
168 const AttributePoint2DPtr& theOriginal,
169 const AttributePoint2DPtr& theMirrored)
171 std::shared_ptr<GeomAPI_Pnt2d> anOriginal = theOriginal->pnt();
172 std::shared_ptr<GeomAPI_Pnt2d> aPtOnLine = theMirrorLine->project(anOriginal);
173 std::shared_ptr<GeomAPI_XY> aPerp = aPtOnLine->xy()->decreased(anOriginal->xy());
174 theMirrored->setValue(anOriginal->x() + aPerp->x() * 2.0, anOriginal->y() + aPerp->y() * 2.0);
177 void mirrorEntities(const std::shared_ptr<GeomAPI_Lin2d>& theMirrorLine,
178 const FeaturePtr& theOriginal,
179 const FeaturePtr& theMirrored)
181 std::list<AttributePtr> aPoints0 =
182 theOriginal->data()->attributes(GeomDataAPI_Point2D::typeId());
183 std::list<AttributePtr> aPoints1 =
184 theMirrored->data()->attributes(GeomDataAPI_Point2D::typeId());
186 // process specific features
187 if (theOriginal->getKind() == SketchPlugin_Arc::ID()) {
188 // orientation of arc
189 theMirrored->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(
190 !theOriginal->boolean(SketchPlugin_Arc::REVERSED_ID())->value());
191 } else if (theOriginal->getKind() == SketchPlugin_Circle::ID()) {
192 // radius of the circle
193 theMirrored->real(SketchPlugin_Circle::RADIUS_ID())->setValue(
194 theOriginal->real(SketchPlugin_Circle::RADIUS_ID())->value());
197 // mirror all initialized points of features
198 std::list<AttributePtr>::iterator anIt0, anIt1;
199 for (anIt0 = aPoints0.begin(), anIt1 = aPoints1.begin();
200 anIt0 != aPoints0.end() && anIt1 != aPoints1.end(); ++anIt0, ++anIt1) {
201 AttributePoint2DPtr aPt0 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt0);
202 AttributePoint2DPtr aPt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt1);
203 if (aPt0->isInitialized() && aPt1->isInitialized())
204 mirrorPoints(theMirrorLine, aPt0, aPt1);