Using Callbacks Tutorial

Overview

The Delphi drawing format program API provides a way to register callbacks when a block or block entry added to the format. These callbacks would primarily be used to override field default values from a database. This tutorial will demostrate using the callbacks but will just place hardcoded values in the field instead of pulling the values from a database. The tutorial assumes you have gone through the Basic Creation & Query tutorial so will not rehash anything covered there. The completed project is located under DSL_BASE_DIR/format_api_examples/callbacks.

Note that the project's DLL will be installed in the startup directory so the ufsta entry point will be used and not ufusr.

Registering Callbacks

The first thing to do will be to register the callbacks upon NX startup. We plan to provide our own values from both the titleblock and revision block so we need to register two callbacks. Here is the basic skeleton:

#include <dph_format.hpp>

#include <uf.h>
#include <uf_exit.h>
#include <NXOpen/Part.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Drawings_DrawingSheetCollection.hxx>

namespace
{
    void setTitleblockDefaults( const std::string& blockId )
    {
    }

    void setRevBlockEntryDefaults( const std::string& blockId, int entryIndex )
    {
    }
}

void ufsta( char *param, int *retcode, int rlen )
{
    try
    {
        DphFormat::registerInsertBlockCB("dphTitle", setTitleblockDefaults);
        DphFormat::registerAppendEntryCB("dphRevision", setRevBlockEntryDefaults);
    }
    catch ( const std::exception& error )
    {
        UF_print_syslog( (char*)error.what(), FALSE );
    }
}

The above code registers the callbacks for both blocks. If you have other blocks you want to process, you would register a callback for each one.

Implementing a Callback

So far, the callbacks do not do anything. Let's change that. When modifying a field value, we need the pointer to the block object so let's create a function to do just that.

DphFormat::BlockPtr getBlock( const std::string& blockId )
{
    NXOpen::Part *workPart = NXOpen::Session::GetSession()->GetParts()->GetWork();
    DphFormat::Format format(workPart->GetDrawingSheets()->GetCurrentDrawingSheet());
    return( format.getBlock(blockId));
}

Place the code above both callbacks in the empty namespace since they will be using it.

Now we can implement the callbacks. The implementations are not very large so I will just list them in full.

void setTitleblockDefaults( const std::string& blockId )
{
    DphFormat::BlockPtr titleBlock(getBlock(blockId));
    titleBlock->setFieldValue("drawingName", "TEST API OF\nDELPHI FORMAT PROGRAM");
    titleBlock->setFieldValue("drawingNumber", "12345678");
}

void setRevBlockEntryDefaults( const std::string& blockId, int entryIndex )
{
    DphFormat::BlockPtr revBlock(getBlock(blockId));
    revBlock->setEntryFieldValue(entryIndex, "revision", "000");
    revBlock->setEntryFieldValue(entryIndex, "history", "INITIAL RELEASE");
}

Note that no beginUpdate or finishUpdate calls are needed when editing the field values since that will be done by the client application.

That is really all there is to using the callbacks. Of course, where I have hardcoded the field values, you would pull them from a database. You can now build and install the DLL in a startup directory and run NX. When you create a Delphi Corporate format, the fields in the titleblock will automatically be filled in. Also when an entry is added to the revision block, the revision and history fields will have the above defaults.