ImportOperator.cxx 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
//=========================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//=========================================================================

#include "ImportOperator.h"

13
#include "smtk/bridge/discrete/Session.h"
14 15 16

#include "smtk/attribute/Attribute.h"
#include "smtk/attribute/FileItem.h"
17
#include "smtk/attribute/IntItem.h"
18 19 20 21
#include "smtk/attribute/StringItem.h"
#include "smtk/attribute/ModelEntityItem.h"

#include "smtk/model/Manager.h"
22 23 24
#include "smtk/model/Model.h"
#include "smtk/model/Operator.h"
#include "smtk/model/SessionRef.h"
25 26 27

#include "vtkDiscreteModelWrapper.h"
#include "vtkModelItem.h"
28
#include "vtkModel.h"
29 30 31
#include "vtkPDataSetReader.h"
#include "vtkMasterPolyDataNormals.h"
#include "vtkMergeDuplicateCells.h"
32 33
#include "smtk/bridge/discrete/extension/reader/vtkDataSetRegionSurfaceFilter.h"
#include "smtk/bridge/discrete/extension/reader/vtkCMBGeometryReader.h"
34

35
#ifdef SMTK_ENABLE_REMUS_SUPPORT
36
  #include "smtk/bridge/discrete/extension/reader/vtkCMBGeometry2DReader.h"
37 38 39
  #include "smtk/bridge/discrete/extension/reader/vtkCMBMapReader.h"
  #include "smtk/bridge/discrete/extension/meshing/vtkCMBTriangleMesher.h"
#endif
40

41
#ifdef SMTK_ENABLE_MOAB_DISCRETE_READER
42 43 44
#include "smtk/bridge/discrete/moabreader/vtkCmbMoabReader.h"
#endif

45
#include <vtksys/SystemTools.hxx>
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include "ModelParserHelper.h"
#include "ImportOperator_xml.h"

#include "smtk/io/ExportJSON.h"
#include "cJSON.h"

using namespace smtk::model;

namespace smtk {
  namespace bridge {

  namespace discrete {

ImportOperator::ImportOperator()
{
}

bool ImportOperator::ableToOperate()
{
65
  if(!this->specification()->isValid())
66
    return false;
67

68 69 70 71
  std::string filename = this->specification()->findFile("filename")->value();
  if (filename.empty())
    return false;
  std::string ext = vtksys::SystemTools::GetFilenameLastExtension(filename);
72
  std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
73 74
  bool able = (ext == ".vtk" || ext == ".2dm" ||
               ext == ".3dm" ||
75
#ifdef SMTK_ENABLE_MOAB_DISCRETE_READER
76 77 78
               ext == ".h5m" || ext == ".sat" ||
               ext == ".brep" || ext == ".stp" ||
               ext == ".cub" || ext == ".exo" ||
79
#endif
80
#ifdef SMTK_ENABLE_REMUS_SUPPORT
81 82
               ext == ".poly" || ext == ".smesh" || ext == ".map" ||
#endif
83 84
  /*  ext == ".tin" ||
      ext == ".fac" ||
85 86 87 88
      ext == ".obj" ||
      ext == ".sol" ||*/
      ext == ".stl");

89 90
// for shape files, the reader needs user inputs, so
// "ShapeBoundaryStyle" item needs to be checked first.
91
#ifdef SMTK_ENABLE_REMUS_SUPPORT
92 93 94 95 96 97 98 99
  if(ext == ".shp")
    {
    smtk::attribute::StringItem::Ptr boundaryItem =
      this->specification()->findString("ShapeBoundaryStyle");
    able = boundaryItem->isEnabled();
    }
#endif

100
  return able;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

OperatorResult ImportOperator::operateInternal()
{
/*
  smtk::attribute::FileItem::Ptr filenameItem =
    this->specification()->findFile("filename");
  smtk::attribute::StringItem::Ptr filetypeItem =
    this->specification()->findString("filetype");

  std::string filename = filenameItem ?
    filenameItem->value() : "";
//  std::string filetype = filetypeItem ?
//    filetypeItem->value() : "";
*/
  std::string filename = this->specification()->findFile("filename")->value();

/*
  cJSON* json = cJSON_CreateObject();
  smtk::io::ExportJSON::forOperator(this->specification(), json);
  std::cout << "Import Operator: " << cJSON_Print(json) << "\n";
  cJSON_Delete(json);

*/
  if (filename.empty())
    {
    std::cerr << "File name is empty!\n";
    return this->createResult(OPERATION_FAILED);
    }

  // Create a new model to hold the result.
  vtkNew<vtkDiscreteModelWrapper> mod;

// ******************************************************************************
// This is where we should have the logic to import files other than .cmb formats
// ******************************************************************************
  std::string ext = vtksys::SystemTools::GetFilenameLastExtension(filename);
138 139 140
  if(ext == ".h5m" || ext == ".sat" ||
     ext == ".brep" || ext == ".stp" ||
     ext == ".cub" || ext == ".exo" )
141
    {
142
#ifdef SMTK_ENABLE_MOAB_DISCRETE_READER
143 144 145 146 147 148 149 150

    vtkNew<vtkCmbMoabReader> reader;
    reader->SetFileName(filename.c_str());
    reader->Update();

    this->m_op->Operate(mod.GetPointer(), reader.GetPointer());
#endif
    }
151 152
  else if (ext == ".2dm" ||
      ext == ".3dm" ||
153
#ifdef SMTK_ENABLE_REMUS_SUPPORT
154 155 156
      ext == ".poly" || ext == ".smesh" ||
#endif
  /*  ext == ".tin" ||
157
      ext == ".fac" ||
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
      ext == ".obj" ||
      ext == ".sol" || */
      ext == ".stl")
    {
    vtkNew<vtkCMBGeometryReader> reader;
    reader->SetFileName(filename.c_str());
    reader->SetPrepNonClosedSurfaceForModelCreation(true);
    reader->Update();

    bool hasBoundaryEdges = reader->GetHasBoundaryEdges();

    if(ext == ".poly" || ext == ".smesh" || hasBoundaryEdges)
      {
      this->m_op->Operate(mod.GetPointer(), reader.GetPointer());
      }
    else
      {
      vtkNew<vtkMasterPolyDataNormals> normals;
      normals->SetInputData(0, reader->GetOutputDataObject(0));
      normals->Update();

      vtkNew<vtkMergeDuplicateCells> merge;
      merge->SetModelRegionArrayName(ModelParserHelper::GetShellTagName());
      merge->SetModelFaceArrayName(ModelParserHelper::GetModelFaceTagName());
      merge->SetInputData(0, normals->GetOutputDataObject(0));
      merge->Update();

      this->m_op->Operate(mod.GetPointer(), merge.GetPointer());
      }
    }
188
#ifdef SMTK_ENABLE_REMUS_SUPPORT
189 190 191 192 193 194 195 196 197 198 199 200 201
  else if(ext == ".map")
    {
    vtkNew<vtkCMBMapReader> reader;
    reader->SetFileName(filename.c_str());
    reader->Update();

    vtkNew<vtkCMBTriangleMesher> trimesher;
    trimesher->SetPreserveEdgesAndNodes(true);
    trimesher->SetInputData(0, reader->GetOutputDataObject(0));
    trimesher->Update();

    this->m_mapOp->Operate(mod.GetPointer(), trimesher.GetPointer());
    }
202 203
  else if(ext == ".shp")
    {
204 205 206
    smtk::attribute::StringItem::Ptr boundaryItem =
      this->specification()->findString("ShapeBoundaryStyle");
    if(boundaryItem->isEnabled())
207
      {
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      vtkNew<vtkCMBGeometry2DReader> reader;
      reader->SetFileName(filename.c_str());
      std::string boundaryStyle = boundaryItem->value();
      if (boundaryStyle == "None") // default
        {
        reader->SetBoundaryStyle(vtkCMBGeometry2DReader::NONE);
        }
      else if (boundaryStyle == "Relative Margin")
        {
        reader->SetBoundaryStyle(vtkCMBGeometry2DReader::RELATIVE_MARGIN);
        smtk::attribute::StringItem::Ptr relMarginItem =
          this->specification()->findString("relative margin");
        reader->SetRelativeMarginString(relMarginItem->value().c_str());
        }
      else if (boundaryStyle == "Absolute Margin")
223
        {
224 225 226 227
        reader->SetBoundaryStyle(vtkCMBGeometry2DReader::ABSOLUTE_MARGIN);
        smtk::attribute::StringItem::Ptr absMarginItem =
          this->specification()->findString("absolute margin");
        reader->SetAbsoluteMarginString(absMarginItem->value().c_str());
228
        }
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
      else if (boundaryStyle == "Bounding Box")
        {
        reader->SetBoundaryStyle(vtkCMBGeometry2DReader::ABSOLUTE_BOUNDS);
        smtk::attribute::StringItem::Ptr absBoundsItem =
          this->specification()->findString("absolute bounds");
        reader->SetAbsoluteBoundsString(absBoundsItem->value().c_str());
        }
      else if (boundaryStyle == "Bounding File")
        {
        reader->SetBoundaryStyle(vtkCMBGeometry2DReader::IMPORTED_POLYGON);
        smtk::attribute::StringItem::Ptr boundsFileItem =
          this->specification()->findString("imported polygon");
        reader->SetBoundaryFile(boundsFileItem->value().c_str());
        }
      else
        {
        std::cerr << "Invalid Shape file boundary. No boundary will be set.\n";
        reader->SetBoundaryStyle(vtkCMBGeometry2DReader::NONE);
        }
      reader->Update();
      this->m_shpOp->Operate(mod.GetPointer(), reader.GetPointer(),
                             /*cleanVerts:*/ 0);
251
      }
252 253
    else
      std::cerr << "Shape file boundary has to be set.\n";
254
    }
255
#endif
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  else if(ext == ".vtk")
    {
    vtkNew<vtkPDataSetReader> reader;
    reader->SetFileName(filename.c_str());
    reader->Update();

    vtkNew<vtkDataSetRegionSurfaceFilter> surface;
    surface->SetRegionArrayName(ModelParserHelper::GetShellTagName());
    surface->SetInputData(0, reader->GetOutputDataObject(0));
    surface->Update();

    vtkNew<vtkMasterPolyDataNormals> normals;
    normals->SetInputData(0, surface->GetOutputDataObject(0));
    normals->Update();

    vtkNew<vtkMergeDuplicateCells> merge;
    merge->SetModelRegionArrayName(ModelParserHelper::GetShellTagName());
    merge->SetModelFaceArrayName(ModelParserHelper::GetModelFaceTagName());
    merge->SetInputData(0, normals->GetOutputDataObject(0));
    merge->Update();

    this->m_op->Operate(mod.GetPointer(), merge.GetPointer());
    }

  // Now assign a UUID to the model and associate its filename with
  // a URL property (if things went OK).
282
  if (!this->m_op->GetOperateSucceeded()
283
#ifdef SMTK_ENABLE_REMUS_SUPPORT
284 285 286 287
   && !this->m_mapOp->GetOperateSucceeded()
   && !this->m_shpOp->GetOperateSucceeded()
#endif
   )
288 289 290 291 292
    {
    std::cerr << "Failed to import file \"" << filename << "\".\n";
    return this->createResult(OPERATION_FAILED);
    }

293
  smtk::common::UUID modelId = this->discreteSession()->trackModel(
294
    mod.GetPointer(), filename, this->manager());
295
  smtk::model::EntityRef modelEntity(this->manager(), modelId);
296 297

  OperatorResult result = this->createResult(OPERATION_SUCCEEDED);
298
  this->addEntityToResult(result, modelEntity, CREATED);
299

300 301 302 303 304
/*
//#include "smtk/io/ExportJSON.h"
//#include "cJSON.h"

  cJSON* json = cJSON_CreateObject();
305
  smtk::io::ExportJSON::fromModelManager(json, this->manager());
306 307 308 309
  std::cout << "Result " << cJSON_Print(json) << "\n";
  cJSON_Delete(json);
  */
/*
310
std::string json = smtk::io::ExportJSON::fromModelManager(this->manager());
Yumin Yuan's avatar
Yumin Yuan committed
311
    std::ofstream file("/tmp/import_op_out.json");
312 313 314
    file << json;
    file.close();
*/
315

316 317 318 319
  modelEntity.as<smtk::model::Model>().setSession(
    smtk::model::SessionRef(
      modelEntity.manager(),
      this->session()->sessionId()));
320

321 322 323
  return result;
}

324
Session* ImportOperator::discreteSession() const
325
{
326
  return dynamic_cast<Session*>(this->session());
327 328 329 330 331 332 333 334
}

    } // namespace discrete
  } // namespace bridge

} // namespace smtk

smtkImplementsModelOperator(
335
  SMTKDISCRETESESSION_EXPORT,
336 337 338 339
  smtk::bridge::discrete::ImportOperator,
  discrete_import,
  "import",
  ImportOperator_xml,
340
  smtk::bridge::discrete::Session);