Salome HOME
7ca40305f969e08f1881ece7a9143237b8e1fb07
[modules/geom.git] / src / GEOM_SWIG / STEPPluginBuilder.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2014-2023  CEA/DEN, EDF R&D, 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 GEOM import ISTEPOperations
22 import GEOM
23
24 # Engine Library Name
25 __libraryName__ = "STEPPluginEngine"
26
27 def GetSTEPPluginOperations(self):
28     anOp = self.GetPluginOperations(__libraryName__)
29     return anOp._narrow(ISTEPOperations)
30
31 ## Export the given shape into a file with given name in STEP format.
32 #  @param theObject Shape to be stored in the file.
33 #  @param theFileName Name of the file to store the given shape in.
34 #  @param theUnit the length unit (see GEOM::length_unit). In meters by default.
35 #  @ingroup l2_import_export
36 def ExportSTEP(self, theObject, theFileName, theUnit=GEOM.LU_METER):
37     """
38     Export the given shape into a file with given name in STEP format.
39
40     Parameters: 
41         theObject Shape to be stored in the file.
42         theFileName Name of the file to store the given shape in.
43         theUnit the length unit (see GEOM::length_unit). In meters by default.
44     """
45     anOp = GetSTEPPluginOperations(self)
46     anOp.ExportSTEP(theObject, theFileName, theUnit)
47     if anOp.IsDone() == 0:
48         raise RuntimeError("Export : " + anOp.GetErrorCode())
49         pass
50     pass
51
52 ## Import a shape from the STEP file with given name.
53 #  @param theFileName The file, containing the shape.
54 #  @param theIsIgnoreUnits If True, file length units will be ignored (set to 'meter')
55 #         and result model will be scaled, if its units are not meters.
56 #         If False (default), file length units will be taken into account.
57 #  @param IsCreateAssemblies If True, for each assembly compound is created in
58 #         the result. If False Compounds that contain a single shape
59 #         are eliminated from the result.
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 #  @note Auto publishing is allowed for the shape itself. Imported
69 #        material groups are not automatically published.
70 #
71 #  @ref swig_Import_Export "Example"
72 #  @ingroup l2_import_export
73 def ImportSTEP(self, theFileName, theIsIgnoreUnits = False,
74                IsCreateAssemblies = False, theName=None):
75     """
76     Import a shape from the STEP file with given name.
77
78     Parameters:
79         theFileName The file, containing the shape.
80         ignoreUnits If True, file length units will be ignored (set to 'meter')
81                     and result model will be scaled, if its units are not meters.
82                     If False (default), file length units will be taken into account.
83         IsCreateAssemblies If True, for each assembly compound is created in
84                     the result. If False Compounds that contain a single shape
85                     are eliminated from the result.
86         theName Object name; when specified, this parameter is used
87                 for result publication in the study. Otherwise, if automatic
88                 publication is switched on, default value is used for result name.
89
90     Returns:
91         New GEOM.GEOM_Object, containing the imported shape.
92         If material names are imported it returns the list of
93         objects. The first one is the imported object followed by
94         material groups.
95     Note:
96         Auto publishing is allowed for the shape itself. Imported
97         material groups are not automatically published.
98     """
99     # Example: see GEOM_TestOthers.py
100     from salome.geom.geomBuilder import RaiseIfFailed
101     anOp = GetSTEPPluginOperations(self)
102     
103     anIsIgnoreUnits = theIsIgnoreUnits
104     anIsCreateAssemblies = IsCreateAssemblies;
105     aName = theName
106     if isinstance( theIsIgnoreUnits, str ):
107         anIsIgnoreUnits = False
108         aName = theIsIgnoreUnits
109         pass
110     elif isinstance( IsCreateAssemblies, str ):
111         anIsCreateAssemblies = False
112         aName = IsCreateAssemblies
113         pass
114
115     aListObj = anOp.ImportSTEP(theFileName,anIsIgnoreUnits,anIsCreateAssemblies)
116     RaiseIfFailed("ImportSTEP", anOp)
117     aNbObj = len(aListObj)
118     if aNbObj > 0:
119         self._autoPublish(aListObj[0], aName, "imported")
120     if aNbObj == 1:
121         return aListObj[0]
122     return aListObj
123
124 ## Return length unit from given STEP file
125 #  @param theFileName The file, containing the shape.
126 #  @return String, containing the units name.
127 #
128 #  @ref swig_Import_Export "Example"
129 #  @ingroup l2_import_export
130 def GetSTEPUnit(self, theFileName):
131     """
132     Return length units from given STEP file
133
134     Parameters:
135         theFileName The file, containing the shape.
136
137     Returns:
138         String, containing the units name.
139     """
140     # Example: see GEOM_TestOthers.py
141     anOp = GetSTEPPluginOperations(self)
142     aUnitName = anOp.ReadValue( theFileName, "LEN_UNITS")
143     return aUnitName