Drawings SDK Developer Guide > Working with .dwg Files > Working with Databases > Working with Database Containers > Working with Predefined Tables of Named Records > Working with Specific Predefined Tables of Named Records > Linetypes Table > Manipulating of Linetypes > Iterating Linetypes
Iterating through Linetypes

To traverse through the linetype table, use the OdDbLinetypeTableIterator class which implements a bidirectional iterator in the linetype table. The iterator refers to the linetype of the linetype table for which it can return various information. The iterator has the start() method for setting the start position to the first or last linetype, the step() method for traversing to the next or previous linetype, the done() method for checking the result, the seek() method for finding the specified linetype, and the getRecordId() method and getRecord() method for getting an existing linetype to which the iterator refers.

In the examples below, the pLinetypes variable stores a pointer to the linetype table object.

Creating an iterator

The iterator must be created before traversing. To create a new iterator, declare a variable of the OdDbLinetypeTableIteratorPtr type and call the NewIterator() method of the linetype table object. The NewIterator() method creates the instance of the iterator, sets the new iterator to the first unerased linetype record object in the linetype table, and returns the typified smart pointer to it. For example:


OdDbLinetypeTableIteratorPtr itLinetype1 = pLinetypes->newIterator();

To set the default position of the new iterator, use the first and second arguments of the NewIterator() method. The first argument specifies the start position as a Boolean value, which is True when the start position is the first linetype record object in the table or False when the start position is the last linetype record object in the table. The second argument specifies the erase status as a Boolean value, which is True when the first or last linetype record object must have the unerased status (that is, the linetype cannot be erased) or False when the first or last linetype record object can have any status (that is, the linetype either is erased or unerased). Both arguments are optional and are set to True by default. For example:


// Create a new iterator in the position of the last unerased linetype
OdDbLinetypeTableIteratorPtr itLinetype2 = pLinetypes->newIterator(false, true);

// Create a new iterator in the position of the first erased or unerased linetype
OdDbLinetypeTableIteratorPtr itLinetype3 = pLinetypes->newIterator(true, false);

The created iterator can be used multiple times for traversing through linetypes from the beginning to the end or from the end to the beginning of the linetype table.

Placing an iterator in the start position

Multiple use of the iterator requires setting the start position for new traversing. To set the start position of an existing iterator, use the start() method, which moves it to the first or last linetype in the linetype table and reinitializes the traversing. For example:


itLinetype1->start();

The start() method has two arguments and does not return a value. The first argument specifies the start position as a Boolean value which is True when the first linetype record object is the start position for traversing forward or False when the last linetype record object is the start position for traversing backward. The second argument specifies the erase status as a Boolean value which is True when the linetype record object must have the unerased status or False when the linetype record object can have the erased or unerased status. Both arguments are optional and are set to True by default. For example:


// Set the iterator to the first unerased linetype
// itLinetype1->start(true, true);   or   itLinetype1->start()

// Set the iterator to the last unerased linetype
itLinetype2->start(false, true);

// Set the iterator to the first erased or unerased linetype
itLinetype3->start(true, false);    

Moving an iterator

To move the iterator through linetypes, use the step() method which traverses it to the beginning or end of the linetype table. The step() method moves the iterator to the next unerased linetype record object by default. For example:


itLinetype1->step();

The step() method has two arguments and does not return a value. The first argument specifies the traverse direction as a Boolean value which is True when the iterator moves forward to the next linetype record object or False when the iterator moves backward to the previous linetype record object. The second argument specifies the erase status as a Boolean value which is True when the iterator skips the erased linetype record objects or False when the iterator processes each existing linetype record object of any status. Both arguments are optional and are set to True by default. For example:


// Move the iterator forward to the next unerased linetype
// itLinetype1->step(true, true);   or   itLinetype1->step();

// Move the iterator backward to the previous unerased linetype
itLinetype2->step(false, true);

// Move the iterator forward to the next linetype of the erased or unerased status
itLinetype3->step(true, false);    

Checking an iterator

To check whether traversing of an iterator is complete, use the done() method; it returns True when the iterator is out of the traverse scope or False when the iterator is placed in the traverse scope. For example:


while(!itLinetype1->done()) itLinetype1->step();

Getting a linetype using the iterator

To get the linetype, declare a variable of the OdDbLinetypeTableRecordPtr type and use the getRecord() method that returns a smart pointer to the referenced linetype record object to which the iterator refers. The getRecord() method opens the unerased linetype record object in read mode by default. For example:


OdDbLinetypeTableRecordPtr pLinetype1 = itLinetype1->getRecord();

The getRecord() method can have two optional arguments. The first argument specifies the open mode for the linetype record object as a value of the OdDb::OpenMode enumerator (kForRead, kForWrite, or kForNotify). The second argument specifies the erase status as a Boolean value which is True when the iterator must open an erased linetype record object or False when the iterator must open an unerased linetype record object. The first argument is OdDb::kForRead by default; the second argument is False by default. For example:


// Open the unerased linetype object in the read mode
// pLinetype1 = itLinetype1->getRecord(OdDb::kForRead, false);   or   pLinetype1 = itLinetype1->getRecord();

// Open the unerased linetype object in the write mode
OdDbLinetypeTableRecordPtr pLinetype2 = itLinetype2->getRecord(OdDb::kForWrite, false);

// Open the erased linetype object in the notify mode
OdDbLinetypeTableRecordPtr pLinetype3 = itLinetype3->getRecord(OdDb::kForNotify, true);    

Alternatively, the iterator can open a linetype using the getRecordId() method that does not have arguments and returns the OdDbObjectId instance associated with the linetype record object to which the iterator refers. For example:


OdDbObjectId idLinetype1 = itLinetype1->getRecordId();

To get the smart pointer to the linetype record object using its object ID, use the safeOpenObject() method of the OdDbObjectId object and pass to it the open mode as a value of the OdDb::OpenMode enumerator and the erase status as a Boolean value. The first argument is OdDb::kForRead by default; the second argument is False by default. For example:


// Open the unerased linetype object in the read mode
OdDbLinetypeTableRecordPtr pLinetype1 = idLinetype1.safeOpenObject();

// Open the unerased linetype object in the write mode
OdDbLinetypeTableRecordPtr pLinetype2 = itLinetype2->getRecordId().safeOpenObject(OdDb::kForWrite, false);

// Open the erased linetype object in the notify mode
OdDbLinetypeTableRecordPtr pLinetype3 = itLinetype3->getRecordId().safeOpenObject(OdDb::kForNotify, true);

Examples of iterating

The following example demonstrates iterating through unerased linetypes from the beginning to the end of the linetype table for printing:


odPrintConsoleString(L"\nThe list of the accessible linetypes:");

for(itLinetype1->start() ; !itLinetype1->done() ; itLinetype1->step())
{
  pLinetype1 = itLinetype1->getRecord();
  odPrintConsoleString(L"\n[%ls] Linetype: \"%ls\"", pLinetype1->handle().ascii(), pLinetype1->getName().c_str());
}

The following example demonstrates iterating through unerased linetypes from the end to the beginning of the linetype table for modifying properties:


for(itLinetype2->start(false, true) ; !itLinetype2->done() ; itLinetype2->step(false, true))
{
  pLinetype2 = itLinetype2->getRecord(OdDb::kForWrite);
  pLinetype2->setIsScaledToFit(false);
  pLinetype2->setPatternLength(2.5);
}

The following example demonstrates iterating through linetypes of unerased or erased status from the beginning to the end of the linetype table:


odPrintConsoleString(L"\nThe list of linetypes:");

for(itLinetype3->start(true, false) ; !itLinetype3->done() ; itLinetype3->step(true, false))
{
  pLinetype3 = itLinetype3->getRecordId().safeOpenObject(OdDb::kForWrite, true);
  odPrintConsoleString(L"\nLinetype: \"%ls\" is %ls", pLinetype3->getName().c_str(), 
                       ((pLinetype3->isErased()) ? L"erased" : L"unerased"));
}

See Also

Working with Linetypes

Getting and Checking Linetypes

Example of Working with the Linetype Table Object

Example of Working with the Linetype Record Object

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