ImportStep.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2008     *
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 <TopTools_HSequenceOfShape.hxx>
00029 # include <STEPControl_Writer.hxx>
00030 # include <STEPControl_Reader.hxx>
00031 # include <StepData_StepModel.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 #endif
00039 
00040 #include <Handle_XSControl_WorkSession.hxx>
00041 #include <Handle_XSControl_TransferReader.hxx>
00042 #include <XSControl_WorkSession.hxx>
00043 #include <XSControl_TransferReader.hxx>
00044 #include <Transfer_TransientProcess.hxx>
00045 
00046 #include <STEPConstruct_Styles.hxx>
00047 #include <TColStd_HSequenceOfTransient.hxx>
00048 #include <STEPConstruct.hxx>
00049 #include <StepVisual_StyledItem.hxx>
00050 #include <Handle_StepShape_ShapeRepresentation.hxx>
00051 #include <Handle_StepVisual_PresentationStyleByContext.hxx>
00052 #include <StepVisual_StyleContextSelect.hxx>
00053 #include <StepVisual_PresentationStyleByContext.hxx>
00054 #include <Interface_EntityIterator.hxx>
00055 #include <StepRepr_RepresentedDefinition.hxx>
00056 #include <StepShape_ShapeDefinitionRepresentation.hxx>
00057 #include <StepRepr_CharacterizedDefinition.hxx>
00058 #include <StepRepr_ProductDefinitionShape.hxx>
00059 #include <Handle_StepRepr_AssemblyComponentUsage.hxx>
00060 #include <StepRepr_AssemblyComponentUsage.hxx>
00061 #include <StepRepr_SpecifiedHigherUsageOccurrence.hxx>
00062 #include <Quantity_Color.hxx>
00063 #include <TCollection_ExtendedString.hxx>
00064 #include <StepBasic_Product.hxx>
00065 #include <Handle_StepBasic_Product.hxx>
00066 #include <StepBasic_ProductDefinition.hxx>
00067 #include <Handle_StepBasic_ProductDefinition.hxx>
00068 #include <StepBasic_ProductDefinitionFormation.hxx>
00069 
00070 #include <Base/Console.h>
00071 #include <Base/Sequencer.h>
00072 #include <App/Application.h>
00073 #include <App/Document.h>
00074 
00075 #include "ImportStep.h"
00076 #include "PartFeature.h"
00077 #include "ProgressIndicator.h"
00078 
00079 using namespace Part;
00080 
00081 namespace Part {
00082 bool ReadColors (const Handle(XSControl_WorkSession) &WS, std::map<int, Quantity_Color>& hash_col);
00083 bool ReadNames (const Handle(XSControl_WorkSession) &WS);
00084 }
00085 
00086 int Part::ImportStepParts(App::Document *pcDoc, const char* Name)
00087 {
00088     STEPControl_Reader aReader;
00089     TopoDS_Shape aShape;
00090     Base::FileInfo fi(Name);
00091 
00092     if (!fi.isReadable()) {
00093         Base::Console().Log("ImportStep() not able to open %s!\n",Name);
00094         throw Base::Exception("Cannot open STEP file");
00095     }
00096 
00097     if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) {
00098         throw Base::Exception("Cannot open STEP file");
00099     }
00100 
00101     Handle_Message_ProgressIndicator pi = new ProgressIndicator(100);
00102     aReader.WS()->MapReader()->SetProgress(pi);
00103     pi->NewScope(100, "Reading STEP file...");
00104     pi->Show();
00105 
00106     // Root transfers
00107     Standard_Integer nbr = aReader.NbRootsForTransfer();
00108     //aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
00109     for (Standard_Integer n = 1; n<= nbr; n++) {
00110         Base::Console().Log("STEP: Transferring Root %d\n",n);
00111         aReader.TransferRoot(n);
00112     }
00113     pi->EndScope();
00114 
00115     // Collecting resulting entities
00116     Standard_Integer nbs = aReader.NbShapes();
00117     if (nbs == 0) {
00118         throw Base::Exception("No shapes found in file ");
00119     }
00120     else {
00121         //Handle(StepData_StepModel) Model = aReader.StepModel();
00122         //Handle_XSControl_WorkSession ws = aReader.WS();
00123         //Handle_XSControl_TransferReader tr = ws->TransferReader();
00124 
00125         std::map<int, Quantity_Color> hash_col;
00126         ReadColors(aReader.WS(), hash_col);
00127         //ReadNames(aReader.WS());
00128 
00129         for (Standard_Integer i=1; i<=nbs; i++) {
00130             Base::Console().Log("STEP:   Transferring Shape %d\n",i);
00131             aShape = aReader.Shape(i);
00132 
00133             // load each solid as an own object
00134             TopExp_Explorer ex;
00135             for (ex.Init(aShape, TopAbs_SOLID); ex.More(); ex.Next())
00136             {
00137                 // get the shape 
00138                 const TopoDS_Solid& aSolid = TopoDS::Solid(ex.Current());
00139 
00140                 std::string name = fi.fileNamePure();
00141                 //Handle_Standard_Transient ent = tr->EntityFromShapeResult(aSolid, 3);
00142                 //if (!ent.IsNull()) {
00143                 //    name += ws->Model()->StringLabel(ent)->ToCString();
00144                 //}
00145 
00146                 Part::Feature *pcFeature;
00147                 pcFeature = static_cast<Part::Feature*>(pcDoc->addObject("Part::Feature", name.c_str()));
00148                 pcFeature->Shape.setValue(aSolid);
00149 
00150                 // This is a trick to access the GUI via Python and set the color property
00151                 // of the associated view provider. If no GUI is up an exception is thrown
00152                 // and cleared immediately
00153                 std::map<int, Quantity_Color>::iterator it = hash_col.find(aSolid.HashCode(INT_MAX));
00154                 if (it != hash_col.end()) {
00155                     try {
00156                         Py::Object obj(pcFeature->getPyObject(), true);
00157                         Py::Object vp(obj.getAttr("ViewObject"));
00158                         Py::Tuple col(3);
00159                         col.setItem(0, Py::Float(it->second.Red()));
00160                         col.setItem(1, Py::Float(it->second.Green()));
00161                         col.setItem(2, Py::Float(it->second.Blue()));
00162                         vp.setAttr("ShapeColor", col);
00163                         //Base::Console().Message("Set color to shape\n");
00164                     }
00165                     catch (Py::Exception& e) {
00166                         e.clear();
00167                     }
00168                 }
00169             }
00170             // load all non-solids now
00171             for (ex.Init(aShape, TopAbs_SHELL, TopAbs_SOLID); ex.More(); ex.Next())
00172             {
00173                 // get the shape 
00174                 const TopoDS_Shell& aShell = TopoDS::Shell(ex.Current());
00175 
00176                 std::string name = fi.fileNamePure();
00177                 //Handle_Standard_Transient ent = tr->EntityFromShapeResult(aShell, 3);
00178                 //if (!ent.IsNull()) {
00179                 //    name += ws->Model()->StringLabel(ent)->ToCString();
00180                 //}
00181 
00182                 Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject("Part::Feature", name.c_str()));
00183                 pcFeature->Shape.setValue(aShell);
00184             }
00185 
00186             // put all other free-flying shapes into a single compound
00187             Standard_Boolean emptyComp = Standard_True;
00188             BRep_Builder builder;
00189             TopoDS_Compound comp;
00190             builder.MakeCompound(comp);
00191 
00192             for (ex.Init(aShape, TopAbs_FACE, TopAbs_SHELL); ex.More(); ex.Next()) {
00193                 if (!ex.Current().IsNull()) {
00194                     builder.Add(comp, ex.Current());
00195                     emptyComp = Standard_False;
00196                 }
00197             }
00198             for (ex.Init(aShape, TopAbs_WIRE, TopAbs_FACE); ex.More(); ex.Next()) {
00199                 if (!ex.Current().IsNull()) {
00200                     builder.Add(comp, ex.Current());
00201                     emptyComp = Standard_False;
00202                 }
00203             }
00204             for (ex.Init(aShape, TopAbs_EDGE, TopAbs_WIRE); ex.More(); ex.Next()) {
00205                 if (!ex.Current().IsNull()) {
00206                     builder.Add(comp, ex.Current());
00207                     emptyComp = Standard_False;
00208                 }
00209             }
00210             for (ex.Init(aShape, TopAbs_VERTEX, TopAbs_EDGE); ex.More(); ex.Next()) {
00211                 if (!ex.Current().IsNull()) {
00212                     builder.Add(comp, ex.Current());
00213                     emptyComp = Standard_False;
00214                 }
00215             }
00216 
00217             if (!emptyComp) {
00218                 std::string name = fi.fileNamePure();
00219                 Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject
00220                     ("Part::Feature", name.c_str()));
00221                 pcFeature->Shape.setValue(comp);
00222             }
00223         }
00224     }
00225 
00226     return 0;
00227 }
00228 
00229 static void findStyledSR (const Handle(StepVisual_StyledItem) &style,
00230                           Handle(StepShape_ShapeRepresentation)& aSR)
00231 {
00232     // search Shape Represenatation for component styled item
00233     for (Standard_Integer j=1; j <= style->NbStyles(); j++) {
00234         Handle(StepVisual_PresentationStyleByContext) PSA = 
00235             Handle(StepVisual_PresentationStyleByContext)::DownCast(style->StylesValue ( j ));
00236         if (PSA.IsNull())
00237             continue;
00238         StepVisual_StyleContextSelect aStyleCntxSlct = PSA->StyleContext();
00239         Handle(StepShape_ShapeRepresentation) aCurrentSR = 
00240             Handle(StepShape_ShapeRepresentation)::DownCast(aStyleCntxSlct.Representation());
00241         if (aCurrentSR.IsNull())
00242             continue;
00243         aSR = aCurrentSR;
00244             break;
00245     }
00246 }
00247 
00248 bool Part::ReadColors (const Handle(XSControl_WorkSession) &WS, std::map<int, Quantity_Color>& hash_col)
00249 {
00250     STEPConstruct_Styles Styles (WS);
00251     if (!Styles.LoadStyles()) {
00252 #ifdef FC_DEBUG
00253         std::cout << "Warning: no styles are found in the model" << std::endl;
00254 #endif
00255         return Standard_False;
00256     }
00257     // searching for invisible items in the model
00258     Handle(TColStd_HSequenceOfTransient) aHSeqOfInvisStyle = new TColStd_HSequenceOfTransient;
00259     Styles.LoadInvisStyles( aHSeqOfInvisStyle );
00260 
00261     // parse and search for color attributes
00262     Standard_Integer nb = Styles.NbStyles();
00263     for (Standard_Integer i=1; i <= nb; i++) {
00264         Handle_StepVisual_StyledItem style = Styles.Style (i);
00265         if (style.IsNull()) continue;
00266 
00267         Standard_Boolean IsVisible = Standard_True;
00268         // check the visibility of styled item.
00269         for (Standard_Integer si = 1; si <= aHSeqOfInvisStyle->Length(); si++) {
00270             if (style != aHSeqOfInvisStyle->Value(si))
00271                 continue;
00272             // found that current style is invisible.
00273 #ifdef FC_DEBUG
00274             std::cout << "Warning: item No " << i << "(" << style->Item()->DynamicType()->Name() << ") is invisible" << std::endl;
00275 #endif
00276             IsVisible = Standard_False;
00277             break;
00278         }
00279 
00280         Handle(StepVisual_Colour) SurfCol, BoundCol, CurveCol;
00281         // check if it is component style
00282         Standard_Boolean IsComponent = Standard_False;
00283         if (!Styles.GetColors (style, SurfCol, BoundCol, CurveCol, IsComponent) && IsVisible)
00284             continue;
00285 
00286         // find shape
00287         TopoDS_Shape S = STEPConstruct::FindShape(Styles.TransientProcess(), style->Item());
00288         //TopAbs_ShapeEnum type = S.ShapeType();
00289         Standard_Boolean isSkipSHUOstyle = Standard_False;
00290         // take shape with real location.
00291         while (IsComponent) {
00292             // take SR of NAUO
00293             Handle_StepShape_ShapeRepresentation aSR;
00294             findStyledSR(style, aSR);
00295             // search for SR along model
00296             if (aSR.IsNull())
00297                 break;
00298 //          Handle(Interface_InterfaceModel) Model = WS->Model();
00299             Handle_XSControl_TransferReader TR = WS->TransferReader();
00300             Handle_Transfer_TransientProcess TP = TR->TransientProcess();
00301             Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings( aSR );
00302             Handle_StepShape_ShapeDefinitionRepresentation aSDR;
00303             for (subs.Start(); subs.More(); subs.Next()) {
00304                 aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
00305                 if (aSDR.IsNull())
00306                     continue;
00307                 StepRepr_RepresentedDefinition aPDSselect = aSDR->Definition();
00308                 Handle_StepRepr_ProductDefinitionShape PDS = 
00309                     Handle_StepRepr_ProductDefinitionShape::DownCast(aPDSselect.PropertyDefinition());
00310                 if (PDS.IsNull())
00311                     continue;
00312                 StepRepr_CharacterizedDefinition aCharDef = PDS->Definition();
00313         
00314                 Handle(StepRepr_AssemblyComponentUsage) ACU = 
00315                     Handle(StepRepr_AssemblyComponentUsage)::DownCast(aCharDef.ProductDefinitionRelationship());
00316                 // PTV 10.02.2003 skip styled item that refer to SHUO
00317                 if (ACU->IsKind(STANDARD_TYPE(StepRepr_SpecifiedHigherUsageOccurrence))) {
00318                     isSkipSHUOstyle = Standard_True;
00319                     break;
00320                 }
00321                 Handle_StepRepr_NextAssemblyUsageOccurrence NAUO =
00322                     Handle_StepRepr_NextAssemblyUsageOccurrence::DownCast(ACU);
00323                 if (NAUO.IsNull())
00324                     continue;
00325         
00326                 TopoDS_Shape aSh;
00327                 // PTV 10.02.2003 to find component of assembly CORRECTLY
00328                 STEPConstruct_Tool Tool( WS );
00329 //              Handle(Transfer_Binder) binder = TP->Find(NAUO);
00330 //              if (binder.IsNull() || ! binder->HasResult())
00331 //                  continue;
00332 //              aSh = TransferBRep::ShapeResult ( TP, binder );
00333                 if (!aSh.IsNull()) {
00334                     S = aSh;
00335                     break;
00336                 }
00337             }
00338             break;
00339         }
00340         if (isSkipSHUOstyle)
00341             continue; // skip styled item which refer to SHUO
00342     
00343         if ( S.IsNull() ) {
00344 #ifdef FC_DEBUG
00345             std::cout << "Warning: item No " << i << "(" << style->Item()->DynamicType()->Name() << ") is not mapped to shape" << std::endl;
00346 #endif
00347             continue;
00348         }
00349     
00350         if (!SurfCol.IsNull()) {
00351             Quantity_Color col;
00352             Styles.DecodeColor (SurfCol, col);
00353             //Base::Console().Message("%d: (%.2f,%.2f,%.2f)\n",col.Name(),col.Red(),col.Green(),col.Blue());
00354             hash_col[S.HashCode(INT_MAX)] = col;
00355         }
00356         if (!BoundCol.IsNull()) {
00357             Quantity_Color col;
00358             Styles.DecodeColor (BoundCol, col);
00359             //Base::Console().Message("%d: (%.2f,%.2f,%.2f)\n",col.Name(),col.Red(),col.Green(),col.Blue());
00360             hash_col[S.HashCode(INT_MAX)] = col;
00361         }
00362         if (!CurveCol.IsNull()) {
00363             Quantity_Color col;
00364             Styles.DecodeColor (CurveCol, col);
00365             //Base::Console().Message("%d: (%.2f,%.2f,%.2f)\n",col.Name(),col.Red(),col.Green(),col.Blue());
00366             hash_col[S.HashCode(INT_MAX)] = col;
00367         }
00368         if (!IsVisible) {
00369             // sets the invisibility for shape.
00370         }
00371     }
00372   
00373     return Standard_True;
00374 }
00375 
00376 bool Part::ReadNames (const Handle(XSControl_WorkSession) &WS)
00377 {
00378     // get starting data
00379     Handle(Interface_InterfaceModel) Model = WS->Model();
00380     Handle(XSControl_TransferReader) TR = WS->TransferReader();
00381     Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
00382 
00383     STEPConstruct_Tool Tool ( WS );
00384 
00385     // iterate on model to find all SDRs and CDSRs
00386     Standard_Integer nb = Model->NbEntities();
00387     Handle(Standard_Type) tNAUO = STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence);
00388     Handle(Standard_Type) tPD  = STANDARD_TYPE(StepBasic_ProductDefinition);
00389     Handle(TCollection_HAsciiString) name;
00390     for (Standard_Integer i = 1; i <= nb; i++) {
00391         Handle(Standard_Transient) enti = Model->Value(i);
00392 
00393         // get description of NAUO
00394         if (enti->DynamicType() == tNAUO) {
00395             Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO = 
00396                 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(enti);
00397             if (NAUO.IsNull()) continue;
00398             Interface_EntityIterator subs = WS->Graph().Sharings(NAUO);
00399             for (subs.Start(); subs.More(); subs.Next()) {
00400                 Handle(StepRepr_ProductDefinitionShape) PDS = 
00401                     Handle(StepRepr_ProductDefinitionShape)::DownCast(subs.Value());
00402                 if (PDS.IsNull()) continue;
00403                 Handle(StepBasic_ProductDefinitionRelationship) PDR = PDS->Definition().ProductDefinitionRelationship();
00404                 if (PDR.IsNull()) continue;
00405                 if (PDR->HasDescription() && 
00406                     PDR->Description()->Length() >0 ) name = PDR->Description();
00407                 else if (PDR->Name()->Length() >0) name = PDR->Name();
00408                 else name = PDR->Id();
00409             }
00410 
00411             // find proper label
00412             TCollection_ExtendedString str ( name->String() );
00413             Base::Console().Message("Name: %s\n",name->String().ToCString());
00414         }
00415 
00416         // for PD get name of associated product
00417         if (enti->DynamicType() == tPD) {
00418             Handle(StepBasic_ProductDefinition) PD = 
00419                 Handle(StepBasic_ProductDefinition)::DownCast(enti);
00420             if (PD.IsNull()) continue;
00421             Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
00422             if (Prod->Name()->UsefullLength()>0) name = Prod->Name();
00423             else name = Prod->Id();
00424 
00425             TCollection_ExtendedString str ( name->String() );
00426             Base::Console().Message("Name: %s\n",name->String().ToCString());
00427         }
00428         // set a name to the document
00429         //TCollection_ExtendedString str ( name->String() );
00430         //TDataStd_Name::Set ( L, str );
00431     }
00432 
00433     return Standard_True;
00434 }

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