If you have set up your Visual Studio project and activated ODA Software, you're ready to begin coding. This topic presents step-by-step instructions on how to implement the following functionality:
FirstApp.cpp
and include directives for system, ODA, and Facet Modeler header files:
//System includes
#include "StdAfx.h"
//ODA includes
#include "StaticRxObject.h"
#include "ExSystemServices.h"
#include "Ge/GeCircArc2d.h"
#include "ExPrintConsole.h"
//FacetModeler includes
#include "FMDataSerialize.h"
using namespace FacetModeler;
enum CreationMode
{
Cube,
Pyramid,
SliceBody
};
Body createCube(const DeviationParams& devDeviation, double edgeLen = 200.0);
Body createPyramid(const DeviationParams& devDeviation, double radius = 200.0, double height = 300.0, unsigned int sidesCnt = 7);
Profile3D sliceBody(const DeviationParams& devDeviation);
void CreateBodyExample(const DeviationParams&, OdStreamBuf*, CreationMode);
void propsBodyExample(const DeviationParams&, OdStreamBuf*, CreationMode);
main()
function; this version contains:
argv[]
arguments.CreateBodyExample()
method that initiates body creation (or the propsBodyExample()
method that initiates profile creation) and writing to the file.int main(int argc, char* argv[])
{
CreationMode mode;
if (argc != 3)
{
odPrintConsoleString(L"Something went wrong.\n");
return 1;
}
else
{
//Determining the creation mode
char* s = argv[1];
if (!strcmp(s, "Cube"))
{
mode = Cube;
}
else if (!strcmp(s, "Pyramid"))
{
mode = Pyramid;
}
else if (!strcmp(s, "SliceBody"))
{
mode = SliceBody;
}
else
{
odPrintConsoleString(L"Wrong creation mode parameter.\n");
return 1;
}
//Initializing ODA
OdStaticRxObject<ExSystemServices> svcs;
odrxInitialize(&svcs);
FacetModeler::DeviationParams devParams;
try
{
OdString filename(argv[2]);
OdStreamBufPtr pStream = odrxSystemServices()->createFile(filename, Oda::kFileWrite, Oda::kShareDenyNo, Oda::kCreateAlways);
if (mode == SliceBody)
{
propsBodyExample(devParams, pStream, mode);
}
else
{
CreateBodyExample(devParams, pStream, mode);
}
}
catch (OdError& e)
{
odPrintConsoleString(L"\n\nError: %ls", e.description().c_str());
}
catch (...)
{
odPrintConsoleString(L"\n\nUnexpected error.");
}
odrxUninitialize();
return 0;
}
}
CreateBodyExample()
and propsBodyExample()
functions that call the correct method depending on the CreationMode
parameter:
void CreateBodyExample(const DeviationParams& devParams, OdStreamBuf* pStream, CreationMode mode)
{
Body body;
switch (mode)
{
case Cube: body = createCube(devParams); break;
case Pyramid: body = createPyramid(devParams); break;
default: ODA_ASSERT(false);
}
BinaryStream sout;
sout.Create(pStream);
sout.Write(body);
}
void propsBodyExample(const DeviationParams& devParams, OdStreamBuf* pStream, CreationMode mode)
{
BinaryStream sout;
sout.Create(pStream);
Profile3D profile = sliceBody(devParams);
sout.Write(profile.as2d());
}
createCube
, createPyramid
, sliceBody
):
Body createCube(const DeviationParams& devDeviation, double edgeLen)
{
// Create dimensions vector
OdGeVector3d dimensions(edgeLen, edgeLen, edgeLen);
// For moving the origin point to the center of the cube base
OdGeVector3d shift(edgeLen / 2, edgeLen / 2, 0.0);
return Body::box(OdGePoint3d::kOrigin - shift, dimensions);
}
Body createPyramid(const DeviationParams& devDeviation, double radius, double height, unsigned int sidesCnt)
{
// Create a circle for polygon inscribing
OdGeCircArc2d circle(OdGePoint3d::kOrigin.convert2d(), radius);
OdGePoint2dArray points;
// Inscribe a polygon in a circle and get vertices
circle.getSamplePoints(sidesCnt, points);
// Create base profile
Profile2D cBase;
// Only with one contour
cBase.resize(1);
// Fill contour with vertices
cBase.front().appendVertices(points);
// Close profile
cBase.front().setClosed();
// Make contour outer
cBase.front().makeCCW();
// Create apex
OdGePoint3d apex
(
OdGePoint3d::kOrigin.x,
OdGePoint3d::kOrigin.y,
OdGePoint3d::kOrigin.z + height
);
// Create pyramid
return Body::pyramid(cBase, apex, devDeviation);
}
Profile3D sliceBody(const DeviationParams& devDeviation)
{
// Create plane
OdGePlane plane(OdGePoint3d(1., 1., 1.), OdGeVector3d(5., 4., 3.));
// Create body
Body body = createCube(devDeviation);
// Call slice method and receive resulting Profile3D
Profile3D result;
body.slice(plane, result, true);
return result;
}
Now that all the main functionality has been implemented, the next step is to build and run your application.
Copyright © 2002 – 2022. Open Design Alliance. All rights reserved.
|