Difficulty: Medium
Next Tutorial: Importing OpenSCAD Code

The Dummy Part


In this example we will show how to create a DummyPart. Once the part is created it works as any other C++ object. You can translate, rotate, add, etc. it. And you can of course inherit other objects from it or create more complex parts or primitives using it (great, isn't it??).

All parts inherit from AbstractPart, and must implement, besides the constructor the build function. Which is a pure virtual function in AbstractPart. This function will return the part.

In the following example we will create a DummyPart, consisting of a simple cube, with different sides (by default 3, 2, 1).

DummyPart.h

#ifndef DUMMYPART_H_INCLUDED
#define DUMMYPART_H_INCLUDED
 
#ifdef WIN32
#ifdef OOMLParts_EXPORTS
#define OOMLParts_EXP_DEC __declspec(dllexport)
#else
#define OOMLParts_EXP_DEC __declspec(dllimport)
#endif
#else
#define OOMLParts_EXP_DEC
#endif
 
#include <core/AbstractPart.h>
 
/**
 * \brief DummyPart
 *
 * This class provides a DummyPart that can be used as template for other parts.
 */
class OOMLParts_EXP_DEC DummyPart : public AbstractPart
{
public:
 
    /**
     * @brief The Data struct
     */
    struct Data{
        double sx; //!< x dimension (mm)
        double sy; //!< y dimension (mm)
        double sz; //!< z dimension (mm)
    };
 
    /**
     * \brief Default parametrized constructor.
     */
    DummyPart(double sx, double sy, double sz):
        AbstractPart()
    {
        if (sx<=0) sx=1;
        if (sy<=0) sy=1;
        if (sz<=0) sz=1;
 
        data.sx=sx;
        data.sy=sy;
        data.sz=sz;
 
        rebuild();
    }
    /**
     * \brief Default destructor.
     */
    virtual ~DummyPart() {}
 
    inline Data getData(){ return data;}
protected:
    /**
     * \brief Build the piece.
     *
     * This method build the piece from simpler objects.
     *
     * \return The piece built.
     *
     * \note Complete the link, now it's incomplete.
     */
    virtual Component build();
 
private:
    Data data;
};
 
#endif // LINK_H_INCLUDED

DummyPart.cpp

#include <parts/DummyPart.h>
#include <components/Cube.h>
 
 
Component DummyPart::build()
{
    Component part = Cube(data.sx,data.sy,data.sz);
    return part;
}

Let's analyze code

#ifndef DUMMYPART_H_INCLUDED
#define DUMMYPART_H_INCLUDED
 
#ifdef WIN32
#ifdef OOMLParts_EXPORTS
#define OOMLParts_EXP_DEC __declspec(dllexport)
#else
#define OOMLParts_EXP_DEC __declspec(dllimport)
#endif
#else
#define OOMLParts_EXP_DEC
#endif
 
#include <core/AbstractPart.h>
 
/**
 * \brief DummyPart
 *
 * This class provides a DummyPart that can be used as template for other parts.
 */
class OOMLParts_EXP_DEC DummyPart : public AbstractPart

The first part before the class declaration allows the OOML Parts to be portable to Ms. Windows. Do not try to understand it, just copy/paste it.

DummyPart derives from AbstractPart, as all parts the user develops.

class OOMLParts_EXP_DEC DummyPart : public AbstractPart
{
public:
 
    /**
     * @brief The Data struct
     */
    struct Data{
        double sx; //!< x dimension (mm)
        double sy; //!< y dimension (mm)
        double sz; //!< z dimension (mm)
    };

We begin defining the data that will configure our part. These parameters will be available though the getData() function defined afterward.

/**
     * \brief Default parametrized constructor.
     *
     * \param sx x size(mm).
     * \param sy y size(mm).
     * \param sz z size(mm).
     */
    DummyPart(double sx, double sy, double sz):
        AbstractPart()
    {
        if (sx<=0) sx=1;
        if (sy<=0) sy=1;
        if (sz<=0) sz=1;
 
        data.sx=sx;
        data.sy=sy;
        data.sz=sz;
 
        rebuild();
    }

The constructor initializes the values of the parameteres, perfomrming sanity checks when necessary. At the end it calls the rebuild function. This function is implemented in the AbstractPart class, it will call the pure virtual function “build()” and will make another operations hidden to the developer (for simplicity).

The custom part developed will be then defined inside the build function.

protected:
    /**
     * \brief Build the piece.
     *
     * This method build the piece from simpler objects.
     *
     * \return The piece built.
     *
     * \note Complete the link, now it's incomplete.
     */
    virtual Component build();

Component DummyPart::build()
{
    Component part = Cube(data.sx,data.sy,data.sz);
    return part;
}

Recent changes RSS feed Creative Commons License Donate Minima Template by Wikidesign Driven by DokuWiki