]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintMirror.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintMirror.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintMirror.cpp
4 // Created: 17 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintMirror.h"
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_Session.h>
14
15 #include <SketchPlugin_Line.h>
16 #include <SketchPlugin_Sketch.h>
17
18 #include <SketcherPrs_Factory.h>
19
20 #include <Config_PropManager.h>
21
22 SketchPlugin_ConstraintMirror::SketchPlugin_ConstraintMirror()
23 {
24 }
25
26 void SketchPlugin_ConstraintMirror::initAttributes()
27 {
28   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
29   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
30   data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
31 }
32
33 void SketchPlugin_ConstraintMirror::execute()
34 {
35   // Objects to be mirrored will be created here
36   std::shared_ptr<ModelAPI_Data> aData = data();
37   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
38       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
39   if (!aRefListOfShapes->isInitialized())
40     return ;
41
42   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
43       aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
44   // Check consistency of initial list and mirrored list
45   std::list<ObjectPtr> anInitialList =  aRefListOfShapes->list();
46   std::list<ObjectPtr> aMirroredList =  aRefListOfMirrored->list();
47   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
48   std::list<ObjectPtr>::iterator aMirrorIter = aMirroredList.begin();
49   int indFirstWrong = 0; // index of element starts difference in the lists
50   std::set<int> anInvalidInd; // list of indices of removed features
51   std::shared_ptr<SketchPlugin_Feature> aFeatureIn, aFeatureOut;
52   for ( ; anInitIter != anInitialList.end(); anInitIter++, indFirstWrong++) {
53     // Add features and store indices of objects to remove
54     aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
55     aFeatureOut = aMirrorIter != aMirroredList.end() ? 
56         std::dynamic_pointer_cast<SketchPlugin_Feature>(*aMirrorIter) :
57         std::shared_ptr<SketchPlugin_Feature>();
58     if (!aFeatureIn) {
59       if (aFeatureOut)
60         break; // the lists are inconsistent
61       continue;
62     }
63     if (!aFeatureOut) {
64       if (aMirrorIter != aMirroredList.end())
65         break; // the lists are inconsistent
66       // There is no mirrored object yet, create it
67       FeaturePtr aNewFeature = sketch()->addFeature(aFeatureIn->getKind());
68       aFeatureIn->data()->copyTo(aNewFeature->data());
69       aRefListOfMirrored->append(aNewFeature);
70       continue;
71     }
72     if (aFeatureIn->getKind() != aFeatureOut->getKind())
73       break; // the lists are inconsistent
74     if (!aFeatureIn->data()->isValid()) {
75       // initial feature was removed, delete it from lists
76       anInvalidInd.insert(indFirstWrong);
77     }
78     aMirrorIter++;
79   }
80   // Remove from the list objects already deleted before
81   std::set<int>::reverse_iterator anIt = anInvalidInd.rbegin();
82   for ( ; anIt != anInvalidInd.rend(); anIt++) {
83     if (*anIt < indFirstWrong) indFirstWrong--;
84     aRefListOfShapes->remove(aRefListOfShapes->object(*anIt));
85     aRefListOfMirrored->remove(aRefListOfMirrored->object(*anIt));
86   }
87   // If the lists inconsistent, remove all objects from mirrored list starting from indFirstWrong
88   if (anInitIter != anInitialList.end()) {
89     while (aRefListOfMirrored->size() > indFirstWrong)
90       aRefListOfMirrored->remove(aRefListOfMirrored->object(indFirstWrong));
91     // Create mirrored features instead of removed
92     anInitialList =  aRefListOfShapes->list();
93     anInitIter = anInitialList.begin();
94     for (int i = 0; i < indFirstWrong; i++) anInitIter++;
95     for ( ; anInitIter != anInitialList.end(); anInitIter++) {
96       aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
97       FeaturePtr aNewFeature = aFeatureIn->document()->addFeature(aFeatureIn->getKind());
98       aRefListOfMirrored->append(aNewFeature);
99     }
100   }
101 }
102
103 AISObjectPtr SketchPlugin_ConstraintMirror::getAISObject(AISObjectPtr thePrevious)
104 {
105   if (!sketch())
106     return thePrevious;
107
108   AISObjectPtr anAIS = thePrevious;
109   /// TODO: Equal constraint presentation should be put here
110   return anAIS;
111 }
112
113