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