Application Programming Interface (API)
Plugins are implemented via Application Programming Interface (API) implementations. Each Plugin Class has its specific API and in the 3D Plugin tutorials we have seen examples of the implementation of the 3D Plugin API as declared by the header 3d_plugin.h
. Plugins may also rely on other APIs defined within the KiCad source tree; in the case of 3D plugins, all plugins which support visualization of models must interact with the Scene Graph API as declared in the header ifsg_all.h
and its included headers.
This section describes the details of available Plugin Class APIs and other KiCad APIs which may be required for implementations of plugin classes.
Plugin Class APIs
There is currently only one plugin class declared for KiCad: the 3D Plugin Class. All KiCad plugin classes must implement a basic set of functions declared in the header file kicad_plugin.h
; these declarations are referred to as the Base Kicad Plugin Class. No implementation of the Base Kicad Plugin Class exists; the header file exists purely to ensure that plugin developers implement these defined functions in each plugin implementation.
Within KiCad, each instance of a Plugin Loader implements the API presented by a plugin as though the Plugin Loader is a class providing the plugin’s services. This is achieved by the Plugin Loader class providing a public interface containing function names which are similar to those implemented by the plugin; the argument lists may vary to accommodate the need to inform the user of any problems which may be encountered if, for example, no plugin is loaded. Internally the Plugin Loader uses a stored pointer to each API function to invoke each function on behalf of the user.
API: Base Kicad Plugin Class
The Base Kicad Plugin Class is defined by the header file kicad_plugin.h
. This header must be included in the declaration of all other plugin classes; for an example see the 3D Plugin Class declaration in the header file 3d_plugin.h
. The prototypes for these functions were briefly described in Plugin Classes. The API is implemented by the base plugin loader as defined in pluginldr.cpp
.
To help make sense of the functions required by the base KiCad plugin header we must look at what happens in the base Plugin Loader class. The Plugin Loader class declares a virtual function Open()
which takes the full path to the plugin to be loaded. The implementation of the Open()
function within a specific plugin class loader will initially invoke the protected open()
function of the base plugin loader; this base open()
function attempts to find the address of each of the required basic plugin functions; once the addresses of each function have been retrieved, a number of checks are enforced:
Plugin
GetKicadPluginClass()
is invoked and the result is compared to the Plugin Class string provided by the Plugin Loader implementation; if these strings do not match then the opened plugin is not intended for the Plugin Loader instance.Plugin
GetClassVersion()
is invoked to retrieve the Plugin Class API Version implemented by the plugin.Plugin Loader virtual
GetLoaderVersion()
function is invoked to retrieve the Plugin Class API Version implemented by the loader.The Plugin Class API Version reported by the plugin and the loader are required to have the same Major Version number, otherwise they are considered incompatible. This is the most basic version test and it is enforced by the base plugin loader.
Plugin
CheckClassVersion()
is invoked with the Plugin Class API Version information of the Plugin Loader; if the Plugin supports the given version then it returnstrue
to indicate success. If successful the loader creates a PluginInfo string based on the results ofGetKicadPluginName()
andGetPluginVersion()
, and the plugin loading procedure continues within the Plugin Loader’sOpen()
implementation.
API: 3D Plugin Class
The 3D Plugin Class is declared by the header file 3d_plugin.h
and it extends the required plugin functions as described in Plugin Class: PLUGIN_3D. The corresponding Plugin Loader is defined in pluginldr3D.cpp
and the loader implements the following public functions in addition to the required API functions:
/* Open the plugin specified by the full path "aFullFileName" */
bool Open( const wxString& aFullFileName );
/* Close the currently opened plugin */
void Close( void );
/* Retrieve the Plugin Class API Version implemented by this Plugin Loader */
void GetLoaderVersion( unsigned char* Major, unsigned char* Minor,
unsigned char* Revision, unsigned char* Patch ) const;
The required 3D Plugin Class functions are exposed via the following functions:
/* returns the Plugin Class or NULL if no plugin loaded */
char const* GetKicadPluginClass( void );
/* returns false if no plugin loaded */
bool GetClassVersion( unsigned char* Major, unsigned char* Minor,
unsigned char* Patch, unsigned char* Revision );
/* returns false if the class version check fails or no plugin is loaded */
bool CheckClassVersion( unsigned char Major, unsigned char Minor,
unsigned char Patch, unsigned char Revision );
/* returns the Plugin Name or NULL if no plugin loaded */
const char* GetKicadPluginName( void );
/*
returns false if no plugin is loaded, otherwise the arguments
contain the result of GetPluginVersion()
*/
bool GetVersion( unsigned char* Major, unsigned char* Minor,
unsigned char* Patch, unsigned char* Revision );
/*
sets aPluginInfo to an empty string if no plugin is loaded,
otherwise aPluginInfo is set to a string of the form:
[NAME]:[MAJOR].[MINOR].[PATCH].[REVISION] where
NAME = name provided by GetKicadPluginClass()
MAJOR, MINOR, PATCH, REVISION = version information from
GetPluginVersion()
*/
void GetPluginInfo( std::string& aPluginInfo );
In typical situations, the user would do the following:
Create an instance of
KICAD_PLUGIN_LDR_3D
.Invoke
Open( "/path/to/myplugin.so" )
to open a specific plugin. The return value must be checked to ensure that the plugin loaded as desired.Invoke any of the 3D Plugin Class calls as exposed by
KICAD_PLUGIN_LDR_3D
.Invoke
Close()
to close (unlink) the plugin.Destroy the
KICAD_PLUGIN_LDR_3D
instance.
Scenegraph Class APIs
The Scenegraph Class API is defined by the header ifsg_all.h
and its included headers. The API consists of a number of helper routines with the namespace S3D
as defined in ifsg_api.h
and wrapper classes defined by the various ifsg_*.h
headers; the wrappers support the underlying scene graph classes which, taken together, form a scene graph structure which is compatible with VRML2.0 static scene graphs. The headers, structures, classes and their public functions are as follows:
sg_version.h
/*
Defines version information of the SceneGraph Classes.
All plugins which use the scenegraph class should include this header
and check the version information against the version reported by
S3D::GetLibVersion() to ensure compatibility
*/
#define KICADSG_VERSION_MAJOR 2
#define KICADSG_VERSION_MINOR 0
#define KICADSG_VERSION_PATCH 0
#define KICADSG_VERSION_REVISION 0
sg_types.h
/*
Defines the SceneGraph Class Types; these types
are closely related to VRML2.0 node types.
*/
namespace S3D
{
enum SGTYPES
{
SGTYPE_TRANSFORM = 0,
SGTYPE_APPEARANCE,
SGTYPE_COLORS,
SGTYPE_COLORINDEX,
SGTYPE_FACESET,
SGTYPE_COORDS,
SGTYPE_COORDINDEX,
SGTYPE_NORMALS,
SGTYPE_SHAPE,
SGTYPE_END
};
};
The sg_base.h
header contains declarations of basic data types used by the scenegraph classes.
sg_base.h
/*
This is an RGB color model equivalent to the VRML2.0
RGB model where each color may have a value within the
range [0..1].
*/
class SGCOLOR
{
public:
SGCOLOR();
SGCOLOR( float aRVal, float aGVal, float aBVal );
void GetColor( float& aRedVal, float& aGreenVal, float& aBlueVal ) const;
void GetColor( SGCOLOR& aColor ) const;
void GetColor( SGCOLOR* aColor ) const;
bool SetColor( float aRedVal, float aGreenVal, float aBlueVal );
bool SetColor( const SGCOLOR& aColor );
bool SetColor( const SGCOLOR* aColor );
};
class SGPOINT
{
public:
double x;
double y;
double z;
public:
SGPOINT();
SGPOINT( double aXVal, double aYVal, double aZVal );
void GetPoint( double& aXVal, double& aYVal, double& aZVal );
void GetPoint( SGPOINT& aPoint );
void GetPoint( SGPOINT* aPoint );
void SetPoint( double aXVal, double aYVal, double aZVal );
void SetPoint( const SGPOINT& aPoint );
};
/*
A SGVECTOR has 3 components (x,y,z) similar to a point; however
a vector ensures that the stored values are normalized and
prevents direct manipulation of the component variables.
*/
class SGVECTOR
{
public:
SGVECTOR();
SGVECTOR( double aXVal, double aYVal, double aZVal );
void GetVector( double& aXVal, double& aYVal, double& aZVal ) const;
void SetVector( double aXVal, double aYVal, double aZVal );
void SetVector( const SGVECTOR& aVector );
SGVECTOR& operator=( const SGVECTOR& source );
};
The IFSG_NODE
class is the base class for all scenegraph nodes. All scenegraph objects implement the public functions of this class but in some cases a particular function may have no meaning for a specific class.
ifsg_node.h
class IFSG_NODE
{
public:
IFSG_NODE();
virtual ~IFSG_NODE();
/**
* Function Destroy
* deletes the scenegraph object held by this wrapper
*/
void Destroy( void );
/**
* Function Attach
* associates a given SGNODE* with this wrapper
*/
virtual bool Attach( SGNODE* aNode ) = 0;
/**
* Function NewNode
* creates a new node to associate with this wrapper
*/
virtual bool NewNode( SGNODE* aParent ) = 0;
virtual bool NewNode( IFSG_NODE& aParent ) = 0;
/**
* Function GetRawPtr()
* returns the raw internal SGNODE pointer
*/
SGNODE* GetRawPtr( void );
/**
* Function GetNodeType
* returns the type of this node instance
*/
S3D::SGTYPES GetNodeType( void ) const;
/**
* Function GetParent
* returns a pointer to the parent SGNODE of this object
* or NULL if the object has no parent (ie. top level transform)
* or if the wrapper is not currently associated with an SGNODE.
*/
SGNODE* GetParent( void ) const;
/**
* Function SetParent
* sets the parent SGNODE of this object.
*
* @param aParent [in] is the desired parent node
* @return true if the operation succeeds; false if
* the given node is not allowed to be a parent to
* the derived object.
*/
bool SetParent( SGNODE* aParent );
/**
* Function GetNodeTypeName
* returns the text representation of the node type
* or NULL if the node somehow has an invalid type
*/
const char * GetNodeTypeName( S3D::SGTYPES aNodeType ) const;
/**
* Function AddRefNode
* adds a reference to an existing node which is not owned by
* (not a child of) this node.
*
* @return true on success
*/
bool AddRefNode( SGNODE* aNode );
bool AddRefNode( IFSG_NODE& aNode );
/**
* Function AddChildNode
* adds a node as a child owned by this node.
*
* @return true on success
*/
bool AddChildNode( SGNODE* aNode );
bool AddChildNode( IFSG_NODE& aNode );
};
IFSG_TRANSFORM
is similar to a VRML2.0 Transform node; it may contain any number of child IFSG_SHAPE and IFSG_TRANSFORM nodes and any number of referenced IFSG_SHAPE and IFSG_TRANSFORM nodes. A valid scenegraph must have a single IFSG_TRANSFORM
object as a root.
ifsg_transform.h
/**
* Class IFSG_TRANSFORM
* is the wrapper for the VRML compatible TRANSFORM block class SCENEGRAPH
*/
class IFSG_TRANSFORM : public IFSG_NODE
{
public:
IFSG_TRANSFORM( bool create );
IFSG_TRANSFORM( SGNODE* aParent );
bool SetScaleOrientation( const SGVECTOR& aScaleAxis, double aAngle );
bool SetRotation( const SGVECTOR& aRotationAxis, double aAngle );
bool SetScale( const SGPOINT& aScale );
bool SetScale( double aScale );
bool SetCenter( const SGPOINT& aCenter );
bool SetTranslation( const SGPOINT& aTranslation );
/* various base class functions not shown here */
};
IFSG_SHAPE
is similar to a VRML2.0 Shape node; it must contain a single child or reference FACESET node and may contain a single child or reference APPEARANCE node.
ifsg_shape.h
/**
* Class IFSG_SHAPE
* is the wrapper for the SGSHAPE class
*/
class IFSG_SHAPE : public IFSG_NODE
{
public:
IFSG_SHAPE( bool create );
IFSG_SHAPE( SGNODE* aParent );
IFSG_SHAPE( IFSG_NODE& aParent );
/* various base class functions not shown here */
};
IFSG_APPEARANCE
is similar to a VRML2.0 Appearance node, however, at the moment it only represents the equivalent of an Appearance node containing a Material node.
ifsg_appearance.h
class IFSG_APPEARANCE : public IFSG_NODE
{
public:
IFSG_APPEARANCE( bool create );
IFSG_APPEARANCE( SGNODE* aParent );
IFSG_APPEARANCE( IFSG_NODE& aParent );
bool SetEmissive( float aRVal, float aGVal, float aBVal );
bool SetEmissive( const SGCOLOR* aRGBColor );
bool SetEmissive( const SGCOLOR& aRGBColor );
bool SetDiffuse( float aRVal, float aGVal, float aBVal );
bool SetDiffuse( const SGCOLOR* aRGBColor );
bool SetDiffuse( const SGCOLOR& aRGBColor );
bool SetSpecular( float aRVal, float aGVal, float aBVal );
bool SetSpecular( const SGCOLOR* aRGBColor );
bool SetSpecular( const SGCOLOR& aRGBColor );
bool SetAmbient( float aRVal, float aGVal, float aBVal );
bool SetAmbient( const SGCOLOR* aRGBColor );
bool SetAmbient( const SGCOLOR& aRGBColor );
bool SetShininess( float aShininess );
bool SetTransparency( float aTransparency );
/* various base class functions not shown here */
/* the following functions make no sense within an
appearance node and always return a failure code
bool AddRefNode( SGNODE* aNode );
bool AddRefNode( IFSG_NODE& aNode );
bool AddChildNode( SGNODE* aNode );
bool AddChildNode( IFSG_NODE& aNode );
*/
};
IFSG_FACESET
is similar to a VRML2.0 Geometry node which contains an IndexedFaceSet node. It must contain a single child or reference COORDS node, a single child COORDINDEX node, and a single child or reference NORMALS node; in addition there may be a single child or reference COLORS node. A simplistic normals calculation function is provided to aid the user in assigning normal values to surfaces. The deviations from the VRML2.0 analogue are as follows:
Normals are always per-vertex.
Colors are always per vertex.
The coordinate index set must describe triangular faces only.
ifsg_faceset.h
/**
* Class IFSG_FACESET
* is the wrapper for the SGFACESET class
*/
class IFSG_FACESET : public IFSG_NODE
{
public:
IFSG_FACESET( bool create );
IFSG_FACESET( SGNODE* aParent );
IFSG_FACESET( IFSG_NODE& aParent );
bool CalcNormals( SGNODE** aPtr );
/* various base class functions not shown here */
};
ifsg_coords.h
/**
* Class IFSG_COORDS
* is the wrapper for SGCOORDS
*/
class IFSG_COORDS : public IFSG_NODE
{
public:
IFSG_COORDS( bool create );
IFSG_COORDS( SGNODE* aParent );
IFSG_COORDS( IFSG_NODE& aParent );
bool GetCoordsList( size_t& aListSize, SGPOINT*& aCoordsList );
bool SetCoordsList( size_t aListSize, const SGPOINT* aCoordsList );
bool AddCoord( double aXValue, double aYValue, double aZValue );
bool AddCoord( const SGPOINT& aPoint );
/* various base class functions not shown here */
/* the following functions make no sense within a
coords node and always return a failure code
bool AddRefNode( SGNODE* aNode );
bool AddRefNode( IFSG_NODE& aNode );
bool AddChildNode( SGNODE* aNode );
bool AddChildNode( IFSG_NODE& aNode );
*/
};
IFSG_COORDINDEX
is similar to a VRML2.0 coordIdx[] set except it must exclusively describe triangular faces, which implies that the total number of indices is divisible by 3.
ifsg_coordindex.h
/**
* Class IFSG_COORDINDEX
* is the wrapper for SGCOORDINDEX
*/
class IFSG_COORDINDEX : public IFSG_INDEX
{
public:
IFSG_COORDINDEX( bool create );
IFSG_COORDINDEX( SGNODE* aParent );
IFSG_COORDINDEX( IFSG_NODE& aParent );
bool GetIndices( size_t& nIndices, int*& aIndexList );
bool SetIndices( size_t nIndices, int* aIndexList );
bool AddIndex( int aIndex );
/* various base class functions not shown here */
/* the following functions make no sense within a
coordindex node and always return a failure code
bool AddRefNode( SGNODE* aNode );
bool AddRefNode( IFSG_NODE& aNode );
bool AddChildNode( SGNODE* aNode );
bool AddChildNode( IFSG_NODE& aNode );
*/
};
IFSG_NORMALS
is equivalent to a VRML2.0 Normals node.
ifsg_normals.h
/**
* Class IFSG_NORMALS
* is the wrapper for the SGNORMALS class
*/
class IFSG_NORMALS : public IFSG_NODE
{
public:
IFSG_NORMALS( bool create );
IFSG_NORMALS( SGNODE* aParent );
IFSG_NORMALS( IFSG_NODE& aParent );
bool GetNormalList( size_t& aListSize, SGVECTOR*& aNormalList );
bool SetNormalList( size_t aListSize, const SGVECTOR* aNormalList );
bool AddNormal( double aXValue, double aYValue, double aZValue );
bool AddNormal( const SGVECTOR& aNormal );
/* various base class functions not shown here */
/* the following functions make no sense within a
normals node and always return a failure code
bool AddRefNode( SGNODE* aNode );
bool AddRefNode( IFSG_NODE& aNode );
bool AddChildNode( SGNODE* aNode );
bool AddChildNode( IFSG_NODE& aNode );
*/
};
IFSG_COLORS
is similar to a VRML2.0 colors[] set.
ifsg_colors.h
/**
* Class IFSG_COLORS
* is the wrapper for SGCOLORS
*/
class IFSG_COLORS : public IFSG_NODE
{
public:
IFSG_COLORS( bool create );
IFSG_COLORS( SGNODE* aParent );
IFSG_COLORS( IFSG_NODE& aParent );
bool GetColorList( size_t& aListSize, SGCOLOR*& aColorList );
bool SetColorList( size_t aListSize, const SGCOLOR* aColorList );
bool AddColor( double aRedValue, double aGreenValue, double aBlueValue );
bool AddColor( const SGCOLOR& aColor );
/* various base class functions not shown here */
/* the following functions make no sense within a
normals node and always return a failure code
bool AddRefNode( SGNODE* aNode );
bool AddRefNode( IFSG_NODE& aNode );
bool AddChildNode( SGNODE* aNode );
bool AddChildNode( IFSG_NODE& aNode );
*/
};
The remaining API functions are defined in ifsg_api.h
as follows:
ifsg_api.h
namespace S3D
{
/**
* Function GetLibVersion retrieves version information of the
* kicad_3dsg library
*/
SGLIB_API void GetLibVersion( unsigned char* Major, unsigned char* Minor,
unsigned char* Patch, unsigned char* Revision );
// functions to extract information from SGNODE pointers
SGLIB_API S3D::SGTYPES GetSGNodeType( SGNODE* aNode );
SGLIB_API SGNODE* GetSGNodeParent( SGNODE* aNode );
SGLIB_API bool AddSGNodeRef( SGNODE* aParent, SGNODE* aChild );
SGLIB_API bool AddSGNodeChild( SGNODE* aParent, SGNODE* aChild );
SGLIB_API void AssociateSGNodeWrapper( SGNODE* aObject, SGNODE** aRefPtr );
/**
* Function CalcTriNorm
* returns the normal vector of a triangle described by vertices p1, p2, p3
*/
SGLIB_API SGVECTOR CalcTriNorm( const SGPOINT& p1, const SGPOINT& p2, const SGPOINT& p3 );
/**
* Function WriteCache
* writes the SGNODE tree to a binary cache file
*
* @param aFileName is the name of the file to write
* @param overwrite must be set to true to overwrite an existing file
* @param aNode is any node within the node tree which is to be written
* @return true on success
*/
SGLIB_API bool WriteCache( const char* aFileName, bool overwrite, SGNODE* aNode,
const char* aPluginInfo );
/**
* Function ReadCache
* reads a binary cache file and creates an SGNODE tree
*
* @param aFileName is the name of the binary cache file to be read
* @return NULL on failure, on success a pointer to the top level SCENEGRAPH node;
* if desired this node can be associated with an IFSG_TRANSFORM wrapper via
* the IFSG_TRANSFORM::Attach() function.
*/
SGLIB_API SGNODE* ReadCache( const char* aFileName, void* aPluginMgr,
bool (*aTagCheck)( const char*, void* ) );
/**
* Function WriteVRML
* writes out the given node and its subnodes to a VRML2 file
*
* @param filename is the name of the output file
* @param overwrite should be set to true to overwrite an existing VRML file
* @param aTopNode is a pointer to a SCENEGRAPH object representing the VRML scene
* @param reuse should be set to true to make use of VRML DEF/USE features
* @return true on success
*/
SGLIB_API bool WriteVRML( const char* filename, bool overwrite, SGNODE* aTopNode,
bool reuse, bool renameNodes );
// NOTE: The following functions are used in combination to create a VRML
// assembly which may use various instances of each SG* representation of a module.
// A typical use case would be:
// 1. invoke 'ResetNodeIndex()' to reset the global node name indices
// 2. for each model pointer provided by 'S3DCACHE->Load()', invoke 'RenameNodes()' once;
// this ensures that all nodes have a unique name to present to the final output file.
// Internally, RenameNodes() will only rename the given node and all Child subnodes;
// nodes which are only referenced will not be renamed. Using the pointer supplied
// by 'S3DCACHE->Load()' ensures that all nodes but the returned node (top node) are
// children of at least one node, so all nodes are given unique names.
// 3. if SG* trees are created independently of S3DCACHE->Load() the user must invoke
// RenameNodes() as appropriate to ensure that all nodes have a unique name
// 4. create an assembly structure by creating new IFSG_TRANSFORM nodes as appropriate
// for each instance of a component; the component base model as returned by
// S3DCACHE->Load() may be added to these IFSG_TRANSFORM nodes via 'AddRefNode()';
// set the offset, rotation, etc of the IFSG_TRANSFORM node to ensure correct
// 5. Ensure that all new IFSG_TRANSFORM nodes are placed as child nodes within a
// top level IFSG_TRANSFORM node in preparation for final node naming and output
// 6. Invoke RenameNodes() on the top level assembly node
// 7. Invoke WriteVRML() as normal, with renameNodes = false, to write the entire assembly
// structure to a single VRML file
// 8. Clean up by deleting any extra IFSG_TRANSFORM wrappers and their underlying SG*
// classes which have been created solely for the assembly output
/**
* Function ResetNodeIndex
* resets the global SG* class indices
*
* @param aNode may be any valid SGNODE
*/
SGLIB_API void ResetNodeIndex( SGNODE* aNode );
/**
* Function RenameNodes
* renames a node and all children nodes based on the current
* values of the global SG* class indices
*
* @param aNode is a top level node
*/
SGLIB_API void RenameNodes( SGNODE* aNode );
/**
* Function DestroyNode
* deletes the given SG* class node. This function makes it possible
* to safely delete an SG* node without associating the node with
* its corresponding IFSG* wrapper.
*/
SGLIB_API void DestroyNode( SGNODE* aNode );
// NOTE: The following functions facilitate the creation and destruction
// of data structures for rendering
/**
* Function GetModel
* creates an S3DMODEL representation of aNode (raw data, no transforms)
*
* @param aNode is the node to be transcribed into an S3DMODEL representation
* @return an S3DMODEL representation of aNode on success, otherwise NULL
*/
SGLIB_API S3DMODEL* GetModel( SCENEGRAPH* aNode );
/**
* Function Destroy3DModel
* frees memory used by an S3DMODEL structure and sets the pointer to
* the structure to NULL
*/
SGLIB_API void Destroy3DModel( S3DMODEL** aModel );
/**
* Function Free3DModel
* frees memory used internally by an S3DMODEL structure
*/
SGLIB_API void Free3DModel( S3DMODEL& aModel );
/**
* Function Free3DMesh
* frees memory used internally by an SMESH structure
*/
SGLIB_API void Free3DMesh( SMESH& aMesh );
/**
* Function New3DModel
* creates and initializes an S3DMODEL struct
*/
SGLIB_API S3DMODEL* New3DModel( void );
/**
* Function Init3DMaterial
* initializes an SMATERIAL struct
*/
SGLIB_API void Init3DMaterial( SMATERIAL& aMat );
/**
* Function Init3DMesh
* creates and initializes an SMESH struct
*/
SGLIB_API void Init3DMesh( SMESH& aMesh );
};
For actual usage examples of the Scenegraph API see the Advanced 3D Plugin tutorial above and the KiCad VRML1, VRML2, and X3D parsers.