ImportIges.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <fcntl.h>
00027 # include <BRep_Builder.hxx>
00028 # include <Interface_Static.hxx>
00029 # include <TopTools_HSequenceOfShape.hxx>
00030 # include <IGESControl_Controller.hxx>
00031 # include <IGESControl_Reader.hxx>
00032 # include <TopoDS.hxx>
00033 # include <TopoDS_Shape.hxx>
00034 # include <TopoDS_Shell.hxx>
00035 # include <TopoDS_Solid.hxx>
00036 # include <TopoDS_Compound.hxx>
00037 # include <TopExp_Explorer.hxx>
00038 # include <Message_MsgFile.hxx>
00039 # include <Handle_TColStd_HSequenceOfTransient.hxx>
00040 # include <TColStd_HSequenceOfTransient.hxx>
00041 # include <IGESBasic_Group.hxx>
00042 # include <IGESSolid_ManifoldSolid.hxx>
00043 # include <IGESBasic_SingularSubfigure.hxx>
00044 #endif
00045 
00046 #include <Handle_XSControl_WorkSession.hxx>
00047 #include <Handle_XSControl_TransferReader.hxx>
00048 #include <XSControl_WorkSession.hxx>
00049 #include <XSControl_TransferReader.hxx>
00050 #include <Transfer_TransientProcess.hxx>
00051 
00052 #include <Interface_EntityIterator.hxx>
00053 #include <Quantity_Color.hxx>
00054 #include <TCollection_ExtendedString.hxx>
00055 
00056 #include <Base/Console.h>
00057 #include <Base/Sequencer.h>
00058 #include <App/Application.h>
00059 #include <App/Document.h>
00060 
00061 #include "ImportIges.h"
00062 #include "PartFeature.h"
00063 #include "ProgressIndicator.h"
00064 
00065 using namespace Part;
00066 
00067 int Part::ImportIgesParts(App::Document *pcDoc, const char* FileName)
00068 {
00069     try {
00070         Base::FileInfo fi(FileName);
00071         // read iges file
00072         // http://www.opencascade.org/org/forum/thread_20801/
00073         IGESControl_Controller::Init();
00074         Interface_Static::SetIVal("read.surfacecurve.mode",3);
00075 
00076         // load data exchange message files
00077         Message_MsgFile::LoadFromEnv("CSF_XSMessage","IGES");
00078 
00079         // load shape healing message files
00080         Message_MsgFile::LoadFromEnv("CSF_SHMessageStd","SHAPEStd");
00081 
00082         IGESControl_Reader aReader;
00083         if (aReader.ReadFile((const Standard_CString)FileName) != IFSelect_RetDone)
00084             throw Base::Exception("Error in reading IGES");
00085 
00086         // check file conformity and output stats
00087         aReader.PrintCheckLoad(Standard_True,IFSelect_GeneralInfo);
00088 
00089 #if 1
00090         std::string aName = fi.fileNamePure();
00091         Handle_Message_ProgressIndicator pi = new ProgressIndicator(100);
00092         pi->NewScope(100, "Reading IGES file...");
00093         pi->Show();
00094         aReader.WS()->MapReader()->SetProgress(pi);
00095 
00096         // make model
00097         aReader.ClearShapes();
00098         Standard_Integer nbRootsForTransfer = aReader.NbRootsForTransfer();
00099         aReader.TransferRoots();
00100         pi->EndScope();
00101 
00102         // put all other free-flying shapes into a single compound
00103         Standard_Boolean emptyComp = Standard_True;
00104         BRep_Builder builder;
00105         TopoDS_Compound comp;
00106         builder.MakeCompound(comp);
00107 
00108         Standard_Integer nbShapes = aReader.NbShapes();
00109         for (Standard_Integer i=1; i<=nbShapes; i++) {
00110             TopoDS_Shape aShape = aReader.Shape(i);
00111             if (!aShape.IsNull()) {
00112                 if (aShape.ShapeType() == TopAbs_SOLID ||
00113                     aShape.ShapeType() == TopAbs_COMPOUND ||
00114                     aShape.ShapeType() == TopAbs_SHELL) {
00115                         App::DocumentObject* obj = pcDoc->addObject("Part::Feature", aName.c_str());
00116                         static_cast<Part::Feature*>(obj)->Shape.setValue(aShape);
00117                 }
00118                 else {
00119                     builder.Add(comp, aShape);
00120                     emptyComp = Standard_False;
00121                 }
00122             }
00123         }
00124         if (!emptyComp) {
00125             std::string name = fi.fileNamePure();
00126             Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject
00127                 ("Part::Feature", name.c_str()));
00128             pcFeature->Shape.setValue(comp);
00129         }
00130 #else
00131         // put all other free-flying shapes into a single compound
00132         Standard_Boolean emptyComp = Standard_True;
00133         BRep_Builder builder;
00134         TopoDS_Compound comp;
00135         builder.MakeCompound(comp);
00136 
00137         // get all entities
00138         Handle_TColStd_HSequenceOfTransient aRootList=aReader.GiveList("xst-transferrable-roots");
00139         Base::SequencerLauncher seq("Reading IGES file...", aRootList->Length());
00140         Standard_Integer j;
00141         for (j=1; j<=aRootList->Length(); j++) {
00142             seq.next();
00143             Handle(IGESData_IGESEntity) igesEntity = Handle(IGESData_IGESEntity)::DownCast(aRootList->Value(j));
00144             if (igesEntity.IsNull()) continue;
00145             // clear any old shape
00146             aReader.ClearShapes();
00147 
00148 #ifdef _DEBUG
00149             std::string type = igesEntity->DynamicType()->Name();
00150             (void)type;
00151 #endif
00152             
00153             // is it a group, singular sub-figure or solid?
00154             if (igesEntity->IsKind(STANDARD_TYPE(IGESBasic_Group)) ||
00155                 igesEntity->IsKind(STANDARD_TYPE(IGESBasic_SingularSubfigure)) || 
00156                 igesEntity->IsKind(STANDARD_TYPE(IGESSolid_ManifoldSolid))) {
00157                 try {
00158                     if (aReader.TransferEntity(igesEntity)) {
00159                         if (aReader.NbShapes()>0) {
00160                             // get the shape
00161                             std::string aName = fi.fileNamePure();
00162                             if (igesEntity->HasShortLabel()) {
00163                                 Handle(TCollection_HAsciiString) aLabel=igesEntity->ShortLabel();
00164                                 aName = aLabel->ToCString();
00165                             }
00166                             TopoDS_Shape aShape=aReader.OneShape();
00167                             App::DocumentObject* obj = pcDoc->addObject("Part::Feature", aName.c_str());
00168                             obj->Label.setValue(aName);
00169                             static_cast<Part::Feature*>(obj)->Shape.setValue(aShape);
00170                             int iColor;
00171                             if (igesEntity->RankColor()>-1) {
00172                                 iColor = igesEntity->RankColor();
00173                             }
00174                         }
00175                     }
00176                 }
00177                 catch (Standard_Failure) {
00178                 }
00179             }
00180             // normal shapes
00181             else {
00182                 try {
00183                     if (aReader.TransferEntity(igesEntity)) {
00184                         if (aReader.NbShapes()>0) {
00185                             TopoDS_Shape aShape=aReader.OneShape();
00186                             builder.Add(comp, aShape);
00187                             emptyComp = Standard_False;
00188                         }
00189                     }
00190                 }
00191                 catch (Standard_Failure) {
00192                 }
00193             }
00194         }
00195 
00196         if (!emptyComp) {
00197             std::string name = fi.fileNamePure();
00198             Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject
00199                 ("Part::Feature", name.c_str()));
00200             pcFeature->Shape.setValue(comp);
00201         }
00202 #endif
00203     }
00204     catch (Standard_Failure) {
00205         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00206         throw Base::Exception(aFail->GetMessageString());
00207     }
00208 
00209     return 0;
00210 }

Generated on Wed Nov 23 19:00:18 2011 for FreeCAD by  doxygen 1.6.1