Back to index

salome-geom  6.5.0
sketcher.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 #
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # This library is free software; you can redistribute it and/or
00006 # modify it under the terms of the GNU Lesser General Public
00007 # License as published by the Free Software Foundation; either
00008 # version 2.1 of the License.
00009 #
00010 # This library is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 # Lesser General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU Lesser General Public
00016 # License along with this library; if not, write to the Free Software
00017 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 #
00019 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00020 #
00021 """
00022 This module provides the user with a simple python API to realize
00023 various sketches from the GEOM text user interface.
00024 
00025 Example::
00026 
00027     from salome.geom.sketcher import Sketcher
00028 
00029     # Create the sketch
00030     mysketcher = Sketcher()
00031     mysketcher.startAtPoint(0,0)
00032     mysketcher.translateToPoint(100,0)
00033     mysketcher.translateWithAngleAndLength(30,100)
00034     mysketcher.intersectYWithAngle(15,100)
00035     mysketcher.close()
00036 
00037     # Get the geom object
00038     wire = mysketcher.getGeomWire()
00039 
00040     # Put it in the study
00041     import geompy
00042     geompy.addToStudy( wire, 'mysketch' )
00043 
00044 Additionnal examples can be found as unit tests in the source code.
00045 """
00046 
00047 geompyEnable = True
00048 try:
00049     import geompy
00050 except:
00051     geompyEnable = False
00052     
00053 
00054 class Sketcher:
00055 
00056     __sketch = None
00057     __xstart = 0
00058     __ystart = 0
00059 
00060     def __init__(self):
00061         self.__sketch = "Sketcher:"
00062 
00063     def startAtPoint(self,x=0,y=0):
00064         self.__sketch = "Sketcher:F "+str(x)+" "+str(y)
00065         # the data x and y are registered to process the ending closure.
00066         self.__xstart = x
00067         self.__ystart = y
00068 
00069     def translateToPoint(self,x=10,y=0):
00070         '''segment->point->absolute'''
00071         self.__sketch = self.__sketch + ":TT "+str(x)+" "+str(y) 
00072 
00073     def translateWithVector(self,deltax=10,deltay=0):
00074         '''segment->point->relative'''
00075         self.__sketch = self.__sketch + ":T "+str(deltax)+" "+str(deltay) 
00076 
00077     #def translateToSelection(self,vertex):
00078     #    '''segment->point->selection'''
00079     #    self.__sketch = self.__sketch + ":T "+str(deltax)+" "+str(deltay) 
00080 
00081     def translateWithAngleAndLength(self,angle=30,length=100):
00082         '''segment->direction->angle+length'''
00083         self.__sketch = self.__sketch + ":R "+str(angle)+":L "+str(length)
00084 
00085     def intersectXWithAngle(self,angle=30,x=100):
00086         '''segment->direction->angle+length'''
00087         self.__sketch = self.__sketch + ":R "+str(angle)+":IX "+str(x)
00088 
00089     def intersectYWithAngle(self,angle=30,y=100):
00090         '''segment->direction->angle+length'''
00091         self.__sketch = self.__sketch + ":R "+str(angle)+":IY "+str(y)
00092 
00093     def close(self):
00094         '''
00095         add a segment to reach the starting point and get a closed wire.
00096         '''
00097         self.__sketch = self.__sketch + ":WW"
00098 
00099     def toString(self):
00100         return self.__sketch
00101 
00102     def getGeomWire(self):
00103         if not geompyEnable:
00104             return None
00105         wire = geompy.MakeSketcher(self.__sketch, [0, 0, 0, 0, 0, 1, 1, 0, -0])
00106         return wire
00107 #
00108 # ===============================================================
00109 # Use cases and unit tests
00110 # ===============================================================
00111 #
00112 def TEST_toString():
00113     mysketcher = Sketcher()
00114 
00115     mysketcher.startAtPoint(1.234,4.321)
00116     mysketcher.translateToPoint(2.234,5.321)
00117 
00118     expectedResult = "Sketcher:F 1.234 4.321:TT 2.234 5.321"
00119     result = mysketcher.toString()
00120     print "sketcher=",mysketcher.toString()
00121     if result == expectedResult:
00122         print "OK"
00123     else:
00124         print "Not OK"
00125 
00126 def TEST_usingGeom():
00127     mysketcher = Sketcher()
00128     mysketcher.startAtPoint(0,0)
00129     mysketcher.translateToPoint(100,0)
00130     mysketcher.translateWithAngleAndLength(30,100)
00131     mysketcher.intersectYWithAngle(15,100)
00132     mysketcher.close()
00133     wire = mysketcher.getGeomWire()
00134     if geompyEnable:
00135         geompy.addToStudy( wire, "Sketch" )
00136         
00137 
00138 if __name__ == "__main__":
00139     #TEST_toString()
00140     TEST_usingGeom()