明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2517|回复: 2

在ARX中如何定义一个复杂块,望高手指点

[复制链接]
发表于 2004-5-20 08:01:00 | 显示全部楼层 |阅读模式
在ARX中如何定义一个复杂块,然后插入模型空间中?.望高手指点, qq 172675612
发表于 2004-5-21 20:36:00 | 显示全部楼层
Acad::ErrorStatus createBlockRecord (const char *name) {
// First, check if a block of the same name already exists
// by verifying in the current database block table.
AcDbBlockTable *pBlockTable ;
// Open the block table for read
Acad::ErrorStatus es ;
if ( (es =acdbHostApplicationServices ()->workingDatabase ()->getBlockTable (pBlockTable, AcDb::kForRead)) != Acad::eOk )
return (es) ; if ( pBlockTable->has (name) == Adesk::kTrue ) {
pBlockTable->close () ;
return (Acad::eDuplicateKey) ;
}
// Now we know the block does not exist, so we create it
// using the name passed in.
AcDbBlockTableRecord *pBlockTableRecord =new AcDbBlockTableRecord ;
pBlockTableRecord->setName (name) ;
// To keep it simple, we use the origin for the insertion point
pBlockTableRecord->setOrigin (AcGePoint3d::kOrigin) ;
// Open the block table for write
// since we are adding a new block definition
if ( (es =pBlockTable->upgradeOpen ()) != Acad::eOk ) {
delete pBlockTableRecord ;
pBlockTable->close () ;
return (es) ;
}
// Add the new block table record to the block table.
// For now, the block table record is empty.
if ( (es =pBlockTable->add (pBlockTableRecord)) != Acad::eOk ) {
// The block table record has not been added
// to the block table, so we have to delete it.
pBlockTable->close();
delete pBlockTableRecord;
return (es) ;
}
pBlockTable->close () ;
// Now the block table record is in the database, but is empty
// (has no sub-entity).
// Note that after having been added to the database, an object or an entity
// is implicitely opened for write.
//
// So we create the sub entities to append to the block
// which will represent a "happy face":
// the block should consist of a round yellow face (circle)
// two blue eyes (circles) and a red mouth (arc)
AcDbCircle *pFace =new AcDbCircle (AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 1.0) ;
AcDbCircle *pLeftEye =new AcDbCircle (AcGePoint3d (0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbCircle *pRightEye =new AcDbCircle (AcGePoint3d (-0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbArc *pMouth =new AcDbArc (AcGePoint3d (0, 0.5, 0), 1.0, 3.141592 + (3.141592 * 0.3), 3.141592 + (3.141592 * 0.7)) ;
// Set the color property.
pFace->setColorIndex (2) ;
pLeftEye->setColorIndex (5) ;
pRightEye->setColorIndex (5) ;
pMouth->setColorIndex (1) ;
// add the entities to the new block table record
if ( (es =pBlockTableRecord->appendAcDbEntity (pFace)) != Acad::eOk ) {
delete pFace ;
delete pLeftEye ;
delete pRightEye ;
delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pFace->close () ; if ( (es =pBlockTableRecord->appendAcDbEntity (pLeftEye)) != Acad::eOk ) {
delete pLeftEye ;
delete pRightEye ;
delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pLeftEye->close () ; if ( (es =pBlockTableRecord->appendAcDbEntity (pRightEye)) != Acad::eOk ) {
delete pRightEye ;
delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pRightEye->close () ; if ( (es =pBlockTableRecord->appendAcDbEntity (pMouth)) != Acad::eOk ) {
delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pMouth->close () ; pBlockTableRecord->close () ; return (Acad::eOk) ;
} 插入块 输入块的名称 void insrtBlk()
{
char blkName[50];
AcDbDatabase *pCurDb;
AcDbBlockTable *pBlkTable;
AcDbBlockTableRecord *pBlkTableRecord;
AcDbBlockReference *pInsrtObj;
AcDbObjectId blkId; AcGePoint3d insPt; int retCode; retCode = acedGetString(0, "\nEnter Block Name: ", blkName);
if(retCode != RTNORM || blkName[0] == '\0')
{
acutPrintf("\nInvalid block name.");
return;
} pCurDb = acdbHostApplicationServices()->workingDatabase(); // Check to see if the block table
// has blkName pCurDb->getBlockTable(pBlkTable, AcDb::kForRead);
if(!pBlkTable->has(blkName))
{
acutPrintf("\nBlock definition %s not found. ", blkName);
pBlkTable->close();
return;
} // Get the AcDbObjectId of the block
// definition.
pBlkTable->getAt(blkName, blkId); pBlkTable->getAt(ACDB_MODEL_SPACE, pBlkTableRecord, AcDb::kForWrite);
pBlkTable->close();

acedInitGet(RSG_NONULL, NULL);
acedGetPoint(NULL, "\nPick insertion point: ", asDblArray(insPt)); pInsrtObj = new AcDbBlockReference(insPt, blkId); // Here is where you can set scale, rotation and other
// properties to the block entity. If you want to
// see the AcDbBlockReference class for more details. pBlkTableRecord->appendAcDbEntity(blkId, pInsrtObj); pBlkTableRecord->close();
pInsrtObj->close();
}
发表于 2004-5-24 15:47:00 | 显示全部楼层
// This is command 'INSERTBLK'
void defineblockwithattrib()
{
AcDbObjectId blockId;
AcGePoint3d basePoint(0.0,0.0,0.0);
double textHeight=3.5;
double textAngle=0.0; int retCode = 0;
AcDbDatabase *pCurDb;
AcDbBlockTable *pBlockTable = NULL;
AcDbBlockTableRecord *pBlockRecord = new AcDbBlockTableRecord;
AcDbObjectId entityId;
AcGeMatrix3d ucsMat; //第一步:设置块定义的块名和基点
pBlockRecord->setName("ROUGHNESS");
pBlockRecord->setOrigin(basePoint);
//写状态下打开块表
pCurDb = acdbHostApplicationServices()->workingDatabase();
pCurDb->getBlockTable(pBlockTable,AcDb::kForWrite); //第二步:增加块表记录到块表
pBlockTable->add(blockId,pBlockRecord); //第三步:创建粗糙度标注符号实体
AcGePoint2d plinePt;
AcGePoint2dArray plinePtArr; plinePt.x=1.4*tan(PI/6.0)*textHeight;
plinePt.y=1.4*textHeight;
plinePtArr.append(plinePt); plinePt.x=-1.4*tan(PI/6.0)*textHeight;
plinePt.y=1.4*textHeight;
plinePtArr.append(plinePt); plinePt.x=0.0;
plinePt.y=0.0;
plinePtArr.append(plinePt); plinePt.x=2.8*tan(PI/6)*textHeight;
plinePt.y=2.8*textHeight;
plinePtArr.append(plinePt); AcDbPolyline *pLine = new AcDbPolyline(4);
for(int idx=0;idx<4;idx++)
{
plinePt=plinePtArr.at(idx);
pLine->addVertexAt(idx,plinePt);
} //把粗糙度标注符号实体加入块表记录中 pBlockRecord->appendAcDbEntity(entityId,pLine);
pLine->close();
//第四步:创建属性定义
AcDbAttributeDefinition *pAttdef
=new AcDbAttributeDefinition; //设置属性定义值 //定义属性的位置
AcGePoint3d attposition(-5.0,6.0,0.0);
pAttdef->setPosition(attposition);
pAttdef->setHeight(textHeight);
pAttdef->setRotation(textAngle);
pAttdef->setHorizontalMode(AcDb::kTextLeft);
pAttdef->setVerticalMode(AcDb::kTextBase);
pAttdef->setPrompt("粗糙度");
pAttdef->setTextString("3.2");
pAttdef->setTag("粗糙度");
pAttdef->setInvisible(Adesk::kFalse);
pAttdef->setVerifiable(Adesk::kFalse);
pAttdef->setPreset(Adesk::kFalse);
pAttdef->setConstant(Adesk::kFalse);
pAttdef->setFieldLength(4); //把属性定义加入块中 pBlockRecord->appendAcDbEntity(entityId,pAttdef);
pAttdef->close();
pBlockRecord->close();
pBlockTable->close();
return;
} // This is command 'INSBLOCK'
void insblock()
{
// TODO: Implement the command
char blkName[50];
AcDbDatabase *pCurDb;
AcDbBlockTable *pBlkTable;
AcDbBlockTableRecord *pBlkTableRecord;
AcDbBlockTableRecord *pBlkDefRecord;
AcDbBlockReference *pInsrtObj;
AcDbEntity *pEnt;
AcDbBlockTableRecordIterator *pIterator;
AcDbAttributeDefinition *pAttDef;
AcDbAttribute *pAtt;
AcDbObjectId blkId;
AcDbObjectId insrtId;
char *pTagPrompt;
AcGePoint3d insPt;
AcGePoint3d basePt;
int retCode;
//第一步:判断块是不是存在
retCode=acedGetString(0,"\nEnter Block Name:",blkName);
if(retCode != RTNORM || blkName[0]=='\0')
{
acutPrintf("\n Invalid block name");
return;
} pCurDb = acdbHostApplicationServices()->workingDatabase();
pCurDb->getBlockTable(pBlkTable,AcDb::kForRead);
if(!pBlkTable->has(blkName))
{
acutPrintf("\nBlock definition %s not found.",blkName);
pBlkTable->close();
return;
} //Get the AcDbObjectId of the block definition.
pBlkTable->getAt(blkName,blkId);
//Get the block record of the model space to add the
//block reference
pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForWrite);
pBlkTable->close(); //第二步:创建块参考实体
acedInitGet(RSG_NONULL,NULL);
acedGetPoint(NULL,"\nPick insertion point:",asDblArray(insPt));
pInsrtObj = new AcDbBlockReference(insPt,blkId); //here is where you can set scale.rotation and other
//properties to the block entity. if you want to
//see the AcDbBlockReference class for more details // pBlkTableRecord->appendAcDbEntity(insrtId,pInsrtObj); //第三步:根据块中的属性定义来定义一个属性实体,并把属性实体加入块参考对象中
acdbOpenObject(pBlkDefRecord,blkId,AcDb::kForWrite);
//Now check to see if the block Definition
//has attributes.if if does we will add
//a Block Table Record Iterator to step through
//the entity and find the attribute Definitions. if(pBlkDefRecord->hasAttributeDefinitions())
{
pBlkDefRecord->newIterator(pIterator);
//check to see if the entity is a attribue definition.
for(pIterator->start();!pIterator->done();
pIterator->step())
{
pIterator->getEntity(pEnt,AcDb::kForRead);
//check to see if the entity is an attribute definition
pAttDef = AcDbAttributeDefinition::cast(pEnt);
if(pAttDef != NULL && !pAttDef->isConstant())
{
//if it is and it's not constant
//create a new attribute
pAtt = new AcDbAttribute(); //setPropertiesFrom will copy Color.
//Layer,Linetype,Linetype Scale and Visiblity
pAtt->setPropertiesFrom(pAttDef);
//setup more properties from the attribute definition
pAtt->setInvisible(pAttDef->isInvisible());
basePt = pAttDef->position();
basePt += pInsrtObj->position().asVector();
pAtt->setPosition(basePt);
pAtt->setHeight(pAttDef->height());
pAtt->setRotation(pAttDef->rotation()); //take note how we get the tag.
pTagPrompt = pAttDef->tag();
pAtt->setTag(pTagPrompt);
free(pTagPrompt);
//normally you would prompt the user
//and ask for input values
pTagPrompt = pAttDef->prompt();
acutPrintf("%s%s","\n",pTagPrompt);
free(pTagPrompt); //The setFieldLength is not required
//even though it is listed in the documentation
pAtt->setFieldLength(25); //setTextString is the value the
//attribute recieves which would
//normally be a user input value.
pAtt->setTextString("6.4");
//将属性加入块参考对象中去
pInsrtObj->appendAttribute(pAtt);
pAtt->close();
}//end if
pEnt->close();
}//end for
} //end if
pBlkTableRecord->appendAcDbEntity(insrtId,pInsrtObj);
delete pIterator;
//Note that we close the Model space block
//table record after we have added our attribute
pBlkTableRecord->close();
pBlkDefRecord->close();
pInsrtObj->close(); }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 05:38 , Processed in 0.149061 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表