Salome HOME
Issue #2171: Avoid error message appears for tangency constraint
[modules/shaper.git] / src / PythonAddons / macros / rectangle / feature.py
index aaa65512a20296f9696a03efefb497c6ce131b34..8a5cfa6b7ed363ac1507c07d5d77bab02ed0638f 100644 (file)
@@ -4,7 +4,7 @@ Author: Artem ZHIDKOV
 Copyright (C) 2016-20xx CEA/DEN, EDF R&D
 """
 
-import model
+from salome.shaper import model
 import ModelAPI
 import GeomDataAPI
 
@@ -77,19 +77,12 @@ class SketchPlugin_Rectangle(model.Feature):
         # Retrieving list of already created lines
         aLinesList = self.reflist(self.LINES_LIST_ID())
         aNbLines = aLinesList.size()
-        if aNbLines == 0:
-            # Search the sketch containing this rectangle
-            self.__sketch = None
-            aRefs = self.data().refsToMe();
-            for iter in aRefs:
-                aFeature = ModelAPI.objectToFeature(iter.owner())
-                if aFeature.getKind() == "Sketch":
-                    self.__sketch = ModelAPI.featureToCompositeFeature(aFeature)
-                    break
-            # Create lines to compose the rectangle
-            for i in range (0, 4):
+        if aNbLines == 1:
+            # Create 1-4 lines to compose the rectangle
+            for i in range (0, 3):
                 aLine = self.__sketch.addFeature("SketchLine")
                 aLinesList.append(aLine)
+            self.updateLines()
             aNbLines = aLinesList.size()
             # Create constraints to keep the rectangle
             for i in range (0, aNbLines):
@@ -106,8 +99,8 @@ class SketchPlugin_Rectangle(model.Feature):
                 aRefAttrB.setAttr(aLine.attribute("StartPoint"))
             # Flags which show horizontal or vertical constraint is build for correponding line
             self.__isHV = [False, False, False, False]
-        # Update coordinates of created lines
-        self.updateLines()
+            # Update coordinates of created lines
+            self.updateLines()
         # Add horizontal and vertical constraint for the lines which already have result
         for i in range (0, aNbLines):
             if self.__isHV[i]:
@@ -126,16 +119,43 @@ class SketchPlugin_Rectangle(model.Feature):
 
     def attributeChanged(self, theID):
         if theID == self.START_ID() or theID == self.END_ID():
+            # Search the sketch containing this rectangle
+            self.__sketch = None
+            aRefs = self.data().refsToMe();
+            for iter in aRefs:
+                aFeature = ModelAPI.objectToFeature(iter.owner())
+                if aFeature.getKind() == "Sketch":
+                    self.__sketch = ModelAPI.featureToCompositeFeature(aFeature)
+                    break
+
+            aLinesList = self.reflist(self.LINES_LIST_ID())
+            aNbLines = aLinesList.size()
+            if aNbLines == 0:
+                # Create first line to be able to create a coincidence with selected point/feature
+                for i in range (0, 1):
+                    aLine = self.__sketch.addFeature("SketchLine")
+                    aLinesList.append(aLine)
+
             aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
             aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID()))
-            if aStartPoint.isInitialized() and aEndPoint.isInitialized:
-                self.updateLines()
+            if aStartPoint.isInitialized() and aEndPoint.isInitialized():
+              self.updateLines()
+            else:
+              self.updateStartPoint()
+        if theID == self.AUXILIARY_ID():
+            anAuxiliary = self.data().boolean(self.AUXILIARY_ID()).value()
+            aLinesList = self.reflist(self.LINES_LIST_ID())
+            aNbLines = aLinesList.size()
+            # Update coordinates of rectangle lines
+            for i in range (0, aNbLines):
+                aLine = ModelAPI.objectToFeature(aLinesList.object(i))
+                aLine.data().boolean("Auxiliary").setValue(anAuxiliary)
+
 
     def updateLines(self):
         # Retrieving list of already created lines
         aLinesList = self.reflist(self.LINES_LIST_ID())
         aNbLines = aLinesList.size()
-
         aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
         aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID()))
         aX = [aStartPoint.x(), aStartPoint.x(), aEndPoint.x(), aEndPoint.x()]
@@ -148,3 +168,18 @@ class SketchPlugin_Rectangle(model.Feature):
             aLineEnd = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint"))
             aLineStart.setValue(aX[i-1], aY[i-1])
             aLineEnd.setValue(aX[i], aY[i])
+
+    def updateStartPoint(self):
+        # Retrieving list of already created lines
+        aLinesList = self.reflist(self.LINES_LIST_ID())
+        aNbLines = aLinesList.size()
+
+        aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
+        aX = aStartPoint.x()
+        aY = aStartPoint.y()
+
+        # Update coordinates of rectangle lines
+        for i in range (0, aNbLines):
+            aLine = ModelAPI.objectToFeature(aLinesList.object(i))
+            aLineStart = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint"))
+            aLineStart.setValue(aX, aY)