Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / Test / TestOffset1.py
1 # Copyright (C) 2020-2022  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 email : webmaster.salome@opencascade.com
18 #
19
20 """
21     TestOffset.py
22     Unit test of SketchPlugin_Offset class
23
24     SketchPlugin_Offset
25         static const std::string ID("SketchOffset");
26         data()->addAttribute(EDGES_ID(), ModelAPI_AttributeRefList::typeId());
27         data()->addAttribute(VALUE_ID(), ModelAPI_AttributeDouble::typeId());
28         data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
29         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
30         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
31         data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeIntArray::typeId());
32 """
33
34 from GeomDataAPI import *
35 from ModelAPI import *
36 import math
37 from salome.shaper import model
38
39 #=========================================================================
40 # Initialization of the test
41 #=========================================================================
42
43 __updated__ = "2020-06-30"
44
45 #=========================================================================
46 # Auxiliary functions
47 #=========================================================================
48 def normalize(theDir):
49     aLen = math.hypot(theDir[0], theDir[1])
50     if aLen < 1.e-10:
51         aLen = 1.0
52     return [theDir[0] / aLen, theDir[1] / aLen]
53
54 def checkOffset(theListIn, theListOut, theOutToIn, theDist, isReversed, nbIn, nbOut):
55     TOL = 6.e-5
56     aNbIn  = theListIn.size()
57     aNbOut = theListOut.size()
58
59     #print("**checkOffset**")
60     assert (theListIn.size() == nbIn)
61     assert (theListOut.size() == nbOut)
62     assert (theOutToIn.size() == nbOut)
63
64     for ind in range(0, aNbOut):
65         aFeatureOut = ModelAPI_Feature.feature(theListOut.object(ind))
66         assert(aFeatureOut is not None)
67         anInInd = theOutToIn.value(ind)
68         if (anInInd == -1):
69             assert(aFeatureOut.getKind() == "SketchArc")
70         else:
71             aFeatureIn = ModelAPI_Feature.feature(theListIn.object(anInInd))
72             assert(aFeatureIn is not None)
73
74             #print(aFeatureIn.getKind())
75             if (aFeatureIn.getKind() == "SketchLine"):
76                 assert(aFeatureOut.getKind() == aFeatureIn.getKind())
77                 # Line and its offset are parallel
78                 aP1Out = geomDataAPI_Point2D(aFeatureOut.attribute('StartPoint'))
79                 aP2Out = geomDataAPI_Point2D(aFeatureOut.attribute('EndPoint'))
80                 aP1In  = geomDataAPI_Point2D(aFeatureIn.attribute('StartPoint'))
81                 aP2In  = geomDataAPI_Point2D(aFeatureIn.attribute('EndPoint'))
82                 aDirOut = [aP2Out.x() - aP1Out.x(), aP2Out.y() - aP1Out.y()]
83                 aDirIn  = [ aP2In.x() -  aP1In.x(),  aP2In.y() -  aP1In.y()]
84                 aCross = aDirOut[0] * aDirIn[1] - aDirOut[1] * aDirIn[0]
85                 assert math.fabs(aCross) < TOL, "aCross = {0}".format(aCross)
86             elif (aFeatureIn.getKind() == "SketchArc"):
87                 assert(aFeatureOut.getKind() == aFeatureIn.getKind())
88                 # Arc and its offset have the same center
89                 aCPOut = geomDataAPI_Point2D(aFeatureOut.attribute('center_point'))
90                 aCPIn  = geomDataAPI_Point2D(aFeatureIn.attribute('center_point'))
91                 assert (math.fabs(aCPOut.x() - aCPIn.x()) < TOL)
92                 assert (math.fabs(aCPOut.y() - aCPIn.y()) < TOL)
93
94
95 #=========================================================================
96 # Start of test
97 #=========================================================================
98 aSession = ModelAPI_Session.get()
99 aDocument = aSession.moduleDocument()
100 #=========================================================================
101 # Creation of a sketch
102 #=========================================================================
103 aSession.startOperation()
104 aSketchCommonFeature = aDocument.addFeature("Sketch")
105 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
106 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
107 origin.setValue(0, 0, 0)
108 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
109 dirx.setValue(1, 0, 0)
110 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
111 norm.setValue(0, 0, 1)
112 aSession.finishOperation()
113 #=========================================================================
114 # Creation of an arc and two lines
115 #=========================================================================
116 # Arc
117 aSession.startOperation()
118 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
119 anArcCentr = geomDataAPI_Point2D(aSketchArc1.attribute("center_point"))
120 anArcCentr.setValue(10., 10.)
121 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("start_point"))
122 anArcStartPoint.setValue(50., 0.)
123 anArcEndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("end_point"))
124 anArcEndPoint.setValue(0., 50.)
125 aSession.finishOperation()
126 # Line 1
127 aSession.startOperation()
128 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
129 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
130 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
131 aLine1StartPoint.setValue(0., 50.)
132 aLine1EndPoint.setValue(-20., 0.)
133 aSession.finishOperation()
134 # Line 2
135 aSession.startOperation()
136 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
137 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
138 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
139 aLine2StartPoint.setValue(50., 0.)
140 aLine2EndPoint.setValue(-20., 0.)
141 aSession.finishOperation()
142 assert (model.dof(aSketchFeature) == 13)
143 #=========================================================================
144 # Link arc points and lines points by the coincidence constraints
145 #=========================================================================
146 aSession.startOperation()
147 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
148 reflistA = aConstraint.refattr("ConstraintEntityA")
149 reflistB = aConstraint.refattr("ConstraintEntityB")
150 reflistA.setAttr(anArcEndPoint)
151 reflistB.setAttr(aLine1StartPoint)
152 aConstraint.execute()
153 aSession.finishOperation()
154 aSession.startOperation()
155 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
156 reflistA = aConstraint.refattr("ConstraintEntityA")
157 reflistB = aConstraint.refattr("ConstraintEntityB")
158 reflistA.setAttr(anArcStartPoint)
159 reflistB.setAttr(aLine2StartPoint)
160 aConstraint.execute()
161 aSession.finishOperation()
162 aSession.startOperation()
163 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
164 reflistA = aConstraint.refattr("ConstraintEntityA")
165 reflistB = aConstraint.refattr("ConstraintEntityB")
166 reflistA.setAttr(aLine1EndPoint)
167 reflistB.setAttr(aLine2EndPoint)
168 aConstraint.execute()
169 aSession.finishOperation()
170 assert (model.dof(aSketchFeature) == 7)
171 #=========================================================================
172 # Make offset for objects created above
173 #=========================================================================
174 VALUE = 13
175 IS_REVERSED = False
176 aSession.startOperation()
177 anOffset = aSketchFeature.addFeature("SketchOffset")
178 aRefListInitial = anOffset.reflist("segments")
179 aRefListInitial.append(aSketchLine1.lastResult())
180 aRefListInitial.append(aSketchArc1.lastResult())
181 aRefListInitial.append(aSketchLine2.lastResult())
182 anOffset.real("offset_value").setValue(VALUE)
183 anOffset.boolean("reversed").setValue(IS_REVERSED)
184 anOffset.execute()
185 aSession.finishOperation()
186 assert (model.dof(aSketchFeature) == 7)
187 #=========================================================================
188 # Verify all offset objects
189 #=========================================================================
190 aRefListA = anOffset.reflist("ConstraintEntityA")
191 aRefListB = anOffset.reflist("ConstraintEntityB")
192 anOffsetToBaseMap = anOffset.intArray("ConstraintEntityC")
193 checkOffset(aRefListA, aRefListB, anOffsetToBaseMap, VALUE, IS_REVERSED, 3, 6)
194 assert (model.dof(aSketchFeature) == 7)
195
196 #=========================================================================
197 # Remove object from offset
198 #=========================================================================
199 aSession.startOperation()
200 aRefListInitial.remove(aSketchLine2.lastResult())
201 aSession.finishOperation()
202 checkOffset(aRefListA, aRefListB, anOffsetToBaseMap, VALUE, IS_REVERSED, 2, 4)
203 assert (model.dof(aSketchFeature) == 7)
204
205 #=========================================================================
206 # Clear list of objects
207 #=========================================================================
208 aSession.startOperation()
209 aRefListInitial.clear()
210 #TODO: uncomment next line
211 #checkOffset(aRefListA, aRefListB, anOffsetToBaseMap, VALUE, IS_REVERSED, 0, 0)
212 # add arc once again
213 aRefListInitial.append(aSketchArc1.lastResult())
214 aSession.finishOperation()
215 checkOffset(aRefListA, aRefListB, anOffsetToBaseMap, VALUE, IS_REVERSED, 1, 1)
216 assert (model.dof(aSketchFeature) == 7)
217
218 #=========================================================================
219 # End of test
220 #=========================================================================
221
222 assert(model.checkPythonDump())