Versions Compared

Key

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

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
Status
titleOBJ-C

The most important step is to embed the viewer object and connect it with the host application. The viewer object CFxARESDocumentView can be used and configured in a simple view controller or inside the storyboard. This object is a member of the host application. The host application’s main class is the AppDelegate that has a synergy with a class named CFxAresDelegate . The CFxAresDelegate 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 CFxAresDelegate 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: ViewController.h

Code Block
@property (weak, nonatomic) IBOutlet CFxARESDocumentView *m_DocumentView;

File: AppDelegate.m

Code Block
-(void)startARESInstance:(NSString*)login password:(NSString*)password
{
    ...
    aresDelegate = [[CFxAresDelegate alloc] init];
    [CFxARESInstance instance].delegate = aresDelegate;
    
    inputDelegate = [[UserInputDelegate alloc] init];
    [[CFxARESInstance instance] userInput].inputDelegate = inputDelegate;
    
    [[CFxARESInstance instance] startWith:login password:password];
    
    uiDelegate = [[CommonUIDelegate alloc] init];
    [CFxCommonUI startWithDelegate:uiDelegate];
    ...
}

File: CFxAresDelegate.m

Code Block
-(CFxARESDocumentView*)getDocumentView
{
    return [self getMainView].m_DocumentView;
}

DWG Viewer - Load drawing

Status
titleOBJ-C

The DWG Viewer reads all drawings from the Support folder of the IPA. 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 -(void)runCommand:(NSString*)command method of the singleton [CFxARESInstance instance]. Once the drawing is loaded the CFxAresDelegate triggers -(void)documentCreated and the CFxARESDocumentView can become visible. 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: CFxAresDelegate.m

Code Block
languageobjective-c
-(void)documentCreated
{
    [CFxARESInstance runOnUiThread:^
    {
        [self getMainView].m_DocumentView.hidden = NO;
    }];
}

 

File: ViewController.m

Code Block
languageobjective-c
 -(void)viewDidAppear:(BOOL)animated
 {
    ...
    NSString* command = @"_OPEN\n";
    command = [command stringByAppendingString:document];
    command = [command stringByAppendingString:@"\n"];

    [[CFxARESInstance instance] runCommand:command];
    ...
}

DWG Viewer - Close active drawing

Status
titleOBJ-C

The active drawing is closed with the CLOSE command. When the document is closed the CFxARESDelegate triggers -(void)documentDestroyed , the drawing is removed from the memory and the CFxARESDocumentView  can become hidden. The host application can set up the user interface for a new drawing like switching between state view controllers.

File: CFxAresDelegate.m

Code Block
languageobjective-c
-(void)documentDestroyed
{
    [CFxARESInstance runOnUiThread:^
    {
        [self getMainView].m_DocumentView.hidden = YES;
        ...
    }];
}

 

File: ViewController.m

Code Block
-(IBAction)onClosePressed:(id)sender
{
    ...
    [[CFxARESInstance instance] runCommand:@"_CLOSE\n"];
}

DWG Viewer - Change viewing modes

Status
titleOBJ-C

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 -(void)runCommand:(NSString*)command method of the singleton [CFxARESInstance instance].

 

File: ViewController.m

Code Block
languageobjective-c
-(void)SwitchViewModeToPan:(BOOL)panMode
{
    if (m_bPanMode != panMode)
    {
        m_bPanMode = panMode;
        ...
        [[CFxARESInstance instance] runCommand:@"'2dmode $M=$(if,$(and,$(getvar,2dmode),1),$(-,$(getvar,2dmode),1),$(+,$(getvar,2dmode),1))"];
    }
}


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

DWG Viewer - Change layout background color

Status
titleOBJ-C
Status
colourYellow
titleC++

The DWG Viewer uses a native method -(void)SetBackgroundColor:(Colors) color 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 -(void)SetBackgroundColor:(Colors) color manipulates the C++ part of the application.

File: CFxNativeConnection.mm

Code Block
-(void)SetBackgroundColor:(Colors)color
{
    ODCOLORREF background = 0;
    switch ( color )
    {
        case BLACK:
            background = 0;
            break;
        case WHITE:
            background = ODRGB( 255, 255, 255 );
            break;
        case MAGENTA:
            background = ODRGB( 255, 0, 255 );
            break;
    }

    OdEditorImplPtr pEditor = odedEditor();
    pEditor->fire_modelessOperationWillStart( L"BACKGROUND_COLOR" );
    ACTIVE_DOCUMENT()->GetFxDisplayDevice()->SetBackground( background );
    CFxDisplayDevicePtr pDevice = ACTIVE_DOCUMENT()->GetFxDisplayDevice();
    for ( int i = 0; i < pDevice->numViews(); i++ )
    {
        OdGsView *pView = pDevice->viewAt(i );
        if ( pView->isVisible() )
            pView->invalidate();
    }
    pEditor->fire_modelessOperationEnded( L"BACKGROUND_COLOR" );
}

 

File: ViewController.m

Code Block
- (IBAction)onBlackBackgroundPressed:(id)sender
{
    [[CFxNativeConnection GetNativeConnection] SetBackgroundColor:BLACK];
}

- (IBAction)onWhiteBackgroundPressed:(id)sender
{
    [[CFxNativeConnection GetNativeConnection] SetBackgroundColor:WHITE];
}

- (IBAction)onMagentaBackgroundPressed:(id)sender
{
    [[CFxNativeConnection GetNativeConnection] SetBackgroundColor:MAGENTA];
}

 

DWG Viewer - Switch between model and sheets

Status
titleOBJ-C
Status
colourYellow
titleC++

The DWG Viewer uses a native method -(void)AddSheetButtonsTo:(UIStackView*)sheets to get all available sheets of the drawing. All available sheets are stored as a UIButton inside a UIStackView. The button is linked with a command expression that is executed when tapped. The command _SHEET_CONTROL handles the layout switch.

 

File: ViewController.m

Code Block
languageobjective-c
-(void)viewDidAppear:(BOOL)animated
{
    ...
    [[CFxNativeConnection GetNativeConnection] AddSheetButtonsTo:m_StackSheets.firstObject];
}


File: CFxNativeConnection.mm

Code Block
languageobjective-c
-(void)AddSheetButtonsTo:(UIStackView*)sheets
{
    [CFxARESInstance runOnWorkingThread:^
    {
        std::map<int, CFxString> layouts;

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

        OdDbDictionaryPtr layoutDictionary = pDB->getLayoutDictionaryId().openObject();
        OdDbDictionaryIteratorPtr iterator = layoutDictionary->newIterator();
        while( !iterator->done() )
        {
            OdDbLayoutPtr pLayout = iterator->object();
            layouts[ pLayout->getTabOrder() ] = pLayout->getLayoutName();
            iterator->next();
        }

        [CFxARESInstance runOnUiThread:^
        {
            [sheets setSpacing:10];
            for (auto layout : layouts)
            {
                UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];

                NSString* sheet = [NSString stringWithFormat:@"%@", [CFxNativeConnection nsStringFromWchar:layout.second.wide_str()]];
                [button setTitle:sheet forState:UIControlStateNormal];

                [button setContentEdgeInsets:UIEdgeInsetsMake(0, 12, 0, 12)];

                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                CGFloat grayLevel = 2.0/3.0;
                [button setBackgroundColor:[UIColor colorWithRed:grayLevel green:grayLevel blue:grayLevel alpha:1]];

                [button addTarget:self action:@selector(OnSheetButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

                [sheets addArrangedSubview:button];
            }
            UIView* filler = [[UIView alloc] init];
            [sheets addArrangedSubview:filler];
        }];
    }];
}

 

File: CFxNativeConnection.mm

Code Block
languageobjective-c
-(IBAction)OnSheetButtonPressed:(UIButton*)button
{
    NSString* cmd = [NSString stringWithFormat:@"_SHEET_CONTROL\n_ACTIVATE\n%@\n", [button currentTitle]];
    [[CFxARESInstance instance] runCommand:cmd];
}