2 Macro-feature to produce rectangle in the sketcher
4 Copyright (C) 2016-20xx CEA/DEN, EDF R&D
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)
85 aNbLines = aLinesList.size()
86 # Create constraints to keep the rectangle
87 for i in range (0, aNbLines):
88 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
89 # connect neighbor lines by coincidence
93 aPrevLine = ModelAPI.objectToFeature(aLinesList.object(iPrev))
94 aCoincidence = self.__sketch.addFeature("SketchConstraintCoincidence")
95 aRefAttrA = aCoincidence.refattr("ConstraintEntityA")
96 aRefAttrB = aCoincidence.refattr("ConstraintEntityB")
97 aRefAttrA.setAttr(aPrevLine.attribute("EndPoint"))
98 aRefAttrB.setAttr(aLine.attribute("StartPoint"))
99 # Flags which show horizontal or vertical constraint is build for correponding line
100 self.__isHV = [False, False, False, False]
101 # Update coordinates of created lines
103 # Add horizontal and vertical constraint for the lines which already have result
104 for i in range (0, aNbLines):
107 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
108 aLineResult = aLine.lastResult()
109 if aLineResult is None:
111 aHVName = "SketchConstraintHorizontal"
113 aHVName = "SketchConstraintVertical"
114 aHVConstraint = self.__sketch.addFeature(aHVName)
115 aRefAttrA = aHVConstraint.refattr("ConstraintEntityA")
116 aRefAttrA.setObject(aLine.lastResult())
117 self.__isHV[i] = True
119 def attributeChanged(self, theID):
120 if theID == self.START_ID() or theID == self.END_ID():
121 # Search the sketch containing this rectangle
123 aRefs = self.data().refsToMe();
125 aFeature = ModelAPI.objectToFeature(iter.owner())
126 if aFeature.getKind() == "Sketch":
127 self.__sketch = ModelAPI.featureToCompositeFeature(aFeature)
130 aLinesList = self.reflist(self.LINES_LIST_ID())
131 aNbLines = aLinesList.size()
133 # Create first line to be able to create a coincidence with selected point/feature
134 for i in range (0, 1):
135 aLine = self.__sketch.addFeature("SketchLine")
136 aLinesList.append(aLine)
138 aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
139 aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID()))
140 if aStartPoint.isInitialized() and aEndPoint.isInitialized():
143 self.updateStartPoint()
145 def updateLines(self):
146 # Retrieving list of already created lines
147 aLinesList = self.reflist(self.LINES_LIST_ID())
148 aNbLines = aLinesList.size()
149 aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
150 aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID()))
151 aX = [aStartPoint.x(), aStartPoint.x(), aEndPoint.x(), aEndPoint.x()]
152 aY = [aStartPoint.y(), aEndPoint.y(), aEndPoint.y(), aStartPoint.y()]
154 # Update coordinates of rectangle lines
155 for i in range (0, aNbLines):
156 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
157 aLineStart = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("StartPoint"))
158 aLineEnd = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint"))
159 aLineStart.setValue(aX[i-1], aY[i-1])
160 aLineEnd.setValue(aX[i], aY[i])
162 def updateStartPoint(self):
163 # Retrieving list of already created lines
164 aLinesList = self.reflist(self.LINES_LIST_ID())
165 aNbLines = aLinesList.size()
167 aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID()))
171 # Update coordinates of rectangle lines
172 for i in range (0, aNbLines):
173 aLine = ModelAPI.objectToFeature(aLinesList.object(i))
174 aLineStart = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint"))
175 aLineStart.setValue(aX, aY)