Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...


File: ARES_Simplified_JNI.cpp

Code Block
languagecpp
JNIEXPORT jobjectArray JNICALL Java_com_graebert_dwgeditor_JNI_GetBlockNames( JNIEnv * env, jclass clazz )
{
    if ( ACTIVE_DOCUMENT() == 0 )
        return nullptr;

    CFxDatabasePtr pDB = ACTIVE_DOCUMENT()->GetFxDatabase();
    if ( pDB.isNull() )
        return nullptr;

    OdDbBlockTablePtr pBlockTable = pDB->getBlockTableId().openObject(OdDb::kForRead);
    if ( pBlockTable.isNull() )
        return nullptr;

    OdStringArray blockNames;
    OdDbSymbolTableIteratorPtr iterator = pBlockTable->newIterator();
    for ( ; !iterator->done(); iterator->step() )
    {
        OdDbBlockTableRecordPtr pBlockTableRecord = iterator->getRecord();
        OdString blockName = pBlockTableRecord->getName();

        if ( blockName.isEmpty() )
            continue;

        if ( blockName.getAt(0) == L'*' )
            continue;

        if ( pBlockTableRecord->isAnonymous() )
            continue;

        if ( pBlockTableRecord->isLayout() || pBlockTableRecord->isFromExternalReference() )
            continue;

        blockNames.push_back( blockName );
    }

    jclass stringClass = GET_JAVA()->FindClass( "java/lang/String" );
    CFxJavaObjectArray result( blockNames.size(), stringClass );

    jsize index = 0;
    for ( const CFxString& name : blockNames )
    {
        CFxJavaString javaString( name );
        result.SetElement( index, javaString.GetStrongJString() );
        index++;
    }

    return reinterpret_cast<jobjectArray >( result.GetStrongJObject() );
}

File: MainActivity.java

Code Block
languagejava
String m_Blocks[] = null;

public void DocumentActivated( String[] layouts, String[] blocks )
{
   m_Blocks = blocks;
...
}

private void InsertBlock()
{
   if ( m_Blocks == null || m_Blocks.length == 0 )
   {
       AlertDialog.Builder warningMessage = new AlertDialog.Builder( this );
       warningMessage.setTitle( getResources().getString( R.string.app_name ) );
       warningMessage.setMessage( "No blocks to insert in this drawing." );
       warningMessage.create().show();
       return;
   }

   AlertDialog.Builder builder = new AlertDialog.Builder( this );
   builder.setTitle("Select block");
   builder.setItems( m_Blocks, new DialogInterface.OnClickListener()
   {
       @Override
       public void onClick(DialogInterface dialog, int which)
       {
           String command = "_-INSERTBLOCK\n\"" + m_Blocks[which] + "\"\n\\1.0\n1.0\n0.0\n";
           CFxARESInstance.instance().runCommand(command);
       }
   });

   builder.setNegativeButton("Cancel", null);
   AlertDialog dialog = builder.create();
   dialog.show();
}