26 #include <onurbs/opennurbs.h>
36 namespace extensions {
38 void writeON_Init(ONX_Model & model)
46 model.m_properties.m_RevisionHistory.NewRevision();
49 model.m_properties.m_Application.m_application_name =
"OpenNURBS write_curves_example() function";
50 model.m_properties.m_Application.m_application_URL =
"http://www.opennurbs.org";
51 model.m_properties.m_Application.m_application_details =
"Example program in OpenNURBS toolkit.";
54 model.m_properties.m_Notes.m_notes =
"This file was made with the OpenNURBS write_curves_example() function.";
55 model.m_properties.m_Notes.m_bVisible =
true;
59 model.m_settings.m_ModelUnitsAndTolerances.m_unit_system = ON::inches;
60 model.m_settings.m_ModelUnitsAndTolerances.m_absolute_tolerance = 0.001;
61 model.m_settings.m_ModelUnitsAndTolerances.m_angle_tolerance = ON_PI/180.0;
62 model.m_settings.m_ModelUnitsAndTolerances.m_relative_tolerance = 0.01;
74 bool writeON_Write3dm(ONX_Model & model,
const std::string & fname)
76 const char* filename = fname.c_str();
82 ON_TextLog message_log;
100 FILE* fp = ON::OpenFile( filename,
"wb" );
101 ON_BinaryFile archive( ON::write3dm, fp );
103 const char* sStartSectionComment = __FILE__
"write_points_example()" __DATE__;
107 bool ok = model.Write(archive, version, sStartSectionComment, &error_log );
111 message_log.Print(
"Successfully wrote %s.\n",filename);
113 message_log.Print(
"Errors while writing %s.\n",filename);
123 bool writeON_NurbsCurve(
const gsCurve<T> & curve, ONX_Model & model,
const std::string & name)
126 ON_NurbsCurve* wiggle =
new ON_NurbsCurve(
133 for (
int k = 0; k < wiggle->CVCount(); k++ )
135 ON_3dPoint pt( cast<T,double>(curve.coef(k,0)), cast<T,double>(curve.coef(k,1)), 0.0 );
136 wiggle->SetCV( k, pt );
139 const gsKnotVector<T> & kv =
140 dynamic_cast<const gsBSplineBasis<T>&
>( curve.basis() ).knots();
143 for (
size_t k = 1; k < kv.size()-1; k++ )
145 wiggle->SetKnot(k-1, cast<T,double>(kv[k]) );
149 if ( wiggle->IsValid(&log) )
151 ONX_Model_Object& mo = model.m_object_table.AppendNew();
152 mo.m_object = wiggle;
153 mo.m_bDeleteObject =
true;
154 mo.m_attributes.m_layer_index = 0;
155 mo.m_attributes.m_name = name.c_str();
166 bool writeON_NurbsSurface(
const gsSurface<T> & surface,
167 ONX_Model & model,
const std::string & name)
169 ON_NurbsSurface* onsurf =
new ON_NurbsSurface(
172 surface.basis().degree(0)+1,
173 surface.basis().degree(1)+1,
174 surface.basis().component(0).size(),
175 surface.basis().component(1).size()
179 bool fs = (surface.geoDim()<3?
false:
true);
180 for (
int j = 0; j < onsurf->CVCount(1); j++ )
181 for (
int i = 0; i < onsurf->CVCount(0); i++ )
183 ON_3dPoint pt(cast<T,double>(surface.coef(c,0)), cast<T,double>(surface.coef(c,1)), (fs? cast<T,double>(surface.coef(c,2)) : 0.0) );
185 onsurf->SetCV( i, j, pt );
189 const gsKnotVector<T> & kv1 =
190 dynamic_cast<const gsBSplineBasis<T>&
>( surface.basis().component(0) ).knots();
191 const gsKnotVector<T> & kv2 =
192 dynamic_cast<const gsBSplineBasis<T>&
>( surface.basis().component(1) ).knots();
194 for (
size_t k = 1; k < kv1.size()-1; k++ )
195 onsurf->SetKnot(0, k-1, cast<T,double>(kv1[k]) );
197 for (
size_t k = 1; k < kv2.size()-1; k++ )
198 onsurf->SetKnot(1, k-1, cast<T,double>(kv2[k]) );
201 if ( onsurf->IsValid(&log) )
203 ONX_Model_Object& mo = model.m_object_table.AppendNew();
204 mo.m_object = onsurf;
205 mo.m_bDeleteObject =
true;
206 mo.m_attributes.m_layer_index = 0;
207 mo.m_attributes.m_name = name.c_str();
217 bool writeON_NurbsSurface(
const gsSurface<T> & srf,
const std::string & name)
221 writeON_NurbsSurface(srf, model,
"srf");
222 return writeON_Write3dm(model,name+
".3dm");
226 bool writeON_NurbsCurve(
const gsCurve<T> & curve,
const std::string & name)
230 writeON_NurbsCurve(curve, model,
"curve");
231 return writeON_Write3dm(model,name+
".3dm");
236 bool writeON_MultiPatch(
const gsMultiPatch<T> & patches,
const std::string & name)
241 for(
size_t i = 0; i < patches.nPatches(); ++i)
244 std::stringstream nm(
"patch");
247 if (
const gsCurve<T> * c =
dynamic_cast<const gsCurve<T>*
>(&patches.patch(i) ) )
249 writeON_NurbsCurve(*c, model, nm.str() );
252 if (
const gsSurface<T> * c =
dynamic_cast<const gsSurface<T>*
>(&patches.patch(i) ) )
254 writeON_NurbsSurface(*c, model, nm.str() );
258 return writeON_Write3dm(model,name+
".3dm");
263 bool writeON_PlanarDomain(
const gsPlanarDomain<T> & pd,
const std::string & name)
268 for(
index_t i =0; i<pd.numLoops();i++)
269 for(
index_t j =0; j< pd.loop(i).numCurves() ; j++)
271 const gsCurve<T> & c = pd.loop(i).curve(j);
272 gsInfo<<
"Write loop "<< i <<
", curve "<<j<<
"\n";
274 std::stringstream nm(
"curve");
277 writeON_NurbsCurve(c, model, nm.str() );
280 return writeON_Write3dm(model,name+
".3dm");
286 bool writeON_Mesh(
const gsMesh<T> & msh,
const std::string & name)
291 const size_t nf = msh.numFaces();
292 const size_t nv = msh.numVertices();
294 ON_Mesh* mesh =
new ON_Mesh(nf, nv,
true,
false);
296 for (
size_t i = 0; i <
nv; i++)
298 const gsVertex<T> & v = msh.vertex(i);
299 mesh->SetVertex(i, ON_3fPoint(cast<T,double>(v.x()), cast<T,double>(v.y()), cast<T,double>(v.z())) );
303 for (
size_t i = 0; i < nf; i++)
305 v = msh.faceIndices(i);
309 mesh->SetTriangle(i, v[0], v[1], v[2]);
312 mesh->SetQuad(i, v[0], v[1], v[2], v[3]);
319 mesh->ComputeVertexNormals();
323 if ( mesh->IsValid(&log) )
325 ONX_Model_Object& mo = model.m_object_table.AppendNew();
327 mo.m_bDeleteObject =
true;
328 mo.m_attributes.m_layer_index = 0;
329 mo.m_attributes.m_name = name.c_str();
335 return writeON_Write3dm(model,name+
".3dm");
Knot vector for B-splines.
Provides declaration of functions that write 3DM file format.
#define index_t
Definition: gsConfig.h:32
EIGEN_STRONG_INLINE onormal_expr< T > nv(const gsGeometryMap< T > &u)
The (outer pointing) boundary normal of a geometry map.
Definition: gsExpressions.h:4508
Provides declaration of BSplineBasis class.
Provides declaration of the MultiPatch class.
#define gsInfo
Definition: gsDebug.h:43
Provides declaration of the Mesh class.
Provides declaration of gsPlanarDomain class. The outer boundary (m_loops[0]) is a loop of curves...