]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAddons/macros/rectangle/feature.py
Salome HOME
Meet the coding style (line length <= 100)
[modules/shaper.git] / src / PythonAddons / macros / rectangle / feature.py
1 """
2 Macro-feature to produce rectangle in the sketcher
3 Author: Artem ZHIDKOV
4 Copyright (C) 2016-20xx CEA/DEN, EDF R&D
5 """
6
7 from salome.shaper import model
8 import ModelAPI
9 import GeomDataAPI
10
11 class SketchPlugin_Rectangle(model.Feature):
12     """
13     Implementation of rectangle creation.
14
15     It produced 2 horizontal lines and 2 vertical lines connected by coincidence constraints
16     """
17
18 # Initializations
19
20     def __init__(self):
21         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
22         model.Feature.__init__(self)
23
24     @staticmethod
25     def ID():
26         """Rectangle feature kind."""
27         return "SketchRectangle"
28
29     @staticmethod
30     def START_ID():
31         """Returns ID of first corner."""
32         return "RectStartPoint"
33
34     @staticmethod
35     def END_ID():
36         """Returns ID of second corner."""
37         return "RectEndPoint"
38
39     @staticmethod
40     def AUXILIARY_ID():
41         """Returns whether the rectangle is accessory."""
42         return "Auxiliary"
43
44     @staticmethod
45     def LINES_LIST_ID():
46         """Returns ID of list containing lines created."""
47         return "RectangleList"
48
49     def getKind(self):
50         """Override Feature.getKind()"""
51         return SketchPlugin_Rectangle.ID()
52
53
54 # Initialization of the rectangle
55
56     def initAttributes(self):
57         """Override Feature.initAttributes()"""
58         # Flag whether the rectangle is accessory
59         self.data().addAttribute(self.AUXILIARY_ID(), ModelAPI.ModelAPI_AttributeBoolean_typeId())
60         # Creating corners of the rectangle
61         self.data().addAttribute(self.START_ID(), GeomDataAPI.GeomDataAPI_Point2D_typeId())
62         self.data().addAttribute(self.END_ID(), GeomDataAPI.GeomDataAPI_Point2D_typeId())
63         # Creating list to store lines
64         self.data().addAttribute(self.LINES_LIST_ID(), ModelAPI.ModelAPI_AttributeRefList_typeId())
65         ModelAPI.ModelAPI_Session.get().validators().registerNotObligatory(self.getKind(), self.LINES_LIST_ID())
66
67     def isMacro(self):
68         """
69         Override Feature.isMacro().
70         Rectangle feature is macro: removes itself on the creation transaction finish.
71         """
72         return True
73
74 # Edition of the rectangle
75
76     def execute(self):
77         # Retrieving list of already created lines
78         aLinesList = self.reflist(self.LINES_LIST_ID())
79         aNbLines = aLinesList.size()
80         if aNbLines == 1:
81             # Create 1-4 lines to compose the rectangle
82             for i in range (0, 3):
83                 aLine = self.__sketch.addFeature("SketchLine")
84                 aLinesList.append(aLine)
85             self.updateLines()
86             aNbLines = aLinesList.size()
87             # Create constraints to keep the rectangle
88             for i in range (0, aNbLines):
89                 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
90                 # connect neighbor lines by coincidence
91                 iPrev = i - 1
92                 if iPrev < 0:
93                     iPrev = aNbLines - 1
94                 aPrevLine = ModelAPI.objectToFeature(aLinesList.object(iPrev))
95                 aCoincidence = self.__sketch.addFeature("SketchConstraintCoincidence")
96                 aRefAttrA = aCoincidence.refattr("ConstraintEntityA")
97                 aRefAttrB = aCoincidence.refattr("ConstraintEntityB")
98                 aRefAttrA.setAttr(aPrevLine.attribute("EndPoint"))
99                 aRefAttrB.setAttr(aLine.attribute("StartPoint"))
100             # Flags which show horizontal or vertical constraint is build for correponding line
101             self.__isHV = [False, False, False, False]
102             # Update coordinates of created lines
103             self.updateLines()
104         # Add horizontal and vertical constraint for the lines which already have result
105         for i in range (0, aNbLines):
106             if self.__isHV[i]:
107                 continue
108             aLine = ModelAPI.objectToFeature(aLinesList.object(i))
109             aLineResult = aLine.lastResult()
110             if aLineResult is None:
111                 continue
112             aHVName = "SketchConstraintHorizontal"
113             if i % 2 == 1:
114                 aHVName = "SketchConstraintVertical"
115             aHVConstraint = self.__sketch.addFeature(aHVName)
116             aRefAttrA = aHVConstraint.refattr("ConstraintEntityA")
117             aRefAttrA.setObject(aLine.lastResult())
118             self.__isHV[i] = True
119
120     def attributeChanged(self, theID):
121         if theID == self.START_ID() or theID == self.END_ID():
122             # Search the sketch containing this rectangle
123             self.__sketch = None
124             aRefs = self.data().refsToMe();
125             for iter in aRefs:
126                 aFeature = ModelAPI.objectToFeature(iter.owner())
127                 if aFeature.getKind() == "Sketch":
128                     self.__sketch = ModelAPI.featureToCompositeFeature(aFeature)
129                     break
130
131             aLinesList = self.reflist(self.LINES_LIST_ID())
132             aNbLines = aLinesList.size()
133             if aNbLines == 0:
134                 # Create first line to be able to create a coincidence with selected point/feature
135                 for i in range (0, 1):
136                     aLine = self.__sketch.addFeature("SketchLine")
137                     aLinesList.append(aLine)
138
139             aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
140             aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID()))
141             if aStartPoint.isInitialized() and aEndPoint.isInitialized():
142               self.updateLines()
143             else:
144               self.updateStartPoint()
145         if theID == self.AUXILIARY_ID():
146             anAuxiliary = self.data().boolean(self.AUXILIARY_ID()).value()
147             aLinesList = self.reflist(self.LINES_LIST_ID())
148             aNbLines = aLinesList.size()
149             # Update coordinates of rectangle lines
150             for i in range (0, aNbLines):
151                 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
152                 aLine.data().boolean("Auxiliary").setValue(anAuxiliary)
153
154
155     def updateLines(self):
156         # Retrieving list of already created lines
157         aLinesList = self.reflist(self.LINES_LIST_ID())
158         aNbLines = aLinesList.size()
159         aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
160         aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID()))
161         aX = [aStartPoint.x(), aStartPoint.x(), aEndPoint.x(), aEndPoint.x()]
162         aY = [aStartPoint.y(), aEndPoint.y(), aEndPoint.y(), aStartPoint.y()]
163
164         # Update coordinates of rectangle lines
165         for i in range (0, aNbLines):
166             aLine = ModelAPI.objectToFeature(aLinesList.object(i))
167             aLineStart = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("StartPoint"))
168             aLineEnd = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint"))
169             aLineStart.setValue(aX[i-1], aY[i-1])
170             aLineEnd.setValue(aX[i], aY[i])
171
172     def updateStartPoint(self):
173         # Retrieving list of already created lines
174         aLinesList = self.reflist(self.LINES_LIST_ID())
175         aNbLines = aLinesList.size()
176
177         aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
178         aX = aStartPoint.x()
179         aY = aStartPoint.y()
180
181         # Update coordinates of rectangle lines
182         for i in range (0, aNbLines):
183             aLine = ModelAPI.objectToFeature(aLinesList.object(i))
184             aLineStart = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint"))
185             aLineStart.setValue(aX, aY)