From 251c9a2600dd5c134bc2d9a0607a198a20753d9d Mon Sep 17 00:00:00 2001 From: Renaud NEDELEC Date: Thu, 15 Oct 2015 10:44:18 +0200 Subject: [PATCH] [PythonAPI] reorganised sketcher API in a separate package --- src/PythonAPI/CMakeLists.txt | 2 +- src/PythonAPI/modeler/__init__.py | 12 +-- src/PythonAPI/modeler/sketcher/__init__.py | 0 src/PythonAPI/modeler/sketcher/circle.py | 15 ++++ src/PythonAPI/modeler/sketcher/line.py | 40 +++++++++ src/PythonAPI/modeler/sketcher/point.py | 16 ++++ .../{sketcher.py => sketcher/sketch.py} | 87 ++----------------- 7 files changed, 87 insertions(+), 85 deletions(-) create mode 100644 src/PythonAPI/modeler/sketcher/__init__.py create mode 100644 src/PythonAPI/modeler/sketcher/circle.py create mode 100644 src/PythonAPI/modeler/sketcher/line.py create mode 100644 src/PythonAPI/modeler/sketcher/point.py rename src/PythonAPI/modeler/{sketcher.py => sketcher/sketch.py} (71%) diff --git a/src/PythonAPI/CMakeLists.txt b/src/PythonAPI/CMakeLists.txt index ca494a0b1..bda9c204c 100644 --- a/src/PythonAPI/CMakeLists.txt +++ b/src/PythonAPI/CMakeLists.txt @@ -3,7 +3,7 @@ SET(CMAKE_AUTOMOC ON) -INSTALL(DIRECTORY extension geom modeler DESTINATION pythonAPI) +INSTALL(DIRECTORY extension geom modeler DESTINATION PythonAPI) # --------- Unit tests ----------- INCLUDE(UnitTest) diff --git a/src/PythonAPI/modeler/__init__.py b/src/PythonAPI/modeler/__init__.py index 8ffaa381e..f16e4030b 100644 --- a/src/PythonAPI/modeler/__init__.py +++ b/src/PythonAPI/modeler/__init__.py @@ -8,9 +8,9 @@ from roots import * # Built-in features -from part import Part as addPart -from sketcher import Sketch as addSketch -from extrusion import Extrusion as addExtrusion -from boolean import Addition as addAddition -from boolean import Subtraction as addSubtraction -from boolean import Intersection as addIntersection \ No newline at end of file +from part import Part as addPart +from sketcher.sketch import Sketch as addSketch +from extrusion import Extrusion as addExtrusion +from boolean import Addition as addAddition +from boolean import Subtraction as addSubtraction +from boolean import Intersection as addIntersection diff --git a/src/PythonAPI/modeler/sketcher/__init__.py b/src/PythonAPI/modeler/sketcher/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/PythonAPI/modeler/sketcher/circle.py b/src/PythonAPI/modeler/sketcher/circle.py new file mode 100644 index 000000000..855651731 --- /dev/null +++ b/src/PythonAPI/modeler/sketcher/circle.py @@ -0,0 +1,15 @@ +from GeomDataAPI import geomDataAPI_Point2D + +class Circle(): + + def __init__(self, sketch, x, y, r): + self.my = sketch.addFeature("SketchCircle") + geomDataAPI_Point2D( self.my.data().attribute("CircleCenter") ).setValue(x, y) + self.my.data().real("CircleRadius").setValue(r) + self.my.execute() + + def centerData (self): + return geomDataAPI_Point2D( self.my.data().attribute("CircleCenter") ) + + def result (self): + return self.my.lastResult() # Returns the circular line attribute diff --git a/src/PythonAPI/modeler/sketcher/line.py b/src/PythonAPI/modeler/sketcher/line.py new file mode 100644 index 000000000..731a238fd --- /dev/null +++ b/src/PythonAPI/modeler/sketcher/line.py @@ -0,0 +1,40 @@ +from GeomDataAPI import geomDataAPI_Point2D + + +class Line(): + + def __init__(self, sketch, *args): + self.my = sketch.addFeature("SketchLine") + if len(args) == 4: + self.__createByCoordinates(*args) + elif len(args) == 2: + self.__createByPoints(*args) + elif len(args) == 1: + self.__createByName(sketch, *args) + else: + raise Exception("cannot create the Line") + + def __createByCoordinates(self, x1, y1, x2, y2): + geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ).setValue(x1, y1) + geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ).setValue(x2, y2) + self.my.execute() + + def __createByPoints(self, p1, p2): + geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ).setValue(p1.x(), p1.y()) + geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ).setValue(p2.x(), p2.y()) + self.my.execute() + + def __createByName(self, sketch, name): + self.my.data().selection("External").selectSubShape("EDGE", name) + self.my.execute() + rigid = sketch.addFeature("SketchConstraintRigid") + rigid.refattr("ConstraintEntityA").setObject( self.my.firstResult() ) + + def startPointData (self): + return geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ) + + def endPointData (self): + return geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ) + + def result (self): + return self.my.firstResult() \ No newline at end of file diff --git a/src/PythonAPI/modeler/sketcher/point.py b/src/PythonAPI/modeler/sketcher/point.py new file mode 100644 index 000000000..acd88be81 --- /dev/null +++ b/src/PythonAPI/modeler/sketcher/point.py @@ -0,0 +1,16 @@ +# Class definitions of Sketch features + +from GeomDataAPI import geomDataAPI_Point2D + +class Point(): + + def __init__(self, sketch, x, y): + self.my = sketch.addFeature("SketchPoint") + geomDataAPI_Point2D( self.my.data().attribute("PointCoordindates") ).setValue(x, y) + self.my.execute() + + def pointData (self): + return geomDataAPI_Point2D( self.my.data().attribute("PointCoordindates") ) + + def result (self): + return self.my.firstResult() diff --git a/src/PythonAPI/modeler/sketcher.py b/src/PythonAPI/modeler/sketcher/sketch.py similarity index 71% rename from src/PythonAPI/modeler/sketcher.py rename to src/PythonAPI/modeler/sketcher/sketch.py index c9737224e..14e53efcc 100644 --- a/src/PythonAPI/modeler/sketcher.py +++ b/src/PythonAPI/modeler/sketcher/sketch.py @@ -6,15 +6,18 @@ Copyright (C) 2014-20xx CEA/DEN, EDF R&D from ModelAPI import * from GeomDataAPI import * from GeomAlgoAPI import * +from modeler.sketcher.point import Point +from modeler.sketcher.line import Line +from modeler.sketcher.circle import Circle class Sketch(): def __init__(self, doc, plane): """Initializes a 2D Sketch on the given plane and adds the Sketch to the given Part or Partset. - The plane can be defined either by: - - a 3D axis system (geom.Ax3), - - an existing face identified by its topological name. + The plane can be defined either by: + - a 3D axis system (geom.Ax3), + - an existing face identified by its topological name. """ self.my = featureToCompositeFeature( doc.addFeature("Sketch") ) self.selection = None # Entities used for building the result shape @@ -69,7 +72,7 @@ class Sketch(): The end of consecutive segments are defined as coincident. """ pg = self.addPolyline(*coords) - # Closing the poly-line supposed being defined by at least 3 points + # Closing the poly-line supposed being defined by at least 3 points c0 = coords[0] cn = coords[len(coords)-1] ln = self.addLine(cn, c0) @@ -144,8 +147,8 @@ class Sketch(): def selectFace (self, *args): """Selects the geometrical entities of this Sketch on which the result Face must be built. - When no entity is given, the face is based on all existing geometry of this Sketch. - """ + When no entity is given, the face is based on all existing geometry of this Sketch. + """ #self.resultype ="Face" if len(args) == 0: self.selection = modelAPI_ResultConstruction( self.my.firstResult() ).shape() @@ -169,75 +172,3 @@ class Sketch(): def result (self): """Returns the result data of this Feature.""" return self.my.firstResult() - - -# Class definitions of Sketch features - -class Point(): - - def __init__(self, sketch, x, y): - self.my = sketch.addFeature("SketchPoint") - geomDataAPI_Point2D( self.my.data().attribute("PointCoordindates") ).setValue(x, y) - self.my.execute() - - def pointData (self): - return geomDataAPI_Point2D( self.my.data().attribute("PointCoordindates") ) - - def result (self): - return self.my.firstResult() - - -class Line(): - - def __init__(self, sketch, *args): - self.my = sketch.addFeature("SketchLine") - if len(args) == 4: - self.__createByCoordinates(*args) - elif len(args) == 2: - self.__createByPoints(*args) - elif len(args) == 1: - self.__createByName(sketch, *args) - else: - raise Exception("cannot create the Line") - - def __createByCoordinates(self, x1, y1, x2, y2): - geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ).setValue(x1, y1) - geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ).setValue(x2, y2) - self.my.execute() - - def __createByPoints(self, p1, p2): - geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ).setValue(p1.x(), p1.y()) - geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ).setValue(p2.x(), p2.y()) - self.my.execute() - - def __createByName(self, sketch, name): - self.my.data().selection("External").selectSubShape("EDGE", name) - self.my.execute() - rigid = sketch.addFeature("SketchConstraintRigid") - rigid.refattr("ConstraintEntityA").setObject( self.my.firstResult() ) - - def startPointData (self): - return geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ) - - def endPointData (self): - return geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ) - - def result (self): - return self.my.firstResult() - - -class Circle(): - - def __init__(self, sketch, x, y, r): - self.my = sketch.addFeature("SketchCircle") - geomDataAPI_Point2D( self.my.data().attribute("CircleCenter") ).setValue(x, y) - self.my.data().real("CircleRadius").setValue(r) - self.my.execute() - - def centerData (self): - return geomDataAPI_Point2D( self.my.data().attribute("CircleCenter") ) - - def result (self): - return self.my.lastResult() # Returns the circular line attribute - - -- 2.39.2