第六讲 在ObjectARX中创建对象
在ObjectARX中创建对象本节中的ObjectARX源代码创建了与之前完全相同的实体(直线和圆),并演示了创建一个新层、改变线的颜色和添加到组词典中的过程.创建实体
下列代码创建了直线并将其添加到模型空间块表记录中:
AcDbObjectIdcreateLine()//创建直线{ AcGePoint3d startPt(4.0, 2.0, 0.0);//起点 AcGePoint3d endPt(10.0, 7.0, 0.0);//终点 AcDbLine *pLine = new AcDbLine(startPt,endPt);//内存中创建新的直线 AcDbBlockTable *pBlockTable; acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pBlockTable,AcDb::kForRead); AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord, AcDb::kForWrite); pBlockTable->close(); AcDbObjectId lineId; pBlockTableRecord->appendAcDbEntity(lineId,pLine); pBlockTableRecord->close(); pLine->close(); return lineId;}
createLine()程序从当前图形中获取块表,然后以写模式打开模型空间块表记录.关闭块表后,添加实体到块表记录并关闭块表记录和实体.注意 当用完任何ObjectARX对象后,必须尽早地将其关闭下面的 createCircle() 程序创建一个圆并将其添加到模型空间块表记录中.
AcDbObjectIdcreateCircle(){ AcGePoint3d center(9.0, 3.0, 0.0); AcGeVector3d normal(0.0, 0.0, 1.0); AcDbCircle *pCirc = new AcDbCircle(center,normal, 2.0); AcDbBlockTable *pBlockTable; acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pBlockTable,AcDb::kForRead); AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord, AcDb::kForWrite); pBlockTable->close(); AcDbObjectId circleId; pBlockTableRecord->appendAcDbEntity(circleId,pCirc); pBlockTableRecord->close(); pCirc->close(); return circleId;}
创建新层
下面的代码从数据库中获得层表,创建一个新的层表记录,并将其命名为ASDK_MYLAYER,然后将层表记录添加到层表中
void createNewLayer(){ AcDbLayerTable *pLayerTable; acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pLayerTable,AcDb::kForWrite); AcDbLayerTableRecord *pLayerTableRecord = new AcDbLayerTableRecord; pLayerTableRecord->setName("ASDK_MYLAYER"); // Defaults are used for other propertiesof // the layer if they are not otherwisespecified. // pLayerTable->add(pLayerTableRecord); pLayerTable->close(); pLayerTableRecord->close();}
打开和关闭ObjectARX对象
上述所有程序的例子都说明了打开和关闭对象的协议,每当我们操作数据库驻留对象时都要遵守该协议.该协议确保当对象被访问时在物理内存中,而当对象未被访问时可以被分页存储到磁盘中。在我们可以修改对象之前,必须打开它,例如:acdbOpenObject(pObject,objId, AcDb::kForWrite);打开函数有一个模式参数,用来说明要打开的对象是用于读、写或是通知操作.当以写模式打开对象时,我们可以对其进行修改;当我们使用完对象后,必须如下例那样关闭对象,不管以什么模式打开对象,都以同一方式关闭:pObject->close();下面是一个改变实体颜色的程序代码示例:
Acad::ErrorStatuschangeColor(AcDbObjectIdentId, Adesk::UInt16 newColor){ AcDbEntity *pEntity; acdbOpenObject(pEntity, entId, AcDb::kForWrite); pEntity->setColorIndex(newColor); pEntity->close(); return Acad::eOk;}
一个对象的新实例在打开时,缺省是用于写操作的.某些函数,如AcDbBlockTable::getAt()函数,获取对象ID的同时打开对象.对象被添加到数据库之后才能将其关闭;在对象被添加到数据库之前,我们拥有对象并可以随时删除对象.然而,一旦对象已经被添加到数据库后,我们就不能够直接删除它;我们可以通过调用AcDbObject::erase()函数,将对象标记为删除;被标记为删除的对象仍然保留在数据库中,直到数据库被析构为止,但是当保存时,不保存被标记为删除的对象.警告 如果直接删除已经被.添加到数据库的对象,将导致AutoCAD终止.在组词典中添加组
下列源代码将前面createLine()函数创建的直线和createCircle()函数创建的圆创建为一个编组(pGroup),并将其放进一个组词典中.线和圆的对象ID是通过参数传入函数的.请注意下面的程序是如何以写模式打开组词典,进行修改,然后将其关闭的.
void createGroup(AcDbObjectIdArray&objIds, char* pGroupName){ AcDbGroup *pGroup = newAcDbGroup(pGroupName); // Put the group in the group dictionarywhich resides // in the named object dictionary. // AcDbDictionary *pGroupDict; acdbHostApplicationServices()->workingDatabase() ->getGroupDictionary(pGroupDict,AcDb::kForWrite); AcDbObjectId pGroupId; pGroupDict->setAt(pGroupName, pGroup,pGroupId); pGroupDict->close(); // Now that the group has been added, ithas an ObjectID. // This is important since the group willbecome a persistent // reactor for the added entities... for (int i = 0; i < objIds.length();i++) { pGroup->append(objIds); } pGroup->close();} 学习了。。
页:
[1]