1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkSTLWriter.cxx,v $
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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 "vtkSTLWriter.h"
#include "vtkByteSwap.h"
#include "vtkCellArray.h"
#include "vtkErrorCode.h"
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkTriangle.h"
#if !defined(_WIN32) || defined(__CYGWIN__)
# include <unistd.h> /* unlink */
#else
# include <io.h> /* unlink */
#endif
vtkCxxRevisionMacro(vtkSTLWriter, "$Revision: 1.57 $");
vtkStandardNewMacro(vtkSTLWriter);
vtkSTLWriter::vtkSTLWriter()
{
this->FileType = VTK_ASCII;
}
void vtkSTLWriter::WriteData()
{
vtkPoints *pts;
vtkCellArray *polys;
vtkPolyData *input = this->GetInput();
polys = input->GetPolys();
pts = input->GetPoints();
if (pts == NULL || polys == NULL )
{
vtkErrorMacro(<<"No data to write!");
return;
}
if ( this->FileName == NULL)
{
vtkErrorMacro(<< "Please specify FileName to write");
this->SetErrorCode(vtkErrorCode::NoFileNameError);
return;
}
if ( this->FileType == VTK_BINARY )
{
this->WriteBinarySTL(pts,polys);
if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)
{
vtkErrorMacro("Ran out of disk space; deleting file: "
<< this->FileName);
unlink(this->FileName);
}
}
else
{
this->WriteAsciiSTL(pts,polys);
if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)
{
vtkErrorMacro("Ran out of disk space; deleting file: "
<< this->FileName);
unlink(this->FileName);
}
}
}
static char header[]="Visualization Toolkit generated SLA File ";
void vtkSTLWriter::WriteAsciiSTL(vtkPoints *pts, vtkCellArray *polys)
{
FILE *fp;
double n[3], v1[3], v2[3], v3[3];
vtkIdType npts = 0;
vtkIdType *indx = 0;
if ((fp = fopen(this->FileName, "w")) == NULL)
{
vtkErrorMacro(<< "Couldn't open file: " << this->FileName);
this->SetErrorCode(vtkErrorCode::CannotOpenFileError);
return;
}
//
// Write header
//
vtkDebugMacro("Writing ASCII sla file");
if (fprintf (fp, "solid ascii\n") < 0)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
//
// Write out triangle polygons. In not a triangle polygon, only first
// three vertices are written.
//
for (polys->InitTraversal(); polys->GetNextCell(npts,indx); )
{
pts->GetPoint(indx[0],v1);
pts->GetPoint(indx[1],v2);
pts->GetPoint(indx[2],v3);
vtkTriangle::ComputeNormal(pts, npts, indx, n);
if (fprintf (fp, " facet normal %.6g %.6g %.6g\n outer loop\n",
n[0], n[1], n[2]) < 0)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
if (fprintf (fp, " vertex %.6g %.6g %.6g\n", v1[0], v1[1], v1[2]) < 0)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
if (fprintf (fp, " vertex %.6g %.6g %.6g\n", v2[0], v2[1], v2[2]) < 0)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
if (fprintf (fp, " vertex %.6g %.6g %.6g\n", v3[0], v3[1], v3[2]) < 0)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
if (fprintf (fp, " endloop\n endfacet\n") < 0)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
}
if (fprintf (fp, "endsolid\n") < 0)
{
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
}
fclose (fp);
}
void vtkSTLWriter::WriteBinarySTL(vtkPoints *pts, vtkCellArray *polys)
{
FILE *fp;
double dn[3], v1[3], v2[3], v3[3];
vtkIdType npts = 0;
vtkIdType *indx = 0;
unsigned long ulint;
unsigned short ibuff2=0;
if ((fp = fopen(this->FileName, "wb")) == NULL)
{
vtkErrorMacro(<< "Couldn't open file: " << this->FileName);
this->SetErrorCode(vtkErrorCode::CannotOpenFileError);
return;
}
// Write header
//
vtkDebugMacro("Writing Binary STL file");
if (fwrite (header, 1, 80, fp) < 80)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
ulint = (unsigned long int) polys->GetNumberOfCells();
vtkByteSwap::Swap4LE(&ulint);
if (fwrite (&ulint, 1, 4, fp) < 4)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
// Write out triangle polygons. In not a triangle polygon, only first
// three vertices are written.
//
for (polys->InitTraversal(); polys->GetNextCell(npts,indx); )
{
pts->GetPoint(indx[0],v1);
pts->GetPoint(indx[1],v2);
pts->GetPoint(indx[2],v3);
vtkTriangle::ComputeNormal(pts, npts, indx, dn);
float n[3];
n[0] = (float)dn[0];
n[1] = (float)dn[1];
n[2] = (float)dn[2];
vtkByteSwap::Swap4LE(n);
vtkByteSwap::Swap4LE(n+1);
vtkByteSwap::Swap4LE(n+2);
if (fwrite (n, 4, 3, fp) < 3)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
n[0] = (float)v1[0]; n[1] = (float)v1[1]; n[2] = (float)v1[2];
vtkByteSwap::Swap4LE(n);
vtkByteSwap::Swap4LE(n+1);
vtkByteSwap::Swap4LE(n+2);
if (fwrite (n, 4, 3, fp) < 3)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
n[0] = (float)v2[0]; n[1] = (float)v2[1]; n[2] = (float)v2[2];
vtkByteSwap::Swap4LE(n);
vtkByteSwap::Swap4LE(n+1);
vtkByteSwap::Swap4LE(n+2);
if (fwrite (n, 4, 3, fp) < 3)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
n[0] = (float)v3[0]; n[1] = (float)v3[1]; n[2] = (float)v3[2];
vtkByteSwap::Swap4LE(n);
vtkByteSwap::Swap4LE(n+1);
vtkByteSwap::Swap4LE(n+2);
if (fwrite (n, 4, 3, fp) < 3)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
if (fwrite (&ibuff2, 2, 1, fp) < 1)
{
fclose(fp);
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return;
}
}
fclose (fp);
}
//----------------------------------------------------------------------------
void vtkSTLWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|