[vtkusers] multiple copies of models...why?

J. Scott Pendergrass jsp at xontech.com
Tue Jul 17 17:35:03 EDT 2001


VTK Gurus:

I'm trying to write an application to view CAD models. The application 
works fine for the first model that I load in, but when I try to load other 
models, the older ones do not go away.

I've attached a code snippet of the function that reads the data from a 
file and builds the VTK stuff. Each component of the CAD model has its own 
vtkPolyDataCollection and vtkActorCollection.

Any suggestions?

Thanks,
Scott
-------------- next part --------------


vtkActor *actorFacet;
vtkActor *actorAxes;
vtkActor *actorOutline;
vtkFollower *actorXLabel, *actorYLabel, *actorZLabel;
vtkActorCollection *actorCollectFacet;


//
// read the data from the input file and build actors
//
void ReadData(Widget textWindow)
{
  int i, j, n, numNodes, partCount;
  int numBigParts, numSmallParts, numFacets, numSidesFacet, iCoat;
  int nodes[3];
  char line[MAX_CHARS_PER_LINE];
  float x, y, z;

  static int initialized = 0;

  // points and connectivity for facets
  vtkActor *actorFoo, *actorTemp;
  vtkCellArray *caFacet;
  vtkPoints *fpFacet;
  vtkPolyData *pdFacet;
  vtkPolyDataMapper *polyMapAxes = vtkPolyDataMapper::New();
  vtkPolyDataMapper *polyMapFacet;
  vtkPolyDataMapper *polyMapOutline = vtkPolyDataMapper::New();
  vtkPolyDataCollection *pdc = vtkPolyDataCollection::New();
  vtkOutlineFilter *outline = vtkOutlineFilter::New();  // bounding box
  vtkAxes *axes = vtkAxes::New();  // (x,y,z) axes;
  
  if (!initialized)
  {
    initialized = TRUE;
  }
  else
  {
    // clean up earlier stuff
    actorCollectFacet->InitTraversal();
    //actorFoo = vtkActor::New();
    while ((actorFoo = actorCollectFacet->GetNextActor()) != NULL)
    {
      ren1->RemoveActor(actorFoo);
      cout << "removing actor" << endl;
    }

    pdc->RemoveAllItems(); // doesn't seem to do anything

    vtkActorCollection *acFoo;
    acFoo = ren1->GetActors();
    acFoo->InitTraversal();
    while ((actorFoo = acFoo->GetNextActor()) != NULL)
    {
      ren1->RemoveActor(actorFoo);
      cout << "removing ren1 actor" << endl;
    }
  }

  //-------------------------
  // begin reading facet file
  //-------------------------

  numFacetsTotal = 0;
  
  facetFile.getline(line,MAX_CHARS_PER_LINE);  // read header
  cout << line << endl;
  UtlDispMsg(line,textWindow);
  UtlDispMsg("\n",textWindow);

  // read number of big parts
  facetFile.getline(line,MAX_CHARS_PER_LINE);
  sscanf(line,"%d",&numBigParts);
  cout << "number of big parts: " << numBigParts << endl;
  UtlDispMsg(line,textWindow);
  UtlDispMsg("\n",textWindow);

  partCount = 0;

  // loop over number of big parts
  for (n = 0; n < numBigParts; n++)
  {
    fpFacet = vtkPoints::New();
    
    facetFile.getline(line,MAX_CHARS_PER_LINE);  // read the name of this big part
    cout << "name of big part: " << line << endl;;
    UtlDispMsg(line,textWindow);
    UtlDispMsg("\n",textWindow);
    
    facetFile.getline(line,MAX_CHARS_PER_LINE);

    // read the number of nodes in this big part
    facetFile.getline(line,MAX_CHARS_PER_LINE);  
    sscanf(line,"%d",&numNodes);
    cout << "number of nodes: " << numNodes << endl;
    
    // read the (x,y,z) coordinates of each nodes
    for (i = 0; i < numNodes; i++)
    {
      facetFile.getline(line,MAX_CHARS_PER_LINE);
      sscanf(line,"%f %f %f", &x, &y, &z);
      fpFacet->InsertNextPoint(x,y,z);
    }

    pdFacet = vtkPolyData::New();
    pdFacet->SetPoints(fpFacet);
    
    // read the number of small parts in this big part
    facetFile.getline(line,MAX_CHARS_PER_LINE);
    sscanf(line,"%d",&numSmallParts);
    cout << "number of small parts: " << numSmallParts << endl;

    // loop over the number small parts
    for (i = 0; i < numSmallParts; i++)
    {
      caFacet = vtkCellArray::New();

      facetFile.getline(line,MAX_CHARS_PER_LINE);  // read the name of this small part
      cout << "small part name: " << line << endl;
      UtlDispMsg(line,textWindow);
      UtlDispMsg("\n",textWindow);

      // read the number of facets and the number of sides per facet
      facetFile.getline(line,MAX_CHARS_PER_LINE);
      sscanf(line,"%d %d", &numFacets, &numSidesFacet);

      // loop over the number of facets
      for (j = 0; j < numFacets; j++)
      {
	facetFile.getline(line,MAX_CHARS_PER_LINE);
        // read the node numbers for each facet
	sscanf(line,"%d %d %d %d", &nodes[0], &nodes[1], &nodes[2], &iCoat);
        for (int k = 0; k < 3; k++) nodes[k]--;
	caFacet->InsertNextCell(3,nodes);
	numFacetsTotal++;
      }

      pdFacet->SetPolys(caFacet);
      caFacet->Delete();

      pdc->AddItem(pdFacet);
      pdFacet->Delete();

      partCount++;
    }
  }

  //------------------------
  // end reading facet file
  //------------------------


  pdc->InitTraversal();

  for (i = 0; i < partCount; i++)
  {
    polyMapFacet = vtkPolyDataMapper::New();
    polyMapFacet->SetInput(pdc->GetNextItem());
    //polyMapFacet->ImmediateModeRenderingOff();
    actorTemp = vtkActor::New();
    actorTemp->SetMapper(polyMapFacet);
    actorCollectFacet->AddItem(actorTemp);
  }

  actorCollectFacet->InitTraversal();
  while ((actorFoo = actorCollectFacet->GetNextActor()) != NULL)
  {
    ren1->AddActor(actorFoo);
  }

  pdc->InitTraversal();

  float maxDim;

  for (int np = 0; np < partCount; np++)
  {
    bounds = pdc->GetNextItem()->GetBounds();
    maxDim = 0.0;
    for (i = 0; i < 6; i++)
    {
      if (bounds[i] > maxDim)
      {
	maxDim = bounds[i];
      }
    }
  }

  float scaleFactor = 2.0*maxDim;

  // create axes
  axes->SetOrigin(0,0,0);
  axes->SetScaleFactor(scaleFactor);
  polyMapAxes->SetInput(axes->GetOutput());
  actorAxes->SetMapper(polyMapAxes);
  actorAxes->PickableOff();

  // labels for axes

  float lSize = 0.1*maxDim;

  // x axis
  vtkVectorText *xText = vtkVectorText::New();
  xText->SetText("X");
  vtkPolyDataMapper *xTextMapper = vtkPolyDataMapper::New();
  xTextMapper->SetInput(xText->GetOutput());
  actorXLabel->SetMapper(xTextMapper);
  actorXLabel->SetScale(lSize,lSize,lSize);
  actorXLabel->SetPosition(scaleFactor,0.0,0.0);
  actorXLabel->GetProperty()->SetColor(0,0,0);

  // y axis
  vtkVectorText *yText = vtkVectorText::New();
  yText->SetText("Y");
  vtkPolyDataMapper *yTextMapper = vtkPolyDataMapper::New();
  yTextMapper->SetInput(yText->GetOutput());
  actorYLabel->SetMapper(yTextMapper);
  actorYLabel->SetScale(lSize,lSize,lSize);
  actorYLabel->SetPosition(0.0,scaleFactor,0.0);
  actorYLabel->GetProperty()->SetColor(0,0,0);

  // z axis
  vtkVectorText *zText = vtkVectorText::New();
  zText->SetText("Z");
  vtkPolyDataMapper *zTextMapper = vtkPolyDataMapper::New();
  zTextMapper->SetInput(zText->GetOutput());
  actorZLabel->SetMapper(zTextMapper);
  actorZLabel->SetScale(lSize,lSize,lSize);
  actorZLabel->SetPosition(0.0,0.0,scaleFactor);
  actorZLabel->GetProperty()->SetColor(0,0,0);

  ResetCamera();
}
-------------- next part --------------

----------
J. Scott Pendergrass
XonTech, Inc.
5030 Bradford Drive NW
Building 1, Suite 220
Huntsville, AL 35805

e-mail: jsp at xontech.com
voice : (256) 837-9123
FAX   : (256) 837-9125

----------


More information about the vtkusers mailing list