From: Renaud NEDELEC Date: Thu, 5 Nov 2015 12:58:39 +0000 (+0100) Subject: [PythonAPI / sketcher] first try for mirror constraint interface (not working) and... X-Git-Tag: V_2.1.0~206^2~11 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=ed856908eae66db89da5c2e10274956afe0a9a31;p=modules%2Fshaper.git [PythonAPI / sketcher] first try for mirror constraint interface (not working) and small doc modification --- diff --git a/src/PythonAPI/Test/TestSketcherAddMirror.py b/src/PythonAPI/Test/TestSketcherAddMirror.py new file mode 100644 index 000000000..dc23f0994 --- /dev/null +++ b/src/PythonAPI/Test/TestSketcherAddMirror.py @@ -0,0 +1,29 @@ +import unittest +import model +from TestSketcher import SketcherTestCase +from GeomDataAPI import geomDataAPI_Point2D + +import logging + +class SketcherAddLine(SketcherTestCase): + def setUp(self): + SketcherTestCase.setUp(self) + self.line = self.sketch.addLine(0, 0, 0, 1) + self.circle_1 = self.sketch.addCircle(30, 0, 10) + + def test_add_mirror(self): + mirror = self.sketch.addMirror( + self.line.result(), self.circle_1.result() + ) + model.do() + mirrored_objects = mirror.mirroredObjects() + mirrored_circle = mirrored_objects.object(0) + center = geomDataAPI_Point2D(mirrored_circle.data().attribute("CircleCenter")) + self.assertTrue(False, msg="%s" % center.x()) + #dir(mirrored_circle) + #center = geomDataAPI_Point2D(mirrored_circle.data().attribute("CircleCenter")) + #self.assertEqual(center.x(), -30, msg="%s"%(dir(mirrored_circle))) + #self.assertTrue(False, msg="%s" % (dir(mirrored_circle))) + +if __name__ == "__main__": + unittest.main(verbosity=2) \ No newline at end of file diff --git a/src/PythonAPI/model/sketcher/circle.py b/src/PythonAPI/model/sketcher/circle.py index b6ce45e8a..c815ab110 100644 --- a/src/PythonAPI/model/sketcher/circle.py +++ b/src/PythonAPI/model/sketcher/circle.py @@ -5,7 +5,7 @@ from model.roots import Interface class Circle(Interface): """Interface for circle feature data manipulation.""" - def __init__(self, feature, x, y, radius): + def __init__(self, feature, *args): Interface.__init__(self, feature) assert(self._feature.getKind() == "SketchCircle") @@ -13,8 +13,18 @@ class Circle(Interface): self._feature.data().attribute("CircleCenter") ) self._radius = self._feature.data().real("CircleRadius") - self.setCenter(x, y) - self.setRadius(radius) + + if not args: + return + + if len(args) != 3: + raise TypeError( + "Invalid number of arguments, 3 arguments needed (%s given)" + % len(args) + ) + + self.setCenter(args[0], args[1]) + self.setRadius(args[2]) self.execute() def setCenter(self, x, y): diff --git a/src/PythonAPI/model/sketcher/mirror.py b/src/PythonAPI/model/sketcher/mirror.py new file mode 100644 index 000000000..dba62b4ad --- /dev/null +++ b/src/PythonAPI/model/sketcher/mirror.py @@ -0,0 +1,32 @@ +"""Sketch point feature interface.""" + +from GeomDataAPI import geomDataAPI_Point2D +from ModelAPI import ModelAPI_Feature +from model.roots import Interface +from model.errors import FeatureInputInvalid + +class Mirror(Interface): + """Interface on mirror constraint for data manipulation.""" + def __init__(self, feature, mirror_line, *mirror_objects): + Interface.__init__(self, feature) + assert(self._feature.getKind() == "SketchConstraintMirror") + + self._feature.data().refattr("ConstraintEntityA").setObject(mirror_line) + self._feature.data().reflist("ConstraintEntityB").clear() + for object_ in mirror_objects: + self._feature.data().reflist("ConstraintEntityB").append(object_) + self._feature.data().reflist("ConstraintMirrorList").append(object_) + self.execute() + + def mirroredObjects(self): + return self._feature.data().reflist("ConstraintEntityC") + #feature = ModelAPI_Feature(object_) + #if feature.getKind() == "SketchCircle": + #objects.append(Circle(feature)) + #elif feature.getKind() == "SketchLine": + #objects.append(Line(feature)) + #else: + #raise TypeError( + #"%s is not a valid feature type" % feature.getKind() + #) + diff --git a/src/PythonAPI/model/sketcher/sketch.py b/src/PythonAPI/model/sketcher/sketch.py index 9e92a8e26..1efd09d1d 100644 --- a/src/PythonAPI/model/sketcher/sketch.py +++ b/src/PythonAPI/model/sketcher/sketch.py @@ -1,7 +1,13 @@ +# Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov +# finalized by Renaud Nedelec and Sergey Pokhodenko +# Copyright (C) 2014-20xx CEA/DEN, EDF R&D + """Sketch Feature Interface -Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov - finalized by Renaud Nedelec and Sergey Pokhodenko -Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +This interface allows to add a Sketch +in a Part. +The created sketch object provides all the needed methods +for sketch modification and constraint edition. """ from ModelAPI import modelAPI_ResultConstruction, featureToCompositeFeature @@ -12,6 +18,7 @@ from model.sketcher.point import Point from model.sketcher.line import Line from model.sketcher.circle import Circle from model.sketcher.arc import Arc +from model.sketcher.mirror import Mirror from model.roots import Interface from model.tools import Selection @@ -22,6 +29,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""" feature = featureToCompositeFeature(doc.addFeature("Sketch")) @@ -108,7 +116,7 @@ class Sketch(Interface): return Circle(circle_feature, *args) def addArc(self, *args): - """Add an arc to this Sketch.""" + """Add an arc to the sketch.""" if not args: raise TypeError("No arguments given") arc_feature = self._feature.addFeature("SketchArc") @@ -236,6 +244,8 @@ class Sketch(Interface): def setTangent(self, object_1, object_2): """Set a tangential continuity between two objects at their coincidence point.""" + if object_1 is None or object_2 is None: + raise TypeError("NoneType argument given") constraint = self._feature.addFeature("SketchConstraintTangent") constraint.data().refattr("ConstraintEntityA").setObject(object_1) constraint.data().refattr("ConstraintEntityB").setObject(object_2) @@ -258,6 +268,25 @@ class Sketch(Interface): constraint.data().refattr("ConstraintEntityA").setObject(object_) self.execute() return constraint + + #------------------------------------------------------------- + # + # Transformation constraints + # + #------------------------------------------------------------- + + def addMirror(self, mirror_line, sketch_objects): + """Add a mirror transformation of the given objects to the sketch. + + This transformation is a constraint. + :return: interface to the constraint + :rtype: Mirror object + """ + mirror_constraint = self._feature.addFeature("SketchConstraintMirror") + mirror_interface = Mirror(mirror_constraint, mirror_line, sketch_objects) + self.execute() + return mirror_interface + #------------------------------------------------------------- #