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>
22 Macro-feature to produce rectangle in the sketcher
24 Copyright (C) 2016-20xx CEA/DEN, EDF R&D
27 from salome.shaper import model
31 class SketchPlugin_Rectangle(model.Feature):
33 Implementation of rectangle creation.
35 It produced 2 horizontal lines and 2 vertical lines connected by coincidence constraints
41 """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
42 model.Feature.__init__(self)
46 """Rectangle feature kind."""
47 return "SketchRectangle"
51 """Returns ID of first corner."""
52 return "RectStartPoint"
56 """Returns ID of second corner."""
61 """Returns whether the rectangle is accessory."""
66 """Returns ID of list containing lines created."""
67 return "RectangleList"
70 """Override Feature.getKind()"""
71 return SketchPlugin_Rectangle.ID()
74 # Initialization of the rectangle
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())
89 Override Feature.isMacro().
90 Rectangle feature is macro: removes itself on the creation transaction finish.
94 # Edition of the rectangle
97 # Retrieving list of already created lines
98 aLinesList = self.reflist(self.LINES_LIST_ID())
99 aNbLines = aLinesList.size()
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)
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
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
124 # Add horizontal and vertical constraint for the lines which already have result
125 for i in range (0, aNbLines):
128 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
129 aLineResult = aLine.lastResult()
130 if aLineResult is None:
132 aHVName = "SketchConstraintHorizontal"
134 aHVName = "SketchConstraintVertical"
135 aHVConstraint = self.__sketch.addFeature(aHVName)
136 aRefAttrA = aHVConstraint.refattr("ConstraintEntityA")
137 aRefAttrA.setObject(aLine.lastResult())
138 self.__isHV[i] = True
140 def attributeChanged(self, theID):
141 if theID == self.START_ID() or theID == self.END_ID():
142 # Search the sketch containing this rectangle
144 aRefs = self.data().refsToMe();
146 aFeature = ModelAPI.objectToFeature(iter.owner())
147 if aFeature.getKind() == "Sketch":
148 self.__sketch = ModelAPI.featureToCompositeFeature(aFeature)
151 aLinesList = self.reflist(self.LINES_LIST_ID())
152 aNbLines = aLinesList.size()
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)
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():
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)
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 anAuxiliary = self.data().boolean(self.AUXILIARY_ID()).value()
185 # Update coordinates of rectangle lines
186 for i in range (0, aNbLines):
187 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
188 aLineStart = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("StartPoint"))
189 aLineEnd = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint"))
190 aLineStart.setValue(aX[i-1], aY[i-1])
191 aLineEnd.setValue(aX[i], aY[i])
192 aLine.data().boolean("Auxiliary").setValue(anAuxiliary)
194 def updateStartPoint(self):
195 # Retrieving list of already created lines
196 aLinesList = self.reflist(self.LINES_LIST_ID())
197 aNbLines = aLinesList.size()
199 aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
203 # Update coordinates of rectangle lines
204 for i in range (0, aNbLines):
205 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
206 aLineStart = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint"))
207 aLineStart.setValue(aX, aY)