Salome HOME
04b229a43e449189d0a99931f4b0c81cb0a698aa
[plugins/dxfplugin.git] / src / DXFPluginBuilder.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2014-2016  OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 from DXFPlugin import IDXFOperations
22
23 # Engine Library Name
24 __libraryName__ = "DXFPluginEngine"
25
26 ## Get DXF plugin operations interface
27 #  @return an instance of DXF plugin operations interface
28 def GetDXFPluginOperations(self):
29     """
30     Get DXF plugin operations interface.
31
32     Returns:
33         An instance of DXF plugin operations interface
34     """
35     anOp = self.GetPluginOperations(self.myStudyId, __libraryName__)
36     return anOp._narrow(IDXFOperations)
37
38 ## Export the given shape into a file with given name in DXF format.
39 #  @param theObject Shape to be stored in the file.
40 #  @param theFileName Name of the file to store the given shape in.
41 #  @sa ImportDXF
42 def ExportDXF(self, theObject, theFileName):
43     """
44     Export the given shape into a file with given name in DXF format.
45
46     Parameters: 
47         theObject Shape to be stored in the file.
48         theFileName Name of the file to store the given shape in.
49     """
50     anOp = GetDXFPluginOperations(self)
51     anOp.ExportDXF(theObject, theFileName)
52     if anOp.IsDone() == 0:
53         raise RuntimeError,  "Export : " + anOp.GetErrorCode()
54         pass
55     pass
56
57
58 ## Import a shape from the DXF file
59 #  @param theFileName The file, containing the shape.
60 #  @param theName Object name; when specified, this parameter is used
61 #         for result publication in the study. Otherwise, if automatic
62 #         publication is switched on, default value is used for result name.
63 #
64 #  @return New GEOM.GEOM_Object, containing the imported shape.
65 #          If material names are imported it returns the list of
66 #          objects. The first one is the imported object followed by
67 #          material groups.
68 #
69 #  @note Auto publishing is allowed for the shape itself. Imported
70 #        material groups are not automatically published.
71 #
72 #  @sa ExportDXF
73 def ImportDXF(self, theFileName, theName=None):
74     """
75     Import a shape from the DXF file
76
77     Parameters: 
78         theFileName The file, containing the shape.
79         theName Object name; when specified, this parameter is used
80                 for result publication in the study. Otherwise, if automatic
81                 publication is switched on, default value is used for result name.
82
83     Returns:
84         New GEOM.GEOM_Object, containing the imported shape.
85         If material names are imported it returns the list of
86         objects. The first one is the imported object followed by
87         material groups.
88     Note:
89         Auto publishing is allowed for the shape itself. Imported
90         material groups are not automatically published.
91     """
92     from salome.geom.geomBuilder import RaiseIfFailed
93     anOp = GetDXFPluginOperations(self)
94     aListObj = anOp.ImportDXF(theFileName)
95     RaiseIfFailed("ImportDXF", anOp)
96     aNbObj = len(aListObj)
97     if aNbObj > 0:
98         self._autoPublish(aListObj[0], theName, "imported")
99     if aNbObj == 1:
100         return aListObj[0]
101     return aListObj