/
Android: DWG Viewer

Android: DWG Viewer

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

 

 

Features

  • Viewer object

  • Load drawing

  • Close drawing

  • Change viewing modes

  • Change layout background color

  • Switch between model and sheets

 

DWG Viewer - Viewer object JAVA

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’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 kernel SDK starts to render the drawing into the CFxARESDocumentView object that will be accessed by getDocumentView() from the ARESDelegate.java when a document is loaded successfully.

Note: The host 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

<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" />

 

File: MainActivity.java

@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 ) ... }

 

File: ARESDelegate.java

@Override public CFxARESDocumentView getDocumentView() {    return m_Host.m_ARESView; }

 

DWG Viewer - Load drawing JAVA

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

 

File: MainActivity.java

 

DWG Viewer - Close active drawing JAVA

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.

 

File: ARESDelegate.java

 

File: MainActivity.java

 

DWG Viewer - Change viewing modes JAVA

It is not necessary to implement viewing gestures. The framework comes with a set of basic finger gestures to change the active view of the drawing. It supports zooming in and out but has to know if it runs in Pan or Orbit mode. The viewing mode can be switched with a Diesel expression that can be executed with the runCommand() method of the singleton CFxARESInstance.instance().

 

File: MainActivity.java


Hint:  You can achieve similar results with commands like ZOOM or PAN.

 

DWG Viewer - Java Native Interface JAVA C++
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.


All necessary C++ files ( headers or source files ) as well as native libraries are treated within a nested structure of files named CMakeLists.txt. Subprojects may have their own CMakeLists.txt to reduce the complexity of all C++ parts. Including an additional block like externalNativeBuild inside the build.gradle of the project takes over the configuration and manages the compilation of the C++ code if everything is set up correctly.

JNI specification: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html

CMake specification:

https://cmake.org/cmake/help/v2.8.8/cmake.html

Gradle Native Code:

https://developer.android.com/studio/projects/gradle-external-native-builds


Note: More details can be found in the topics “Java and C++ synergy” and “C++ modules”.

 

DWG Viewer - Change layout background color JAVA C++

The DWG Viewer uses a native method SetBackgroundColor() to change the background color of the active model or sheet.  The offered color indexes are linked to a button. The native method is called when the button is tapped.  The native method SetBackgroundColor() manipulates the C++ part of the application and must be executed from the working thread.  A  suitable method runOnWorkingThread() is provided by CFxARESInstance.instance().

 

File: MainActivity.java

 

File: JNI.java

 

File: ARES_Simplified_JNI.cpp

 

DWG Viewer - Switch between model and sheets JAVA C++

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 method runOnUiThread() is provided by CFxARESInstance.instance().

 

File: JNI.java


File: ARES_Simplified_JNI.cpp

 

File: ARESDelegate.java

 

File: MainActivity.java

 

Related content