Example of Using AcGi

The following example illustrates how to use AcGi. First, it saves the current entity property values for color, linetype, and layer. Then it changes the subentity trait color value to blue and draws a three-point polyline. Next, it changes the subentity trait color value to the current layer color, changes the linetype value to DASHDOT, and draws an xline.

The sample code includes two functions, getLinetypeFromString() and getLayerIdFromString(), which allow you to obtain an ID for a linetype or layer from a string.

Note: In practice, these functions might be too slow to use within worldDraw(), and object IDs should be stored and used directly.

static Acad::ErrorStatus
getLinetypeIdFromString(const TCHAR* str, AcDbObjectId& id);
static Acad::ErrorStatus
getLayerIdFromString(const TCHAR* str, AcDbObjectId& id);
 Adesk::Boolean 
 AsdkTraitsSamp::subWorldDraw(AcGiWorldDraw* pW) 
 { 
 // At this point, the current property traits are 
 // the entity's property traits.  If the current 
 // property traits are changed and you want to 
 // reapply the entity's property traits, this is 
 // the place to save them. 
 // 
 Adesk::UInt16 entity_color 
 = pW->subEntityTraits().color(); 
 AcDbObjectId  entity_linetype 
 = pW->subEntityTraits().lineTypeId(); 
 AcDbObjectId  entity_layer 
 = pW->subEntityTraits().layerId(); 
  
 // Override the current color and make it blue. 
 // 
 pW->subEntityTraits().setColor(kBlue); 
  
 // Draw a blue 3-point polyline. 
 // 
 int num_pts = 3; 
 AcGePoint3d *pVerts = new AcGePoint3d[num_pts]; 
 pVerts[0] = AcGePoint3d(0.0, 0.0, 0); 
 pVerts[1] = AcGePoint3d(1.0, 0.0, 0); 
 pVerts[2] = AcGePoint3d(1.0, 1.0, 0); 
  
 pW->geometry().polyline(num_pts, pVerts); 
  
 // Force the current color to use current layer's color. 
 // 
 pW->subEntityTraits().setColor(kColorByLayer); 
  
 // Force current line type to "DASHDOT".  If 
 // "DASHDOT" is not loaded, the current line 
 // type will still be in effect. 
 // 
 AcDbObjectId dashdotId; 
 if (getLinetypeIdFromString(_T("DASHDOT"), dashdotId) 
 == Acad::eOk) 
 { 
 pW->subEntityTraits().setLineType(dashdotId); 
 } 
  
 // Force current layer to "MY_LAYER".  If 
 // "MY_LAYER" is not loaded, the current layer 
 // will still be in effect. 
 // 
 AcDbObjectId layerId; 
 if (getLayerIdFromString(_T("MY_LAYER"), layerId) 
 == Acad::eOk) 
 { 
 pW->subEntityTraits().setLayer(layerId); 
 } 
  
 // Draw a dashdot'd xline in "MY_LAYER"'s color. 
 // 
 pW->geometry().xline(pVerts[0], pVerts[2]); 
  
 delete [] pVerts; 
  
 return Adesk::kTrue; 
 } 
  
  
 // A useful function that gets the linetype ID from the 
 // linetype's name -- must be in upper case. 
 // 
 static Acad::ErrorStatus 
 getLinetypeIdFromString(const TCHAR* str, AcDbObjectId& id) 
 { 
 Acad::ErrorStatus err; 
  
 // Get the table of currently loaded linetypes. 
 // 
 AcDbLinetypeTable *pLinetypeTable; 
 err = acdbHostApplicationServices()->workingDatabase() 
 ->getSymbolTable(pLinetypeTable, AcDb::kForRead); 
 if (err != Acad::eOk) 
 return err; 
  
 // Get the id of the linetype with the name that 
 // 'str' contains. 
 // 
 err = pLinetypeTable->getAt(str, id, Adesk::kTrue); 
  
 pLinetypeTable->close(); 
  
 return err; 
 } 
  
  
 // A useful function that gets the layer ID from the 
 // layer's name -- must be in upper case. 
 // 
 static Acad::ErrorStatus 
 getLayerIdFromString(const TCHAR* str, AcDbObjectId& id) 
 { 
 Acad::ErrorStatus err; 
  
 // Get the table of currently loaded layers. 
 // 
 AcDbLayerTable *pLayerTable; 
 err = acdbHostApplicationServices()->workingDatabase() 
 ->getSymbolTable(pLayerTable, AcDb::kForRead); 
 if (err != Acad::eOk) 
 return err; 
  
 // Get the ID of the layer with the name that 'str' 
 // contains. 
 // 
 err = pLayerTable->getAt(str, id, Adesk::kTrue); 
  
 pLayerTable->close(); 
  
 return err; 
 }