Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Example of Working with the Scale Dictionary Object

This example demonstrates working with the scale dictionary object for adding, renaming, deleting, modifying, and printing scale objects. This example uses a console menu for the following operations: printing a list of the accessible scales [L], printing specific properties of all scales contained in the dictionary [P], creating a new scale and adding it in the dictionary [A], renaming an existing scale [N], deleting an existing scale [D], and modifying the specific properties of an existing scale [M].

The ModifyScales() function requires a pointer to the database object and implements the console menu for the listed operations. The function creates a loop that inquires about the operation code and uses the switch statement to select whether the case must be performed. The ModifyScales() function uses the PrintScaleProperties() function for displaying the specific properties of the scale object passed to it, and the function uses the EntryRecordName() function for entering the external scale name and checking whether this name is correct and exists or is absent from the dictionary. The EntryRecordName() function returns the entered name as an OdString value when the name is correct for the specified options, or it returns an empty string when the name is incorrect or is in conflict with other names. The ModifyScales() function obtains the smart pointer to the scale dictionary using the getScaleListDictionaryId() method of the passed database object. The scale dictionary object is not a predefined object and can be absent from the database, therefore the function passes a True value as an argument of the getScaleListDictionaryId() method to create the scale dictionary if it is absent. Before the loop, the function declares a smart pointer to the iterator and a smart pointer to the scale object. The ModifyScales() function processes user actions in the loop until the user selects [Q] operation.

When the user selects [L], the ModifyScales() function displays a list of the external scale names which are accessible in the dictionary. The function uses an iterator for traversing through the dictionary and the name() method of the iterator for displaying the external name of each scale object to which the iterator refers. When the user selects [P], the function prints the specific properties of all scale objects contained in the dictionary. The function uses an iterator for traversing through the dictionary, the getObject() method of the iterator for getting the smart pointer of the scale object to which the iterator refers, and the PrintScaleProperties() function for displaying specific properties of the obtained scale object. To traverse the dictionary, the ModifyScales() function creates a new iterator for each traverse using the newIterator() method, organizes the loop until the traverse is not completed using the done() method, and moves the iterator to the next scale object using the next() method. The PrintScaleProperties() function requires a pointer to the OdDbScale instance as an argument, therefore the ModifyScales() function uses the getObject() method of the iterator to get the smart pointer and the get() method of the smart pointer to get the raw pointer.

When the user selects [A], the ModifyScales() function inquires about the external name for a new scale object using the EntryRecordName() function. The external scale name must be absent from the dictionary because the dictionary cannot contain duplicate names; the function passes the kMustAbsent value as an argument for checking the new name. If the entered name is correct and is absent from the dictionary, the function creates a new scale object using its static pseudo-constructor. Then, the function adds the new scale object with the specified external name in the dictionary using the setAt() method of the dictionary object. The function uses the try…catch statement to catch exceptions when the object is added. If the addition is successful, the function goes to the [L] operation for printing the scale list. When an exception occurs, the function displays an error message and breaks to the console menu.

When the user selects [N], the ModifyScales() function inquires about the external name of the existing scale object to be renamed and a new external name using the EntryRecordName() function. The first name must exist in the dictionary; the function passes the kMustExist value as an argument for checking the entered name. The second name must be absent from the dictionary because the dictionary cannot contain duplicates; the function passes the kMustAbsent value as an argument for checking the new name. If both names are correct, the function calls the setName() method of the dictionary object and passes to it these external names as arguments. The function uses the try…catch statement to catch exceptions when the object changes the external names. If an exception occurs, the function displays an error message and breaks to the console menu.

When the user selects [D], the ModifyScales() function inquires about the external name of the existing scale object to be deleted using the EntryRecordName() function. The name must exist in the dictionary; the function passes the kMustExist value as a second argument for checking the entered name. If the entered name is correct and exists, the ModifyScales() function removes the scale object from the dictionary. The function calls the remove() method of the dictionary object and passes to it the external name as an argument. The removed scale object continues to exist outside of the dictionary, therefore the function gets the OdDbObjectId instance associated with the removed object using the remove() method. To delete the removed object, the function uses the safeOpenObject() method to get a smart pointer to it, calls the erase() method of the object, and passes to it a True value as an argument. After deletion, the function checks the result returned by the erase() method. If the result is non-zero, the function displays an error message using the getErrorDescription() method of the host object.

When the user selects [M], the ModifyScales() function inquires about the external name of the existing scale object to be modified using the EntryRecordName() function. The name must exist in the dictionary; the function passes the kMustExist value as an argument for checking the name. If the entered name is correct and exists, the ModifyScales() function gets a smart pointer to the existing scale object specified by the external name using the getAt() method of the dictionary object and opens it in write mode. Then, the function calls the ModifyScaleProperties() function that organizes its own console menu for modifying specific properties of the specified scale object. The ModifyScales() function uses the get() method of the obtained smart pointer to pass the raw pointer to the scale object to be modified.

When the user selects [Q], the function ends the loop and returns to the calling function.

The ModifyScales() function has the following implementation:


#include "..\Common\DemoMain.h"
#include "DbScale.h"
#include <iostream>
using namespace std;

void PrintScaleProperties(OdDbScale* pScale);
void ModifyScaleProperties(OdDbScale* pScale);

void ModifyScales(OdDbDatabase* pDb)
{
  wchar_t ch = L'L';
  OdString sName, sNewName;
  OdResult result;

  OdDbDictionaryPtr pScales = pDb->getScaleListDictionaryId(true).safeOpenObject(OdDb::kForWrite);
  OdDbDictionaryIteratorPtr itScale;
  OdDbScalePtr pScale;

  wcout << L"\n\nStart modifying of scales";
  wcout << L"\nScale's Table " << pScales->handle().ascii() << ", " << pScales->isA()->name()
        << L", Class: " << OdDbScale::desc()->name() << L"\n";

  do {
    switch(ch)
    {
      case L'A':          // Add a new scale
      case L'a':
        wcout << L"\nEntry the key of a new scale:>";
        if((sName = EntryRecordName(pScales, kMustAbsent)).isEmpty()) break;

        pScale = OdDbScale::createObject();
       
        try {
          pScales->setAt(sName, pScale);
        }
        catch(OdError& e)
        {
          wcout << L"\nError: " << e.code() << L" - " << e.description() << L"\n";
          pScale->erase();
          break;
        }


      case L'L':          // List the accessible scales
      case L'l':
        wcout << L"\nList of the accessible scales:\n"; 
        for(itScale = pScales->newIterator() ; !itScale->done() ; itScale->next())
          wcout << L"\"" << itScale->name() << L"\"\n";
        break;


      case L'P':          // Print the specific properties of all scales
      case L'p':
        wcout << L"\nList of scales:"; 
        for(itScale = pScales->newIterator() ; !itScale->done() ; itScale->next())
        {
          pScale = itScale->getObject();
          PrintScaleProperties(pScale.get());
        }
        itScale = NULL;
        wcout << L"\n";        
        break;


      case L'N':
      case L'n':
        wcout << L"\nEntry the scale key to be renamed:>" ;
        if((sName = EntryRecordName(pScales, kMustExist)).isEmpty()) break;

        pScale = pScales->getAt(sName, OdDb::kForWrite);

        wcout << L"\nEntry the new scale key:>";
        if((sNewName = EntryRecordName(pScales, kMustAbsent)).isEmpty()) break;

        try {
          pScales->setName(sName, sNewName);;
        }
        catch(OdError& e) { wcout << L"\nError: " << e.code() << L" - " << e.description() << L"\n"; }
        break;


      case L'D':
      case L'd':
        wcout << L"\nEntry the scale key to be deleted:>" ;
        if((sName = EntryRecordName(pScales, kMustExist)).isEmpty()) break;

        pScale = pScales->remove(sName).safeOpenObject(OdDb::kForWrite);

        result = pScale->erase(true);

        if(result) wcout << L"\nError: " << result << L" - " 
                         << pDb->appServices()->getErrorDescription(result) << L"\n"; 
        break;


      case L'M':
      case L'm':
       	wcout << L"\nEntry the scale key to be modified:>" ;
        if((sName = EntryRecordName(pScales, kMustExist)).isEmpty()) break;

        pScale = pScales->getAt(sName, OdDb::kForWrite);

        ModifyScaleProperties(pScale.get());
        break;


      case L'\0':         // Skip an action
        break;

      default:
        wcout << L"Error: Incorrect operation\n";
    }
    wcout << L"\nL. List the accessible scales";
    wcout << L"\nP. Print the properties of all scales";
    wcout << L"\nA. Add a new scale";
    wcout << L"\nN. Rename the selected scale";
    wcout << L"\nM. Modify the selected scale";
    wcout << L"\nD. Delete the selected scale";
    wcout << L"\nQ. Quit";
    wcout << L"\nSelect operation:>";
    wcin >> ch;
  }
  while(ch != L'Q' && ch != L'q');

  wcout << L"Stop modifying of scales\n";
}

Testing gives the following results:


Start modifying of scales
Scale's Table 5D, AcDbDictionary, Class: AcDbScale

List of the accessible scales:
"A0"
"A1"
"A2"
"A3"
"A4"
"A5"
"A6"
"A7"
"A8"
"A9"

L. List the accessible scales
P. Print the properties of all scales
A. Add a new scale
N. Rename the selected scale
M. Modify the selected scale
D. Delete the selected scale
Q. Quit
Select operation:>

When the operation is A-"Add a new scale":


Entry the key of a new scale:>ZX4
List of the accessible scales:
"A0"
"A1"
"A2"
"A3"
"A4"
"A5"
"A6"
"A7"
"A8"
"A9"
"ZX4"

When the operation is A-"Add a new scale" and the scale key exists in the dictionary:


Entry the key of a new scale:>A0
Error: the name "A0" - already exists

When the operation is A-"Add a new scale" and the scale key is incorrect:


Entry the key of a new scale:>A:0?
Error: the name contains inadmissible letters: <>/\":;?,|=`

When the operation is N-"Rename the selected scale":


Entry the scale key to be renamed:>ZX4
Entry the new scale key:>Z0

When the operation is N-"Rename the selected scale" and the scale key is absent from the dictionary:


Entry the scale key to be renamed:>ZX4
Error: the name "ZX4" - is not found

When the operation is D-"Delete the selected scale":


Entry the scale key to be deleted:>A9

When the operation is D-"Delete the selected scale" and the scale is absent from the dictionary or the object was already deleted:


Entry the scale key to be deleted:>A9
Error: the name "A9" - is not found

When the operation is M-"Modify the selected scale":


Entry the scale key to be modified:>Z0
Start modifying of the scale properties

Selected scale: "Z0"-<> = 1
N. Set the Scale Name
U. Set the Paper Units
D. Set the Drawing Units
S. Set the Default status
T. Set the Temporary status
P. Print the specific properties
Q. Quit
Select operation:>N

Entry a new scale name:>3:8
Selected scale: "Z0"-<3:8> = 1

Select operation:>U

Entry a new paper units factor [f > 0]:>3.0
Paper units is set to: 3
Selected scale: "Z0"-<3:8> = 3

Select operation:>D

Entry a new drawing units factor [f > 0]:>8.0
Drawing units is set to: 8
Selected scale: "Z0"-<3:8> = 0.375

Select operation:>T

Entry the Temporary status [T-scale is temporary|F-scale is fixed]:>T
Status is set to: Temporary
Selected scale: "Z0"-<3:8> = 0.375

Select operation:>Q
Stop modifying of the scale properties

When the operation is P-"Print the specific properties of all scales":


List of scales:
H=41
  Scale Key = "A0"
  Scale Name = <1:1>
  Status: unerased,resident,fixed,selected(1:1)
  Scale = 1
  Paper Units = 1
  Drawing Units = 1
H=42
  Scale Key = "A1"
  Scale Name = <1:2>
  Status: unerased,resident,fixed
  Scale = 0.5
  Paper Units = 1
  Drawing Units = 2
H=43
  Scale Key = "A2"
  Scale Name = <1:4>
  Status: unerased,resident,fixed
  Scale = 0.25
  Paper Units = 1
  Drawing Units = 4
H=44
  Scale Key = "A3"
  Scale Name = <1:5>
  Status: unerased,resident,fixed
  Scale = 0.2
  Paper Units = 1
  Drawing Units = 5
H=45
  Scale Key = "A4"
  Scale Name = <1:8>
  Status: unerased,resident,fixed
  Scale = 0.125
  Paper Units = 1
  Drawing Units = 8
H=46
  Scale Key = "A5"
  Scale Name = <1:10>
  Status: unerased,resident,fixed
  Scale = 0.1
  Paper Units = 1
  Drawing Units = 10
H=47
  Scale Key = "A6"
  Scale Name = <1:16>
  Status: unerased,resident,fixed
  Scale = 0.0625
  Paper Units = 1
  Drawing Units = 16
H=48
  Scale Key = "A7"
  Scale Name = <1:20>
  Status: unerased,resident,fixed
  Scale = 0.05
  Paper Units = 1
  Drawing Units = 20
H=49
  Scale Key = "A8"
  Scale Name = <1:30>
  Status: unerased,resident,fixed
  Scale = 0.0333333
  Paper Units = 1
  Drawing Units = 30
H=4A
  Scale Key = "Z0"
  Scale Name = <3:8>
  Status: unerased,resident,temporary
  Scale = 0.375
  Paper Units = 3
  Drawing Units = 8

Use the Q-"Quit" operation to exit:


Stop modifying of scales

See Also

Working with Scales

Working with Dictionaries of Objects

Manipulating Objects of the Scale Dictionary

Example of Working with the Scale Object

Copyright © 2002 – 2022. Open Design Alliance. All rights reserved.