- UID
- 3606
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-4-4
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2002-7-5 12:03:07
|
显示全部楼层
下面的程序实现了我预想的功能,如果当前文档已经有块,直接插入,如果没有则从指定的数据库中Clone一个实体过来然后在指定的点插入。
新的问题:在原数据库中定义的块是红色的(在第二帖的dwg文档中是红色的),Clone到当前文档后变为了白色,不知道这是为什么?
帮助分析分析,谢谢!
新的代码:
///////////////////////////////////////////////////////////////////////////////////////////
//
// ObjectARX defined commands, created by [七月/02/2002], ,
#include "StdAfx.h"
#include "StdArx.h"
#include "stdafx.h"
#include "resource.h"
#include "acutads.h"
#include "dbents.h"
#include "dbpl.h"
#include "MALLOC.h"
#include "STDLIB.h"
#include "acedads.h"
#include "acutads.h"
#include "dbmtext.h "
#include "stdio.h"
#include "dbapserv.h"
#include "dbidmap.h"
#include "dbsymutl.h"
#include "dbsymtb.h"
#include "actrans.h"
#include "dbhatch.h"
#include "gepnt2d.h"
#include "util.h"
bool InsertBlock(AcGePoint3d insertPt,char BlockName[100], CString BlockLayer,double BlockRotation)
{
AcDbBlockTable *pBlockTable = NULL;
acdbHostApplicationServices()->workingDatabase()
->getSymbolTable(pBlockTable,AcDb::kForWrite);
AcDbBlockTableRecord *pBlockTableRecord;
AcDbObjectId blockId;
if(pBlockTable->getAt(BlockName,blockId)==Acad::eOk)
{
AcDbBlockReference *pBlockReference = new AcDbBlockReference;
AcDbObjectId referenceId;
pBlockReference->setBlockTableRecord(blockId);
pBlockReference->setPosition(insertPt); //设置插入点
pBlockReference->setRotation(BlockRotation);//设置旋转角度
pBlockReference->setLayer(BlockLayer);//为插入的图块设置图层
pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
pBlockTableRecord->appendAcDbEntity(referenceId,pBlockReference);
pBlockTable->close();
pBlockTableRecord->close();
pBlockReference->close();
//此处原想将插入的块引用改变颜色,但是实际证明,如果块已经定义了,就不能更改块引用的颜色了
AcDbEntity *pEnt=NULL;
acdbOpenAcDbEntity(pEnt,referenceId,AcDb::kForWrite);
pEnt->setColorIndex(1);
pEnt->close();
return TRUE;
}
else
{
//没有图块什么也不做
pBlockTable->close();
return FALSE;
}
}
//-----------------------------------------------------------------------------
// This is command 'TEST, by [七月/02/2002], ,
void SKDtest()
{
#ifdef OARXWIZDEBUG
acutPrintf ("\nOARXWIZDEBUG - SKDtest() called.");
#endif // OARXWIZDEBUG
//Program Start
//获得块的插入点
ads_point Pnt1;
AcGePoint3d insertPt;
if (acedGetPoint(NULL,"\nPlease give me Insert point:",Pnt1)!=RTNORM)
{
acutPrintf("\n...User Cancel\n");
return;
}
insertPt[X]=Pnt1[X];
insertPt[Y]=Pnt1[Y];
insertPt[Z]=0;
if (InsertBlock(insertPt,"1","0",0) == FALSE)
{
//对数据库操作开始
AcDbDatabase *pCurrentDb = NULL; //用于指向当前数据库
AcDbDatabase *pSourceDb = new AcDbDatabase;//用于指向后台数据库
AcDbObjectId InsertblockId; //块的ObjctId
AcDbObjectId referenceId;//快参考的ObjectId
AcDbIdMapping pIdMap;
AcDbObjectIdArray list;
AcDbObjectId MyBlockId;
AcDbBlockTable *pBlockTable = NULL;
AcDbBlockTableRecord *pBlockTableRecord;
char *pBlockName;
AcDbBlockTableIterator *pLtIterator;
//*********************************************************************************
//以后台方式打开TCAD01.dwt数据库
//pSourceDb->readDwgFile("c:\\MapMaper\\TCAD01.dwg"); //Line 79
pSourceDb->readDwgFile("c:\\TCAD01.dwg");
pSourceDb->getSymbolTable(pBlockTable,AcDb::kForRead); //Line 80
//以前台方式打开TCAD01.dwt数据库
//acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,AcDb::kForRead); //Line 82
//***********************************************************************************
//以下for语句用于测试块表中的块的名称
//!!! 问题在于如果以后台方式打开就无法访问块表,如果以前台打开(当前文档就是TCAD01.dwt)就可以 Why???
pBlockTable->newIterator(pLtIterator);
pBlockTable->close(); //少了这一句
for(;!pLtIterator->done();pLtIterator->step ())
{
pLtIterator->getRecord(pBlockTableRecord,AcDb::kForRead);
pBlockTableRecord->getName(pBlockName);
AcDbObjectId aaaa;
AcDbHandle acHandle;
char handleStr[20];
aaaa = pBlockTableRecord->objectId();
pBlockTableRecord->getAcDbHandle(acHandle);
acHandle.getIntoAsciiBuffer(handleStr);
pBlockTableRecord->close();
acutPrintf("\n Block Name is: %s , Handle: %s",pBlockName,handleStr);
}
/////////////////////////////////////
delete pLtIterator;
if(pBlockTable->has("1"))
{
AcDbDatabase* toDb = acDocManager->curDocument()->database();//设置目标数据库
AcDbIdMapping idMap;
idMap.setDestDb(toDb);
AcDbObjectId destBlkId;
destBlkId = toDb->currentSpaceId();//得到当前数据库
pBlockTable->getAt("1",MyBlockId);
list.append( MyBlockId );
pSourceDb->wblockCloneObjects(list,destBlkId,idMap,AcDb::kDrcIgnore);//复制块到当前打开的文档
InsertBlock(insertPt,"1","0",0);
}
else
{
pBlockTable->close();
pSourceDb->closeInput();
delete pSourceDb;
return; //没有图块返回
}
pBlockTable->close();
pSourceDb->closeInput();
delete pSourceDb;
//Program End
}
}
//结束 |
|