1 // Copyright (C) 2014 OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <XTPLUGIN_exports.h>
21 #include <XTPLUGIN_version.h>
23 #include <Basics_Utils.hxx>
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>
50 #include <XTPLUGIN_license.h>
52 #include <Standard_LicenseError.hxx>
53 #include <OCCLicense_Activate.hxx>
54 #endif // XT_HASLICENSE
59 \brief Get version of the plugin.
60 \return the version of the plugin
65 return XTPLUGIN_VERSION;
69 \brief Get version of the plugin.
70 \return the string representation of the plugin's version
75 return (char*)XTPLUGIN_VERSION_STR;
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
87 TopoDS_Shape Import(const TCollection_AsciiString& theFileName,
88 const TCollection_AsciiString& /*theFormatName*/,
89 TCollection_AsciiString& theError,
90 const TDF_Label& theShapeLabel)
92 TopoDS_Shape aResShape;
96 OCCLicense_Activate( "DXF-R-"OCC_VERSION_STRING, DXF_READ_LICENSE);
98 catch (Standard_LicenseError) {
101 #endif // XT_HASLICENSE
103 // Set "C" numeric locale to save numbers correctly
104 Kernel_Utils::Localizer loc;
106 XtControl_Reader aReader;
109 IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
110 if (status == IFSelect_RetDone) {
111 aReader.TransferRoots();
112 aResShape = aReader.OneShape();
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) {
119 TopoDS_Shape currShape;
120 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
121 for (; It.More(); It.Next()) {
123 currShape = It.Value();
126 aResShape = currShape;
128 Handle(XtData_Model) aModel = Handle(XtData_Model)::DownCast( aReader.WS()->Model() );
129 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
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;
137 Handle(XtAttributes_AttribGroup) XtAG;
138 Standard_Boolean MayBeName = Standard_False;
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;
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;
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;
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;
162 TCollection_AsciiString string;
163 Standard_Boolean IsName = Standard_False;
164 Handle(XtAttributes_Attribute) XtA = Handle(XtAttributes_Attribute)::DownCast(XtAG);
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;
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;
192 L = aTag.NewChild(theShapeLabel);
193 TNaming_Builder tnBuild(L);
194 tnBuild.Generated(S);
196 // set a name to the document
198 string.RightAdjust();
199 TCollection_ExtendedString str ( string );
200 TDataStd_Name::Set ( L, str );
206 theError = "Wrong format of the imported file. Can't import file.";
211 catch (Standard_Failure) {
212 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
213 theError = aFail->GetMessageString();
218 } // end of extern "C"