Salome HOME
initial commit
[plugins/xtplugin.git] / src / XTImport / XTImport.cxx
1 // Copyright (C) 2014  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 <XTPLUGIN_exports.h>
21 #include <XTPLUGIN_version.h>
22
23 #include <Basics_Utils.hxx>
24
25 #include <IFSelect_ReturnStatus.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TDF_Label.hxx>
28 #include <TDataStd_Name.hxx>
29 #include <TNaming_Builder.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Iterator.hxx>
32 #include <TransferBRep.hxx>
33 #include <Transfer_Binder.hxx>
34 #include <Transfer_TransientProcess.hxx>
35 #include <XtAttributes_AttribGroup.hxx>
36 #include <XtAttributes_Attribute.hxx>
37 #include <XtAttributes_CharValues.hxx>
38 #include <XSControl_TransferReader.hxx>
39 #include <XSControl_WorkSession.hxx>
40 #include <XtControl_Reader.hxx>
41 #include <XtData_HArray1OfObject.hxx>
42 #include <XtData_Model.hxx>
43 #include <XtData_Object.hxx>
44 #include <XtGeom_Curve.hxx>
45 #include <XtGeom_Point.hxx>
46 #include <XtGeom_Surface.hxx>
47 #include <XtTopoDS_Topology.hxx>
48
49 #ifdef XT_HASLICENSE
50 #include <XTPLUGIN_license.h>
51
52 #include <Standard_LicenseError.hxx>
53 #include <OCCLicense_Activate.hxx>
54 #endif // XT_HASLICENSE
55
56 extern "C"
57 {
58   /*!
59     \brief Get version of the plugin.
60     \return the version of the plugin
61   */
62   XTPLUGIN_EXPORT
63   int GetVersion()
64   {
65     return XTPLUGIN_VERSION;
66   }
67
68   /*!
69     \brief Get version of the plugin.
70     \return the string representation of the plugin's version
71   */
72   XTPLUGIN_EXPORT
73   char* GetVersionStr()
74   {
75     return (char*)XTPLUGIN_VERSION_STR;
76   }
77
78   /*!
79     \brief Import shape from the XT format.
80     \param theFileName file path
81     \param theFormatName file format signature
82     \param theError error description, if there's any, is returned via this parameter
83     \param theShapeLabel a label in the CAF tree where shape attributes are set to
84     \return imported shape
85   */
86   XTPLUGIN_EXPORT
87   TopoDS_Shape Import(const TCollection_AsciiString& theFileName,
88                       const TCollection_AsciiString& /*theFormatName*/,
89                       TCollection_AsciiString&       theError,
90                       const TDF_Label& theShapeLabel)
91   {
92     TopoDS_Shape aResShape;
93     
94 #ifdef XT_HASLICENSE
95     try {
96       OCCLicense_Activate( "DXF-R-"OCC_VERSION_STRING, DXF_READ_LICENSE);
97     }
98     catch (Standard_LicenseError) {
99       return aResShape;
100     }
101 #endif // XT_HASLICENSE
102     
103     // Set "C" numeric locale to save numbers correctly
104     Kernel_Utils::Localizer loc;
105     
106     XtControl_Reader aReader;
107     
108     try {
109       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
110       if (status == IFSelect_RetDone) {
111         aReader.TransferRoots();
112         aResShape = aReader.OneShape();
113         
114         // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
115         // It should be removed after patching OCCT for bug OCC22436
116         // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
117         if (aResShape.ShapeType() == TopAbs_COMPOUND) {
118           int nbSub1 = 0;
119           TopoDS_Shape currShape;
120           TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
121           for (; It.More(); It.Next()) {
122             nbSub1++;
123             currShape = It.Value();
124           }
125           if (nbSub1 == 1)
126             aResShape = currShape;
127         }
128         Handle(XtData_Model) aModel = Handle(XtData_Model)::DownCast( aReader.WS()->Model() );
129         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
130         if (!TR.IsNull()) {
131           Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
132           Standard_Integer nb = aModel->NbEntities();
133           for (Standard_Integer i = 1; i <= nb; i ++) {
134             Handle(XtData_Object) XtOb = Handle(XtData_Object)::DownCast ( aModel->Value(i) );
135             if (XtOb.IsNull()) continue;
136             
137             Handle(XtAttributes_AttribGroup) XtAG;
138             Standard_Boolean MayBeName = Standard_False;
139             
140             if (XtOb->IsKind(STANDARD_TYPE(XtTopoDS_Topology))) {
141               Handle(XtTopoDS_Topology) XtT = Handle(XtTopoDS_Topology)::DownCast(XtOb);
142               XtAG = XtT->AttributesGroups();
143               MayBeName = Standard_True;
144             }
145             else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Surface))) {
146               Handle(XtGeom_Surface) XtGS = Handle(XtGeom_Surface)::DownCast(XtOb);
147               XtAG = XtGS->AttributesGroups();
148               MayBeName = Standard_True;
149             }
150             else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Curve))) {
151               Handle(XtGeom_Curve) XtGC = Handle(XtGeom_Curve)::DownCast(XtOb);
152               XtAG = XtGC->AttributesGroups();
153               MayBeName = Standard_True;
154             }
155             else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Point))) {
156               Handle(XtGeom_Point) XtGP = Handle(XtGeom_Point)::DownCast(XtOb);
157               XtAG = XtGP->AttributesGroups();
158               MayBeName = Standard_True;
159             }
160             
161             if (MayBeName) {
162               TCollection_AsciiString string;
163               Standard_Boolean IsName = Standard_False;
164               Handle(XtAttributes_Attribute) XtA = Handle(XtAttributes_Attribute)::DownCast(XtAG);
165               if (!XtA.IsNull()) {
166                 Handle(XtAttributes_AttribDef) XtAD = XtA->Definition();
167                 while (!XtAD.IsNull()) {
168                   if (XtAD->TypeID()==8017) {
169                     const Handle(XtData_HArray1OfObject) &HAFV = XtA->Fields();
170                     const Standard_Integer NbFV = HAFV->Length();
171                     for (Standard_Integer j=1; j<=NbFV; j++) {
172                       const Handle(XtData_Object) &FV = HAFV->Value(j);
173                       if(!FV.IsNull() && FV->IsKind(STANDARD_TYPE(XtAttributes_CharValues))) {
174                         string = Handle(XtAttributes_CharValues)::DownCast(FV)->Values();
175                         IsName = Standard_True;
176                       }
177                     }
178                   }
179                   XtAD = XtAD->Next();
180                 }
181               }
182               
183               if (IsName) {
184                 // find target shape
185                 Handle(Transfer_Binder) binder = TP->Find ( XtOb );
186                 if ( binder.IsNull() ) continue;
187                 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
188                 if ( S.IsNull() ) continue;
189                 
190                 TDF_Label L;
191                 TDF_TagSource aTag;
192                 L = aTag.NewChild(theShapeLabel);
193                 TNaming_Builder tnBuild(L);
194                 tnBuild.Generated(S);
195                 
196                 // set a name to the document
197                 string.LeftAdjust();
198                 string.RightAdjust();
199                 TCollection_ExtendedString str ( string );
200                 TDataStd_Name::Set ( L, str );
201               }
202             }
203           }
204         }
205         else {
206           theError = "Wrong format of the imported file. Can't import file.";
207           aResShape.Nullify();
208         }
209       }
210     }
211     catch (Standard_Failure) {
212       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
213       theError = aFail->GetMessageString();
214       aResShape.Nullify();
215     }
216     return aResShape;
217   }
218 } // end of extern "C"