Salome HOME
Copyrights update 2015.
[plugins/acisplugin.git] / src / ACISImport / ACISImport.cxx
1 // Copyright (C) 2014-2015  OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ACISPLUGIN_exports.h>
21 #include <ACISPLUGIN_version.h>
22
23 #include <Basics_Utils.hxx>
24
25 #include <AcisAttr_AttribGenName.hxx>
26 #include <AcisEnt_Attrib.hxx>
27 #include <IFSelect_ReturnStatus.hxx>
28 #include <Interface_InterfaceModel.hxx>
29 #include <SatControl_Reader.hxx>
30 #include <TCollection_AsciiString.hxx>
31 #include <TDataStd_Name.hxx>
32 #include <TDF_Label.hxx>
33 #include <TNaming_Builder.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TransferBRep.hxx>
36 #include <Transfer_Binder.hxx>
37 #include <Transfer_TransientProcess.hxx>
38 #include <XSControl_TransferReader.hxx>
39 #include <XSControl_WorkSession.hxx>
40
41 #ifdef ACIS_HASLICENSE
42 #include <ACISPLUGIN_license.h>
43
44 #include <OCCLicense_Activate.hxx>
45 #include <Standard_LicenseError.hxx>
46 #endif // ACIS_HASLICENSE
47
48 extern "C"
49 {
50   /*!
51     \brief Get version of the plugin.
52     \return the version of the plugin
53   */
54   ACISPLUGIN_EXPORT
55   int GetVersion()
56   {
57     return ACISPLUGIN_VERSION;
58   }
59
60   /*!
61     \brief Get version of the plugin.
62     \return the string representation of the plugin's version
63   */
64   ACISPLUGIN_EXPORT
65   char* GetVersionStr()
66   {
67     return (char*)ACISPLUGIN_VERSION_STR;
68   }
69
70   /*!
71     \brief Import shape from the ACIS format.
72     \param theFileName file path
73     \param theFormatName file format signature
74     \param theError error description, if there's any, is returned via this parameter
75     \param theShapeLabel a label in the CAF tree where shape attributes are set to
76     \return imported shape
77   */
78   ACISPLUGIN_EXPORT
79   TopoDS_Shape Import(const TCollection_AsciiString& theFileName,
80                       const TCollection_AsciiString& /*theFormatName*/,
81                       TCollection_AsciiString&       theError,
82                       const TDF_Label& theShapeLabel)
83   {
84     TopoDS_Shape aResShape;
85     
86 #ifdef ACIS_HASLICENSE
87     try {
88       OCCLicense_Activate( "SAT-R-"OCC_VERSION_STRING, ACIS_READ_LICENSE);
89     }
90     catch (Standard_LicenseError) {
91       return aResShape;
92     }
93 #endif // ACIS_HASLICENSE
94
95     // Set "C" numeric locale to save numbers correctly
96     Kernel_Utils::Localizer loc;
97     
98     SatControl_Reader aReader;
99     
100     try {
101       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
102       if (status == IFSelect_RetDone) {
103         aReader.TransferRoots();
104         aResShape = aReader.OneShape();
105         Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
106         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
107         if( !TR.IsNull() ) {
108           Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
109           Standard_Integer nb = Model->NbEntities();
110           for (Standard_Integer i = 1; i <= nb; i ++) {
111             Handle(AcisEnt_Attrib) attr = Handle(AcisEnt_Attrib)::DownCast ( Model->Value(i) );
112             if ( attr.IsNull() ) continue; //not only Entity Label (f.18) but Name Property also
113             
114             TCollection_AsciiString aName;
115             if ( attr->IsKind(STANDARD_TYPE(AcisAttr_AttribGenName)) ) {
116               Handle(AcisAttr_AttribGenName) attrname = Handle(AcisAttr_AttribGenName)::DownCast ( attr );
117               aName = attrname->myNameAttr;
118             }
119             else continue; // no name assigned
120             
121             // find target entity
122             Handle(AcisEnt_Entity) ent;
123             do {
124               ent = attr->myEntity;
125               attr = Handle(AcisEnt_Attrib)::DownCast ( ent );
126             } while ( ! attr.IsNull() );
127             if ( ent.IsNull() ) continue; // strange - no normal entity found ?
128             
129             // find target shape
130             Handle(Transfer_Binder) binder = TP->Find ( ent );
131             if ( binder.IsNull() ) continue;
132             TopoDS_Shape S = TransferBRep::ShapeResult (binder);
133             if ( S.IsNull() ) continue;
134             
135             //if( S.IsEqual(aResShape) ) continue;
136             
137             // create label and set shape
138             TDF_Label L;
139             TDF_TagSource aTag;
140             L = aTag.NewChild(theShapeLabel);
141             TNaming_Builder tnBuild(L);
142             tnBuild.Generated(S);
143             
144             // set a name
145             TCollection_ExtendedString str(aName);
146             TDataStd_Name::Set(L,str);
147           }
148         }
149       }
150       else {
151         theError = "Wrong format of the imported file. Can't import file.";
152         aResShape.Nullify();
153       }
154     }
155     catch (Standard_Failure) {
156       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
157       theError = aFail->GetMessageString();
158       aResShape.Nullify();
159     }
160     return aResShape;
161   }
162 } // end of extern "C"
163