]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestOffset.py
Salome HOME
Task #3231: Sketcher Offset of a curve. Unit test.
[modules/shaper.git] / src / SketchPlugin / Test / TestOffset.py
1 # Copyright (C) 2014-2019  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 (not anInInd == -1):
69             aFeatureIn = ModelAPI_Feature.feature(theListIn.object(anInInd))
70             assert(aFeatureIn is not None)
71
72             #print(aFeatureIn.getKind())
73             if (aFeatureIn.getKind() == "SketchLine"):
74                 assert(aFeatureOut.getKind() == aFeatureIn.getKind())
75                 # Line and its offset are parallel
76                 aP1Out = geomDataAPI_Point2D(aFeatureOut.attribute('StartPoint'))
77                 aP2Out = geomDataAPI_Point2D(aFeatureOut.attribute('EndPoint'))
78                 aP1In  = geomDataAPI_Point2D(aFeatureIn.attribute('StartPoint'))
79                 aP2In  = geomDataAPI_Point2D(aFeatureIn.attribute('EndPoint'))
80                 aDirOut = [aP2Out.x() - aP1Out.x(), aP2Out.y() - aP1Out.y()]
81                 aDirIn  = [ aP2In.x() -  aP1In.x(),  aP2In.y() -  aP1In.y()]
82                 aCross = aDirOut[0] * aDirIn[1] - aDirOut[1] * aDirIn[0]
83                 assert math.fabs(aCross) < TOL, "aCross = {0}".format(aCross)
84             elif (aFeatureIn.getKind() == "SketchArc"):
85                 assert(aFeatureOut.getKind() == aFeatureIn.getKind())
86                 # Arc and its offset have the same center
87                 aCPOut = geomDataAPI_Point2D(aFeatureOut.attribute('center_point'))
88                 aCPIn  = geomDataAPI_Point2D(aFeatureIn.attribute('center_point'))
89                 assert (math.fabs(aCPOut.x() - aCPIn.x()) < TOL)
90                 assert (math.fabs(aCPOut.y() - aCPIn.y()) < TOL)
91
92
93 #=========================================================================
94 # Start of test
95 #=========================================================================
96 aSession = ModelAPI_Session.get()
97 aDocument = aSession.moduleDocument()
98 #=========================================================================
99 # Creation of a sketch
100 #=========================================================================
101 aSession.startOperation()
102 aSketchCommonFeature = aDocument.addFeature("Sketch")
103 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
104 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
105 origin.setValue(0, 0, 0)
106 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
107 dirx.setValue(1, 0, 0)
108 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
109 norm.setValue(0, 0, 1)
110 aSession.finishOperation()
111 #=========================================================================
112 # Creation of an arc and two lines
113 #=========================================================================
114 # Arc
115 aSession.startOperation()
116 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
117 anArcCentr = geomDataAPI_Point2D(aSketchArc1.attribute("center_point"))
118 anArcCentr.setValue(10., 10.)
119 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("start_point"))
120 anArcStartPoint.setValue(50., 0.)
121 anArcEndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("end_point"))
122 anArcEndPoint.setValue(0., 50.)
123 aSession.finishOperation()
124 # Line 1
125 aSession.startOperation()
126 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
127 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
128 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
129 aLine1StartPoint.setValue(0., 50.)
130 aLine1EndPoint.setValue(-20., 0.)
131 aSession.finishOperation()
132 # Line 2
133 aSession.startOperation()
134 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
135 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
136 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
137 aLine2StartPoint.setValue(50., 0.)
138 aLine2EndPoint.setValue(-20., 0.)
139 aSession.finishOperation()
140 assert (model.dof(aSketchFeature) == 13)
141 #=========================================================================
142 # Link arc points and lines points by the coincidence constraint
143 #=========================================================================
144 aSession.startOperation()
145 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
146 reflistA = aConstraint.refattr("ConstraintEntityA")
147 reflistB = aConstraint.refattr("ConstraintEntityB")
148 reflistA.setAttr(anArcEndPoint)
149 reflistB.setAttr(aLine1StartPoint)
150 aConstraint.execute()
151 aSession.finishOperation()
152 aSession.startOperation()
153 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
154 reflistA = aConstraint.refattr("ConstraintEntityA")
155 reflistB = aConstraint.refattr("ConstraintEntityB")
156 reflistA.setAttr(anArcStartPoint)
157 reflistB.setAttr(aLine2StartPoint)
158 aConstraint.execute()
159 aSession.finishOperation()
160 assert (model.dof(aSketchFeature) == 9)
161 #=========================================================================
162 # Make offset for objects created above
163 #=========================================================================
164 VALUE = 13
165 IS_REVERSED = False
166 aSession.startOperation()
167 anOffset = aSketchFeature.addFeature("SketchOffset")
168 aRefListInitial = anOffset.reflist("segments")
169 aRefListInitial.append(aSketchLine1.lastResult())
170 aRefListInitial.append(aSketchArc1.lastResult())
171 aRefListInitial.append(aSketchLine2.lastResult())
172 anOffset.real("offset_value").setValue(VALUE)
173 anOffset.boolean("reversed").setValue(IS_REVERSED)
174 anOffset.execute()
175 aSession.finishOperation()
176 assert (model.dof(aSketchFeature) == 9)
177 #=========================================================================
178 # Verify all offset objects
179 #=========================================================================
180 aRefListA = anOffset.reflist("ConstraintEntityA")
181 aRefListB = anOffset.reflist("ConstraintEntityB")
182 anOffsetToBaseMap = anOffset.intArray("ConstraintEntityC")
183 checkOffset(aRefListA, aRefListB, anOffsetToBaseMap, VALUE, IS_REVERSED, 3, 6)
184 assert (model.dof(aSketchFeature) == 9)
185
186 #=========================================================================
187 # Remove object from offset
188 #=========================================================================
189 aSession.startOperation()
190 aRefListInitial.remove(aSketchLine2.lastResult())
191 aSession.finishOperation()
192 checkOffset(aRefListA, aRefListB, anOffsetToBaseMap, VALUE, IS_REVERSED, 2, 4)
193 assert (model.dof(aSketchFeature) == 9)
194
195 #=========================================================================
196 # Clear list of objects
197 #=========================================================================
198 aSession.startOperation()
199 aRefListInitial.clear()
200 #TODO: uncomment next line
201 #checkOffset(aRefListA, aRefListB, anOffsetToBaseMap, VALUE, IS_REVERSED, 0, 0)
202 # add arc once again
203 aRefListInitial.append(aSketchArc1.lastResult())
204 aSession.finishOperation()
205 checkOffset(aRefListA, aRefListB, anOffsetToBaseMap, VALUE, IS_REVERSED, 1, 1)
206 assert (model.dof(aSketchFeature) == 9)
207
208 #=========================================================================
209 # End of test
210 #=========================================================================
211
212 assert(model.checkPythonDump())