Versions Compared

Key

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

The DWG Viewer explains emphasizes how to embed the framework into a stand alone application and covers basic viewing and editing capabilities.

...

  • Viewer object

  • Load drawing

  • Close drawing

  • Change viewing modes

  • Change layout background color

  • Switch between model and sheets

DWG Viewer - Viewer object
Status
titleJAVA

The most important step is to embed the viewer object and connect it with the host application. The viewer object com.graebert.aresinstance.CFxARESDocumentView can be used and configured in a layout file. The next step is to bind the layout object with a property of type CFxARESDocumentView using findViewById().  This object is a member of the host application. The host application is connected to a class ARESDelegate.java extended by CFxARESInstanceDelegate and is application’s main class is the MainActivity.java that has a synergy with a class named ARESDelegate.java . The ARESDelegate.java is derived from CFxARESInstanceDelegate, is the core piece between the host application and the kernel SDK and must be initialized with the singleton CFxARESInstance.instance() from the main class. The core kernel SDK starts to render the drawing into the CFxARESDocumentView object that will be accessed by getDocumentView()  when from the ARESDelegate.java when a document is loaded and activated successfully.

Note: The host application application’s main class may be directly used as CFxARESInstanceDelegate but it is easier to split both parts of the application.

File: content_main.xml

Code Block
languagexml
<com.graebert.aresinstance.CFxARESDocumentView
     android:id="@+id/id_aresview"
     android:layout_width="0dp"
     android:layout_height="0dp"
     android:visibility="gone"
     app:layout_constraintBottom_toTopOf="@id/id_bottompanel"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toBottomOf="@id/id_background" />

...

Code Block
languagejava
@Override
protected void onCreate(Bundle savedInstanceState)
{
...
m_ARES = new ARESDelegate( this );
m_CommonUI = new CommonUIDelegate();
...
CFxARESInstance.instance().delegate = m_ARES;
CFxARESInstance.instance().start( this , "" , "" );
CFxCommonUI.instance().startWithDelegate( m_CommonUI );
...
m_ARESView = findViewById( R.id.id_aresview )
...
}

...

The DWG Viewer reads all drawings from the assets/resources of the APK. This may result in a slower opening and rendering time. It is recommended to load drawings from a local or external folder of the device. The drawing is loaded with the OPEN command  using the runCommand() method of the singleton CFxARESInstance.instance() and the CFxARESDocumentView can become visible.  Once the drawing is loaded the CFxARESDelegate triggers documentCreated() followed by documentActivated(). This is the time when additional user interfaces can show up or drawing content can be accessed. In general, the CFxARESDelegate informs the host application about all important drawing events.

Note: The framework supports opening DWG, DXF and DWT files.

...

File: ARESDelegate.java

Code Block
languagejava
@Override
public void documentCreated(){...}

@Override
public void documentActivated(){...}

...

File: MainActivity.java

Code Block
languagejava
private void LoadDrawing(){ 
...
m_ARESView.setVisibility( View.VISIBLE );
...
String fileName = "assets:/Samples/" + sampleDrawings[ which ];
String command = "_OPEN\n" + fileName + "\n";
CFxARESInstance.instance().runCommand( command, false );
...
}

public void DocumentCreated(){...}
public void DocumentActivated(String[] layouts){...}

...

The active drawing is closed with the CLOSE command. When the document is closed the CFxARESDelegate triggers documentDestroyed() , the drawing is removed from the memory and the CFxARESDocumentView  can become invisible. The host application can set up the user interface for a new drawing.

...

Code Block
private void SwitchViewMode( boolean panMode ){
...
CFxARESInstance.instance().runCommand("'2dmode     $M=$(if,$(and,$(getvar,2dmode),1),$(-,$(getvar,2dmode),1),$(+,$(getva    r,2dmode),1))");
}


NoteHint:  The core also provides You can achieve similar results with commands like ZOOM or PAN to achieve similar results.

DWG Viewer - Java Native Interface

Status
titleJAVA
Status
colourYellow
titleC++

Executing core commands is a good approach to quickly achieve results but in some cases it is necessary to access directly the drawing content or system relevant parameters.  The JNI, the Java Native Interface , allows to exchange data between Java and C++ or to directly manipulate drawing content. The synergy works with a combination of native Java methods and a matching C++ implementation that follows the JNI specifications. The C++ code is managed with CMake and compiled with the NDK. The created shared library (*.so) must be loaded using System.loadLibrary(). The JNI supports executing code in both directions.

...

Code Block
languagecpp
JNIEXPORT void JNICALL Java_com_graebert_aressimplified_JNI_SetBackgroundColor( JNIEnv * env, jclass clazz, jint color )
{ ... }

...


    ODCOLORREF background = 0;
    switch ( color )
    {
        case 1:
            background = 0;
            break;
        case 2:
            background = ODRGB( 255, 255, 255 );
            break;
        case 3:
            background = ODRGB( 255, 0, 255 );
            break;
    }

    OdEditorImplPtr pEditor = odedEditor();
    pEditor->fire_modelessOperationWillStart( L"BACKGROUND_COLOR" );
    ACTIVE_DOCUMENT()->GetFxDisplayDevice()->SetBackground( background );
    pEditor->fire_modelessOperationEnded( L"BACKGROUND_COLOR" );
}

DWG Viewer - Switch between model and sheets

Status
titleJAVA
Status
colourYellow
titleC++

The DWG Viewer uses a native method GetLayoutNames() to get all available sheets of the drawing when documentActivated() is triggered. All layouts are passed to the method DocumentActivated() of the main activity  and a corresponding button is created for each layout. The button is linked with a command expression that is executed when tapped. The command _SHEET_CONTROL handles the layout switch. The native method GetLayoutNames() manipulates the UI of the application and must be executed from the ui thread of the host application. A  suitable A suitable method runOnUiThread() is provided by CFxARESInstance.instance().

...

Code Block
languagecpp
JNIEXPORT jobjectArray JNICALL Java_com_graebert_aressimplified_JNI_GetLayoutNames( JNIEnv * env, jclass clazz )
{
    CFxDatabasePtr pDB = ACTIVE_DOCUMENT()->GetFxDatabase();
    if ( pDB.isNull() )
        return nullptr;

    OdDbDictionaryPtr layoutDictionary = pDB->getLayoutDictionaryId().openObject();
    int iLayoutCount = 0;
    OdDbDictionaryIteratorPtr iterator = layoutDictionary->newIterator();
    std::map<int, CFxString> layouts;
    while( !iterator->done() )
    {
        OdDbLayoutPtr pLayout = iterator->object();
        layouts[ pLayout->getTabOrder() ] = pLayout->getLayoutName();
        iterator->next();
    }

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

    jsize index = 0;
    for ( const auto& it : layouts )
    {
        CFxJavaString javaString( it.second );
        result.SetElement( index, javaString.GetStrongJString() );
        index++;
    }

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

File: ARESDelegate.java

Code Block
@Override
public void documentActivated()
{
   final String layouts[] = JNI.GetLayoutNames();
   CFxARESInstance.instance().runOnUiThread(new Runnable()
   {
       @Override
       public void run()
       {
           m_Host.DocumentActivated( layouts );
       }
   });
}

...