From: Renaud NEDELEC Date: Tue, 20 Oct 2015 14:08:08 +0000 (+0200) Subject: [PythonAPI / sketcher] modified addLine and added setAngle method X-Git-Tag: V_2.1.0~206^2~83 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=dfe2d108d162e47e889c2488b2fe3676eff3d973;p=modules%2Fshaper.git [PythonAPI / sketcher] modified addLine and added setAngle method --- diff --git a/src/PythonAPI/CMakeLists.txt b/src/PythonAPI/CMakeLists.txt index f1ab36274..551a8506e 100644 --- a/src/PythonAPI/CMakeLists.txt +++ b/src/PythonAPI/CMakeLists.txt @@ -20,4 +20,5 @@ ADD_UNIT_TESTS( TestSketcherSetHorizontal.py TestSketcherSetVertical.py TestSketcherSetRadius.py + TestSketcherSetAngle.py ) diff --git a/src/PythonAPI/Test/TestSketcherSetAngle.py b/src/PythonAPI/Test/TestSketcherSetAngle.py new file mode 100644 index 000000000..cbdd5fc3a --- /dev/null +++ b/src/PythonAPI/Test/TestSketcherSetAngle.py @@ -0,0 +1,34 @@ +import unittest +import model +import math +import TestSketcher +from TestSketcher import SketcherTestCase + +class SketcherSetAngle(SketcherTestCase): + def runTest(self): + # Set the constraint + l1 = self.sketch.addLine(0, 0, 0, 1) + l2 = self.sketch.addLine(0, 0, 1, 1) + self.sketch.setAngle(l1.result(), l2.result(), 30.0) + # Commit the transaction + model.do() + # Check the result + dot_product = (l1.endPointData().x() - l1.startPointData().x()) * \ + (l2.endPointData().x() - l2.startPointData().x()) + \ + (l1.endPointData().y() - l1.startPointData().y()) * \ + (l2.endPointData().y() - l2.startPointData().y()) + norm_1 = math.sqrt( + math.pow((l1.endPointData().x() - l1.startPointData().x()), 2) + + math.pow((l1.endPointData().y() - l1.startPointData().y()), 2) + ) + norm_2 = math.sqrt( + math.pow((l2.endPointData().x() - l2.startPointData().x()), 2) + + math.pow((l2.endPointData().y() - l2.startPointData().y()), 2) + ) + angle = math.acos(dot_product / (norm_1 * norm_2)) + self.assertAlmostEqual( + angle * 180 / math.pi, 30.0, delta=TestSketcher.DELTA + ) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/src/PythonAPI/model/sketcher/line.py b/src/PythonAPI/model/sketcher/line.py index dfe8a1c93..22c322f2c 100644 --- a/src/PythonAPI/model/sketcher/line.py +++ b/src/PythonAPI/model/sketcher/line.py @@ -15,7 +15,7 @@ class Line(): elif len(args) == 2: self.__createByPoints(*args) elif len(args) == 1: - self.__createByName(sketch, *args) + self.__createByName(*args) else: raise Exception("cannot create the Line") @@ -29,11 +29,9 @@ class Line(): self._end_point.setValue(p2.x(), p2.y()) self._feature.execute() - def __createByName(self, sketch, name): + def __createByName(self, name): self._feature.data().selection("External").selectSubShape("EDGE", name) self._feature.execute() - rigid = sketch.addFeature("SketchConstraintRigid") - rigid.refattr("ConstraintEntityA").setObject(self._feature.firstResult()) def startPointData(self): return self._start_point diff --git a/src/PythonAPI/model/sketcher/sketch.py b/src/PythonAPI/model/sketcher/sketch.py index 4b4ea7233..4a809f0b4 100644 --- a/src/PythonAPI/model/sketcher/sketch.py +++ b/src/PythonAPI/model/sketcher/sketch.py @@ -3,7 +3,7 @@ Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov Copyright (C) 2014-20xx CEA/DEN, EDF R&D """ -from ModelAPI import modelAPI_ResultConstruction, featureToCompositeFeature +from ModelAPI import modelAPI_ResultConstruction, featureToCompositeFeature from GeomDataAPI import geomDataAPI_Point, geomDataAPI_Dir from GeomAlgoAPI import GeomAlgoAPI_SketchBuilder from model.sketcher.point import Point @@ -18,7 +18,7 @@ def addSketch(doc, plane): A Sketch object is instanciated with a feature as input parameter it provides an interface for manipulation of the feature data. :return: interface on the feature - :rtype: Sketch object""" + :rtype: Sketch""" feature = featureToCompositeFeature(doc.addFeature("Sketch")) return Sketch(feature, plane) @@ -41,21 +41,23 @@ class Sketch(): self.__sketchOnPlane(plane) def __sketchOnPlane(self, plane): - o = plane.location() - d = plane.direction() - dx = plane.xDirection() + """Create the sketch on a plane.""" + origin = plane.location() + normal = plane.direction() + x_direction = plane.xDirection() geomDataAPI_Point( self._feature.data().attribute("Origin") - ).setValue( o.x(), o.y(), o.z() ) + ).setValue(origin.x(), origin.y(), origin.z()) geomDataAPI_Dir( self._feature.data().attribute("DirX") - ).setValue( dx.x(), dx.y(), dx.z() ) + ).setValue(x_direction.x(), x_direction.y(), x_direction.z()) geomDataAPI_Dir( self._feature.data().attribute("Norm") - ).setValue( d.x(), d.y(), d.z() ) + ).setValue(normal.x(), normal.y(), normal.z() ) - def __sketchOnFace(self, plane): - self._feature.data().selection("External").selectSubShape("FACE", plane) + def __sketchOnFace(self, name): + """Initialize the sketch on a face given by its name.""" + self._feature.data().selection("External").selectSubShape("FACE", name) #------------------------------------------------------------- # @@ -71,7 +73,15 @@ class Sketch(): def addLine(self, *args): """Add a line to this Sketch.""" line_feature = self._feature.addFeature("SketchLine") - return Line(line_feature, *args) + line_interface = Line(line_feature, *args) + # if the line is created by name add a rigid constraint + # to the created line + if len(args) == 1 and isinstance(args[0], str): + constraint = sketch.addFeature("SketchConstraintRigid") + constraint.refattr("ConstraintEntityA").setObject( + line_feature.firstResult() + ) + return line_interface def addCircle(self, *args): """Add a circle to this Sketch.""" @@ -149,6 +159,16 @@ class Sketch(): constraint.data().real("ConstraintValue").setValue(length) self._feature.execute() return constraint + + def setAngle(self, line_1, line_2, angle): + """Set the angle between the given 2 lines and add the corresponding + constraint to the sketch.""" + constraint = self._feature.addFeature("SketchConstraintAngle") + constraint.data().refattr("ConstraintEntityA").setObject(line_1) + constraint.data().refattr("ConstraintEntityB").setObject(line_2) + constraint.data().real("ConstraintValue").setValue(angle) + self._feature.execute() + return constraint def setRadius(self, circle, radius): """Set the radius of the given circle and add the corresponding