2 Macro-feature to produce rectangle in the sketcher
4 Copyright (C) 2016-20xx CEA/DEN, EDF R&D
7 from salome.shaper import model
11 class SketchPlugin_Rectangle(model.Feature):
13 Implementation of rectangle creation.
15 It produced 2 horizontal lines and 2 vertical lines connected by coincidence constraints
21 """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
22 model.Feature.__init__(self)
26 """Rectangle feature kind."""
27 return "SketchRectangle"
31 """Returns ID of first corner."""
32 return "RectStartPoint"
36 """Returns ID of second corner."""
41 """Returns whether the rectangle is accessory."""
46 """Returns ID of list containing lines created."""
47 return "RectangleList"
50 """Override Feature.getKind()"""
51 return SketchPlugin_Rectangle.ID()
54 # Initialization of the rectangle
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())
69 Override Feature.isMacro().
70 Rectangle feature is macro: removes itself on the creation transaction finish.
74 # Edition of the rectangle
77 # Retrieving list of already created lines
78 aLinesList = self.reflist(self.LINES_LIST_ID())
79 aNbLines = aLinesList.size()
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)
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
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
104 # Add horizontal and vertical constraint for the lines which already have result
105 for i in range (0, aNbLines):
108 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
109 aLineResult = aLine.lastResult()
110 if aLineResult is None:
112 aHVName = "SketchConstraintHorizontal"
114 aHVName = "SketchConstraintVertical"
115 aHVConstraint = self.__sketch.addFeature(aHVName)
116 aRefAttrA = aHVConstraint.refattr("ConstraintEntityA")
117 aRefAttrA.setObject(aLine.lastResult())
118 self.__isHV[i] = True
120 def attributeChanged(self, theID):
121 if theID == self.START_ID() or theID == self.END_ID():
122 # Search the sketch containing this rectangle
124 aRefs = self.data().refsToMe();
126 aFeature = ModelAPI.objectToFeature(iter.owner())
127 if aFeature.getKind() == "Sketch":
128 self.__sketch = ModelAPI.featureToCompositeFeature(aFeature)
131 aLinesList = self.reflist(self.LINES_LIST_ID())
132 aNbLines = aLinesList.size()
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)
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():
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)
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()]
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])
172 def updateStartPoint(self):
173 # Retrieving list of already created lines
174 aLinesList = self.reflist(self.LINES_LIST_ID())
175 aNbLines = aLinesList.size()
177 aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
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)